blob: 79eae90f1256a69724b018221a128412af3a66b2 [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 Kremenek2e939812008-03-27 07:35:49 +0000117 // Add the name of the file.
118
119 {
120 std::ostringstream os;
Ted Kremenek2e939812008-03-27 07:35:49 +0000121
Ted Kremenek5d0f5922008-03-27 17:25:28 +0000122 os << "<h1>";
123
124 const FileEntry* Entry = SMgr.getFileEntryForID(FileID);
125 const char* dname = Entry->getDir()->getName();
126
127 if (strcmp(dname,".") == 0)
128 os << html::EscapeText(llvm::sys::Path::GetCurrentDirectory().toString());
129 else
130 os << html::EscapeText(dname);
131
132 os << "/" << html::EscapeText(Entry->getName()) << "</h1>\n";
Ted Kremenek2e939812008-03-27 07:35:49 +0000133
134 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000135 }
136
137 // Add the bug description.
138
139 const std::string& BugDesc = D.getDescription();
140
141 if (!BugDesc.empty()) {
142 std::ostringstream os;
143 os << "<!-- BUGDESC " << BugDesc << " -->\n";
144 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenek2e939812008-03-27 07:35:49 +0000145 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000146
147 // Add CSS, header, and footer.
148
149 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
150
151 // Get the rewrite buffer.
152 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
153
154 if (!Buf) {
155 llvm::cerr << "warning: no diagnostics generated for main file.\n";
156 return;
157 }
158
159 // Create the stream to write out the HTML.
160 std::ofstream os;
161
162 {
163 // Create a path for the target HTML file.
164 llvm::sys::Path F(FilePrefix);
165 F.makeUnique(false, NULL);
166
167 // Rename the file with an HTML extension.
168 llvm::sys::Path H(F);
169 H.appendSuffix("html");
170 F.renamePathOnDisk(H, NULL);
171
172 os.open(H.toString().c_str());
173
174 if (!os) {
175 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
176 return;
177 }
178 }
179
180 // Emit the HTML to disk.
181
182 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
183 os << *I;
184}
185
186void HTMLDiagnostics::HandlePiece(Rewriter& R,
187 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000188 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000189
190 // For now, just draw a box above the line in question, and emit the
191 // warning.
192
193 FullSourceLoc Pos = P.getLocation();
194
195 if (!Pos.isValid())
196 return;
197
198 SourceManager& SM = R.getSourceMgr();
199 FullSourceLoc LPos = Pos.getLogicalLoc();
200 unsigned FileID = LPos.getLocation().getFileID();
201
202 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
203
204 unsigned MainFileID = SM.getMainFileID();
205
206 if (FileID != MainFileID)
207 return;
208
209 // Compute the column number. Rewind from the current position to the start
210 // of the line.
211
212 unsigned ColNo = LPos.getColumnNumber();
213 const char *TokLogicalPtr = LPos.getCharacterData();
214 const char *LineStart = TokLogicalPtr-ColNo;
215
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000216 // Compute the margin offset by counting tabs and non-tabs.
217
218 unsigned PosNo = 0;
219
220 for (const char* c = LineStart; c != TokLogicalPtr; ++c)
Ted Kremenek8fb00162008-03-31 23:14:05 +0000221 PosNo += *c == '\t' ? 4 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000222
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000223 // Create the html for the message.
224
225 std::ostringstream os;
226
227 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
Ted Kremenek33bd9422008-03-31 23:30:12 +0000228 << "<div id=\"";
229
230 if (num == max)
231 os << "EndPath";
232 else
233 os << "Path" << num;
234
235 os << "\" class=\"msg\" style=\"margin-left:"
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000236 << PosNo << "ex\">";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000237
Ted Kremenek053ef592008-03-27 17:15:29 +0000238 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000239
240 // Insert the new html.
241
242 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
243 const char* FileStart = Buf->getBufferStart();
244
245 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart),
246 os.str());
247
248 // Now highlight the ranges.
249
250 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
251 I != E; ++I)
252 HighlightRange(R, *I, MainFileID);
253}
254
255void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range,
256 unsigned MainFileID) {
257
258 SourceManager& SourceMgr = R.getSourceMgr();
259
260 SourceLocation LogicalStart = SourceMgr.getLogicalLoc(Range.getBegin());
261 unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
262
263 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(Range.getEnd());
264 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
265
266 if (EndLineNo < StartLineNo)
267 return;
268
269 if (LogicalStart.getFileID() != MainFileID ||
270 LogicalEnd.getFileID() != MainFileID)
271 return;
272
273 // Compute the column number of the end.
274 unsigned EndColNo = SourceMgr.getColumnNumber(LogicalEnd);
275 unsigned OldEndColNo = EndColNo;
276
277 if (EndColNo) {
278 // Add in the length of the token, so that we cover multi-char tokens.
279 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SourceMgr);
280 }
281
282 // Highlight the range. Make the span tag the outermost tag for the
283 // selected range.
284
285 SourceLocation E =
286 LogicalEnd.getFileLocWithOffset(OldEndColNo > EndColNo
287 ? -(OldEndColNo - EndColNo)
288 : EndColNo - OldEndColNo);
289
290 R.InsertCStrBefore(LogicalStart, "<span class=\"mrange\">");
291 R.InsertCStrAfter(E, "</span>");
292}