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