blob: 3c1a3b4111d1098db3886a2ee3acc4ab8661a70f [file] [log] [blame]
Ted Kremenek6efb0262008-03-27 06:17:42 +00001//===--- 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
David Blaikie0cc49432011-09-27 01:43:33 +000014#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Decl.h"
Ted Kremenek8cc48422008-03-27 07:35:49 +000017#include "clang/Basic/FileManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Basic/SourceManager.h"
Ted Kremenek6efb0262008-03-27 06:17:42 +000019#include "clang/Lex/Lexer.h"
Ted Kremenekb4947e42009-03-10 05:16:17 +000020#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Rewrite/Core/HTMLRewrite.h"
22#include "clang/Rewrite/Core/Rewriter.h"
23#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000024#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Michael J. Spencere503f892011-01-11 01:21:20 +000025#include "llvm/Support/FileSystem.h"
Ted Kremenek6efb0262008-03-27 06:17:42 +000026#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000027#include "llvm/Support/Path.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000028#include "llvm/Support/raw_ostream.h"
Sylvestre Ledru06aebc42014-06-14 09:28:27 +000029#include <sstream>
Ted Kremenek490b8c02009-10-08 17:44:41 +000030
Ted Kremenek6efb0262008-03-27 06:17:42 +000031using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000032using namespace ento;
Ted Kremenek6efb0262008-03-27 06:17:42 +000033
34//===----------------------------------------------------------------------===//
35// Boilerplate.
36//===----------------------------------------------------------------------===//
37
38namespace {
39
David Blaikie53c125d2011-09-26 00:51:36 +000040class HTMLDiagnostics : public PathDiagnosticConsumer {
Benjamin Kramerfbf914c2013-06-12 18:13:05 +000041 std::string Directory;
Ted Kremenek6efb0262008-03-27 06:17:42 +000042 bool createdDir, noDir;
Daniel Dunbarb5f20252009-11-05 02:41:58 +000043 const Preprocessor &PP;
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +000044 AnalyzerOptions &AnalyzerOpts;
Ted Kremenek6efb0262008-03-27 06:17:42 +000045public:
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +000046 HTMLDiagnostics(AnalyzerOptions &AnalyzerOpts, const std::string& prefix, const Preprocessor &pp);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000047
Alexander Kornienko34eb2072015-04-11 02:00:23 +000048 ~HTMLDiagnostics() override { FlushDiagnostics(nullptr); }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000049
Craig Topperfb6b25b2014-03-15 04:29:04 +000050 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
51 FilesMade *filesMade) override;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000052
Craig Topperfb6b25b2014-03-15 04:29:04 +000053 StringRef getName() const override {
Ted Kremenek5e860442009-11-05 02:09:23 +000054 return "HTMLDiagnostics";
55 }
Mike Stump11289f42009-09-09 15:08:12 +000056
Ted Kremenek5ef32db2011-08-12 23:37:29 +000057 unsigned ProcessMacroPiece(raw_ostream &os,
Ted Kremenekb4947e42009-03-10 05:16:17 +000058 const PathDiagnosticMacroPiece& P,
59 unsigned num);
Mike Stump11289f42009-09-09 15:08:12 +000060
Chris Lattnerd32480d2009-01-17 06:22:33 +000061 void HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenekc99332c2008-07-23 23:18:15 +000062 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Mike Stump11289f42009-09-09 15:08:12 +000063
Douglas Gregor87f95b02009-02-26 21:00:50 +000064 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
65 const char *HighlightStart = "<span class=\"mrange\">",
66 const char *HighlightEnd = "</span>");
Ted Kremenek9718c9e2008-04-22 16:15:03 +000067
Ted Kremenek5e860442009-11-05 02:09:23 +000068 void ReportDiag(const PathDiagnostic& D,
Ted Kremenek9bf9af92012-08-16 17:45:23 +000069 FilesMade *filesMade);
Ted Kremenek6efb0262008-03-27 06:17:42 +000070};
Mike Stump11289f42009-09-09 15:08:12 +000071
Ted Kremenek6efb0262008-03-27 06:17:42 +000072} // end anonymous namespace
73
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +000074HTMLDiagnostics::HTMLDiagnostics(AnalyzerOptions &AnalyzerOpts,
75 const std::string& prefix,
Daniel Dunbarb5f20252009-11-05 02:41:58 +000076 const Preprocessor &pp)
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +000077 : Directory(prefix), createdDir(false), noDir(false), PP(pp), AnalyzerOpts(AnalyzerOpts) {
Ted Kremenek6efb0262008-03-27 06:17:42 +000078}
79
Ted Kremenek3a081a02012-12-19 01:35:35 +000080void ento::createHTMLDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
81 PathDiagnosticConsumers &C,
Ted Kremenek9bf9af92012-08-16 17:45:23 +000082 const std::string& prefix,
83 const Preprocessor &PP) {
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +000084 C.push_back(new HTMLDiagnostics(AnalyzerOpts, prefix, PP));
Ted Kremenek6efb0262008-03-27 06:17:42 +000085}
86
87//===----------------------------------------------------------------------===//
88// Report processing.
89//===----------------------------------------------------------------------===//
90
Ted Kremenek8e4c4262012-01-25 23:47:14 +000091void HTMLDiagnostics::FlushDiagnosticsImpl(
92 std::vector<const PathDiagnostic *> &Diags,
Ted Kremenek9bf9af92012-08-16 17:45:23 +000093 FilesMade *filesMade) {
Ted Kremenek8e4c4262012-01-25 23:47:14 +000094 for (std::vector<const PathDiagnostic *>::iterator it = Diags.begin(),
95 et = Diags.end(); it != et; ++it) {
Ted Kremenek9bf9af92012-08-16 17:45:23 +000096 ReportDiag(**it, filesMade);
Ted Kremenek9718c9e2008-04-22 16:15:03 +000097 }
Ted Kremenek9718c9e2008-04-22 16:15:03 +000098}
99
Ted Kremenek5e860442009-11-05 02:09:23 +0000100void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
Ted Kremenek9bf9af92012-08-16 17:45:23 +0000101 FilesMade *filesMade) {
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000102
Ted Kremenek6efb0262008-03-27 06:17:42 +0000103 // Create the HTML directory if it is missing.
Ted Kremenek6efb0262008-03-27 06:17:42 +0000104 if (!createdDir) {
105 createdDir = true;
Rafael Espindolac0809172014-06-12 14:02:15 +0000106 if (std::error_code ec = llvm::sys::fs::create_directories(Directory)) {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000107 llvm::errs() << "warning: could not create directory '"
Benjamin Kramerfbf914c2013-06-12 18:13:05 +0000108 << Directory << "': " << ec.message() << '\n';
Mike Stump11289f42009-09-09 15:08:12 +0000109
Ted Kremenek6efb0262008-03-27 06:17:42 +0000110 noDir = true;
Mike Stump11289f42009-09-09 15:08:12 +0000111
Ted Kremenek6efb0262008-03-27 06:17:42 +0000112 return;
113 }
114 }
Mike Stump11289f42009-09-09 15:08:12 +0000115
Ted Kremenek6efb0262008-03-27 06:17:42 +0000116 if (noDir)
117 return;
Mike Stump11289f42009-09-09 15:08:12 +0000118
Ted Kremenek60a78202012-02-24 06:00:00 +0000119 // First flatten out the entire path to make it easier to use.
Jordan Rose3eb3cd42012-08-03 23:08:54 +0000120 PathPieces path = D.path.flatten(/*ShouldFlattenMacros=*/false);
Ted Kremenek0f70a6f2012-02-28 23:27:39 +0000121
122 // The path as already been prechecked that all parts of the path are
123 // from the same file and that it is non-empty.
Ted Kremenekeba09fa2013-04-29 23:12:59 +0000124 const SourceManager &SMgr = (*path.begin())->getLocation().getManager();
Ted Kremenek0f70a6f2012-02-28 23:27:39 +0000125 assert(!path.empty());
126 FileID FID =
Ted Kremenekeba09fa2013-04-29 23:12:59 +0000127 (*path.begin())->getLocation().asLocation().getExpansionLoc().getFileID();
Ted Kremenek0f70a6f2012-02-28 23:27:39 +0000128 assert(!FID.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000129
Ted Kremenekc99332c2008-07-23 23:18:15 +0000130 // Create a new rewriter to generate HTML.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000131 Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOpts());
Mike Stump11289f42009-09-09 15:08:12 +0000132
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000133 // Get the function/method name
134 SmallString<128> declName("unknown");
135 int offsetDecl = 0;
136 if (const Decl *DeclWithIssue = D.getDeclWithIssue()) {
137 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DeclWithIssue)) {
138 declName = ND->getDeclName().getAsString();
139 }
140
141 if (const Stmt *Body = DeclWithIssue->getBody()) {
142 // Retrieve the relative position of the declaration which will be used
143 // for the file name
144 FullSourceLoc L(
145 SMgr.getExpansionLoc((*path.rbegin())->getLocation().asLocation()),
146 SMgr);
147 FullSourceLoc FunL(SMgr.getExpansionLoc(Body->getLocStart()), SMgr);
148 offsetDecl = L.getExpansionLineNumber() - FunL.getExpansionLineNumber();
149 }
150 }
151
Mike Stump11289f42009-09-09 15:08:12 +0000152 // Process the path.
Ted Kremenek60a78202012-02-24 06:00:00 +0000153 unsigned n = path.size();
Ted Kremeneka9590d12008-03-31 23:30:12 +0000154 unsigned max = n;
Mike Stump11289f42009-09-09 15:08:12 +0000155
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000156 for (PathPieces::const_reverse_iterator I = path.rbegin(),
Ted Kremenek60a78202012-02-24 06:00:00 +0000157 E = path.rend();
Ted Kremenekafa6e2492012-02-08 04:32:34 +0000158 I != E; ++I, --n)
Ted Kremenekeba09fa2013-04-29 23:12:59 +0000159 HandlePiece(R, FID, **I, n, max);
Mike Stump11289f42009-09-09 15:08:12 +0000160
Ted Kremenek6efb0262008-03-27 06:17:42 +0000161 // Add line numbers, header, footer, etc.
Mike Stump11289f42009-09-09 15:08:12 +0000162
Chris Lattnerd32480d2009-01-17 06:22:33 +0000163 // unsigned FID = R.getSourceMgr().getMainFileID();
164 html::EscapeText(R, FID);
165 html::AddLineNumbers(R, FID);
Mike Stump11289f42009-09-09 15:08:12 +0000166
Ted Kremenekf2e6fcf2008-04-16 16:39:56 +0000167 // If we have a preprocessor, relex the file and syntax highlight.
168 // We might not have a preprocessor if we come from a deserialized AST file,
169 // for example.
Mike Stump11289f42009-09-09 15:08:12 +0000170
Daniel Dunbarb5f20252009-11-05 02:41:58 +0000171 html::SyntaxHighlight(R, FID, PP);
172 html::HighlightMacros(R, FID, PP);
Mike Stump11289f42009-09-09 15:08:12 +0000173
Ted Kremenek3276af42008-04-02 20:44:16 +0000174 // Get the full directory name of the analyzed file.
175
Chris Lattnerd32480d2009-01-17 06:22:33 +0000176 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Mike Stump11289f42009-09-09 15:08:12 +0000177
Ted Kremenek64fa3be2008-04-24 23:37:03 +0000178 // This is a cludge; basically we want to append either the full
179 // working directory if we have no directory information. This is
180 // a work in progress.
181
Benjamin Kramerfbf914c2013-06-12 18:13:05 +0000182 llvm::SmallString<0> DirName;
Mike Stump11289f42009-09-09 15:08:12 +0000183
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000184 if (llvm::sys::path::is_relative(Entry->getName())) {
Benjamin Kramerfbf914c2013-06-12 18:13:05 +0000185 llvm::sys::fs::current_path(DirName);
186 DirName += '/';
Ted Kremenek5f56cbb2008-05-02 22:04:53 +0000187 }
Mike Stump11289f42009-09-09 15:08:12 +0000188
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000189 int LineNumber = (*path.rbegin())->getLocation().asLocation().getExpansionLineNumber();
190 int ColumnNumber = (*path.rbegin())->getLocation().asLocation().getExpansionColumnNumber();
191
Mike Stump11289f42009-09-09 15:08:12 +0000192 // Add the name of the file as an <h1> tag.
193
Ted Kremenek8cc48422008-03-27 07:35:49 +0000194 {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000195 std::string s;
196 llvm::raw_string_ostream os(s);
Mike Stump11289f42009-09-09 15:08:12 +0000197
Ted Kremenekb76a3f442008-09-22 17:33:32 +0000198 os << "<!-- REPORTHEADER -->\n"
Ted Kremenek0bb09092009-04-01 06:13:56 +0000199 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek7d6219f2008-04-15 21:25:08 +0000200 "<tr><td class=\"rowname\">File:</td><td>"
Ted Kremenek0bb09092009-04-01 06:13:56 +0000201 << html::EscapeText(DirName)
202 << html::EscapeText(Entry->getName())
203 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
Mike Stump11289f42009-09-09 15:08:12 +0000204 "<a href=\"#EndPath\">line "
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000205 << LineNumber
Ted Kremenek0bb09092009-04-01 06:13:56 +0000206 << ", column "
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000207 << ColumnNumber
Ted Kremenek0bb09092009-04-01 06:13:56 +0000208 << "</a></td></tr>\n"
209 "<tr><td class=\"rowname\">Description:</td><td>"
Jordan Rosecc0b1bf2012-08-31 00:36:26 +0000210 << D.getVerboseDescription() << "</td></tr>\n";
Mike Stump11289f42009-09-09 15:08:12 +0000211
Ted Kremenekb0f87c42008-04-30 23:47:44 +0000212 // Output any other meta data.
Mike Stump11289f42009-09-09 15:08:12 +0000213
Ted Kremenekb0f87c42008-04-30 23:47:44 +0000214 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 }
Mike Stump11289f42009-09-09 15:08:12 +0000218
Ted Kremenekb76a3f442008-09-22 17:33:32 +0000219 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
Mike Stump11289f42009-09-09 15:08:12 +0000220 "<h3>Annotated Source Code</h3>\n";
221
Daniel Dunbar62c850f2009-08-19 20:32:38 +0000222 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremeneke6eed292008-04-02 07:04:46 +0000223 }
Mike Stump11289f42009-09-09 15:08:12 +0000224
Ted Kremenek3276af42008-04-02 20:44:16 +0000225 // Embed meta-data tags.
Ted Kremenek3276af42008-04-02 20:44:16 +0000226 {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000227 std::string s;
228 llvm::raw_string_ostream os(s);
Mike Stump11289f42009-09-09 15:08:12 +0000229
Jordan Rosecc0b1bf2012-08-31 00:36:26 +0000230 StringRef BugDesc = D.getVerboseDescription();
Ted Kremenek3724cde2009-08-03 23:44:55 +0000231 if (!BugDesc.empty())
232 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Mike Stump11289f42009-09-09 15:08:12 +0000233
Jordan Rosecc0b1bf2012-08-31 00:36:26 +0000234 StringRef BugType = D.getBugType();
Ted Kremenek3724cde2009-08-03 23:44:55 +0000235 if (!BugType.empty())
236 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
Mike Stump11289f42009-09-09 15:08:12 +0000237
Jordan Rosecc0b1bf2012-08-31 00:36:26 +0000238 StringRef BugCategory = D.getCategory();
Ted Kremenek3724cde2009-08-03 23:44:55 +0000239 if (!BugCategory.empty())
240 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
241
Ted Kremenek64fa3be2008-04-24 23:37:03 +0000242 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenek3724cde2009-08-03 23:44:55 +0000243
Sylvestre Ledruac5a08a2014-06-14 08:49:40 +0000244 os << "\n<!-- FILENAME " << llvm::sys::path::filename(Entry->getName()) << " -->\n";
245
246 os << "\n<!-- FUNCTIONNAME " << declName << " -->\n";
247
Chris Lattner8a425862009-01-16 07:36:28 +0000248 os << "\n<!-- BUGLINE "
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000249 << LineNumber
Ted Kremenek0bb09092009-04-01 06:13:56 +0000250 << " -->\n";
Ted Kremenek3724cde2009-08-03 23:44:55 +0000251
Jordan Rose11288ce2013-11-15 02:11:11 +0000252 os << "\n<!-- BUGCOLUMN "
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000253 << ColumnNumber
Jordan Rose11288ce2013-11-15 02:11:11 +0000254 << " -->\n";
255
Ted Kremenek60a78202012-02-24 06:00:00 +0000256 os << "\n<!-- BUGPATHLENGTH " << path.size() << " -->\n";
Mike Stump11289f42009-09-09 15:08:12 +0000257
Ted Kremenek3724cde2009-08-03 23:44:55 +0000258 // Mark the end of the tags.
259 os << "\n<!-- BUGMETAEND -->\n";
Mike Stump11289f42009-09-09 15:08:12 +0000260
Ted Kremenek3724cde2009-08-03 23:44:55 +0000261 // Insert the text.
Daniel Dunbar62c850f2009-08-19 20:32:38 +0000262 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek3276af42008-04-02 20:44:16 +0000263 }
Mike Stump11289f42009-09-09 15:08:12 +0000264
Ted Kremenek6efb0262008-03-27 06:17:42 +0000265 // Add CSS, header, and footer.
Mike Stump11289f42009-09-09 15:08:12 +0000266
Chris Lattnerd32480d2009-01-17 06:22:33 +0000267 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000268
Ted Kremenek6efb0262008-03-27 06:17:42 +0000269 // Get the rewrite buffer.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000270 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Mike Stump11289f42009-09-09 15:08:12 +0000271
Ted Kremenek6efb0262008-03-27 06:17:42 +0000272 if (!Buf) {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000273 llvm::errs() << "warning: no diagnostics generated for main file.\n";
Ted Kremenek6efb0262008-03-27 06:17:42 +0000274 return;
275 }
276
Ted Kremenek490b8c02009-10-08 17:44:41 +0000277 // Create a path for the target HTML file.
Benjamin Kramerfbf914c2013-06-12 18:13:05 +0000278 int FD;
279 SmallString<128> Model, ResultPath;
Mike Stump11289f42009-09-09 15:08:12 +0000280
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000281 if (!AnalyzerOpts.shouldWriteStableReportFilename()) {
282 llvm::sys::path::append(Model, Directory, "report-%%%%%%.html");
283
284 if (std::error_code EC =
Yaron Keren92e1b622015-03-18 10:17:07 +0000285 llvm::sys::fs::createUniqueFile(Model, FD, ResultPath)) {
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000286 llvm::errs() << "warning: could not create file in '" << Directory
287 << "': " << EC.message() << '\n';
288 return;
289 }
290
291 } else {
292 int i = 1;
293 std::error_code EC;
294 do {
295 // Find a filename which is not already used
Sylvestre Ledru06aebc42014-06-14 09:28:27 +0000296 std::stringstream filename;
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000297 Model = "";
Sylvestre Ledru06aebc42014-06-14 09:28:27 +0000298 filename << "report-"
299 << llvm::sys::path::filename(Entry->getName()).str()
300 << "-" << declName.c_str()
301 << "-" << offsetDecl
302 << "-" << i << ".html";
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000303 llvm::sys::path::append(Model, Directory,
Sylvestre Ledru06aebc42014-06-14 09:28:27 +0000304 filename.str());
Yaron Keren92e1b622015-03-18 10:17:07 +0000305 EC = llvm::sys::fs::openFileForWrite(Model,
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000306 FD,
307 llvm::sys::fs::F_RW |
308 llvm::sys::fs::F_Excl);
309 if (EC && EC != std::errc::file_exists) {
Yaron Keren92e1b622015-03-18 10:17:07 +0000310 llvm::errs() << "warning: could not create file '" << Model
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000311 << "': " << EC.message() << '\n';
312 return;
313 }
314 i++;
315 } while (EC);
Ted Kremenek6efb0262008-03-27 06:17:42 +0000316 }
Mike Stump11289f42009-09-09 15:08:12 +0000317
Benjamin Kramerfbf914c2013-06-12 18:13:05 +0000318 llvm::raw_fd_ostream os(FD, true);
319
320 if (filesMade)
321 filesMade->addDiagnostic(D, getName(),
322 llvm::sys::path::filename(ResultPath));
Ted Kremenek490b8c02009-10-08 17:44:41 +0000323
Ted Kremenek6efb0262008-03-27 06:17:42 +0000324 // Emit the HTML to disk.
Ted Kremenek076d1332008-04-20 01:02:33 +0000325 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000326 os << *I;
Ted Kremenek6efb0262008-03-27 06:17:42 +0000327}
328
Chris Lattnerd32480d2009-01-17 06:22:33 +0000329void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek6efb0262008-03-27 06:17:42 +0000330 const PathDiagnosticPiece& P,
Ted Kremeneka9590d12008-03-31 23:30:12 +0000331 unsigned num, unsigned max) {
Mike Stump11289f42009-09-09 15:08:12 +0000332
Ted Kremenek6efb0262008-03-27 06:17:42 +0000333 // For now, just draw a box above the line in question, and emit the
334 // warning.
Ted Kremenek0bb09092009-04-01 06:13:56 +0000335 FullSourceLoc Pos = P.getLocation().asLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000336
Ted Kremenek6efb0262008-03-27 06:17:42 +0000337 if (!Pos.isValid())
Mike Stump11289f42009-09-09 15:08:12 +0000338 return;
339
Chris Lattnerd32480d2009-01-17 06:22:33 +0000340 SourceManager &SM = R.getSourceMgr();
Chris Lattnere4ad4172009-02-04 00:55:58 +0000341 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
Chandler Carruthc7ca5212011-07-25 20:52:32 +0000342 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedExpansionLoc(Pos);
Mike Stump11289f42009-09-09 15:08:12 +0000343
Chris Lattnere4ad4172009-02-04 00:55:58 +0000344 if (LPosInfo.first != BugFileID)
Ted Kremenek6efb0262008-03-27 06:17:42 +0000345 return;
Mike Stump11289f42009-09-09 15:08:12 +0000346
Chris Lattnere4ad4172009-02-04 00:55:58 +0000347 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Mike Stump11289f42009-09-09 15:08:12 +0000348 const char* FileStart = Buf->getBufferStart();
349
Ted Kremenek6efb0262008-03-27 06:17:42 +0000350 // Compute the column number. Rewind from the current position to the start
351 // of the line.
Chris Lattnere4ad4172009-02-04 00:55:58 +0000352 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
Chandler Carruth35f53202011-07-25 16:49:02 +0000353 const char *TokInstantiationPtr =Pos.getExpansionLoc().getCharacterData();
Chris Lattner8a425862009-01-16 07:36:28 +0000354 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremeneke6d24192008-05-06 23:42:18 +0000355
Ted Kremenekea3a9e22009-02-18 22:10:00 +0000356 // Compute LineEnd.
Chris Lattner8a425862009-01-16 07:36:28 +0000357 const char *LineEnd = TokInstantiationPtr;
Ted Kremenekea3a9e22009-02-18 22:10:00 +0000358 const char* FileEnd = Buf->getBufferEnd();
359 while (*LineEnd != '\n' && LineEnd != FileEnd)
360 ++LineEnd;
Mike Stump11289f42009-09-09 15:08:12 +0000361
Ted Kremenek01fa5d22008-03-31 21:40:14 +0000362 // Compute the margin offset by counting tabs and non-tabs.
Mike Stump11289f42009-09-09 15:08:12 +0000363 unsigned PosNo = 0;
Chris Lattner8a425862009-01-16 07:36:28 +0000364 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremeneke6d24192008-05-06 23:42:18 +0000365 PosNo += *c == '\t' ? 8 : 1;
Mike Stump11289f42009-09-09 15:08:12 +0000366
Ted Kremenek6efb0262008-03-27 06:17:42 +0000367 // Create the html for the message.
Ted Kremenekb4947e42009-03-10 05:16:17 +0000368
Craig Topper0dbb7832014-05-27 02:45:47 +0000369 const char *Kind = nullptr;
Ted Kremenekb4947e42009-03-10 05:16:17 +0000370 switch (P.getKind()) {
Ted Kremenek60a78202012-02-24 06:00:00 +0000371 case PathDiagnosticPiece::Call:
372 llvm_unreachable("Calls should already be handled");
Mike Stump281d6d72010-01-20 02:03:14 +0000373 case PathDiagnosticPiece::Event: Kind = "Event"; break;
374 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
375 // Setting Kind to "Control" is intentional.
376 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
Ted Kremenekb4947e42009-03-10 05:16:17 +0000377 }
Mike Stump11289f42009-09-09 15:08:12 +0000378
Ted Kremenekb4947e42009-03-10 05:16:17 +0000379 std::string sbuf;
380 llvm::raw_string_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +0000381
Ted Kremenekb4947e42009-03-10 05:16:17 +0000382 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
Mike Stump11289f42009-09-09 15:08:12 +0000383
Ted Kremenekb4947e42009-03-10 05:16:17 +0000384 if (num == max)
385 os << "EndPath";
386 else
387 os << "Path" << num;
Mike Stump11289f42009-09-09 15:08:12 +0000388
Ted Kremenekb4947e42009-03-10 05:16:17 +0000389 os << "\" class=\"msg";
390 if (Kind)
Mike Stump11289f42009-09-09 15:08:12 +0000391 os << " msg" << Kind;
Ted Kremenekb4947e42009-03-10 05:16:17 +0000392 os << "\" style=\"margin-left:" << PosNo << "ex";
Mike Stump11289f42009-09-09 15:08:12 +0000393
Ted Kremenekb4947e42009-03-10 05:16:17 +0000394 // Output a maximum size.
395 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremenek80b2b162008-09-21 18:52:59 +0000396 // Get the string and determining its maximum substring.
397 const std::string& Msg = P.getString();
398 unsigned max_token = 0;
399 unsigned cnt = 0;
400 unsigned len = Msg.size();
Mike Stump11289f42009-09-09 15:08:12 +0000401
Ted Kremenek80b2b162008-09-21 18:52:59 +0000402 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
403 switch (*I) {
Mike Stump281d6d72010-01-20 02:03:14 +0000404 default:
405 ++cnt;
406 continue;
407 case ' ':
408 case '\t':
409 case '\n':
410 if (cnt > max_token) max_token = cnt;
411 cnt = 0;
Ted Kremenek80b2b162008-09-21 18:52:59 +0000412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
Ted Kremenekb4947e42009-03-10 05:16:17 +0000414 if (cnt > max_token)
415 max_token = cnt;
Mike Stump11289f42009-09-09 15:08:12 +0000416
Ted Kremenekb4947e42009-03-10 05:16:17 +0000417 // Determine the approximate size of the message bubble in em.
Ted Kremenek80b2b162008-09-21 18:52:59 +0000418 unsigned em;
Ted Kremenek7f16ed42009-03-02 23:06:15 +0000419 const unsigned max_line = 120;
Mike Stump11289f42009-09-09 15:08:12 +0000420
Ted Kremenek80b2b162008-09-21 18:52:59 +0000421 if (max_token >= max_line)
422 em = max_token / 2;
423 else {
424 unsigned characters = max_line;
425 unsigned lines = len / max_line;
Mike Stump11289f42009-09-09 15:08:12 +0000426
Ted Kremenek80b2b162008-09-21 18:52:59 +0000427 if (lines > 0) {
428 for (; characters > max_token; --characters)
429 if (len / characters > lines) {
430 ++characters;
431 break;
432 }
433 }
Mike Stump11289f42009-09-09 15:08:12 +0000434
Ted Kremenek80b2b162008-09-21 18:52:59 +0000435 em = characters / 2;
436 }
Mike Stump11289f42009-09-09 15:08:12 +0000437
Ted Kremenekb4947e42009-03-10 05:16:17 +0000438 if (em < max_line/2)
Mike Stump11289f42009-09-09 15:08:12 +0000439 os << "; max-width:" << em << "em";
Ted Kremenekb4947e42009-03-10 05:16:17 +0000440 }
441 else
442 os << "; max-width:100em";
Mike Stump11289f42009-09-09 15:08:12 +0000443
Ted Kremenekb4947e42009-03-10 05:16:17 +0000444 os << "\">";
Mike Stump11289f42009-09-09 15:08:12 +0000445
Ted Kremenekb4947e42009-03-10 05:16:17 +0000446 if (max > 1) {
447 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
448 os << "<div class=\"PathIndex";
449 if (Kind) os << " PathIndex" << Kind;
450 os << "\">" << num << "</div>";
Jordan Rose11790a42012-08-02 02:26:19 +0000451
452 if (num > 1) {
453 os << "</td><td><div class=\"PathNav\"><a href=\"#Path"
454 << (num - 1)
455 << "\" title=\"Previous event ("
456 << (num - 1)
457 << ")\">&#x2190;</a></div></td>";
458 }
459
Ted Kremenekb4947e42009-03-10 05:16:17 +0000460 os << "</td><td>";
461 }
462
463 if (const PathDiagnosticMacroPiece *MP =
Mike Stump11289f42009-09-09 15:08:12 +0000464 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
Ted Kremenekb4947e42009-03-10 05:16:17 +0000465
466 os << "Within the expansion of the macro '";
Mike Stump11289f42009-09-09 15:08:12 +0000467
Ted Kremenekb4947e42009-03-10 05:16:17 +0000468 // Get the name of the macro by relexing it.
469 {
Chandler Carruth35f53202011-07-25 16:49:02 +0000470 FullSourceLoc L = MP->getLocation().asLocation().getExpansionLoc();
Ted Kremenekb4947e42009-03-10 05:16:17 +0000471 assert(L.isFileID());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000472 StringRef BufferInfo = L.getBufferData();
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +0000473 std::pair<FileID, unsigned> LocInfo = L.getDecomposedLoc();
474 const char* MacroName = LocInfo.second + BufferInfo.data();
475 Lexer rawLexer(SM.getLocForStartOfFile(LocInfo.first), PP.getLangOpts(),
476 BufferInfo.begin(), MacroName, BufferInfo.end());
Mike Stump11289f42009-09-09 15:08:12 +0000477
Ted Kremenekb4947e42009-03-10 05:16:17 +0000478 Token TheTok;
479 rawLexer.LexFromRawLexer(TheTok);
480 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
481 os << MacroName[i];
Ted Kremenekc62af6c2009-03-02 23:05:40 +0000482 }
Mike Stump11289f42009-09-09 15:08:12 +0000483
Ted Kremenekb4947e42009-03-10 05:16:17 +0000484 os << "':\n";
Mike Stump11289f42009-09-09 15:08:12 +0000485
Jordan Rosefa49c922012-08-02 02:43:42 +0000486 if (max > 1) {
487 os << "</td>";
488 if (num < max) {
489 os << "<td><div class=\"PathNav\"><a href=\"#";
490 if (num == max - 1)
491 os << "EndPath";
492 else
493 os << "Path" << (num + 1);
494 os << "\" title=\"Next event ("
495 << (num + 1)
496 << ")\">&#x2192;</a></div></td>";
497 }
498
499 os << "</tr></table>";
500 }
Ted Kremenekb4947e42009-03-10 05:16:17 +0000501
502 // Within a macro piece. Write out each event.
503 ProcessMacroPiece(os, *MP, 0);
504 }
505 else {
506 os << html::EscapeText(P.getString());
Mike Stump11289f42009-09-09 15:08:12 +0000507
Jordan Rose11790a42012-08-02 02:26:19 +0000508 if (max > 1) {
509 os << "</td>";
510 if (num < max) {
511 os << "<td><div class=\"PathNav\"><a href=\"#";
512 if (num == max - 1)
513 os << "EndPath";
514 else
515 os << "Path" << (num + 1);
516 os << "\" title=\"Next event ("
517 << (num + 1)
518 << ")\">&#x2192;</a></div></td>";
519 }
Sylvestre Ledru9882e1a2014-06-14 08:45:32 +0000520
Jordan Rose11790a42012-08-02 02:26:19 +0000521 os << "</tr></table>";
522 }
Ted Kremenek80b2b162008-09-21 18:52:59 +0000523 }
Mike Stump11289f42009-09-09 15:08:12 +0000524
Ted Kremenekb4947e42009-03-10 05:16:17 +0000525 os << "</div></td></tr>";
526
527 // Insert the new html.
Mike Stump11289f42009-09-09 15:08:12 +0000528 unsigned DisplayPos = LineEnd - FileStart;
529 SourceLocation Loc =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000530 SM.getLocForStartOfFile(LPosInfo.first).getLocWithOffset(DisplayPos);
Ted Kremenekb4947e42009-03-10 05:16:17 +0000531
Daniel Dunbar62c850f2009-08-19 20:32:38 +0000532 R.InsertTextBefore(Loc, os.str());
Ted Kremenekb4947e42009-03-10 05:16:17 +0000533
Mike Stump11289f42009-09-09 15:08:12 +0000534 // Now highlight the ranges.
Ted Kremenek1e602732012-08-16 17:45:29 +0000535 ArrayRef<SourceRange> Ranges = P.getRanges();
536 for (ArrayRef<SourceRange>::iterator I = Ranges.begin(),
537 E = Ranges.end(); I != E; ++I) {
Chris Lattnere4ad4172009-02-04 00:55:58 +0000538 HighlightRange(R, LPosInfo.first, *I);
Ted Kremenek1e602732012-08-16 17:45:29 +0000539 }
Ted Kremenek6efb0262008-03-27 06:17:42 +0000540}
541
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000542static void EmitAlphaCounter(raw_ostream &os, unsigned n) {
Benjamin Kramer5ac3b0b2010-03-13 11:34:41 +0000543 unsigned x = n % ('z' - 'a');
544 n /= 'z' - 'a';
Ted Kremenekb4947e42009-03-10 05:16:17 +0000545
Benjamin Kramer5ac3b0b2010-03-13 11:34:41 +0000546 if (n > 0)
547 EmitAlphaCounter(os, n);
Mike Stump11289f42009-09-09 15:08:12 +0000548
Benjamin Kramer5ac3b0b2010-03-13 11:34:41 +0000549 os << char('a' + x);
Ted Kremenekb4947e42009-03-10 05:16:17 +0000550}
551
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000552unsigned HTMLDiagnostics::ProcessMacroPiece(raw_ostream &os,
Ted Kremenekb4947e42009-03-10 05:16:17 +0000553 const PathDiagnosticMacroPiece& P,
554 unsigned num) {
Mike Stump11289f42009-09-09 15:08:12 +0000555
Ted Kremenekeba09fa2013-04-29 23:12:59 +0000556 for (PathPieces::const_iterator I = P.subPieces.begin(), E=P.subPieces.end();
Ted Kremenekb4947e42009-03-10 05:16:17 +0000557 I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000558
Ted Kremenekb4947e42009-03-10 05:16:17 +0000559 if (const PathDiagnosticMacroPiece *MP =
Ted Kremenekeba09fa2013-04-29 23:12:59 +0000560 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
Ted Kremenekb4947e42009-03-10 05:16:17 +0000561 num = ProcessMacroPiece(os, *MP, num);
562 continue;
563 }
564
Ted Kremenekeba09fa2013-04-29 23:12:59 +0000565 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
Ted Kremenekb4947e42009-03-10 05:16:17 +0000566 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
567 "margin-left:5px\">"
568 "<table class=\"msgT\"><tr>"
569 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
570 EmitAlphaCounter(os, num++);
571 os << "</div></td><td valign=\"top\">"
572 << html::EscapeText(EP->getString())
573 << "</td></tr></table></div>\n";
574 }
575 }
Mike Stump11289f42009-09-09 15:08:12 +0000576
Ted Kremenekb4947e42009-03-10 05:16:17 +0000577 return num;
578}
579
Chris Lattnerd32480d2009-01-17 06:22:33 +0000580void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor87f95b02009-02-26 21:00:50 +0000581 SourceRange Range,
582 const char *HighlightStart,
583 const char *HighlightEnd) {
Chris Lattner184e65d2009-04-14 23:22:57 +0000584 SourceManager &SM = R.getSourceMgr();
585 const LangOptions &LangOpts = R.getLangOpts();
Mike Stump11289f42009-09-09 15:08:12 +0000586
Chandler Carruth35f53202011-07-25 16:49:02 +0000587 SourceLocation InstantiationStart = SM.getExpansionLoc(Range.getBegin());
Chandler Carruthd48db212011-07-25 21:09:52 +0000588 unsigned StartLineNo = SM.getExpansionLineNumber(InstantiationStart);
Mike Stump11289f42009-09-09 15:08:12 +0000589
Chandler Carruth35f53202011-07-25 16:49:02 +0000590 SourceLocation InstantiationEnd = SM.getExpansionLoc(Range.getEnd());
Chandler Carruthd48db212011-07-25 21:09:52 +0000591 unsigned EndLineNo = SM.getExpansionLineNumber(InstantiationEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000592
Ted Kremenek6efb0262008-03-27 06:17:42 +0000593 if (EndLineNo < StartLineNo)
594 return;
Mike Stump11289f42009-09-09 15:08:12 +0000595
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000596 if (SM.getFileID(InstantiationStart) != BugFileID ||
597 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek6efb0262008-03-27 06:17:42 +0000598 return;
Mike Stump11289f42009-09-09 15:08:12 +0000599
Ted Kremenek6efb0262008-03-27 06:17:42 +0000600 // Compute the column number of the end.
Chandler Carruth42f35f92011-07-25 20:57:57 +0000601 unsigned EndColNo = SM.getExpansionColumnNumber(InstantiationEnd);
Ted Kremenek6efb0262008-03-27 06:17:42 +0000602 unsigned OldEndColNo = EndColNo;
603
604 if (EndColNo) {
605 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner184e65d2009-04-14 23:22:57 +0000606 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
Ted Kremenek6efb0262008-03-27 06:17:42 +0000607 }
Mike Stump11289f42009-09-09 15:08:12 +0000608
Ted Kremenek6efb0262008-03-27 06:17:42 +0000609 // Highlight the range. Make the span tag the outermost tag for the
610 // selected range.
Mike Stump11289f42009-09-09 15:08:12 +0000611
Chris Lattner8a425862009-01-16 07:36:28 +0000612 SourceLocation E =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000613 InstantiationEnd.getLocWithOffset(EndColNo - OldEndColNo);
Mike Stump11289f42009-09-09 15:08:12 +0000614
Douglas Gregor87f95b02009-02-26 21:00:50 +0000615 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek6efb0262008-03-27 06:17:42 +0000616}