blob: 917949fac179fad20887967ba58bebdf578392f8 [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());
135 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000136
137 // Add CSS, header, and footer.
138
139 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
140
141 // Get the rewrite buffer.
142 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
143
144 if (!Buf) {
145 llvm::cerr << "warning: no diagnostics generated for main file.\n";
146 return;
147 }
148
149 // Create the stream to write out the HTML.
150 std::ofstream os;
151
152 {
153 // Create a path for the target HTML file.
154 llvm::sys::Path F(FilePrefix);
155 F.makeUnique(false, NULL);
156
157 // Rename the file with an HTML extension.
158 llvm::sys::Path H(F);
159 H.appendSuffix("html");
160 F.renamePathOnDisk(H, NULL);
161
162 os.open(H.toString().c_str());
163
164 if (!os) {
165 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
166 return;
167 }
168 }
169
170 // Emit the HTML to disk.
171
172 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
173 os << *I;
174}
175
176void HTMLDiagnostics::HandlePiece(Rewriter& R,
177 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000178 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000179
180 // For now, just draw a box above the line in question, and emit the
181 // warning.
182
183 FullSourceLoc Pos = P.getLocation();
184
185 if (!Pos.isValid())
186 return;
187
188 SourceManager& SM = R.getSourceMgr();
189 FullSourceLoc LPos = Pos.getLogicalLoc();
190 unsigned FileID = LPos.getLocation().getFileID();
191
192 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
193
194 unsigned MainFileID = SM.getMainFileID();
195
196 if (FileID != MainFileID)
197 return;
198
199 // Compute the column number. Rewind from the current position to the start
200 // of the line.
201
202 unsigned ColNo = LPos.getColumnNumber();
203 const char *TokLogicalPtr = LPos.getCharacterData();
204 const char *LineStart = TokLogicalPtr-ColNo;
205
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000206 // Compute the margin offset by counting tabs and non-tabs.
207
208 unsigned PosNo = 0;
209
210 for (const char* c = LineStart; c != TokLogicalPtr; ++c)
Ted Kremenek8fb00162008-03-31 23:14:05 +0000211 PosNo += *c == '\t' ? 4 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000212
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000213 // Create the html for the message.
214
215 std::ostringstream os;
216
217 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
Ted Kremenek33bd9422008-03-31 23:30:12 +0000218 << "<div id=\"";
219
220 if (num == max)
221 os << "EndPath";
222 else
223 os << "Path" << num;
224
225 os << "\" class=\"msg\" style=\"margin-left:"
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000226 << PosNo << "ex\">";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000227
Ted Kremenek053ef592008-03-27 17:15:29 +0000228 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000229
230 // Insert the new html.
231
232 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
233 const char* FileStart = Buf->getBufferStart();
234
235 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart),
236 os.str());
237
238 // Now highlight the ranges.
239
240 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
241 I != E; ++I)
242 HighlightRange(R, *I, MainFileID);
243}
244
245void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range,
246 unsigned MainFileID) {
247
248 SourceManager& SourceMgr = R.getSourceMgr();
249
250 SourceLocation LogicalStart = SourceMgr.getLogicalLoc(Range.getBegin());
251 unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
252
253 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(Range.getEnd());
254 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
255
256 if (EndLineNo < StartLineNo)
257 return;
258
259 if (LogicalStart.getFileID() != MainFileID ||
260 LogicalEnd.getFileID() != MainFileID)
261 return;
262
263 // Compute the column number of the end.
264 unsigned EndColNo = SourceMgr.getColumnNumber(LogicalEnd);
265 unsigned OldEndColNo = EndColNo;
266
267 if (EndColNo) {
268 // Add in the length of the token, so that we cover multi-char tokens.
269 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SourceMgr);
270 }
271
272 // Highlight the range. Make the span tag the outermost tag for the
273 // selected range.
274
275 SourceLocation E =
276 LogicalEnd.getFileLocWithOffset(OldEndColNo > EndColNo
277 ? -(OldEndColNo - EndColNo)
278 : EndColNo - OldEndColNo);
279
280 R.InsertCStrBefore(LogicalStart, "<span class=\"mrange\">");
281 R.InsertCStrAfter(E, "</span>");
282}