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