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 | |
| 14 | #include "HTMLDiagnostics.h" |
| 15 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/Analysis/PathDiagnostic.h" |
| 19 | #include "clang/Rewrite/Rewriter.h" |
| 20 | #include "clang/Rewrite/HTMLRewrite.h" |
| 21 | #include "clang/Lex/Lexer.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/Streams.h" |
| 25 | #include "llvm/System/Path.h" |
| 26 | #include <fstream> |
| 27 | #include <sstream> |
| 28 | |
| 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 | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 41 | PreprocessorFactory* PPF; |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 42 | std::vector<const PathDiagnostic*> BatchedDiags; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 43 | public: |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 44 | HTMLDiagnostics(const std::string& prefix, Preprocessor* pp, |
| 45 | PreprocessorFactory* ppf); |
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 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 49 | virtual void HandlePathDiagnostic(const PathDiagnostic* D); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame^] | 51 | void HandlePiece(Rewriter& R, unsigned BugFileID, |
| 52 | const PathDiagnosticPiece& P, unsigned num, unsigned max); |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame^] | 54 | void HighlightRange(Rewriter& R, unsigned BugFileID, SourceRange Range); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 55 | |
| 56 | void ReportDiag(const PathDiagnostic& D); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | } // end anonymous namespace |
| 60 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 61 | HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp, |
| 62 | PreprocessorFactory* ppf) |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 63 | : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false), |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 64 | PP(pp), PPF(ppf) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 65 | |
| 66 | // All html files begin with "report" |
| 67 | FilePrefix.appendComponent("report"); |
| 68 | } |
| 69 | |
| 70 | PathDiagnosticClient* |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 71 | clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP, |
| 72 | PreprocessorFactory* PPF) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 74 | return new HTMLDiagnostics(prefix, PP, PPF); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | //===----------------------------------------------------------------------===// |
| 78 | // Report processing. |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 81 | void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) { |
| 82 | if (!D) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 83 | return; |
| 84 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 85 | if (D->empty()) { |
| 86 | delete D; |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | BatchedDiags.push_back(D); |
| 91 | } |
| 92 | |
| 93 | HTMLDiagnostics::~HTMLDiagnostics() { |
| 94 | |
| 95 | while (!BatchedDiags.empty()) { |
| 96 | const PathDiagnostic* D = BatchedDiags.back(); |
| 97 | BatchedDiags.pop_back(); |
| 98 | ReportDiag(*D); |
| 99 | delete D; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) { |
| 104 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 105 | // Create the HTML directory if it is missing. |
| 106 | |
| 107 | if (!createdDir) { |
| 108 | createdDir = true; |
Ted Kremenek | 344f7e3 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 109 | std::string ErrorMsg; |
| 110 | Directory.createDirectoryOnDisk(true, &ErrorMsg); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 111 | |
| 112 | if (!Directory.isDirectory()) { |
| 113 | llvm::cerr << "warning: could not create directory '" |
Ted Kremenek | 344f7e3 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 114 | << Directory.toString() << "'\n" |
| 115 | << "reason: " << ErrorMsg << '\n'; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 116 | |
| 117 | noDir = true; |
| 118 | |
| 119 | return; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (noDir) |
| 124 | return; |
| 125 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 126 | SourceManager& SMgr = D.begin()->getLocation().getManager(); |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame^] | 127 | |
| 128 | unsigned FileID = 0; |
| 129 | bool FileIDInitialized = false; |
| 130 | |
| 131 | // Verify that the entire path is from the same FileID. |
| 132 | for (PathDiagnostic::const_iterator I=D.begin(), E=D.end(); I != E; ++I) { |
| 133 | |
| 134 | FullSourceLoc L = I->getLocation(); |
| 135 | |
| 136 | if (!L.isFileID()) |
| 137 | return; // FIXME: Emit a warning? |
| 138 | |
| 139 | if (!FileIDInitialized) { |
| 140 | FileID = L.getCanonicalFileID(); |
| 141 | FileIDInitialized = true; |
| 142 | } |
| 143 | else if (L.getCanonicalFileID() != FileID) |
| 144 | return; // FIXME: Emit a warning? |
| 145 | |
| 146 | // Check the source ranges. |
| 147 | for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(), |
| 148 | RE=I->ranges_end(); RI!=RE; ++RI) { |
| 149 | |
| 150 | SourceLocation L = RI->getBegin(); |
| 151 | |
| 152 | if (!L.isFileID()) |
| 153 | return; // FIXME: Emit a warning? |
| 154 | |
| 155 | if (SMgr.getCanonicalFileID(L) != FileID) |
| 156 | return; // FIXME: Emit a warning? |
| 157 | |
| 158 | L = RI->getEnd(); |
| 159 | |
| 160 | if (!L.isFileID()) |
| 161 | return; // FIXME: Emit a warning? |
| 162 | |
| 163 | if (SMgr.getCanonicalFileID(L) != FileID) |
| 164 | return; // FIXME: Emit a warning? |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (!FileIDInitialized) |
| 169 | return; // FIXME: Emit a warning? |
| 170 | |
| 171 | // Create a new rewriter to generate HTML. |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 172 | Rewriter R(SMgr); |
| 173 | |
| 174 | // Process the path. |
| 175 | |
| 176 | unsigned n = D.size(); |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 177 | unsigned max = n; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 178 | |
| 179 | for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend(); |
| 180 | I!=E; ++I, --n) { |
| 181 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame^] | 182 | HandlePiece(R, FileID, *I, n, max); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // Add line numbers, header, footer, etc. |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame^] | 187 | // unsigned FileID = R.getSourceMgr().getMainFileID(); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 188 | html::EscapeText(R, FileID); |
| 189 | html::AddLineNumbers(R, FileID); |
| 190 | |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 191 | // If we have a preprocessor, relex the file and syntax highlight. |
| 192 | // We might not have a preprocessor if we come from a deserialized AST file, |
| 193 | // for example. |
| 194 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 195 | if (PP) html::SyntaxHighlight(R, FileID, *PP); |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 196 | |
| 197 | // FIXME: We eventually want to use PPF to create a fresh Preprocessor, |
| 198 | // once we have worked out the bugs. |
| 199 | // |
| 200 | // if (PPF) html::HighlightMacros(R, FileID, *PPF); |
| 201 | // |
| 202 | if (PP) html::HighlightMacros(R, FileID, *PP); |
Ted Kremenek | 47abe76 | 2008-04-16 16:39:56 +0000 | [diff] [blame] | 203 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 204 | // Get the full directory name of the analyzed file. |
| 205 | |
| 206 | const FileEntry* Entry = SMgr.getFileEntryForID(FileID); |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 207 | |
Ted Kremenek | 7fc8957 | 2008-04-24 23:37:03 +0000 | [diff] [blame] | 208 | // This is a cludge; basically we want to append either the full |
| 209 | // working directory if we have no directory information. This is |
| 210 | // a work in progress. |
| 211 | |
Ted Kremenek | 7a4648d | 2008-05-02 22:04:53 +0000 | [diff] [blame] | 212 | std::string DirName = ""; |
| 213 | |
| 214 | if (!llvm::sys::Path(Entry->getName()).isAbsolute()) { |
| 215 | llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory(); |
| 216 | DirName = P.toString() + "/"; |
| 217 | } |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 219 | // Add the name of the file as an <h1> tag. |
| 220 | |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 221 | { |
| 222 | std::ostringstream os; |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 224 | os << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n" |
| 225 | "<tr><td class=\"rowname\">File:</td><td>" |
| 226 | << html::EscapeText(DirName) |
| 227 | << html::EscapeText(Entry->getName()) |
| 228 | << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>" |
| 229 | "<a href=\"#EndPath\">line " |
| 230 | << (*D.rbegin()).getLocation().getLogicalLineNumber() |
| 231 | << ", column " |
| 232 | << (*D.rbegin()).getLocation().getLogicalColumnNumber() |
| 233 | << "</a></td></tr>\n" |
| 234 | "<tr><td class=\"rowname\">Description:</td><td>" |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 235 | << D.getDescription() << "</td></tr>\n"; |
| 236 | |
| 237 | // Output any other meta data. |
| 238 | |
| 239 | for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end(); |
| 240 | I!=E; ++I) { |
| 241 | os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n"; |
| 242 | } |
| 243 | |
| 244 | os << "</table>\n<h3>Annotated Source Code</h3>\n"; |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 245 | |
Ted Kremenek | 2e93981 | 2008-03-27 07:35:49 +0000 | [diff] [blame] | 246 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 249 | // Embed meta-data tags. |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 250 | |
| 251 | const std::string& BugDesc = D.getDescription(); |
| 252 | |
| 253 | if (!BugDesc.empty()) { |
| 254 | std::ostringstream os; |
Ted Kremenek | 86b4381 | 2008-04-02 18:03:20 +0000 | [diff] [blame] | 255 | os << "\n<!-- BUGDESC " << BugDesc << " -->\n"; |
Ted Kremenek | 0761540 | 2008-04-02 07:04:46 +0000 | [diff] [blame] | 256 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | { |
| 260 | std::ostringstream os; |
Ted Kremenek | 7fc8957 | 2008-04-24 23:37:03 +0000 | [diff] [blame] | 261 | os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n"; |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 262 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 263 | } |
| 264 | |
| 265 | { |
| 266 | std::ostringstream os; |
Ted Kremenek | 344f7e3 | 2008-04-03 17:55:57 +0000 | [diff] [blame] | 267 | os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber() |
Ted Kremenek | b947639 | 2008-04-02 20:44:16 +0000 | [diff] [blame] | 268 | << " -->\n"; |
| 269 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 270 | } |
| 271 | |
| 272 | { |
| 273 | std::ostringstream os; |
| 274 | os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n"; |
| 275 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); |
| 276 | } |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 277 | |
| 278 | // Add CSS, header, and footer. |
| 279 | |
Ted Kremenek | f6f593f | 2008-07-07 18:31:05 +0000 | [diff] [blame] | 280 | html::AddHeaderFooterInternalBuiltinCSS(R, FileID, Entry->getName()); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 281 | |
| 282 | // Get the rewrite buffer. |
| 283 | const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID); |
| 284 | |
| 285 | if (!Buf) { |
| 286 | llvm::cerr << "warning: no diagnostics generated for main file.\n"; |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | // Create the stream to write out the HTML. |
| 291 | std::ofstream os; |
| 292 | |
| 293 | { |
| 294 | // Create a path for the target HTML file. |
| 295 | llvm::sys::Path F(FilePrefix); |
| 296 | F.makeUnique(false, NULL); |
| 297 | |
| 298 | // Rename the file with an HTML extension. |
| 299 | llvm::sys::Path H(F); |
| 300 | H.appendSuffix("html"); |
| 301 | F.renamePathOnDisk(H, NULL); |
| 302 | |
| 303 | os.open(H.toString().c_str()); |
| 304 | |
| 305 | if (!os) { |
| 306 | llvm::cerr << "warning: could not create file '" << F.toString() << "'\n"; |
| 307 | return; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Emit the HTML to disk. |
| 312 | |
Ted Kremenek | 7414dc0 | 2008-04-20 01:02:33 +0000 | [diff] [blame] | 313 | for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I) |
Ted Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 314 | os << *I; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame^] | 317 | void HTMLDiagnostics::HandlePiece(Rewriter& R, unsigned BugFileID, |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 318 | const PathDiagnosticPiece& P, |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 319 | unsigned num, unsigned max) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 320 | |
| 321 | // For now, just draw a box above the line in question, and emit the |
| 322 | // warning. |
| 323 | |
| 324 | FullSourceLoc Pos = P.getLocation(); |
| 325 | |
| 326 | if (!Pos.isValid()) |
| 327 | return; |
| 328 | |
| 329 | SourceManager& SM = R.getSourceMgr(); |
| 330 | FullSourceLoc LPos = Pos.getLogicalLoc(); |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 331 | unsigned FileID = SM.getCanonicalFileID(LPos.getLocation()); |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 333 | assert (&LPos.getManager() == &SM && "SourceManagers are different!"); |
| 334 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame^] | 335 | if (LPos.getCanonicalFileID() != BugFileID) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 336 | return; |
| 337 | |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 338 | const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID); |
| 339 | const char* FileStart = Buf->getBufferStart(); |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 340 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 341 | // Compute the column number. Rewind from the current position to the start |
| 342 | // of the line. |
| 343 | |
| 344 | unsigned ColNo = LPos.getColumnNumber(); |
| 345 | const char *TokLogicalPtr = LPos.getCharacterData(); |
| 346 | const char *LineStart = TokLogicalPtr-ColNo; |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 347 | |
| 348 | // Only compute LineEnd if we display below a line. |
| 349 | const char *LineEnd = TokLogicalPtr; |
| 350 | |
| 351 | if (P.getDisplayHint() == PathDiagnosticPiece::Below) { |
| 352 | const char* FileEnd = Buf->getBufferEnd(); |
| 353 | |
| 354 | while (*LineEnd != '\n' && LineEnd != FileEnd) |
| 355 | ++LineEnd; |
| 356 | } |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | 2aa13b5 | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 358 | // Compute the margin offset by counting tabs and non-tabs. |
| 359 | |
| 360 | unsigned PosNo = 0; |
| 361 | |
| 362 | for (const char* c = LineStart; c != TokLogicalPtr; ++c) |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 363 | PosNo += *c == '\t' ? 8 : 1; |
Ted Kremenek | 2aa13b5 | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 364 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 365 | // Create the html for the message. |
| 366 | |
| 367 | std::ostringstream os; |
| 368 | |
| 369 | os << "\n<tr><td class=\"num\"></td><td class=\"line\">" |
Ted Kremenek | 33bd942 | 2008-03-31 23:30:12 +0000 | [diff] [blame] | 370 | << "<div id=\""; |
| 371 | |
| 372 | if (num == max) |
| 373 | os << "EndPath"; |
| 374 | else |
| 375 | os << "Path" << num; |
| 376 | |
| 377 | os << "\" class=\"msg\" style=\"margin-left:" |
Ted Kremenek | 2aa13b5 | 2008-03-31 21:40:14 +0000 | [diff] [blame] | 378 | << PosNo << "ex\">"; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 379 | |
Ted Kremenek | 718ceb1 | 2008-04-02 21:04:20 +0000 | [diff] [blame] | 380 | if (max > 1) |
| 381 | os << "<span class=\"PathIndex\">[" << num << "]</span> "; |
| 382 | |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 383 | os << html::EscapeText(P.getString()) << "</div></td></tr>"; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 384 | |
| 385 | // Insert the new html. |
| 386 | |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 387 | unsigned DisplayPos = 0; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 388 | |
Ted Kremenek | ea17d6a | 2008-05-06 23:42:18 +0000 | [diff] [blame] | 389 | switch (P.getDisplayHint()) { |
| 390 | case PathDiagnosticPiece::Above: |
| 391 | DisplayPos = LineStart - FileStart; |
| 392 | break; |
| 393 | case PathDiagnosticPiece::Below: |
| 394 | DisplayPos = LineEnd - FileStart; |
| 395 | break; |
| 396 | default: |
| 397 | assert (false && "Unhandled hint."); |
| 398 | } |
| 399 | |
| 400 | R.InsertStrBefore(SourceLocation::getFileLoc(FileID, DisplayPos), os.str()); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 401 | |
| 402 | // Now highlight the ranges. |
| 403 | |
| 404 | for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end(); |
| 405 | I != E; ++I) |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame^] | 406 | HighlightRange(R, FileID, *I); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame^] | 409 | void HTMLDiagnostics::HighlightRange(Rewriter& R, unsigned BugFileID, |
| 410 | SourceRange Range) { |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 411 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 412 | SourceManager& SM = R.getSourceMgr(); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 413 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 414 | SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin()); |
| 415 | unsigned StartLineNo = SM.getLineNumber(LogicalStart); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 416 | |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 417 | SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd()); |
| 418 | unsigned EndLineNo = SM.getLineNumber(LogicalEnd); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 419 | |
| 420 | if (EndLineNo < StartLineNo) |
| 421 | return; |
| 422 | |
Ted Kremenek | fd8fc4e | 2008-07-23 23:18:15 +0000 | [diff] [blame^] | 423 | if (!SM.getCanonicalFileID(LogicalStart) != BugFileID || |
| 424 | !SM.getCanonicalFileID(LogicalEnd) != BugFileID) |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 425 | return; |
| 426 | |
| 427 | // Compute the column number of the end. |
Ted Kremenek | 5dd0041 | 2008-04-14 21:06:04 +0000 | [diff] [blame] | 428 | unsigned EndColNo = SM.getColumnNumber(LogicalEnd); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 429 | unsigned OldEndColNo = EndColNo; |
| 430 | |
| 431 | if (EndColNo) { |
| 432 | // Add in the length of the token, so that we cover multi-char tokens. |
Ted Kremenek | 12fc014 | 2008-04-18 05:01:33 +0000 | [diff] [blame] | 433 | EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | // Highlight the range. Make the span tag the outermost tag for the |
| 437 | // selected range. |
Ted Kremenek | dab4ead | 2008-04-08 21:29:14 +0000 | [diff] [blame] | 438 | |
Ted Kremenek | 12fc014 | 2008-04-18 05:01:33 +0000 | [diff] [blame] | 439 | SourceLocation E = LogicalEnd.getFileLocWithOffset(EndColNo - OldEndColNo); |
Ted Kremenek | b747814 | 2008-04-17 18:37:23 +0000 | [diff] [blame] | 440 | |
| 441 | html::HighlightRange(R, LogicalStart, E, |
| 442 | "<span class=\"mrange\">", "</span>"); |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 443 | } |