blob: c29096d0f68232ac7f58895ec35abfe0d7c551b3 [file] [log] [blame]
Ted Kremenekfd75e312008-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 Kremenekdfb08d42008-03-27 07:35:49 +000016#include "clang/Basic/FileManager.h"
Ted Kremenekfd75e312008-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 Kremenek72879792008-03-31 23:30:12 +000047 void HandlePiece(Rewriter& R, const PathDiagnosticPiece& P,
48 unsigned num, unsigned max);
49
Ted Kremenekfd75e312008-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
Ted Kremenek21aa0962008-04-03 07:13:10 +000077 if (!D.back()->getLocation().isFileID())
78 return;
79
Ted Kremenekfd75e312008-03-27 06:17:42 +000080 // Create the HTML directory if it is missing.
81
82 if (!createdDir) {
83 createdDir = true;
84 Directory.createDirectoryOnDisk(true, NULL);
85
86 if (!Directory.isDirectory()) {
87 llvm::cerr << "warning: could not create directory '"
88 << FilePrefix.toString() << "'\n";
89
90 noDir = true;
91
92 return;
93 }
94 }
95
96 if (noDir)
97 return;
98
99 // Create a new rewriter to generate HTML.
100 SourceManager& SMgr = D.begin()->getLocation().getManager();
101 Rewriter R(SMgr);
102
103 // Process the path.
104
105 unsigned n = D.size();
Ted Kremenek72879792008-03-31 23:30:12 +0000106 unsigned max = n;
Ted Kremenekfd75e312008-03-27 06:17:42 +0000107
108 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
109 I!=E; ++I, --n) {
110
Ted Kremenek72879792008-03-31 23:30:12 +0000111 HandlePiece(R, *I, n, max);
Ted Kremenekfd75e312008-03-27 06:17:42 +0000112 }
113
114 // Add line numbers, header, footer, etc.
Ted Kremenekdfb08d42008-03-27 07:35:49 +0000115
Ted Kremenekfd75e312008-03-27 06:17:42 +0000116 unsigned FileID = R.getSourceMgr().getMainFileID();
117 html::EscapeText(R, FileID);
118 html::AddLineNumbers(R, FileID);
119
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000120 // Get the full directory name of the analyzed file.
121
122 const FileEntry* Entry = SMgr.getFileEntryForID(FileID);
123 std::string DirName(Entry->getDir()->getName());
Ted Kremenekdfb08d42008-03-27 07:35:49 +0000124
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000125 if (DirName == ".")
126 DirName = llvm::sys::Path::GetCurrentDirectory().toString();
127
128 // Add the name of the file as an <h1> tag.
129
Ted Kremenekdfb08d42008-03-27 07:35:49 +0000130 {
131 std::ostringstream os;
Ted Kremenekdfb08d42008-03-27 07:35:49 +0000132
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000133 os << "<h1>" << html::EscapeText(DirName)
134 << "/" << html::EscapeText(Entry->getName()) << "</h1>\n";
Ted Kremenekdfb08d42008-03-27 07:35:49 +0000135
136 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenek44a70e12008-04-02 07:04:46 +0000137 }
138
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000139 // Embed meta-data tags.
Ted Kremenek44a70e12008-04-02 07:04:46 +0000140
141 const std::string& BugDesc = D.getDescription();
142
143 if (!BugDesc.empty()) {
144 std::ostringstream os;
Ted Kremenek1cae1f42008-04-02 18:03:20 +0000145 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Ted Kremenek44a70e12008-04-02 07:04:46 +0000146 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000147 }
148
149 {
150 std::ostringstream os;
151 os << "\n<!-- BUGFILE " << DirName << "/" << Entry->getName() << " -->\n";
152 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
153 }
154
155 {
156 std::ostringstream os;
Ted Kremenek21aa0962008-04-03 07:13:10 +0000157 FullSourceLoc L = D.back()->getLocation();
158 os << "\n<!-- BUGLINE " << L.getLineNumber()
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000159 << " -->\n";
160 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
161 }
162
163 {
164 std::ostringstream os;
165 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
166 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
167 }
Ted Kremenekfd75e312008-03-27 06:17:42 +0000168
169 // Add CSS, header, and footer.
170
171 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
172
173 // Get the rewrite buffer.
174 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
175
176 if (!Buf) {
177 llvm::cerr << "warning: no diagnostics generated for main file.\n";
178 return;
179 }
180
181 // Create the stream to write out the HTML.
182 std::ofstream os;
183
184 {
185 // Create a path for the target HTML file.
186 llvm::sys::Path F(FilePrefix);
187 F.makeUnique(false, NULL);
188
189 // Rename the file with an HTML extension.
190 llvm::sys::Path H(F);
191 H.appendSuffix("html");
192 F.renamePathOnDisk(H, NULL);
193
194 os.open(H.toString().c_str());
195
196 if (!os) {
197 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
198 return;
199 }
200 }
201
202 // Emit the HTML to disk.
203
204 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
205 os << *I;
206}
207
208void HTMLDiagnostics::HandlePiece(Rewriter& R,
209 const PathDiagnosticPiece& P,
Ted Kremenek72879792008-03-31 23:30:12 +0000210 unsigned num, unsigned max) {
Ted Kremenekfd75e312008-03-27 06:17:42 +0000211
212 // For now, just draw a box above the line in question, and emit the
213 // warning.
214
215 FullSourceLoc Pos = P.getLocation();
216
217 if (!Pos.isValid())
218 return;
219
220 SourceManager& SM = R.getSourceMgr();
221 FullSourceLoc LPos = Pos.getLogicalLoc();
222 unsigned FileID = LPos.getLocation().getFileID();
223
224 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
225
226 unsigned MainFileID = SM.getMainFileID();
227
228 if (FileID != MainFileID)
229 return;
230
231 // Compute the column number. Rewind from the current position to the start
232 // of the line.
233
234 unsigned ColNo = LPos.getColumnNumber();
235 const char *TokLogicalPtr = LPos.getCharacterData();
236 const char *LineStart = TokLogicalPtr-ColNo;
237
Ted Kremenekdbdb01e2008-03-31 21:40:14 +0000238 // Compute the margin offset by counting tabs and non-tabs.
239
240 unsigned PosNo = 0;
241
242 for (const char* c = LineStart; c != TokLogicalPtr; ++c)
Ted Kremenekcd2eb9b2008-03-31 23:14:05 +0000243 PosNo += *c == '\t' ? 4 : 1;
Ted Kremenekdbdb01e2008-03-31 21:40:14 +0000244
Ted Kremenekfd75e312008-03-27 06:17:42 +0000245 // Create the html for the message.
246
247 std::ostringstream os;
248
249 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
Ted Kremenek72879792008-03-31 23:30:12 +0000250 << "<div id=\"";
251
252 if (num == max)
253 os << "EndPath";
254 else
255 os << "Path" << num;
256
257 os << "\" class=\"msg\" style=\"margin-left:"
Ted Kremenekdbdb01e2008-03-31 21:40:14 +0000258 << PosNo << "ex\">";
Ted Kremenekfd75e312008-03-27 06:17:42 +0000259
Ted Kremenekd8a68b22008-04-02 21:04:20 +0000260 if (max > 1)
261 os << "<span class=\"PathIndex\">[" << num << "]</span> ";
262
Ted Kremenek57ba65c2008-03-27 17:15:29 +0000263 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenekfd75e312008-03-27 06:17:42 +0000264
265 // Insert the new html.
266
267 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
268 const char* FileStart = Buf->getBufferStart();
269
270 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart),
271 os.str());
272
273 // Now highlight the ranges.
274
275 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
276 I != E; ++I)
277 HighlightRange(R, *I, MainFileID);
278}
279
280void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range,
281 unsigned MainFileID) {
282
283 SourceManager& SourceMgr = R.getSourceMgr();
284
285 SourceLocation LogicalStart = SourceMgr.getLogicalLoc(Range.getBegin());
286 unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
287
288 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(Range.getEnd());
289 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
290
291 if (EndLineNo < StartLineNo)
292 return;
293
294 if (LogicalStart.getFileID() != MainFileID ||
295 LogicalEnd.getFileID() != MainFileID)
296 return;
297
298 // Compute the column number of the end.
299 unsigned EndColNo = SourceMgr.getColumnNumber(LogicalEnd);
300 unsigned OldEndColNo = EndColNo;
301
302 if (EndColNo) {
303 // Add in the length of the token, so that we cover multi-char tokens.
304 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SourceMgr);
305 }
306
307 // Highlight the range. Make the span tag the outermost tag for the
308 // selected range.
309
310 SourceLocation E =
311 LogicalEnd.getFileLocWithOffset(OldEndColNo > EndColNo
312 ? -(OldEndColNo - EndColNo)
313 : EndColNo - OldEndColNo);
314
315 R.InsertCStrBefore(LogicalStart, "<span class=\"mrange\">");
316 R.InsertCStrAfter(E, "</span>");
317}