blob: 4d95c17b67041e00b4378fa5230916da448b4ebb [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 Kremenek88f5cde2008-03-27 06:17:42 +000042public:
Ted Kremenek339b9c22008-04-17 22:31:54 +000043 HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
44 PreprocessorFactory* ppf);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000045
46 virtual ~HTMLDiagnostics() {}
47
48 virtual void HandlePathDiagnostic(const PathDiagnostic& D);
49
Ted Kremenek33bd9422008-03-31 23:30:12 +000050 void HandlePiece(Rewriter& R, const PathDiagnosticPiece& P,
51 unsigned num, unsigned max);
52
Ted Kremenek5dd00412008-04-14 21:06:04 +000053 void HighlightRange(Rewriter& R, SourceRange Range);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000054};
55
56} // end anonymous namespace
57
Ted Kremenek339b9c22008-04-17 22:31:54 +000058HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
59 PreprocessorFactory* ppf)
Ted Kremenek47abe762008-04-16 16:39:56 +000060 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenek339b9c22008-04-17 22:31:54 +000061 PP(pp), PPF(ppf) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000062
63 // All html files begin with "report"
64 FilePrefix.appendComponent("report");
65}
66
67PathDiagnosticClient*
Ted Kremenek339b9c22008-04-17 22:31:54 +000068clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
69 PreprocessorFactory* PPF) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000070
Ted Kremenek339b9c22008-04-17 22:31:54 +000071 return new HTMLDiagnostics(prefix, PP, PPF);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000072}
73
74//===----------------------------------------------------------------------===//
75// Report processing.
76//===----------------------------------------------------------------------===//
77
78void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic& D) {
79
80 if (D.empty())
81 return;
82
83 // Create the HTML directory if it is missing.
84
85 if (!createdDir) {
86 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +000087 std::string ErrorMsg;
88 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000089
90 if (!Directory.isDirectory()) {
91 llvm::cerr << "warning: could not create directory '"
Ted Kremenek344f7e32008-04-03 17:55:57 +000092 << Directory.toString() << "'\n"
93 << "reason: " << ErrorMsg << '\n';
Ted Kremenek88f5cde2008-03-27 06:17:42 +000094
95 noDir = true;
96
97 return;
98 }
99 }
100
101 if (noDir)
102 return;
103
104 // Create a new rewriter to generate HTML.
105 SourceManager& SMgr = D.begin()->getLocation().getManager();
106 Rewriter R(SMgr);
107
108 // Process the path.
109
110 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000111 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000112
113 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
114 I!=E; ++I, --n) {
115
Ted Kremenek33bd9422008-03-31 23:30:12 +0000116 HandlePiece(R, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000117 }
118
119 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000120
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000121 unsigned FileID = R.getSourceMgr().getMainFileID();
122 html::EscapeText(R, FileID);
123 html::AddLineNumbers(R, FileID);
124
Ted Kremenek47abe762008-04-16 16:39:56 +0000125 // If we have a preprocessor, relex the file and syntax highlight.
126 // We might not have a preprocessor if we come from a deserialized AST file,
127 // for example.
128
Ted Kremenek339b9c22008-04-17 22:31:54 +0000129 if (PP) html::SyntaxHighlight(R, FileID, *PP);
130 if (PPF) html::HighlightMacros(R, FileID, *PPF);
Ted Kremenek47abe762008-04-16 16:39:56 +0000131
Ted Kremenekb9476392008-04-02 20:44:16 +0000132 // Get the full directory name of the analyzed file.
133
134 const FileEntry* Entry = SMgr.getFileEntryForID(FileID);
135 std::string DirName(Entry->getDir()->getName());
Ted Kremenek2e939812008-03-27 07:35:49 +0000136
Ted Kremenekb9476392008-04-02 20:44:16 +0000137 if (DirName == ".")
138 DirName = llvm::sys::Path::GetCurrentDirectory().toString();
139
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000140 // Add the name of the file as an <h1> tag.
141
Ted Kremenek2e939812008-03-27 07:35:49 +0000142 {
143 std::ostringstream os;
Ted Kremenek2e939812008-03-27 07:35:49 +0000144
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000145 os << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
146 "<tr><td class=\"rowname\">File:</td><td>"
147 << html::EscapeText(DirName)
148 << html::EscapeText(Entry->getName())
149 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
150 "<a href=\"#EndPath\">line "
151 << (*D.rbegin()).getLocation().getLogicalLineNumber()
152 << ", column "
153 << (*D.rbegin()).getLocation().getLogicalColumnNumber()
154 << "</a></td></tr>\n"
155 "<tr><td class=\"rowname\">Description:</td><td>"
156 << D.getDescription()
157 << "</td></tr>\n</table>\n"
158 "<h3>Annotated Source Code</h3>\n";
159
Ted Kremenek2e939812008-03-27 07:35:49 +0000160 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000161 }
162
Ted Kremenekb9476392008-04-02 20:44:16 +0000163 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000164
165 const std::string& BugDesc = D.getDescription();
166
167 if (!BugDesc.empty()) {
168 std::ostringstream os;
Ted Kremenek86b43812008-04-02 18:03:20 +0000169 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Ted Kremenek07615402008-04-02 07:04:46 +0000170 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000171 }
172
173 {
174 std::ostringstream os;
175 os << "\n<!-- BUGFILE " << DirName << "/" << Entry->getName() << " -->\n";
176 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
177 }
178
179 {
180 std::ostringstream os;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000181 os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber()
Ted Kremenekb9476392008-04-02 20:44:16 +0000182 << " -->\n";
183 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
184 }
185
186 {
187 std::ostringstream os;
188 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
189 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
190 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000191
192 // Add CSS, header, and footer.
193
194 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
195
196 // Get the rewrite buffer.
197 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
198
199 if (!Buf) {
200 llvm::cerr << "warning: no diagnostics generated for main file.\n";
201 return;
202 }
203
204 // Create the stream to write out the HTML.
205 std::ofstream os;
206
207 {
208 // Create a path for the target HTML file.
209 llvm::sys::Path F(FilePrefix);
210 F.makeUnique(false, NULL);
211
212 // Rename the file with an HTML extension.
213 llvm::sys::Path H(F);
214 H.appendSuffix("html");
215 F.renamePathOnDisk(H, NULL);
216
217 os.open(H.toString().c_str());
218
219 if (!os) {
220 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
221 return;
222 }
223 }
224
225 // Emit the HTML to disk.
226
Ted Kremenekfa5be362008-04-08 22:37:58 +0000227 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I) {
228 // Expand tabs.
229 if (*I == '\t')
230 os << " ";
231 else
232 os << *I;
233 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000234}
235
236void HTMLDiagnostics::HandlePiece(Rewriter& R,
237 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000238 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000239
240 // For now, just draw a box above the line in question, and emit the
241 // warning.
242
243 FullSourceLoc Pos = P.getLocation();
244
245 if (!Pos.isValid())
246 return;
247
248 SourceManager& SM = R.getSourceMgr();
249 FullSourceLoc LPos = Pos.getLogicalLoc();
Ted Kremenek5dd00412008-04-14 21:06:04 +0000250 unsigned FileID = SM.getCanonicalFileID(LPos.getLocation());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000251
252 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
253
Ted Kremenek5dd00412008-04-14 21:06:04 +0000254 if (!SM.isFromMainFile(LPos.getLocation()))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000255 return;
256
257 // Compute the column number. Rewind from the current position to the start
258 // of the line.
259
260 unsigned ColNo = LPos.getColumnNumber();
261 const char *TokLogicalPtr = LPos.getCharacterData();
262 const char *LineStart = TokLogicalPtr-ColNo;
263
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000264 // Compute the margin offset by counting tabs and non-tabs.
265
266 unsigned PosNo = 0;
267
268 for (const char* c = LineStart; c != TokLogicalPtr; ++c)
Ted Kremenek8fb00162008-03-31 23:14:05 +0000269 PosNo += *c == '\t' ? 4 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000270
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000271 // Create the html for the message.
272
273 std::ostringstream os;
274
275 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
Ted Kremenek33bd9422008-03-31 23:30:12 +0000276 << "<div id=\"";
277
278 if (num == max)
279 os << "EndPath";
280 else
281 os << "Path" << num;
282
283 os << "\" class=\"msg\" style=\"margin-left:"
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000284 << PosNo << "ex\">";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000285
Ted Kremenek718ceb12008-04-02 21:04:20 +0000286 if (max > 1)
287 os << "<span class=\"PathIndex\">[" << num << "]</span> ";
288
Ted Kremenek053ef592008-03-27 17:15:29 +0000289 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000290
291 // Insert the new html.
292
293 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
294 const char* FileStart = Buf->getBufferStart();
295
296 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart),
297 os.str());
298
299 // Now highlight the ranges.
300
301 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
302 I != E; ++I)
Ted Kremenek5dd00412008-04-14 21:06:04 +0000303 HighlightRange(R, *I);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000304}
305
Ted Kremenek5dd00412008-04-14 21:06:04 +0000306void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000307
Ted Kremenek5dd00412008-04-14 21:06:04 +0000308 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000309
Ted Kremenek5dd00412008-04-14 21:06:04 +0000310 SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin());
311 unsigned StartLineNo = SM.getLineNumber(LogicalStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000312
Ted Kremenek5dd00412008-04-14 21:06:04 +0000313 SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd());
314 unsigned EndLineNo = SM.getLineNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000315
316 if (EndLineNo < StartLineNo)
317 return;
318
Ted Kremenek5dd00412008-04-14 21:06:04 +0000319 if (!SM.isFromMainFile(LogicalStart) ||
320 !SM.isFromMainFile(LogicalEnd))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000321 return;
322
323 // Compute the column number of the end.
Ted Kremenek5dd00412008-04-14 21:06:04 +0000324 unsigned EndColNo = SM.getColumnNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000325 unsigned OldEndColNo = EndColNo;
326
327 if (EndColNo) {
328 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek5dd00412008-04-14 21:06:04 +0000329 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000330 }
331
332 // Highlight the range. Make the span tag the outermost tag for the
333 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000334
Ted Kremenekb7478142008-04-17 18:37:23 +0000335 SourceLocation E = LogicalEnd.getFileLocWithOffset(OldEndColNo > EndColNo
336 ? -(OldEndColNo - EndColNo)
337 : EndColNo - OldEndColNo);
338
339 html::HighlightRange(R, LogicalStart, E,
340 "<span class=\"mrange\">", "</span>");
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000341}