blob: 1561fc455d7c70f7036e6d2f8f86fc70e7d37048 [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 Kremeneka26ddab2009-01-27 01:53:39 +0000249 const std::string& BugType = D.getBugType();
250 if (!BugType.empty()) {
251 std::string s;
252 llvm::raw_string_ostream os(s);
253 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
254 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
255 }
256
Ted Kremenek8c036c72008-09-20 04:23:38 +0000257 const std::string& BugCategory = D.getCategory();
258
259 if (!BugCategory.empty()) {
260 std::string s;
261 llvm::raw_string_ostream os(s);
262 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000263 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek8c036c72008-09-20 04:23:38 +0000264 }
265
Ted Kremenekb9476392008-04-02 20:44:16 +0000266 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000267 std::string s;
268 llvm::raw_string_ostream os(s);
Ted Kremenek7fc89572008-04-24 23:37:03 +0000269 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\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);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000276 os << "\n<!-- BUGLINE "
277 << D.back()->getLocation().getInstantiationLineNumber() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000278 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000279 }
280
281 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000282 std::string s;
283 llvm::raw_string_ostream os(s);
Ted Kremenekb9476392008-04-02 20:44:16 +0000284 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000285 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000286 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000287
288 // Add CSS, header, and footer.
289
Chris Lattner2b2453a2009-01-17 06:22:33 +0000290 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000291
292 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000293 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000294
295 if (!Buf) {
296 llvm::cerr << "warning: no diagnostics generated for main file.\n";
297 return;
298 }
299
300 // Create the stream to write out the HTML.
301 std::ofstream os;
302
303 {
304 // Create a path for the target HTML file.
305 llvm::sys::Path F(FilePrefix);
306 F.makeUnique(false, NULL);
307
308 // Rename the file with an HTML extension.
309 llvm::sys::Path H(F);
310 H.appendSuffix("html");
311 F.renamePathOnDisk(H, NULL);
312
313 os.open(H.toString().c_str());
314
315 if (!os) {
316 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
317 return;
318 }
319 }
320
321 // Emit the HTML to disk.
322
Ted Kremenek7414dc02008-04-20 01:02:33 +0000323 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000324 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000325}
326
Chris Lattner2b2453a2009-01-17 06:22:33 +0000327void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000328 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000329 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000330
331 // For now, just draw a box above the line in question, and emit the
332 // warning.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000333 FullSourceLoc Pos = P.getLocation();
334
335 if (!Pos.isValid())
336 return;
337
Chris Lattner2b2453a2009-01-17 06:22:33 +0000338 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000339 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
340 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000341
Chris Lattner7da5aea2009-02-04 00:55:58 +0000342 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000343 return;
344
Chris Lattner7da5aea2009-02-04 00:55:58 +0000345 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
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.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000350 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
351 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000352 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 =
Chris Lattner7da5aea2009-02-04 00:55:58 +0000447 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000448 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 Lattner7da5aea2009-02-04 00:55:58 +0000455 HighlightRange(R, LPosInfo.first, *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());
Chris Lattner30fc9332009-02-04 01:06:56 +0000464 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000465
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000466 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000467 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000468
469 if (EndLineNo < StartLineNo)
470 return;
471
Chris Lattnera11d6172009-01-19 07:46:45 +0000472 if (SM.getFileID(InstantiationStart) != BugFileID ||
473 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000474 return;
475
476 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000477 unsigned EndColNo = SM.getInstantiationColumnNumber(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}