Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 1 | //===--- HTMLDiagnostics.cpp - HTML Diagnostics for Paths ----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the HTMLDiagnostics object. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/PathDiagnosticClients.h" |
Ted Kremenek | 6b67630 | 2010-01-25 17:10:22 +0000 | [diff] [blame] | 15 | #include "clang/Checker/BugReporter/PathDiagnostic.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Decl.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 19 | #include "clang/Basic/FileManager.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 20 | #include "clang/Rewrite/Rewriter.h" |
| 21 | #include "clang/Rewrite/HTMLRewrite.h" |
| 22 | #include "clang/Lex/Lexer.h" |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 26 | #include "llvm/System/Path.h" |
Ted Kremenek | b8fc325 | 2009-10-08 17:44:41 +0000 | [diff] [blame] | 27 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | // Boilerplate. |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | namespace { |
| 35 | |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 36 | class HTMLDiagnostics : public PathDiagnosticClient { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 37 | llvm::sys::Path Directory, FilePrefix; |
| 38 | bool createdDir, noDir; |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 39 | const Preprocessor &PP; |
Ted Kremenek | f755606 | 2009-07-27 22:13:39 +0000 | [diff] [blame] | 40 | std::vector<const PathDiagnostic*> BatchedDiags; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 41 | public: |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 42 | HTMLDiagnostics(const std::string& prefix, const Preprocessor &pp); |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 43 | |
| 44 | virtual ~HTMLDiagnostics() { FlushDiagnostics(NULL); } |
| 45 | |
| 46 | virtual void FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 48 | virtual void HandlePathDiagnostic(const PathDiagnostic* D); |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 49 | |
| 50 | virtual llvm::StringRef getName() const { |
| 51 | return "HTMLDiagnostics"; |
| 52 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 54 | unsigned ProcessMacroPiece(llvm::raw_ostream& os, |
| 55 | const PathDiagnosticMacroPiece& P, |
| 56 | unsigned num); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 58 | void HandlePiece(Rewriter& R, FileID BugFileID, |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 59 | const PathDiagnosticPiece& P, unsigned num, unsigned max); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 61 | void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range, |
| 62 | const char *HighlightStart = "<span class=\"mrange\">", |
| 63 | const char *HighlightEnd = "</span>"); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 64 | |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 65 | void ReportDiag(const PathDiagnostic& D, |
| 66 | llvm::SmallVectorImpl<std::string> *FilesMade); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 67 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 69 | } // end anonymous namespace |
| 70 | |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 71 | HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, |
| 72 | const Preprocessor &pp) |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 73 | : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false), |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 74 | PP(pp) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | // All html files begin with "report" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 76 | FilePrefix.appendComponent("report"); |
| 77 | } |
| 78 | |
| 79 | PathDiagnosticClient* |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 80 | clang::CreateHTMLDiagnosticClient(const std::string& prefix, |
| 81 | const Preprocessor &PP) { |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 82 | return new HTMLDiagnostics(prefix, PP); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | //===----------------------------------------------------------------------===// |
| 86 | // Report processing. |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 89 | void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) { |
| 90 | if (!D) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 91 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 93 | if (D->empty()) { |
| 94 | delete D; |
| 95 | return; |
| 96 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | 7db0a94 | 2009-04-02 05:17:38 +0000 | [diff] [blame] | 98 | const_cast<PathDiagnostic*>(D)->flattenLocations(); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 99 | BatchedDiags.push_back(D); |
| 100 | } |
| 101 | |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 102 | void |
| 103 | HTMLDiagnostics::FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade) |
| 104 | { |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 105 | while (!BatchedDiags.empty()) { |
| 106 | const PathDiagnostic* D = BatchedDiags.back(); |
| 107 | BatchedDiags.pop_back(); |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 108 | ReportDiag(*D, FilesMade); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 109 | delete D; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | } |
Ted Kremenek | cca5e1e | 2009-11-13 03:02:57 +0000 | [diff] [blame] | 111 | |
| 112 | BatchedDiags.clear(); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 115 | void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D, |
| 116 | llvm::SmallVectorImpl<std::string> *FilesMade){ |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 117 | // Create the HTML directory if it is missing. |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 118 | if (!createdDir) { |
| 119 | createdDir = true; |
Ted Kremenek | 344f7e3 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 120 | std::string ErrorMsg; |
| 121 | Directory.createDirectoryOnDisk(true, &ErrorMsg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 123 | if (!Directory.isDirectory()) { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 124 | llvm::errs() << "warning: could not create directory '" |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 125 | << Directory.str() << "'\n" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | << "reason: " << ErrorMsg << '\n'; |
| 127 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 128 | noDir = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 134 | if (noDir) |
| 135 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 137 | const SourceManager &SMgr = D.begin()->getLocation().getManager(); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 138 | FileID FID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 140 | // Verify that the entire path is from the same FileID. |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 141 | for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) { |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 142 | FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 144 | if (FID.isInvalid()) { |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 145 | FID = SMgr.getFileID(L); |
| 146 | } else if (SMgr.getFileID(L) != FID) |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 147 | return; // FIXME: Emit a warning? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 149 | // Check the source ranges. |
| 150 | for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(), |
| 151 | RE=I->ranges_end(); RI!=RE; ++RI) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 153 | SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin()); |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 154 | |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 155 | if (!L.isFileID() || SMgr.getFileID(L) != FID) |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 156 | return; // FIXME: Emit a warning? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 158 | L = SMgr.getInstantiationLoc(RI->getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 160 | if (!L.isFileID() || SMgr.getFileID(L) != FID) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | return; // FIXME: Emit a warning? |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 165 | if (FID.isInvalid()) |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 166 | return; // FIXME: Emit a warning? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame] | 168 | // Create a new rewriter to generate HTML. |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 169 | Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOptions()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | |
| 171 | // Process the path. |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 172 | unsigned n = D.size(); |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 173 | unsigned max = n; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 175 | for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend(); |
Chris Lattner | 409d4e7 | 2009-04-17 20:40:01 +0000 | [diff] [blame] | 176 | I!=E; ++I, --n) |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 177 | HandlePiece(R, FID, *I, n, max); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 179 | // Add line numbers, header, footer, etc. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 181 | // unsigned FID = R.getSourceMgr().getMainFileID(); |
| 182 | html::EscapeText(R, FID); |
| 183 | html::AddLineNumbers(R, FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 185 | // If we have a preprocessor, relex the file and syntax highlight. |
| 186 | // We might not have a preprocessor if we come from a deserialized AST file, |
| 187 | // for example. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 189 | html::SyntaxHighlight(R, FID, PP); |
| 190 | html::HighlightMacros(R, FID, PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 192 | // Get the full directory name of the analyzed file. |
| 193 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 194 | const FileEntry* Entry = SMgr.getFileEntryForID(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | 7fc8957 | 2008-04-24 23:37:03 +0000 | [diff] [blame] | 196 | // This is a cludge; basically we want to append either the full |
| 197 | // working directory if we have no directory information. This is |
| 198 | // a work in progress. |
| 199 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 200 | std::string DirName = ""; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 202 | if (!llvm::sys::Path(Entry->getName()).isAbsolute()) { |
| 203 | llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory(); |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 204 | DirName = P.str() + "/"; |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 205 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
| 207 | // Add the name of the file as an <h1> tag. |
| 208 | |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 209 | { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 210 | std::string s; |
| 211 | llvm::raw_string_ostream os(s); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
Ted Kremenek | 778246a | 2008-09-22 17:33:32 +0000 | [diff] [blame] | 213 | os << "<!-- REPORTHEADER -->\n" |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 214 | << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n" |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 215 | "<tr><td class=\"rowname\">File:</td><td>" |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 216 | << html::EscapeText(DirName) |
| 217 | << html::EscapeText(Entry->getName()) |
| 218 | << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | "<a href=\"#EndPath\">line " |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 220 | << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber() |
| 221 | << ", column " |
| 222 | << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber() |
| 223 | << "</a></td></tr>\n" |
| 224 | "<tr><td class=\"rowname\">Description:</td><td>" |
| 225 | << D.getDescription() << "</td></tr>\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 227 | // Output any other meta data. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 229 | for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end(); |
| 230 | I!=E; ++I) { |
| 231 | os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n"; |
| 232 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | 778246a | 2008-09-22 17:33:32 +0000 | [diff] [blame] | 234 | os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | "<h3>Annotated Source Code</h3>\n"; |
| 236 | |
Daniel Dunbar | 44ba7bf | 2009-08-19 20:32:38 +0000 | [diff] [blame] | 237 | R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str()); |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 238 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 240 | // Embed meta-data tags. |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 241 | { |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 242 | std::string s; |
| 243 | llvm::raw_string_ostream os(s); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
| 245 | const std::string& BugDesc = D.getDescription(); |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 246 | if (!BugDesc.empty()) |
| 247 | os << "\n<!-- BUGDESC " << BugDesc << " -->\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 249 | const std::string& BugType = D.getBugType(); |
| 250 | if (!BugType.empty()) |
| 251 | os << "\n<!-- BUGTYPE " << BugType << " -->\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
| 253 | const std::string& BugCategory = D.getCategory(); |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 254 | if (!BugCategory.empty()) |
| 255 | os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n"; |
| 256 | |
Ted Kremenek | 7fc8957 | 2008-04-24 23:37:03 +0000 | [diff] [blame] | 257 | os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n"; |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 258 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 259 | os << "\n<!-- BUGLINE " |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 260 | << D.back()->getLocation().asLocation().getInstantiationLineNumber() |
| 261 | << " -->\n"; |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 262 | |
| 263 | os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 265 | // Mark the end of the tags. |
| 266 | os << "\n<!-- BUGMETAEND -->\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Ted Kremenek | 9b4d45c | 2009-08-03 23:44:55 +0000 | [diff] [blame] | 268 | // Insert the text. |
Daniel Dunbar | 44ba7bf | 2009-08-19 20:32:38 +0000 | [diff] [blame] | 269 | R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str()); |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 270 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 272 | // Add CSS, header, and footer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 274 | html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 276 | // Get the rewrite buffer. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 277 | const RewriteBuffer *Buf = R.getRewriteBufferFor(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 279 | if (!Buf) { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 280 | llvm::errs() << "warning: no diagnostics generated for main file.\n"; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 281 | return; |
| 282 | } |
| 283 | |
Ted Kremenek | b8fc325 | 2009-10-08 17:44:41 +0000 | [diff] [blame] | 284 | // Create a path for the target HTML file. |
| 285 | llvm::sys::Path F(FilePrefix); |
| 286 | F.makeUnique(false, NULL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | b8fc325 | 2009-10-08 17:44:41 +0000 | [diff] [blame] | 288 | // Rename the file with an HTML extension. |
| 289 | llvm::sys::Path H(F); |
| 290 | H.appendSuffix("html"); |
| 291 | F.renamePathOnDisk(H, NULL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | |
Ted Kremenek | b8fc325 | 2009-10-08 17:44:41 +0000 | [diff] [blame] | 293 | std::string ErrorMsg; |
| 294 | llvm::raw_fd_ostream os(H.c_str(), ErrorMsg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | b8fc325 | 2009-10-08 17:44:41 +0000 | [diff] [blame] | 296 | if (!ErrorMsg.empty()) { |
| 297 | (llvm::errs() << "warning: could not create file '" << F.str() |
| 298 | << "'\n").flush(); |
| 299 | return; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 300 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | |
Ted Kremenek | b8fc325 | 2009-10-08 17:44:41 +0000 | [diff] [blame] | 302 | if (FilesMade) |
| 303 | FilesMade->push_back(H.getLast()); |
| 304 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 305 | // Emit the HTML to disk. |
Ted Kremenek | 7414dc0 | 2008-04-20 01:02:33 +0000 | [diff] [blame] | 306 | for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | os << *I; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 310 | void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID, |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 311 | const PathDiagnosticPiece& P, |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 312 | unsigned num, unsigned max) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 314 | // For now, just draw a box above the line in question, and emit the |
| 315 | // warning. |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 316 | FullSourceLoc Pos = P.getLocation().asLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 318 | if (!Pos.isValid()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | return; |
| 320 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 321 | SourceManager &SM = R.getSourceMgr(); |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 322 | assert(&Pos.getManager() == &SM && "SourceManagers are different!"); |
| 323 | std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 325 | if (LPosInfo.first != BugFileID) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 326 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 328 | const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | const char* FileStart = Buf->getBufferStart(); |
| 330 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 331 | // Compute the column number. Rewind from the current position to the start |
| 332 | // of the line. |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 333 | unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second); |
| 334 | const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 335 | const char *LineStart = TokInstantiationPtr-ColNo; |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | 3f0b656 | 2009-02-18 22:10:00 +0000 | [diff] [blame] | 337 | // Compute LineEnd. |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 338 | const char *LineEnd = TokInstantiationPtr; |
Ted Kremenek | 3f0b656 | 2009-02-18 22:10:00 +0000 | [diff] [blame] | 339 | const char* FileEnd = Buf->getBufferEnd(); |
| 340 | while (*LineEnd != '\n' && LineEnd != FileEnd) |
| 341 | ++LineEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | |
Ted Kremenek | 2aa13b5 | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 343 | // Compute the margin offset by counting tabs and non-tabs. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | unsigned PosNo = 0; |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 345 | for (const char* c = LineStart; c != TokInstantiationPtr; ++c) |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 346 | PosNo += *c == '\t' ? 8 : 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 348 | // Create the html for the message. |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 349 | |
| 350 | const char *Kind = 0; |
| 351 | switch (P.getKind()) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 352 | case PathDiagnosticPiece::Event: Kind = "Event"; break; |
| 353 | case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break; |
| 354 | // Setting Kind to "Control" is intentional. |
| 355 | case PathDiagnosticPiece::Macro: Kind = "Control"; break; |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 356 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 358 | std::string sbuf; |
| 359 | llvm::raw_string_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 361 | os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\""; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 363 | if (num == max) |
| 364 | os << "EndPath"; |
| 365 | else |
| 366 | os << "Path" << num; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 368 | os << "\" class=\"msg"; |
| 369 | if (Kind) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | os << " msg" << Kind; |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 371 | os << "\" style=\"margin-left:" << PosNo << "ex"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 373 | // Output a maximum size. |
| 374 | if (!isa<PathDiagnosticMacroPiece>(P)) { |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 375 | // Get the string and determining its maximum substring. |
| 376 | const std::string& Msg = P.getString(); |
| 377 | unsigned max_token = 0; |
| 378 | unsigned cnt = 0; |
| 379 | unsigned len = Msg.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 381 | for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I) |
| 382 | switch (*I) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 383 | default: |
| 384 | ++cnt; |
| 385 | continue; |
| 386 | case ' ': |
| 387 | case '\t': |
| 388 | case '\n': |
| 389 | if (cnt > max_token) max_token = cnt; |
| 390 | cnt = 0; |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 391 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 393 | if (cnt > max_token) |
| 394 | max_token = cnt; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 396 | // Determine the approximate size of the message bubble in em. |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 397 | unsigned em; |
Ted Kremenek | 4e25ee3 | 2009-03-02 23:06:15 +0000 | [diff] [blame] | 398 | const unsigned max_line = 120; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 400 | if (max_token >= max_line) |
| 401 | em = max_token / 2; |
| 402 | else { |
| 403 | unsigned characters = max_line; |
| 404 | unsigned lines = len / max_line; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 406 | if (lines > 0) { |
| 407 | for (; characters > max_token; --characters) |
| 408 | if (len / characters > lines) { |
| 409 | ++characters; |
| 410 | break; |
| 411 | } |
| 412 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 414 | em = characters / 2; |
| 415 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 417 | if (em < max_line/2) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | os << "; max-width:" << em << "em"; |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 419 | } |
| 420 | else |
| 421 | os << "; max-width:100em"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 423 | os << "\">"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 425 | if (max > 1) { |
| 426 | os << "<table class=\"msgT\"><tr><td valign=\"top\">"; |
| 427 | os << "<div class=\"PathIndex"; |
| 428 | if (Kind) os << " PathIndex" << Kind; |
| 429 | os << "\">" << num << "</div>"; |
| 430 | os << "</td><td>"; |
| 431 | } |
| 432 | |
| 433 | if (const PathDiagnosticMacroPiece *MP = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | dyn_cast<PathDiagnosticMacroPiece>(&P)) { |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 435 | |
| 436 | os << "Within the expansion of the macro '"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 438 | // Get the name of the macro by relexing it. |
| 439 | { |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 440 | FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc(); |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 441 | assert(L.isFileID()); |
| 442 | std::pair<const char*, const char*> BufferInfo = L.getBufferData(); |
| 443 | const char* MacroName = L.getDecomposedLoc().second + BufferInfo.first; |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 444 | Lexer rawLexer(L, PP.getLangOptions(), BufferInfo.first, |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 445 | MacroName, BufferInfo.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 447 | Token TheTok; |
| 448 | rawLexer.LexFromRawLexer(TheTok); |
| 449 | for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i) |
| 450 | os << MacroName[i]; |
Ted Kremenek | 80bae76 | 2009-03-02 23:05:40 +0000 | [diff] [blame] | 451 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 453 | os << "':\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 455 | if (max > 1) |
Ted Kremenek | 80bae76 | 2009-03-02 23:05:40 +0000 | [diff] [blame] | 456 | os << "</td></tr></table>"; |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 457 | |
| 458 | // Within a macro piece. Write out each event. |
| 459 | ProcessMacroPiece(os, *MP, 0); |
| 460 | } |
| 461 | else { |
| 462 | os << html::EscapeText(P.getString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 464 | if (max > 1) |
| 465 | os << "</td></tr></table>"; |
Ted Kremenek | a6aa83e | 2008-09-21 18:52:59 +0000 | [diff] [blame] | 466 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 468 | os << "</div></td></tr>"; |
| 469 | |
| 470 | // Insert the new html. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | unsigned DisplayPos = LineEnd - FileStart; |
| 472 | SourceLocation Loc = |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 473 | SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos); |
| 474 | |
Daniel Dunbar | 44ba7bf | 2009-08-19 20:32:38 +0000 | [diff] [blame] | 475 | R.InsertTextBefore(Loc, os.str()); |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 476 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | // Now highlight the ranges. |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 478 | for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end(); |
| 479 | I != E; ++I) |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 480 | HighlightRange(R, LPosInfo.first, *I); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 481 | |
| 482 | #if 0 |
| 483 | // If there is a code insertion hint, insert that code. |
| 484 | // FIXME: This code is disabled because it seems to mangle the HTML |
| 485 | // output. I'm leaving it here because it's generally the right idea, |
| 486 | // but needs some help from someone more familiar with the rewriter. |
| 487 | for (const CodeModificationHint *Hint = P.code_modifications_begin(), |
| 488 | *HintEnd = P.code_modifications_end(); |
| 489 | Hint != HintEnd; ++Hint) { |
| 490 | if (Hint->RemoveRange.isValid()) { |
| 491 | HighlightRange(R, LPosInfo.first, Hint->RemoveRange, |
| 492 | "<span class=\"CodeRemovalHint\">", "</span>"); |
| 493 | } |
| 494 | if (Hint->InsertionLoc.isValid()) { |
| 495 | std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true); |
| 496 | EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode |
| 497 | + "</span>"; |
Daniel Dunbar | 44ba7bf | 2009-08-19 20:32:38 +0000 | [diff] [blame] | 498 | R.InsertTextBefore(Hint->InsertionLoc, EscapedCode); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | #endif |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 504 | static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) { |
| 505 | llvm::SmallVector<char, 10> buf; |
| 506 | |
| 507 | do { |
| 508 | unsigned x = n % ('z' - 'a'); |
| 509 | buf.push_back('a' + x); |
| 510 | n = n / ('z' - 'a'); |
| 511 | } while (n); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 513 | assert(!buf.empty()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 514 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 515 | for (llvm::SmallVectorImpl<char>::reverse_iterator I=buf.rbegin(), |
| 516 | E=buf.rend(); I!=E; ++I) |
| 517 | os << *I; |
| 518 | } |
| 519 | |
| 520 | unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os, |
| 521 | const PathDiagnosticMacroPiece& P, |
| 522 | unsigned num) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 523 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 524 | for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end(); |
| 525 | I!=E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 526 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 527 | if (const PathDiagnosticMacroPiece *MP = |
| 528 | dyn_cast<PathDiagnosticMacroPiece>(*I)) { |
| 529 | num = ProcessMacroPiece(os, *MP, num); |
| 530 | continue; |
| 531 | } |
| 532 | |
| 533 | if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) { |
| 534 | os << "<div class=\"msg msgEvent\" style=\"width:94%; " |
| 535 | "margin-left:5px\">" |
| 536 | "<table class=\"msgT\"><tr>" |
| 537 | "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">"; |
| 538 | EmitAlphaCounter(os, num++); |
| 539 | os << "</div></td><td valign=\"top\">" |
| 540 | << html::EscapeText(EP->getString()) |
| 541 | << "</td></tr></table></div>\n"; |
| 542 | } |
| 543 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 545 | return num; |
| 546 | } |
| 547 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 548 | void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID, |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 549 | SourceRange Range, |
| 550 | const char *HighlightStart, |
| 551 | const char *HighlightEnd) { |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 552 | SourceManager &SM = R.getSourceMgr(); |
| 553 | const LangOptions &LangOpts = R.getLangOpts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 555 | SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin()); |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 556 | unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 558 | SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd()); |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 559 | unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 560 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 561 | if (EndLineNo < StartLineNo) |
| 562 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 564 | if (SM.getFileID(InstantiationStart) != BugFileID || |
| 565 | SM.getFileID(InstantiationEnd) != BugFileID) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 566 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 568 | // Compute the column number of the end. |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 569 | unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 570 | unsigned OldEndColNo = EndColNo; |
| 571 | |
| 572 | if (EndColNo) { |
| 573 | // Add in the length of the token, so that we cover multi-char tokens. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 574 | EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 575 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 577 | // Highlight the range. Make the span tag the outermost tag for the |
| 578 | // selected range. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 580 | SourceLocation E = |
| 581 | InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 583 | html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 584 | } |