blob: 2772cf34226838b8a84c2cdad029673b7c1703a7 [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 +000029using 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;
Ted Kremenek47abe762008-04-16 16:39:56 +000040 Preprocessor* PP;
Ted Kremenek339b9c22008-04-17 22:31:54 +000041 PreprocessorFactory* PPF;
Ted Kremenek55851142008-04-22 16:15:03 +000042 std::vector<const PathDiagnostic*> BatchedDiags;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000043public:
Ted Kremenek339b9c22008-04-17 22:31:54 +000044 HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
45 PreprocessorFactory* ppf);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000046
Ted Kremenek55851142008-04-22 16:15:03 +000047 virtual ~HTMLDiagnostics();
Ted Kremenek88f5cde2008-03-27 06:17:42 +000048
Ted Kremenek55851142008-04-22 16:15:03 +000049 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000050
Chris Lattner2b2453a2009-01-17 06:22:33 +000051 void HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +000052 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Ted Kremenek33bd9422008-03-31 23:30:12 +000053
Chris Lattner2b2453a2009-01-17 06:22:33 +000054 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range);
Ted Kremenek55851142008-04-22 16:15:03 +000055
56 void ReportDiag(const PathDiagnostic& D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000057};
58
59} // end anonymous namespace
60
Ted Kremenek339b9c22008-04-17 22:31:54 +000061HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
62 PreprocessorFactory* ppf)
Ted Kremenek47abe762008-04-16 16:39:56 +000063 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenek339b9c22008-04-17 22:31:54 +000064 PP(pp), PPF(ppf) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000065
66 // All html files begin with "report"
67 FilePrefix.appendComponent("report");
68}
69
70PathDiagnosticClient*
Ted Kremenek339b9c22008-04-17 22:31:54 +000071clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
72 PreprocessorFactory* PPF) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000073
Ted Kremenek339b9c22008-04-17 22:31:54 +000074 return new HTMLDiagnostics(prefix, PP, PPF);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000075}
76
77//===----------------------------------------------------------------------===//
78// Report processing.
79//===----------------------------------------------------------------------===//
80
Ted Kremenek55851142008-04-22 16:15:03 +000081void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
82 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +000083 return;
84
Ted Kremenek55851142008-04-22 16:15:03 +000085 if (D->empty()) {
86 delete D;
87 return;
88 }
89
90 BatchedDiags.push_back(D);
91}
92
93HTMLDiagnostics::~HTMLDiagnostics() {
94
95 while (!BatchedDiags.empty()) {
96 const PathDiagnostic* D = BatchedDiags.back();
97 BatchedDiags.pop_back();
98 ReportDiag(*D);
99 delete D;
100 }
101}
102
103void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) {
104
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000105 // Create the HTML directory if it is missing.
106
107 if (!createdDir) {
108 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000109 std::string ErrorMsg;
110 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000111
112 if (!Directory.isDirectory()) {
113 llvm::cerr << "warning: could not create directory '"
Ted Kremenek344f7e32008-04-03 17:55:57 +0000114 << Directory.toString() << "'\n"
115 << "reason: " << ErrorMsg << '\n';
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000116
117 noDir = true;
118
119 return;
120 }
121 }
122
123 if (noDir)
124 return;
125
Chris Lattner4abb87e2009-01-16 22:59:51 +0000126 SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000127 FileID FID;
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000128
129 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000130 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
131 FullSourceLoc L = I->getLocation().getInstantiationLoc();
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000132
Chris Lattner2b2453a2009-01-17 06:22:33 +0000133 if (FID.isInvalid()) {
Chris Lattnera11d6172009-01-19 07:46:45 +0000134 FID = SMgr.getFileID(L);
135 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000136 return; // FIXME: Emit a warning?
137
138 // Check the source ranges.
139 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
140 RE=I->ranges_end(); RI!=RE; ++RI) {
141
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000142 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000143
Chris Lattnera11d6172009-01-19 07:46:45 +0000144 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000145 return; // FIXME: Emit a warning?
146
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000147 L = SMgr.getInstantiationLoc(RI->getEnd());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000148
Chris Lattnera11d6172009-01-19 07:46:45 +0000149 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000150 return; // FIXME: Emit a warning?
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000151 }
152 }
153
Chris Lattner2b2453a2009-01-17 06:22:33 +0000154 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000155 return; // FIXME: Emit a warning?
156
157 // Create a new rewriter to generate HTML.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000158 Rewriter R(SMgr);
159
160 // Process the path.
161
162 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000163 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000164
165 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
166 I!=E; ++I, --n) {
167
Chris Lattner2b2453a2009-01-17 06:22:33 +0000168 HandlePiece(R, FID, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000169 }
170
171 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000172
Chris Lattner2b2453a2009-01-17 06:22:33 +0000173 // unsigned FID = R.getSourceMgr().getMainFileID();
174 html::EscapeText(R, FID);
175 html::AddLineNumbers(R, FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000176
Ted Kremenek47abe762008-04-16 16:39:56 +0000177 // If we have a preprocessor, relex the file and syntax highlight.
178 // We might not have a preprocessor if we come from a deserialized AST file,
179 // for example.
180
Chris Lattner2b2453a2009-01-17 06:22:33 +0000181 if (PP) html::SyntaxHighlight(R, FID, *PP);
Ted Kremenek55851142008-04-22 16:15:03 +0000182
183 // FIXME: We eventually want to use PPF to create a fresh Preprocessor,
184 // once we have worked out the bugs.
185 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000186 // if (PPF) html::HighlightMacros(R, FID, *PPF);
Ted Kremenek55851142008-04-22 16:15:03 +0000187 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000188 if (PP) html::HighlightMacros(R, FID, *PP);
Ted Kremenek47abe762008-04-16 16:39:56 +0000189
Ted Kremenekb9476392008-04-02 20:44:16 +0000190 // Get the full directory name of the analyzed file.
191
Chris Lattner2b2453a2009-01-17 06:22:33 +0000192 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Ted Kremenek2e939812008-03-27 07:35:49 +0000193
Ted Kremenek7fc89572008-04-24 23:37:03 +0000194 // This is a cludge; basically we want to append either the full
195 // working directory if we have no directory information. This is
196 // a work in progress.
197
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000198 std::string DirName = "";
199
200 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
201 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
202 DirName = P.toString() + "/";
203 }
Ted Kremenekb9476392008-04-02 20:44:16 +0000204
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000205 // Add the name of the file as an <h1> tag.
206
Ted Kremenek2e939812008-03-27 07:35:49 +0000207 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000208 std::string s;
209 llvm::raw_string_ostream os(s);
Ted Kremenek2e939812008-03-27 07:35:49 +0000210
Ted Kremenek778246a2008-09-22 17:33:32 +0000211 os << "<!-- REPORTHEADER -->\n"
212 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000213 "<tr><td class=\"rowname\">File:</td><td>"
214 << html::EscapeText(DirName)
215 << html::EscapeText(Entry->getName())
216 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
217 "<a href=\"#EndPath\">line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000218 << (*D.rbegin()).getLocation().getInstantiationLineNumber()
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000219 << ", column "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000220 << (*D.rbegin()).getLocation().getInstantiationColumnNumber()
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000221 << "</a></td></tr>\n"
222 "<tr><td class=\"rowname\">Description:</td><td>"
Ted Kremenek072192b2008-04-30 23:47:44 +0000223 << D.getDescription() << "</td></tr>\n";
224
225 // Output any other meta data.
226
227 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
228 I!=E; ++I) {
229 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
230 }
231
Ted Kremenek778246a2008-09-22 17:33:32 +0000232 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
233 "<h3>Annotated Source Code</h3>\n";
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000234
Chris Lattner2b2453a2009-01-17 06:22:33 +0000235 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000236 }
237
Ted Kremenekb9476392008-04-02 20:44:16 +0000238 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000239
240 const std::string& BugDesc = D.getDescription();
241
242 if (!BugDesc.empty()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000243 std::string s;
244 llvm::raw_string_ostream os(s);
Ted Kremenek86b43812008-04-02 18:03:20 +0000245 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000246 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000247 }
248
Ted Kremenek8c036c72008-09-20 04:23:38 +0000249 const std::string& BugCategory = D.getCategory();
250
251 if (!BugCategory.empty()) {
252 std::string s;
253 llvm::raw_string_ostream os(s);
254 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000255 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek8c036c72008-09-20 04:23:38 +0000256 }
257
Ted Kremenekb9476392008-04-02 20:44:16 +0000258 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000259 std::string s;
260 llvm::raw_string_ostream os(s);
Ted Kremenek7fc89572008-04-24 23:37:03 +0000261 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000262 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000263 }
264
265 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000266 std::string s;
267 llvm::raw_string_ostream os(s);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000268 os << "\n<!-- BUGLINE "
269 << D.back()->getLocation().getInstantiationLineNumber() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000270 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000271 }
272
273 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000274 std::string s;
275 llvm::raw_string_ostream os(s);
Ted Kremenekb9476392008-04-02 20:44:16 +0000276 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000277 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000278 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000279
280 // Add CSS, header, and footer.
281
Chris Lattner2b2453a2009-01-17 06:22:33 +0000282 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000283
284 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000285 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000286
287 if (!Buf) {
288 llvm::cerr << "warning: no diagnostics generated for main file.\n";
289 return;
290 }
291
292 // Create the stream to write out the HTML.
293 std::ofstream os;
294
295 {
296 // Create a path for the target HTML file.
297 llvm::sys::Path F(FilePrefix);
298 F.makeUnique(false, NULL);
299
300 // Rename the file with an HTML extension.
301 llvm::sys::Path H(F);
302 H.appendSuffix("html");
303 F.renamePathOnDisk(H, NULL);
304
305 os.open(H.toString().c_str());
306
307 if (!os) {
308 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
309 return;
310 }
311 }
312
313 // Emit the HTML to disk.
314
Ted Kremenek7414dc02008-04-20 01:02:33 +0000315 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000316 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000317}
318
Chris Lattner2b2453a2009-01-17 06:22:33 +0000319void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000320 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000321 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000322
323 // For now, just draw a box above the line in question, and emit the
324 // warning.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000325 FullSourceLoc Pos = P.getLocation();
326
327 if (!Pos.isValid())
328 return;
329
Chris Lattner2b2453a2009-01-17 06:22:33 +0000330 SourceManager &SM = R.getSourceMgr();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000331 FullSourceLoc LPos = Pos.getInstantiationLoc();
Chris Lattnera11d6172009-01-19 07:46:45 +0000332 FileID FID = SM.getFileID(LPos);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000333 assert(&LPos.getManager() == &SM && "SourceManagers are different!");
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000334
Chris Lattnera11d6172009-01-19 07:46:45 +0000335 if (SM.getFileID(LPos) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000336 return;
337
Chris Lattner2b2453a2009-01-17 06:22:33 +0000338 const llvm::MemoryBuffer *Buf = SM.getBuffer(FID);
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000339 const char* FileStart = Buf->getBufferStart();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000340
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000341 // Compute the column number. Rewind from the current position to the start
342 // of the line.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000343 unsigned ColNo = LPos.getColumnNumber();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000344 const char *TokInstantiationPtr = LPos.getCharacterData();
345 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000346
347 // Only compute LineEnd if we display below a line.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000348 const char *LineEnd = TokInstantiationPtr;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000349
350 if (P.getDisplayHint() == PathDiagnosticPiece::Below) {
351 const char* FileEnd = Buf->getBufferEnd();
352
353 while (*LineEnd != '\n' && LineEnd != FileEnd)
354 ++LineEnd;
355 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000356
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000357 // Compute the margin offset by counting tabs and non-tabs.
358
359 unsigned PosNo = 0;
360
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000361 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000362 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000363
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000364 // Create the html for the message.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000365 {
366 // Get the string and determining its maximum substring.
367 const std::string& Msg = P.getString();
368 unsigned max_token = 0;
369 unsigned cnt = 0;
370 unsigned len = Msg.size();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000371
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000372 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
373 switch (*I) {
374 default:
375 ++cnt;
376 continue;
377 case ' ':
378 case '\t':
379 case '\n':
380 if (cnt > max_token) max_token = cnt;
381 cnt = 0;
382 }
383
384 if (cnt > max_token) max_token = cnt;
385
386 // Next, determine the approximate size of the message bubble in em.
387 unsigned em;
Ted Kremenekf91ce772008-10-24 21:17:16 +0000388 const unsigned max_line = 120;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000389
390 if (max_token >= max_line)
391 em = max_token / 2;
392 else {
393 unsigned characters = max_line;
394 unsigned lines = len / max_line;
395
396 if (lines > 0) {
397 for (; characters > max_token; --characters)
398 if (len / characters > lines) {
399 ++characters;
400 break;
401 }
402 }
403
404 em = characters / 2;
405 }
406
407 // Now generate the message bubble.
408 std::string s;
409 llvm::raw_string_ostream os(s);
410
411 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
412
413 if (num == max)
414 os << "EndPath";
415 else
416 os << "Path" << num;
417
418 os << "\" class=\"msg\" style=\"margin-left:" << PosNo << "ex";
419 if (em < max_line/2) os << "; max-width:" << em << "em";
420 os << "\">";
421
422 if (max > 1)
423 os << "<span class=\"PathIndex\">[" << num << "]</span> ";
424
425 os << html::EscapeText(Msg) << "</div></td></tr>";
426
427 // Insert the new html.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000428 unsigned DisplayPos;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000429 switch (P.getDisplayHint()) {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000430 default: assert(0 && "Unhandled hint.");
431 case PathDiagnosticPiece::Above:
432 DisplayPos = LineStart - FileStart;
433 break;
434 case PathDiagnosticPiece::Below:
435 DisplayPos = LineEnd - FileStart;
436 break;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000437 }
Chris Lattner2b2453a2009-01-17 06:22:33 +0000438
439 SourceLocation Loc =
440 SM.getLocForStartOfFile(FID).getFileLocWithOffset(DisplayPos);
441 R.InsertStrBefore(Loc, os.str());
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000442 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000443
444 // Now highlight the ranges.
445
446 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
447 I != E; ++I)
Chris Lattner2b2453a2009-01-17 06:22:33 +0000448 HighlightRange(R, FID, *I);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000449}
450
Chris Lattner2b2453a2009-01-17 06:22:33 +0000451void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000452 SourceRange Range) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000453
Ted Kremenek5dd00412008-04-14 21:06:04 +0000454 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000455
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000456 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
457 unsigned StartLineNo = SM.getLineNumber(InstantiationStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000458
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000459 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
460 unsigned EndLineNo = SM.getLineNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000461
462 if (EndLineNo < StartLineNo)
463 return;
464
Chris Lattnera11d6172009-01-19 07:46:45 +0000465 if (SM.getFileID(InstantiationStart) != BugFileID ||
466 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000467 return;
468
469 // Compute the column number of the end.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000470 unsigned EndColNo = SM.getColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000471 unsigned OldEndColNo = EndColNo;
472
473 if (EndColNo) {
474 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000475 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000476 }
477
478 // Highlight the range. Make the span tag the outermost tag for the
479 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000480
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000481 SourceLocation E =
482 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000483
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000484 html::HighlightRange(R, InstantiationStart, E,
Ted Kremenekb7478142008-04-17 18:37:23 +0000485 "<span class=\"mrange\">", "</span>");
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000486}