blob: 81cedba557788ae53292258d19f08fbb5cb1177a [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
Ted Kremenekad99dbf2008-11-03 22:31:48 +000014#include "clang/Driver/PathDiagnosticClients.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000015#include "clang/Analysis/PathDiagnostic.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000018#include "clang/Basic/SourceManager.h"
Ted Kremenek2e939812008-03-27 07:35:49 +000019#include "clang/Basic/FileManager.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000020#include "clang/Rewrite/Rewriter.h"
21#include "clang/Rewrite/HTMLRewrite.h"
22#include "clang/Lex/Lexer.h"
23#include "llvm/Support/Compiler.h"
24#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/Streams.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000027#include "llvm/System/Path.h"
28#include <fstream>
Ted Kremenek88f5cde2008-03-27 06:17:42 +000029
30using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// Boilerplate.
34//===----------------------------------------------------------------------===//
35
36namespace {
37
38class VISIBILITY_HIDDEN HTMLDiagnostics : public PathDiagnosticClient {
39 llvm::sys::Path Directory, FilePrefix;
40 bool createdDir, noDir;
Ted Kremenek47abe762008-04-16 16:39:56 +000041 Preprocessor* PP;
Ted Kremenek339b9c22008-04-17 22:31:54 +000042 PreprocessorFactory* PPF;
Ted Kremenek55851142008-04-22 16:15:03 +000043 std::vector<const PathDiagnostic*> BatchedDiags;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000044public:
Ted Kremenek339b9c22008-04-17 22:31:54 +000045 HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
46 PreprocessorFactory* ppf);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000047
Ted Kremenek55851142008-04-22 16:15:03 +000048 virtual ~HTMLDiagnostics();
Ted Kremenek88f5cde2008-03-27 06:17:42 +000049
Ted Kremenek55851142008-04-22 16:15:03 +000050 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000051
Chris Lattner2b2453a2009-01-17 06:22:33 +000052 void HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +000053 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Ted Kremenek33bd9422008-03-31 23:30:12 +000054
Chris Lattner2b2453a2009-01-17 06:22:33 +000055 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range);
Ted Kremenek55851142008-04-22 16:15:03 +000056
57 void ReportDiag(const PathDiagnostic& D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000058};
59
60} // end anonymous namespace
61
Ted Kremenek339b9c22008-04-17 22:31:54 +000062HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
63 PreprocessorFactory* ppf)
Ted Kremenek47abe762008-04-16 16:39:56 +000064 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenek339b9c22008-04-17 22:31:54 +000065 PP(pp), PPF(ppf) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000066
67 // All html files begin with "report"
68 FilePrefix.appendComponent("report");
69}
70
71PathDiagnosticClient*
Ted Kremenek339b9c22008-04-17 22:31:54 +000072clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
73 PreprocessorFactory* PPF) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000074
Ted Kremenek339b9c22008-04-17 22:31:54 +000075 return new HTMLDiagnostics(prefix, PP, PPF);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000076}
77
78//===----------------------------------------------------------------------===//
79// Report processing.
80//===----------------------------------------------------------------------===//
81
Ted Kremenek55851142008-04-22 16:15:03 +000082void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
83 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +000084 return;
85
Ted Kremenek55851142008-04-22 16:15:03 +000086 if (D->empty()) {
87 delete D;
88 return;
89 }
90
91 BatchedDiags.push_back(D);
92}
93
94HTMLDiagnostics::~HTMLDiagnostics() {
95
96 while (!BatchedDiags.empty()) {
97 const PathDiagnostic* D = BatchedDiags.back();
98 BatchedDiags.pop_back();
99 ReportDiag(*D);
100 delete D;
101 }
102}
103
104void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) {
105
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000106 // Create the HTML directory if it is missing.
107
108 if (!createdDir) {
109 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000110 std::string ErrorMsg;
111 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000112
113 if (!Directory.isDirectory()) {
114 llvm::cerr << "warning: could not create directory '"
Ted Kremenek344f7e32008-04-03 17:55:57 +0000115 << Directory.toString() << "'\n"
116 << "reason: " << ErrorMsg << '\n';
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000117
118 noDir = true;
119
120 return;
121 }
122 }
123
124 if (noDir)
125 return;
126
Chris Lattner4abb87e2009-01-16 22:59:51 +0000127 SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000128 FileID FID;
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000129
130 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000131 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
132 FullSourceLoc L = I->getLocation().getInstantiationLoc();
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000133
Chris Lattner2b2453a2009-01-17 06:22:33 +0000134 if (FID.isInvalid()) {
135 FID = SMgr.getCanonicalFileID(L);
136 } else if (SMgr.getCanonicalFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000137 return; // FIXME: Emit a warning?
138
139 // Check the source ranges.
140 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
141 RE=I->ranges_end(); RI!=RE; ++RI) {
142
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000143 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000144
145 if (!L.isFileID())
146 return; // FIXME: Emit a warning?
147
Chris Lattner2b2453a2009-01-17 06:22:33 +0000148 if (SMgr.getCanonicalFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000149 return; // FIXME: Emit a warning?
150
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000151 L = SMgr.getInstantiationLoc(RI->getEnd());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000152
153 if (!L.isFileID())
154 return; // FIXME: Emit a warning?
155
Chris Lattner2b2453a2009-01-17 06:22:33 +0000156 if (SMgr.getCanonicalFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000157 return; // FIXME: Emit a warning?
158 }
159 }
160
Chris Lattner2b2453a2009-01-17 06:22:33 +0000161 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000162 return; // FIXME: Emit a warning?
163
164 // Create a new rewriter to generate HTML.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000165 Rewriter R(SMgr);
166
167 // Process the path.
168
169 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000170 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000171
172 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
173 I!=E; ++I, --n) {
174
Chris Lattner2b2453a2009-01-17 06:22:33 +0000175 HandlePiece(R, FID, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000176 }
177
178 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000179
Chris Lattner2b2453a2009-01-17 06:22:33 +0000180 // unsigned FID = R.getSourceMgr().getMainFileID();
181 html::EscapeText(R, FID);
182 html::AddLineNumbers(R, FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000183
Ted Kremenek47abe762008-04-16 16:39:56 +0000184 // If we have a preprocessor, relex the file and syntax highlight.
185 // We might not have a preprocessor if we come from a deserialized AST file,
186 // for example.
187
Chris Lattner2b2453a2009-01-17 06:22:33 +0000188 if (PP) html::SyntaxHighlight(R, FID, *PP);
Ted Kremenek55851142008-04-22 16:15:03 +0000189
190 // FIXME: We eventually want to use PPF to create a fresh Preprocessor,
191 // once we have worked out the bugs.
192 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000193 // if (PPF) html::HighlightMacros(R, FID, *PPF);
Ted Kremenek55851142008-04-22 16:15:03 +0000194 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000195 if (PP) html::HighlightMacros(R, FID, *PP);
Ted Kremenek47abe762008-04-16 16:39:56 +0000196
Ted Kremenekb9476392008-04-02 20:44:16 +0000197 // Get the full directory name of the analyzed file.
198
Chris Lattner2b2453a2009-01-17 06:22:33 +0000199 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Ted Kremenek2e939812008-03-27 07:35:49 +0000200
Ted Kremenek7fc89572008-04-24 23:37:03 +0000201 // This is a cludge; basically we want to append either the full
202 // working directory if we have no directory information. This is
203 // a work in progress.
204
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000205 std::string DirName = "";
206
207 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
208 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
209 DirName = P.toString() + "/";
210 }
Ted Kremenekb9476392008-04-02 20:44:16 +0000211
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000212 // Add the name of the file as an <h1> tag.
213
Ted Kremenek2e939812008-03-27 07:35:49 +0000214 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000215 std::string s;
216 llvm::raw_string_ostream os(s);
Ted Kremenek2e939812008-03-27 07:35:49 +0000217
Ted Kremenek778246a2008-09-22 17:33:32 +0000218 os << "<!-- REPORTHEADER -->\n"
219 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000220 "<tr><td class=\"rowname\">File:</td><td>"
221 << html::EscapeText(DirName)
222 << html::EscapeText(Entry->getName())
223 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
224 "<a href=\"#EndPath\">line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000225 << (*D.rbegin()).getLocation().getInstantiationLineNumber()
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000226 << ", column "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000227 << (*D.rbegin()).getLocation().getInstantiationColumnNumber()
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000228 << "</a></td></tr>\n"
229 "<tr><td class=\"rowname\">Description:</td><td>"
Ted Kremenek072192b2008-04-30 23:47:44 +0000230 << D.getDescription() << "</td></tr>\n";
231
232 // Output any other meta data.
233
234 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
235 I!=E; ++I) {
236 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
237 }
238
Ted Kremenek778246a2008-09-22 17:33:32 +0000239 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
240 "<h3>Annotated Source Code</h3>\n";
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000241
Chris Lattner2b2453a2009-01-17 06:22:33 +0000242 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000243 }
244
Ted Kremenekb9476392008-04-02 20:44:16 +0000245 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000246
247 const std::string& BugDesc = D.getDescription();
248
249 if (!BugDesc.empty()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000250 std::string s;
251 llvm::raw_string_ostream os(s);
Ted Kremenek86b43812008-04-02 18:03:20 +0000252 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000253 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000254 }
255
Ted Kremenek8c036c72008-09-20 04:23:38 +0000256 const std::string& BugCategory = D.getCategory();
257
258 if (!BugCategory.empty()) {
259 std::string s;
260 llvm::raw_string_ostream os(s);
261 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000262 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek8c036c72008-09-20 04:23:38 +0000263 }
264
Ted Kremenekb9476392008-04-02 20:44:16 +0000265 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000266 std::string s;
267 llvm::raw_string_ostream os(s);
Ted Kremenek7fc89572008-04-24 23:37:03 +0000268 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000269 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000270 }
271
272 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000273 std::string s;
274 llvm::raw_string_ostream os(s);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000275 os << "\n<!-- BUGLINE "
276 << D.back()->getLocation().getInstantiationLineNumber() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000277 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000278 }
279
280 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000281 std::string s;
282 llvm::raw_string_ostream os(s);
Ted Kremenekb9476392008-04-02 20:44:16 +0000283 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000284 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000285 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000286
287 // Add CSS, header, and footer.
288
Chris Lattner2b2453a2009-01-17 06:22:33 +0000289 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000290
291 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000292 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000293
294 if (!Buf) {
295 llvm::cerr << "warning: no diagnostics generated for main file.\n";
296 return;
297 }
298
299 // Create the stream to write out the HTML.
300 std::ofstream os;
301
302 {
303 // Create a path for the target HTML file.
304 llvm::sys::Path F(FilePrefix);
305 F.makeUnique(false, NULL);
306
307 // Rename the file with an HTML extension.
308 llvm::sys::Path H(F);
309 H.appendSuffix("html");
310 F.renamePathOnDisk(H, NULL);
311
312 os.open(H.toString().c_str());
313
314 if (!os) {
315 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
316 return;
317 }
318 }
319
320 // Emit the HTML to disk.
321
Ted Kremenek7414dc02008-04-20 01:02:33 +0000322 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000323 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000324}
325
Chris Lattner2b2453a2009-01-17 06:22:33 +0000326void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000327 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000328 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000329
330 // For now, just draw a box above the line in question, and emit the
331 // warning.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000332 FullSourceLoc Pos = P.getLocation();
333
334 if (!Pos.isValid())
335 return;
336
Chris Lattner2b2453a2009-01-17 06:22:33 +0000337 SourceManager &SM = R.getSourceMgr();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000338 FullSourceLoc LPos = Pos.getInstantiationLoc();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000339 FileID FID = SM.getCanonicalFileID(LPos);
340 assert(&LPos.getManager() == &SM && "SourceManagers are different!");
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000341
Chris Lattner59ddeab2009-01-16 23:06:35 +0000342 if (SM.getCanonicalFileID(LPos) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000343 return;
344
Chris Lattner2b2453a2009-01-17 06:22:33 +0000345 const llvm::MemoryBuffer *Buf = SM.getBuffer(FID);
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000346 const char* FileStart = Buf->getBufferStart();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000347
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000348 // Compute the column number. Rewind from the current position to the start
349 // of the line.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000350 unsigned ColNo = LPos.getColumnNumber();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000351 const char *TokInstantiationPtr = LPos.getCharacterData();
352 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000353
354 // Only compute LineEnd if we display below a line.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000355 const char *LineEnd = TokInstantiationPtr;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000356
357 if (P.getDisplayHint() == PathDiagnosticPiece::Below) {
358 const char* FileEnd = Buf->getBufferEnd();
359
360 while (*LineEnd != '\n' && LineEnd != FileEnd)
361 ++LineEnd;
362 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000363
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000364 // Compute the margin offset by counting tabs and non-tabs.
365
366 unsigned PosNo = 0;
367
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000368 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000369 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000370
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000371 // Create the html for the message.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000372 {
373 // Get the string and determining its maximum substring.
374 const std::string& Msg = P.getString();
375 unsigned max_token = 0;
376 unsigned cnt = 0;
377 unsigned len = Msg.size();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000378
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000379 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
380 switch (*I) {
381 default:
382 ++cnt;
383 continue;
384 case ' ':
385 case '\t':
386 case '\n':
387 if (cnt > max_token) max_token = cnt;
388 cnt = 0;
389 }
390
391 if (cnt > max_token) max_token = cnt;
392
393 // Next, determine the approximate size of the message bubble in em.
394 unsigned em;
Ted Kremenekf91ce772008-10-24 21:17:16 +0000395 const unsigned max_line = 120;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000396
397 if (max_token >= max_line)
398 em = max_token / 2;
399 else {
400 unsigned characters = max_line;
401 unsigned lines = len / max_line;
402
403 if (lines > 0) {
404 for (; characters > max_token; --characters)
405 if (len / characters > lines) {
406 ++characters;
407 break;
408 }
409 }
410
411 em = characters / 2;
412 }
413
414 // Now generate the message bubble.
415 std::string s;
416 llvm::raw_string_ostream os(s);
417
418 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
419
420 if (num == max)
421 os << "EndPath";
422 else
423 os << "Path" << num;
424
425 os << "\" class=\"msg\" style=\"margin-left:" << PosNo << "ex";
426 if (em < max_line/2) os << "; max-width:" << em << "em";
427 os << "\">";
428
429 if (max > 1)
430 os << "<span class=\"PathIndex\">[" << num << "]</span> ";
431
432 os << html::EscapeText(Msg) << "</div></td></tr>";
433
434 // Insert the new html.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000435 unsigned DisplayPos;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000436 switch (P.getDisplayHint()) {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000437 default: assert(0 && "Unhandled hint.");
438 case PathDiagnosticPiece::Above:
439 DisplayPos = LineStart - FileStart;
440 break;
441 case PathDiagnosticPiece::Below:
442 DisplayPos = LineEnd - FileStart;
443 break;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000444 }
Chris Lattner2b2453a2009-01-17 06:22:33 +0000445
446 SourceLocation Loc =
447 SM.getLocForStartOfFile(FID).getFileLocWithOffset(DisplayPos);
448 R.InsertStrBefore(Loc, os.str());
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000449 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000450
451 // Now highlight the ranges.
452
453 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
454 I != E; ++I)
Chris Lattner2b2453a2009-01-17 06:22:33 +0000455 HighlightRange(R, FID, *I);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000456}
457
Chris Lattner2b2453a2009-01-17 06:22:33 +0000458void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000459 SourceRange Range) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000460
Ted Kremenek5dd00412008-04-14 21:06:04 +0000461 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000462
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000463 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
464 unsigned StartLineNo = SM.getLineNumber(InstantiationStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000465
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000466 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
467 unsigned EndLineNo = SM.getLineNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000468
469 if (EndLineNo < StartLineNo)
470 return;
471
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000472 if (SM.getCanonicalFileID(InstantiationStart) != BugFileID ||
473 SM.getCanonicalFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000474 return;
475
476 // Compute the column number of the end.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000477 unsigned EndColNo = SM.getColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000478 unsigned OldEndColNo = EndColNo;
479
480 if (EndColNo) {
481 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000482 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000483 }
484
485 // Highlight the range. Make the span tag the outermost tag for the
486 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000487
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000488 SourceLocation E =
489 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000490
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000491 html::HighlightRange(R, InstantiationStart, E,
Ted Kremenekb7478142008-04-17 18:37:23 +0000492 "<span class=\"mrange\">", "</span>");
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000493}