1 //===--- HTMLDiagnostics.cpp - HTML Diagnostics for Paths ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the HTMLDiagnostics object.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Lex/Lexer.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "clang/Rewrite/Core/HTMLRewrite.h"
21 #include "clang/Rewrite/Core/Rewriter.h"
22 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
23 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
24 #include "clang/StaticAnalyzer/Core/IssueHash.h"
25 #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
26 #include "llvm/Support/Errc.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/raw_ostream.h"
33 using namespace clang;
36 //===----------------------------------------------------------------------===//
38 //===----------------------------------------------------------------------===//
42 class HTMLDiagnostics : public PathDiagnosticConsumer {
43 std::string Directory;
44 bool createdDir, noDir;
45 const Preprocessor &PP;
46 AnalyzerOptions &AnalyzerOpts;
47 const bool SupportsCrossFileDiagnostics;
49 HTMLDiagnostics(AnalyzerOptions &AnalyzerOpts,
50 const std::string& prefix,
51 const Preprocessor &pp,
52 bool supportsMultipleFiles);
54 ~HTMLDiagnostics() override { FlushDiagnostics(nullptr); }
56 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
57 FilesMade *filesMade) override;
59 StringRef getName() const override {
60 return "HTMLDiagnostics";
63 bool supportsCrossFileDiagnostics() const override {
64 return SupportsCrossFileDiagnostics;
67 unsigned ProcessMacroPiece(raw_ostream &os,
68 const PathDiagnosticMacroPiece& P,
71 void HandlePiece(Rewriter& R, FileID BugFileID,
72 const PathDiagnosticPiece& P, unsigned num, unsigned max);
74 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
75 const char *HighlightStart = "<span class=\"mrange\">",
76 const char *HighlightEnd = "</span>");
78 void ReportDiag(const PathDiagnostic& D,
79 FilesMade *filesMade);
81 // Generate the full HTML report
82 std::string GenerateHTML(const PathDiagnostic& D, Rewriter &R,
83 const SourceManager& SMgr, const PathPieces& path,
84 const char *declName);
86 // Add HTML header/footers to file specified by FID
87 void FinalizeHTML(const PathDiagnostic& D, Rewriter &R,
88 const SourceManager& SMgr, const PathPieces& path,
89 FileID FID, const FileEntry *Entry, const char *declName);
91 // Rewrite the file specified by FID with HTML formatting.
92 void RewriteFile(Rewriter &R, const SourceManager& SMgr,
93 const PathPieces& path, FileID FID);
96 } // end anonymous namespace
98 HTMLDiagnostics::HTMLDiagnostics(AnalyzerOptions &AnalyzerOpts,
99 const std::string& prefix,
100 const Preprocessor &pp,
101 bool supportsMultipleFiles)
106 AnalyzerOpts(AnalyzerOpts),
107 SupportsCrossFileDiagnostics(supportsMultipleFiles) {}
109 void ento::createHTMLDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
110 PathDiagnosticConsumers &C,
111 const std::string& prefix,
112 const Preprocessor &PP) {
113 C.push_back(new HTMLDiagnostics(AnalyzerOpts, prefix, PP, true));
116 void ento::createHTMLSingleFileDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
117 PathDiagnosticConsumers &C,
118 const std::string& prefix,
119 const Preprocessor &PP) {
120 C.push_back(new HTMLDiagnostics(AnalyzerOpts, prefix, PP, false));
123 //===----------------------------------------------------------------------===//
124 // Report processing.
125 //===----------------------------------------------------------------------===//
127 void HTMLDiagnostics::FlushDiagnosticsImpl(
128 std::vector<const PathDiagnostic *> &Diags,
129 FilesMade *filesMade) {
130 for (std::vector<const PathDiagnostic *>::iterator it = Diags.begin(),
131 et = Diags.end(); it != et; ++it) {
132 ReportDiag(**it, filesMade);
136 void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
137 FilesMade *filesMade) {
139 // Create the HTML directory if it is missing.
142 if (std::error_code ec = llvm::sys::fs::create_directories(Directory)) {
143 llvm::errs() << "warning: could not create directory '"
144 << Directory << "': " << ec.message() << '\n';
155 // First flatten out the entire path to make it easier to use.
156 PathPieces path = D.path.flatten(/*ShouldFlattenMacros=*/false);
158 // The path as already been prechecked that the path is non-empty.
159 assert(!path.empty());
160 const SourceManager &SMgr = path.front()->getLocation().getManager();
162 // Create a new rewriter to generate HTML.
163 Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOpts());
165 // The file for the first path element is considered the main report file, it
166 // will usually be equivalent to SMgr.getMainFileID(); however, it might be a
167 // header when -analyzer-opt-analyze-headers is used.
168 FileID ReportFile = path.front()->getLocation().asLocation().getExpansionLoc().getFileID();
170 // Get the function/method name
171 SmallString<128> declName("unknown");
173 if (const Decl *DeclWithIssue = D.getDeclWithIssue()) {
174 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue))
175 declName = ND->getDeclName().getAsString();
177 if (const Stmt *Body = DeclWithIssue->getBody()) {
178 // Retrieve the relative position of the declaration which will be used
181 SMgr.getExpansionLoc(path.back()->getLocation().asLocation()),
183 FullSourceLoc FunL(SMgr.getExpansionLoc(Body->getLocStart()), SMgr);
184 offsetDecl = L.getExpansionLineNumber() - FunL.getExpansionLineNumber();
188 std::string report = GenerateHTML(D, R, SMgr, path, declName.c_str());
189 if (report.empty()) {
190 llvm::errs() << "warning: no diagnostics generated for main file.\n";
194 // Create a path for the target HTML file.
196 SmallString<128> Model, ResultPath;
198 if (!AnalyzerOpts.shouldWriteStableReportFilename()) {
199 llvm::sys::path::append(Model, Directory, "report-%%%%%%.html");
200 if (std::error_code EC =
201 llvm::sys::fs::make_absolute(Model)) {
202 llvm::errs() << "warning: could not make '" << Model
203 << "' absolute: " << EC.message() << '\n';
206 if (std::error_code EC =
207 llvm::sys::fs::createUniqueFile(Model, FD, ResultPath)) {
208 llvm::errs() << "warning: could not create file in '" << Directory
209 << "': " << EC.message() << '\n';
217 // Find a filename which is not already used
218 const FileEntry* Entry = SMgr.getFileEntryForID(ReportFile);
219 std::stringstream filename;
221 filename << "report-"
222 << llvm::sys::path::filename(Entry->getName()).str()
223 << "-" << declName.c_str()
225 << "-" << i << ".html";
226 llvm::sys::path::append(Model, Directory,
228 EC = llvm::sys::fs::openFileForWrite(Model,
230 llvm::sys::fs::F_RW |
231 llvm::sys::fs::F_Excl);
232 if (EC && EC != llvm::errc::file_exists) {
233 llvm::errs() << "warning: could not create file '" << Model
234 << "': " << EC.message() << '\n';
241 llvm::raw_fd_ostream os(FD, true);
244 filesMade->addDiagnostic(D, getName(),
245 llvm::sys::path::filename(ResultPath));
247 // Emit the HTML to disk.
251 std::string HTMLDiagnostics::GenerateHTML(const PathDiagnostic& D, Rewriter &R,
252 const SourceManager& SMgr, const PathPieces& path, const char *declName) {
254 // Rewrite source files as HTML for every new file the path crosses
255 std::vector<FileID> FileIDs;
256 for (auto I : path) {
257 FileID FID = I->getLocation().asLocation().getExpansionLoc().getFileID();
258 if (std::find(FileIDs.begin(), FileIDs.end(), FID) != FileIDs.end())
261 FileIDs.push_back(FID);
262 RewriteFile(R, SMgr, path, FID);
265 if (SupportsCrossFileDiagnostics && FileIDs.size() > 1) {
266 // Prefix file names, anchor tags, and nav cursors to every file
267 for (auto I = FileIDs.begin(), E = FileIDs.end(); I != E; I++) {
269 llvm::raw_string_ostream os(s);
271 if (I != FileIDs.begin())
272 os << "<hr class=divider>\n";
274 os << "<div id=File" << I->getHashValue() << ">\n";
277 if (I != FileIDs.begin())
278 os << "<div class=FileNav><a href=\"#File" << (I - 1)->getHashValue()
279 << "\">←</a></div>";
281 os << "<h4 class=FileName>" << SMgr.getFileEntryForID(*I)->getName()
286 os << "<div class=FileNav><a href=\"#File" << (I + 1)->getHashValue()
287 << "\">→</a></div>";
291 R.InsertTextBefore(SMgr.getLocForStartOfFile(*I), os.str());
294 // Append files to the main report file in the order they appear in the path
295 for (auto I : llvm::make_range(FileIDs.begin() + 1, FileIDs.end())) {
297 llvm::raw_string_ostream os(s);
299 const RewriteBuffer *Buf = R.getRewriteBufferFor(I);
303 R.InsertTextAfter(SMgr.getLocForEndOfFile(FileIDs[0]), os.str());
307 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileIDs[0]);
311 // Add CSS, header, and footer.
312 const FileEntry* Entry = SMgr.getFileEntryForID(FileIDs[0]);
313 FinalizeHTML(D, R, SMgr, path, FileIDs[0], Entry, declName);
316 llvm::raw_string_ostream os(file);
323 void HTMLDiagnostics::FinalizeHTML(const PathDiagnostic& D, Rewriter &R,
324 const SourceManager& SMgr, const PathPieces& path, FileID FID,
325 const FileEntry *Entry, const char *declName) {
326 // This is a cludge; basically we want to append either the full
327 // working directory if we have no directory information. This is
328 // a work in progress.
330 llvm::SmallString<0> DirName;
332 if (llvm::sys::path::is_relative(Entry->getName())) {
333 llvm::sys::fs::current_path(DirName);
337 int LineNumber = path.back()->getLocation().asLocation().getExpansionLineNumber();
338 int ColumnNumber = path.back()->getLocation().asLocation().getExpansionColumnNumber();
340 // Add the name of the file as an <h1> tag.
343 llvm::raw_string_ostream os(s);
345 os << "<!-- REPORTHEADER -->\n"
346 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
347 "<tr><td class=\"rowname\">File:</td><td>"
348 << html::EscapeText(DirName)
349 << html::EscapeText(Entry->getName())
350 << "</td></tr>\n<tr><td class=\"rowname\">Warning:</td><td>"
351 "<a href=\"#EndPath\">line "
356 << D.getVerboseDescription() << "</td></tr>\n";
358 // The navigation across the extra notes pieces.
359 unsigned NumExtraPieces = 0;
360 for (const auto &Piece : path) {
361 if (const auto *P = dyn_cast<PathDiagnosticNotePiece>(Piece.get())) {
363 P->getLocation().asLocation().getExpansionLineNumber();
365 P->getLocation().asLocation().getExpansionColumnNumber();
366 os << "<tr><td class=\"rowname\">Note:</td><td>"
367 << "<a href=\"#Note" << NumExtraPieces << "\">line "
368 << LineNumber << ", column " << ColumnNumber << "</a><br />"
369 << P->getString() << "</td></tr>";
374 // Output any other meta data.
376 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
378 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
381 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
382 "<h3>Annotated Source Code</h3>\n";
384 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
387 // Embed meta-data tags.
390 llvm::raw_string_ostream os(s);
392 StringRef BugDesc = D.getVerboseDescription();
393 if (!BugDesc.empty())
394 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
396 StringRef BugType = D.getBugType();
397 if (!BugType.empty())
398 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
400 PathDiagnosticLocation UPDLoc = D.getUniqueingLoc();
401 FullSourceLoc L(SMgr.getExpansionLoc(UPDLoc.isValid()
402 ? UPDLoc.asLocation()
403 : D.getLocation().asLocation()),
405 const Decl *DeclWithIssue = D.getDeclWithIssue();
407 StringRef BugCategory = D.getCategory();
408 if (!BugCategory.empty())
409 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
411 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
413 os << "\n<!-- FILENAME " << llvm::sys::path::filename(Entry->getName()) << " -->\n";
415 os << "\n<!-- FUNCTIONNAME " << declName << " -->\n";
417 os << "\n<!-- ISSUEHASHCONTENTOFLINEINCONTEXT "
418 << GetIssueHash(SMgr, L, D.getCheckName(), D.getBugType(), DeclWithIssue,
419 PP.getLangOpts()) << " -->\n";
421 os << "\n<!-- BUGLINE "
425 os << "\n<!-- BUGCOLUMN "
429 os << "\n<!-- BUGPATHLENGTH " << path.size() << " -->\n";
431 // Mark the end of the tags.
432 os << "\n<!-- BUGMETAEND -->\n";
435 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
438 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
441 void HTMLDiagnostics::RewriteFile(Rewriter &R, const SourceManager& SMgr,
442 const PathPieces& path, FileID FID) {
444 // Maintain the counts of extra note pieces separately.
445 unsigned TotalPieces = path.size();
446 unsigned TotalNotePieces =
447 std::count_if(path.begin(), path.end(),
448 [](const std::shared_ptr<PathDiagnosticPiece> &p) {
449 return isa<PathDiagnosticNotePiece>(*p);
452 unsigned TotalRegularPieces = TotalPieces - TotalNotePieces;
453 unsigned NumRegularPieces = TotalRegularPieces;
454 unsigned NumNotePieces = TotalNotePieces;
456 for (auto I = path.rbegin(), E = path.rend(); I != E; ++I) {
457 if (isa<PathDiagnosticNotePiece>(I->get())) {
458 // This adds diagnostic bubbles, but not navigation.
459 // Navigation through note pieces would be added later,
460 // as a separate pass through the piece list.
461 HandlePiece(R, FID, **I, NumNotePieces, TotalNotePieces);
464 HandlePiece(R, FID, **I, NumRegularPieces, TotalRegularPieces);
469 // Add line numbers, header, footer, etc.
471 html::EscapeText(R, FID);
472 html::AddLineNumbers(R, FID);
474 // If we have a preprocessor, relex the file and syntax highlight.
475 // We might not have a preprocessor if we come from a deserialized AST file,
478 html::SyntaxHighlight(R, FID, PP);
479 html::HighlightMacros(R, FID, PP);
482 void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
483 const PathDiagnosticPiece& P,
484 unsigned num, unsigned max) {
486 // For now, just draw a box above the line in question, and emit the
488 FullSourceLoc Pos = P.getLocation().asLocation();
493 SourceManager &SM = R.getSourceMgr();
494 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
495 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedExpansionLoc(Pos);
497 if (LPosInfo.first != BugFileID)
500 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
501 const char* FileStart = Buf->getBufferStart();
503 // Compute the column number. Rewind from the current position to the start
505 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
506 const char *TokInstantiationPtr =Pos.getExpansionLoc().getCharacterData();
507 const char *LineStart = TokInstantiationPtr-ColNo;
510 const char *LineEnd = TokInstantiationPtr;
511 const char* FileEnd = Buf->getBufferEnd();
512 while (*LineEnd != '\n' && LineEnd != FileEnd)
515 // Compute the margin offset by counting tabs and non-tabs.
517 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
518 PosNo += *c == '\t' ? 8 : 1;
520 // Create the html for the message.
522 const char *Kind = nullptr;
524 bool SuppressIndex = (max == 1);
525 switch (P.getKind()) {
526 case PathDiagnosticPiece::Call:
527 llvm_unreachable("Calls and extra notes should already be handled");
528 case PathDiagnosticPiece::Event: Kind = "Event"; break;
529 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
530 // Setting Kind to "Control" is intentional.
531 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
532 case PathDiagnosticPiece::Note:
535 SuppressIndex = true;
540 llvm::raw_string_ostream os(sbuf);
542 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
551 os << "\" class=\"msg";
553 os << " msg" << Kind;
554 os << "\" style=\"margin-left:" << PosNo << "ex";
556 // Output a maximum size.
557 if (!isa<PathDiagnosticMacroPiece>(P)) {
558 // Get the string and determining its maximum substring.
559 const auto &Msg = P.getString();
560 unsigned max_token = 0;
562 unsigned len = Msg.size();
572 if (cnt > max_token) max_token = cnt;
579 // Determine the approximate size of the message bubble in em.
581 const unsigned max_line = 120;
583 if (max_token >= max_line)
586 unsigned characters = max_line;
587 unsigned lines = len / max_line;
590 for (; characters > max_token; --characters)
591 if (len / characters > lines) {
601 os << "; max-width:" << em << "em";
604 os << "; max-width:100em";
608 if (!SuppressIndex) {
609 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
610 os << "<div class=\"PathIndex";
611 if (Kind) os << " PathIndex" << Kind;
612 os << "\">" << num << "</div>";
615 os << "</td><td><div class=\"PathNav\"><a href=\"#Path"
617 << "\" title=\"Previous event ("
619 << ")\">←</a></div></td>";
625 if (const PathDiagnosticMacroPiece *MP =
626 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
628 os << "Within the expansion of the macro '";
630 // Get the name of the macro by relexing it.
632 FullSourceLoc L = MP->getLocation().asLocation().getExpansionLoc();
633 assert(L.isFileID());
634 StringRef BufferInfo = L.getBufferData();
635 std::pair<FileID, unsigned> LocInfo = L.getDecomposedLoc();
636 const char* MacroName = LocInfo.second + BufferInfo.data();
637 Lexer rawLexer(SM.getLocForStartOfFile(LocInfo.first), PP.getLangOpts(),
638 BufferInfo.begin(), MacroName, BufferInfo.end());
641 rawLexer.LexFromRawLexer(TheTok);
642 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
648 if (!SuppressIndex) {
651 os << "<td><div class=\"PathNav\"><a href=\"#";
655 os << "Path" << (num + 1);
656 os << "\" title=\"Next event ("
658 << ")\">→</a></div></td>";
661 os << "</tr></table>";
664 // Within a macro piece. Write out each event.
665 ProcessMacroPiece(os, *MP, 0);
668 os << html::EscapeText(P.getString());
670 if (!SuppressIndex) {
673 os << "<td><div class=\"PathNav\"><a href=\"#";
677 os << "Path" << (num + 1);
678 os << "\" title=\"Next event ("
680 << ")\">→</a></div></td>";
683 os << "</tr></table>";
687 os << "</div></td></tr>";
689 // Insert the new html.
690 unsigned DisplayPos = LineEnd - FileStart;
692 SM.getLocForStartOfFile(LPosInfo.first).getLocWithOffset(DisplayPos);
694 R.InsertTextBefore(Loc, os.str());
696 // Now highlight the ranges.
697 ArrayRef<SourceRange> Ranges = P.getRanges();
698 for (ArrayRef<SourceRange>::iterator I = Ranges.begin(),
699 E = Ranges.end(); I != E; ++I) {
700 HighlightRange(R, LPosInfo.first, *I);
704 static void EmitAlphaCounter(raw_ostream &os, unsigned n) {
705 unsigned x = n % ('z' - 'a');
709 EmitAlphaCounter(os, n);
714 unsigned HTMLDiagnostics::ProcessMacroPiece(raw_ostream &os,
715 const PathDiagnosticMacroPiece& P,
718 for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end();
721 if (const PathDiagnosticMacroPiece *MP =
722 dyn_cast<PathDiagnosticMacroPiece>(I->get())) {
723 num = ProcessMacroPiece(os, *MP, num);
727 if (PathDiagnosticEventPiece *EP =
728 dyn_cast<PathDiagnosticEventPiece>(I->get())) {
729 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
731 "<table class=\"msgT\"><tr>"
732 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
733 EmitAlphaCounter(os, num++);
734 os << "</div></td><td valign=\"top\">"
735 << html::EscapeText(EP->getString())
736 << "</td></tr></table></div>\n";
743 void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
745 const char *HighlightStart,
746 const char *HighlightEnd) {
747 SourceManager &SM = R.getSourceMgr();
748 const LangOptions &LangOpts = R.getLangOpts();
750 SourceLocation InstantiationStart = SM.getExpansionLoc(Range.getBegin());
751 unsigned StartLineNo = SM.getExpansionLineNumber(InstantiationStart);
753 SourceLocation InstantiationEnd = SM.getExpansionLoc(Range.getEnd());
754 unsigned EndLineNo = SM.getExpansionLineNumber(InstantiationEnd);
756 if (EndLineNo < StartLineNo)
759 if (SM.getFileID(InstantiationStart) != BugFileID ||
760 SM.getFileID(InstantiationEnd) != BugFileID)
763 // Compute the column number of the end.
764 unsigned EndColNo = SM.getExpansionColumnNumber(InstantiationEnd);
765 unsigned OldEndColNo = EndColNo;
768 // Add in the length of the token, so that we cover multi-char tokens.
769 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
772 // Highlight the range. Make the span tag the outermost tag for the
776 InstantiationEnd.getLocWithOffset(EndColNo - OldEndColNo);
778 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);