blob: 0675ec277da58d7ebea4066b7f1479fbcd5d9655 [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
14#include "HTMLDiagnostics.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"
23#include "llvm/Support/Compiler.h"
24#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/Streams.h"
26#include "llvm/System/Path.h"
27#include <fstream>
28#include <sstream>
29
30using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// Boilerplate.
34//===----------------------------------------------------------------------===//
35
36namespace {
37
38class VISIBILITY_HIDDEN HTMLDiagnostics : public PathDiagnosticClient {
39 llvm::sys::Path Directory, FilePrefix;
40 bool createdDir, noDir;
Ted Kremenek47abe762008-04-16 16:39:56 +000041 Preprocessor* PP;
Ted Kremenek339b9c22008-04-17 22:31:54 +000042 PreprocessorFactory* PPF;
Ted Kremenek55851142008-04-22 16:15:03 +000043 std::vector<const PathDiagnostic*> BatchedDiags;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000044public:
Ted Kremenek339b9c22008-04-17 22:31:54 +000045 HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
46 PreprocessorFactory* ppf);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000047
Ted Kremenek55851142008-04-22 16:15:03 +000048 virtual ~HTMLDiagnostics();
Ted Kremenek88f5cde2008-03-27 06:17:42 +000049
Ted Kremenek55851142008-04-22 16:15:03 +000050 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000051
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +000052 void HandlePiece(Rewriter& R, unsigned BugFileID,
53 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Ted Kremenek33bd9422008-03-31 23:30:12 +000054
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +000055 void HighlightRange(Rewriter& R, unsigned BugFileID, SourceRange Range);
Ted Kremenek55851142008-04-22 16:15:03 +000056
57 void ReportDiag(const PathDiagnostic& D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000058};
59
60} // end anonymous namespace
61
Ted Kremenek339b9c22008-04-17 22:31:54 +000062HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
63 PreprocessorFactory* ppf)
Ted Kremenek47abe762008-04-16 16:39:56 +000064 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenek339b9c22008-04-17 22:31:54 +000065 PP(pp), PPF(ppf) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000066
67 // All html files begin with "report"
68 FilePrefix.appendComponent("report");
69}
70
71PathDiagnosticClient*
Ted Kremenek339b9c22008-04-17 22:31:54 +000072clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
73 PreprocessorFactory* PPF) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000074
Ted Kremenek339b9c22008-04-17 22:31:54 +000075 return new HTMLDiagnostics(prefix, PP, PPF);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000076}
77
78//===----------------------------------------------------------------------===//
79// Report processing.
80//===----------------------------------------------------------------------===//
81
Ted Kremenek55851142008-04-22 16:15:03 +000082void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
83 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +000084 return;
85
Ted Kremenek55851142008-04-22 16:15:03 +000086 if (D->empty()) {
87 delete D;
88 return;
89 }
90
91 BatchedDiags.push_back(D);
92}
93
94HTMLDiagnostics::~HTMLDiagnostics() {
95
96 while (!BatchedDiags.empty()) {
97 const PathDiagnostic* D = BatchedDiags.back();
98 BatchedDiags.pop_back();
99 ReportDiag(*D);
100 delete D;
101 }
102}
103
104void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) {
105
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000106 // Create the HTML directory if it is missing.
107
108 if (!createdDir) {
109 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000110 std::string ErrorMsg;
111 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000112
113 if (!Directory.isDirectory()) {
114 llvm::cerr << "warning: could not create directory '"
Ted Kremenek344f7e32008-04-03 17:55:57 +0000115 << Directory.toString() << "'\n"
116 << "reason: " << ErrorMsg << '\n';
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000117
118 noDir = true;
119
120 return;
121 }
122 }
123
124 if (noDir)
125 return;
126
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000127 SourceManager& SMgr = D.begin()->getLocation().getManager();
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000128
129 unsigned FileID = 0;
130 bool FileIDInitialized = false;
131
132 // Verify that the entire path is from the same FileID.
133 for (PathDiagnostic::const_iterator I=D.begin(), E=D.end(); I != E; ++I) {
134
135 FullSourceLoc L = I->getLocation();
136
137 if (!L.isFileID())
138 return; // FIXME: Emit a warning?
139
140 if (!FileIDInitialized) {
141 FileID = L.getCanonicalFileID();
142 FileIDInitialized = true;
143 }
144 else if (L.getCanonicalFileID() != FileID)
145 return; // FIXME: Emit a warning?
146
147 // Check the source ranges.
148 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
149 RE=I->ranges_end(); RI!=RE; ++RI) {
150
151 SourceLocation L = RI->getBegin();
152
153 if (!L.isFileID())
154 return; // FIXME: Emit a warning?
155
156 if (SMgr.getCanonicalFileID(L) != FileID)
157 return; // FIXME: Emit a warning?
158
159 L = RI->getEnd();
160
161 if (!L.isFileID())
162 return; // FIXME: Emit a warning?
163
164 if (SMgr.getCanonicalFileID(L) != FileID)
165 return; // FIXME: Emit a warning?
166 }
167 }
168
169 if (!FileIDInitialized)
170 return; // FIXME: Emit a warning?
171
172 // Create a new rewriter to generate HTML.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000173 Rewriter R(SMgr);
174
175 // Process the path.
176
177 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000178 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000179
180 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
181 I!=E; ++I, --n) {
182
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000183 HandlePiece(R, FileID, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000184 }
185
186 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000187
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000188 // unsigned FileID = R.getSourceMgr().getMainFileID();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000189 html::EscapeText(R, FileID);
190 html::AddLineNumbers(R, FileID);
191
Ted Kremenek47abe762008-04-16 16:39:56 +0000192 // If we have a preprocessor, relex the file and syntax highlight.
193 // We might not have a preprocessor if we come from a deserialized AST file,
194 // for example.
195
Ted Kremenek339b9c22008-04-17 22:31:54 +0000196 if (PP) html::SyntaxHighlight(R, FileID, *PP);
Ted Kremenek55851142008-04-22 16:15:03 +0000197
198 // FIXME: We eventually want to use PPF to create a fresh Preprocessor,
199 // once we have worked out the bugs.
200 //
201 // if (PPF) html::HighlightMacros(R, FileID, *PPF);
202 //
203 if (PP) html::HighlightMacros(R, FileID, *PP);
Ted Kremenek47abe762008-04-16 16:39:56 +0000204
Ted Kremenekb9476392008-04-02 20:44:16 +0000205 // Get the full directory name of the analyzed file.
206
207 const FileEntry* Entry = SMgr.getFileEntryForID(FileID);
Ted Kremenek2e939812008-03-27 07:35:49 +0000208
Ted Kremenek7fc89572008-04-24 23:37:03 +0000209 // This is a cludge; basically we want to append either the full
210 // working directory if we have no directory information. This is
211 // a work in progress.
212
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000213 std::string DirName = "";
214
215 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
216 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
217 DirName = P.toString() + "/";
218 }
Ted Kremenekb9476392008-04-02 20:44:16 +0000219
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000220 // Add the name of the file as an <h1> tag.
221
Ted Kremenek2e939812008-03-27 07:35:49 +0000222 {
223 std::ostringstream os;
Ted Kremenek2e939812008-03-27 07:35:49 +0000224
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000225 os << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
226 "<tr><td class=\"rowname\">File:</td><td>"
227 << html::EscapeText(DirName)
228 << html::EscapeText(Entry->getName())
229 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
230 "<a href=\"#EndPath\">line "
231 << (*D.rbegin()).getLocation().getLogicalLineNumber()
232 << ", column "
233 << (*D.rbegin()).getLocation().getLogicalColumnNumber()
234 << "</a></td></tr>\n"
235 "<tr><td class=\"rowname\">Description:</td><td>"
Ted Kremenek072192b2008-04-30 23:47:44 +0000236 << D.getDescription() << "</td></tr>\n";
237
238 // Output any other meta data.
239
240 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
241 I!=E; ++I) {
242 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
243 }
244
245 os << "</table>\n<h3>Annotated Source Code</h3>\n";
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000246
Ted Kremenek2e939812008-03-27 07:35:49 +0000247 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000248 }
249
Ted Kremenekb9476392008-04-02 20:44:16 +0000250 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000251
252 const std::string& BugDesc = D.getDescription();
253
254 if (!BugDesc.empty()) {
255 std::ostringstream os;
Ted Kremenek86b43812008-04-02 18:03:20 +0000256 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Ted Kremenek07615402008-04-02 07:04:46 +0000257 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000258 }
259
260 {
261 std::ostringstream os;
Ted Kremenek7fc89572008-04-24 23:37:03 +0000262 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenekb9476392008-04-02 20:44:16 +0000263 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
264 }
265
266 {
267 std::ostringstream os;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000268 os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber()
Ted Kremenekb9476392008-04-02 20:44:16 +0000269 << " -->\n";
270 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
271 }
272
273 {
274 std::ostringstream os;
275 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
276 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
277 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000278
279 // Add CSS, header, and footer.
280
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000281 html::AddHeaderFooterInternalBuiltinCSS(R, FileID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000282
283 // Get the rewrite buffer.
284 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
285
286 if (!Buf) {
287 llvm::cerr << "warning: no diagnostics generated for main file.\n";
288 return;
289 }
290
291 // Create the stream to write out the HTML.
292 std::ofstream os;
293
294 {
295 // Create a path for the target HTML file.
296 llvm::sys::Path F(FilePrefix);
297 F.makeUnique(false, NULL);
298
299 // Rename the file with an HTML extension.
300 llvm::sys::Path H(F);
301 H.appendSuffix("html");
302 F.renamePathOnDisk(H, NULL);
303
304 os.open(H.toString().c_str());
305
306 if (!os) {
307 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
308 return;
309 }
310 }
311
312 // Emit the HTML to disk.
313
Ted Kremenek7414dc02008-04-20 01:02:33 +0000314 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000315 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000316}
317
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000318void HTMLDiagnostics::HandlePiece(Rewriter& R, unsigned BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000319 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000320 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000321
322 // For now, just draw a box above the line in question, and emit the
323 // warning.
324
325 FullSourceLoc Pos = P.getLocation();
326
327 if (!Pos.isValid())
328 return;
329
330 SourceManager& SM = R.getSourceMgr();
331 FullSourceLoc LPos = Pos.getLogicalLoc();
Ted Kremenek5dd00412008-04-14 21:06:04 +0000332 unsigned FileID = SM.getCanonicalFileID(LPos.getLocation());
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000333
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000334 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
335
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000336 if (LPos.getCanonicalFileID() != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000337 return;
338
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000339 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
340 const char* FileStart = Buf->getBufferStart();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000341
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000342 // Compute the column number. Rewind from the current position to the start
343 // of the line.
344
345 unsigned ColNo = LPos.getColumnNumber();
346 const char *TokLogicalPtr = LPos.getCharacterData();
347 const char *LineStart = TokLogicalPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000348
349 // Only compute LineEnd if we display below a line.
350 const char *LineEnd = TokLogicalPtr;
351
352 if (P.getDisplayHint() == PathDiagnosticPiece::Below) {
353 const char* FileEnd = Buf->getBufferEnd();
354
355 while (*LineEnd != '\n' && LineEnd != FileEnd)
356 ++LineEnd;
357 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000358
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000359 // Compute the margin offset by counting tabs and non-tabs.
360
361 unsigned PosNo = 0;
362
363 for (const char* c = LineStart; c != TokLogicalPtr; ++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.
367
368 std::ostringstream os;
369
370 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
Ted Kremenek33bd9422008-03-31 23:30:12 +0000371 << "<div id=\"";
372
373 if (num == max)
374 os << "EndPath";
375 else
376 os << "Path" << num;
377
378 os << "\" class=\"msg\" style=\"margin-left:"
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000379 << PosNo << "ex\">";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000380
Ted Kremenek718ceb12008-04-02 21:04:20 +0000381 if (max > 1)
382 os << "<span class=\"PathIndex\">[" << num << "]</span> ";
383
Ted Kremenek053ef592008-03-27 17:15:29 +0000384 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000385
386 // Insert the new html.
387
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000388 unsigned DisplayPos = 0;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000389
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000390 switch (P.getDisplayHint()) {
391 case PathDiagnosticPiece::Above:
392 DisplayPos = LineStart - FileStart;
393 break;
394 case PathDiagnosticPiece::Below:
395 DisplayPos = LineEnd - FileStart;
396 break;
397 default:
398 assert (false && "Unhandled hint.");
399 }
400
401 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, DisplayPos), os.str());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000402
403 // Now highlight the ranges.
404
405 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
406 I != E; ++I)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000407 HighlightRange(R, FileID, *I);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000408}
409
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000410void HTMLDiagnostics::HighlightRange(Rewriter& R, unsigned BugFileID,
411 SourceRange Range) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000412
Ted Kremenek5dd00412008-04-14 21:06:04 +0000413 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000414
Ted Kremenek5dd00412008-04-14 21:06:04 +0000415 SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin());
416 unsigned StartLineNo = SM.getLineNumber(LogicalStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000417
Ted Kremenek5dd00412008-04-14 21:06:04 +0000418 SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd());
419 unsigned EndLineNo = SM.getLineNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000420
421 if (EndLineNo < StartLineNo)
422 return;
423
Ted Kremenekae8a2552008-07-29 23:35:38 +0000424 if (SM.getCanonicalFileID(LogicalStart) != BugFileID ||
425 SM.getCanonicalFileID(LogicalEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000426 return;
427
428 // Compute the column number of the end.
Ted Kremenek5dd00412008-04-14 21:06:04 +0000429 unsigned EndColNo = SM.getColumnNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000430 unsigned OldEndColNo = EndColNo;
431
432 if (EndColNo) {
433 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000434 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000435 }
436
437 // Highlight the range. Make the span tag the outermost tag for the
438 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000439
Ted Kremenek12fc0142008-04-18 05:01:33 +0000440 SourceLocation E = LogicalEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000441
442 html::HighlightRange(R, LogicalStart, E,
443 "<span class=\"mrange\">", "</span>");
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000444}