blob: c850c05a58f7fc52c947d8878fed28776f1190cb [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 Kremenekb9476392008-04-02 20:44:16 +0000165 if (DirName == ".")
166 DirName = llvm::sys::Path::GetCurrentDirectory().toString();
167
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000168 // Add the name of the file as an <h1> tag.
169
Ted Kremenek2e939812008-03-27 07:35:49 +0000170 {
171 std::ostringstream os;
Ted Kremenek2e939812008-03-27 07:35:49 +0000172
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000173 os << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
174 "<tr><td class=\"rowname\">File:</td><td>"
175 << html::EscapeText(DirName)
176 << html::EscapeText(Entry->getName())
177 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
178 "<a href=\"#EndPath\">line "
179 << (*D.rbegin()).getLocation().getLogicalLineNumber()
180 << ", column "
181 << (*D.rbegin()).getLocation().getLogicalColumnNumber()
182 << "</a></td></tr>\n"
183 "<tr><td class=\"rowname\">Description:</td><td>"
184 << D.getDescription()
185 << "</td></tr>\n</table>\n"
186 "<h3>Annotated Source Code</h3>\n";
187
Ted Kremenek2e939812008-03-27 07:35:49 +0000188 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000189 }
190
Ted Kremenekb9476392008-04-02 20:44:16 +0000191 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000192
193 const std::string& BugDesc = D.getDescription();
194
195 if (!BugDesc.empty()) {
196 std::ostringstream os;
Ted Kremenek86b43812008-04-02 18:03:20 +0000197 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Ted Kremenek07615402008-04-02 07:04:46 +0000198 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000199 }
200
201 {
202 std::ostringstream os;
203 os << "\n<!-- BUGFILE " << DirName << "/" << Entry->getName() << " -->\n";
204 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
205 }
206
207 {
208 std::ostringstream os;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000209 os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber()
Ted Kremenekb9476392008-04-02 20:44:16 +0000210 << " -->\n";
211 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
212 }
213
214 {
215 std::ostringstream os;
216 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
217 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
218 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000219
220 // Add CSS, header, and footer.
221
222 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
223
224 // Get the rewrite buffer.
225 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
226
227 if (!Buf) {
228 llvm::cerr << "warning: no diagnostics generated for main file.\n";
229 return;
230 }
231
232 // Create the stream to write out the HTML.
233 std::ofstream os;
234
235 {
236 // Create a path for the target HTML file.
237 llvm::sys::Path F(FilePrefix);
238 F.makeUnique(false, NULL);
239
240 // Rename the file with an HTML extension.
241 llvm::sys::Path H(F);
242 H.appendSuffix("html");
243 F.renamePathOnDisk(H, NULL);
244
245 os.open(H.toString().c_str());
246
247 if (!os) {
248 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
249 return;
250 }
251 }
252
253 // Emit the HTML to disk.
254
Ted Kremenek7414dc02008-04-20 01:02:33 +0000255 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000256 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000257}
258
259void HTMLDiagnostics::HandlePiece(Rewriter& R,
260 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000261 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000262
263 // For now, just draw a box above the line in question, and emit the
264 // warning.
265
266 FullSourceLoc Pos = P.getLocation();
267
268 if (!Pos.isValid())
269 return;
270
271 SourceManager& SM = R.getSourceMgr();
272 FullSourceLoc LPos = Pos.getLogicalLoc();
Ted Kremenek5dd00412008-04-14 21:06:04 +0000273 unsigned FileID = SM.getCanonicalFileID(LPos.getLocation());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000274
275 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
276
Ted Kremenek5dd00412008-04-14 21:06:04 +0000277 if (!SM.isFromMainFile(LPos.getLocation()))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000278 return;
279
280 // Compute the column number. Rewind from the current position to the start
281 // of the line.
282
283 unsigned ColNo = LPos.getColumnNumber();
284 const char *TokLogicalPtr = LPos.getCharacterData();
285 const char *LineStart = TokLogicalPtr-ColNo;
286
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000287 // Compute the margin offset by counting tabs and non-tabs.
288
289 unsigned PosNo = 0;
290
291 for (const char* c = LineStart; c != TokLogicalPtr; ++c)
Ted Kremenek8fb00162008-03-31 23:14:05 +0000292 PosNo += *c == '\t' ? 4 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000293
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000294 // Create the html for the message.
295
296 std::ostringstream os;
297
298 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
Ted Kremenek33bd9422008-03-31 23:30:12 +0000299 << "<div id=\"";
300
301 if (num == max)
302 os << "EndPath";
303 else
304 os << "Path" << num;
305
306 os << "\" class=\"msg\" style=\"margin-left:"
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000307 << PosNo << "ex\">";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000308
Ted Kremenek718ceb12008-04-02 21:04:20 +0000309 if (max > 1)
310 os << "<span class=\"PathIndex\">[" << num << "]</span> ";
311
Ted Kremenek053ef592008-03-27 17:15:29 +0000312 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000313
314 // Insert the new html.
315
316 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
317 const char* FileStart = Buf->getBufferStart();
318
319 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart),
320 os.str());
321
322 // Now highlight the ranges.
323
324 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
325 I != E; ++I)
Ted Kremenek5dd00412008-04-14 21:06:04 +0000326 HighlightRange(R, *I);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000327}
328
Ted Kremenek5dd00412008-04-14 21:06:04 +0000329void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000330
Ted Kremenek5dd00412008-04-14 21:06:04 +0000331 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000332
Ted Kremenek5dd00412008-04-14 21:06:04 +0000333 SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin());
334 unsigned StartLineNo = SM.getLineNumber(LogicalStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000335
Ted Kremenek5dd00412008-04-14 21:06:04 +0000336 SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd());
337 unsigned EndLineNo = SM.getLineNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000338
339 if (EndLineNo < StartLineNo)
340 return;
341
Ted Kremenek5dd00412008-04-14 21:06:04 +0000342 if (!SM.isFromMainFile(LogicalStart) ||
343 !SM.isFromMainFile(LogicalEnd))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000344 return;
345
346 // Compute the column number of the end.
Ted Kremenek5dd00412008-04-14 21:06:04 +0000347 unsigned EndColNo = SM.getColumnNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000348 unsigned OldEndColNo = EndColNo;
349
350 if (EndColNo) {
351 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000352 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000353 }
354
355 // Highlight the range. Make the span tag the outermost tag for the
356 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000357
Ted Kremenek12fc0142008-04-18 05:01:33 +0000358 SourceLocation E = LogicalEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000359
360 html::HighlightRange(R, LogicalStart, E,
361 "<span class=\"mrange\">", "</span>");
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000362}