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