blob: 327a77f8dfde72a95d27e8c866501f986ce242fd [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;
40public:
41 HTMLDiagnostics(const std::string& prefix);
42
43 virtual ~HTMLDiagnostics() {}
44
45 virtual void HandlePathDiagnostic(const PathDiagnostic& D);
46
Ted Kremenek33bd9422008-03-31 23:30:12 +000047 void HandlePiece(Rewriter& R, const PathDiagnosticPiece& P,
48 unsigned num, unsigned max);
49
Ted Kremenek5dd00412008-04-14 21:06:04 +000050 void HighlightRange(Rewriter& R, SourceRange Range);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000051};
52
53} // end anonymous namespace
54
55HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix)
56 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false) {
57
58 // All html files begin with "report"
59 FilePrefix.appendComponent("report");
60}
61
62PathDiagnosticClient*
63clang::CreateHTMLDiagnosticClient(const std::string& prefix) {
64
65 return new HTMLDiagnostics(prefix);
66}
67
68//===----------------------------------------------------------------------===//
69// Report processing.
70//===----------------------------------------------------------------------===//
71
72void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic& D) {
73
74 if (D.empty())
75 return;
76
77 // Create the HTML directory if it is missing.
78
79 if (!createdDir) {
80 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +000081 std::string ErrorMsg;
82 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000083
84 if (!Directory.isDirectory()) {
85 llvm::cerr << "warning: could not create directory '"
Ted Kremenek344f7e32008-04-03 17:55:57 +000086 << Directory.toString() << "'\n"
87 << "reason: " << ErrorMsg << '\n';
Ted Kremenek88f5cde2008-03-27 06:17:42 +000088
89 noDir = true;
90
91 return;
92 }
93 }
94
95 if (noDir)
96 return;
97
98 // Create a new rewriter to generate HTML.
99 SourceManager& SMgr = D.begin()->getLocation().getManager();
100 Rewriter R(SMgr);
101
102 // Process the path.
103
104 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000105 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000106
107 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
108 I!=E; ++I, --n) {
109
Ted Kremenek33bd9422008-03-31 23:30:12 +0000110 HandlePiece(R, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000111 }
112
113 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000114
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000115 unsigned FileID = R.getSourceMgr().getMainFileID();
116 html::EscapeText(R, FileID);
117 html::AddLineNumbers(R, FileID);
118
Ted Kremenekb9476392008-04-02 20:44:16 +0000119 // Get the full directory name of the analyzed file.
120
121 const FileEntry* Entry = SMgr.getFileEntryForID(FileID);
122 std::string DirName(Entry->getDir()->getName());
Ted Kremenek2e939812008-03-27 07:35:49 +0000123
Ted Kremenekb9476392008-04-02 20:44:16 +0000124 if (DirName == ".")
125 DirName = llvm::sys::Path::GetCurrentDirectory().toString();
126
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000127 // Add the name of the file as an <h1> tag.
128
Ted Kremenek2e939812008-03-27 07:35:49 +0000129 {
130 std::ostringstream os;
Ted Kremenek2e939812008-03-27 07:35:49 +0000131
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000132 os << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
133 "<tr><td class=\"rowname\">File:</td><td>"
134 << html::EscapeText(DirName)
135 << html::EscapeText(Entry->getName())
136 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
137 "<a href=\"#EndPath\">line "
138 << (*D.rbegin()).getLocation().getLogicalLineNumber()
139 << ", column "
140 << (*D.rbegin()).getLocation().getLogicalColumnNumber()
141 << "</a></td></tr>\n"
142 "<tr><td class=\"rowname\">Description:</td><td>"
143 << D.getDescription()
144 << "</td></tr>\n</table>\n"
145 "<h3>Annotated Source Code</h3>\n";
146
Ted Kremenek2e939812008-03-27 07:35:49 +0000147 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000148 }
149
Ted Kremenekb9476392008-04-02 20:44:16 +0000150 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000151
152 const std::string& BugDesc = D.getDescription();
153
154 if (!BugDesc.empty()) {
155 std::ostringstream os;
Ted Kremenek86b43812008-04-02 18:03:20 +0000156 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Ted Kremenek07615402008-04-02 07:04:46 +0000157 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000158 }
159
160 {
161 std::ostringstream os;
162 os << "\n<!-- BUGFILE " << DirName << "/" << Entry->getName() << " -->\n";
163 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
164 }
165
166 {
167 std::ostringstream os;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000168 os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber()
Ted Kremenekb9476392008-04-02 20:44:16 +0000169 << " -->\n";
170 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
171 }
172
173 {
174 std::ostringstream os;
175 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
176 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
177 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000178
179 // Add CSS, header, and footer.
180
181 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
182
183 // Get the rewrite buffer.
184 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
185
186 if (!Buf) {
187 llvm::cerr << "warning: no diagnostics generated for main file.\n";
188 return;
189 }
190
191 // Create the stream to write out the HTML.
192 std::ofstream os;
193
194 {
195 // Create a path for the target HTML file.
196 llvm::sys::Path F(FilePrefix);
197 F.makeUnique(false, NULL);
198
199 // Rename the file with an HTML extension.
200 llvm::sys::Path H(F);
201 H.appendSuffix("html");
202 F.renamePathOnDisk(H, NULL);
203
204 os.open(H.toString().c_str());
205
206 if (!os) {
207 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
208 return;
209 }
210 }
211
212 // Emit the HTML to disk.
213
Ted Kremenekfa5be362008-04-08 22:37:58 +0000214 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I) {
215 // Expand tabs.
216 if (*I == '\t')
217 os << " ";
218 else
219 os << *I;
220 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000221}
222
223void HTMLDiagnostics::HandlePiece(Rewriter& R,
224 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000225 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000226
227 // For now, just draw a box above the line in question, and emit the
228 // warning.
229
230 FullSourceLoc Pos = P.getLocation();
231
232 if (!Pos.isValid())
233 return;
234
235 SourceManager& SM = R.getSourceMgr();
236 FullSourceLoc LPos = Pos.getLogicalLoc();
Ted Kremenek5dd00412008-04-14 21:06:04 +0000237 unsigned FileID = SM.getCanonicalFileID(LPos.getLocation());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000238
239 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
240
Ted Kremenek5dd00412008-04-14 21:06:04 +0000241 if (!SM.isFromMainFile(LPos.getLocation()))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000242 return;
243
244 // Compute the column number. Rewind from the current position to the start
245 // of the line.
246
247 unsigned ColNo = LPos.getColumnNumber();
248 const char *TokLogicalPtr = LPos.getCharacterData();
249 const char *LineStart = TokLogicalPtr-ColNo;
250
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000251 // Compute the margin offset by counting tabs and non-tabs.
252
253 unsigned PosNo = 0;
254
255 for (const char* c = LineStart; c != TokLogicalPtr; ++c)
Ted Kremenek8fb00162008-03-31 23:14:05 +0000256 PosNo += *c == '\t' ? 4 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000257
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000258 // Create the html for the message.
259
260 std::ostringstream os;
261
262 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
Ted Kremenek33bd9422008-03-31 23:30:12 +0000263 << "<div id=\"";
264
265 if (num == max)
266 os << "EndPath";
267 else
268 os << "Path" << num;
269
270 os << "\" class=\"msg\" style=\"margin-left:"
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000271 << PosNo << "ex\">";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000272
Ted Kremenek718ceb12008-04-02 21:04:20 +0000273 if (max > 1)
274 os << "<span class=\"PathIndex\">[" << num << "]</span> ";
275
Ted Kremenek053ef592008-03-27 17:15:29 +0000276 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000277
278 // Insert the new html.
279
280 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
281 const char* FileStart = Buf->getBufferStart();
282
283 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart),
284 os.str());
285
286 // Now highlight the ranges.
287
288 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
289 I != E; ++I)
Ted Kremenek5dd00412008-04-14 21:06:04 +0000290 HighlightRange(R, *I);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000291}
292
Ted Kremenek5dd00412008-04-14 21:06:04 +0000293void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000294
Ted Kremenek5dd00412008-04-14 21:06:04 +0000295 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000296
Ted Kremenek5dd00412008-04-14 21:06:04 +0000297 SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin());
298 unsigned StartLineNo = SM.getLineNumber(LogicalStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000299
Ted Kremenek5dd00412008-04-14 21:06:04 +0000300 SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd());
301 unsigned EndLineNo = SM.getLineNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000302
303 if (EndLineNo < StartLineNo)
304 return;
305
Ted Kremenek5dd00412008-04-14 21:06:04 +0000306 if (!SM.isFromMainFile(LogicalStart) ||
307 !SM.isFromMainFile(LogicalEnd))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000308 return;
309
310 // Compute the column number of the end.
Ted Kremenek5dd00412008-04-14 21:06:04 +0000311 unsigned EndColNo = SM.getColumnNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000312 unsigned OldEndColNo = EndColNo;
313
314 if (EndColNo) {
315 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek5dd00412008-04-14 21:06:04 +0000316 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000317 }
318
319 // Highlight the range. Make the span tag the outermost tag for the
320 // selected range.
321
322 SourceLocation E =
323 LogicalEnd.getFileLocWithOffset(OldEndColNo > EndColNo
324 ? -(OldEndColNo - EndColNo)
325 : EndColNo - OldEndColNo);
326
327 R.InsertCStrBefore(LogicalStart, "<span class=\"mrange\">");
328 R.InsertCStrAfter(E, "</span>");
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000329
330 if (EndLineNo == StartLineNo)
331 return;
332
333 // Add in </span><span> tags for intermediate lines.
334
Ted Kremenek5dd00412008-04-14 21:06:04 +0000335 unsigned FileID = SM.getCanonicalFileID(LogicalStart);
336 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000337
Ted Kremenek5dd00412008-04-14 21:06:04 +0000338 unsigned Pos = SM.getFullFilePos(LogicalStart);
339 unsigned EndPos = SM.getFullFilePos(E);
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000340 const char* buf = Buf->getBufferStart();
341
342 for (; Pos != EndPos; ++Pos) {
343
Ted Kremenek5dd00412008-04-14 21:06:04 +0000344 SourceLocation L = SourceLocation::getFileLoc(FileID, Pos);
345 unsigned Col = SM.getColumnNumber(L);
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000346
347 if (Col == 1) {
348
349 // Start if a new line. Scan to see if we hit anything that is not
350 // whitespace or a newline.
351
352 unsigned PosTmp = Pos;
353 bool NewLine = false;
354
355 for ( ; PosTmp != EndPos ; ++PosTmp) {
356 switch (buf[PosTmp]) {
357 case ' ':
358 case '\t': continue;
359 case '\n':
360 NewLine = true;
361 break;
362 default:
363 break;
364 }
365
366 break;
367 }
368
369 if (PosTmp == EndPos)
370 break;
371
372 Pos = PosTmp;
373
374 // Don't highlight a blank line.
375 if (NewLine)
376 continue;
377
378 // This line contains text that we should highlight.
379 // Ignore leading whitespace.
Ted Kremenek5dd00412008-04-14 21:06:04 +0000380 L = SourceLocation::getFileLoc(FileID, Pos);
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000381 R.InsertCStrAfter(L, "<span class=\"mrange\">");
382 }
383 else if (buf[Pos] == '\n')
384 R.InsertCStrBefore(L, "</span>");
385 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000386}