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/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"
29 using namespace clang;
32 //===----------------------------------------------------------------------===//
34 //===----------------------------------------------------------------------===//
38 class HTMLDiagnostics : public PathDiagnosticConsumer {
39 llvm::sys::Path Directory, FilePrefix;
40 bool createdDir, noDir;
41 const Preprocessor &PP;
43 HTMLDiagnostics(const std::string& prefix, const Preprocessor &pp);
45 virtual ~HTMLDiagnostics() { FlushDiagnostics(NULL); }
47 virtual void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
48 SmallVectorImpl<std::string> *FilesMade);
50 virtual StringRef getName() const {
51 return "HTMLDiagnostics";
54 unsigned ProcessMacroPiece(raw_ostream &os,
55 const PathDiagnosticMacroPiece& P,
58 void HandlePiece(Rewriter& R, FileID BugFileID,
59 const PathDiagnosticPiece& P, unsigned num, unsigned max);
61 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
62 const char *HighlightStart = "<span class=\"mrange\">",
63 const char *HighlightEnd = "</span>");
65 void ReportDiag(const PathDiagnostic& D,
66 SmallVectorImpl<std::string> *FilesMade);
69 } // end anonymous namespace
71 HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix,
72 const Preprocessor &pp)
73 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
75 // All html files begin with "report"
76 FilePrefix.appendComponent("report");
79 PathDiagnosticConsumer*
80 ento::createHTMLDiagnosticConsumer(const std::string& prefix,
81 const Preprocessor &PP) {
82 return new HTMLDiagnostics(prefix, PP);
85 //===----------------------------------------------------------------------===//
87 //===----------------------------------------------------------------------===//
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);
98 static void flattenPath(PathPieces &primaryPath, PathPieces ¤tPath,
99 const PathPieces &oldPath) {
100 for (PathPieces::const_iterator it = oldPath.begin(), et = oldPath.end();
102 PathDiagnosticPiece *piece = it->getPtr();
103 if (const PathDiagnosticCallPiece *call =
104 dyn_cast<PathDiagnosticCallPiece>(piece)) {
105 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callEnter =
106 call->getCallEnterEvent();
108 currentPath.push_back(callEnter);
109 flattenPath(primaryPath, primaryPath, call->path);
110 IntrusiveRefCntPtr<PathDiagnosticEventPiece> callExit =
111 call->getCallExitEvent();
113 currentPath.push_back(callExit);
116 if (PathDiagnosticMacroPiece *macro =
117 dyn_cast<PathDiagnosticMacroPiece>(piece)) {
118 currentPath.push_back(piece);
120 flattenPath(primaryPath, newPath, macro->subPieces);
121 macro->subPieces = newPath;
125 currentPath.push_back(piece);
129 void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
130 SmallVectorImpl<std::string> *FilesMade) {
132 // Create the HTML directory if it is missing.
135 std::string ErrorMsg;
136 Directory.createDirectoryOnDisk(true, &ErrorMsg);
139 if (llvm::sys::fs::is_directory(Directory.str(), IsDirectory) ||
141 llvm::errs() << "warning: could not create directory '"
142 << Directory.str() << "'\n"
143 << "reason: " << ErrorMsg << '\n';
154 // First flatten out the entire path to make it easier to use.
156 flattenPath(path, path, D.path);
158 // The path as already been prechecked that all parts of the path are
159 // from the same file and that it is non-empty.
160 const SourceManager &SMgr = (*path.begin())->getLocation().getManager();
161 assert(!path.empty());
163 (*path.begin())->getLocation().asLocation().getExpansionLoc().getFileID();
164 assert(!FID.isInvalid());
166 // Create a new rewriter to generate HTML.
167 Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOpts());
170 unsigned n = path.size();
173 for (PathPieces::const_reverse_iterator I = path.rbegin(),
176 HandlePiece(R, FID, **I, n, max);
178 // Add line numbers, header, footer, etc.
180 // unsigned FID = R.getSourceMgr().getMainFileID();
181 html::EscapeText(R, FID);
182 html::AddLineNumbers(R, FID);
184 // If we have a preprocessor, relex the file and syntax highlight.
185 // We might not have a preprocessor if we come from a deserialized AST file,
188 html::SyntaxHighlight(R, FID, PP);
189 html::HighlightMacros(R, FID, PP);
191 // Get the full directory name of the analyzed file.
193 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
195 // This is a cludge; basically we want to append either the full
196 // working directory if we have no directory information. This is
197 // a work in progress.
199 std::string DirName = "";
201 if (llvm::sys::path::is_relative(Entry->getName())) {
202 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
203 DirName = P.str() + "/";
206 // Add the name of the file as an <h1> tag.
210 llvm::raw_string_ostream os(s);
212 os << "<!-- REPORTHEADER -->\n"
213 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
214 "<tr><td class=\"rowname\">File:</td><td>"
215 << html::EscapeText(DirName)
216 << html::EscapeText(Entry->getName())
217 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
218 "<a href=\"#EndPath\">line "
219 << (*path.rbegin())->getLocation().asLocation().getExpansionLineNumber()
221 << (*path.rbegin())->getLocation().asLocation().getExpansionColumnNumber()
222 << "</a></td></tr>\n"
223 "<tr><td class=\"rowname\">Description:</td><td>"
224 << D.getDescription() << "</td></tr>\n";
226 // Output any other meta data.
228 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
230 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
233 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
234 "<h3>Annotated Source Code</h3>\n";
236 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
239 // Embed meta-data tags.
242 llvm::raw_string_ostream os(s);
244 const std::string& BugDesc = D.getDescription();
245 if (!BugDesc.empty())
246 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
248 const std::string& BugType = D.getBugType();
249 if (!BugType.empty())
250 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
252 const std::string& BugCategory = D.getCategory();
253 if (!BugCategory.empty())
254 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
256 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
258 os << "\n<!-- BUGLINE "
259 << path.back()->getLocation().asLocation().getExpansionLineNumber()
262 os << "\n<!-- BUGPATHLENGTH " << path.size() << " -->\n";
264 // Mark the end of the tags.
265 os << "\n<!-- BUGMETAEND -->\n";
268 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
271 // Add CSS, header, and footer.
273 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
275 // Get the rewrite buffer.
276 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
279 llvm::errs() << "warning: no diagnostics generated for main file.\n";
283 // Create a path for the target HTML file.
284 llvm::sys::Path F(FilePrefix);
285 F.makeUnique(false, NULL);
287 // Rename the file with an HTML extension.
288 llvm::sys::Path H(F);
289 H.appendSuffix("html");
290 F.renamePathOnDisk(H, NULL);
292 std::string ErrorMsg;
293 llvm::raw_fd_ostream os(H.c_str(), ErrorMsg);
295 if (!ErrorMsg.empty()) {
296 llvm::errs() << "warning: could not create file '" << F.str()
302 FilesMade->push_back(llvm::sys::path::filename(H.str()));
304 // Emit the HTML to disk.
305 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
309 void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
310 const PathDiagnosticPiece& P,
311 unsigned num, unsigned max) {
313 // For now, just draw a box above the line in question, and emit the
315 FullSourceLoc Pos = P.getLocation().asLocation();
320 SourceManager &SM = R.getSourceMgr();
321 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
322 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedExpansionLoc(Pos);
324 if (LPosInfo.first != BugFileID)
327 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
328 const char* FileStart = Buf->getBufferStart();
330 // Compute the column number. Rewind from the current position to the start
332 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
333 const char *TokInstantiationPtr =Pos.getExpansionLoc().getCharacterData();
334 const char *LineStart = TokInstantiationPtr-ColNo;
337 const char *LineEnd = TokInstantiationPtr;
338 const char* FileEnd = Buf->getBufferEnd();
339 while (*LineEnd != '\n' && LineEnd != FileEnd)
342 // Compute the margin offset by counting tabs and non-tabs.
344 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
345 PosNo += *c == '\t' ? 8 : 1;
347 // Create the html for the message.
349 const char *Kind = 0;
350 switch (P.getKind()) {
351 case PathDiagnosticPiece::Call:
352 llvm_unreachable("Calls should already be handled");
353 case PathDiagnosticPiece::Event: Kind = "Event"; break;
354 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
355 // Setting Kind to "Control" is intentional.
356 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
360 llvm::raw_string_ostream os(sbuf);
362 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
369 os << "\" class=\"msg";
371 os << " msg" << Kind;
372 os << "\" style=\"margin-left:" << PosNo << "ex";
374 // Output a maximum size.
375 if (!isa<PathDiagnosticMacroPiece>(P)) {
376 // Get the string and determining its maximum substring.
377 const std::string& Msg = P.getString();
378 unsigned max_token = 0;
380 unsigned len = Msg.size();
382 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
390 if (cnt > max_token) max_token = cnt;
397 // Determine the approximate size of the message bubble in em.
399 const unsigned max_line = 120;
401 if (max_token >= max_line)
404 unsigned characters = max_line;
405 unsigned lines = len / max_line;
408 for (; characters > max_token; --characters)
409 if (len / characters > lines) {
419 os << "; max-width:" << em << "em";
422 os << "; max-width:100em";
427 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
428 os << "<div class=\"PathIndex";
429 if (Kind) os << " PathIndex" << Kind;
430 os << "\">" << num << "</div>";
434 if (const PathDiagnosticMacroPiece *MP =
435 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
437 os << "Within the expansion of the macro '";
439 // Get the name of the macro by relexing it.
441 FullSourceLoc L = MP->getLocation().asLocation().getExpansionLoc();
442 assert(L.isFileID());
443 StringRef BufferInfo = L.getBufferData();
444 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.data();
445 Lexer rawLexer(L, PP.getLangOpts(), BufferInfo.begin(),
446 MacroName, BufferInfo.end());
449 rawLexer.LexFromRawLexer(TheTok);
450 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
457 os << "</td></tr></table>";
459 // Within a macro piece. Write out each event.
460 ProcessMacroPiece(os, *MP, 0);
463 os << html::EscapeText(P.getString());
466 os << "</td></tr></table>";
469 os << "</div></td></tr>";
471 // Insert the new html.
472 unsigned DisplayPos = LineEnd - FileStart;
474 SM.getLocForStartOfFile(LPosInfo.first).getLocWithOffset(DisplayPos);
476 R.InsertTextBefore(Loc, os.str());
478 // Now highlight the ranges.
479 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
481 HighlightRange(R, LPosInfo.first, *I);
484 // If there is a code insertion hint, insert that code.
485 // FIXME: This code is disabled because it seems to mangle the HTML
486 // output. I'm leaving it here because it's generally the right idea,
487 // but needs some help from someone more familiar with the rewriter.
488 for (const FixItHint *Hint = P.fixit_begin(), *HintEnd = P.fixit_end();
489 Hint != HintEnd; ++Hint) {
490 if (Hint->RemoveRange.isValid()) {
491 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
492 "<span class=\"CodeRemovalHint\">", "</span>");
494 if (Hint->InsertionLoc.isValid()) {
495 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
496 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
498 R.InsertTextBefore(Hint->InsertionLoc, EscapedCode);
504 static void EmitAlphaCounter(raw_ostream &os, unsigned n) {
505 unsigned x = n % ('z' - 'a');
509 EmitAlphaCounter(os, n);
514 unsigned HTMLDiagnostics::ProcessMacroPiece(raw_ostream &os,
515 const PathDiagnosticMacroPiece& P,
518 for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end();
521 if (const PathDiagnosticMacroPiece *MP =
522 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
523 num = ProcessMacroPiece(os, *MP, num);
527 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
528 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
530 "<table class=\"msgT\"><tr>"
531 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
532 EmitAlphaCounter(os, num++);
533 os << "</div></td><td valign=\"top\">"
534 << html::EscapeText(EP->getString())
535 << "</td></tr></table></div>\n";
542 void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
544 const char *HighlightStart,
545 const char *HighlightEnd) {
546 SourceManager &SM = R.getSourceMgr();
547 const LangOptions &LangOpts = R.getLangOpts();
549 SourceLocation InstantiationStart = SM.getExpansionLoc(Range.getBegin());
550 unsigned StartLineNo = SM.getExpansionLineNumber(InstantiationStart);
552 SourceLocation InstantiationEnd = SM.getExpansionLoc(Range.getEnd());
553 unsigned EndLineNo = SM.getExpansionLineNumber(InstantiationEnd);
555 if (EndLineNo < StartLineNo)
558 if (SM.getFileID(InstantiationStart) != BugFileID ||
559 SM.getFileID(InstantiationEnd) != BugFileID)
562 // Compute the column number of the end.
563 unsigned EndColNo = SM.getExpansionColumnNumber(InstantiationEnd);
564 unsigned OldEndColNo = EndColNo;
567 // Add in the length of the token, so that we cover multi-char tokens.
568 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
571 // Highlight the range. Make the span tag the outermost tag for the
575 InstantiationEnd.getLocWithOffset(EndColNo - OldEndColNo);
577 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);