blob: 0b266cda0f0c3fc1253bcbef8d5e8c400a50b0eb [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
Chris Lattner4abb87e2009-01-16 22:59:51 +0000133 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) {
138 FullSourceLoc L = I->getLocation().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 Kremenek88f5cde2008-03-27 06:17:42 +0000165 Rewriter R(SMgr);
166
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"
218 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000219 "<tr><td class=\"rowname\">File:</td><td>"
220 << html::EscapeText(DirName)
221 << html::EscapeText(Entry->getName())
222 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
223 "<a href=\"#EndPath\">line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000224 << (*D.rbegin()).getLocation().getInstantiationLineNumber()
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000225 << ", column "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000226 << (*D.rbegin()).getLocation().getInstantiationColumnNumber()
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000227 << "</a></td></tr>\n"
228 "<tr><td class=\"rowname\">Description:</td><td>"
Ted Kremenek072192b2008-04-30 23:47:44 +0000229 << D.getDescription() << "</td></tr>\n";
230
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 "
283 << D.back()->getLocation().getInstantiationLineNumber() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000284 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000285 }
286
287 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000288 std::string s;
289 llvm::raw_string_ostream os(s);
Ted Kremenekb9476392008-04-02 20:44:16 +0000290 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000291 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000292 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000293
294 // Add CSS, header, and footer.
295
Chris Lattner2b2453a2009-01-17 06:22:33 +0000296 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000297
298 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000299 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000300
301 if (!Buf) {
302 llvm::cerr << "warning: no diagnostics generated for main file.\n";
303 return;
304 }
305
306 // Create the stream to write out the HTML.
307 std::ofstream os;
308
309 {
310 // Create a path for the target HTML file.
311 llvm::sys::Path F(FilePrefix);
312 F.makeUnique(false, NULL);
313
314 // Rename the file with an HTML extension.
315 llvm::sys::Path H(F);
316 H.appendSuffix("html");
317 F.renamePathOnDisk(H, NULL);
318
319 os.open(H.toString().c_str());
320
321 if (!os) {
322 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
323 return;
324 }
325 }
326
327 // Emit the HTML to disk.
328
Ted Kremenek7414dc02008-04-20 01:02:33 +0000329 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000330 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000331}
332
Chris Lattner2b2453a2009-01-17 06:22:33 +0000333void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000334 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000335 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000336
337 // For now, just draw a box above the line in question, and emit the
338 // warning.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000339 FullSourceLoc Pos = P.getLocation();
340
341 if (!Pos.isValid())
342 return;
343
Chris Lattner2b2453a2009-01-17 06:22:33 +0000344 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000345 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
346 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000347
Chris Lattner7da5aea2009-02-04 00:55:58 +0000348 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000349 return;
350
Chris Lattner7da5aea2009-02-04 00:55:58 +0000351 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000352 const char* FileStart = Buf->getBufferStart();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000353
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000354 // Compute the column number. Rewind from the current position to the start
355 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000356 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
357 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000358 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000359
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000360 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000361 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000362 const char* FileEnd = Buf->getBufferEnd();
363 while (*LineEnd != '\n' && LineEnd != FileEnd)
364 ++LineEnd;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000365
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000366 // Compute the margin offset by counting tabs and non-tabs.
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000367 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000368 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000369 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000370
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000371 // Create the html for the message.
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000372
373 const char *Kind = 0;
374 switch (P.getKind()) {
375 case PathDiagnosticPiece::Event: Kind = "Event"; break;
376 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
377 // Setting Kind to "Control" is intentional.
378 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
379 }
380
381 std::string sbuf;
382 llvm::raw_string_ostream os(sbuf);
383
384 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
385
386 if (num == max)
387 os << "EndPath";
388 else
389 os << "Path" << num;
390
391 os << "\" class=\"msg";
392 if (Kind)
393 os << " msg" << Kind;
394 os << "\" style=\"margin-left:" << PosNo << "ex";
395
396 // Output a maximum size.
397 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000398 // Get the string and determining its maximum substring.
399 const std::string& Msg = P.getString();
400 unsigned max_token = 0;
401 unsigned cnt = 0;
402 unsigned len = Msg.size();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000403
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000404 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
405 switch (*I) {
406 default:
407 ++cnt;
Ted Kremenek00086832009-03-10 02:49:29 +0000408 continue;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000409 case ' ':
410 case '\t':
411 case '\n':
412 if (cnt > max_token) max_token = cnt;
413 cnt = 0;
414 }
415
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000416 if (cnt > max_token)
417 max_token = cnt;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000418
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000419 // Determine the approximate size of the message bubble in em.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000420 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000421 const unsigned max_line = 120;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000422
423 if (max_token >= max_line)
424 em = max_token / 2;
425 else {
426 unsigned characters = max_line;
427 unsigned lines = len / max_line;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000428
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000429 if (lines > 0) {
430 for (; characters > max_token; --characters)
431 if (len / characters > lines) {
432 ++characters;
433 break;
434 }
435 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000436
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000437 em = characters / 2;
438 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000439
440 if (em < max_line/2)
441 os << "; max-width:" << em << "em";
442 }
443 else
444 os << "; max-width:100em";
445
446 os << "\">";
447
448 if (max > 1) {
449 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
450 os << "<div class=\"PathIndex";
451 if (Kind) os << " PathIndex" << Kind;
452 os << "\">" << num << "</div>";
453 os << "</td><td>";
454 }
455
456 if (const PathDiagnosticMacroPiece *MP =
457 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
458
459 os << "Within the expansion of the macro '";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000460
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000461 // Get the name of the macro by relexing it.
462 {
463 FullSourceLoc L = MP->getLocation().getInstantiationLoc();
464 assert(L.isFileID());
465 std::pair<const char*, const char*> BufferInfo = L.getBufferData();
466 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.first;
467 Lexer rawLexer(L, PP->getLangOptions(), BufferInfo.first,
468 MacroName, BufferInfo.second);
469
470 Token TheTok;
471 rawLexer.LexFromRawLexer(TheTok);
472 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
473 os << MacroName[i];
Ted Kremenek80bae762009-03-02 23:05:40 +0000474 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000475
476 os << "':\n";
Ted Kremenek80bae762009-03-02 23:05:40 +0000477
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000478 if (max > 1)
Ted Kremenek80bae762009-03-02 23:05:40 +0000479 os << "</td></tr></table>";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000480
481 // Within a macro piece. Write out each event.
482 ProcessMacroPiece(os, *MP, 0);
483 }
484 else {
485 os << html::EscapeText(P.getString());
Ted Kremenek80bae762009-03-02 23:05:40 +0000486
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000487 if (max > 1)
488 os << "</td></tr></table>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000489 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000490
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000491 os << "</div></td></tr>";
492
493 // Insert the new html.
494 unsigned DisplayPos = LineEnd - FileStart;
495 SourceLocation Loc =
496 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
497
498 R.InsertStrBefore(Loc, os.str());
499
500 // Now highlight the ranges.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000501 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
502 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000503 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000504
505#if 0
506 // If there is a code insertion hint, insert that code.
507 // FIXME: This code is disabled because it seems to mangle the HTML
508 // output. I'm leaving it here because it's generally the right idea,
509 // but needs some help from someone more familiar with the rewriter.
510 for (const CodeModificationHint *Hint = P.code_modifications_begin(),
511 *HintEnd = P.code_modifications_end();
512 Hint != HintEnd; ++Hint) {
513 if (Hint->RemoveRange.isValid()) {
514 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
515 "<span class=\"CodeRemovalHint\">", "</span>");
516 }
517 if (Hint->InsertionLoc.isValid()) {
518 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
519 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
520 + "</span>";
521 R.InsertStrBefore(Hint->InsertionLoc, EscapedCode);
522 }
523 }
524#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000525}
526
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000527static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
528 llvm::SmallVector<char, 10> buf;
529
530 do {
531 unsigned x = n % ('z' - 'a');
532 buf.push_back('a' + x);
533 n = n / ('z' - 'a');
534 } while (n);
535
536 assert(!buf.empty());
537
538 for (llvm::SmallVectorImpl<char>::reverse_iterator I=buf.rbegin(),
539 E=buf.rend(); I!=E; ++I)
540 os << *I;
541}
542
543unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
544 const PathDiagnosticMacroPiece& P,
545 unsigned num) {
546
547 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
548 I!=E; ++I) {
549
550 if (const PathDiagnosticMacroPiece *MP =
551 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
552 num = ProcessMacroPiece(os, *MP, num);
553 continue;
554 }
555
556 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
557 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
558 "margin-left:5px\">"
559 "<table class=\"msgT\"><tr>"
560 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
561 EmitAlphaCounter(os, num++);
562 os << "</div></td><td valign=\"top\">"
563 << html::EscapeText(EP->getString())
564 << "</td></tr></table></div>\n";
565 }
566 }
567
568 return num;
569}
570
Chris Lattner2b2453a2009-01-17 06:22:33 +0000571void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000572 SourceRange Range,
573 const char *HighlightStart,
574 const char *HighlightEnd) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000575
Ted Kremenek5dd00412008-04-14 21:06:04 +0000576 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000577
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000578 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000579 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000580
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000581 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000582 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000583
584 if (EndLineNo < StartLineNo)
585 return;
586
Chris Lattnera11d6172009-01-19 07:46:45 +0000587 if (SM.getFileID(InstantiationStart) != BugFileID ||
588 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000589 return;
590
591 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000592 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000593 unsigned OldEndColNo = EndColNo;
594
595 if (EndColNo) {
596 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000597 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000598 }
599
600 // Highlight the range. Make the span tag the outermost tag for the
601 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000602
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000603 SourceLocation E =
604 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000605
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000606 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000607}