blob: bd20d481a99d8a4f8be7b0b4770d76cba69bc4db [file] [log] [blame]
Ted Kremenek88f5cde2008-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
Ted Kremenek21142582010-12-23 19:38:26 +000014#include "clang/StaticAnalyzer/PathDiagnosticClients.h"
15#include "clang/StaticAnalyzer/BugReporter/PathDiagnostic.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000018#include "clang/Basic/SourceManager.h"
Ted Kremenek2e939812008-03-27 07:35:49 +000019#include "clang/Basic/FileManager.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000020#include "clang/Rewrite/Rewriter.h"
21#include "clang/Rewrite/HTMLRewrite.h"
22#include "clang/Lex/Lexer.h"
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000023#include "clang/Lex/Preprocessor.h"
Michael J. Spencer4d484a72011-01-11 01:21:20 +000024#include "llvm/Support/FileSystem.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000025#include "llvm/Support/MemoryBuffer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000027#include "llvm/Support/Path.h"
Ted Kremenekb8fc3252009-10-08 17:44:41 +000028
Ted Kremenek88f5cde2008-03-27 06:17:42 +000029using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000030using namespace ento;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000031
32//===----------------------------------------------------------------------===//
33// Boilerplate.
34//===----------------------------------------------------------------------===//
35
36namespace {
37
Benjamin Kramerbd218282009-11-28 10:07:24 +000038class HTMLDiagnostics : public PathDiagnosticClient {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000039 llvm::sys::Path Directory, FilePrefix;
40 bool createdDir, noDir;
Daniel Dunbarefceabd2009-11-05 02:41:58 +000041 const Preprocessor &PP;
Ted Kremenekf7556062009-07-27 22:13:39 +000042 std::vector<const PathDiagnostic*> BatchedDiags;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000043public:
Daniel Dunbarefceabd2009-11-05 02:41:58 +000044 HTMLDiagnostics(const std::string& prefix, const Preprocessor &pp);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000045
Ted Kremenekb697a4e2009-11-05 02:09:23 +000046 virtual ~HTMLDiagnostics() { FlushDiagnostics(NULL); }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000047
Ted Kremenekb697a4e2009-11-05 02:09:23 +000048 virtual void FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade);
Mike Stump1eb44332009-09-09 15:08:12 +000049
Ted Kremenek55851142008-04-22 16:15:03 +000050 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000051
Ted Kremenekb697a4e2009-11-05 02:09:23 +000052 virtual llvm::StringRef getName() const {
53 return "HTMLDiagnostics";
54 }
Mike Stump1eb44332009-09-09 15:08:12 +000055
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000056 unsigned ProcessMacroPiece(llvm::raw_ostream& os,
57 const PathDiagnosticMacroPiece& P,
58 unsigned num);
Mike Stump1eb44332009-09-09 15:08:12 +000059
Chris Lattner2b2453a2009-01-17 06:22:33 +000060 void HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +000061 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Mike Stump1eb44332009-09-09 15:08:12 +000062
Douglas Gregor4b2d3f72009-02-26 21:00:50 +000063 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
64 const char *HighlightStart = "<span class=\"mrange\">",
65 const char *HighlightEnd = "</span>");
Ted Kremenek55851142008-04-22 16:15:03 +000066
Ted Kremenekb697a4e2009-11-05 02:09:23 +000067 void ReportDiag(const PathDiagnostic& D,
68 llvm::SmallVectorImpl<std::string> *FilesMade);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000069};
Mike Stump1eb44332009-09-09 15:08:12 +000070
Ted Kremenek88f5cde2008-03-27 06:17:42 +000071} // end anonymous namespace
72
Daniel Dunbarefceabd2009-11-05 02:41:58 +000073HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix,
74 const Preprocessor &pp)
Ted Kremenek47abe762008-04-16 16:39:56 +000075 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenekb697a4e2009-11-05 02:09:23 +000076 PP(pp) {
Mike Stump1eb44332009-09-09 15:08:12 +000077 // All html files begin with "report"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000078 FilePrefix.appendComponent("report");
79}
80
81PathDiagnosticClient*
Ted Kremenek9ef65372010-12-23 07:20:52 +000082ento::createHTMLDiagnosticClient(const std::string& prefix,
83 const Preprocessor &PP) {
Ted Kremenekb697a4e2009-11-05 02:09:23 +000084 return new HTMLDiagnostics(prefix, PP);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000085}
86
87//===----------------------------------------------------------------------===//
88// Report processing.
89//===----------------------------------------------------------------------===//
90
Ted Kremenek55851142008-04-22 16:15:03 +000091void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
92 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +000093 return;
Mike Stump1eb44332009-09-09 15:08:12 +000094
Ted Kremenek55851142008-04-22 16:15:03 +000095 if (D->empty()) {
96 delete D;
97 return;
98 }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremenek7db0a942009-04-02 05:17:38 +0000100 const_cast<PathDiagnostic*>(D)->flattenLocations();
Ted Kremenek55851142008-04-22 16:15:03 +0000101 BatchedDiags.push_back(D);
102}
103
Ted Kremenekb697a4e2009-11-05 02:09:23 +0000104void
105HTMLDiagnostics::FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade)
106{
Ted Kremenek55851142008-04-22 16:15:03 +0000107 while (!BatchedDiags.empty()) {
108 const PathDiagnostic* D = BatchedDiags.back();
109 BatchedDiags.pop_back();
Ted Kremenekb697a4e2009-11-05 02:09:23 +0000110 ReportDiag(*D, FilesMade);
Ted Kremenek55851142008-04-22 16:15:03 +0000111 delete D;
Mike Stump1eb44332009-09-09 15:08:12 +0000112 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000113
Ted Kremenekcca5e1e2009-11-13 03:02:57 +0000114 BatchedDiags.clear();
Ted Kremenek55851142008-04-22 16:15:03 +0000115}
116
Ted Kremenekb697a4e2009-11-05 02:09:23 +0000117void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
118 llvm::SmallVectorImpl<std::string> *FilesMade){
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000119 // Create the HTML directory if it is missing.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000120 if (!createdDir) {
121 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000122 std::string ErrorMsg;
123 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Michael J. Spencer4d484a72011-01-11 01:21:20 +0000125 bool IsDirectory;
126 if (llvm::sys::fs::is_directory(Directory.str(), IsDirectory) ||
127 !IsDirectory) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000128 llvm::errs() << "warning: could not create directory '"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000129 << Directory.str() << "'\n"
Mike Stump1eb44332009-09-09 15:08:12 +0000130 << "reason: " << ErrorMsg << '\n';
131
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000132 noDir = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000134 return;
135 }
136 }
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000138 if (noDir)
139 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000141 const SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000142 FileID FID;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000144 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000145 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000146 FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc();
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Chris Lattner2b2453a2009-01-17 06:22:33 +0000148 if (FID.isInvalid()) {
Chris Lattnera11d6172009-01-19 07:46:45 +0000149 FID = SMgr.getFileID(L);
150 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000151 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000153 // Check the source ranges.
154 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
155 RE=I->ranges_end(); RI!=RE; ++RI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000157 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000158
Chris Lattnera11d6172009-01-19 07:46:45 +0000159 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000160 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000162 L = SMgr.getInstantiationLoc(RI->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Chris Lattnera11d6172009-01-19 07:46:45 +0000164 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Mike Stump1eb44332009-09-09 15:08:12 +0000165 return; // FIXME: Emit a warning?
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000166 }
167 }
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Chris Lattner2b2453a2009-01-17 06:22:33 +0000169 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000170 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000172 // Create a new rewriter to generate HTML.
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000173 Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOptions());
Mike Stump1eb44332009-09-09 15:08:12 +0000174
175 // Process the path.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000176 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000177 unsigned max = n;
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000179 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
Chris Lattner409d4e72009-04-17 20:40:01 +0000180 I!=E; ++I, --n)
Chris Lattner2b2453a2009-01-17 06:22:33 +0000181 HandlePiece(R, FID, *I, n, max);
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000183 // Add line numbers, header, footer, etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Chris Lattner2b2453a2009-01-17 06:22:33 +0000185 // unsigned FID = R.getSourceMgr().getMainFileID();
186 html::EscapeText(R, FID);
187 html::AddLineNumbers(R, FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenek47abe762008-04-16 16:39:56 +0000189 // If we have a preprocessor, relex the file and syntax highlight.
190 // We might not have a preprocessor if we come from a deserialized AST file,
191 // for example.
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000193 html::SyntaxHighlight(R, FID, PP);
194 html::HighlightMacros(R, FID, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Ted Kremenekb9476392008-04-02 20:44:16 +0000196 // Get the full directory name of the analyzed file.
197
Chris Lattner2b2453a2009-01-17 06:22:33 +0000198 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremenek7fc89572008-04-24 23:37:03 +0000200 // This is a cludge; basically we want to append either the full
201 // working directory if we have no directory information. This is
202 // a work in progress.
203
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000204 std::string DirName = "";
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Michael J. Spencer256053b2010-12-17 21:22:22 +0000206 if (llvm::sys::path::is_relative(Entry->getName())) {
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000207 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000208 DirName = P.str() + "/";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000209 }
Mike Stump1eb44332009-09-09 15:08:12 +0000210
211 // Add the name of the file as an <h1> tag.
212
Ted Kremenek2e939812008-03-27 07:35:49 +0000213 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000214 std::string s;
215 llvm::raw_string_ostream os(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Ted Kremenek778246a2008-09-22 17:33:32 +0000217 os << "<!-- REPORTHEADER -->\n"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000218 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000219 "<tr><td class=\"rowname\">File:</td><td>"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000220 << html::EscapeText(DirName)
221 << html::EscapeText(Entry->getName())
222 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
Mike Stump1eb44332009-09-09 15:08:12 +0000223 "<a href=\"#EndPath\">line "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000224 << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber()
225 << ", column "
226 << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber()
227 << "</a></td></tr>\n"
228 "<tr><td class=\"rowname\">Description:</td><td>"
229 << D.getDescription() << "</td></tr>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Ted Kremenek072192b2008-04-30 23:47:44 +0000231 // Output any other meta data.
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Ted Kremenek072192b2008-04-30 23:47:44 +0000233 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
234 I!=E; ++I) {
235 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
236 }
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Ted Kremenek778246a2008-09-22 17:33:32 +0000238 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
Mike Stump1eb44332009-09-09 15:08:12 +0000239 "<h3>Annotated Source Code</h3>\n";
240
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000241 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000242 }
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Ted Kremenekb9476392008-04-02 20:44:16 +0000244 // Embed meta-data tags.
Ted Kremenekb9476392008-04-02 20:44:16 +0000245 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000246 std::string s;
247 llvm::raw_string_ostream os(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000248
249 const std::string& BugDesc = D.getDescription();
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000250 if (!BugDesc.empty())
251 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000253 const std::string& BugType = D.getBugType();
254 if (!BugType.empty())
255 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000256
257 const std::string& BugCategory = D.getCategory();
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000258 if (!BugCategory.empty())
259 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
260
Ted Kremenek7fc89572008-04-24 23:37:03 +0000261 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000262
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000263 os << "\n<!-- BUGLINE "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000264 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
265 << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000266
267 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000269 // Mark the end of the tags.
270 os << "\n<!-- BUGMETAEND -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000272 // Insert the text.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000273 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000274 }
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000276 // Add CSS, header, and footer.
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Chris Lattner2b2453a2009-01-17 06:22:33 +0000278 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000280 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000281 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000283 if (!Buf) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000284 llvm::errs() << "warning: no diagnostics generated for main file.\n";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000285 return;
286 }
287
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000288 // Create a path for the target HTML file.
289 llvm::sys::Path F(FilePrefix);
290 F.makeUnique(false, NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000292 // Rename the file with an HTML extension.
293 llvm::sys::Path H(F);
294 H.appendSuffix("html");
295 F.renamePathOnDisk(H, NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000297 std::string ErrorMsg;
298 llvm::raw_fd_ostream os(H.c_str(), ErrorMsg);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000300 if (!ErrorMsg.empty()) {
Dan Gohmand784d4d2010-06-28 15:56:07 +0000301 llvm::errs() << "warning: could not create file '" << F.str()
302 << "'\n";
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000303 return;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000304 }
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000306 if (FilesMade)
Michael J. Spencer472ccff2010-12-18 00:19:12 +0000307 FilesMade->push_back(llvm::sys::path::filename(H.str()));
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000308
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000309 // Emit the HTML to disk.
Ted Kremenek7414dc02008-04-20 01:02:33 +0000310 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000311 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000312}
313
Chris Lattner2b2453a2009-01-17 06:22:33 +0000314void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000315 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000316 unsigned num, unsigned max) {
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000318 // For now, just draw a box above the line in question, and emit the
319 // warning.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000320 FullSourceLoc Pos = P.getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000322 if (!Pos.isValid())
Mike Stump1eb44332009-09-09 15:08:12 +0000323 return;
324
Chris Lattner2b2453a2009-01-17 06:22:33 +0000325 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000326 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
327 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Chris Lattner7da5aea2009-02-04 00:55:58 +0000329 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000330 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Chris Lattner7da5aea2009-02-04 00:55:58 +0000332 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Mike Stump1eb44332009-09-09 15:08:12 +0000333 const char* FileStart = Buf->getBufferStart();
334
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000335 // Compute the column number. Rewind from the current position to the start
336 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000337 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
338 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000339 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000340
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000341 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000342 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000343 const char* FileEnd = Buf->getBufferEnd();
344 while (*LineEnd != '\n' && LineEnd != FileEnd)
345 ++LineEnd;
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000347 // Compute the margin offset by counting tabs and non-tabs.
Mike Stump1eb44332009-09-09 15:08:12 +0000348 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000349 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000350 PosNo += *c == '\t' ? 8 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000352 // Create the html for the message.
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000353
354 const char *Kind = 0;
355 switch (P.getKind()) {
Mike Stumpb7166332010-01-20 02:03:14 +0000356 case PathDiagnosticPiece::Event: Kind = "Event"; break;
357 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
358 // Setting Kind to "Control" is intentional.
359 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000360 }
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000362 std::string sbuf;
363 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000365 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000367 if (num == max)
368 os << "EndPath";
369 else
370 os << "Path" << num;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000372 os << "\" class=\"msg";
373 if (Kind)
Mike Stump1eb44332009-09-09 15:08:12 +0000374 os << " msg" << Kind;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000375 os << "\" style=\"margin-left:" << PosNo << "ex";
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000377 // Output a maximum size.
378 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000379 // Get the string and determining its maximum substring.
380 const std::string& Msg = P.getString();
381 unsigned max_token = 0;
382 unsigned cnt = 0;
383 unsigned len = Msg.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000385 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
386 switch (*I) {
Mike Stumpb7166332010-01-20 02:03:14 +0000387 default:
388 ++cnt;
389 continue;
390 case ' ':
391 case '\t':
392 case '\n':
393 if (cnt > max_token) max_token = cnt;
394 cnt = 0;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000395 }
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000397 if (cnt > max_token)
398 max_token = cnt;
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000400 // Determine the approximate size of the message bubble in em.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000401 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000402 const unsigned max_line = 120;
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000404 if (max_token >= max_line)
405 em = max_token / 2;
406 else {
407 unsigned characters = max_line;
408 unsigned lines = len / max_line;
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000410 if (lines > 0) {
411 for (; characters > max_token; --characters)
412 if (len / characters > lines) {
413 ++characters;
414 break;
415 }
416 }
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000418 em = characters / 2;
419 }
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000421 if (em < max_line/2)
Mike Stump1eb44332009-09-09 15:08:12 +0000422 os << "; max-width:" << em << "em";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000423 }
424 else
425 os << "; max-width:100em";
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000427 os << "\">";
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000429 if (max > 1) {
430 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
431 os << "<div class=\"PathIndex";
432 if (Kind) os << " PathIndex" << Kind;
433 os << "\">" << num << "</div>";
434 os << "</td><td>";
435 }
436
437 if (const PathDiagnosticMacroPiece *MP =
Mike Stump1eb44332009-09-09 15:08:12 +0000438 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000439
440 os << "Within the expansion of the macro '";
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000442 // Get the name of the macro by relexing it.
443 {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000444 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000445 assert(L.isFileID());
Benjamin Kramerceafc4b2010-03-16 14:48:07 +0000446 llvm::StringRef BufferInfo = L.getBufferData();
447 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.data();
448 Lexer rawLexer(L, PP.getLangOptions(), BufferInfo.begin(),
449 MacroName, BufferInfo.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000451 Token TheTok;
452 rawLexer.LexFromRawLexer(TheTok);
453 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
454 os << MacroName[i];
Ted Kremenek80bae762009-03-02 23:05:40 +0000455 }
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000457 os << "':\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000459 if (max > 1)
Ted Kremenek80bae762009-03-02 23:05:40 +0000460 os << "</td></tr></table>";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000461
462 // Within a macro piece. Write out each event.
463 ProcessMacroPiece(os, *MP, 0);
464 }
465 else {
466 os << html::EscapeText(P.getString());
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000468 if (max > 1)
469 os << "</td></tr></table>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000470 }
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000472 os << "</div></td></tr>";
473
474 // Insert the new html.
Mike Stump1eb44332009-09-09 15:08:12 +0000475 unsigned DisplayPos = LineEnd - FileStart;
476 SourceLocation Loc =
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000477 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
478
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000479 R.InsertTextBefore(Loc, os.str());
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000480
Mike Stump1eb44332009-09-09 15:08:12 +0000481 // Now highlight the ranges.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000482 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
483 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000484 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000485
486#if 0
487 // If there is a code insertion hint, insert that code.
488 // FIXME: This code is disabled because it seems to mangle the HTML
489 // output. I'm leaving it here because it's generally the right idea,
490 // but needs some help from someone more familiar with the rewriter.
Douglas Gregor849b2432010-03-31 17:46:05 +0000491 for (const FixItHint *Hint = P.fixit_begin(), *HintEnd = P.fixit_end();
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000492 Hint != HintEnd; ++Hint) {
493 if (Hint->RemoveRange.isValid()) {
494 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
495 "<span class=\"CodeRemovalHint\">", "</span>");
496 }
497 if (Hint->InsertionLoc.isValid()) {
498 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
499 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
500 + "</span>";
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000501 R.InsertTextBefore(Hint->InsertionLoc, EscapedCode);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000502 }
503 }
504#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000505}
506
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000507static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
Benjamin Kramer92d6cd22010-03-13 11:34:41 +0000508 unsigned x = n % ('z' - 'a');
509 n /= 'z' - 'a';
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000510
Benjamin Kramer92d6cd22010-03-13 11:34:41 +0000511 if (n > 0)
512 EmitAlphaCounter(os, n);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Benjamin Kramer92d6cd22010-03-13 11:34:41 +0000514 os << char('a' + x);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000515}
516
517unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
518 const PathDiagnosticMacroPiece& P,
519 unsigned num) {
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000521 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
522 I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000524 if (const PathDiagnosticMacroPiece *MP =
525 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
526 num = ProcessMacroPiece(os, *MP, num);
527 continue;
528 }
529
530 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
531 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
532 "margin-left:5px\">"
533 "<table class=\"msgT\"><tr>"
534 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
535 EmitAlphaCounter(os, num++);
536 os << "</div></td><td valign=\"top\">"
537 << html::EscapeText(EP->getString())
538 << "</td></tr></table></div>\n";
539 }
540 }
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000542 return num;
543}
544
Chris Lattner2b2453a2009-01-17 06:22:33 +0000545void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000546 SourceRange Range,
547 const char *HighlightStart,
548 const char *HighlightEnd) {
Chris Lattner2c78b872009-04-14 23:22:57 +0000549 SourceManager &SM = R.getSourceMgr();
550 const LangOptions &LangOpts = R.getLangOpts();
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000552 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000553 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000555 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000556 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000558 if (EndLineNo < StartLineNo)
559 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattnera11d6172009-01-19 07:46:45 +0000561 if (SM.getFileID(InstantiationStart) != BugFileID ||
562 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000563 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000565 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000566 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000567 unsigned OldEndColNo = EndColNo;
568
569 if (EndColNo) {
570 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +0000571 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000572 }
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000574 // Highlight the range. Make the span tag the outermost tag for the
575 // selected range.
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000577 SourceLocation E =
578 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000580 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000581}