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