blob: 9cfe0b2a6124b21bc02fa320e6a5102456d25e49 [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 Kremenek55851142008-04-22 16:15:03 +000042 std::vector<const PathDiagnostic*> BatchedDiags;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000043public:
Chris Lattner409d4e72009-04-17 20:40:01 +000044 HTMLDiagnostics(const std::string& prefix, Preprocessor* pp);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000045
Ted Kremenek55851142008-04-22 16:15:03 +000046 virtual ~HTMLDiagnostics();
Ted Kremenek88f5cde2008-03-27 06:17:42 +000047
Chris Lattner409d4e72009-04-17 20:40:01 +000048 virtual void SetPreprocessor(Preprocessor *pp) { PP = pp; }
49
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
Chris Lattner409d4e72009-04-17 20:40:01 +000068HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp)
Ted Kremenek47abe762008-04-16 16:39:56 +000069 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Chris Lattner409d4e72009-04-17 20:40:01 +000070 PP(pp) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000071
72 // All html files begin with "report"
73 FilePrefix.appendComponent("report");
74}
75
76PathDiagnosticClient*
Ted Kremenek339b9c22008-04-17 22:31:54 +000077clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
Chris Lattner409d4e72009-04-17 20:40:01 +000078 PreprocessorFactory*) {
79 return new HTMLDiagnostics(prefix, PP);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000080}
81
82//===----------------------------------------------------------------------===//
83// Report processing.
84//===----------------------------------------------------------------------===//
85
Ted Kremenek55851142008-04-22 16:15:03 +000086void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
87 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +000088 return;
89
Ted Kremenek55851142008-04-22 16:15:03 +000090 if (D->empty()) {
91 delete D;
92 return;
93 }
94
Ted Kremenek7db0a942009-04-02 05:17:38 +000095 const_cast<PathDiagnostic*>(D)->flattenLocations();
Ted Kremenek55851142008-04-22 16:15:03 +000096 BatchedDiags.push_back(D);
97}
98
99HTMLDiagnostics::~HTMLDiagnostics() {
Ted Kremenek55851142008-04-22 16:15:03 +0000100 while (!BatchedDiags.empty()) {
101 const PathDiagnostic* D = BatchedDiags.back();
102 BatchedDiags.pop_back();
103 ReportDiag(*D);
104 delete D;
105 }
106}
107
108void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000109 // Create the HTML directory if it is missing.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000110 if (!createdDir) {
111 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000112 std::string ErrorMsg;
113 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000114
115 if (!Directory.isDirectory()) {
116 llvm::cerr << "warning: could not create directory '"
Ted Kremenek344f7e32008-04-03 17:55:57 +0000117 << Directory.toString() << "'\n"
118 << "reason: " << ErrorMsg << '\n';
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000119
120 noDir = true;
121
122 return;
123 }
124 }
125
126 if (noDir)
127 return;
128
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000129 const SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000130 FileID FID;
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000131
132 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000133 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000134 FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc();
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000135
Chris Lattner2b2453a2009-01-17 06:22:33 +0000136 if (FID.isInvalid()) {
Chris Lattnera11d6172009-01-19 07:46:45 +0000137 FID = SMgr.getFileID(L);
138 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000139 return; // FIXME: Emit a warning?
140
141 // Check the source ranges.
142 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
143 RE=I->ranges_end(); RI!=RE; ++RI) {
144
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000145 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000146
Chris Lattnera11d6172009-01-19 07:46:45 +0000147 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000148 return; // FIXME: Emit a warning?
149
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000150 L = SMgr.getInstantiationLoc(RI->getEnd());
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?
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000154 }
155 }
156
Chris Lattner2b2453a2009-01-17 06:22:33 +0000157 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000158 return; // FIXME: Emit a warning?
159
160 // Create a new rewriter to generate HTML.
Chris Lattner2c78b872009-04-14 23:22:57 +0000161 Rewriter R(const_cast<SourceManager&>(SMgr), PP->getLangOptions());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000162
Ted Kremenek00086832009-03-10 02:49:29 +0000163 // Process the path.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000164 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000165 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000166
167 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
Chris Lattner409d4e72009-04-17 20:40:01 +0000168 I!=E; ++I, --n)
Chris Lattner2b2453a2009-01-17 06:22:33 +0000169 HandlePiece(R, FID, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000170
171 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000172
Chris Lattner2b2453a2009-01-17 06:22:33 +0000173 // unsigned FID = R.getSourceMgr().getMainFileID();
174 html::EscapeText(R, FID);
175 html::AddLineNumbers(R, FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000176
Ted Kremenek47abe762008-04-16 16:39:56 +0000177 // If we have a preprocessor, relex the file and syntax highlight.
178 // We might not have a preprocessor if we come from a deserialized AST file,
179 // for example.
180
Chris Lattner2b2453a2009-01-17 06:22:33 +0000181 if (PP) html::SyntaxHighlight(R, FID, *PP);
Ted Kremenek55851142008-04-22 16:15:03 +0000182
183 // FIXME: We eventually want to use PPF to create a fresh Preprocessor,
184 // once we have worked out the bugs.
185 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000186 // if (PPF) html::HighlightMacros(R, FID, *PPF);
Ted Kremenek55851142008-04-22 16:15:03 +0000187 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000188 if (PP) html::HighlightMacros(R, FID, *PP);
Ted Kremenek47abe762008-04-16 16:39:56 +0000189
Ted Kremenekb9476392008-04-02 20:44:16 +0000190 // Get the full directory name of the analyzed file.
191
Chris Lattner2b2453a2009-01-17 06:22:33 +0000192 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Ted Kremenek2e939812008-03-27 07:35:49 +0000193
Ted Kremenek7fc89572008-04-24 23:37:03 +0000194 // This is a cludge; basically we want to append either the full
195 // working directory if we have no directory information. This is
196 // a work in progress.
197
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000198 std::string DirName = "";
199
200 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
201 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
202 DirName = P.toString() + "/";
203 }
Ted Kremenekb9476392008-04-02 20:44:16 +0000204
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000205 // Add the name of the file as an <h1> tag.
206
Ted Kremenek2e939812008-03-27 07:35:49 +0000207 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000208 std::string s;
209 llvm::raw_string_ostream os(s);
Ted Kremenek2e939812008-03-27 07:35:49 +0000210
Ted Kremenek778246a2008-09-22 17:33:32 +0000211 os << "<!-- REPORTHEADER -->\n"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000212 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000213 "<tr><td class=\"rowname\">File:</td><td>"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000214 << html::EscapeText(DirName)
215 << html::EscapeText(Entry->getName())
216 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
217 "<a href=\"#EndPath\">line "
218 << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber()
219 << ", column "
220 << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber()
221 << "</a></td></tr>\n"
222 "<tr><td class=\"rowname\">Description:</td><td>"
223 << D.getDescription() << "</td></tr>\n";
Ted Kremenek072192b2008-04-30 23:47:44 +0000224
225 // Output any other meta data.
226
227 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
228 I!=E; ++I) {
229 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
230 }
231
Ted Kremenek778246a2008-09-22 17:33:32 +0000232 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
233 "<h3>Annotated Source Code</h3>\n";
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000234
Chris Lattner2b2453a2009-01-17 06:22:33 +0000235 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000236 }
237
Ted Kremenekb9476392008-04-02 20:44:16 +0000238 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000239
240 const std::string& BugDesc = D.getDescription();
241
242 if (!BugDesc.empty()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000243 std::string s;
244 llvm::raw_string_ostream os(s);
Ted Kremenek86b43812008-04-02 18:03:20 +0000245 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000246 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000247 }
248
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000249 const std::string& BugType = D.getBugType();
250 if (!BugType.empty()) {
251 std::string s;
252 llvm::raw_string_ostream os(s);
253 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
254 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
255 }
256
Ted Kremenek8c036c72008-09-20 04:23:38 +0000257 const std::string& BugCategory = D.getCategory();
258
259 if (!BugCategory.empty()) {
260 std::string s;
261 llvm::raw_string_ostream os(s);
262 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000263 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek8c036c72008-09-20 04:23:38 +0000264 }
265
Ted Kremenekb9476392008-04-02 20:44:16 +0000266 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000267 std::string s;
268 llvm::raw_string_ostream os(s);
Ted Kremenek7fc89572008-04-24 23:37:03 +0000269 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000270 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000271 }
272
273 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000274 std::string s;
275 llvm::raw_string_ostream os(s);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000276 os << "\n<!-- BUGLINE "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000277 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
278 << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000279 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000280 }
281
282 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000283 std::string s;
284 llvm::raw_string_ostream os(s);
Ted Kremenekb9476392008-04-02 20:44:16 +0000285 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000286 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000287 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000288
289 // Add CSS, header, and footer.
290
Chris Lattner2b2453a2009-01-17 06:22:33 +0000291 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000292
293 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000294 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000295
296 if (!Buf) {
297 llvm::cerr << "warning: no diagnostics generated for main file.\n";
298 return;
299 }
300
301 // Create the stream to write out the HTML.
302 std::ofstream os;
303
304 {
305 // Create a path for the target HTML file.
306 llvm::sys::Path F(FilePrefix);
307 F.makeUnique(false, NULL);
308
309 // Rename the file with an HTML extension.
310 llvm::sys::Path H(F);
311 H.appendSuffix("html");
312 F.renamePathOnDisk(H, NULL);
313
314 os.open(H.toString().c_str());
315
316 if (!os) {
317 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
318 return;
319 }
320 }
321
322 // Emit the HTML to disk.
323
Ted Kremenek7414dc02008-04-20 01:02:33 +0000324 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000325 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000326}
327
Chris Lattner2b2453a2009-01-17 06:22:33 +0000328void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000329 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000330 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000331
332 // For now, just draw a box above the line in question, and emit the
333 // warning.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000334 FullSourceLoc Pos = P.getLocation().asLocation();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000335
336 if (!Pos.isValid())
337 return;
338
Chris Lattner2b2453a2009-01-17 06:22:33 +0000339 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000340 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
341 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000342
Chris Lattner7da5aea2009-02-04 00:55:58 +0000343 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000344 return;
345
Chris Lattner7da5aea2009-02-04 00:55:58 +0000346 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000347 const char* FileStart = Buf->getBufferStart();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000348
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000349 // Compute the column number. Rewind from the current position to the start
350 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000351 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
352 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000353 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000354
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000355 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000356 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000357 const char* FileEnd = Buf->getBufferEnd();
358 while (*LineEnd != '\n' && LineEnd != FileEnd)
359 ++LineEnd;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000360
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000361 // Compute the margin offset by counting tabs and non-tabs.
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000362 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000363 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000364 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000365
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000366 // Create the html for the message.
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000367
368 const char *Kind = 0;
369 switch (P.getKind()) {
370 case PathDiagnosticPiece::Event: Kind = "Event"; break;
371 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
372 // Setting Kind to "Control" is intentional.
373 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
374 }
375
376 std::string sbuf;
377 llvm::raw_string_ostream os(sbuf);
378
379 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
380
381 if (num == max)
382 os << "EndPath";
383 else
384 os << "Path" << num;
385
386 os << "\" class=\"msg";
387 if (Kind)
388 os << " msg" << Kind;
389 os << "\" style=\"margin-left:" << PosNo << "ex";
390
391 // Output a maximum size.
392 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000393 // Get the string and determining its maximum substring.
394 const std::string& Msg = P.getString();
395 unsigned max_token = 0;
396 unsigned cnt = 0;
397 unsigned len = Msg.size();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000398
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000399 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
400 switch (*I) {
401 default:
402 ++cnt;
Ted Kremenek00086832009-03-10 02:49:29 +0000403 continue;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000404 case ' ':
405 case '\t':
406 case '\n':
407 if (cnt > max_token) max_token = cnt;
408 cnt = 0;
409 }
410
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000411 if (cnt > max_token)
412 max_token = cnt;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000413
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000414 // Determine the approximate size of the message bubble in em.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000415 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000416 const unsigned max_line = 120;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000417
418 if (max_token >= max_line)
419 em = max_token / 2;
420 else {
421 unsigned characters = max_line;
422 unsigned lines = len / max_line;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000423
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000424 if (lines > 0) {
425 for (; characters > max_token; --characters)
426 if (len / characters > lines) {
427 ++characters;
428 break;
429 }
430 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000431
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000432 em = characters / 2;
433 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000434
435 if (em < max_line/2)
436 os << "; max-width:" << em << "em";
437 }
438 else
439 os << "; max-width:100em";
440
441 os << "\">";
442
443 if (max > 1) {
444 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
445 os << "<div class=\"PathIndex";
446 if (Kind) os << " PathIndex" << Kind;
447 os << "\">" << num << "</div>";
448 os << "</td><td>";
449 }
450
451 if (const PathDiagnosticMacroPiece *MP =
452 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
453
454 os << "Within the expansion of the macro '";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000455
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000456 // Get the name of the macro by relexing it.
457 {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000458 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000459 assert(L.isFileID());
460 std::pair<const char*, const char*> BufferInfo = L.getBufferData();
461 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.first;
462 Lexer rawLexer(L, PP->getLangOptions(), BufferInfo.first,
463 MacroName, BufferInfo.second);
464
465 Token TheTok;
466 rawLexer.LexFromRawLexer(TheTok);
467 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
468 os << MacroName[i];
Ted Kremenek80bae762009-03-02 23:05:40 +0000469 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000470
471 os << "':\n";
Ted Kremenek80bae762009-03-02 23:05:40 +0000472
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000473 if (max > 1)
Ted Kremenek80bae762009-03-02 23:05:40 +0000474 os << "</td></tr></table>";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000475
476 // Within a macro piece. Write out each event.
477 ProcessMacroPiece(os, *MP, 0);
478 }
479 else {
480 os << html::EscapeText(P.getString());
Ted Kremenek80bae762009-03-02 23:05:40 +0000481
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000482 if (max > 1)
483 os << "</td></tr></table>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000484 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000485
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000486 os << "</div></td></tr>";
487
488 // Insert the new html.
489 unsigned DisplayPos = LineEnd - FileStart;
490 SourceLocation Loc =
491 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
492
493 R.InsertStrBefore(Loc, os.str());
494
495 // Now highlight the ranges.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000496 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
497 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000498 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000499
500#if 0
501 // If there is a code insertion hint, insert that code.
502 // FIXME: This code is disabled because it seems to mangle the HTML
503 // output. I'm leaving it here because it's generally the right idea,
504 // but needs some help from someone more familiar with the rewriter.
505 for (const CodeModificationHint *Hint = P.code_modifications_begin(),
506 *HintEnd = P.code_modifications_end();
507 Hint != HintEnd; ++Hint) {
508 if (Hint->RemoveRange.isValid()) {
509 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
510 "<span class=\"CodeRemovalHint\">", "</span>");
511 }
512 if (Hint->InsertionLoc.isValid()) {
513 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
514 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
515 + "</span>";
516 R.InsertStrBefore(Hint->InsertionLoc, EscapedCode);
517 }
518 }
519#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000520}
521
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000522static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
523 llvm::SmallVector<char, 10> buf;
524
525 do {
526 unsigned x = n % ('z' - 'a');
527 buf.push_back('a' + x);
528 n = n / ('z' - 'a');
529 } while (n);
530
531 assert(!buf.empty());
532
533 for (llvm::SmallVectorImpl<char>::reverse_iterator I=buf.rbegin(),
534 E=buf.rend(); I!=E; ++I)
535 os << *I;
536}
537
538unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
539 const PathDiagnosticMacroPiece& P,
540 unsigned num) {
541
542 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
543 I!=E; ++I) {
544
545 if (const PathDiagnosticMacroPiece *MP =
546 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
547 num = ProcessMacroPiece(os, *MP, num);
548 continue;
549 }
550
551 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
552 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
553 "margin-left:5px\">"
554 "<table class=\"msgT\"><tr>"
555 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
556 EmitAlphaCounter(os, num++);
557 os << "</div></td><td valign=\"top\">"
558 << html::EscapeText(EP->getString())
559 << "</td></tr></table></div>\n";
560 }
561 }
562
563 return num;
564}
565
Chris Lattner2b2453a2009-01-17 06:22:33 +0000566void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000567 SourceRange Range,
568 const char *HighlightStart,
569 const char *HighlightEnd) {
Chris Lattner2c78b872009-04-14 23:22:57 +0000570 SourceManager &SM = R.getSourceMgr();
571 const LangOptions &LangOpts = R.getLangOpts();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000572
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000573 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000574 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000575
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000576 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000577 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000578
579 if (EndLineNo < StartLineNo)
580 return;
581
Chris Lattnera11d6172009-01-19 07:46:45 +0000582 if (SM.getFileID(InstantiationStart) != BugFileID ||
583 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000584 return;
585
586 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000587 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000588 unsigned OldEndColNo = EndColNo;
589
590 if (EndColNo) {
591 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +0000592 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000593 }
594
595 // Highlight the range. Make the span tag the outermost tag for the
596 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000597
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000598 SourceLocation E =
599 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000600
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000601 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000602}