blob: 5fa5ab7f50c43a3c7731453568ffb958d595dacd [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
162 // Process the path.
163
164 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000165 unsigned max = n;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000166
167 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
168 I!=E; ++I, --n) {
169
Chris Lattner2b2453a2009-01-17 06:22:33 +0000170 HandlePiece(R, FID, *I, n, max);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000171 }
172
173 // Add line numbers, header, footer, etc.
Ted Kremenek2e939812008-03-27 07:35:49 +0000174
Chris Lattner2b2453a2009-01-17 06:22:33 +0000175 // unsigned FID = R.getSourceMgr().getMainFileID();
176 html::EscapeText(R, FID);
177 html::AddLineNumbers(R, FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000178
Ted Kremenek47abe762008-04-16 16:39:56 +0000179 // If we have a preprocessor, relex the file and syntax highlight.
180 // We might not have a preprocessor if we come from a deserialized AST file,
181 // for example.
182
Chris Lattner2b2453a2009-01-17 06:22:33 +0000183 if (PP) html::SyntaxHighlight(R, FID, *PP);
Ted Kremenek55851142008-04-22 16:15:03 +0000184
185 // FIXME: We eventually want to use PPF to create a fresh Preprocessor,
186 // once we have worked out the bugs.
187 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000188 // if (PPF) html::HighlightMacros(R, FID, *PPF);
Ted Kremenek55851142008-04-22 16:15:03 +0000189 //
Chris Lattner2b2453a2009-01-17 06:22:33 +0000190 if (PP) html::HighlightMacros(R, FID, *PP);
Ted Kremenek47abe762008-04-16 16:39:56 +0000191
Ted Kremenekb9476392008-04-02 20:44:16 +0000192 // Get the full directory name of the analyzed file.
193
Chris Lattner2b2453a2009-01-17 06:22:33 +0000194 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Ted Kremenek2e939812008-03-27 07:35:49 +0000195
Ted Kremenek7fc89572008-04-24 23:37:03 +0000196 // This is a cludge; basically we want to append either the full
197 // working directory if we have no directory information. This is
198 // a work in progress.
199
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000200 std::string DirName = "";
201
202 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
203 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
204 DirName = P.toString() + "/";
205 }
Ted Kremenekb9476392008-04-02 20:44:16 +0000206
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000207 // Add the name of the file as an <h1> tag.
208
Ted Kremenek2e939812008-03-27 07:35:49 +0000209 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000210 std::string s;
211 llvm::raw_string_ostream os(s);
Ted Kremenek2e939812008-03-27 07:35:49 +0000212
Ted Kremenek778246a2008-09-22 17:33:32 +0000213 os << "<!-- REPORTHEADER -->\n"
214 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000215 "<tr><td class=\"rowname\">File:</td><td>"
216 << html::EscapeText(DirName)
217 << html::EscapeText(Entry->getName())
218 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
219 "<a href=\"#EndPath\">line "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000220 << (*D.rbegin()).getLocation().getInstantiationLineNumber()
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000221 << ", column "
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000222 << (*D.rbegin()).getLocation().getInstantiationColumnNumber()
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000223 << "</a></td></tr>\n"
224 "<tr><td class=\"rowname\">Description:</td><td>"
Ted Kremenek072192b2008-04-30 23:47:44 +0000225 << D.getDescription() << "</td></tr>\n";
226
227 // Output any other meta data.
228
229 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
230 I!=E; ++I) {
231 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
232 }
233
Ted Kremenek778246a2008-09-22 17:33:32 +0000234 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
235 "<h3>Annotated Source Code</h3>\n";
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000236
Chris Lattner2b2453a2009-01-17 06:22:33 +0000237 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000238 }
239
Ted Kremenekb9476392008-04-02 20:44:16 +0000240 // Embed meta-data tags.
Ted Kremenek07615402008-04-02 07:04:46 +0000241
242 const std::string& BugDesc = D.getDescription();
243
244 if (!BugDesc.empty()) {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000245 std::string s;
246 llvm::raw_string_ostream os(s);
Ted Kremenek86b43812008-04-02 18:03:20 +0000247 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000248 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000249 }
250
Ted Kremeneka26ddab2009-01-27 01:53:39 +0000251 const std::string& BugType = D.getBugType();
252 if (!BugType.empty()) {
253 std::string s;
254 llvm::raw_string_ostream os(s);
255 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
256 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
257 }
258
Ted Kremenek8c036c72008-09-20 04:23:38 +0000259 const std::string& BugCategory = D.getCategory();
260
261 if (!BugCategory.empty()) {
262 std::string s;
263 llvm::raw_string_ostream os(s);
264 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000265 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek8c036c72008-09-20 04:23:38 +0000266 }
267
Ted Kremenekb9476392008-04-02 20:44:16 +0000268 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000269 std::string s;
270 llvm::raw_string_ostream os(s);
Ted Kremenek7fc89572008-04-24 23:37:03 +0000271 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000272 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000273 }
274
275 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000276 std::string s;
277 llvm::raw_string_ostream os(s);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000278 os << "\n<!-- BUGLINE "
279 << D.back()->getLocation().getInstantiationLineNumber() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000280 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000281 }
282
283 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000284 std::string s;
285 llvm::raw_string_ostream os(s);
Ted Kremenekb9476392008-04-02 20:44:16 +0000286 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Chris Lattner2b2453a2009-01-17 06:22:33 +0000287 R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000288 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000289
290 // Add CSS, header, and footer.
291
Chris Lattner2b2453a2009-01-17 06:22:33 +0000292 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000293
294 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000295 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000296
297 if (!Buf) {
298 llvm::cerr << "warning: no diagnostics generated for main file.\n";
299 return;
300 }
301
302 // Create the stream to write out the HTML.
303 std::ofstream os;
304
305 {
306 // Create a path for the target HTML file.
307 llvm::sys::Path F(FilePrefix);
308 F.makeUnique(false, NULL);
309
310 // Rename the file with an HTML extension.
311 llvm::sys::Path H(F);
312 H.appendSuffix("html");
313 F.renamePathOnDisk(H, NULL);
314
315 os.open(H.toString().c_str());
316
317 if (!os) {
318 llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
319 return;
320 }
321 }
322
323 // Emit the HTML to disk.
324
Ted Kremenek7414dc02008-04-20 01:02:33 +0000325 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Ted Kremenekfa5be362008-04-08 22:37:58 +0000326 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000327}
328
Chris Lattner2b2453a2009-01-17 06:22:33 +0000329void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000330 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000331 unsigned num, unsigned max) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000332
333 // For now, just draw a box above the line in question, and emit the
334 // warning.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000335 FullSourceLoc Pos = P.getLocation();
336
337 if (!Pos.isValid())
338 return;
339
Chris Lattner2b2453a2009-01-17 06:22:33 +0000340 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000341 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
342 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000343
Chris Lattner7da5aea2009-02-04 00:55:58 +0000344 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000345 return;
346
Chris Lattner7da5aea2009-02-04 00:55:58 +0000347 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000348 const char* FileStart = Buf->getBufferStart();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000349
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000350 // Compute the column number. Rewind from the current position to the start
351 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000352 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
353 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000354 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000355
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000356 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000357 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000358 const char* FileEnd = Buf->getBufferEnd();
359 while (*LineEnd != '\n' && LineEnd != FileEnd)
360 ++LineEnd;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000361
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000362 // Compute the margin offset by counting tabs and non-tabs.
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000363 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000364 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000365 PosNo += *c == '\t' ? 8 : 1;
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000366
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000367 // Create the html for the message.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000368 {
369 // Get the string and determining its maximum substring.
370 const std::string& Msg = P.getString();
371 unsigned max_token = 0;
372 unsigned cnt = 0;
373 unsigned len = Msg.size();
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000374
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000375 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
376 switch (*I) {
377 default:
378 ++cnt;
379 continue;
380 case ' ':
381 case '\t':
382 case '\n':
383 if (cnt > max_token) max_token = cnt;
384 cnt = 0;
385 }
386
387 if (cnt > max_token) max_token = cnt;
388
389 // Next, determine the approximate size of the message bubble in em.
390 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000391 const unsigned max_line = 120;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000392
393 if (max_token >= max_line)
394 em = max_token / 2;
395 else {
396 unsigned characters = max_line;
397 unsigned lines = len / max_line;
398
399 if (lines > 0) {
400 for (; characters > max_token; --characters)
401 if (len / characters > lines) {
402 ++characters;
403 break;
404 }
405 }
406
407 em = characters / 2;
408 }
409
Ted Kremenek80bae762009-03-02 23:05:40 +0000410 // Now generate the message bubble.
411 const char *Kind = 0;
412 switch (P.getKind()) {
413 default: break;
414 case PathDiagnosticPiece::Event: Kind = "Event"; break;
415 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
416 }
417
418 std::string sbuf;
419 llvm::raw_string_ostream os(sbuf);
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000420
421 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
422
423 if (num == max)
424 os << "EndPath";
425 else
426 os << "Path" << num;
427
Ted Kremenek2f103982009-03-02 21:42:01 +0000428 os << "\" class=\"msg";
Ted Kremenek80bae762009-03-02 23:05:40 +0000429 if (Kind) os << " msg" << Kind;
Ted Kremenek2f103982009-03-02 21:42:01 +0000430 os << "\" style=\"margin-left:" << PosNo << "ex";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000431 if (em < max_line/2) os << "; max-width:" << em << "em";
432 os << "\">";
433
Ted Kremenek80bae762009-03-02 23:05:40 +0000434 if (max > 1) {
435 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
436 os << "<div class=\"PathIndex";
437 if (Kind) os << " PathIndex" << Kind;
438 os << "\">" << num << "</div>";
439 os << "</td><td>";
440 }
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000441
Ted Kremenek80bae762009-03-02 23:05:40 +0000442 os << html::EscapeText(Msg);
443
444 if (max > 1) {
445 os << "</td></tr></table>";
446 }
447
448 os << "</div></td></tr>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000449
450 // Insert the new html.
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000451 unsigned DisplayPos = LineEnd - FileStart;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000452 SourceLocation Loc =
Chris Lattner7da5aea2009-02-04 00:55:58 +0000453 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000454
Chris Lattner2b2453a2009-01-17 06:22:33 +0000455 R.InsertStrBefore(Loc, os.str());
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000456 }
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000457
458 // Now highlight the ranges.
459
460 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
461 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000462 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000463
464#if 0
465 // If there is a code insertion hint, insert that code.
466 // FIXME: This code is disabled because it seems to mangle the HTML
467 // output. I'm leaving it here because it's generally the right idea,
468 // but needs some help from someone more familiar with the rewriter.
469 for (const CodeModificationHint *Hint = P.code_modifications_begin(),
470 *HintEnd = P.code_modifications_end();
471 Hint != HintEnd; ++Hint) {
472 if (Hint->RemoveRange.isValid()) {
473 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
474 "<span class=\"CodeRemovalHint\">", "</span>");
475 }
476 if (Hint->InsertionLoc.isValid()) {
477 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
478 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
479 + "</span>";
480 R.InsertStrBefore(Hint->InsertionLoc, EscapedCode);
481 }
482 }
483#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000484}
485
Chris Lattner2b2453a2009-01-17 06:22:33 +0000486void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000487 SourceRange Range,
488 const char *HighlightStart,
489 const char *HighlightEnd) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000490
Ted Kremenek5dd00412008-04-14 21:06:04 +0000491 SourceManager& SM = R.getSourceMgr();
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000492
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000493 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000494 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000495
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000496 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000497 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000498
499 if (EndLineNo < StartLineNo)
500 return;
501
Chris Lattnera11d6172009-01-19 07:46:45 +0000502 if (SM.getFileID(InstantiationStart) != BugFileID ||
503 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000504 return;
505
506 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000507 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000508 unsigned OldEndColNo = EndColNo;
509
510 if (EndColNo) {
511 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek12fc0142008-04-18 05:01:33 +0000512 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000513 }
514
515 // Highlight the range. Make the span tag the outermost tag for the
516 // selected range.
Ted Kremenekdab4ead2008-04-08 21:29:14 +0000517
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000518 SourceLocation E =
519 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Ted Kremenekb7478142008-04-17 18:37:23 +0000520
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000521 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000522}