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/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Lex/Lexer.h"
20 #include "clang/Lex/Preprocessor.h"
21 #include "clang/Rewrite/Core/HTMLRewrite.h"
22 #include "clang/Rewrite/Core/Rewriter.h"
23 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/raw_ostream.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   std::string Directory;
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   void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
48                             FilesMade *filesMade) override;
49 
50   StringRef getName() const override {
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                   FilesMade *filesMade);
67 };
68 
69 } // end anonymous namespace
70 
71 HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix,
72                                  const Preprocessor &pp)
73   : Directory(prefix), createdDir(false), noDir(false), PP(pp) {
74 }
75 
76 void ento::createHTMLDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
77                                         PathDiagnosticConsumers &C,
78                                         const std::string& prefix,
79                                         const Preprocessor &PP) {
80   C.push_back(new HTMLDiagnostics(prefix, PP));
81 }
82 
83 //===----------------------------------------------------------------------===//
84 // Report processing.
85 //===----------------------------------------------------------------------===//
86 
87 void HTMLDiagnostics::FlushDiagnosticsImpl(
88   std::vector<const PathDiagnostic *> &Diags,
89   FilesMade *filesMade) {
90   for (std::vector<const PathDiagnostic *>::iterator it = Diags.begin(),
91        et = Diags.end(); it != et; ++it) {
92     ReportDiag(**it, filesMade);
93   }
94 }
95 
96 void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
97                                  FilesMade *filesMade) {
98 
99   // Create the HTML directory if it is missing.
100   if (!createdDir) {
101     createdDir = true;
102     if (llvm::error_code ec = llvm::sys::fs::create_directories(Directory)) {
103       llvm::errs() << "warning: could not create directory '"
104                    << Directory << "': " << ec.message() << '\n';
105 
106       noDir = true;
107 
108       return;
109     }
110   }
111 
112   if (noDir)
113     return;
114 
115   // First flatten out the entire path to make it easier to use.
116   PathPieces path = D.path.flatten(/*ShouldFlattenMacros=*/false);
117 
118   // The path as already been prechecked that all parts of the path are
119   // from the same file and that it is non-empty.
120   const SourceManager &SMgr = (*path.begin())->getLocation().getManager();
121   assert(!path.empty());
122   FileID FID =
123     (*path.begin())->getLocation().asLocation().getExpansionLoc().getFileID();
124   assert(!FID.isInvalid());
125 
126   // Create a new rewriter to generate HTML.
127   Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOpts());
128 
129   // Process the path.
130   unsigned n = path.size();
131   unsigned max = n;
132 
133   for (PathPieces::const_reverse_iterator I = path.rbegin(),
134        E = path.rend();
135         I != E; ++I, --n)
136     HandlePiece(R, FID, **I, n, max);
137 
138   // Add line numbers, header, footer, etc.
139 
140   // unsigned FID = R.getSourceMgr().getMainFileID();
141   html::EscapeText(R, FID);
142   html::AddLineNumbers(R, FID);
143 
144   // If we have a preprocessor, relex the file and syntax highlight.
145   // We might not have a preprocessor if we come from a deserialized AST file,
146   // for example.
147 
148   html::SyntaxHighlight(R, FID, PP);
149   html::HighlightMacros(R, FID, PP);
150 
151   // Get the full directory name of the analyzed file.
152 
153   const FileEntry* Entry = SMgr.getFileEntryForID(FID);
154 
155   // This is a cludge; basically we want to append either the full
156   // working directory if we have no directory information.  This is
157   // a work in progress.
158 
159   llvm::SmallString<0> DirName;
160 
161   if (llvm::sys::path::is_relative(Entry->getName())) {
162     llvm::sys::fs::current_path(DirName);
163     DirName += '/';
164   }
165 
166   // Add the name of the file as an <h1> tag.
167 
168   {
169     std::string s;
170     llvm::raw_string_ostream os(s);
171 
172     os << "<!-- REPORTHEADER -->\n"
173       << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
174           "<tr><td class=\"rowname\">File:</td><td>"
175       << html::EscapeText(DirName)
176       << html::EscapeText(Entry->getName())
177       << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
178          "<a href=\"#EndPath\">line "
179       << (*path.rbegin())->getLocation().asLocation().getExpansionLineNumber()
180       << ", column "
181       << (*path.rbegin())->getLocation().asLocation().getExpansionColumnNumber()
182       << "</a></td></tr>\n"
183          "<tr><td class=\"rowname\">Description:</td><td>"
184       << D.getVerboseDescription() << "</td></tr>\n";
185 
186     // Output any other meta data.
187 
188     for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
189          I!=E; ++I) {
190       os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
191     }
192 
193     os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
194           "<h3>Annotated Source Code</h3>\n";
195 
196     R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
197   }
198 
199   // Embed meta-data tags.
200   {
201     std::string s;
202     llvm::raw_string_ostream os(s);
203 
204     StringRef BugDesc = D.getVerboseDescription();
205     if (!BugDesc.empty())
206       os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
207 
208     StringRef BugType = D.getBugType();
209     if (!BugType.empty())
210       os << "\n<!-- BUGTYPE " << BugType << " -->\n";
211 
212     StringRef BugCategory = D.getCategory();
213     if (!BugCategory.empty())
214       os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
215 
216     os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
217 
218     os << "\n<!-- BUGLINE "
219        << path.back()->getLocation().asLocation().getExpansionLineNumber()
220        << " -->\n";
221 
222     os << "\n<!-- BUGCOLUMN "
223       << path.back()->getLocation().asLocation().getExpansionColumnNumber()
224       << " -->\n";
225 
226     os << "\n<!-- BUGPATHLENGTH " << path.size() << " -->\n";
227 
228     // Mark the end of the tags.
229     os << "\n<!-- BUGMETAEND -->\n";
230 
231     // Insert the text.
232     R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
233   }
234 
235   // Add CSS, header, and footer.
236 
237   html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
238 
239   // Get the rewrite buffer.
240   const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
241 
242   if (!Buf) {
243     llvm::errs() << "warning: no diagnostics generated for main file.\n";
244     return;
245   }
246 
247   // Create a path for the target HTML file.
248   int FD;
249   SmallString<128> Model, ResultPath;
250   llvm::sys::path::append(Model, Directory, "report-%%%%%%.html");
251 
252   if (llvm::error_code EC =
253           llvm::sys::fs::createUniqueFile(Model.str(), FD, ResultPath)) {
254     llvm::errs() << "warning: could not create file in '" << Directory
255                  << "': " << EC.message() << '\n';
256     return;
257   }
258 
259   llvm::raw_fd_ostream os(FD, true);
260 
261   if (filesMade)
262     filesMade->addDiagnostic(D, getName(),
263                              llvm::sys::path::filename(ResultPath));
264 
265   // Emit the HTML to disk.
266   for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
267       os << *I;
268 }
269 
270 void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
271                                   const PathDiagnosticPiece& P,
272                                   unsigned num, unsigned max) {
273 
274   // For now, just draw a box above the line in question, and emit the
275   // warning.
276   FullSourceLoc Pos = P.getLocation().asLocation();
277 
278   if (!Pos.isValid())
279     return;
280 
281   SourceManager &SM = R.getSourceMgr();
282   assert(&Pos.getManager() == &SM && "SourceManagers are different!");
283   std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedExpansionLoc(Pos);
284 
285   if (LPosInfo.first != BugFileID)
286     return;
287 
288   const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
289   const char* FileStart = Buf->getBufferStart();
290 
291   // Compute the column number.  Rewind from the current position to the start
292   // of the line.
293   unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
294   const char *TokInstantiationPtr =Pos.getExpansionLoc().getCharacterData();
295   const char *LineStart = TokInstantiationPtr-ColNo;
296 
297   // Compute LineEnd.
298   const char *LineEnd = TokInstantiationPtr;
299   const char* FileEnd = Buf->getBufferEnd();
300   while (*LineEnd != '\n' && LineEnd != FileEnd)
301     ++LineEnd;
302 
303   // Compute the margin offset by counting tabs and non-tabs.
304   unsigned PosNo = 0;
305   for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
306     PosNo += *c == '\t' ? 8 : 1;
307 
308   // Create the html for the message.
309 
310   const char *Kind = 0;
311   switch (P.getKind()) {
312   case PathDiagnosticPiece::Call:
313       llvm_unreachable("Calls should already be handled");
314   case PathDiagnosticPiece::Event:  Kind = "Event"; break;
315   case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
316     // Setting Kind to "Control" is intentional.
317   case PathDiagnosticPiece::Macro: Kind = "Control"; break;
318   }
319 
320   std::string sbuf;
321   llvm::raw_string_ostream os(sbuf);
322 
323   os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
324 
325   if (num == max)
326     os << "EndPath";
327   else
328     os << "Path" << num;
329 
330   os << "\" class=\"msg";
331   if (Kind)
332     os << " msg" << Kind;
333   os << "\" style=\"margin-left:" << PosNo << "ex";
334 
335   // Output a maximum size.
336   if (!isa<PathDiagnosticMacroPiece>(P)) {
337     // Get the string and determining its maximum substring.
338     const std::string& Msg = P.getString();
339     unsigned max_token = 0;
340     unsigned cnt = 0;
341     unsigned len = Msg.size();
342 
343     for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
344       switch (*I) {
345       default:
346         ++cnt;
347         continue;
348       case ' ':
349       case '\t':
350       case '\n':
351         if (cnt > max_token) max_token = cnt;
352         cnt = 0;
353       }
354 
355     if (cnt > max_token)
356       max_token = cnt;
357 
358     // Determine the approximate size of the message bubble in em.
359     unsigned em;
360     const unsigned max_line = 120;
361 
362     if (max_token >= max_line)
363       em = max_token / 2;
364     else {
365       unsigned characters = max_line;
366       unsigned lines = len / max_line;
367 
368       if (lines > 0) {
369         for (; characters > max_token; --characters)
370           if (len / characters > lines) {
371             ++characters;
372             break;
373           }
374       }
375 
376       em = characters / 2;
377     }
378 
379     if (em < max_line/2)
380       os << "; max-width:" << em << "em";
381   }
382   else
383     os << "; max-width:100em";
384 
385   os << "\">";
386 
387   if (max > 1) {
388     os << "<table class=\"msgT\"><tr><td valign=\"top\">";
389     os << "<div class=\"PathIndex";
390     if (Kind) os << " PathIndex" << Kind;
391     os << "\">" << num << "</div>";
392 
393     if (num > 1) {
394       os << "</td><td><div class=\"PathNav\"><a href=\"#Path"
395          << (num - 1)
396          << "\" title=\"Previous event ("
397          << (num - 1)
398          << ")\">&#x2190;</a></div></td>";
399     }
400 
401     os << "</td><td>";
402   }
403 
404   if (const PathDiagnosticMacroPiece *MP =
405         dyn_cast<PathDiagnosticMacroPiece>(&P)) {
406 
407     os << "Within the expansion of the macro '";
408 
409     // Get the name of the macro by relexing it.
410     {
411       FullSourceLoc L = MP->getLocation().asLocation().getExpansionLoc();
412       assert(L.isFileID());
413       StringRef BufferInfo = L.getBufferData();
414       std::pair<FileID, unsigned> LocInfo = L.getDecomposedLoc();
415       const char* MacroName = LocInfo.second + BufferInfo.data();
416       Lexer rawLexer(SM.getLocForStartOfFile(LocInfo.first), PP.getLangOpts(),
417                      BufferInfo.begin(), MacroName, BufferInfo.end());
418 
419       Token TheTok;
420       rawLexer.LexFromRawLexer(TheTok);
421       for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
422         os << MacroName[i];
423     }
424 
425     os << "':\n";
426 
427     if (max > 1) {
428       os << "</td>";
429       if (num < max) {
430         os << "<td><div class=\"PathNav\"><a href=\"#";
431         if (num == max - 1)
432           os << "EndPath";
433         else
434           os << "Path" << (num + 1);
435         os << "\" title=\"Next event ("
436         << (num + 1)
437         << ")\">&#x2192;</a></div></td>";
438       }
439 
440       os << "</tr></table>";
441     }
442 
443     // Within a macro piece.  Write out each event.
444     ProcessMacroPiece(os, *MP, 0);
445   }
446   else {
447     os << html::EscapeText(P.getString());
448 
449     if (max > 1) {
450       os << "</td>";
451       if (num < max) {
452         os << "<td><div class=\"PathNav\"><a href=\"#";
453         if (num == max - 1)
454           os << "EndPath";
455         else
456           os << "Path" << (num + 1);
457         os << "\" title=\"Next event ("
458            << (num + 1)
459            << ")\">&#x2192;</a></div></td>";
460       }
461 
462       os << "</tr></table>";
463     }
464   }
465 
466   os << "</div></td></tr>";
467 
468   // Insert the new html.
469   unsigned DisplayPos = LineEnd - FileStart;
470   SourceLocation Loc =
471     SM.getLocForStartOfFile(LPosInfo.first).getLocWithOffset(DisplayPos);
472 
473   R.InsertTextBefore(Loc, os.str());
474 
475   // Now highlight the ranges.
476   ArrayRef<SourceRange> Ranges = P.getRanges();
477   for (ArrayRef<SourceRange>::iterator I = Ranges.begin(),
478                                        E = Ranges.end(); I != E; ++I) {
479     HighlightRange(R, LPosInfo.first, *I);
480   }
481 }
482 
483 static void EmitAlphaCounter(raw_ostream &os, unsigned n) {
484   unsigned x = n % ('z' - 'a');
485   n /= 'z' - 'a';
486 
487   if (n > 0)
488     EmitAlphaCounter(os, n);
489 
490   os << char('a' + x);
491 }
492 
493 unsigned HTMLDiagnostics::ProcessMacroPiece(raw_ostream &os,
494                                             const PathDiagnosticMacroPiece& P,
495                                             unsigned num) {
496 
497   for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end();
498         I!=E; ++I) {
499 
500     if (const PathDiagnosticMacroPiece *MP =
501           dyn_cast<PathDiagnosticMacroPiece>(*I)) {
502       num = ProcessMacroPiece(os, *MP, num);
503       continue;
504     }
505 
506     if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
507       os << "<div class=\"msg msgEvent\" style=\"width:94%; "
508             "margin-left:5px\">"
509             "<table class=\"msgT\"><tr>"
510             "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
511       EmitAlphaCounter(os, num++);
512       os << "</div></td><td valign=\"top\">"
513          << html::EscapeText(EP->getString())
514          << "</td></tr></table></div>\n";
515     }
516   }
517 
518   return num;
519 }
520 
521 void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
522                                      SourceRange Range,
523                                      const char *HighlightStart,
524                                      const char *HighlightEnd) {
525   SourceManager &SM = R.getSourceMgr();
526   const LangOptions &LangOpts = R.getLangOpts();
527 
528   SourceLocation InstantiationStart = SM.getExpansionLoc(Range.getBegin());
529   unsigned StartLineNo = SM.getExpansionLineNumber(InstantiationStart);
530 
531   SourceLocation InstantiationEnd = SM.getExpansionLoc(Range.getEnd());
532   unsigned EndLineNo = SM.getExpansionLineNumber(InstantiationEnd);
533 
534   if (EndLineNo < StartLineNo)
535     return;
536 
537   if (SM.getFileID(InstantiationStart) != BugFileID ||
538       SM.getFileID(InstantiationEnd) != BugFileID)
539     return;
540 
541   // Compute the column number of the end.
542   unsigned EndColNo = SM.getExpansionColumnNumber(InstantiationEnd);
543   unsigned OldEndColNo = EndColNo;
544 
545   if (EndColNo) {
546     // Add in the length of the token, so that we cover multi-char tokens.
547     EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
548   }
549 
550   // Highlight the range.  Make the span tag the outermost tag for the
551   // selected range.
552 
553   SourceLocation E =
554     InstantiationEnd.getLocWithOffset(EndColNo - OldEndColNo);
555 
556   html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
557 }
558