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 void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
99 SmallVectorImpl<std::string> *FilesMade) {
101 // Create the HTML directory if it is missing.
104 std::string ErrorMsg;
105 Directory.createDirectoryOnDisk(true, &ErrorMsg);
108 if (llvm::sys::fs::is_directory(Directory.str(), IsDirectory) ||
110 llvm::errs() << "warning: could not create directory '"
111 << Directory.str() << "'\n"
112 << "reason: " << ErrorMsg << '\n';
123 // First flatten out the entire path to make it easier to use.
124 PathPieces path = D.path.flatten(/*ShouldFlattenMacros=*/false);
126 // The path as already been prechecked that all parts of the path are
127 // from the same file and that it is non-empty.
128 const SourceManager &SMgr = (*path.begin())->getLocation().getManager();
129 assert(!path.empty());
131 (*path.begin())->getLocation().asLocation().getExpansionLoc().getFileID();
132 assert(!FID.isInvalid());
134 // Create a new rewriter to generate HTML.
135 Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOpts());
138 unsigned n = path.size();
141 for (PathPieces::const_reverse_iterator I = path.rbegin(),
144 HandlePiece(R, FID, **I, n, max);
146 // Add line numbers, header, footer, etc.
148 // unsigned FID = R.getSourceMgr().getMainFileID();
149 html::EscapeText(R, FID);
150 html::AddLineNumbers(R, FID);
152 // If we have a preprocessor, relex the file and syntax highlight.
153 // We might not have a preprocessor if we come from a deserialized AST file,
156 html::SyntaxHighlight(R, FID, PP);
157 html::HighlightMacros(R, FID, PP);
159 // Get the full directory name of the analyzed file.
161 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
163 // This is a cludge; basically we want to append either the full
164 // working directory if we have no directory information. This is
165 // a work in progress.
167 std::string DirName = "";
169 if (llvm::sys::path::is_relative(Entry->getName())) {
170 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
171 DirName = P.str() + "/";
174 // Add the name of the file as an <h1> tag.
178 llvm::raw_string_ostream os(s);
180 os << "<!-- REPORTHEADER -->\n"
181 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
182 "<tr><td class=\"rowname\">File:</td><td>"
183 << html::EscapeText(DirName)
184 << html::EscapeText(Entry->getName())
185 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
186 "<a href=\"#EndPath\">line "
187 << (*path.rbegin())->getLocation().asLocation().getExpansionLineNumber()
189 << (*path.rbegin())->getLocation().asLocation().getExpansionColumnNumber()
190 << "</a></td></tr>\n"
191 "<tr><td class=\"rowname\">Description:</td><td>"
192 << D.getDescription() << "</td></tr>\n";
194 // Output any other meta data.
196 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
198 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
201 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
202 "<h3>Annotated Source Code</h3>\n";
204 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
207 // Embed meta-data tags.
210 llvm::raw_string_ostream os(s);
212 const std::string& BugDesc = D.getDescription();
213 if (!BugDesc.empty())
214 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
216 const std::string& BugType = D.getBugType();
217 if (!BugType.empty())
218 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
220 const std::string& BugCategory = D.getCategory();
221 if (!BugCategory.empty())
222 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
224 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
226 os << "\n<!-- BUGLINE "
227 << path.back()->getLocation().asLocation().getExpansionLineNumber()
230 os << "\n<!-- BUGPATHLENGTH " << path.size() << " -->\n";
232 // Mark the end of the tags.
233 os << "\n<!-- BUGMETAEND -->\n";
236 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
239 // Add CSS, header, and footer.
241 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
243 // Get the rewrite buffer.
244 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
247 llvm::errs() << "warning: no diagnostics generated for main file.\n";
251 // Create a path for the target HTML file.
252 llvm::sys::Path F(FilePrefix);
253 F.makeUnique(false, NULL);
255 // Rename the file with an HTML extension.
256 llvm::sys::Path H(F);
257 H.appendSuffix("html");
258 F.renamePathOnDisk(H, NULL);
260 std::string ErrorMsg;
261 llvm::raw_fd_ostream os(H.c_str(), ErrorMsg);
263 if (!ErrorMsg.empty()) {
264 llvm::errs() << "warning: could not create file '" << F.str()
270 FilesMade->push_back(llvm::sys::path::filename(H.str()));
272 // Emit the HTML to disk.
273 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
277 void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
278 const PathDiagnosticPiece& P,
279 unsigned num, unsigned max) {
281 // For now, just draw a box above the line in question, and emit the
283 FullSourceLoc Pos = P.getLocation().asLocation();
288 SourceManager &SM = R.getSourceMgr();
289 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
290 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedExpansionLoc(Pos);
292 if (LPosInfo.first != BugFileID)
295 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
296 const char* FileStart = Buf->getBufferStart();
298 // Compute the column number. Rewind from the current position to the start
300 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
301 const char *TokInstantiationPtr =Pos.getExpansionLoc().getCharacterData();
302 const char *LineStart = TokInstantiationPtr-ColNo;
305 const char *LineEnd = TokInstantiationPtr;
306 const char* FileEnd = Buf->getBufferEnd();
307 while (*LineEnd != '\n' && LineEnd != FileEnd)
310 // Compute the margin offset by counting tabs and non-tabs.
312 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
313 PosNo += *c == '\t' ? 8 : 1;
315 // Create the html for the message.
317 const char *Kind = 0;
318 switch (P.getKind()) {
319 case PathDiagnosticPiece::Call:
320 llvm_unreachable("Calls should already be handled");
321 case PathDiagnosticPiece::Event: Kind = "Event"; break;
322 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
323 // Setting Kind to "Control" is intentional.
324 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
328 llvm::raw_string_ostream os(sbuf);
330 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
337 os << "\" class=\"msg";
339 os << " msg" << Kind;
340 os << "\" style=\"margin-left:" << PosNo << "ex";
342 // Output a maximum size.
343 if (!isa<PathDiagnosticMacroPiece>(P)) {
344 // Get the string and determining its maximum substring.
345 const std::string& Msg = P.getString();
346 unsigned max_token = 0;
348 unsigned len = Msg.size();
350 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
358 if (cnt > max_token) max_token = cnt;
365 // Determine the approximate size of the message bubble in em.
367 const unsigned max_line = 120;
369 if (max_token >= max_line)
372 unsigned characters = max_line;
373 unsigned lines = len / max_line;
376 for (; characters > max_token; --characters)
377 if (len / characters > lines) {
387 os << "; max-width:" << em << "em";
390 os << "; max-width:100em";
395 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
396 os << "<div class=\"PathIndex";
397 if (Kind) os << " PathIndex" << Kind;
398 os << "\">" << num << "</div>";
401 os << "</td><td><div class=\"PathNav\"><a href=\"#Path"
403 << "\" title=\"Previous event ("
405 << ")\">←</a></div></td>";
411 if (const PathDiagnosticMacroPiece *MP =
412 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
414 os << "Within the expansion of the macro '";
416 // Get the name of the macro by relexing it.
418 FullSourceLoc L = MP->getLocation().asLocation().getExpansionLoc();
419 assert(L.isFileID());
420 StringRef BufferInfo = L.getBufferData();
421 std::pair<FileID, unsigned> LocInfo = L.getDecomposedLoc();
422 const char* MacroName = LocInfo.second + BufferInfo.data();
423 Lexer rawLexer(SM.getLocForStartOfFile(LocInfo.first), PP.getLangOpts(),
424 BufferInfo.begin(), MacroName, BufferInfo.end());
427 rawLexer.LexFromRawLexer(TheTok);
428 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
437 os << "<td><div class=\"PathNav\"><a href=\"#";
441 os << "Path" << (num + 1);
442 os << "\" title=\"Next event ("
444 << ")\">→</a></div></td>";
447 os << "</tr></table>";
450 // Within a macro piece. Write out each event.
451 ProcessMacroPiece(os, *MP, 0);
454 os << html::EscapeText(P.getString());
459 os << "<td><div class=\"PathNav\"><a href=\"#";
463 os << "Path" << (num + 1);
464 os << "\" title=\"Next event ("
466 << ")\">→</a></div></td>";
469 os << "</tr></table>";
473 os << "</div></td></tr>";
475 // Insert the new html.
476 unsigned DisplayPos = LineEnd - FileStart;
478 SM.getLocForStartOfFile(LPosInfo.first).getLocWithOffset(DisplayPos);
480 R.InsertTextBefore(Loc, os.str());
482 // Now highlight the ranges.
483 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
485 HighlightRange(R, LPosInfo.first, *I);
488 // If there is a code insertion hint, insert that code.
489 // FIXME: This code is disabled because it seems to mangle the HTML
490 // output. I'm leaving it here because it's generally the right idea,
491 // but needs some help from someone more familiar with the rewriter.
492 for (const FixItHint *Hint = P.fixit_begin(), *HintEnd = P.fixit_end();
493 Hint != HintEnd; ++Hint) {
494 if (Hint->RemoveRange.isValid()) {
495 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
496 "<span class=\"CodeRemovalHint\">", "</span>");
498 if (Hint->InsertionLoc.isValid()) {
499 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
500 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
502 R.InsertTextBefore(Hint->InsertionLoc, EscapedCode);
508 static void EmitAlphaCounter(raw_ostream &os, unsigned n) {
509 unsigned x = n % ('z' - 'a');
513 EmitAlphaCounter(os, n);
518 unsigned HTMLDiagnostics::ProcessMacroPiece(raw_ostream &os,
519 const PathDiagnosticMacroPiece& P,
522 for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end();
525 if (const PathDiagnosticMacroPiece *MP =
526 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
527 num = ProcessMacroPiece(os, *MP, num);
531 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
532 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
534 "<table class=\"msgT\"><tr>"
535 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
536 EmitAlphaCounter(os, num++);
537 os << "</div></td><td valign=\"top\">"
538 << html::EscapeText(EP->getString())
539 << "</td></tr></table></div>\n";
546 void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
548 const char *HighlightStart,
549 const char *HighlightEnd) {
550 SourceManager &SM = R.getSourceMgr();
551 const LangOptions &LangOpts = R.getLangOpts();
553 SourceLocation InstantiationStart = SM.getExpansionLoc(Range.getBegin());
554 unsigned StartLineNo = SM.getExpansionLineNumber(InstantiationStart);
556 SourceLocation InstantiationEnd = SM.getExpansionLoc(Range.getEnd());
557 unsigned EndLineNo = SM.getExpansionLineNumber(InstantiationEnd);
559 if (EndLineNo < StartLineNo)
562 if (SM.getFileID(InstantiationStart) != BugFileID ||
563 SM.getFileID(InstantiationEnd) != BugFileID)
566 // Compute the column number of the end.
567 unsigned EndColNo = SM.getExpansionColumnNumber(InstantiationEnd);
568 unsigned OldEndColNo = EndColNo;
571 // Add in the length of the token, so that we cover multi-char tokens.
572 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
575 // Highlight the range. Make the span tag the outermost tag for the
579 InstantiationEnd.getLocWithOffset(EndColNo - OldEndColNo);
581 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);