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