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