blob: d92e0d43538fc14e724b010d0ef2759fcb732c66 [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
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000273 R.InsertTextBefore(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 Kremenekb9476392008-04-02 20:44:16 +0000277 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000278 std::string s;
279 llvm::raw_string_ostream os(s);
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000280
281 const std::string& BugDesc = D.getDescription();
282 if (!BugDesc.empty())
283 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
284
285 const std::string& BugType = D.getBugType();
286 if (!BugType.empty())
287 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
288
289 const std::string& BugCategory = D.getCategory();
290 if (!BugCategory.empty())
291 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
292
Ted Kremenek7fc89572008-04-24 23:37:03 +0000293 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000294
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000295 os << "\n<!-- BUGLINE "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000296 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
297 << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000298
299 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
300
301 // Mark the end of the tags.
302 os << "\n<!-- BUGMETAEND -->\n";
303
304 // Insert the text.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000305 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000306 }
307
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000308 // Add CSS, header, and footer.
309
Chris Lattner2b2453a2009-01-17 06:22:33 +0000310 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000311
312 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000313 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000314
315 if (!Buf) {
316 llvm::cerr << "warning: no diagnostics generated for main file.\n";
317 return;
318 }
319
320 // Create the stream to write out the HTML.
321 std::ofstream os;
322
323 {
324 // Create a path for the target HTML file.
325 llvm::sys::Path F(FilePrefix);
326 F.makeUnique(false, NULL);
327
328 // Rename the file with an HTML extension.
329 llvm::sys::Path H(F);
330 H.appendSuffix("html");
331 F.renamePathOnDisk(H, NULL);
332
333 os.open(H.toString().c_str());
334
335 if (!os) {
336 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
337 return;
338 }
Ted Kremenekf7556062009-07-27 22:13:39 +0000339
340 if (FilesMade)
341 FilesMade->push_back(H.getLast());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000342 }
343
344 // Emit the HTML to disk.
Ted Kremenek7414dc02008-04-20 01:02:33 +0000345 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekf7556062009-07-27 22:13:39 +0000346 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000347}
348
Chris Lattner2b2453a2009-01-17 06:22:33 +0000349void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000350 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000351 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000352
353 // For now, just draw a box above the line in question, and emit the
354 // warning.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000355 FullSourceLoc Pos = P.getLocation().asLocation();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000356
357 if (!Pos.isValid())
358 return;
359
Chris Lattner2b2453a2009-01-17 06:22:33 +0000360 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000361 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
362 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000363
Chris Lattner7da5aea2009-02-04 00:55:58 +0000364 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000365 return;
366
Chris Lattner7da5aea2009-02-04 00:55:58 +0000367 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000368 const char* FileStart = Buf->getBufferStart();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000369
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000370 // Compute the column number. Rewind from the current position to the start
371 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000372 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
373 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000374 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000375
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000376 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000377 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000378 const char* FileEnd = Buf->getBufferEnd();
379 while (*LineEnd != '\n' && LineEnd != FileEnd)
380 ++LineEnd;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000381
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000382 // Compute the margin offset by counting tabs and non-tabs.
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000383 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000384 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000385 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000386
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000387 // Create the html for the message.
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000388
389 const char *Kind = 0;
390 switch (P.getKind()) {
391 case PathDiagnosticPiece::Event: Kind = "Event"; break;
392 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
393 // Setting Kind to "Control" is intentional.
394 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
395 }
396
397 std::string sbuf;
398 llvm::raw_string_ostream os(sbuf);
399
400 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
401
402 if (num == max)
403 os << "EndPath";
404 else
405 os << "Path" << num;
406
407 os << "\" class=\"msg";
408 if (Kind)
409 os << " msg" << Kind;
410 os << "\" style=\"margin-left:" << PosNo << "ex";
411
412 // Output a maximum size.
413 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000414 // Get the string and determining its maximum substring.
415 const std::string& Msg = P.getString();
416 unsigned max_token = 0;
417 unsigned cnt = 0;
418 unsigned len = Msg.size();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000419
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000420 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
421 switch (*I) {
422 default:
423 ++cnt;
Ted Kremenek00086832009-03-10 02:49:29 +0000424 continue;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000425 case ' ':
426 case '\t':
427 case '\n':
428 if (cnt > max_token) max_token = cnt;
429 cnt = 0;
430 }
431
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000432 if (cnt > max_token)
433 max_token = cnt;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000434
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000435 // Determine the approximate size of the message bubble in em.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000436 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000437 const unsigned max_line = 120;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000438
439 if (max_token >= max_line)
440 em = max_token / 2;
441 else {
442 unsigned characters = max_line;
443 unsigned lines = len / max_line;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000444
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000445 if (lines > 0) {
446 for (; characters > max_token; --characters)
447 if (len / characters > lines) {
448 ++characters;
449 break;
450 }
451 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000452
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000453 em = characters / 2;
454 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000455
456 if (em < max_line/2)
457 os << "; max-width:" << em << "em";
458 }
459 else
460 os << "; max-width:100em";
461
462 os << "\">";
463
464 if (max > 1) {
465 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
466 os << "<div class=\"PathIndex";
467 if (Kind) os << " PathIndex" << Kind;
468 os << "\">" << num << "</div>";
469 os << "</td><td>";
470 }
471
472 if (const PathDiagnosticMacroPiece *MP =
473 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
474
475 os << "Within the expansion of the macro '";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000476
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000477 // Get the name of the macro by relexing it.
478 {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000479 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000480 assert(L.isFileID());
481 std::pair<const char*, const char*> BufferInfo = L.getBufferData();
482 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.first;
483 Lexer rawLexer(L, PP->getLangOptions(), BufferInfo.first,
484 MacroName, BufferInfo.second);
485
486 Token TheTok;
487 rawLexer.LexFromRawLexer(TheTok);
488 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
489 os << MacroName[i];
Ted Kremenek80bae762009-03-02 23:05:40 +0000490 }
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000491
492 os << "':\n";
Ted Kremenek80bae762009-03-02 23:05:40 +0000493
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000494 if (max > 1)
Ted Kremenek80bae762009-03-02 23:05:40 +0000495 os << "</td></tr></table>";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000496
497 // Within a macro piece. Write out each event.
498 ProcessMacroPiece(os, *MP, 0);
499 }
500 else {
501 os << html::EscapeText(P.getString());
Ted Kremenek80bae762009-03-02 23:05:40 +0000502
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000503 if (max > 1)
504 os << "</td></tr></table>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000505 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000506
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000507 os << "</div></td></tr>";
508
509 // Insert the new html.
510 unsigned DisplayPos = LineEnd - FileStart;
511 SourceLocation Loc =
512 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
513
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000514 R.InsertTextBefore(Loc, os.str());
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000515
516 // Now highlight the ranges.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000517 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
518 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000519 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000520
521#if 0
522 // If there is a code insertion hint, insert that code.
523 // FIXME: This code is disabled because it seems to mangle the HTML
524 // output. I'm leaving it here because it's generally the right idea,
525 // but needs some help from someone more familiar with the rewriter.
526 for (const CodeModificationHint *Hint = P.code_modifications_begin(),
527 *HintEnd = P.code_modifications_end();
528 Hint != HintEnd; ++Hint) {
529 if (Hint->RemoveRange.isValid()) {
530 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
531 "<span class=\"CodeRemovalHint\">", "</span>");
532 }
533 if (Hint->InsertionLoc.isValid()) {
534 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
535 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
536 + "</span>";
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000537 R.InsertTextBefore(Hint->InsertionLoc, EscapedCode);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000538 }
539 }
540#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000541}
542
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000543static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
544 llvm::SmallVector<char, 10> buf;
545
546 do {
547 unsigned x = n % ('z' - 'a');
548 buf.push_back('a' + x);
549 n = n / ('z' - 'a');
550 } while (n);
551
552 assert(!buf.empty());
553
554 for (llvm::SmallVectorImpl<char>::reverse_iterator I=buf.rbegin(),
555 E=buf.rend(); I!=E; ++I)
556 os << *I;
557}
558
559unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
560 const PathDiagnosticMacroPiece& P,
561 unsigned num) {
562
563 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
564 I!=E; ++I) {
565
566 if (const PathDiagnosticMacroPiece *MP =
567 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
568 num = ProcessMacroPiece(os, *MP, num);
569 continue;
570 }
571
572 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
573 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
574 "margin-left:5px\">"
575 "<table class=\"msgT\"><tr>"
576 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
577 EmitAlphaCounter(os, num++);
578 os << "</div></td><td valign=\"top\">"
579 << html::EscapeText(EP->getString())
580 << "</td></tr></table></div>\n";
581 }
582 }
583
584 return num;
585}
586
Chris Lattner2b2453a2009-01-17 06:22:33 +0000587void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000588 SourceRange Range,
589 const char *HighlightStart,
590 const char *HighlightEnd) {
Chris Lattner2c78b872009-04-14 23:22:57 +0000591 SourceManager &SM = R.getSourceMgr();
592 const LangOptions &LangOpts = R.getLangOpts();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000593
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000594 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000595 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000596
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000597 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000598 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000599
600 if (EndLineNo < StartLineNo)
601 return;
602
Chris Lattnera11d6172009-01-19 07:46:45 +0000603 if (SM.getFileID(InstantiationStart) != BugFileID ||
604 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000605 return;
606
607 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000608 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000609 unsigned OldEndColNo = EndColNo;
610
611 if (EndColNo) {
612 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +0000613 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000614 }
615
616 // Highlight the range. Make the span tag the outermost tag for the
617 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000618
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000619 SourceLocation E =
620 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000621
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000622 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000623}