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