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