blob: 3e3571c5129348c793e931be611186ccec574f20 [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);
163 std::string DirName(Entry->getDir()->getName());
Ted Kremenek2e939812008-03-27 07:35:49 +0000164
Ted Kremenek7fc89572008-04-24 23:37:03 +0000165 // This is a cludge; basically we want to append either the full
166 // working directory if we have no directory information. This is
167 // a work in progress.
168
Ted Kremenekb9476392008-04-02 20:44:16 +0000169 if (DirName == ".")
170 DirName = llvm::sys::Path::GetCurrentDirectory().toString();
Ted Kremenek7fc89572008-04-24 23:37:03 +0000171 else if (llvm::sys::Path(Entry->getName()).isAbsolute())
172 DirName = "";
Ted Kremenekb9476392008-04-02 20:44:16 +0000173
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000174 // Add the name of the file as an <h1> tag.
175
Ted Kremenek2e939812008-03-27 07:35:49 +0000176 {
177 std::ostringstream os;
Ted Kremenek2e939812008-03-27 07:35:49 +0000178
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000179 os << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
180 "<tr><td class=\"rowname\">File:</td><td>"
181 << html::EscapeText(DirName)
182 << html::EscapeText(Entry->getName())
183 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
184 "<a href=\"#EndPath\">line "
185 << (*D.rbegin()).getLocation().getLogicalLineNumber()
186 << ", column "
187 << (*D.rbegin()).getLocation().getLogicalColumnNumber()
188 << "</a></td></tr>\n"
189 "<tr><td class=\"rowname\">Description:</td><td>"
190 << D.getDescription()
191 << "</td></tr>\n</table>\n"
192 "<h3>Annotated Source Code</h3>\n";
193
Ted Kremenek2e939812008-03-27 07:35:49 +0000194 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000195 }
196
Ted Kremenekb9476392008-04-02 20:44:16 +0000197 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000198
199 const std::string& BugDesc = D.getDescription();
200
201 if (!BugDesc.empty()) {
202 std::ostringstream os;
Ted Kremenek86b43812008-04-02 18:03:20 +0000203 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Ted Kremenek07615402008-04-02 07:04:46 +0000204 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000205 }
206
207 {
208 std::ostringstream os;
Ted Kremenek7fc89572008-04-24 23:37:03 +0000209 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenekb9476392008-04-02 20:44:16 +0000210 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
211 }
212
213 {
214 std::ostringstream os;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000215 os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber()
Ted Kremenekb9476392008-04-02 20:44:16 +0000216 << " -->\n";
217 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
218 }
219
220 {
221 std::ostringstream os;
222 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
223 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
224 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000225
226 // Add CSS, header, and footer.
227
228 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
229
230 // Get the rewrite buffer.
231 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
232
233 if (!Buf) {
234 llvm::cerr << "warning: no diagnostics generated for main file.\n";
235 return;
236 }
237
238 // Create the stream to write out the HTML.
239 std::ofstream os;
240
241 {
242 // Create a path for the target HTML file.
243 llvm::sys::Path F(FilePrefix);
244 F.makeUnique(false, NULL);
245
246 // Rename the file with an HTML extension.
247 llvm::sys::Path H(F);
248 H.appendSuffix("html");
249 F.renamePathOnDisk(H, NULL);
250
251 os.open(H.toString().c_str());
252
253 if (!os) {
254 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
255 return;
256 }
257 }
258
259 // Emit the HTML to disk.
260
Ted Kremenek7414dc02008-04-20 01:02:33 +0000261 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000262 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000263}
264
265void HTMLDiagnostics::HandlePiece(Rewriter& R,
266 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000267 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000268
269 // For now, just draw a box above the line in question, and emit the
270 // warning.
271
272 FullSourceLoc Pos = P.getLocation();
273
274 if (!Pos.isValid())
275 return;
276
277 SourceManager& SM = R.getSourceMgr();
278 FullSourceLoc LPos = Pos.getLogicalLoc();
Ted Kremenek5dd00412008-04-14 21:06:04 +0000279 unsigned FileID = SM.getCanonicalFileID(LPos.getLocation());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000280
281 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
282
Ted Kremenek5dd00412008-04-14 21:06:04 +0000283 if (!SM.isFromMainFile(LPos.getLocation()))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000284 return;
285
286 // Compute the column number. Rewind from the current position to the start
287 // of the line.
288
289 unsigned ColNo = LPos.getColumnNumber();
290 const char *TokLogicalPtr = LPos.getCharacterData();
291 const char *LineStart = TokLogicalPtr-ColNo;
292
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000293 // Compute the margin offset by counting tabs and non-tabs.
294
295 unsigned PosNo = 0;
296
297 for (const char* c = LineStart; c != TokLogicalPtr; ++c)
Ted Kremenek8fb00162008-03-31 23:14:05 +0000298 PosNo += *c == '\t' ? 4 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000299
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000300 // Create the html for the message.
301
302 std::ostringstream os;
303
304 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
Ted Kremenek33bd9422008-03-31 23:30:12 +0000305 << "<div id=\"";
306
307 if (num == max)
308 os << "EndPath";
309 else
310 os << "Path" << num;
311
312 os << "\" class=\"msg\" style=\"margin-left:"
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000313 << PosNo << "ex\">";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000314
Ted Kremenek718ceb12008-04-02 21:04:20 +0000315 if (max > 1)
316 os << "<span class=\"PathIndex\">[" << num << "]</span> ";
317
Ted Kremenek053ef592008-03-27 17:15:29 +0000318 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000319
320 // Insert the new html.
321
322 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
323 const char* FileStart = Buf->getBufferStart();
324
325 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart),
326 os.str());
327
328 // Now highlight the ranges.
329
330 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
331 I != E; ++I)
Ted Kremenek5dd00412008-04-14 21:06:04 +0000332 HighlightRange(R, *I);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000333}
334
Ted Kremenek5dd00412008-04-14 21:06:04 +0000335void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000336
Ted Kremenek5dd00412008-04-14 21:06:04 +0000337 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000338
Ted Kremenek5dd00412008-04-14 21:06:04 +0000339 SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin());
340 unsigned StartLineNo = SM.getLineNumber(LogicalStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000341
Ted Kremenek5dd00412008-04-14 21:06:04 +0000342 SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd());
343 unsigned EndLineNo = SM.getLineNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000344
345 if (EndLineNo < StartLineNo)
346 return;
347
Ted Kremenek5dd00412008-04-14 21:06:04 +0000348 if (!SM.isFromMainFile(LogicalStart) ||
349 !SM.isFromMainFile(LogicalEnd))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000350 return;
351
352 // Compute the column number of the end.
Ted Kremenek5dd00412008-04-14 21:06:04 +0000353 unsigned EndColNo = SM.getColumnNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000354 unsigned OldEndColNo = EndColNo;
355
356 if (EndColNo) {
357 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000358 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000359 }
360
361 // Highlight the range. Make the span tag the outermost tag for the
362 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000363
Ted Kremenek12fc0142008-04-18 05:01:33 +0000364 SourceLocation E = LogicalEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000365
366 html::HighlightRange(R, LogicalStart, E,
367 "<span class=\"mrange\">", "</span>");
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000368}