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;
48 HTMLDiagnostics(AnalyzerOptions &AnalyzerOpts, const std::string& prefix, const Preprocessor &pp);
50 ~HTMLDiagnostics() override { FlushDiagnostics(nullptr); }
52 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
53 FilesMade *filesMade) override;
55 StringRef getName() const override {
56 return "HTMLDiagnostics";
59 unsigned ProcessMacroPiece(raw_ostream &os,
60 const PathDiagnosticMacroPiece& P,
63 void HandlePiece(Rewriter& R, FileID BugFileID,
64 const PathDiagnosticPiece& P, unsigned num, unsigned max);
66 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
67 const char *HighlightStart = "<span class=\"mrange\">",
68 const char *HighlightEnd = "</span>");
70 void ReportDiag(const PathDiagnostic& D,
71 FilesMade *filesMade);
74 } // end anonymous namespace
76 HTMLDiagnostics::HTMLDiagnostics(AnalyzerOptions &AnalyzerOpts,
77 const std::string& prefix,
78 const Preprocessor &pp)
79 : Directory(prefix), createdDir(false), noDir(false), PP(pp), AnalyzerOpts(AnalyzerOpts) {
82 void ento::createHTMLDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
83 PathDiagnosticConsumers &C,
84 const std::string& prefix,
85 const Preprocessor &PP) {
86 C.push_back(new HTMLDiagnostics(AnalyzerOpts, prefix, PP));
89 //===----------------------------------------------------------------------===//
91 //===----------------------------------------------------------------------===//
93 void HTMLDiagnostics::FlushDiagnosticsImpl(
94 std::vector<const PathDiagnostic *> &Diags,
95 FilesMade *filesMade) {
96 for (std::vector<const PathDiagnostic *>::iterator it = Diags.begin(),
97 et = Diags.end(); it != et; ++it) {
98 ReportDiag(**it, filesMade);
102 void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
103 FilesMade *filesMade) {
105 // Create the HTML directory if it is missing.
108 if (std::error_code ec = llvm::sys::fs::create_directories(Directory)) {
109 llvm::errs() << "warning: could not create directory '"
110 << Directory << "': " << ec.message() << '\n';
121 // First flatten out the entire path to make it easier to use.
122 PathPieces path = D.path.flatten(/*ShouldFlattenMacros=*/false);
124 // The path as already been prechecked that all parts of the path are
125 // from the same file and that it is non-empty.
126 const SourceManager &SMgr = path.front()->getLocation().getManager();
127 assert(!path.empty());
129 path.front()->getLocation().asLocation().getExpansionLoc().getFileID();
130 assert(FID.isValid());
132 // Create a new rewriter to generate HTML.
133 Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOpts());
135 // Get the function/method name
136 SmallString<128> declName("unknown");
138 if (const Decl *DeclWithIssue = D.getDeclWithIssue()) {
139 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
140 declName = ND->getDeclName().getAsString();
143 if (const Stmt *Body = DeclWithIssue->getBody()) {
144 // Retrieve the relative position of the declaration which will be used
147 SMgr.getExpansionLoc(path.back()->getLocation().asLocation()),
149 FullSourceLoc FunL(SMgr.getExpansionLoc(Body->getLocStart()), SMgr);
150 offsetDecl = L.getExpansionLineNumber() - FunL.getExpansionLineNumber();
155 unsigned n = path.size();
158 for (PathPieces::const_reverse_iterator I = path.rbegin(),
161 HandlePiece(R, FID, **I, n, max);
163 // Add line numbers, header, footer, etc.
165 // unsigned FID = R.getSourceMgr().getMainFileID();
166 html::EscapeText(R, FID);
167 html::AddLineNumbers(R, FID);
169 // If we have a preprocessor, relex the file and syntax highlight.
170 // We might not have a preprocessor if we come from a deserialized AST file,
173 html::SyntaxHighlight(R, FID, PP);
174 html::HighlightMacros(R, FID, PP);
176 // Get the full directory name of the analyzed file.
178 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
180 // This is a cludge; basically we want to append either the full
181 // working directory if we have no directory information. This is
182 // a work in progress.
184 llvm::SmallString<0> DirName;
186 if (llvm::sys::path::is_relative(Entry->getName())) {
187 llvm::sys::fs::current_path(DirName);
191 int LineNumber = path.back()->getLocation().asLocation().getExpansionLineNumber();
192 int ColumnNumber = path.back()->getLocation().asLocation().getExpansionColumnNumber();
194 // Add the name of the file as an <h1> tag.
198 llvm::raw_string_ostream os(s);
200 os << "<!-- REPORTHEADER -->\n"
201 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
202 "<tr><td class=\"rowname\">File:</td><td>"
203 << html::EscapeText(DirName)
204 << html::EscapeText(Entry->getName())
205 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
206 "<a href=\"#EndPath\">line "
210 << "</a></td></tr>\n"
211 "<tr><td class=\"rowname\">Description:</td><td>"
212 << D.getVerboseDescription() << "</td></tr>\n";
214 // Output any other meta data.
216 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
218 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
221 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
222 "<h3>Annotated Source Code</h3>\n";
224 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
227 // Embed meta-data tags.
230 llvm::raw_string_ostream os(s);
232 StringRef BugDesc = D.getVerboseDescription();
233 if (!BugDesc.empty())
234 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
236 StringRef BugType = D.getBugType();
237 if (!BugType.empty())
238 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
240 PathDiagnosticLocation UPDLoc = D.getUniqueingLoc();
241 FullSourceLoc L(SMgr.getExpansionLoc(UPDLoc.isValid()
242 ? UPDLoc.asLocation()
243 : D.getLocation().asLocation()),
245 const Decl *DeclWithIssue = D.getDeclWithIssue();
247 StringRef BugCategory = D.getCategory();
248 if (!BugCategory.empty())
249 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
251 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
253 os << "\n<!-- FILENAME " << llvm::sys::path::filename(Entry->getName()) << " -->\n";
255 os << "\n<!-- FUNCTIONNAME " << declName << " -->\n";
257 os << "\n<!-- ISSUEHASHCONTENTOFLINEINCONTEXT "
258 << GetIssueHash(SMgr, L, D.getCheckName(), D.getBugType(), DeclWithIssue,
259 PP.getLangOpts()) << " -->\n";
261 os << "\n<!-- BUGLINE "
265 os << "\n<!-- BUGCOLUMN "
269 os << "\n<!-- BUGPATHLENGTH " << path.size() << " -->\n";
271 // Mark the end of the tags.
272 os << "\n<!-- BUGMETAEND -->\n";
275 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
278 // Add CSS, header, and footer.
280 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
282 // Get the rewrite buffer.
283 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
286 llvm::errs() << "warning: no diagnostics generated for main file.\n";
290 // Create a path for the target HTML file.
292 SmallString<128> Model, ResultPath;
294 if (!AnalyzerOpts.shouldWriteStableReportFilename()) {
295 llvm::sys::path::append(Model, Directory, "report-%%%%%%.html");
296 if (std::error_code EC =
297 llvm::sys::fs::make_absolute(Model)) {
298 llvm::errs() << "warning: could not make '" << Model
299 << "' absolute: " << EC.message() << '\n';
302 if (std::error_code EC =
303 llvm::sys::fs::createUniqueFile(Model, FD, ResultPath)) {
304 llvm::errs() << "warning: could not create file in '" << Directory
305 << "': " << EC.message() << '\n';
313 // Find a filename which is not already used
314 std::stringstream filename;
316 filename << "report-"
317 << llvm::sys::path::filename(Entry->getName()).str()
318 << "-" << declName.c_str()
320 << "-" << i << ".html";
321 llvm::sys::path::append(Model, Directory,
323 EC = llvm::sys::fs::openFileForWrite(Model,
325 llvm::sys::fs::F_RW |
326 llvm::sys::fs::F_Excl);
327 if (EC && EC != llvm::errc::file_exists) {
328 llvm::errs() << "warning: could not create file '" << Model
329 << "': " << EC.message() << '\n';
336 llvm::raw_fd_ostream os(FD, true);
339 filesMade->addDiagnostic(D, getName(),
340 llvm::sys::path::filename(ResultPath));
342 // Emit the HTML to disk.
343 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
347 void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
348 const PathDiagnosticPiece& P,
349 unsigned num, unsigned max) {
351 // For now, just draw a box above the line in question, and emit the
353 FullSourceLoc Pos = P.getLocation().asLocation();
358 SourceManager &SM = R.getSourceMgr();
359 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
360 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedExpansionLoc(Pos);
362 if (LPosInfo.first != BugFileID)
365 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
366 const char* FileStart = Buf->getBufferStart();
368 // Compute the column number. Rewind from the current position to the start
370 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
371 const char *TokInstantiationPtr =Pos.getExpansionLoc().getCharacterData();
372 const char *LineStart = TokInstantiationPtr-ColNo;
375 const char *LineEnd = TokInstantiationPtr;
376 const char* FileEnd = Buf->getBufferEnd();
377 while (*LineEnd != '\n' && LineEnd != FileEnd)
380 // Compute the margin offset by counting tabs and non-tabs.
382 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
383 PosNo += *c == '\t' ? 8 : 1;
385 // Create the html for the message.
387 const char *Kind = nullptr;
388 switch (P.getKind()) {
389 case PathDiagnosticPiece::Call:
390 llvm_unreachable("Calls should already be handled");
391 case PathDiagnosticPiece::Event: Kind = "Event"; break;
392 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
393 // Setting Kind to "Control" is intentional.
394 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
398 llvm::raw_string_ostream os(sbuf);
400 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
407 os << "\" class=\"msg";
409 os << " msg" << Kind;
410 os << "\" style=\"margin-left:" << PosNo << "ex";
412 // Output a maximum size.
413 if (!isa<PathDiagnosticMacroPiece>(P)) {
414 // Get the string and determining its maximum substring.
415 const auto &Msg = P.getString();
416 unsigned max_token = 0;
418 unsigned len = Msg.size();
428 if (cnt > max_token) max_token = cnt;
435 // Determine the approximate size of the message bubble in em.
437 const unsigned max_line = 120;
439 if (max_token >= max_line)
442 unsigned characters = max_line;
443 unsigned lines = len / max_line;
446 for (; characters > max_token; --characters)
447 if (len / characters > lines) {
457 os << "; max-width:" << em << "em";
460 os << "; max-width:100em";
465 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
466 os << "<div class=\"PathIndex";
467 if (Kind) os << " PathIndex" << Kind;
468 os << "\">" << num << "</div>";
471 os << "</td><td><div class=\"PathNav\"><a href=\"#Path"
473 << "\" title=\"Previous event ("
475 << ")\">←</a></div></td>";
481 if (const PathDiagnosticMacroPiece *MP =
482 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
484 os << "Within the expansion of the macro '";
486 // Get the name of the macro by relexing it.
488 FullSourceLoc L = MP->getLocation().asLocation().getExpansionLoc();
489 assert(L.isFileID());
490 StringRef BufferInfo = L.getBufferData();
491 std::pair<FileID, unsigned> LocInfo = L.getDecomposedLoc();
492 const char* MacroName = LocInfo.second + BufferInfo.data();
493 Lexer rawLexer(SM.getLocForStartOfFile(LocInfo.first), PP.getLangOpts(),
494 BufferInfo.begin(), MacroName, BufferInfo.end());
497 rawLexer.LexFromRawLexer(TheTok);
498 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
507 os << "<td><div class=\"PathNav\"><a href=\"#";
511 os << "Path" << (num + 1);
512 os << "\" title=\"Next event ("
514 << ")\">→</a></div></td>";
517 os << "</tr></table>";
520 // Within a macro piece. Write out each event.
521 ProcessMacroPiece(os, *MP, 0);
524 os << html::EscapeText(P.getString());
529 os << "<td><div class=\"PathNav\"><a href=\"#";
533 os << "Path" << (num + 1);
534 os << "\" title=\"Next event ("
536 << ")\">→</a></div></td>";
539 os << "</tr></table>";
543 os << "</div></td></tr>";
545 // Insert the new html.
546 unsigned DisplayPos = LineEnd - FileStart;
548 SM.getLocForStartOfFile(LPosInfo.first).getLocWithOffset(DisplayPos);
550 R.InsertTextBefore(Loc, os.str());
552 // Now highlight the ranges.
553 ArrayRef<SourceRange> Ranges = P.getRanges();
554 for (ArrayRef<SourceRange>::iterator I = Ranges.begin(),
555 E = Ranges.end(); I != E; ++I) {
556 HighlightRange(R, LPosInfo.first, *I);
560 static void EmitAlphaCounter(raw_ostream &os, unsigned n) {
561 unsigned x = n % ('z' - 'a');
565 EmitAlphaCounter(os, n);
570 unsigned HTMLDiagnostics::ProcessMacroPiece(raw_ostream &os,
571 const PathDiagnosticMacroPiece& P,
574 for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end();
577 if (const PathDiagnosticMacroPiece *MP =
578 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
579 num = ProcessMacroPiece(os, *MP, num);
583 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
584 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
586 "<table class=\"msgT\"><tr>"
587 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
588 EmitAlphaCounter(os, num++);
589 os << "</div></td><td valign=\"top\">"
590 << html::EscapeText(EP->getString())
591 << "</td></tr></table></div>\n";
598 void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
600 const char *HighlightStart,
601 const char *HighlightEnd) {
602 SourceManager &SM = R.getSourceMgr();
603 const LangOptions &LangOpts = R.getLangOpts();
605 SourceLocation InstantiationStart = SM.getExpansionLoc(Range.getBegin());
606 unsigned StartLineNo = SM.getExpansionLineNumber(InstantiationStart);
608 SourceLocation InstantiationEnd = SM.getExpansionLoc(Range.getEnd());
609 unsigned EndLineNo = SM.getExpansionLineNumber(InstantiationEnd);
611 if (EndLineNo < StartLineNo)
614 if (SM.getFileID(InstantiationStart) != BugFileID ||
615 SM.getFileID(InstantiationEnd) != BugFileID)
618 // Compute the column number of the end.
619 unsigned EndColNo = SM.getExpansionColumnNumber(InstantiationEnd);
620 unsigned OldEndColNo = EndColNo;
623 // Add in the length of the token, so that we cover multi-char tokens.
624 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
627 // Highlight the range. Make the span tag the outermost tag for the
631 InstantiationEnd.getLocWithOffset(EndColNo - OldEndColNo);
633 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);