blob: fbca057849ba4fd75e5551c59beb1a7b08dacf4c [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"
Daniel Dunbare91593e2008-08-11 04:54:23 +000015#include "clang/Analysis/PathDiagnostic.h"
16#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/Compiler.h"
25#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/Streams.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000028#include "llvm/System/Path.h"
29#include <fstream>
Ted Kremenek88f5cde2008-03-27 06:17:42 +000030using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// Boilerplate.
34//===----------------------------------------------------------------------===//
35
36namespace {
37
38class VISIBILITY_HIDDEN HTMLDiagnostics : public PathDiagnosticClient {
39 llvm::sys::Path Directory, FilePrefix;
40 bool createdDir, noDir;
Ted Kremenek47abe762008-04-16 16:39:56 +000041 Preprocessor* PP;
Ted Kremenek339b9c22008-04-17 22:31:54 +000042 PreprocessorFactory* PPF;
Ted Kremenek55851142008-04-22 16:15:03 +000043 std::vector<const PathDiagnostic*> BatchedDiags;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000044public:
Ted Kremenek339b9c22008-04-17 22:31:54 +000045 HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
46 PreprocessorFactory* ppf);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000047
Ted Kremenek55851142008-04-22 16:15:03 +000048 virtual ~HTMLDiagnostics();
Ted Kremenek88f5cde2008-03-27 06:17:42 +000049
Ted Kremenek55851142008-04-22 16:15:03 +000050 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000051
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000052 unsigned ProcessMacroPiece(llvm::raw_ostream& os,
53 const PathDiagnosticMacroPiece& P,
54 unsigned num);
55
Chris Lattner2b2453a2009-01-17 06:22:33 +000056 void HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +000057 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Ted Kremenek33bd9422008-03-31 23:30:12 +000058
Douglas Gregor4b2d3f72009-02-26 21:00:50 +000059 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
60 const char *HighlightStart = "<span class=\"mrange\">",
61 const char *HighlightEnd = "</span>");
Ted Kremenek55851142008-04-22 16:15:03 +000062
63 void ReportDiag(const PathDiagnostic& D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000064};
65
66} // end anonymous namespace
67
Ted Kremenek339b9c22008-04-17 22:31:54 +000068HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
69 PreprocessorFactory* ppf)
Ted Kremenek47abe762008-04-16 16:39:56 +000070 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenek339b9c22008-04-17 22:31:54 +000071 PP(pp), PPF(ppf) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000072
73 // All html files begin with "report"
74 FilePrefix.appendComponent("report");
75}
76
77PathDiagnosticClient*
Ted Kremenek339b9c22008-04-17 22:31:54 +000078clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
79 PreprocessorFactory* PPF) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000080
Ted Kremenek339b9c22008-04-17 22:31:54 +000081 return new HTMLDiagnostics(prefix, PP, PPF);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000082}
83
84//===----------------------------------------------------------------------===//
85// Report processing.
86//===----------------------------------------------------------------------===//
87
Ted Kremenek55851142008-04-22 16:15:03 +000088void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
89 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +000090 return;
91
Ted Kremenek55851142008-04-22 16:15:03 +000092 if (D->empty()) {
93 delete D;
94 return;
95 }
96
97 BatchedDiags.push_back(D);
98}
99
100HTMLDiagnostics::~HTMLDiagnostics() {
101
102 while (!BatchedDiags.empty()) {
103 const PathDiagnostic* D = BatchedDiags.back();
104 BatchedDiags.pop_back();
105 ReportDiag(*D);
106 delete D;
107 }
108}
109
110void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) {
111
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000112 // Create the HTML directory if it is missing.
113
114 if (!createdDir) {
115 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000116 std::string ErrorMsg;
117 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000118
119 if (!Directory.isDirectory()) {
120 llvm::cerr << "warning: could not create directory '"
Ted Kremenek344f7e32008-04-03 17:55:57 +0000121 << Directory.toString() << "'\n"
122 << "reason: " << ErrorMsg << '\n';
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000123
124 noDir = true;
125
126 return;
127 }
128 }
129
130 if (noDir)
131 return;
132
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000133 const SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000134 FileID FID;
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000135
136 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000137 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000138 FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc();
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000139
Chris Lattner2b2453a2009-01-17 06:22:33 +0000140 if (FID.isInvalid()) {
Chris Lattnera11d6172009-01-19 07:46:45 +0000141 FID = SMgr.getFileID(L);
142 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000143 return; // FIXME: Emit a warning?
144
145 // Check the source ranges.
146 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
147 RE=I->ranges_end(); RI!=RE; ++RI) {
148
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000149 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000150
Chris Lattnera11d6172009-01-19 07:46:45 +0000151 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000152 return; // FIXME: Emit a warning?
153
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000154 L = SMgr.getInstantiationLoc(RI->getEnd());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000155
Chris Lattnera11d6172009-01-19 07:46:45 +0000156 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000157 return; // FIXME: Emit a warning?
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000158 }
159 }
160
Chris Lattner2b2453a2009-01-17 06:22:33 +0000161 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000162 return; // FIXME: Emit a warning?
163
164 // Create a new rewriter to generate HTML.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000165 Rewriter R(const_cast<SourceManager&>(SMgr));
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000166
Ted Kremenek00086832009-03-10 02:49:29 +0000167 // Process the path.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000168 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000169 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000170
171 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
172 I!=E; ++I, --n) {
173
Chris Lattner2b2453a2009-01-17 06:22:33 +0000174 HandlePiece(R, FID, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000175 }
176
177 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000178
Chris Lattner2b2453a2009-01-17 06:22:33 +0000179 // unsigned FID = R.getSourceMgr().getMainFileID();
180 html::EscapeText(R, FID);
181 html::AddLineNumbers(R, FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000182
Ted Kremenek47abe762008-04-16 16:39:56 +0000183 // If we have a preprocessor, relex the file and syntax highlight.
184 // We might not have a preprocessor if we come from a deserialized AST file,
185 // for example.
186
Chris Lattner2b2453a2009-01-17 06:22:33 +0000187 if (PP) html::SyntaxHighlight(R, FID, *PP);
Ted Kremenek55851142008-04-22 16:15:03 +0000188
189 // FIXME: We eventually want to use PPF to create a fresh Preprocessor,
190 // once we have worked out the bugs.
191 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000192 // if (PPF) html::HighlightMacros(R, FID, *PPF);
Ted Kremenek55851142008-04-22 16:15:03 +0000193 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000194 if (PP) html::HighlightMacros(R, FID, *PP);
Ted Kremenek47abe762008-04-16 16:39:56 +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);
Ted Kremenek2e939812008-03-27 07:35:49 +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 = "";
205
206 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
207 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
208 DirName = P.toString() + "/";
209 }
Ted Kremenekb9476392008-04-02 20:44:16 +0000210
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000211 // 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);
Ted Kremenek2e939812008-03-27 07:35:49 +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>"
223 "<a href=\"#EndPath\">line "
224 << (*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";
Ted Kremenek072192b2008-04-30 23:47:44 +0000230
231 // Output any other meta data.
232
233 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 }
237
Ted Kremenek778246a2008-09-22 17:33:32 +0000238 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
239 "<h3>Annotated Source Code</h3>\n";
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000240
Chris Lattner2b2453a2009-01-17 06:22:33 +0000241 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000242 }
243
Ted Kremenekb9476392008-04-02 20:44:16 +0000244 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000245
246 const std::string& BugDesc = D.getDescription();
247
248 if (!BugDesc.empty()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000249 std::string s;
250 llvm::raw_string_ostream os(s);
Ted Kremenek86b43812008-04-02 18:03:20 +0000251 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000252 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000253 }
254
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000255 const std::string& BugType = D.getBugType();
256 if (!BugType.empty()) {
257 std::string s;
258 llvm::raw_string_ostream os(s);
259 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
260 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
261 }
262
Ted Kremenek8c036c72008-09-20 04:23:38 +0000263 const std::string& BugCategory = D.getCategory();
264
265 if (!BugCategory.empty()) {
266 std::string s;
267 llvm::raw_string_ostream os(s);
268 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000269 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek8c036c72008-09-20 04:23:38 +0000270 }
271
Ted Kremenekb9476392008-04-02 20:44:16 +0000272 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000273 std::string s;
274 llvm::raw_string_ostream os(s);
Ted Kremenek7fc89572008-04-24 23:37:03 +0000275 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000276 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000277 }
278
279 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000280 std::string s;
281 llvm::raw_string_ostream os(s);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000282 os << "\n<!-- BUGLINE "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000283 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
284 << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000285 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000286 }
287
288 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000289 std::string s;
290 llvm::raw_string_ostream os(s);
Ted Kremenekb9476392008-04-02 20:44:16 +0000291 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000292 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000293 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000294
295 // Add CSS, header, and footer.
296
Chris Lattner2b2453a2009-01-17 06:22:33 +0000297 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000298
299 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000300 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000301
302 if (!Buf) {
303 llvm::cerr << "warning: no diagnostics generated for main file.\n";
304 return;
305 }
306
307 // Create the stream to write out the HTML.
308 std::ofstream os;
309
310 {
311 // Create a path for the target HTML file.
312 llvm::sys::Path F(FilePrefix);
313 F.makeUnique(false, NULL);
314
315 // Rename the file with an HTML extension.
316 llvm::sys::Path H(F);
317 H.appendSuffix("html");
318 F.renamePathOnDisk(H, NULL);
319
320 os.open(H.toString().c_str());
321
322 if (!os) {
323 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
324 return;
325 }
326 }
327
328 // Emit the HTML to disk.
329
Ted Kremenek7414dc02008-04-20 01:02:33 +0000330 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000331 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000332}
333
Chris Lattner2b2453a2009-01-17 06:22:33 +0000334void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000335 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000336 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000337
338 // For now, just draw a box above the line in question, and emit the
339 // warning.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000340 FullSourceLoc Pos = P.getLocation().asLocation();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000341
342 if (!Pos.isValid())
343 return;
344
Chris Lattner2b2453a2009-01-17 06:22:33 +0000345 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000346 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
347 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000348
Chris Lattner7da5aea2009-02-04 00:55:58 +0000349 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000350 return;
351
Chris Lattner7da5aea2009-02-04 00:55:58 +0000352 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000353 const char* FileStart = Buf->getBufferStart();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000354
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000355 // Compute the column number. Rewind from the current position to the start
356 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000357 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
358 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000359 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000360
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000361 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000362 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000363 const char* FileEnd = Buf->getBufferEnd();
364 while (*LineEnd != '\n' && LineEnd != FileEnd)
365 ++LineEnd;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000366
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000367 // Compute the margin offset by counting tabs and non-tabs.
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000368 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000369 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000370 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000371
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000372 // Create the html for the message.
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000373
374 const char *Kind = 0;
375 switch (P.getKind()) {
376 case PathDiagnosticPiece::Event: Kind = "Event"; break;
377 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
378 // Setting Kind to "Control" is intentional.
379 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
380 }
381
382 std::string sbuf;
383 llvm::raw_string_ostream os(sbuf);
384
385 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
386
387 if (num == max)
388 os << "EndPath";
389 else
390 os << "Path" << num;
391
392 os << "\" class=\"msg";
393 if (Kind)
394 os << " msg" << Kind;
395 os << "\" style=\"margin-left:" << PosNo << "ex";
396
397 // Output a maximum size.
398 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000399 // Get the string and determining its maximum substring.
400 const std::string& Msg = P.getString();
401 unsigned max_token = 0;
402 unsigned cnt = 0;
403 unsigned len = Msg.size();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000404
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000405 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
406 switch (*I) {
407 default:
408 ++cnt;
Ted Kremenek00086832009-03-10 02:49:29 +0000409 continue;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000410 case ' ':
411 case '\t':
412 case '\n':
413 if (cnt > max_token) max_token = cnt;
414 cnt = 0;
415 }
416
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000417 if (cnt > max_token)
418 max_token = cnt;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000419
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000420 // Determine the approximate size of the message bubble in em.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000421 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000422 const unsigned max_line = 120;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000423
424 if (max_token >= max_line)
425 em = max_token / 2;
426 else {
427 unsigned characters = max_line;
428 unsigned lines = len / max_line;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000429
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000430 if (lines > 0) {
431 for (; characters > max_token; --characters)
432 if (len / characters > lines) {
433 ++characters;
434 break;
435 }
436 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000437
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000438 em = characters / 2;
439 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000440
441 if (em < max_line/2)
442 os << "; max-width:" << em << "em";
443 }
444 else
445 os << "; max-width:100em";
446
447 os << "\">";
448
449 if (max > 1) {
450 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
451 os << "<div class=\"PathIndex";
452 if (Kind) os << " PathIndex" << Kind;
453 os << "\">" << num << "</div>";
454 os << "</td><td>";
455 }
456
457 if (const PathDiagnosticMacroPiece *MP =
458 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
459
460 os << "Within the expansion of the macro '";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000461
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000462 // Get the name of the macro by relexing it.
463 {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000464 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000465 assert(L.isFileID());
466 std::pair<const char*, const char*> BufferInfo = L.getBufferData();
467 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.first;
468 Lexer rawLexer(L, PP->getLangOptions(), BufferInfo.first,
469 MacroName, BufferInfo.second);
470
471 Token TheTok;
472 rawLexer.LexFromRawLexer(TheTok);
473 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
474 os << MacroName[i];
Ted Kremenek80bae762009-03-02 23:05:40 +0000475 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000476
477 os << "':\n";
Ted Kremenek80bae762009-03-02 23:05:40 +0000478
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000479 if (max > 1)
Ted Kremenek80bae762009-03-02 23:05:40 +0000480 os << "</td></tr></table>";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000481
482 // Within a macro piece. Write out each event.
483 ProcessMacroPiece(os, *MP, 0);
484 }
485 else {
486 os << html::EscapeText(P.getString());
Ted Kremenek80bae762009-03-02 23:05:40 +0000487
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000488 if (max > 1)
489 os << "</td></tr></table>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000490 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000491
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000492 os << "</div></td></tr>";
493
494 // Insert the new html.
495 unsigned DisplayPos = LineEnd - FileStart;
496 SourceLocation Loc =
497 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
498
499 R.InsertStrBefore(Loc, os.str());
500
501 // Now highlight the ranges.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000502 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
503 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000504 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000505
506#if 0
507 // If there is a code insertion hint, insert that code.
508 // FIXME: This code is disabled because it seems to mangle the HTML
509 // output. I'm leaving it here because it's generally the right idea,
510 // but needs some help from someone more familiar with the rewriter.
511 for (const CodeModificationHint *Hint = P.code_modifications_begin(),
512 *HintEnd = P.code_modifications_end();
513 Hint != HintEnd; ++Hint) {
514 if (Hint->RemoveRange.isValid()) {
515 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
516 "<span class=\"CodeRemovalHint\">", "</span>");
517 }
518 if (Hint->InsertionLoc.isValid()) {
519 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
520 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
521 + "</span>";
522 R.InsertStrBefore(Hint->InsertionLoc, EscapedCode);
523 }
524 }
525#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000526}
527
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000528static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
529 llvm::SmallVector<char, 10> buf;
530
531 do {
532 unsigned x = n % ('z' - 'a');
533 buf.push_back('a' + x);
534 n = n / ('z' - 'a');
535 } while (n);
536
537 assert(!buf.empty());
538
539 for (llvm::SmallVectorImpl<char>::reverse_iterator I=buf.rbegin(),
540 E=buf.rend(); I!=E; ++I)
541 os << *I;
542}
543
544unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
545 const PathDiagnosticMacroPiece& P,
546 unsigned num) {
547
548 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
549 I!=E; ++I) {
550
551 if (const PathDiagnosticMacroPiece *MP =
552 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
553 num = ProcessMacroPiece(os, *MP, num);
554 continue;
555 }
556
557 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
558 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
559 "margin-left:5px\">"
560 "<table class=\"msgT\"><tr>"
561 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
562 EmitAlphaCounter(os, num++);
563 os << "</div></td><td valign=\"top\">"
564 << html::EscapeText(EP->getString())
565 << "</td></tr></table></div>\n";
566 }
567 }
568
569 return num;
570}
571
Chris Lattner2b2453a2009-01-17 06:22:33 +0000572void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000573 SourceRange Range,
574 const char *HighlightStart,
575 const char *HighlightEnd) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000576
Ted Kremenek5dd00412008-04-14 21:06:04 +0000577 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000578
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000579 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000580 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000581
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000582 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000583 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000584
585 if (EndLineNo < StartLineNo)
586 return;
587
Chris Lattnera11d6172009-01-19 07:46:45 +0000588 if (SM.getFileID(InstantiationStart) != BugFileID ||
589 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000590 return;
591
592 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000593 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000594 unsigned OldEndColNo = EndColNo;
595
596 if (EndColNo) {
597 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000598 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000599 }
600
601 // Highlight the range. Make the span tag the outermost tag for the
602 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000603
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000604 SourceLocation E =
605 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000606
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000607 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000608}