blob: 93af22376d424385eca26fc50e0460e4bafdd5f4 [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;
118 const FileEntry* Entry = SMgr.getFileEntryForID(FileID);
119
Ted Kremenek053ef592008-03-27 17:15:29 +0000120 os << "<h1>" << html::EscapeText(Entry->getDir()->getName())
121 << "/" << html::EscapeText(Entry->getName()) << "</h1>\n";
Ted Kremenek2e939812008-03-27 07:35:49 +0000122
123 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
124 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000125
126 // Add CSS, header, and footer.
127
128 html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
129
130 // Get the rewrite buffer.
131 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
132
133 if (!Buf) {
134 llvm::cerr << "warning: no diagnostics generated for main file.\n";
135 return;
136 }
137
138 // Create the stream to write out the HTML.
139 std::ofstream os;
140
141 {
142 // Create a path for the target HTML file.
143 llvm::sys::Path F(FilePrefix);
144 F.makeUnique(false, NULL);
145
146 // Rename the file with an HTML extension.
147 llvm::sys::Path H(F);
148 H.appendSuffix("html");
149 F.renamePathOnDisk(H, NULL);
150
151 os.open(H.toString().c_str());
152
153 if (!os) {
154 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
155 return;
156 }
157 }
158
159 // Emit the HTML to disk.
160
161 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
162 os << *I;
163}
164
165void HTMLDiagnostics::HandlePiece(Rewriter& R,
166 const PathDiagnosticPiece& P,
167 unsigned num) {
168
169 // For now, just draw a box above the line in question, and emit the
170 // warning.
171
172 FullSourceLoc Pos = P.getLocation();
173
174 if (!Pos.isValid())
175 return;
176
177 SourceManager& SM = R.getSourceMgr();
178 FullSourceLoc LPos = Pos.getLogicalLoc();
179 unsigned FileID = LPos.getLocation().getFileID();
180
181 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
182
183 unsigned MainFileID = SM.getMainFileID();
184
185 if (FileID != MainFileID)
186 return;
187
188 // Compute the column number. Rewind from the current position to the start
189 // of the line.
190
191 unsigned ColNo = LPos.getColumnNumber();
192 const char *TokLogicalPtr = LPos.getCharacterData();
193 const char *LineStart = TokLogicalPtr-ColNo;
194
195 // Create the html for the message.
196
197 std::ostringstream os;
198
199 os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
200 << "<div class=\"msg\" style=\"margin-left:"
201 << ColNo << "ex\">";
202
Ted Kremenek053ef592008-03-27 17:15:29 +0000203 os << html::EscapeText(P.getString()) << "</div></td></tr>";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000204
205 // Insert the new html.
206
207 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
208 const char* FileStart = Buf->getBufferStart();
209
210 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart),
211 os.str());
212
213 // Now highlight the ranges.
214
215 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
216 I != E; ++I)
217 HighlightRange(R, *I, MainFileID);
218}
219
220void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range,
221 unsigned MainFileID) {
222
223 SourceManager& SourceMgr = R.getSourceMgr();
224
225 SourceLocation LogicalStart = SourceMgr.getLogicalLoc(Range.getBegin());
226 unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
227
228 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(Range.getEnd());
229 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
230
231 if (EndLineNo < StartLineNo)
232 return;
233
234 if (LogicalStart.getFileID() != MainFileID ||
235 LogicalEnd.getFileID() != MainFileID)
236 return;
237
238 // Compute the column number of the end.
239 unsigned EndColNo = SourceMgr.getColumnNumber(LogicalEnd);
240 unsigned OldEndColNo = EndColNo;
241
242 if (EndColNo) {
243 // Add in the length of the token, so that we cover multi-char tokens.
244 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SourceMgr);
245 }
246
247 // Highlight the range. Make the span tag the outermost tag for the
248 // selected range.
249
250 SourceLocation E =
251 LogicalEnd.getFileLocWithOffset(OldEndColNo > EndColNo
252 ? -(OldEndColNo - EndColNo)
253 : EndColNo - OldEndColNo);
254
255 R.InsertCStrBefore(LogicalStart, "<span class=\"mrange\">");
256 R.InsertCStrAfter(E, "</span>");
257}