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