blob: 02b327db2ad15bad9b39dab8401cf348ebc36d1b [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
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000014#include "clang/Frontend/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
Douglas Gregor4b2d3f72009-02-26 21:00:50 +000054 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
55 const char *HighlightStart = "<span class=\"mrange\">",
56 const char *HighlightEnd = "</span>");
Ted Kremenek55851142008-04-22 16:15:03 +000057
58 void ReportDiag(const PathDiagnostic& D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000059};
60
61} // end anonymous namespace
62
Ted Kremenek339b9c22008-04-17 22:31:54 +000063HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
64 PreprocessorFactory* ppf)
Ted Kremenek47abe762008-04-16 16:39:56 +000065 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenek339b9c22008-04-17 22:31:54 +000066 PP(pp), PPF(ppf) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000067
68 // All html files begin with "report"
69 FilePrefix.appendComponent("report");
70}
71
72PathDiagnosticClient*
Ted Kremenek339b9c22008-04-17 22:31:54 +000073clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
74 PreprocessorFactory* PPF) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000075
Ted Kremenek339b9c22008-04-17 22:31:54 +000076 return new HTMLDiagnostics(prefix, PP, PPF);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000077}
78
79//===----------------------------------------------------------------------===//
80// Report processing.
81//===----------------------------------------------------------------------===//
82
Ted Kremenek55851142008-04-22 16:15:03 +000083void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
84 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +000085 return;
86
Ted Kremenek55851142008-04-22 16:15:03 +000087 if (D->empty()) {
88 delete D;
89 return;
90 }
91
92 BatchedDiags.push_back(D);
93}
94
95HTMLDiagnostics::~HTMLDiagnostics() {
96
97 while (!BatchedDiags.empty()) {
98 const PathDiagnostic* D = BatchedDiags.back();
99 BatchedDiags.pop_back();
100 ReportDiag(*D);
101 delete D;
102 }
103}
104
105void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) {
106
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000107 // Create the HTML directory if it is missing.
108
109 if (!createdDir) {
110 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000111 std::string ErrorMsg;
112 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000113
114 if (!Directory.isDirectory()) {
115 llvm::cerr << "warning: could not create directory '"
Ted Kremenek344f7e32008-04-03 17:55:57 +0000116 << Directory.toString() << "'\n"
117 << "reason: " << ErrorMsg << '\n';
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000118
119 noDir = true;
120
121 return;
122 }
123 }
124
125 if (noDir)
126 return;
127
Chris Lattner4abb87e2009-01-16 22:59:51 +0000128 SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000129 FileID FID;
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000130
131 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-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 Kremenekfd8fc4e2008-07-23 23:18:15 +0000134
Chris Lattner2b2453a2009-01-17 06:22:33 +0000135 if (FID.isInvalid()) {
Chris Lattnera11d6172009-01-19 07:46:45 +0000136 FID = SMgr.getFileID(L);
137 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000138 return; // FIXME: Emit a warning?
139
140 // Check the source ranges.
141 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
142 RE=I->ranges_end(); RI!=RE; ++RI) {
143
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000144 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000145
Chris Lattnera11d6172009-01-19 07:46:45 +0000146 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000147 return; // FIXME: Emit a warning?
148
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000149 L = SMgr.getInstantiationLoc(RI->getEnd());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000150
Chris Lattnera11d6172009-01-19 07:46:45 +0000151 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000152 return; // FIXME: Emit a warning?
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000153 }
154 }
155
Chris Lattner2b2453a2009-01-17 06:22:33 +0000156 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000157 return; // FIXME: Emit a warning?
158
159 // Create a new rewriter to generate HTML.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000160 Rewriter R(SMgr);
161
Ted Kremenek00086832009-03-10 02:49:29 +0000162 // Process the path.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000163 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000164 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000165
166 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
167 I!=E; ++I, --n) {
168
Chris Lattner2b2453a2009-01-17 06:22:33 +0000169 HandlePiece(R, FID, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000170 }
171
172 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000173
Chris Lattner2b2453a2009-01-17 06:22:33 +0000174 // unsigned FID = R.getSourceMgr().getMainFileID();
175 html::EscapeText(R, FID);
176 html::AddLineNumbers(R, FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000177
Ted Kremenek47abe762008-04-16 16:39:56 +0000178 // If we have a preprocessor, relex the file and syntax highlight.
179 // We might not have a preprocessor if we come from a deserialized AST file,
180 // for example.
181
Chris Lattner2b2453a2009-01-17 06:22:33 +0000182 if (PP) html::SyntaxHighlight(R, FID, *PP);
Ted Kremenek55851142008-04-22 16:15:03 +0000183
184 // FIXME: We eventually want to use PPF to create a fresh Preprocessor,
185 // once we have worked out the bugs.
186 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000187 // if (PPF) html::HighlightMacros(R, FID, *PPF);
Ted Kremenek55851142008-04-22 16:15:03 +0000188 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000189 if (PP) html::HighlightMacros(R, FID, *PP);
Ted Kremenek47abe762008-04-16 16:39:56 +0000190
Ted Kremenekb9476392008-04-02 20:44:16 +0000191 // Get the full directory name of the analyzed file.
192
Chris Lattner2b2453a2009-01-17 06:22:33 +0000193 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Ted Kremenek2e939812008-03-27 07:35:49 +0000194
Ted Kremenek7fc89572008-04-24 23:37:03 +0000195 // This is a cludge; basically we want to append either the full
196 // working directory if we have no directory information. This is
197 // a work in progress.
198
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000199 std::string DirName = "";
200
201 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
202 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
203 DirName = P.toString() + "/";
204 }
Ted Kremenekb9476392008-04-02 20:44:16 +0000205
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000206 // Add the name of the file as an <h1> tag.
207
Ted Kremenek2e939812008-03-27 07:35:49 +0000208 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000209 std::string s;
210 llvm::raw_string_ostream os(s);
Ted Kremenek2e939812008-03-27 07:35:49 +0000211
Ted Kremenek778246a2008-09-22 17:33:32 +0000212 os << "<!-- REPORTHEADER -->\n"
213 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000214 "<tr><td class=\"rowname\">File:</td><td>"
215 << html::EscapeText(DirName)
216 << html::EscapeText(Entry->getName())
217 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
218 "<a href=\"#EndPath\">line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000219 << (*D.rbegin()).getLocation().getInstantiationLineNumber()
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000220 << ", column "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000221 << (*D.rbegin()).getLocation().getInstantiationColumnNumber()
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000222 << "</a></td></tr>\n"
223 "<tr><td class=\"rowname\">Description:</td><td>"
Ted Kremenek072192b2008-04-30 23:47:44 +0000224 << D.getDescription() << "</td></tr>\n";
225
226 // Output any other meta data.
227
228 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
229 I!=E; ++I) {
230 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
231 }
232
Ted Kremenek778246a2008-09-22 17:33:32 +0000233 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
234 "<h3>Annotated Source Code</h3>\n";
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000235
Chris Lattner2b2453a2009-01-17 06:22:33 +0000236 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000237 }
238
Ted Kremenekb9476392008-04-02 20:44:16 +0000239 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000240
241 const std::string& BugDesc = D.getDescription();
242
243 if (!BugDesc.empty()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000244 std::string s;
245 llvm::raw_string_ostream os(s);
Ted Kremenek86b43812008-04-02 18:03:20 +0000246 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000247 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000248 }
249
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000250 const std::string& BugType = D.getBugType();
251 if (!BugType.empty()) {
252 std::string s;
253 llvm::raw_string_ostream os(s);
254 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
255 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
256 }
257
Ted Kremenek8c036c72008-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";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000264 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek8c036c72008-09-20 04:23:38 +0000265 }
266
Ted Kremenekb9476392008-04-02 20:44:16 +0000267 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000268 std::string s;
269 llvm::raw_string_ostream os(s);
Ted Kremenek7fc89572008-04-24 23:37:03 +0000270 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000271 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000272 }
273
274 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000275 std::string s;
276 llvm::raw_string_ostream os(s);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000277 os << "\n<!-- BUGLINE "
278 << D.back()->getLocation().getInstantiationLineNumber() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000279 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000280 }
281
282 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000283 std::string s;
284 llvm::raw_string_ostream os(s);
Ted Kremenekb9476392008-04-02 20:44:16 +0000285 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000286 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000287 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000288
289 // Add CSS, header, and footer.
290
Chris Lattner2b2453a2009-01-17 06:22:33 +0000291 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000292
293 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000294 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000295
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 Kremenek7414dc02008-04-20 01:02:33 +0000324 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000325 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000326}
327
Chris Lattner2b2453a2009-01-17 06:22:33 +0000328void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000329 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000330 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000331
332 // For now, just draw a box above the line in question, and emit the
333 // warning.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000334 FullSourceLoc Pos = P.getLocation();
335
336 if (!Pos.isValid())
337 return;
338
Chris Lattner2b2453a2009-01-17 06:22:33 +0000339 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000340 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
341 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000342
Chris Lattner7da5aea2009-02-04 00:55:58 +0000343 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000344 return;
345
Chris Lattner7da5aea2009-02-04 00:55:58 +0000346 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000347 const char* FileStart = Buf->getBufferStart();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000348
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000349 // Compute the column number. Rewind from the current position to the start
350 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000351 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
352 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000353 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000354
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000355 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000356 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000357 const char* FileEnd = Buf->getBufferEnd();
358 while (*LineEnd != '\n' && LineEnd != FileEnd)
359 ++LineEnd;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000360
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000361 // Compute the margin offset by counting tabs and non-tabs.
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000362 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000363 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000364 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000365
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000366 // Create the html for the message.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000367 {
368 // Get the string and determining its maximum substring.
369 const std::string& Msg = P.getString();
370 unsigned max_token = 0;
371 unsigned cnt = 0;
372 unsigned len = Msg.size();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000373
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000374 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
375 switch (*I) {
376 default:
377 ++cnt;
Ted Kremenek00086832009-03-10 02:49:29 +0000378 continue;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000379 case ' ':
380 case '\t':
381 case '\n':
382 if (cnt > max_token) max_token = cnt;
383 cnt = 0;
384 }
385
386 if (cnt > max_token) max_token = cnt;
387
388 // Next, determine the approximate size of the message bubble in em.
389 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000390 const unsigned max_line = 120;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000391
392 if (max_token >= max_line)
393 em = max_token / 2;
394 else {
395 unsigned characters = max_line;
396 unsigned lines = len / max_line;
397
398 if (lines > 0) {
399 for (; characters > max_token; --characters)
400 if (len / characters > lines) {
401 ++characters;
402 break;
403 }
404 }
405
406 em = characters / 2;
407 }
408
Ted Kremenek80bae762009-03-02 23:05:40 +0000409 // Now generate the message bubble.
410 const char *Kind = 0;
411 switch (P.getKind()) {
Ted Kremenek80bae762009-03-02 23:05:40 +0000412 case PathDiagnosticPiece::Event: Kind = "Event"; break;
413 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
Ted Kremenek00086832009-03-10 02:49:29 +0000414 case PathDiagnosticPiece::Macro: Kind = "Macro"; break;
Ted Kremenek80bae762009-03-02 23:05:40 +0000415 }
416
417 std::string sbuf;
418 llvm::raw_string_ostream os(sbuf);
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000419
420 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
421
422 if (num == max)
423 os << "EndPath";
424 else
425 os << "Path" << num;
426
Ted Kremenek2f103982009-03-02 21:42:01 +0000427 os << "\" class=\"msg";
Ted Kremenek80bae762009-03-02 23:05:40 +0000428 if (Kind) os << " msg" << Kind;
Ted Kremenek2f103982009-03-02 21:42:01 +0000429 os << "\" style=\"margin-left:" << PosNo << "ex";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000430 if (em < max_line/2) os << "; max-width:" << em << "em";
431 os << "\">";
432
Ted Kremenek80bae762009-03-02 23:05:40 +0000433 if (max > 1) {
434 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
435 os << "<div class=\"PathIndex";
436 if (Kind) os << " PathIndex" << Kind;
437 os << "\">" << num << "</div>";
438 os << "</td><td>";
439 }
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000440
Ted Kremenek80bae762009-03-02 23:05:40 +0000441 os << html::EscapeText(Msg);
442
443 if (max > 1) {
444 os << "</td></tr></table>";
445 }
446
447 os << "</div></td></tr>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000448
449 // Insert the new html.
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000450 unsigned DisplayPos = LineEnd - FileStart;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000451 SourceLocation Loc =
Chris Lattner7da5aea2009-02-04 00:55:58 +0000452 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000453
Chris Lattner2b2453a2009-01-17 06:22:33 +0000454 R.InsertStrBefore(Loc, os.str());
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000455 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000456
457 // Now highlight the ranges.
458
459 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
460 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000461 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000462
463#if 0
464 // If there is a code insertion hint, insert that code.
465 // FIXME: This code is disabled because it seems to mangle the HTML
466 // output. I'm leaving it here because it's generally the right idea,
467 // but needs some help from someone more familiar with the rewriter.
468 for (const CodeModificationHint *Hint = P.code_modifications_begin(),
469 *HintEnd = P.code_modifications_end();
470 Hint != HintEnd; ++Hint) {
471 if (Hint->RemoveRange.isValid()) {
472 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
473 "<span class=\"CodeRemovalHint\">", "</span>");
474 }
475 if (Hint->InsertionLoc.isValid()) {
476 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
477 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
478 + "</span>";
479 R.InsertStrBefore(Hint->InsertionLoc, EscapedCode);
480 }
481 }
482#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000483}
484
Chris Lattner2b2453a2009-01-17 06:22:33 +0000485void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000486 SourceRange Range,
487 const char *HighlightStart,
488 const char *HighlightEnd) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000489
Ted Kremenek5dd00412008-04-14 21:06:04 +0000490 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000491
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000492 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000493 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000494
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000495 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000496 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000497
498 if (EndLineNo < StartLineNo)
499 return;
500
Chris Lattnera11d6172009-01-19 07:46:45 +0000501 if (SM.getFileID(InstantiationStart) != BugFileID ||
502 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000503 return;
504
505 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000506 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000507 unsigned OldEndColNo = EndColNo;
508
509 if (EndColNo) {
510 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000511 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000512 }
513
514 // Highlight the range. Make the span tag the outermost tag for the
515 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000516
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000517 SourceLocation E =
518 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000519
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000520 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000521}