blob: f695254cb4666945bc5c550eef3d57fc79f6fa29 [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
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000014#include "clang/Frontend/PathDiagnosticClients.h"
Ted Kremenek6b676302010-01-25 17:10:22 +000015#include "clang/Checker/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"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000024#include "llvm/Support/MemoryBuffer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000026#include "llvm/System/Path.h"
Ted Kremenekb8fc3252009-10-08 17:44:41 +000027
Ted Kremenek88f5cde2008-03-27 06:17:42 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// Boilerplate.
32//===----------------------------------------------------------------------===//
33
34namespace {
35
Benjamin Kramerbd218282009-11-28 10:07:24 +000036class HTMLDiagnostics : public PathDiagnosticClient {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000037 llvm::sys::Path Directory, FilePrefix;
38 bool createdDir, noDir;
Daniel Dunbarefceabd2009-11-05 02:41:58 +000039 const Preprocessor &PP;
Ted Kremenekf7556062009-07-27 22:13:39 +000040 std::vector<const PathDiagnostic*> BatchedDiags;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000041public:
Daniel Dunbarefceabd2009-11-05 02:41:58 +000042 HTMLDiagnostics(const std::string& prefix, const Preprocessor &pp);
Ted Kremenekb697a4e2009-11-05 02:09:23 +000043
44 virtual ~HTMLDiagnostics() { FlushDiagnostics(NULL); }
45
46 virtual void FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade);
Mike Stump1eb44332009-09-09 15:08:12 +000047
Ted Kremenek55851142008-04-22 16:15:03 +000048 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Ted Kremenekb697a4e2009-11-05 02:09:23 +000049
50 virtual llvm::StringRef getName() const {
51 return "HTMLDiagnostics";
52 }
Mike Stump1eb44332009-09-09 15:08:12 +000053
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000054 unsigned ProcessMacroPiece(llvm::raw_ostream& os,
55 const PathDiagnosticMacroPiece& P,
56 unsigned num);
Mike Stump1eb44332009-09-09 15:08:12 +000057
Chris Lattner2b2453a2009-01-17 06:22:33 +000058 void HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +000059 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Mike Stump1eb44332009-09-09 15:08:12 +000060
Douglas Gregor4b2d3f72009-02-26 21:00:50 +000061 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
62 const char *HighlightStart = "<span class=\"mrange\">",
63 const char *HighlightEnd = "</span>");
Ted Kremenek55851142008-04-22 16:15:03 +000064
Ted Kremenekb697a4e2009-11-05 02:09:23 +000065 void ReportDiag(const PathDiagnostic& D,
66 llvm::SmallVectorImpl<std::string> *FilesMade);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000067};
Mike Stump1eb44332009-09-09 15:08:12 +000068
Ted Kremenek88f5cde2008-03-27 06:17:42 +000069} // end anonymous namespace
70
Daniel Dunbarefceabd2009-11-05 02:41:58 +000071HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix,
72 const Preprocessor &pp)
Ted Kremenek47abe762008-04-16 16:39:56 +000073 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenekb697a4e2009-11-05 02:09:23 +000074 PP(pp) {
Mike Stump1eb44332009-09-09 15:08:12 +000075 // All html files begin with "report"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000076 FilePrefix.appendComponent("report");
77}
78
79PathDiagnosticClient*
Daniel Dunbarefceabd2009-11-05 02:41:58 +000080clang::CreateHTMLDiagnosticClient(const std::string& prefix,
81 const Preprocessor &PP) {
Ted Kremenekb697a4e2009-11-05 02:09:23 +000082 return new HTMLDiagnostics(prefix, PP);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000083}
84
85//===----------------------------------------------------------------------===//
86// Report processing.
87//===----------------------------------------------------------------------===//
88
Ted Kremenek55851142008-04-22 16:15:03 +000089void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
90 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +000091 return;
Mike Stump1eb44332009-09-09 15:08:12 +000092
Ted Kremenek55851142008-04-22 16:15:03 +000093 if (D->empty()) {
94 delete D;
95 return;
96 }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Ted Kremenek7db0a942009-04-02 05:17:38 +000098 const_cast<PathDiagnostic*>(D)->flattenLocations();
Ted Kremenek55851142008-04-22 16:15:03 +000099 BatchedDiags.push_back(D);
100}
101
Ted Kremenekb697a4e2009-11-05 02:09:23 +0000102void
103HTMLDiagnostics::FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade)
104{
Ted Kremenek55851142008-04-22 16:15:03 +0000105 while (!BatchedDiags.empty()) {
106 const PathDiagnostic* D = BatchedDiags.back();
107 BatchedDiags.pop_back();
Ted Kremenekb697a4e2009-11-05 02:09:23 +0000108 ReportDiag(*D, FilesMade);
Ted Kremenek55851142008-04-22 16:15:03 +0000109 delete D;
Mike Stump1eb44332009-09-09 15:08:12 +0000110 }
Ted Kremenekcca5e1e2009-11-13 03:02:57 +0000111
112 BatchedDiags.clear();
Ted Kremenek55851142008-04-22 16:15:03 +0000113}
114
Ted Kremenekb697a4e2009-11-05 02:09:23 +0000115void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
116 llvm::SmallVectorImpl<std::string> *FilesMade){
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000117 // Create the HTML directory if it is missing.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000118 if (!createdDir) {
119 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000120 std::string ErrorMsg;
121 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000123 if (!Directory.isDirectory()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000124 llvm::errs() << "warning: could not create directory '"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000125 << Directory.str() << "'\n"
Mike Stump1eb44332009-09-09 15:08:12 +0000126 << "reason: " << ErrorMsg << '\n';
127
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000128 noDir = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000130 return;
131 }
132 }
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000134 if (noDir)
135 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000137 const SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000138 FileID FID;
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000140 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000141 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000142 FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc();
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Chris Lattner2b2453a2009-01-17 06:22:33 +0000144 if (FID.isInvalid()) {
Chris Lattnera11d6172009-01-19 07:46:45 +0000145 FID = SMgr.getFileID(L);
146 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000147 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000149 // Check the source ranges.
150 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
151 RE=I->ranges_end(); RI!=RE; ++RI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000153 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000154
Chris Lattnera11d6172009-01-19 07:46:45 +0000155 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000156 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000158 L = SMgr.getInstantiationLoc(RI->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Chris Lattnera11d6172009-01-19 07:46:45 +0000160 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Mike Stump1eb44332009-09-09 15:08:12 +0000161 return; // FIXME: Emit a warning?
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000162 }
163 }
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Chris Lattner2b2453a2009-01-17 06:22:33 +0000165 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000166 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000168 // Create a new rewriter to generate HTML.
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000169 Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOptions());
Mike Stump1eb44332009-09-09 15:08:12 +0000170
171 // Process the path.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000172 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000173 unsigned max = n;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000175 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
Chris Lattner409d4e72009-04-17 20:40:01 +0000176 I!=E; ++I, --n)
Chris Lattner2b2453a2009-01-17 06:22:33 +0000177 HandlePiece(R, FID, *I, n, max);
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000179 // Add line numbers, header, footer, etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner2b2453a2009-01-17 06:22:33 +0000181 // unsigned FID = R.getSourceMgr().getMainFileID();
182 html::EscapeText(R, FID);
183 html::AddLineNumbers(R, FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Ted Kremenek47abe762008-04-16 16:39:56 +0000185 // If we have a preprocessor, relex the file and syntax highlight.
186 // We might not have a preprocessor if we come from a deserialized AST file,
187 // for example.
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000189 html::SyntaxHighlight(R, FID, PP);
190 html::HighlightMacros(R, FID, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Ted Kremenekb9476392008-04-02 20:44:16 +0000192 // Get the full directory name of the analyzed file.
193
Chris Lattner2b2453a2009-01-17 06:22:33 +0000194 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Ted Kremenek7fc89572008-04-24 23:37:03 +0000196 // This is a cludge; basically we want to append either the full
197 // working directory if we have no directory information. This is
198 // a work in progress.
199
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000200 std::string DirName = "";
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000202 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
203 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000204 DirName = P.str() + "/";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
207 // Add the name of the file as an <h1> tag.
208
Ted Kremenek2e939812008-03-27 07:35:49 +0000209 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000210 std::string s;
211 llvm::raw_string_ostream os(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenek778246a2008-09-22 17:33:32 +0000213 os << "<!-- REPORTHEADER -->\n"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000214 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000215 "<tr><td class=\"rowname\">File:</td><td>"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000216 << html::EscapeText(DirName)
217 << html::EscapeText(Entry->getName())
218 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
Mike Stump1eb44332009-09-09 15:08:12 +0000219 "<a href=\"#EndPath\">line "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000220 << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber()
221 << ", column "
222 << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber()
223 << "</a></td></tr>\n"
224 "<tr><td class=\"rowname\">Description:</td><td>"
225 << D.getDescription() << "</td></tr>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremenek072192b2008-04-30 23:47:44 +0000227 // Output any other meta data.
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Ted Kremenek072192b2008-04-30 23:47:44 +0000229 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
230 I!=E; ++I) {
231 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
232 }
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Ted Kremenek778246a2008-09-22 17:33:32 +0000234 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
Mike Stump1eb44332009-09-09 15:08:12 +0000235 "<h3>Annotated Source Code</h3>\n";
236
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000237 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000238 }
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Ted Kremenekb9476392008-04-02 20:44:16 +0000240 // Embed meta-data tags.
Ted Kremenekb9476392008-04-02 20:44:16 +0000241 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000242 std::string s;
243 llvm::raw_string_ostream os(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
245 const std::string& BugDesc = D.getDescription();
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000246 if (!BugDesc.empty())
247 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000249 const std::string& BugType = D.getBugType();
250 if (!BugType.empty())
251 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000252
253 const std::string& BugCategory = D.getCategory();
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000254 if (!BugCategory.empty())
255 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
256
Ted Kremenek7fc89572008-04-24 23:37:03 +0000257 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000258
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000259 os << "\n<!-- BUGLINE "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000260 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
261 << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000262
263 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000265 // Mark the end of the tags.
266 os << "\n<!-- BUGMETAEND -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000268 // Insert the text.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000269 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000272 // Add CSS, header, and footer.
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Chris Lattner2b2453a2009-01-17 06:22:33 +0000274 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000276 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000277 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000279 if (!Buf) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000280 llvm::errs() << "warning: no diagnostics generated for main file.\n";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000281 return;
282 }
283
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000284 // Create a path for the target HTML file.
285 llvm::sys::Path F(FilePrefix);
286 F.makeUnique(false, NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000288 // Rename the file with an HTML extension.
289 llvm::sys::Path H(F);
290 H.appendSuffix("html");
291 F.renamePathOnDisk(H, NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000293 std::string ErrorMsg;
294 llvm::raw_fd_ostream os(H.c_str(), ErrorMsg);
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000296 if (!ErrorMsg.empty()) {
297 (llvm::errs() << "warning: could not create file '" << F.str()
298 << "'\n").flush();
299 return;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000302 if (FilesMade)
303 FilesMade->push_back(H.getLast());
304
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000305 // Emit the HTML to disk.
Ted Kremenek7414dc02008-04-20 01:02:33 +0000306 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000307 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000308}
309
Chris Lattner2b2453a2009-01-17 06:22:33 +0000310void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000311 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000312 unsigned num, unsigned max) {
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000314 // For now, just draw a box above the line in question, and emit the
315 // warning.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000316 FullSourceLoc Pos = P.getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000318 if (!Pos.isValid())
Mike Stump1eb44332009-09-09 15:08:12 +0000319 return;
320
Chris Lattner2b2453a2009-01-17 06:22:33 +0000321 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000322 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
323 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Chris Lattner7da5aea2009-02-04 00:55:58 +0000325 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000326 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Chris Lattner7da5aea2009-02-04 00:55:58 +0000328 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Mike Stump1eb44332009-09-09 15:08:12 +0000329 const char* FileStart = Buf->getBufferStart();
330
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000331 // Compute the column number. Rewind from the current position to the start
332 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000333 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
334 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000335 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000336
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000337 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000338 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000339 const char* FileEnd = Buf->getBufferEnd();
340 while (*LineEnd != '\n' && LineEnd != FileEnd)
341 ++LineEnd;
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000343 // Compute the margin offset by counting tabs and non-tabs.
Mike Stump1eb44332009-09-09 15:08:12 +0000344 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000345 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000346 PosNo += *c == '\t' ? 8 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000348 // Create the html for the message.
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000349
350 const char *Kind = 0;
351 switch (P.getKind()) {
Mike Stumpb7166332010-01-20 02:03:14 +0000352 case PathDiagnosticPiece::Event: Kind = "Event"; break;
353 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
354 // Setting Kind to "Control" is intentional.
355 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000358 std::string sbuf;
359 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000361 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000363 if (num == max)
364 os << "EndPath";
365 else
366 os << "Path" << num;
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000368 os << "\" class=\"msg";
369 if (Kind)
Mike Stump1eb44332009-09-09 15:08:12 +0000370 os << " msg" << Kind;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000371 os << "\" style=\"margin-left:" << PosNo << "ex";
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000373 // Output a maximum size.
374 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000375 // Get the string and determining its maximum substring.
376 const std::string& Msg = P.getString();
377 unsigned max_token = 0;
378 unsigned cnt = 0;
379 unsigned len = Msg.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000381 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
382 switch (*I) {
Mike Stumpb7166332010-01-20 02:03:14 +0000383 default:
384 ++cnt;
385 continue;
386 case ' ':
387 case '\t':
388 case '\n':
389 if (cnt > max_token) max_token = cnt;
390 cnt = 0;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000393 if (cnt > max_token)
394 max_token = cnt;
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000396 // Determine the approximate size of the message bubble in em.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000397 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000398 const unsigned max_line = 120;
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000400 if (max_token >= max_line)
401 em = max_token / 2;
402 else {
403 unsigned characters = max_line;
404 unsigned lines = len / max_line;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000406 if (lines > 0) {
407 for (; characters > max_token; --characters)
408 if (len / characters > lines) {
409 ++characters;
410 break;
411 }
412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000414 em = characters / 2;
415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000417 if (em < max_line/2)
Mike Stump1eb44332009-09-09 15:08:12 +0000418 os << "; max-width:" << em << "em";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000419 }
420 else
421 os << "; max-width:100em";
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000423 os << "\">";
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000425 if (max > 1) {
426 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
427 os << "<div class=\"PathIndex";
428 if (Kind) os << " PathIndex" << Kind;
429 os << "\">" << num << "</div>";
430 os << "</td><td>";
431 }
432
433 if (const PathDiagnosticMacroPiece *MP =
Mike Stump1eb44332009-09-09 15:08:12 +0000434 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000435
436 os << "Within the expansion of the macro '";
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000438 // Get the name of the macro by relexing it.
439 {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000440 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000441 assert(L.isFileID());
442 std::pair<const char*, const char*> BufferInfo = L.getBufferData();
443 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.first;
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000444 Lexer rawLexer(L, PP.getLangOptions(), BufferInfo.first,
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000445 MacroName, BufferInfo.second);
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000447 Token TheTok;
448 rawLexer.LexFromRawLexer(TheTok);
449 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
450 os << MacroName[i];
Ted Kremenek80bae762009-03-02 23:05:40 +0000451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000453 os << "':\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000455 if (max > 1)
Ted Kremenek80bae762009-03-02 23:05:40 +0000456 os << "</td></tr></table>";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000457
458 // Within a macro piece. Write out each event.
459 ProcessMacroPiece(os, *MP, 0);
460 }
461 else {
462 os << html::EscapeText(P.getString());
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000464 if (max > 1)
465 os << "</td></tr></table>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000468 os << "</div></td></tr>";
469
470 // Insert the new html.
Mike Stump1eb44332009-09-09 15:08:12 +0000471 unsigned DisplayPos = LineEnd - FileStart;
472 SourceLocation Loc =
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000473 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
474
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000475 R.InsertTextBefore(Loc, os.str());
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000476
Mike Stump1eb44332009-09-09 15:08:12 +0000477 // Now highlight the ranges.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000478 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
479 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000480 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000481
482#if 0
483 // If there is a code insertion hint, insert that code.
484 // FIXME: This code is disabled because it seems to mangle the HTML
485 // output. I'm leaving it here because it's generally the right idea,
486 // but needs some help from someone more familiar with the rewriter.
487 for (const CodeModificationHint *Hint = P.code_modifications_begin(),
488 *HintEnd = P.code_modifications_end();
489 Hint != HintEnd; ++Hint) {
490 if (Hint->RemoveRange.isValid()) {
491 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
492 "<span class=\"CodeRemovalHint\">", "</span>");
493 }
494 if (Hint->InsertionLoc.isValid()) {
495 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
496 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
497 + "</span>";
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000498 R.InsertTextBefore(Hint->InsertionLoc, EscapedCode);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000499 }
500 }
501#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000502}
503
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000504static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
505 llvm::SmallVector<char, 10> buf;
506
507 do {
508 unsigned x = n % ('z' - 'a');
509 buf.push_back('a' + x);
510 n = n / ('z' - 'a');
511 } while (n);
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000513 assert(!buf.empty());
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000515 for (llvm::SmallVectorImpl<char>::reverse_iterator I=buf.rbegin(),
516 E=buf.rend(); I!=E; ++I)
517 os << *I;
518}
519
520unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
521 const PathDiagnosticMacroPiece& P,
522 unsigned num) {
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000524 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
525 I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000527 if (const PathDiagnosticMacroPiece *MP =
528 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
529 num = ProcessMacroPiece(os, *MP, num);
530 continue;
531 }
532
533 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
534 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
535 "margin-left:5px\">"
536 "<table class=\"msgT\"><tr>"
537 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
538 EmitAlphaCounter(os, num++);
539 os << "</div></td><td valign=\"top\">"
540 << html::EscapeText(EP->getString())
541 << "</td></tr></table></div>\n";
542 }
543 }
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000545 return num;
546}
547
Chris Lattner2b2453a2009-01-17 06:22:33 +0000548void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000549 SourceRange Range,
550 const char *HighlightStart,
551 const char *HighlightEnd) {
Chris Lattner2c78b872009-04-14 23:22:57 +0000552 SourceManager &SM = R.getSourceMgr();
553 const LangOptions &LangOpts = R.getLangOpts();
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000555 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000556 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000558 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000559 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000561 if (EndLineNo < StartLineNo)
562 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Chris Lattnera11d6172009-01-19 07:46:45 +0000564 if (SM.getFileID(InstantiationStart) != BugFileID ||
565 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000566 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000568 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000569 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000570 unsigned OldEndColNo = EndColNo;
571
572 if (EndColNo) {
573 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +0000574 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000575 }
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000577 // Highlight the range. Make the span tag the outermost tag for the
578 // selected range.
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000580 SourceLocation E =
581 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000583 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000584}