blob: 160da7c09734756027cbfa919b3d0c6d0118a3f7 [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 Kremenekf7556062009-07-27 22:13:39 +000042 std::vector<const PathDiagnostic*> BatchedDiags;
43 llvm::SmallVectorImpl<std::string> *FilesMade;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000044public:
Ted Kremenekf7556062009-07-27 22:13:39 +000045 HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
46 llvm::SmallVectorImpl<std::string> *filesMade = 0);
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
Chris Lattner409d4e72009-04-17 20:40:01 +000050 virtual void SetPreprocessor(Preprocessor *pp) { PP = pp; }
51
Ted Kremenek55851142008-04-22 16:15:03 +000052 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000053
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000054 unsigned ProcessMacroPiece(llvm::raw_ostream& os,
55 const PathDiagnosticMacroPiece& P,
56 unsigned num);
57
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);
Ted Kremenek33bd9422008-03-31 23:30: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
65 void ReportDiag(const PathDiagnostic& D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000066};
67
68} // end anonymous namespace
69
Ted Kremenekf7556062009-07-27 22:13:39 +000070HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
71 llvm::SmallVectorImpl<std::string>* filesMade)
Ted Kremenek47abe762008-04-16 16:39:56 +000072 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenekf7556062009-07-27 22:13:39 +000073 PP(pp), FilesMade(filesMade) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000074
75 // All html files begin with "report"
76 FilePrefix.appendComponent("report");
77}
78
79PathDiagnosticClient*
Ted Kremenek339b9c22008-04-17 22:31:54 +000080clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
Ted Kremenekf7556062009-07-27 22:13:39 +000081 PreprocessorFactory*,
82 llvm::SmallVectorImpl<std::string>* FilesMade)
83{
84 return new HTMLDiagnostics(prefix, PP, FilesMade);
85}
86
87//===----------------------------------------------------------------------===//
88// Factory for HTMLDiagnosticClients
89//===----------------------------------------------------------------------===//
90
91namespace {
92class VISIBILITY_HIDDEN HTMLDiagnosticsFactory
93 : public PathDiagnosticClientFactory {
94
95 std::string Prefix;
96 Preprocessor *PP;
97public:
98 HTMLDiagnosticsFactory(const std::string& prefix, Preprocessor* pp)
99 : Prefix(prefix), PP(pp) {}
100
101 virtual ~HTMLDiagnosticsFactory() {}
102
103 const char *getName() const { return "HTMLDiagnostics"; }
104
105 PathDiagnosticClient*
106 createPathDiagnosticClient(llvm::SmallVectorImpl<std::string> *FilesMade) {
107
108 return new HTMLDiagnostics(Prefix, PP, FilesMade);
109 }
110};
111} // end anonymous namespace
112
113PathDiagnosticClientFactory*
114clang::CreateHTMLDiagnosticClientFactory(const std::string& prefix,
115 Preprocessor* PP,
116 PreprocessorFactory*) {
117 return new HTMLDiagnosticsFactory(prefix, PP);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000118}
119
120//===----------------------------------------------------------------------===//
121// Report processing.
122//===----------------------------------------------------------------------===//
123
Ted Kremenek55851142008-04-22 16:15:03 +0000124void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
125 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000126 return;
127
Ted Kremenek55851142008-04-22 16:15:03 +0000128 if (D->empty()) {
129 delete D;
130 return;
131 }
132
Ted Kremenek7db0a942009-04-02 05:17:38 +0000133 const_cast<PathDiagnostic*>(D)->flattenLocations();
Ted Kremenek55851142008-04-22 16:15:03 +0000134 BatchedDiags.push_back(D);
135}
136
137HTMLDiagnostics::~HTMLDiagnostics() {
Ted Kremenek55851142008-04-22 16:15:03 +0000138 while (!BatchedDiags.empty()) {
139 const PathDiagnostic* D = BatchedDiags.back();
140 BatchedDiags.pop_back();
141 ReportDiag(*D);
142 delete D;
143 }
144}
145
146void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000147 // Create the HTML directory if it is missing.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000148 if (!createdDir) {
149 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000150 std::string ErrorMsg;
151 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000152
153 if (!Directory.isDirectory()) {
154 llvm::cerr << "warning: could not create directory '"
Ted Kremenek344f7e32008-04-03 17:55:57 +0000155 << Directory.toString() << "'\n"
156 << "reason: " << ErrorMsg << '\n';
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000157
158 noDir = true;
159
160 return;
161 }
162 }
163
164 if (noDir)
165 return;
166
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000167 const SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000168 FileID FID;
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000169
170 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000171 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000172 FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc();
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000173
Chris Lattner2b2453a2009-01-17 06:22:33 +0000174 if (FID.isInvalid()) {
Chris Lattnera11d6172009-01-19 07:46:45 +0000175 FID = SMgr.getFileID(L);
176 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000177 return; // FIXME: Emit a warning?
178
179 // Check the source ranges.
180 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
181 RE=I->ranges_end(); RI!=RE; ++RI) {
182
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000183 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000184
Chris Lattnera11d6172009-01-19 07:46:45 +0000185 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000186 return; // FIXME: Emit a warning?
187
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000188 L = SMgr.getInstantiationLoc(RI->getEnd());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000189
Chris Lattnera11d6172009-01-19 07:46:45 +0000190 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000191 return; // FIXME: Emit a warning?
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000192 }
193 }
194
Chris Lattner2b2453a2009-01-17 06:22:33 +0000195 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000196 return; // FIXME: Emit a warning?
197
198 // Create a new rewriter to generate HTML.
Chris Lattner2c78b872009-04-14 23:22:57 +0000199 Rewriter R(const_cast<SourceManager&>(SMgr), PP->getLangOptions());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000200
Ted Kremenek00086832009-03-10 02:49:29 +0000201 // Process the path.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000202 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000203 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000204
205 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
Chris Lattner409d4e72009-04-17 20:40:01 +0000206 I!=E; ++I, --n)
Chris Lattner2b2453a2009-01-17 06:22:33 +0000207 HandlePiece(R, FID, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000208
209 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000210
Chris Lattner2b2453a2009-01-17 06:22:33 +0000211 // unsigned FID = R.getSourceMgr().getMainFileID();
212 html::EscapeText(R, FID);
213 html::AddLineNumbers(R, FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000214
Ted Kremenek47abe762008-04-16 16:39:56 +0000215 // If we have a preprocessor, relex the file and syntax highlight.
216 // We might not have a preprocessor if we come from a deserialized AST file,
217 // for example.
218
Chris Lattner2b2453a2009-01-17 06:22:33 +0000219 if (PP) html::SyntaxHighlight(R, FID, *PP);
Ted Kremenek55851142008-04-22 16:15:03 +0000220
221 // FIXME: We eventually want to use PPF to create a fresh Preprocessor,
222 // once we have worked out the bugs.
223 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000224 // if (PPF) html::HighlightMacros(R, FID, *PPF);
Ted Kremenek55851142008-04-22 16:15:03 +0000225 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000226 if (PP) html::HighlightMacros(R, FID, *PP);
Ted Kremenek47abe762008-04-16 16:39:56 +0000227
Ted Kremenekb9476392008-04-02 20:44:16 +0000228 // Get the full directory name of the analyzed file.
229
Chris Lattner2b2453a2009-01-17 06:22:33 +0000230 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Ted Kremenek2e939812008-03-27 07:35:49 +0000231
Ted Kremenek7fc89572008-04-24 23:37:03 +0000232 // This is a cludge; basically we want to append either the full
233 // working directory if we have no directory information. This is
234 // a work in progress.
235
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000236 std::string DirName = "";
237
238 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
239 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
240 DirName = P.toString() + "/";
241 }
Ted Kremenekb9476392008-04-02 20:44:16 +0000242
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000243 // Add the name of the file as an <h1> tag.
244
Ted Kremenek2e939812008-03-27 07:35:49 +0000245 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000246 std::string s;
247 llvm::raw_string_ostream os(s);
Ted Kremenek2e939812008-03-27 07:35:49 +0000248
Ted Kremenek778246a2008-09-22 17:33:32 +0000249 os << "<!-- REPORTHEADER -->\n"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000250 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000251 "<tr><td class=\"rowname\">File:</td><td>"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000252 << html::EscapeText(DirName)
253 << html::EscapeText(Entry->getName())
254 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
255 "<a href=\"#EndPath\">line "
256 << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber()
257 << ", column "
258 << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber()
259 << "</a></td></tr>\n"
260 "<tr><td class=\"rowname\">Description:</td><td>"
261 << D.getDescription() << "</td></tr>\n";
Ted Kremenek072192b2008-04-30 23:47:44 +0000262
263 // Output any other meta data.
264
265 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
266 I!=E; ++I) {
267 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
268 }
269
Ted Kremenek778246a2008-09-22 17:33:32 +0000270 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
271 "<h3>Annotated Source Code</h3>\n";
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000272
Chris Lattner2b2453a2009-01-17 06:22:33 +0000273 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000274 }
275
Ted Kremenekb9476392008-04-02 20:44:16 +0000276 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000277
278 const std::string& BugDesc = D.getDescription();
279
280 if (!BugDesc.empty()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000281 std::string s;
282 llvm::raw_string_ostream os(s);
Ted Kremenek86b43812008-04-02 18:03:20 +0000283 os << "\n<!-- BUGDESC " << BugDesc << " -->\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
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000287 const std::string& BugType = D.getBugType();
288 if (!BugType.empty()) {
289 std::string s;
290 llvm::raw_string_ostream os(s);
291 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
292 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
293 }
294
Ted Kremenek8c036c72008-09-20 04:23:38 +0000295 const std::string& BugCategory = D.getCategory();
296
297 if (!BugCategory.empty()) {
298 std::string s;
299 llvm::raw_string_ostream os(s);
300 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000301 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek8c036c72008-09-20 04:23:38 +0000302 }
303
Ted Kremenekb9476392008-04-02 20:44:16 +0000304 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000305 std::string s;
306 llvm::raw_string_ostream os(s);
Ted Kremenek7fc89572008-04-24 23:37:03 +0000307 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000308 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000309 }
310
311 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000312 std::string s;
313 llvm::raw_string_ostream os(s);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000314 os << "\n<!-- BUGLINE "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000315 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
316 << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000317 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000318 }
319
320 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000321 std::string s;
322 llvm::raw_string_ostream os(s);
Ted Kremenekb9476392008-04-02 20:44:16 +0000323 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000324 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000325 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000326
327 // Add CSS, header, and footer.
328
Chris Lattner2b2453a2009-01-17 06:22:33 +0000329 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000330
331 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000332 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000333
334 if (!Buf) {
335 llvm::cerr << "warning: no diagnostics generated for main file.\n";
336 return;
337 }
338
339 // Create the stream to write out the HTML.
340 std::ofstream os;
341
342 {
343 // Create a path for the target HTML file.
344 llvm::sys::Path F(FilePrefix);
345 F.makeUnique(false, NULL);
346
347 // Rename the file with an HTML extension.
348 llvm::sys::Path H(F);
349 H.appendSuffix("html");
350 F.renamePathOnDisk(H, NULL);
351
352 os.open(H.toString().c_str());
353
354 if (!os) {
355 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
356 return;
357 }
Ted Kremenekf7556062009-07-27 22:13:39 +0000358
359 if (FilesMade)
360 FilesMade->push_back(H.getLast());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000361 }
362
363 // Emit the HTML to disk.
Ted Kremenek7414dc02008-04-20 01:02:33 +0000364 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekf7556062009-07-27 22:13:39 +0000365 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000366}
367
Chris Lattner2b2453a2009-01-17 06:22:33 +0000368void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000369 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000370 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000371
372 // For now, just draw a box above the line in question, and emit the
373 // warning.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000374 FullSourceLoc Pos = P.getLocation().asLocation();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000375
376 if (!Pos.isValid())
377 return;
378
Chris Lattner2b2453a2009-01-17 06:22:33 +0000379 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000380 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
381 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000382
Chris Lattner7da5aea2009-02-04 00:55:58 +0000383 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000384 return;
385
Chris Lattner7da5aea2009-02-04 00:55:58 +0000386 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000387 const char* FileStart = Buf->getBufferStart();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000388
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000389 // Compute the column number. Rewind from the current position to the start
390 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000391 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
392 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000393 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000394
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000395 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000396 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000397 const char* FileEnd = Buf->getBufferEnd();
398 while (*LineEnd != '\n' && LineEnd != FileEnd)
399 ++LineEnd;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000400
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000401 // Compute the margin offset by counting tabs and non-tabs.
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000402 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000403 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000404 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000405
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000406 // Create the html for the message.
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000407
408 const char *Kind = 0;
409 switch (P.getKind()) {
410 case PathDiagnosticPiece::Event: Kind = "Event"; break;
411 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
412 // Setting Kind to "Control" is intentional.
413 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
414 }
415
416 std::string sbuf;
417 llvm::raw_string_ostream os(sbuf);
418
419 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
420
421 if (num == max)
422 os << "EndPath";
423 else
424 os << "Path" << num;
425
426 os << "\" class=\"msg";
427 if (Kind)
428 os << " msg" << Kind;
429 os << "\" style=\"margin-left:" << PosNo << "ex";
430
431 // Output a maximum size.
432 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000433 // Get the string and determining its maximum substring.
434 const std::string& Msg = P.getString();
435 unsigned max_token = 0;
436 unsigned cnt = 0;
437 unsigned len = Msg.size();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000438
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000439 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
440 switch (*I) {
441 default:
442 ++cnt;
Ted Kremenek00086832009-03-10 02:49:29 +0000443 continue;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000444 case ' ':
445 case '\t':
446 case '\n':
447 if (cnt > max_token) max_token = cnt;
448 cnt = 0;
449 }
450
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000451 if (cnt > max_token)
452 max_token = cnt;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000453
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000454 // Determine the approximate size of the message bubble in em.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000455 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000456 const unsigned max_line = 120;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000457
458 if (max_token >= max_line)
459 em = max_token / 2;
460 else {
461 unsigned characters = max_line;
462 unsigned lines = len / max_line;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000463
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000464 if (lines > 0) {
465 for (; characters > max_token; --characters)
466 if (len / characters > lines) {
467 ++characters;
468 break;
469 }
470 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000471
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000472 em = characters / 2;
473 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000474
475 if (em < max_line/2)
476 os << "; max-width:" << em << "em";
477 }
478 else
479 os << "; max-width:100em";
480
481 os << "\">";
482
483 if (max > 1) {
484 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
485 os << "<div class=\"PathIndex";
486 if (Kind) os << " PathIndex" << Kind;
487 os << "\">" << num << "</div>";
488 os << "</td><td>";
489 }
490
491 if (const PathDiagnosticMacroPiece *MP =
492 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
493
494 os << "Within the expansion of the macro '";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000495
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000496 // Get the name of the macro by relexing it.
497 {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000498 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000499 assert(L.isFileID());
500 std::pair<const char*, const char*> BufferInfo = L.getBufferData();
501 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.first;
502 Lexer rawLexer(L, PP->getLangOptions(), BufferInfo.first,
503 MacroName, BufferInfo.second);
504
505 Token TheTok;
506 rawLexer.LexFromRawLexer(TheTok);
507 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
508 os << MacroName[i];
Ted Kremenek80bae762009-03-02 23:05:40 +0000509 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000510
511 os << "':\n";
Ted Kremenek80bae762009-03-02 23:05:40 +0000512
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000513 if (max > 1)
Ted Kremenek80bae762009-03-02 23:05:40 +0000514 os << "</td></tr></table>";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000515
516 // Within a macro piece. Write out each event.
517 ProcessMacroPiece(os, *MP, 0);
518 }
519 else {
520 os << html::EscapeText(P.getString());
Ted Kremenek80bae762009-03-02 23:05:40 +0000521
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000522 if (max > 1)
523 os << "</td></tr></table>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000524 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000525
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000526 os << "</div></td></tr>";
527
528 // Insert the new html.
529 unsigned DisplayPos = LineEnd - FileStart;
530 SourceLocation Loc =
531 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
532
533 R.InsertStrBefore(Loc, os.str());
534
535 // Now highlight the ranges.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000536 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
537 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000538 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000539
540#if 0
541 // If there is a code insertion hint, insert that code.
542 // FIXME: This code is disabled because it seems to mangle the HTML
543 // output. I'm leaving it here because it's generally the right idea,
544 // but needs some help from someone more familiar with the rewriter.
545 for (const CodeModificationHint *Hint = P.code_modifications_begin(),
546 *HintEnd = P.code_modifications_end();
547 Hint != HintEnd; ++Hint) {
548 if (Hint->RemoveRange.isValid()) {
549 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
550 "<span class=\"CodeRemovalHint\">", "</span>");
551 }
552 if (Hint->InsertionLoc.isValid()) {
553 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
554 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
555 + "</span>";
556 R.InsertStrBefore(Hint->InsertionLoc, EscapedCode);
557 }
558 }
559#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000560}
561
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000562static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
563 llvm::SmallVector<char, 10> buf;
564
565 do {
566 unsigned x = n % ('z' - 'a');
567 buf.push_back('a' + x);
568 n = n / ('z' - 'a');
569 } while (n);
570
571 assert(!buf.empty());
572
573 for (llvm::SmallVectorImpl<char>::reverse_iterator I=buf.rbegin(),
574 E=buf.rend(); I!=E; ++I)
575 os << *I;
576}
577
578unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
579 const PathDiagnosticMacroPiece& P,
580 unsigned num) {
581
582 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
583 I!=E; ++I) {
584
585 if (const PathDiagnosticMacroPiece *MP =
586 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
587 num = ProcessMacroPiece(os, *MP, num);
588 continue;
589 }
590
591 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
592 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
593 "margin-left:5px\">"
594 "<table class=\"msgT\"><tr>"
595 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
596 EmitAlphaCounter(os, num++);
597 os << "</div></td><td valign=\"top\">"
598 << html::EscapeText(EP->getString())
599 << "</td></tr></table></div>\n";
600 }
601 }
602
603 return num;
604}
605
Chris Lattner2b2453a2009-01-17 06:22:33 +0000606void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000607 SourceRange Range,
608 const char *HighlightStart,
609 const char *HighlightEnd) {
Chris Lattner2c78b872009-04-14 23:22:57 +0000610 SourceManager &SM = R.getSourceMgr();
611 const LangOptions &LangOpts = R.getLangOpts();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000612
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000613 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000614 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000615
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000616 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000617 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000618
619 if (EndLineNo < StartLineNo)
620 return;
621
Chris Lattnera11d6172009-01-19 07:46:45 +0000622 if (SM.getFileID(InstantiationStart) != BugFileID ||
623 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000624 return;
625
626 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000627 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000628 unsigned OldEndColNo = EndColNo;
629
630 if (EndColNo) {
631 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +0000632 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000633 }
634
635 // Highlight the range. Make the span tag the outermost tag for the
636 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000637
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000638 SourceLocation E =
639 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000640
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000641 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000642}