blob: cb333b978c159d47a26deee85ac3d2cca1b063b8 [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>"
Ted Kremenek072192b2008-04-30 23:47:44 +0000190 << D.getDescription() << "</td></tr>\n";
191
192 // Output any other meta data.
193
194 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
195 I!=E; ++I) {
196 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
197 }
198
199 os << "</table>\n<h3>Annotated Source Code</h3>\n";
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000200
Ted Kremenek2e939812008-03-27 07:35:49 +0000201 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000202 }
203
Ted Kremenekb9476392008-04-02 20:44:16 +0000204 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000205
206 const std::string& BugDesc = D.getDescription();
207
208 if (!BugDesc.empty()) {
209 std::ostringstream os;
Ted Kremenek86b43812008-04-02 18:03:20 +0000210 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Ted Kremenek07615402008-04-02 07:04:46 +0000211 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000212 }
213
214 {
215 std::ostringstream os;
Ted Kremenek7fc89572008-04-24 23:37:03 +0000216 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenekb9476392008-04-02 20:44:16 +0000217 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
218 }
219
220 {
221 std::ostringstream os;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000222 os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber()
Ted Kremenekb9476392008-04-02 20:44:16 +0000223 << " -->\n";
224 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
225 }
226
227 {
228 std::ostringstream os;
229 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
230 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
231 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000232
233 // Add CSS, header, and footer.
234
235 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
236
237 // Get the rewrite buffer.
238 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
239
240 if (!Buf) {
241 llvm::cerr << "warning: no diagnostics generated for main file.\n";
242 return;
243 }
244
245 // Create the stream to write out the HTML.
246 std::ofstream os;
247
248 {
249 // Create a path for the target HTML file.
250 llvm::sys::Path F(FilePrefix);
251 F.makeUnique(false, NULL);
252
253 // Rename the file with an HTML extension.
254 llvm::sys::Path H(F);
255 H.appendSuffix("html");
256 F.renamePathOnDisk(H, NULL);
257
258 os.open(H.toString().c_str());
259
260 if (!os) {
261 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
262 return;
263 }
264 }
265
266 // Emit the HTML to disk.
267
Ted Kremenek7414dc02008-04-20 01:02:33 +0000268 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000269 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000270}
271
272void HTMLDiagnostics::HandlePiece(Rewriter& R,
273 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000274 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000275
276 // For now, just draw a box above the line in question, and emit the
277 // warning.
278
279 FullSourceLoc Pos = P.getLocation();
280
281 if (!Pos.isValid())
282 return;
283
284 SourceManager& SM = R.getSourceMgr();
285 FullSourceLoc LPos = Pos.getLogicalLoc();
Ted Kremenek5dd00412008-04-14 21:06:04 +0000286 unsigned FileID = SM.getCanonicalFileID(LPos.getLocation());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000287
288 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
289
Ted Kremenek5dd00412008-04-14 21:06:04 +0000290 if (!SM.isFromMainFile(LPos.getLocation()))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000291 return;
292
293 // Compute the column number. Rewind from the current position to the start
294 // of the line.
295
296 unsigned ColNo = LPos.getColumnNumber();
297 const char *TokLogicalPtr = LPos.getCharacterData();
298 const char *LineStart = TokLogicalPtr-ColNo;
299
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000300 // Compute the margin offset by counting tabs and non-tabs.
301
302 unsigned PosNo = 0;
303
304 for (const char* c = LineStart; c != TokLogicalPtr; ++c)
Ted Kremenek8fb00162008-03-31 23:14:05 +0000305 PosNo += *c == '\t' ? 4 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000306
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000307 // Create the html for the message.
308
309 std::ostringstream os;
310
311 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
Ted Kremenek33bd9422008-03-31 23:30:12 +0000312 << "<div id=\"";
313
314 if (num == max)
315 os << "EndPath";
316 else
317 os << "Path" << num;
318
319 os << "\" class=\"msg\" style=\"margin-left:"
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000320 << PosNo << "ex\">";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000321
Ted Kremenek718ceb12008-04-02 21:04:20 +0000322 if (max > 1)
323 os << "<span class=\"PathIndex\">[" << num << "]</span> ";
324
Ted Kremenek053ef592008-03-27 17:15:29 +0000325 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000326
327 // Insert the new html.
328
329 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
330 const char* FileStart = Buf->getBufferStart();
331
332 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart),
333 os.str());
334
335 // Now highlight the ranges.
336
337 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
338 I != E; ++I)
Ted Kremenek5dd00412008-04-14 21:06:04 +0000339 HighlightRange(R, *I);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000340}
341
Ted Kremenek5dd00412008-04-14 21:06:04 +0000342void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000343
Ted Kremenek5dd00412008-04-14 21:06:04 +0000344 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000345
Ted Kremenek5dd00412008-04-14 21:06:04 +0000346 SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin());
347 unsigned StartLineNo = SM.getLineNumber(LogicalStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000348
Ted Kremenek5dd00412008-04-14 21:06:04 +0000349 SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd());
350 unsigned EndLineNo = SM.getLineNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000351
352 if (EndLineNo < StartLineNo)
353 return;
354
Ted Kremenek5dd00412008-04-14 21:06:04 +0000355 if (!SM.isFromMainFile(LogicalStart) ||
356 !SM.isFromMainFile(LogicalEnd))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000357 return;
358
359 // Compute the column number of the end.
Ted Kremenek5dd00412008-04-14 21:06:04 +0000360 unsigned EndColNo = SM.getColumnNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000361 unsigned OldEndColNo = EndColNo;
362
363 if (EndColNo) {
364 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000365 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000366 }
367
368 // Highlight the range. Make the span tag the outermost tag for the
369 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000370
Ted Kremenek12fc0142008-04-18 05:01:33 +0000371 SourceLocation E = LogicalEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000372
373 html::HighlightRange(R, LogicalStart, E,
374 "<span class=\"mrange\">", "</span>");
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000375}