blob: d4b481bfdad64f74e56b92d9a3f126f382d4c821 [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 Kremenek88f5cde2008-03-27 06:17:42 +000050 void HighlightRange(Rewriter& R, SourceRange Range, unsigned MainFileID);
51};
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;
81 Directory.createDirectoryOnDisk(true, NULL);
82
83 if (!Directory.isDirectory()) {
84 llvm::cerr << "warning: could not create directory '"
85 << FilePrefix.toString() << "'\n";
86
87 noDir = true;
88
89 return;
90 }
91 }
92
93 if (noDir)
94 return;
95
96 // Create a new rewriter to generate HTML.
97 SourceManager& SMgr = D.begin()->getLocation().getManager();
98 Rewriter R(SMgr);
99
100 // Process the path.
101
102 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000103 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000104
105 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
106 I!=E; ++I, --n) {
107
Ted Kremenek33bd9422008-03-31 23:30:12 +0000108 HandlePiece(R, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000109 }
110
111 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000112
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000113 unsigned FileID = R.getSourceMgr().getMainFileID();
114 html::EscapeText(R, FileID);
115 html::AddLineNumbers(R, FileID);
116
Ted Kremenekb9476392008-04-02 20:44:16 +0000117 // Get the full directory name of the analyzed file.
118
119 const FileEntry* Entry = SMgr.getFileEntryForID(FileID);
120 std::string DirName(Entry->getDir()->getName());
Ted Kremenek2e939812008-03-27 07:35:49 +0000121
Ted Kremenekb9476392008-04-02 20:44:16 +0000122 if (DirName == ".")
123 DirName = llvm::sys::Path::GetCurrentDirectory().toString();
124
125 // Add the name of the file as an <h1> tag.
126
Ted Kremenek2e939812008-03-27 07:35:49 +0000127 {
128 std::ostringstream os;
Ted Kremenek2e939812008-03-27 07:35:49 +0000129
Ted Kremenekb9476392008-04-02 20:44:16 +0000130 os << "<h1>" << html::EscapeText(DirName)
131 << "/" << html::EscapeText(Entry->getName()) << "</h1>\n";
Ted Kremenek2e939812008-03-27 07:35:49 +0000132
133 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000134 }
135
Ted Kremenekb9476392008-04-02 20:44:16 +0000136 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000137
138 const std::string& BugDesc = D.getDescription();
139
140 if (!BugDesc.empty()) {
141 std::ostringstream os;
Ted Kremenek86b43812008-04-02 18:03:20 +0000142 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Ted Kremenek07615402008-04-02 07:04:46 +0000143 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000144 }
145
146 {
147 std::ostringstream os;
148 os << "\n<!-- BUGFILE " << DirName << "/" << Entry->getName() << " -->\n";
149 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
150 }
151
152 {
153 std::ostringstream os;
154 os << "\n<!-- BUGLINE " << D.back()->getLocation().getLineNumber()
155 << " -->\n";
156 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
157 }
158
159 {
160 std::ostringstream os;
161 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
162 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
163 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000164
165 // Add CSS, header, and footer.
166
167 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
168
169 // Get the rewrite buffer.
170 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
171
172 if (!Buf) {
173 llvm::cerr << "warning: no diagnostics generated for main file.\n";
174 return;
175 }
176
177 // Create the stream to write out the HTML.
178 std::ofstream os;
179
180 {
181 // Create a path for the target HTML file.
182 llvm::sys::Path F(FilePrefix);
183 F.makeUnique(false, NULL);
184
185 // Rename the file with an HTML extension.
186 llvm::sys::Path H(F);
187 H.appendSuffix("html");
188 F.renamePathOnDisk(H, NULL);
189
190 os.open(H.toString().c_str());
191
192 if (!os) {
193 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
194 return;
195 }
196 }
197
198 // Emit the HTML to disk.
199
200 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
201 os << *I;
202}
203
204void HTMLDiagnostics::HandlePiece(Rewriter& R,
205 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000206 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000207
208 // For now, just draw a box above the line in question, and emit the
209 // warning.
210
211 FullSourceLoc Pos = P.getLocation();
212
213 if (!Pos.isValid())
214 return;
215
216 SourceManager& SM = R.getSourceMgr();
217 FullSourceLoc LPos = Pos.getLogicalLoc();
218 unsigned FileID = LPos.getLocation().getFileID();
219
220 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
221
222 unsigned MainFileID = SM.getMainFileID();
223
224 if (FileID != MainFileID)
225 return;
226
227 // Compute the column number. Rewind from the current position to the start
228 // of the line.
229
230 unsigned ColNo = LPos.getColumnNumber();
231 const char *TokLogicalPtr = LPos.getCharacterData();
232 const char *LineStart = TokLogicalPtr-ColNo;
233
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000234 // Compute the margin offset by counting tabs and non-tabs.
235
236 unsigned PosNo = 0;
237
238 for (const char* c = LineStart; c != TokLogicalPtr; ++c)
Ted Kremenek8fb00162008-03-31 23:14:05 +0000239 PosNo += *c == '\t' ? 4 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000240
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000241 // Create the html for the message.
242
243 std::ostringstream os;
244
245 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
Ted Kremenek33bd9422008-03-31 23:30:12 +0000246 << "<div id=\"";
247
248 if (num == max)
249 os << "EndPath";
250 else
251 os << "Path" << num;
252
253 os << "\" class=\"msg\" style=\"margin-left:"
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000254 << PosNo << "ex\">";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000255
Ted Kremenek053ef592008-03-27 17:15:29 +0000256 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000257
258 // Insert the new html.
259
260 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
261 const char* FileStart = Buf->getBufferStart();
262
263 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart),
264 os.str());
265
266 // Now highlight the ranges.
267
268 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
269 I != E; ++I)
270 HighlightRange(R, *I, MainFileID);
271}
272
273void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range,
274 unsigned MainFileID) {
275
276 SourceManager& SourceMgr = R.getSourceMgr();
277
278 SourceLocation LogicalStart = SourceMgr.getLogicalLoc(Range.getBegin());
279 unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
280
281 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(Range.getEnd());
282 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
283
284 if (EndLineNo < StartLineNo)
285 return;
286
287 if (LogicalStart.getFileID() != MainFileID ||
288 LogicalEnd.getFileID() != MainFileID)
289 return;
290
291 // Compute the column number of the end.
292 unsigned EndColNo = SourceMgr.getColumnNumber(LogicalEnd);
293 unsigned OldEndColNo = EndColNo;
294
295 if (EndColNo) {
296 // Add in the length of the token, so that we cover multi-char tokens.
297 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SourceMgr);
298 }
299
300 // Highlight the range. Make the span tag the outermost tag for the
301 // selected range.
302
303 SourceLocation E =
304 LogicalEnd.getFileLocWithOffset(OldEndColNo > EndColNo
305 ? -(OldEndColNo - EndColNo)
306 : EndColNo - OldEndColNo);
307
308 R.InsertCStrBefore(LogicalStart, "<span class=\"mrange\">");
309 R.InsertCStrAfter(E, "</span>");
310}