blob: 2f1b6e3676bed06b7fd665d449cede6843f73c0f [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 Kremenek7414dc02008-04-20 01:02:33 +0000227 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000228 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000229}
230
231void HTMLDiagnostics::HandlePiece(Rewriter& R,
232 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000233 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000234
235 // For now, just draw a box above the line in question, and emit the
236 // warning.
237
238 FullSourceLoc Pos = P.getLocation();
239
240 if (!Pos.isValid())
241 return;
242
243 SourceManager& SM = R.getSourceMgr();
244 FullSourceLoc LPos = Pos.getLogicalLoc();
Ted Kremenek5dd00412008-04-14 21:06:04 +0000245 unsigned FileID = SM.getCanonicalFileID(LPos.getLocation());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000246
247 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
248
Ted Kremenek5dd00412008-04-14 21:06:04 +0000249 if (!SM.isFromMainFile(LPos.getLocation()))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000250 return;
251
252 // Compute the column number. Rewind from the current position to the start
253 // of the line.
254
255 unsigned ColNo = LPos.getColumnNumber();
256 const char *TokLogicalPtr = LPos.getCharacterData();
257 const char *LineStart = TokLogicalPtr-ColNo;
258
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000259 // Compute the margin offset by counting tabs and non-tabs.
260
261 unsigned PosNo = 0;
262
263 for (const char* c = LineStart; c != TokLogicalPtr; ++c)
Ted Kremenek8fb00162008-03-31 23:14:05 +0000264 PosNo += *c == '\t' ? 4 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000265
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000266 // Create the html for the message.
267
268 std::ostringstream os;
269
270 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
Ted Kremenek33bd9422008-03-31 23:30:12 +0000271 << "<div id=\"";
272
273 if (num == max)
274 os << "EndPath";
275 else
276 os << "Path" << num;
277
278 os << "\" class=\"msg\" style=\"margin-left:"
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000279 << PosNo << "ex\">";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000280
Ted Kremenek718ceb12008-04-02 21:04:20 +0000281 if (max > 1)
282 os << "<span class=\"PathIndex\">[" << num << "]</span> ";
283
Ted Kremenek053ef592008-03-27 17:15:29 +0000284 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000285
286 // Insert the new html.
287
288 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
289 const char* FileStart = Buf->getBufferStart();
290
291 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart),
292 os.str());
293
294 // Now highlight the ranges.
295
296 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
297 I != E; ++I)
Ted Kremenek5dd00412008-04-14 21:06:04 +0000298 HighlightRange(R, *I);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000299}
300
Ted Kremenek5dd00412008-04-14 21:06:04 +0000301void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000302
Ted Kremenek5dd00412008-04-14 21:06:04 +0000303 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000304
Ted Kremenek5dd00412008-04-14 21:06:04 +0000305 SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin());
306 unsigned StartLineNo = SM.getLineNumber(LogicalStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000307
Ted Kremenek5dd00412008-04-14 21:06:04 +0000308 SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd());
309 unsigned EndLineNo = SM.getLineNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000310
311 if (EndLineNo < StartLineNo)
312 return;
313
Ted Kremenek5dd00412008-04-14 21:06:04 +0000314 if (!SM.isFromMainFile(LogicalStart) ||
315 !SM.isFromMainFile(LogicalEnd))
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000316 return;
317
318 // Compute the column number of the end.
Ted Kremenek5dd00412008-04-14 21:06:04 +0000319 unsigned EndColNo = SM.getColumnNumber(LogicalEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000320 unsigned OldEndColNo = EndColNo;
321
322 if (EndColNo) {
323 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000324 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000325 }
326
327 // Highlight the range. Make the span tag the outermost tag for the
328 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000329
Ted Kremenek12fc0142008-04-18 05:01:33 +0000330 SourceLocation E = LogicalEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000331
332 html::HighlightRange(R, LogicalStart, E,
333 "<span class=\"mrange\">", "</span>");
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000334}