blob: 838b162b798d826f94fd36eb57f1a2c52402d7ac [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"
Ted Kremeneka95d3752008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000027#include "llvm/System/Path.h"
28#include <fstream>
Ted Kremenek88f5cde2008-03-27 06:17:42 +000029using namespace clang;
30
31//===----------------------------------------------------------------------===//
32// Boilerplate.
33//===----------------------------------------------------------------------===//
34
35namespace {
36
37class VISIBILITY_HIDDEN HTMLDiagnostics : public PathDiagnosticClient {
38 llvm::sys::Path Directory, FilePrefix;
39 bool createdDir, noDir;
Ted Kremenek47abe762008-04-16 16:39:56 +000040 Preprocessor* PP;
Ted Kremenekf7556062009-07-27 22:13:39 +000041 std::vector<const PathDiagnostic*> BatchedDiags;
42 llvm::SmallVectorImpl<std::string> *FilesMade;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000043public:
Ted Kremenekf7556062009-07-27 22:13:39 +000044 HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
45 llvm::SmallVectorImpl<std::string> *filesMade = 0);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000046
Ted Kremenek55851142008-04-22 16:15:03 +000047 virtual ~HTMLDiagnostics();
Ted Kremenek88f5cde2008-03-27 06:17:42 +000048
Chris Lattner409d4e72009-04-17 20:40:01 +000049 virtual void SetPreprocessor(Preprocessor *pp) { PP = pp; }
50
Ted Kremenek55851142008-04-22 16:15:03 +000051 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000052
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000053 unsigned ProcessMacroPiece(llvm::raw_ostream& os,
54 const PathDiagnosticMacroPiece& P,
55 unsigned num);
56
Chris Lattner2b2453a2009-01-17 06:22:33 +000057 void HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +000058 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Ted Kremenek33bd9422008-03-31 23:30:12 +000059
Douglas Gregor4b2d3f72009-02-26 21:00:50 +000060 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
61 const char *HighlightStart = "<span class=\"mrange\">",
62 const char *HighlightEnd = "</span>");
Ted Kremenek55851142008-04-22 16:15:03 +000063
64 void ReportDiag(const PathDiagnostic& D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000065};
66
67} // end anonymous namespace
68
Ted Kremenekf7556062009-07-27 22:13:39 +000069HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
70 llvm::SmallVectorImpl<std::string>* filesMade)
Ted Kremenek47abe762008-04-16 16:39:56 +000071 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenekf7556062009-07-27 22:13:39 +000072 PP(pp), FilesMade(filesMade) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000073
74 // All html files begin with "report"
75 FilePrefix.appendComponent("report");
76}
77
78PathDiagnosticClient*
Ted Kremenek339b9c22008-04-17 22:31:54 +000079clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
Ted Kremenekf7556062009-07-27 22:13:39 +000080 PreprocessorFactory*,
81 llvm::SmallVectorImpl<std::string>* FilesMade)
82{
83 return new HTMLDiagnostics(prefix, PP, FilesMade);
84}
85
86//===----------------------------------------------------------------------===//
87// Factory for HTMLDiagnosticClients
88//===----------------------------------------------------------------------===//
89
90namespace {
91class VISIBILITY_HIDDEN HTMLDiagnosticsFactory
92 : public PathDiagnosticClientFactory {
93
94 std::string Prefix;
95 Preprocessor *PP;
96public:
97 HTMLDiagnosticsFactory(const std::string& prefix, Preprocessor* pp)
98 : Prefix(prefix), PP(pp) {}
99
100 virtual ~HTMLDiagnosticsFactory() {}
101
102 const char *getName() const { return "HTMLDiagnostics"; }
103
104 PathDiagnosticClient*
105 createPathDiagnosticClient(llvm::SmallVectorImpl<std::string> *FilesMade) {
106
107 return new HTMLDiagnostics(Prefix, PP, FilesMade);
108 }
109};
110} // end anonymous namespace
111
112PathDiagnosticClientFactory*
113clang::CreateHTMLDiagnosticClientFactory(const std::string& prefix,
114 Preprocessor* PP,
115 PreprocessorFactory*) {
116 return new HTMLDiagnosticsFactory(prefix, PP);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000117}
118
119//===----------------------------------------------------------------------===//
120// Report processing.
121//===----------------------------------------------------------------------===//
122
Ted Kremenek55851142008-04-22 16:15:03 +0000123void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
124 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000125 return;
126
Ted Kremenek55851142008-04-22 16:15:03 +0000127 if (D->empty()) {
128 delete D;
129 return;
130 }
131
Ted Kremenek7db0a942009-04-02 05:17:38 +0000132 const_cast<PathDiagnostic*>(D)->flattenLocations();
Ted Kremenek55851142008-04-22 16:15:03 +0000133 BatchedDiags.push_back(D);
134}
135
136HTMLDiagnostics::~HTMLDiagnostics() {
Ted Kremenek55851142008-04-22 16:15:03 +0000137 while (!BatchedDiags.empty()) {
138 const PathDiagnostic* D = BatchedDiags.back();
139 BatchedDiags.pop_back();
140 ReportDiag(*D);
141 delete D;
142 }
143}
144
145void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000146 // Create the HTML directory if it is missing.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000147 if (!createdDir) {
148 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000149 std::string ErrorMsg;
150 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000151
152 if (!Directory.isDirectory()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000153 llvm::errs() << "warning: could not create directory '"
154 << Directory.toString() << "'\n"
155 << "reason: " << ErrorMsg << '\n';
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000156
157 noDir = true;
158
159 return;
160 }
161 }
162
163 if (noDir)
164 return;
165
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000166 const SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000167 FileID FID;
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000168
169 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000170 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000171 FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc();
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000172
Chris Lattner2b2453a2009-01-17 06:22:33 +0000173 if (FID.isInvalid()) {
Chris Lattnera11d6172009-01-19 07:46:45 +0000174 FID = SMgr.getFileID(L);
175 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000176 return; // FIXME: Emit a warning?
177
178 // Check the source ranges.
179 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
180 RE=I->ranges_end(); RI!=RE; ++RI) {
181
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000182 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000183
Chris Lattnera11d6172009-01-19 07:46:45 +0000184 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000185 return; // FIXME: Emit a warning?
186
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000187 L = SMgr.getInstantiationLoc(RI->getEnd());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000188
Chris Lattnera11d6172009-01-19 07:46:45 +0000189 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000190 return; // FIXME: Emit a warning?
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000191 }
192 }
193
Chris Lattner2b2453a2009-01-17 06:22:33 +0000194 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000195 return; // FIXME: Emit a warning?
196
197 // Create a new rewriter to generate HTML.
Chris Lattner2c78b872009-04-14 23:22:57 +0000198 Rewriter R(const_cast<SourceManager&>(SMgr), PP->getLangOptions());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000199
Ted Kremenek00086832009-03-10 02:49:29 +0000200 // Process the path.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000201 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000202 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000203
204 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
Chris Lattner409d4e72009-04-17 20:40:01 +0000205 I!=E; ++I, --n)
Chris Lattner2b2453a2009-01-17 06:22:33 +0000206 HandlePiece(R, FID, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000207
208 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000209
Chris Lattner2b2453a2009-01-17 06:22:33 +0000210 // unsigned FID = R.getSourceMgr().getMainFileID();
211 html::EscapeText(R, FID);
212 html::AddLineNumbers(R, FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000213
Ted Kremenek47abe762008-04-16 16:39:56 +0000214 // If we have a preprocessor, relex the file and syntax highlight.
215 // We might not have a preprocessor if we come from a deserialized AST file,
216 // for example.
217
Chris Lattner2b2453a2009-01-17 06:22:33 +0000218 if (PP) html::SyntaxHighlight(R, FID, *PP);
Ted Kremenek55851142008-04-22 16:15:03 +0000219
220 // FIXME: We eventually want to use PPF to create a fresh Preprocessor,
221 // once we have worked out the bugs.
222 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000223 // if (PPF) html::HighlightMacros(R, FID, *PPF);
Ted Kremenek55851142008-04-22 16:15:03 +0000224 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000225 if (PP) html::HighlightMacros(R, FID, *PP);
Ted Kremenek47abe762008-04-16 16:39:56 +0000226
Ted Kremenekb9476392008-04-02 20:44:16 +0000227 // Get the full directory name of the analyzed file.
228
Chris Lattner2b2453a2009-01-17 06:22:33 +0000229 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Ted Kremenek2e939812008-03-27 07:35:49 +0000230
Ted Kremenek7fc89572008-04-24 23:37:03 +0000231 // This is a cludge; basically we want to append either the full
232 // working directory if we have no directory information. This is
233 // a work in progress.
234
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000235 std::string DirName = "";
236
237 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
238 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
239 DirName = P.toString() + "/";
240 }
Ted Kremenekb9476392008-04-02 20:44:16 +0000241
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000242 // Add the name of the file as an <h1> tag.
243
Ted Kremenek2e939812008-03-27 07:35:49 +0000244 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000245 std::string s;
246 llvm::raw_string_ostream os(s);
Ted Kremenek2e939812008-03-27 07:35:49 +0000247
Ted Kremenek778246a2008-09-22 17:33:32 +0000248 os << "<!-- REPORTHEADER -->\n"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000249 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000250 "<tr><td class=\"rowname\">File:</td><td>"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000251 << html::EscapeText(DirName)
252 << html::EscapeText(Entry->getName())
253 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
254 "<a href=\"#EndPath\">line "
255 << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber()
256 << ", column "
257 << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber()
258 << "</a></td></tr>\n"
259 "<tr><td class=\"rowname\">Description:</td><td>"
260 << D.getDescription() << "</td></tr>\n";
Ted Kremenek072192b2008-04-30 23:47:44 +0000261
262 // Output any other meta data.
263
264 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
265 I!=E; ++I) {
266 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
267 }
268
Ted Kremenek778246a2008-09-22 17:33:32 +0000269 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
270 "<h3>Annotated Source Code</h3>\n";
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000271
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000272 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000273 }
274
Ted Kremenekb9476392008-04-02 20:44:16 +0000275 // Embed meta-data tags.
Ted Kremenekb9476392008-04-02 20:44:16 +0000276 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000277 std::string s;
278 llvm::raw_string_ostream os(s);
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000279
280 const std::string& BugDesc = D.getDescription();
281 if (!BugDesc.empty())
282 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
283
284 const std::string& BugType = D.getBugType();
285 if (!BugType.empty())
286 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
287
288 const std::string& BugCategory = D.getCategory();
289 if (!BugCategory.empty())
290 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
291
Ted Kremenek7fc89572008-04-24 23:37:03 +0000292 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000293
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000294 os << "\n<!-- BUGLINE "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000295 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
296 << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000297
298 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
299
300 // Mark the end of the tags.
301 os << "\n<!-- BUGMETAEND -->\n";
302
303 // Insert the text.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000304 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000305 }
306
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000307 // Add CSS, header, and footer.
308
Chris Lattner2b2453a2009-01-17 06:22:33 +0000309 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000310
311 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000312 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000313
314 if (!Buf) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000315 llvm::errs() << "warning: no diagnostics generated for main file.\n";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000316 return;
317 }
318
319 // Create the stream to write out the HTML.
320 std::ofstream os;
321
322 {
323 // Create a path for the target HTML file.
324 llvm::sys::Path F(FilePrefix);
325 F.makeUnique(false, NULL);
326
327 // Rename the file with an HTML extension.
328 llvm::sys::Path H(F);
329 H.appendSuffix("html");
330 F.renamePathOnDisk(H, NULL);
331
332 os.open(H.toString().c_str());
333
334 if (!os) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000335 llvm::errs() << "warning: could not create file '" << F.toString() << "'\n";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000336 return;
337 }
Ted Kremenekf7556062009-07-27 22:13:39 +0000338
339 if (FilesMade)
340 FilesMade->push_back(H.getLast());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000341 }
342
343 // Emit the HTML to disk.
Ted Kremenek7414dc02008-04-20 01:02:33 +0000344 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekf7556062009-07-27 22:13:39 +0000345 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000346}
347
Chris Lattner2b2453a2009-01-17 06:22:33 +0000348void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000349 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000350 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000351
352 // For now, just draw a box above the line in question, and emit the
353 // warning.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000354 FullSourceLoc Pos = P.getLocation().asLocation();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000355
356 if (!Pos.isValid())
357 return;
358
Chris Lattner2b2453a2009-01-17 06:22:33 +0000359 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000360 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
361 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000362
Chris Lattner7da5aea2009-02-04 00:55:58 +0000363 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000364 return;
365
Chris Lattner7da5aea2009-02-04 00:55:58 +0000366 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000367 const char* FileStart = Buf->getBufferStart();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000368
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000369 // Compute the column number. Rewind from the current position to the start
370 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000371 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
372 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000373 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000374
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000375 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000376 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000377 const char* FileEnd = Buf->getBufferEnd();
378 while (*LineEnd != '\n' && LineEnd != FileEnd)
379 ++LineEnd;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000380
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000381 // Compute the margin offset by counting tabs and non-tabs.
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000382 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000383 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000384 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000385
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000386 // Create the html for the message.
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000387
388 const char *Kind = 0;
389 switch (P.getKind()) {
390 case PathDiagnosticPiece::Event: Kind = "Event"; break;
391 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
392 // Setting Kind to "Control" is intentional.
393 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
394 }
395
396 std::string sbuf;
397 llvm::raw_string_ostream os(sbuf);
398
399 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
400
401 if (num == max)
402 os << "EndPath";
403 else
404 os << "Path" << num;
405
406 os << "\" class=\"msg";
407 if (Kind)
408 os << " msg" << Kind;
409 os << "\" style=\"margin-left:" << PosNo << "ex";
410
411 // Output a maximum size.
412 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000413 // Get the string and determining its maximum substring.
414 const std::string& Msg = P.getString();
415 unsigned max_token = 0;
416 unsigned cnt = 0;
417 unsigned len = Msg.size();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000418
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000419 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
420 switch (*I) {
421 default:
422 ++cnt;
Ted Kremenek00086832009-03-10 02:49:29 +0000423 continue;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000424 case ' ':
425 case '\t':
426 case '\n':
427 if (cnt > max_token) max_token = cnt;
428 cnt = 0;
429 }
430
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000431 if (cnt > max_token)
432 max_token = cnt;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000433
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000434 // Determine the approximate size of the message bubble in em.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000435 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000436 const unsigned max_line = 120;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000437
438 if (max_token >= max_line)
439 em = max_token / 2;
440 else {
441 unsigned characters = max_line;
442 unsigned lines = len / max_line;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000443
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000444 if (lines > 0) {
445 for (; characters > max_token; --characters)
446 if (len / characters > lines) {
447 ++characters;
448 break;
449 }
450 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000451
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000452 em = characters / 2;
453 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000454
455 if (em < max_line/2)
456 os << "; max-width:" << em << "em";
457 }
458 else
459 os << "; max-width:100em";
460
461 os << "\">";
462
463 if (max > 1) {
464 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
465 os << "<div class=\"PathIndex";
466 if (Kind) os << " PathIndex" << Kind;
467 os << "\">" << num << "</div>";
468 os << "</td><td>";
469 }
470
471 if (const PathDiagnosticMacroPiece *MP =
472 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
473
474 os << "Within the expansion of the macro '";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000475
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000476 // Get the name of the macro by relexing it.
477 {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000478 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000479 assert(L.isFileID());
480 std::pair<const char*, const char*> BufferInfo = L.getBufferData();
481 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.first;
482 Lexer rawLexer(L, PP->getLangOptions(), BufferInfo.first,
483 MacroName, BufferInfo.second);
484
485 Token TheTok;
486 rawLexer.LexFromRawLexer(TheTok);
487 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
488 os << MacroName[i];
Ted Kremenek80bae762009-03-02 23:05:40 +0000489 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000490
491 os << "':\n";
Ted Kremenek80bae762009-03-02 23:05:40 +0000492
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000493 if (max > 1)
Ted Kremenek80bae762009-03-02 23:05:40 +0000494 os << "</td></tr></table>";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000495
496 // Within a macro piece. Write out each event.
497 ProcessMacroPiece(os, *MP, 0);
498 }
499 else {
500 os << html::EscapeText(P.getString());
Ted Kremenek80bae762009-03-02 23:05:40 +0000501
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000502 if (max > 1)
503 os << "</td></tr></table>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000504 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000505
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000506 os << "</div></td></tr>";
507
508 // Insert the new html.
509 unsigned DisplayPos = LineEnd - FileStart;
510 SourceLocation Loc =
511 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
512
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000513 R.InsertTextBefore(Loc, os.str());
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000514
515 // Now highlight the ranges.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000516 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
517 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000518 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000519
520#if 0
521 // If there is a code insertion hint, insert that code.
522 // FIXME: This code is disabled because it seems to mangle the HTML
523 // output. I'm leaving it here because it's generally the right idea,
524 // but needs some help from someone more familiar with the rewriter.
525 for (const CodeModificationHint *Hint = P.code_modifications_begin(),
526 *HintEnd = P.code_modifications_end();
527 Hint != HintEnd; ++Hint) {
528 if (Hint->RemoveRange.isValid()) {
529 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
530 "<span class=\"CodeRemovalHint\">", "</span>");
531 }
532 if (Hint->InsertionLoc.isValid()) {
533 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
534 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
535 + "</span>";
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000536 R.InsertTextBefore(Hint->InsertionLoc, EscapedCode);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000537 }
538 }
539#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000540}
541
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000542static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
543 llvm::SmallVector<char, 10> buf;
544
545 do {
546 unsigned x = n % ('z' - 'a');
547 buf.push_back('a' + x);
548 n = n / ('z' - 'a');
549 } while (n);
550
551 assert(!buf.empty());
552
553 for (llvm::SmallVectorImpl<char>::reverse_iterator I=buf.rbegin(),
554 E=buf.rend(); I!=E; ++I)
555 os << *I;
556}
557
558unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
559 const PathDiagnosticMacroPiece& P,
560 unsigned num) {
561
562 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
563 I!=E; ++I) {
564
565 if (const PathDiagnosticMacroPiece *MP =
566 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
567 num = ProcessMacroPiece(os, *MP, num);
568 continue;
569 }
570
571 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
572 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
573 "margin-left:5px\">"
574 "<table class=\"msgT\"><tr>"
575 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
576 EmitAlphaCounter(os, num++);
577 os << "</div></td><td valign=\"top\">"
578 << html::EscapeText(EP->getString())
579 << "</td></tr></table></div>\n";
580 }
581 }
582
583 return num;
584}
585
Chris Lattner2b2453a2009-01-17 06:22:33 +0000586void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000587 SourceRange Range,
588 const char *HighlightStart,
589 const char *HighlightEnd) {
Chris Lattner2c78b872009-04-14 23:22:57 +0000590 SourceManager &SM = R.getSourceMgr();
591 const LangOptions &LangOpts = R.getLangOpts();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000592
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000593 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000594 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000595
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000596 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000597 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000598
599 if (EndLineNo < StartLineNo)
600 return;
601
Chris Lattnera11d6172009-01-19 07:46:45 +0000602 if (SM.getFileID(InstantiationStart) != BugFileID ||
603 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000604 return;
605
606 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000607 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000608 unsigned OldEndColNo = EndColNo;
609
610 if (EndColNo) {
611 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +0000612 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000613 }
614
615 // Highlight the range. Make the span tag the outermost tag for the
616 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000617
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000618 SourceLocation E =
619 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000620
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000621 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000622}