blob: 5fab2679563036ad1b8332f44b28d59da6f9149d [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"
15#include "clang/Basic/SourceManager.h"
Ted Kremenek2e939812008-03-27 07:35:49 +000016#include "clang/Basic/FileManager.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000017#include "clang/AST/ASTContext.h"
18#include "clang/Analysis/PathDiagnostic.h"
19#include "clang/Rewrite/Rewriter.h"
20#include "clang/Rewrite/HTMLRewrite.h"
21#include "clang/Lex/Lexer.h"
22#include "llvm/Support/Compiler.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/Streams.h"
25#include "llvm/System/Path.h"
26#include <fstream>
27#include <sstream>
28
29using 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 Kremenek339b9c22008-04-17 22:31:54 +000041 PreprocessorFactory* PPF;
Ted Kremenek55851142008-04-22 16:15:03 +000042 std::vector<const PathDiagnostic*> BatchedDiags;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000043public:
Ted Kremenek339b9c22008-04-17 22:31:54 +000044 HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
45 PreprocessorFactory* ppf);
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
Ted Kremenek55851142008-04-22 16:15:03 +000049 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000050
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +000051 void HandlePiece(Rewriter& R, unsigned BugFileID,
52 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Ted Kremenek33bd9422008-03-31 23:30:12 +000053
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +000054 void HighlightRange(Rewriter& R, unsigned BugFileID, SourceRange Range);
Ted Kremenek55851142008-04-22 16:15:03 +000055
56 void ReportDiag(const PathDiagnostic& D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000057};
58
59} // end anonymous namespace
60
Ted Kremenek339b9c22008-04-17 22:31:54 +000061HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
62 PreprocessorFactory* ppf)
Ted Kremenek47abe762008-04-16 16:39:56 +000063 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenek339b9c22008-04-17 22:31:54 +000064 PP(pp), PPF(ppf) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000065
66 // All html files begin with "report"
67 FilePrefix.appendComponent("report");
68}
69
70PathDiagnosticClient*
Ted Kremenek339b9c22008-04-17 22:31:54 +000071clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
72 PreprocessorFactory* PPF) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000073
Ted Kremenek339b9c22008-04-17 22:31:54 +000074 return new HTMLDiagnostics(prefix, PP, PPF);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000075}
76
77//===----------------------------------------------------------------------===//
78// Report processing.
79//===----------------------------------------------------------------------===//
80
Ted Kremenek55851142008-04-22 16:15:03 +000081void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
82 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +000083 return;
84
Ted Kremenek55851142008-04-22 16:15:03 +000085 if (D->empty()) {
86 delete D;
87 return;
88 }
89
90 BatchedDiags.push_back(D);
91}
92
93HTMLDiagnostics::~HTMLDiagnostics() {
94
95 while (!BatchedDiags.empty()) {
96 const PathDiagnostic* D = BatchedDiags.back();
97 BatchedDiags.pop_back();
98 ReportDiag(*D);
99 delete D;
100 }
101}
102
103void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) {
104
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000105 // Create the HTML directory if it is missing.
106
107 if (!createdDir) {
108 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000109 std::string ErrorMsg;
110 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000111
112 if (!Directory.isDirectory()) {
113 llvm::cerr << "warning: could not create directory '"
Ted Kremenek344f7e32008-04-03 17:55:57 +0000114 << Directory.toString() << "'\n"
115 << "reason: " << ErrorMsg << '\n';
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000116
117 noDir = true;
118
119 return;
120 }
121 }
122
123 if (noDir)
124 return;
125
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000126 SourceManager& SMgr = D.begin()->getLocation().getManager();
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000127
128 unsigned FileID = 0;
129 bool FileIDInitialized = false;
130
131 // Verify that the entire path is from the same FileID.
132 for (PathDiagnostic::const_iterator I=D.begin(), E=D.end(); I != E; ++I) {
133
134 FullSourceLoc L = I->getLocation();
135
136 if (!L.isFileID())
137 return; // FIXME: Emit a warning?
138
139 if (!FileIDInitialized) {
140 FileID = L.getCanonicalFileID();
141 FileIDInitialized = true;
142 }
143 else if (L.getCanonicalFileID() != FileID)
144 return; // FIXME: Emit a warning?
145
146 // Check the source ranges.
147 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
148 RE=I->ranges_end(); RI!=RE; ++RI) {
149
150 SourceLocation L = RI->getBegin();
151
152 if (!L.isFileID())
153 return; // FIXME: Emit a warning?
154
155 if (SMgr.getCanonicalFileID(L) != FileID)
156 return; // FIXME: Emit a warning?
157
158 L = RI->getEnd();
159
160 if (!L.isFileID())
161 return; // FIXME: Emit a warning?
162
163 if (SMgr.getCanonicalFileID(L) != FileID)
164 return; // FIXME: Emit a warning?
165 }
166 }
167
168 if (!FileIDInitialized)
169 return; // FIXME: Emit a warning?
170
171 // Create a new rewriter to generate HTML.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000172 Rewriter R(SMgr);
173
174 // Process the path.
175
176 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000177 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000178
179 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
180 I!=E; ++I, --n) {
181
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000182 HandlePiece(R, FileID, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000183 }
184
185 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000186
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000187 // unsigned FileID = R.getSourceMgr().getMainFileID();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000188 html::EscapeText(R, FileID);
189 html::AddLineNumbers(R, FileID);
190
Ted Kremenek47abe762008-04-16 16:39:56 +0000191 // If we have a preprocessor, relex the file and syntax highlight.
192 // We might not have a preprocessor if we come from a deserialized AST file,
193 // for example.
194
Ted Kremenek339b9c22008-04-17 22:31:54 +0000195 if (PP) html::SyntaxHighlight(R, FileID, *PP);
Ted Kremenek55851142008-04-22 16:15:03 +0000196
197 // FIXME: We eventually want to use PPF to create a fresh Preprocessor,
198 // once we have worked out the bugs.
199 //
200 // if (PPF) html::HighlightMacros(R, FileID, *PPF);
201 //
202 if (PP) html::HighlightMacros(R, FileID, *PP);
Ted Kremenek47abe762008-04-16 16:39:56 +0000203
Ted Kremenekb9476392008-04-02 20:44:16 +0000204 // Get the full directory name of the analyzed file.
205
206 const FileEntry* Entry = SMgr.getFileEntryForID(FileID);
Ted Kremenek2e939812008-03-27 07:35:49 +0000207
Ted Kremenek7fc89572008-04-24 23:37:03 +0000208 // This is a cludge; basically we want to append either the full
209 // working directory if we have no directory information. This is
210 // a work in progress.
211
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000212 std::string DirName = "";
213
214 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
215 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
216 DirName = P.toString() + "/";
217 }
Ted Kremenekb9476392008-04-02 20:44:16 +0000218
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000219 // Add the name of the file as an <h1> tag.
220
Ted Kremenek2e939812008-03-27 07:35:49 +0000221 {
222 std::ostringstream os;
Ted Kremenek2e939812008-03-27 07:35:49 +0000223
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000224 os << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
225 "<tr><td class=\"rowname\">File:</td><td>"
226 << html::EscapeText(DirName)
227 << html::EscapeText(Entry->getName())
228 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
229 "<a href=\"#EndPath\">line "
230 << (*D.rbegin()).getLocation().getLogicalLineNumber()
231 << ", column "
232 << (*D.rbegin()).getLocation().getLogicalColumnNumber()
233 << "</a></td></tr>\n"
234 "<tr><td class=\"rowname\">Description:</td><td>"
Ted Kremenek072192b2008-04-30 23:47:44 +0000235 << D.getDescription() << "</td></tr>\n";
236
237 // Output any other meta data.
238
239 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
240 I!=E; ++I) {
241 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
242 }
243
244 os << "</table>\n<h3>Annotated Source Code</h3>\n";
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000245
Ted Kremenek2e939812008-03-27 07:35:49 +0000246 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000247 }
248
Ted Kremenekb9476392008-04-02 20:44:16 +0000249 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000250
251 const std::string& BugDesc = D.getDescription();
252
253 if (!BugDesc.empty()) {
254 std::ostringstream os;
Ted Kremenek86b43812008-04-02 18:03:20 +0000255 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Ted Kremenek07615402008-04-02 07:04:46 +0000256 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000257 }
258
259 {
260 std::ostringstream os;
Ted Kremenek7fc89572008-04-24 23:37:03 +0000261 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenekb9476392008-04-02 20:44:16 +0000262 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
263 }
264
265 {
266 std::ostringstream os;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000267 os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber()
Ted Kremenekb9476392008-04-02 20:44:16 +0000268 << " -->\n";
269 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
270 }
271
272 {
273 std::ostringstream os;
274 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
275 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
276 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000277
278 // Add CSS, header, and footer.
279
Ted Kremenekf6f593f2008-07-07 18:31:05 +0000280 html::AddHeaderFooterInternalBuiltinCSS(R, FileID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000281
282 // Get the rewrite buffer.
283 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
284
285 if (!Buf) {
286 llvm::cerr << "warning: no diagnostics generated for main file.\n";
287 return;
288 }
289
290 // Create the stream to write out the HTML.
291 std::ofstream os;
292
293 {
294 // Create a path for the target HTML file.
295 llvm::sys::Path F(FilePrefix);
296 F.makeUnique(false, NULL);
297
298 // Rename the file with an HTML extension.
299 llvm::sys::Path H(F);
300 H.appendSuffix("html");
301 F.renamePathOnDisk(H, NULL);
302
303 os.open(H.toString().c_str());
304
305 if (!os) {
306 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
307 return;
308 }
309 }
310
311 // Emit the HTML to disk.
312
Ted Kremenek7414dc02008-04-20 01:02:33 +0000313 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000314 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000315}
316
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000317void HTMLDiagnostics::HandlePiece(Rewriter& R, unsigned BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000318 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000319 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000320
321 // For now, just draw a box above the line in question, and emit the
322 // warning.
323
324 FullSourceLoc Pos = P.getLocation();
325
326 if (!Pos.isValid())
327 return;
328
329 SourceManager& SM = R.getSourceMgr();
330 FullSourceLoc LPos = Pos.getLogicalLoc();
Ted Kremenek5dd00412008-04-14 21:06:04 +0000331 unsigned FileID = SM.getCanonicalFileID(LPos.getLocation());
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000332
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000333 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
334
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000335 if (LPos.getCanonicalFileID() != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000336 return;
337
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000338 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
339 const char* FileStart = Buf->getBufferStart();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000340
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000341 // Compute the column number. Rewind from the current position to the start
342 // of the line.
343
344 unsigned ColNo = LPos.getColumnNumber();
345 const char *TokLogicalPtr = LPos.getCharacterData();
346 const char *LineStart = TokLogicalPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000347
348 // Only compute LineEnd if we display below a line.
349 const char *LineEnd = TokLogicalPtr;
350
351 if (P.getDisplayHint() == PathDiagnosticPiece::Below) {
352 const char* FileEnd = Buf->getBufferEnd();
353
354 while (*LineEnd != '\n' && LineEnd != FileEnd)
355 ++LineEnd;
356 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000357
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000358 // Compute the margin offset by counting tabs and non-tabs.
359
360 unsigned PosNo = 0;
361
362 for (const char* c = LineStart; c != TokLogicalPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000363 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000364
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000365 // Create the html for the message.
366
367 std::ostringstream os;
368
369 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
Ted Kremenek33bd9422008-03-31 23:30:12 +0000370 << "<div id=\"";
371
372 if (num == max)
373 os << "EndPath";
374 else
375 os << "Path" << num;
376
377 os << "\" class=\"msg\" style=\"margin-left:"
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000378 << PosNo << "ex\">";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000379
Ted Kremenek718ceb12008-04-02 21:04:20 +0000380 if (max > 1)
381 os << "<span class=\"PathIndex\">[" << num << "]</span> ";
382
Ted Kremenek053ef592008-03-27 17:15:29 +0000383 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000384
385 // Insert the new html.
386
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000387 unsigned DisplayPos = 0;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000388
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000389 switch (P.getDisplayHint()) {
390 case PathDiagnosticPiece::Above:
391 DisplayPos = LineStart - FileStart;
392 break;
393 case PathDiagnosticPiece::Below:
394 DisplayPos = LineEnd - FileStart;
395 break;
396 default:
397 assert (false && "Unhandled hint.");
398 }
399
400 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, DisplayPos), os.str());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000401
402 // Now highlight the ranges.
403
404 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
405 I != E; ++I)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000406 HighlightRange(R, FileID, *I);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000407}
408
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000409void HTMLDiagnostics::HighlightRange(Rewriter& R, unsigned BugFileID,
410 SourceRange Range) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000411
Ted Kremenek5dd00412008-04-14 21:06:04 +0000412 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000413
Ted Kremenek5dd00412008-04-14 21:06:04 +0000414 SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin());
415 unsigned StartLineNo = SM.getLineNumber(LogicalStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000416
Ted Kremenek5dd00412008-04-14 21:06:04 +0000417 SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd());
418 unsigned EndLineNo = SM.getLineNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000419
420 if (EndLineNo < StartLineNo)
421 return;
422
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000423 if (!SM.getCanonicalFileID(LogicalStart) != BugFileID ||
424 !SM.getCanonicalFileID(LogicalEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000425 return;
426
427 // Compute the column number of the end.
Ted Kremenek5dd00412008-04-14 21:06:04 +0000428 unsigned EndColNo = SM.getColumnNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000429 unsigned OldEndColNo = EndColNo;
430
431 if (EndColNo) {
432 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000433 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000434 }
435
436 // Highlight the range. Make the span tag the outermost tag for the
437 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000438
Ted Kremenek12fc0142008-04-18 05:01:33 +0000439 SourceLocation E = LogicalEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000440
441 html::HighlightRange(R, LogicalStart, E,
442 "<span class=\"mrange\">", "</span>");
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000443}