blob: 83c2ac56a7585b8eab21849c52e19d2883040935 [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
Ted Kremenek7db0a942009-04-02 05:17:38 +000097 const_cast<PathDiagnostic*>(D)->flattenLocations();
Ted Kremenek55851142008-04-22 16:15:03 +000098 BatchedDiags.push_back(D);
99}
100
101HTMLDiagnostics::~HTMLDiagnostics() {
102
103 while (!BatchedDiags.empty()) {
104 const PathDiagnostic* D = BatchedDiags.back();
105 BatchedDiags.pop_back();
106 ReportDiag(*D);
107 delete D;
108 }
109}
110
111void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) {
112
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000113 // Create the HTML directory if it is missing.
114
115 if (!createdDir) {
116 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000117 std::string ErrorMsg;
118 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000119
120 if (!Directory.isDirectory()) {
121 llvm::cerr << "warning: could not create directory '"
Ted Kremenek344f7e32008-04-03 17:55:57 +0000122 << Directory.toString() << "'\n"
123 << "reason: " << ErrorMsg << '\n';
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000124
125 noDir = true;
126
127 return;
128 }
129 }
130
131 if (noDir)
132 return;
133
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000134 const SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000135 FileID FID;
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000136
137 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000138 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000139 FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc();
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000140
Chris Lattner2b2453a2009-01-17 06:22:33 +0000141 if (FID.isInvalid()) {
Chris Lattnera11d6172009-01-19 07:46:45 +0000142 FID = SMgr.getFileID(L);
143 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000144 return; // FIXME: Emit a warning?
145
146 // Check the source ranges.
147 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
148 RE=I->ranges_end(); RI!=RE; ++RI) {
149
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000150 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000151
Chris Lattnera11d6172009-01-19 07:46:45 +0000152 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000153 return; // FIXME: Emit a warning?
154
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000155 L = SMgr.getInstantiationLoc(RI->getEnd());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000156
Chris Lattnera11d6172009-01-19 07:46:45 +0000157 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000158 return; // FIXME: Emit a warning?
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000159 }
160 }
161
Chris Lattner2b2453a2009-01-17 06:22:33 +0000162 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000163 return; // FIXME: Emit a warning?
164
165 // Create a new rewriter to generate HTML.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000166 Rewriter R(const_cast<SourceManager&>(SMgr));
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000167
Ted Kremenek00086832009-03-10 02:49:29 +0000168 // Process the path.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000169 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000170 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000171
172 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
173 I!=E; ++I, --n) {
174
Chris Lattner2b2453a2009-01-17 06:22:33 +0000175 HandlePiece(R, FID, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000176 }
177
178 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000179
Chris Lattner2b2453a2009-01-17 06:22:33 +0000180 // unsigned FID = R.getSourceMgr().getMainFileID();
181 html::EscapeText(R, FID);
182 html::AddLineNumbers(R, FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000183
Ted Kremenek47abe762008-04-16 16:39:56 +0000184 // If we have a preprocessor, relex the file and syntax highlight.
185 // We might not have a preprocessor if we come from a deserialized AST file,
186 // for example.
187
Chris Lattner2b2453a2009-01-17 06:22:33 +0000188 if (PP) html::SyntaxHighlight(R, FID, *PP);
Ted Kremenek55851142008-04-22 16:15:03 +0000189
190 // FIXME: We eventually want to use PPF to create a fresh Preprocessor,
191 // once we have worked out the bugs.
192 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000193 // if (PPF) html::HighlightMacros(R, FID, *PPF);
Ted Kremenek55851142008-04-22 16:15:03 +0000194 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000195 if (PP) html::HighlightMacros(R, FID, *PP);
Ted Kremenek47abe762008-04-16 16:39:56 +0000196
Ted Kremenekb9476392008-04-02 20:44:16 +0000197 // Get the full directory name of the analyzed file.
198
Chris Lattner2b2453a2009-01-17 06:22:33 +0000199 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Ted Kremenek2e939812008-03-27 07:35:49 +0000200
Ted Kremenek7fc89572008-04-24 23:37:03 +0000201 // This is a cludge; basically we want to append either the full
202 // working directory if we have no directory information. This is
203 // a work in progress.
204
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000205 std::string DirName = "";
206
207 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
208 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
209 DirName = P.toString() + "/";
210 }
Ted Kremenekb9476392008-04-02 20:44:16 +0000211
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000212 // Add the name of the file as an <h1> tag.
213
Ted Kremenek2e939812008-03-27 07:35:49 +0000214 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000215 std::string s;
216 llvm::raw_string_ostream os(s);
Ted Kremenek2e939812008-03-27 07:35:49 +0000217
Ted Kremenek778246a2008-09-22 17:33:32 +0000218 os << "<!-- REPORTHEADER -->\n"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000219 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000220 "<tr><td class=\"rowname\">File:</td><td>"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000221 << html::EscapeText(DirName)
222 << html::EscapeText(Entry->getName())
223 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
224 "<a href=\"#EndPath\">line "
225 << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber()
226 << ", column "
227 << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber()
228 << "</a></td></tr>\n"
229 "<tr><td class=\"rowname\">Description:</td><td>"
230 << D.getDescription() << "</td></tr>\n";
Ted Kremenek072192b2008-04-30 23:47:44 +0000231
232 // Output any other meta data.
233
234 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
235 I!=E; ++I) {
236 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
237 }
238
Ted Kremenek778246a2008-09-22 17:33:32 +0000239 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
240 "<h3>Annotated Source Code</h3>\n";
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000241
Chris Lattner2b2453a2009-01-17 06:22:33 +0000242 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000243 }
244
Ted Kremenekb9476392008-04-02 20:44:16 +0000245 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000246
247 const std::string& BugDesc = D.getDescription();
248
249 if (!BugDesc.empty()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000250 std::string s;
251 llvm::raw_string_ostream os(s);
Ted Kremenek86b43812008-04-02 18:03:20 +0000252 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000253 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000254 }
255
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000256 const std::string& BugType = D.getBugType();
257 if (!BugType.empty()) {
258 std::string s;
259 llvm::raw_string_ostream os(s);
260 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
261 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
262 }
263
Ted Kremenek8c036c72008-09-20 04:23:38 +0000264 const std::string& BugCategory = D.getCategory();
265
266 if (!BugCategory.empty()) {
267 std::string s;
268 llvm::raw_string_ostream os(s);
269 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000270 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek8c036c72008-09-20 04:23:38 +0000271 }
272
Ted Kremenekb9476392008-04-02 20:44:16 +0000273 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000274 std::string s;
275 llvm::raw_string_ostream os(s);
Ted Kremenek7fc89572008-04-24 23:37:03 +0000276 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000277 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000278 }
279
280 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000281 std::string s;
282 llvm::raw_string_ostream os(s);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000283 os << "\n<!-- BUGLINE "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000284 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
285 << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000286 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000287 }
288
289 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000290 std::string s;
291 llvm::raw_string_ostream os(s);
Ted Kremenekb9476392008-04-02 20:44:16 +0000292 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000293 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000294 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000295
296 // Add CSS, header, and footer.
297
Chris Lattner2b2453a2009-01-17 06:22:33 +0000298 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000299
300 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000301 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000302
303 if (!Buf) {
304 llvm::cerr << "warning: no diagnostics generated for main file.\n";
305 return;
306 }
307
308 // Create the stream to write out the HTML.
309 std::ofstream os;
310
311 {
312 // Create a path for the target HTML file.
313 llvm::sys::Path F(FilePrefix);
314 F.makeUnique(false, NULL);
315
316 // Rename the file with an HTML extension.
317 llvm::sys::Path H(F);
318 H.appendSuffix("html");
319 F.renamePathOnDisk(H, NULL);
320
321 os.open(H.toString().c_str());
322
323 if (!os) {
324 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
325 return;
326 }
327 }
328
329 // Emit the HTML to disk.
330
Ted Kremenek7414dc02008-04-20 01:02:33 +0000331 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000332 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000333}
334
Chris Lattner2b2453a2009-01-17 06:22:33 +0000335void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000336 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000337 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000338
339 // For now, just draw a box above the line in question, and emit the
340 // warning.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000341 FullSourceLoc Pos = P.getLocation().asLocation();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000342
343 if (!Pos.isValid())
344 return;
345
Chris Lattner2b2453a2009-01-17 06:22:33 +0000346 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000347 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
348 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000349
Chris Lattner7da5aea2009-02-04 00:55:58 +0000350 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000351 return;
352
Chris Lattner7da5aea2009-02-04 00:55:58 +0000353 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000354 const char* FileStart = Buf->getBufferStart();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000355
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000356 // Compute the column number. Rewind from the current position to the start
357 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000358 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
359 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000360 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000361
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000362 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000363 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000364 const char* FileEnd = Buf->getBufferEnd();
365 while (*LineEnd != '\n' && LineEnd != FileEnd)
366 ++LineEnd;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000367
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000368 // Compute the margin offset by counting tabs and non-tabs.
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000369 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000370 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000371 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000372
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000373 // Create the html for the message.
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000374
375 const char *Kind = 0;
376 switch (P.getKind()) {
377 case PathDiagnosticPiece::Event: Kind = "Event"; break;
378 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
379 // Setting Kind to "Control" is intentional.
380 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
381 }
382
383 std::string sbuf;
384 llvm::raw_string_ostream os(sbuf);
385
386 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
387
388 if (num == max)
389 os << "EndPath";
390 else
391 os << "Path" << num;
392
393 os << "\" class=\"msg";
394 if (Kind)
395 os << " msg" << Kind;
396 os << "\" style=\"margin-left:" << PosNo << "ex";
397
398 // Output a maximum size.
399 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000400 // Get the string and determining its maximum substring.
401 const std::string& Msg = P.getString();
402 unsigned max_token = 0;
403 unsigned cnt = 0;
404 unsigned len = Msg.size();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000405
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000406 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
407 switch (*I) {
408 default:
409 ++cnt;
Ted Kremenek00086832009-03-10 02:49:29 +0000410 continue;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000411 case ' ':
412 case '\t':
413 case '\n':
414 if (cnt > max_token) max_token = cnt;
415 cnt = 0;
416 }
417
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000418 if (cnt > max_token)
419 max_token = cnt;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000420
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000421 // Determine the approximate size of the message bubble in em.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000422 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000423 const unsigned max_line = 120;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000424
425 if (max_token >= max_line)
426 em = max_token / 2;
427 else {
428 unsigned characters = max_line;
429 unsigned lines = len / max_line;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000430
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000431 if (lines > 0) {
432 for (; characters > max_token; --characters)
433 if (len / characters > lines) {
434 ++characters;
435 break;
436 }
437 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000438
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000439 em = characters / 2;
440 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000441
442 if (em < max_line/2)
443 os << "; max-width:" << em << "em";
444 }
445 else
446 os << "; max-width:100em";
447
448 os << "\">";
449
450 if (max > 1) {
451 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
452 os << "<div class=\"PathIndex";
453 if (Kind) os << " PathIndex" << Kind;
454 os << "\">" << num << "</div>";
455 os << "</td><td>";
456 }
457
458 if (const PathDiagnosticMacroPiece *MP =
459 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
460
461 os << "Within the expansion of the macro '";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000462
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000463 // Get the name of the macro by relexing it.
464 {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000465 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000466 assert(L.isFileID());
467 std::pair<const char*, const char*> BufferInfo = L.getBufferData();
468 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.first;
469 Lexer rawLexer(L, PP->getLangOptions(), BufferInfo.first,
470 MacroName, BufferInfo.second);
471
472 Token TheTok;
473 rawLexer.LexFromRawLexer(TheTok);
474 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
475 os << MacroName[i];
Ted Kremenek80bae762009-03-02 23:05:40 +0000476 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000477
478 os << "':\n";
Ted Kremenek80bae762009-03-02 23:05:40 +0000479
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000480 if (max > 1)
Ted Kremenek80bae762009-03-02 23:05:40 +0000481 os << "</td></tr></table>";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000482
483 // Within a macro piece. Write out each event.
484 ProcessMacroPiece(os, *MP, 0);
485 }
486 else {
487 os << html::EscapeText(P.getString());
Ted Kremenek80bae762009-03-02 23:05:40 +0000488
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000489 if (max > 1)
490 os << "</td></tr></table>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000491 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000492
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000493 os << "</div></td></tr>";
494
495 // Insert the new html.
496 unsigned DisplayPos = LineEnd - FileStart;
497 SourceLocation Loc =
498 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
499
500 R.InsertStrBefore(Loc, os.str());
501
502 // Now highlight the ranges.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000503 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
504 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000505 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000506
507#if 0
508 // If there is a code insertion hint, insert that code.
509 // FIXME: This code is disabled because it seems to mangle the HTML
510 // output. I'm leaving it here because it's generally the right idea,
511 // but needs some help from someone more familiar with the rewriter.
512 for (const CodeModificationHint *Hint = P.code_modifications_begin(),
513 *HintEnd = P.code_modifications_end();
514 Hint != HintEnd; ++Hint) {
515 if (Hint->RemoveRange.isValid()) {
516 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
517 "<span class=\"CodeRemovalHint\">", "</span>");
518 }
519 if (Hint->InsertionLoc.isValid()) {
520 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
521 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
522 + "</span>";
523 R.InsertStrBefore(Hint->InsertionLoc, EscapedCode);
524 }
525 }
526#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000527}
528
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000529static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
530 llvm::SmallVector<char, 10> buf;
531
532 do {
533 unsigned x = n % ('z' - 'a');
534 buf.push_back('a' + x);
535 n = n / ('z' - 'a');
536 } while (n);
537
538 assert(!buf.empty());
539
540 for (llvm::SmallVectorImpl<char>::reverse_iterator I=buf.rbegin(),
541 E=buf.rend(); I!=E; ++I)
542 os << *I;
543}
544
545unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
546 const PathDiagnosticMacroPiece& P,
547 unsigned num) {
548
549 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
550 I!=E; ++I) {
551
552 if (const PathDiagnosticMacroPiece *MP =
553 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
554 num = ProcessMacroPiece(os, *MP, num);
555 continue;
556 }
557
558 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
559 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
560 "margin-left:5px\">"
561 "<table class=\"msgT\"><tr>"
562 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
563 EmitAlphaCounter(os, num++);
564 os << "</div></td><td valign=\"top\">"
565 << html::EscapeText(EP->getString())
566 << "</td></tr></table></div>\n";
567 }
568 }
569
570 return num;
571}
572
Chris Lattner2b2453a2009-01-17 06:22:33 +0000573void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000574 SourceRange Range,
575 const char *HighlightStart,
576 const char *HighlightEnd) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000577
Ted Kremenek5dd00412008-04-14 21:06:04 +0000578 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000579
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000580 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000581 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000582
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000583 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000584 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000585
586 if (EndLineNo < StartLineNo)
587 return;
588
Chris Lattnera11d6172009-01-19 07:46:45 +0000589 if (SM.getFileID(InstantiationStart) != BugFileID ||
590 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000591 return;
592
593 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000594 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000595 unsigned OldEndColNo = EndColNo;
596
597 if (EndColNo) {
598 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000599 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000600 }
601
602 // Highlight the range. Make the span tag the outermost tag for the
603 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000604
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000605 SourceLocation E =
606 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000607
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000608 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000609}