blob: 760ef25379dc417f9e99e2007e08cfea878251bb [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 Kremenek33bd9422008-03-31 23:30:12 +000051 void HandlePiece(Rewriter& R, const PathDiagnosticPiece& P,
52 unsigned num, unsigned max);
53
Ted Kremenek5dd00412008-04-14 21:06:04 +000054 void HighlightRange(Rewriter& R, 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
126 // Create a new rewriter to generate HTML.
127 SourceManager& SMgr = D.begin()->getLocation().getManager();
128 Rewriter R(SMgr);
129
130 // Process the path.
131
132 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000133 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000134
135 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
136 I!=E; ++I, --n) {
137
Ted Kremenek33bd9422008-03-31 23:30:12 +0000138 HandlePiece(R, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000139 }
140
141 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000142
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000143 unsigned FileID = R.getSourceMgr().getMainFileID();
144 html::EscapeText(R, FileID);
145 html::AddLineNumbers(R, FileID);
146
Ted Kremenek47abe762008-04-16 16:39:56 +0000147 // If we have a preprocessor, relex the file and syntax highlight.
148 // We might not have a preprocessor if we come from a deserialized AST file,
149 // for example.
150
Ted Kremenek339b9c22008-04-17 22:31:54 +0000151 if (PP) html::SyntaxHighlight(R, FileID, *PP);
Ted Kremenek55851142008-04-22 16:15:03 +0000152
153 // FIXME: We eventually want to use PPF to create a fresh Preprocessor,
154 // once we have worked out the bugs.
155 //
156 // if (PPF) html::HighlightMacros(R, FileID, *PPF);
157 //
158 if (PP) html::HighlightMacros(R, FileID, *PP);
Ted Kremenek47abe762008-04-16 16:39:56 +0000159
Ted Kremenekb9476392008-04-02 20:44:16 +0000160 // Get the full directory name of the analyzed file.
161
162 const FileEntry* Entry = SMgr.getFileEntryForID(FileID);
Ted Kremenek2e939812008-03-27 07:35:49 +0000163
Ted Kremenek7fc89572008-04-24 23:37:03 +0000164 // This is a cludge; basically we want to append either the full
165 // working directory if we have no directory information. This is
166 // a work in progress.
167
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000168 std::string DirName = "";
169
170 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
171 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
172 DirName = P.toString() + "/";
173 }
Ted Kremenekb9476392008-04-02 20:44:16 +0000174
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000175 // Add the name of the file as an <h1> tag.
176
Ted Kremenek2e939812008-03-27 07:35:49 +0000177 {
178 std::ostringstream os;
Ted Kremenek2e939812008-03-27 07:35:49 +0000179
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000180 os << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
181 "<tr><td class=\"rowname\">File:</td><td>"
182 << html::EscapeText(DirName)
183 << html::EscapeText(Entry->getName())
184 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
185 "<a href=\"#EndPath\">line "
186 << (*D.rbegin()).getLocation().getLogicalLineNumber()
187 << ", column "
188 << (*D.rbegin()).getLocation().getLogicalColumnNumber()
189 << "</a></td></tr>\n"
190 "<tr><td class=\"rowname\">Description:</td><td>"
Ted Kremenek072192b2008-04-30 23:47:44 +0000191 << D.getDescription() << "</td></tr>\n";
192
193 // Output any other meta data.
194
195 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
196 I!=E; ++I) {
197 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
198 }
199
200 os << "</table>\n<h3>Annotated Source Code</h3>\n";
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000201
Ted Kremenek2e939812008-03-27 07:35:49 +0000202 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000203 }
204
Ted Kremenekb9476392008-04-02 20:44:16 +0000205 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000206
207 const std::string& BugDesc = D.getDescription();
208
209 if (!BugDesc.empty()) {
210 std::ostringstream os;
Ted Kremenek86b43812008-04-02 18:03:20 +0000211 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Ted Kremenek07615402008-04-02 07:04:46 +0000212 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000213 }
214
215 {
216 std::ostringstream os;
Ted Kremenek7fc89572008-04-24 23:37:03 +0000217 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenekb9476392008-04-02 20:44:16 +0000218 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
219 }
220
221 {
222 std::ostringstream os;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000223 os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber()
Ted Kremenekb9476392008-04-02 20:44:16 +0000224 << " -->\n";
225 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
226 }
227
228 {
229 std::ostringstream os;
230 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
231 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
232 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000233
234 // Add CSS, header, and footer.
235
236 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
237
238 // Get the rewrite buffer.
239 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
240
241 if (!Buf) {
242 llvm::cerr << "warning: no diagnostics generated for main file.\n";
243 return;
244 }
245
246 // Create the stream to write out the HTML.
247 std::ofstream os;
248
249 {
250 // Create a path for the target HTML file.
251 llvm::sys::Path F(FilePrefix);
252 F.makeUnique(false, NULL);
253
254 // Rename the file with an HTML extension.
255 llvm::sys::Path H(F);
256 H.appendSuffix("html");
257 F.renamePathOnDisk(H, NULL);
258
259 os.open(H.toString().c_str());
260
261 if (!os) {
262 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
263 return;
264 }
265 }
266
267 // Emit the HTML to disk.
268
Ted Kremenek7414dc02008-04-20 01:02:33 +0000269 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000270 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000271}
272
273void HTMLDiagnostics::HandlePiece(Rewriter& R,
274 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000275 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000276
277 // For now, just draw a box above the line in question, and emit the
278 // warning.
279
280 FullSourceLoc Pos = P.getLocation();
281
282 if (!Pos.isValid())
283 return;
284
285 SourceManager& SM = R.getSourceMgr();
286 FullSourceLoc LPos = Pos.getLogicalLoc();
Ted Kremenek5dd00412008-04-14 21:06:04 +0000287 unsigned FileID = SM.getCanonicalFileID(LPos.getLocation());
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000288
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000289 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
290
Ted Kremenek5dd00412008-04-14 21:06:04 +0000291 if (!SM.isFromMainFile(LPos.getLocation()))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000292 return;
293
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000294 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
295 const char* FileStart = Buf->getBufferStart();
296
297
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000298 // Compute the column number. Rewind from the current position to the start
299 // of the line.
300
301 unsigned ColNo = LPos.getColumnNumber();
302 const char *TokLogicalPtr = LPos.getCharacterData();
303 const char *LineStart = TokLogicalPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000304
305 // Only compute LineEnd if we display below a line.
306 const char *LineEnd = TokLogicalPtr;
307
308 if (P.getDisplayHint() == PathDiagnosticPiece::Below) {
309 const char* FileEnd = Buf->getBufferEnd();
310
311 while (*LineEnd != '\n' && LineEnd != FileEnd)
312 ++LineEnd;
313 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000314
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000315 // Compute the margin offset by counting tabs and non-tabs.
316
317 unsigned PosNo = 0;
318
319 for (const char* c = LineStart; c != TokLogicalPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000320 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000321
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000322 // Create the html for the message.
323
324 std::ostringstream os;
325
326 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
Ted Kremenek33bd9422008-03-31 23:30:12 +0000327 << "<div id=\"";
328
329 if (num == max)
330 os << "EndPath";
331 else
332 os << "Path" << num;
333
334 os << "\" class=\"msg\" style=\"margin-left:"
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000335 << PosNo << "ex\">";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000336
Ted Kremenek718ceb12008-04-02 21:04:20 +0000337 if (max > 1)
338 os << "<span class=\"PathIndex\">[" << num << "]</span> ";
339
Ted Kremenek053ef592008-03-27 17:15:29 +0000340 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000341
342 // Insert the new html.
343
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000344 unsigned DisplayPos = 0;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000345
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000346 switch (P.getDisplayHint()) {
347 case PathDiagnosticPiece::Above:
348 DisplayPos = LineStart - FileStart;
349 break;
350 case PathDiagnosticPiece::Below:
351 DisplayPos = LineEnd - FileStart;
352 break;
353 default:
354 assert (false && "Unhandled hint.");
355 }
356
357 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, DisplayPos), os.str());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000358
359 // Now highlight the ranges.
360
361 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
362 I != E; ++I)
Ted Kremenek5dd00412008-04-14 21:06:04 +0000363 HighlightRange(R, *I);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000364}
365
Ted Kremenek5dd00412008-04-14 21:06:04 +0000366void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000367
Ted Kremenek5dd00412008-04-14 21:06:04 +0000368 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000369
Ted Kremenek5dd00412008-04-14 21:06:04 +0000370 SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin());
371 unsigned StartLineNo = SM.getLineNumber(LogicalStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000372
Ted Kremenek5dd00412008-04-14 21:06:04 +0000373 SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd());
374 unsigned EndLineNo = SM.getLineNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000375
376 if (EndLineNo < StartLineNo)
377 return;
378
Ted Kremenek5dd00412008-04-14 21:06:04 +0000379 if (!SM.isFromMainFile(LogicalStart) ||
380 !SM.isFromMainFile(LogicalEnd))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000381 return;
382
383 // Compute the column number of the end.
Ted Kremenek5dd00412008-04-14 21:06:04 +0000384 unsigned EndColNo = SM.getColumnNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000385 unsigned OldEndColNo = EndColNo;
386
387 if (EndColNo) {
388 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000389 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000390 }
391
392 // Highlight the range. Make the span tag the outermost tag for the
393 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000394
Ted Kremenek12fc0142008-04-18 05:01:33 +0000395 SourceLocation E = LogicalEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000396
397 html::HighlightRange(R, LogicalStart, E,
398 "<span class=\"mrange\">", "</span>");
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000399}