blob: f335396ec4d6f5d4d25a0d42f6ef2304a51e24a3 [file] [log] [blame]
Ted Kremenekfd75e312008-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 Kremenek21b88b72008-11-03 22:31:48 +000014#include "clang/Driver/PathDiagnosticClients.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000015#include "clang/Analysis/PathDiagnostic.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Ted Kremenekfd75e312008-03-27 06:17:42 +000018#include "clang/Basic/SourceManager.h"
Ted Kremenekdfb08d42008-03-27 07:35:49 +000019#include "clang/Basic/FileManager.h"
Ted Kremenekfd75e312008-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 Kremenek7b6f67b2008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenekfd75e312008-03-27 06:17:42 +000027#include "llvm/System/Path.h"
28#include <fstream>
Ted Kremenekfd75e312008-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 Kremenekfe82a542008-04-16 16:39:56 +000041 Preprocessor* PP;
Ted Kremenek4e9899f2008-04-17 22:31:54 +000042 PreprocessorFactory* PPF;
Ted Kremenekf309cf92008-04-22 16:15:03 +000043 std::vector<const PathDiagnostic*> BatchedDiags;
Ted Kremenekfd75e312008-03-27 06:17:42 +000044public:
Ted Kremenek4e9899f2008-04-17 22:31:54 +000045 HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
46 PreprocessorFactory* ppf);
Ted Kremenekfd75e312008-03-27 06:17:42 +000047
Ted Kremenekf309cf92008-04-22 16:15:03 +000048 virtual ~HTMLDiagnostics();
Ted Kremenekfd75e312008-03-27 06:17:42 +000049
Ted Kremenekf309cf92008-04-22 16:15:03 +000050 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Ted Kremenekfd75e312008-03-27 06:17:42 +000051
Ted Kremenek46ca64b2008-07-23 23:18:15 +000052 void HandlePiece(Rewriter& R, unsigned BugFileID,
53 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Ted Kremenek72879792008-03-31 23:30:12 +000054
Ted Kremenek46ca64b2008-07-23 23:18:15 +000055 void HighlightRange(Rewriter& R, unsigned BugFileID, SourceRange Range);
Ted Kremenekf309cf92008-04-22 16:15:03 +000056
57 void ReportDiag(const PathDiagnostic& D);
Ted Kremenekfd75e312008-03-27 06:17:42 +000058};
59
60} // end anonymous namespace
61
Ted Kremenek4e9899f2008-04-17 22:31:54 +000062HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
63 PreprocessorFactory* ppf)
Ted Kremenekfe82a542008-04-16 16:39:56 +000064 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenek4e9899f2008-04-17 22:31:54 +000065 PP(pp), PPF(ppf) {
Ted Kremenekfd75e312008-03-27 06:17:42 +000066
67 // All html files begin with "report"
68 FilePrefix.appendComponent("report");
69}
70
71PathDiagnosticClient*
Ted Kremenek4e9899f2008-04-17 22:31:54 +000072clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
73 PreprocessorFactory* PPF) {
Ted Kremenekfd75e312008-03-27 06:17:42 +000074
Ted Kremenek4e9899f2008-04-17 22:31:54 +000075 return new HTMLDiagnostics(prefix, PP, PPF);
Ted Kremenekfd75e312008-03-27 06:17:42 +000076}
77
78//===----------------------------------------------------------------------===//
79// Report processing.
80//===----------------------------------------------------------------------===//
81
Ted Kremenekf309cf92008-04-22 16:15:03 +000082void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
83 if (!D)
Ted Kremenekfd75e312008-03-27 06:17:42 +000084 return;
85
Ted Kremenekf309cf92008-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 Kremenekfd75e312008-03-27 06:17:42 +0000106 // Create the HTML directory if it is missing.
107
108 if (!createdDir) {
109 createdDir = true;
Ted Kremenek14369902008-04-03 17:55:57 +0000110 std::string ErrorMsg;
111 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Ted Kremenekfd75e312008-03-27 06:17:42 +0000112
113 if (!Directory.isDirectory()) {
114 llvm::cerr << "warning: could not create directory '"
Ted Kremenek14369902008-04-03 17:55:57 +0000115 << Directory.toString() << "'\n"
116 << "reason: " << ErrorMsg << '\n';
Ted Kremenekfd75e312008-03-27 06:17:42 +0000117
118 noDir = true;
119
120 return;
121 }
122 }
123
124 if (noDir)
125 return;
126
Chris Lattner6bca88b2009-01-16 22:59:51 +0000127 SourceManager &SMgr = D.begin()->getLocation().getManager();
Ted Kremenek46ca64b2008-07-23 23:18:15 +0000128 unsigned FileID = 0;
129 bool FileIDInitialized = false;
130
131 // Verify that the entire path is from the same FileID.
Chris Lattner18c8dc02009-01-16 07:36:28 +0000132 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
133 FullSourceLoc L = I->getLocation().getInstantiationLoc();
Ted Kremenek46ca64b2008-07-23 23:18:15 +0000134
Ted Kremenek46ca64b2008-07-23 23:18:15 +0000135 if (!FileIDInitialized) {
Chris Lattnera9bf63b2009-01-16 23:06:35 +0000136 FileID = SMgr.getCanonicalFileID(L);
Ted Kremenek46ca64b2008-07-23 23:18:15 +0000137 FileIDInitialized = true;
Chris Lattnera9bf63b2009-01-16 23:06:35 +0000138 } else if (SMgr.getCanonicalFileID(L) != FileID)
Ted Kremenek46ca64b2008-07-23 23:18:15 +0000139 return; // FIXME: Emit a warning?
140
141 // Check the source ranges.
142 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
143 RE=I->ranges_end(); RI!=RE; ++RI) {
144
Chris Lattner18c8dc02009-01-16 07:36:28 +0000145 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenek46ca64b2008-07-23 23:18:15 +0000146
147 if (!L.isFileID())
148 return; // FIXME: Emit a warning?
149
150 if (SMgr.getCanonicalFileID(L) != FileID)
151 return; // FIXME: Emit a warning?
152
Chris Lattner18c8dc02009-01-16 07:36:28 +0000153 L = SMgr.getInstantiationLoc(RI->getEnd());
Ted Kremenek46ca64b2008-07-23 23:18:15 +0000154
155 if (!L.isFileID())
156 return; // FIXME: Emit a warning?
157
158 if (SMgr.getCanonicalFileID(L) != FileID)
159 return; // FIXME: Emit a warning?
160 }
161 }
162
163 if (!FileIDInitialized)
164 return; // FIXME: Emit a warning?
165
166 // Create a new rewriter to generate HTML.
Ted Kremenekfd75e312008-03-27 06:17:42 +0000167 Rewriter R(SMgr);
168
169 // Process the path.
170
171 unsigned n = D.size();
Ted Kremenek72879792008-03-31 23:30:12 +0000172 unsigned max = n;
Ted Kremenekfd75e312008-03-27 06:17:42 +0000173
174 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
175 I!=E; ++I, --n) {
176
Ted Kremenek46ca64b2008-07-23 23:18:15 +0000177 HandlePiece(R, FileID, *I, n, max);
Ted Kremenekfd75e312008-03-27 06:17:42 +0000178 }
179
180 // Add line numbers, header, footer, etc.
Ted Kremenekdfb08d42008-03-27 07:35:49 +0000181
Ted Kremenek46ca64b2008-07-23 23:18:15 +0000182 // unsigned FileID = R.getSourceMgr().getMainFileID();
Ted Kremenekfd75e312008-03-27 06:17:42 +0000183 html::EscapeText(R, FileID);
184 html::AddLineNumbers(R, FileID);
185
Ted Kremenekfe82a542008-04-16 16:39:56 +0000186 // If we have a preprocessor, relex the file and syntax highlight.
187 // We might not have a preprocessor if we come from a deserialized AST file,
188 // for example.
189
Ted Kremenek4e9899f2008-04-17 22:31:54 +0000190 if (PP) html::SyntaxHighlight(R, FileID, *PP);
Ted Kremenekf309cf92008-04-22 16:15:03 +0000191
192 // FIXME: We eventually want to use PPF to create a fresh Preprocessor,
193 // once we have worked out the bugs.
194 //
195 // if (PPF) html::HighlightMacros(R, FileID, *PPF);
196 //
197 if (PP) html::HighlightMacros(R, FileID, *PP);
Ted Kremenekfe82a542008-04-16 16:39:56 +0000198
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000199 // Get the full directory name of the analyzed file.
200
201 const FileEntry* Entry = SMgr.getFileEntryForID(FileID);
Ted Kremenekdfb08d42008-03-27 07:35:49 +0000202
Ted Kremenek82163e42008-04-24 23:37:03 +0000203 // This is a cludge; basically we want to append either the full
204 // working directory if we have no directory information. This is
205 // a work in progress.
206
Ted Kremenek32280862008-05-02 22:04:53 +0000207 std::string DirName = "";
208
209 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
210 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
211 DirName = P.toString() + "/";
212 }
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000213
Ted Kremenek71e55fc2008-04-15 21:25:08 +0000214 // Add the name of the file as an <h1> tag.
215
Ted Kremenekdfb08d42008-03-27 07:35:49 +0000216 {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +0000217 std::string s;
218 llvm::raw_string_ostream os(s);
Ted Kremenekdfb08d42008-03-27 07:35:49 +0000219
Ted Kremenekbde1c822008-09-22 17:33:32 +0000220 os << "<!-- REPORTHEADER -->\n"
221 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek71e55fc2008-04-15 21:25:08 +0000222 "<tr><td class=\"rowname\">File:</td><td>"
223 << html::EscapeText(DirName)
224 << html::EscapeText(Entry->getName())
225 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
226 "<a href=\"#EndPath\">line "
Chris Lattner18c8dc02009-01-16 07:36:28 +0000227 << (*D.rbegin()).getLocation().getInstantiationLineNumber()
Ted Kremenek71e55fc2008-04-15 21:25:08 +0000228 << ", column "
Chris Lattner18c8dc02009-01-16 07:36:28 +0000229 << (*D.rbegin()).getLocation().getInstantiationColumnNumber()
Ted Kremenek71e55fc2008-04-15 21:25:08 +0000230 << "</a></td></tr>\n"
231 "<tr><td class=\"rowname\">Description:</td><td>"
Ted Kremenekfe30beb2008-04-30 23:47:44 +0000232 << D.getDescription() << "</td></tr>\n";
233
234 // Output any other meta data.
235
236 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
237 I!=E; ++I) {
238 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
239 }
240
Ted Kremenekbde1c822008-09-22 17:33:32 +0000241 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
242 "<h3>Annotated Source Code</h3>\n";
Ted Kremenek71e55fc2008-04-15 21:25:08 +0000243
Ted Kremenekdfb08d42008-03-27 07:35:49 +0000244 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenek44a70e12008-04-02 07:04:46 +0000245 }
246
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000247 // Embed meta-data tags.
Ted Kremenek44a70e12008-04-02 07:04:46 +0000248
249 const std::string& BugDesc = D.getDescription();
250
251 if (!BugDesc.empty()) {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +0000252 std::string s;
253 llvm::raw_string_ostream os(s);
Ted Kremenek1cae1f42008-04-02 18:03:20 +0000254 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Ted Kremenek44a70e12008-04-02 07:04:46 +0000255 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000256 }
257
Ted Kremenekab132342008-09-20 04:23:38 +0000258 const std::string& BugCategory = D.getCategory();
259
260 if (!BugCategory.empty()) {
261 std::string s;
262 llvm::raw_string_ostream os(s);
263 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
264 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
265 }
266
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000267 {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +0000268 std::string s;
269 llvm::raw_string_ostream os(s);
Ted Kremenek82163e42008-04-24 23:37:03 +0000270 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000271 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
272 }
273
274 {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +0000275 std::string s;
276 llvm::raw_string_ostream os(s);
Chris Lattner18c8dc02009-01-16 07:36:28 +0000277 os << "\n<!-- BUGLINE "
278 << D.back()->getLocation().getInstantiationLineNumber() << " -->\n";
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000279 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
280 }
281
282 {
Ted Kremenek7b6f67b2008-09-13 05:16:45 +0000283 std::string s;
284 llvm::raw_string_ostream os(s);
Ted Kremenekfe9b00f2008-04-02 20:44:16 +0000285 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
286 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
287 }
Ted Kremenekfd75e312008-03-27 06:17:42 +0000288
289 // Add CSS, header, and footer.
290
Ted Kremenek9ff57442008-07-07 18:31:05 +0000291 html::AddHeaderFooterInternalBuiltinCSS(R, FileID, Entry->getName());
Ted Kremenekfd75e312008-03-27 06:17:42 +0000292
293 // Get the rewrite buffer.
294 const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
295
296 if (!Buf) {
297 llvm::cerr << "warning: no diagnostics generated for main file.\n";
298 return;
299 }
300
301 // Create the stream to write out the HTML.
302 std::ofstream os;
303
304 {
305 // Create a path for the target HTML file.
306 llvm::sys::Path F(FilePrefix);
307 F.makeUnique(false, NULL);
308
309 // Rename the file with an HTML extension.
310 llvm::sys::Path H(F);
311 H.appendSuffix("html");
312 F.renamePathOnDisk(H, NULL);
313
314 os.open(H.toString().c_str());
315
316 if (!os) {
317 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
318 return;
319 }
320 }
321
322 // Emit the HTML to disk.
323
Ted Kremenek50d7cf42008-04-20 01:02:33 +0000324 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenek88aeff92008-04-08 22:37:58 +0000325 os << *I;
Ted Kremenekfd75e312008-03-27 06:17:42 +0000326}
327
Ted Kremenek46ca64b2008-07-23 23:18:15 +0000328void HTMLDiagnostics::HandlePiece(Rewriter& R, unsigned BugFileID,
Ted Kremenekfd75e312008-03-27 06:17:42 +0000329 const PathDiagnosticPiece& P,
Ted Kremenek72879792008-03-31 23:30:12 +0000330 unsigned num, unsigned max) {
Ted Kremenekfd75e312008-03-27 06:17:42 +0000331
332 // For now, just draw a box above the line in question, and emit the
333 // warning.
334
335 FullSourceLoc Pos = P.getLocation();
336
337 if (!Pos.isValid())
338 return;
339
340 SourceManager& SM = R.getSourceMgr();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000341 FullSourceLoc LPos = Pos.getInstantiationLoc();
Chris Lattnera9bf63b2009-01-16 23:06:35 +0000342 unsigned FileID = SM.getCanonicalFileID(LPos);
Ted Kremenek11101932008-05-06 23:42:18 +0000343
Ted Kremenekfd75e312008-03-27 06:17:42 +0000344 assert (&LPos.getManager() == &SM && "SourceManagers are different!");
345
Chris Lattnera9bf63b2009-01-16 23:06:35 +0000346 if (SM.getCanonicalFileID(LPos) != BugFileID)
Ted Kremenekfd75e312008-03-27 06:17:42 +0000347 return;
348
Ted Kremenek11101932008-05-06 23:42:18 +0000349 const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
350 const char* FileStart = Buf->getBufferStart();
Ted Kremenek11101932008-05-06 23:42:18 +0000351
Ted Kremenekfd75e312008-03-27 06:17:42 +0000352 // Compute the column number. Rewind from the current position to the start
353 // of the line.
Ted Kremenekfd75e312008-03-27 06:17:42 +0000354 unsigned ColNo = LPos.getColumnNumber();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000355 const char *TokInstantiationPtr = LPos.getCharacterData();
356 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenek11101932008-05-06 23:42:18 +0000357
358 // Only compute LineEnd if we display below a line.
Chris Lattner18c8dc02009-01-16 07:36:28 +0000359 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek11101932008-05-06 23:42:18 +0000360
361 if (P.getDisplayHint() == PathDiagnosticPiece::Below) {
362 const char* FileEnd = Buf->getBufferEnd();
363
364 while (*LineEnd != '\n' && LineEnd != FileEnd)
365 ++LineEnd;
366 }
Ted Kremenekfd75e312008-03-27 06:17:42 +0000367
Ted Kremenekdbdb01e2008-03-31 21:40:14 +0000368 // Compute the margin offset by counting tabs and non-tabs.
369
370 unsigned PosNo = 0;
371
Chris Lattner18c8dc02009-01-16 07:36:28 +0000372 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenek11101932008-05-06 23:42:18 +0000373 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenekdbdb01e2008-03-31 21:40:14 +0000374
Ted Kremenekfd75e312008-03-27 06:17:42 +0000375 // Create the html for the message.
Ted Kremenek77892ac2008-09-21 18:52:59 +0000376 {
377 // Get the string and determining its maximum substring.
378 const std::string& Msg = P.getString();
379 unsigned max_token = 0;
380 unsigned cnt = 0;
381 unsigned len = Msg.size();
Ted Kremenek11101932008-05-06 23:42:18 +0000382
Ted Kremenek77892ac2008-09-21 18:52:59 +0000383 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
384 switch (*I) {
385 default:
386 ++cnt;
387 continue;
388 case ' ':
389 case '\t':
390 case '\n':
391 if (cnt > max_token) max_token = cnt;
392 cnt = 0;
393 }
394
395 if (cnt > max_token) max_token = cnt;
396
397 // Next, determine the approximate size of the message bubble in em.
398 unsigned em;
Ted Kremenek27f21262008-10-24 21:17:16 +0000399 const unsigned max_line = 120;
Ted Kremenek77892ac2008-09-21 18:52:59 +0000400
401 if (max_token >= max_line)
402 em = max_token / 2;
403 else {
404 unsigned characters = max_line;
405 unsigned lines = len / max_line;
406
407 if (lines > 0) {
408 for (; characters > max_token; --characters)
409 if (len / characters > lines) {
410 ++characters;
411 break;
412 }
413 }
414
415 em = characters / 2;
416 }
417
418 // Now generate the message bubble.
419 std::string s;
420 llvm::raw_string_ostream os(s);
421
422 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
423
424 if (num == max)
425 os << "EndPath";
426 else
427 os << "Path" << num;
428
429 os << "\" class=\"msg\" style=\"margin-left:" << PosNo << "ex";
430 if (em < max_line/2) os << "; max-width:" << em << "em";
431 os << "\">";
432
433 if (max > 1)
434 os << "<span class=\"PathIndex\">[" << num << "]</span> ";
435
436 os << html::EscapeText(Msg) << "</div></td></tr>";
437
438 // Insert the new html.
439 unsigned DisplayPos = 0;
440
441 switch (P.getDisplayHint()) {
442 case PathDiagnosticPiece::Above:
443 DisplayPos = LineStart - FileStart;
444 break;
445 case PathDiagnosticPiece::Below:
446 DisplayPos = LineEnd - FileStart;
447 break;
448 default:
449 assert (false && "Unhandled hint.");
450 }
451
452 R.InsertStrBefore(SourceLocation::getFileLoc(FileID, DisplayPos), os.str());
453 }
Ted Kremenekfd75e312008-03-27 06:17:42 +0000454
455 // Now highlight the ranges.
456
457 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
458 I != E; ++I)
Ted Kremenek46ca64b2008-07-23 23:18:15 +0000459 HighlightRange(R, FileID, *I);
Ted Kremenekfd75e312008-03-27 06:17:42 +0000460}
461
Ted Kremenek46ca64b2008-07-23 23:18:15 +0000462void HTMLDiagnostics::HighlightRange(Rewriter& R, unsigned BugFileID,
463 SourceRange Range) {
Ted Kremenekfd75e312008-03-27 06:17:42 +0000464
Ted Kremeneke4a9de12008-04-14 21:06:04 +0000465 SourceManager& SM = R.getSourceMgr();
Ted Kremenekfd75e312008-03-27 06:17:42 +0000466
Chris Lattner18c8dc02009-01-16 07:36:28 +0000467 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
468 unsigned StartLineNo = SM.getLineNumber(InstantiationStart);
Ted Kremenekfd75e312008-03-27 06:17:42 +0000469
Chris Lattner18c8dc02009-01-16 07:36:28 +0000470 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
471 unsigned EndLineNo = SM.getLineNumber(InstantiationEnd);
Ted Kremenekfd75e312008-03-27 06:17:42 +0000472
473 if (EndLineNo < StartLineNo)
474 return;
475
Chris Lattner18c8dc02009-01-16 07:36:28 +0000476 if (SM.getCanonicalFileID(InstantiationStart) != BugFileID ||
477 SM.getCanonicalFileID(InstantiationEnd) != BugFileID)
Ted Kremenekfd75e312008-03-27 06:17:42 +0000478 return;
479
480 // Compute the column number of the end.
Chris Lattner18c8dc02009-01-16 07:36:28 +0000481 unsigned EndColNo = SM.getColumnNumber(InstantiationEnd);
Ted Kremenekfd75e312008-03-27 06:17:42 +0000482 unsigned OldEndColNo = EndColNo;
483
484 if (EndColNo) {
485 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek61de3a32008-04-18 05:01:33 +0000486 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenekfd75e312008-03-27 06:17:42 +0000487 }
488
489 // Highlight the range. Make the span tag the outermost tag for the
490 // selected range.
Ted Kremenek352bc0c2008-04-08 21:29:14 +0000491
Chris Lattner18c8dc02009-01-16 07:36:28 +0000492 SourceLocation E =
493 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenek4cad9d92008-04-17 18:37:23 +0000494
Chris Lattner18c8dc02009-01-16 07:36:28 +0000495 html::HighlightRange(R, InstantiationStart, E,
Ted Kremenek4cad9d92008-04-17 18:37:23 +0000496 "<span class=\"mrange\">", "</span>");
Ted Kremenekfd75e312008-03-27 06:17:42 +0000497}