blob: df2bb06636d4083cbf131c4b36f67b345258e744 [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
47 void HandlePiece(Rewriter& R, const PathDiagnosticPiece& P, unsigned num);
48 void HighlightRange(Rewriter& R, SourceRange Range, unsigned MainFileID);
49};
50
51} // end anonymous namespace
52
53HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix)
54 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false) {
55
56 // All html files begin with "report"
57 FilePrefix.appendComponent("report");
58}
59
60PathDiagnosticClient*
61clang::CreateHTMLDiagnosticClient(const std::string& prefix) {
62
63 return new HTMLDiagnostics(prefix);
64}
65
66//===----------------------------------------------------------------------===//
67// Report processing.
68//===----------------------------------------------------------------------===//
69
70void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic& D) {
71
72 if (D.empty())
73 return;
74
75 // Create the HTML directory if it is missing.
76
77 if (!createdDir) {
78 createdDir = true;
79 Directory.createDirectoryOnDisk(true, NULL);
80
81 if (!Directory.isDirectory()) {
82 llvm::cerr << "warning: could not create directory '"
83 << FilePrefix.toString() << "'\n";
84
85 noDir = true;
86
87 return;
88 }
89 }
90
91 if (noDir)
92 return;
93
94 // Create a new rewriter to generate HTML.
95 SourceManager& SMgr = D.begin()->getLocation().getManager();
96 Rewriter R(SMgr);
97
98 // Process the path.
99
100 unsigned n = D.size();
101
102 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
103 I!=E; ++I, --n) {
104
105 HandlePiece(R, *I, n);
106 }
107
108 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000109
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000110 unsigned FileID = R.getSourceMgr().getMainFileID();
111 html::EscapeText(R, FileID);
112 html::AddLineNumbers(R, FileID);
113
Ted Kremenek2e939812008-03-27 07:35:49 +0000114 // Add the name of the file.
115
116 {
117 std::ostringstream os;
Ted Kremenek2e939812008-03-27 07:35:49 +0000118
Ted Kremenek5d0f5922008-03-27 17:25:28 +0000119 os << "<h1>";
120
121 const FileEntry* Entry = SMgr.getFileEntryForID(FileID);
122 const char* dname = Entry->getDir()->getName();
123
124 if (strcmp(dname,".") == 0)
125 os << html::EscapeText(llvm::sys::Path::GetCurrentDirectory().toString());
126 else
127 os << html::EscapeText(dname);
128
129 os << "/" << html::EscapeText(Entry->getName()) << "</h1>\n";
Ted Kremenek2e939812008-03-27 07:35:49 +0000130
131 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
132 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000133
134 // Add CSS, header, and footer.
135
136 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
137
138 // Get the rewrite buffer.
139 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
140
141 if (!Buf) {
142 llvm::cerr << "warning: no diagnostics generated for main file.\n";
143 return;
144 }
145
146 // Create the stream to write out the HTML.
147 std::ofstream os;
148
149 {
150 // Create a path for the target HTML file.
151 llvm::sys::Path F(FilePrefix);
152 F.makeUnique(false, NULL);
153
154 // Rename the file with an HTML extension.
155 llvm::sys::Path H(F);
156 H.appendSuffix("html");
157 F.renamePathOnDisk(H, NULL);
158
159 os.open(H.toString().c_str());
160
161 if (!os) {
162 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
163 return;
164 }
165 }
166
167 // Emit the HTML to disk.
168
169 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
170 os << *I;
171}
172
173void HTMLDiagnostics::HandlePiece(Rewriter& R,
174 const PathDiagnosticPiece& P,
175 unsigned num) {
176
177 // For now, just draw a box above the line in question, and emit the
178 // warning.
179
180 FullSourceLoc Pos = P.getLocation();
181
182 if (!Pos.isValid())
183 return;
184
185 SourceManager& SM = R.getSourceMgr();
186 FullSourceLoc LPos = Pos.getLogicalLoc();
187 unsigned FileID = LPos.getLocation().getFileID();
188
189 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
190
191 unsigned MainFileID = SM.getMainFileID();
192
193 if (FileID != MainFileID)
194 return;
195
196 // Compute the column number. Rewind from the current position to the start
197 // of the line.
198
199 unsigned ColNo = LPos.getColumnNumber();
200 const char *TokLogicalPtr = LPos.getCharacterData();
201 const char *LineStart = TokLogicalPtr-ColNo;
202
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000203 // Compute the margin offset by counting tabs and non-tabs.
204
205 unsigned PosNo = 0;
206
207 for (const char* c = LineStart; c != TokLogicalPtr; ++c)
208 PosNo += *c == '\t' ? 8 : 1;
209
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000210 // Create the html for the message.
211
212 std::ostringstream os;
213
214 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
215 << "<div class=\"msg\" style=\"margin-left:"
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000216 << PosNo << "ex\">";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000217
Ted Kremenek053ef592008-03-27 17:15:29 +0000218 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000219
220 // Insert the new html.
221
222 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
223 const char* FileStart = Buf->getBufferStart();
224
225 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart),
226 os.str());
227
228 // Now highlight the ranges.
229
230 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
231 I != E; ++I)
232 HighlightRange(R, *I, MainFileID);
233}
234
235void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range,
236 unsigned MainFileID) {
237
238 SourceManager& SourceMgr = R.getSourceMgr();
239
240 SourceLocation LogicalStart = SourceMgr.getLogicalLoc(Range.getBegin());
241 unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
242
243 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(Range.getEnd());
244 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
245
246 if (EndLineNo < StartLineNo)
247 return;
248
249 if (LogicalStart.getFileID() != MainFileID ||
250 LogicalEnd.getFileID() != MainFileID)
251 return;
252
253 // Compute the column number of the end.
254 unsigned EndColNo = SourceMgr.getColumnNumber(LogicalEnd);
255 unsigned OldEndColNo = EndColNo;
256
257 if (EndColNo) {
258 // Add in the length of the token, so that we cover multi-char tokens.
259 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SourceMgr);
260 }
261
262 // Highlight the range. Make the span tag the outermost tag for the
263 // selected range.
264
265 SourceLocation E =
266 LogicalEnd.getFileLocWithOffset(OldEndColNo > EndColNo
267 ? -(OldEndColNo - EndColNo)
268 : EndColNo - OldEndColNo);
269
270 R.InsertCStrBefore(LogicalStart, "<span class=\"mrange\">");
271 R.InsertCStrAfter(E, "</span>");
272}