blob: 72a854a85d290f41d95f531069d7cd0962814d4e [file] [log] [blame]
Ted Kremenek6efb0262008-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
Argyrios Kyrtzidis8d602a82010-12-22 18:51:49 +000014#include "clang/GR/PathDiagnosticClients.h"
15#include "clang/GR/BugReporter/PathDiagnostic.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Ted Kremenek6efb0262008-03-27 06:17:42 +000018#include "clang/Basic/SourceManager.h"
Ted Kremenek8cc48422008-03-27 07:35:49 +000019#include "clang/Basic/FileManager.h"
Ted Kremenek6efb0262008-03-27 06:17:42 +000020#include "clang/Rewrite/Rewriter.h"
21#include "clang/Rewrite/HTMLRewrite.h"
22#include "clang/Lex/Lexer.h"
Ted Kremenekb4947e42009-03-10 05:16:17 +000023#include "clang/Lex/Preprocessor.h"
Ted Kremenek6efb0262008-03-27 06:17:42 +000024#include "llvm/Support/MemoryBuffer.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000025#include "llvm/Support/raw_ostream.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000026#include "llvm/Support/Path.h"
Ted Kremenek490b8c02009-10-08 17:44:41 +000027
Ted Kremenek6efb0262008-03-27 06:17:42 +000028using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000029using namespace ento;
Ted Kremenek6efb0262008-03-27 06:17:42 +000030
31//===----------------------------------------------------------------------===//
32// Boilerplate.
33//===----------------------------------------------------------------------===//
34
35namespace {
36
Benjamin Kramer16634c22009-11-28 10:07:24 +000037class HTMLDiagnostics : public PathDiagnosticClient {
Ted Kremenek6efb0262008-03-27 06:17:42 +000038 llvm::sys::Path Directory, FilePrefix;
39 bool createdDir, noDir;
Daniel Dunbarb5f20252009-11-05 02:41:58 +000040 const Preprocessor &PP;
Ted Kremenek04ade6f2009-07-27 22:13:39 +000041 std::vector<const PathDiagnostic*> BatchedDiags;
Ted Kremenek6efb0262008-03-27 06:17:42 +000042public:
Daniel Dunbarb5f20252009-11-05 02:41:58 +000043 HTMLDiagnostics(const std::string& prefix, const Preprocessor &pp);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000044
Ted Kremenek5e860442009-11-05 02:09:23 +000045 virtual ~HTMLDiagnostics() { FlushDiagnostics(NULL); }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000046
Ted Kremenek5e860442009-11-05 02:09:23 +000047 virtual void FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade);
Mike Stump11289f42009-09-09 15:08:12 +000048
Ted Kremenek9718c9e2008-04-22 16:15:03 +000049 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000050
Ted Kremenek5e860442009-11-05 02:09:23 +000051 virtual llvm::StringRef getName() const {
52 return "HTMLDiagnostics";
53 }
Mike Stump11289f42009-09-09 15:08:12 +000054
Ted Kremenekb4947e42009-03-10 05:16:17 +000055 unsigned ProcessMacroPiece(llvm::raw_ostream& os,
56 const PathDiagnosticMacroPiece& P,
57 unsigned num);
Mike Stump11289f42009-09-09 15:08:12 +000058
Chris Lattnerd32480d2009-01-17 06:22:33 +000059 void HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenekc99332c2008-07-23 23:18:15 +000060 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Mike Stump11289f42009-09-09 15:08:12 +000061
Douglas Gregor87f95b02009-02-26 21:00:50 +000062 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
63 const char *HighlightStart = "<span class=\"mrange\">",
64 const char *HighlightEnd = "</span>");
Ted Kremenek9718c9e2008-04-22 16:15:03 +000065
Ted Kremenek5e860442009-11-05 02:09:23 +000066 void ReportDiag(const PathDiagnostic& D,
67 llvm::SmallVectorImpl<std::string> *FilesMade);
Ted Kremenek6efb0262008-03-27 06:17:42 +000068};
Mike Stump11289f42009-09-09 15:08:12 +000069
Ted Kremenek6efb0262008-03-27 06:17:42 +000070} // end anonymous namespace
71
Daniel Dunbarb5f20252009-11-05 02:41:58 +000072HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix,
73 const Preprocessor &pp)
Ted Kremenekf2e6fcf2008-04-16 16:39:56 +000074 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenek5e860442009-11-05 02:09:23 +000075 PP(pp) {
Mike Stump11289f42009-09-09 15:08:12 +000076 // All html files begin with "report"
Ted Kremenek6efb0262008-03-27 06:17:42 +000077 FilePrefix.appendComponent("report");
78}
79
80PathDiagnosticClient*
Ted Kremenek98857c92010-12-23 07:20:52 +000081ento::createHTMLDiagnosticClient(const std::string& prefix,
82 const Preprocessor &PP) {
Ted Kremenek5e860442009-11-05 02:09:23 +000083 return new HTMLDiagnostics(prefix, PP);
Ted Kremenek6efb0262008-03-27 06:17:42 +000084}
85
86//===----------------------------------------------------------------------===//
87// Report processing.
88//===----------------------------------------------------------------------===//
89
Ted Kremenek9718c9e2008-04-22 16:15:03 +000090void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
91 if (!D)
Ted Kremenek6efb0262008-03-27 06:17:42 +000092 return;
Mike Stump11289f42009-09-09 15:08:12 +000093
Ted Kremenek9718c9e2008-04-22 16:15:03 +000094 if (D->empty()) {
95 delete D;
96 return;
97 }
Mike Stump11289f42009-09-09 15:08:12 +000098
Ted Kremeneka64bbc62009-04-02 05:17:38 +000099 const_cast<PathDiagnostic*>(D)->flattenLocations();
Ted Kremenek9718c9e2008-04-22 16:15:03 +0000100 BatchedDiags.push_back(D);
101}
102
Ted Kremenek5e860442009-11-05 02:09:23 +0000103void
104HTMLDiagnostics::FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade)
105{
Ted Kremenek9718c9e2008-04-22 16:15:03 +0000106 while (!BatchedDiags.empty()) {
107 const PathDiagnostic* D = BatchedDiags.back();
108 BatchedDiags.pop_back();
Ted Kremenek5e860442009-11-05 02:09:23 +0000109 ReportDiag(*D, FilesMade);
Ted Kremenek9718c9e2008-04-22 16:15:03 +0000110 delete D;
Mike Stump11289f42009-09-09 15:08:12 +0000111 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000112
Ted Kremenekda002342009-11-13 03:02:57 +0000113 BatchedDiags.clear();
Ted Kremenek9718c9e2008-04-22 16:15:03 +0000114}
115
Ted Kremenek5e860442009-11-05 02:09:23 +0000116void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
117 llvm::SmallVectorImpl<std::string> *FilesMade){
Ted Kremenek6efb0262008-03-27 06:17:42 +0000118 // Create the HTML directory if it is missing.
Ted Kremenek6efb0262008-03-27 06:17:42 +0000119 if (!createdDir) {
120 createdDir = true;
Ted Kremenekb5c82252008-04-03 17:55:57 +0000121 std::string ErrorMsg;
122 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Mike Stump11289f42009-09-09 15:08:12 +0000123
Ted Kremenek6efb0262008-03-27 06:17:42 +0000124 if (!Directory.isDirectory()) {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000125 llvm::errs() << "warning: could not create directory '"
Chris Lattner3441b4f2009-08-23 22:45:33 +0000126 << Directory.str() << "'\n"
Mike Stump11289f42009-09-09 15:08:12 +0000127 << "reason: " << ErrorMsg << '\n';
128
Ted Kremenek6efb0262008-03-27 06:17:42 +0000129 noDir = true;
Mike Stump11289f42009-09-09 15:08:12 +0000130
Ted Kremenek6efb0262008-03-27 06:17:42 +0000131 return;
132 }
133 }
Mike Stump11289f42009-09-09 15:08:12 +0000134
Ted Kremenek6efb0262008-03-27 06:17:42 +0000135 if (noDir)
136 return;
Mike Stump11289f42009-09-09 15:08:12 +0000137
Ted Kremenek0bb09092009-04-01 06:13:56 +0000138 const SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattnerd32480d2009-01-17 06:22:33 +0000139 FileID FID;
Mike Stump11289f42009-09-09 15:08:12 +0000140
Ted Kremenekc99332c2008-07-23 23:18:15 +0000141 // Verify that the entire path is from the same FileID.
Chris Lattner8a425862009-01-16 07:36:28 +0000142 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
Ted Kremenek0bb09092009-04-01 06:13:56 +0000143 FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc();
Mike Stump11289f42009-09-09 15:08:12 +0000144
Chris Lattnerd32480d2009-01-17 06:22:33 +0000145 if (FID.isInvalid()) {
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000146 FID = SMgr.getFileID(L);
147 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekc99332c2008-07-23 23:18:15 +0000148 return; // FIXME: Emit a warning?
Mike Stump11289f42009-09-09 15:08:12 +0000149
Ted Kremenekc99332c2008-07-23 23:18:15 +0000150 // Check the source ranges.
151 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
152 RE=I->ranges_end(); RI!=RE; ++RI) {
Mike Stump11289f42009-09-09 15:08:12 +0000153
Chris Lattner8a425862009-01-16 07:36:28 +0000154 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekc99332c2008-07-23 23:18:15 +0000155
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000156 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekc99332c2008-07-23 23:18:15 +0000157 return; // FIXME: Emit a warning?
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattner8a425862009-01-16 07:36:28 +0000159 L = SMgr.getInstantiationLoc(RI->getEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000160
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000161 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Mike Stump11289f42009-09-09 15:08:12 +0000162 return; // FIXME: Emit a warning?
Ted Kremenekc99332c2008-07-23 23:18:15 +0000163 }
164 }
Mike Stump11289f42009-09-09 15:08:12 +0000165
Chris Lattnerd32480d2009-01-17 06:22:33 +0000166 if (FID.isInvalid())
Ted Kremenekc99332c2008-07-23 23:18:15 +0000167 return; // FIXME: Emit a warning?
Mike Stump11289f42009-09-09 15:08:12 +0000168
Ted Kremenekc99332c2008-07-23 23:18:15 +0000169 // Create a new rewriter to generate HTML.
Daniel Dunbarb5f20252009-11-05 02:41:58 +0000170 Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOptions());
Mike Stump11289f42009-09-09 15:08:12 +0000171
172 // Process the path.
Ted Kremenek6efb0262008-03-27 06:17:42 +0000173 unsigned n = D.size();
Ted Kremeneka9590d12008-03-31 23:30:12 +0000174 unsigned max = n;
Mike Stump11289f42009-09-09 15:08:12 +0000175
Ted Kremenek6efb0262008-03-27 06:17:42 +0000176 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
Chris Lattner25ef69a2009-04-17 20:40:01 +0000177 I!=E; ++I, --n)
Chris Lattnerd32480d2009-01-17 06:22:33 +0000178 HandlePiece(R, FID, *I, n, max);
Mike Stump11289f42009-09-09 15:08:12 +0000179
Ted Kremenek6efb0262008-03-27 06:17:42 +0000180 // Add line numbers, header, footer, etc.
Mike Stump11289f42009-09-09 15:08:12 +0000181
Chris Lattnerd32480d2009-01-17 06:22:33 +0000182 // unsigned FID = R.getSourceMgr().getMainFileID();
183 html::EscapeText(R, FID);
184 html::AddLineNumbers(R, FID);
Mike Stump11289f42009-09-09 15:08:12 +0000185
Ted Kremenekf2e6fcf2008-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.
Mike Stump11289f42009-09-09 15:08:12 +0000189
Daniel Dunbarb5f20252009-11-05 02:41:58 +0000190 html::SyntaxHighlight(R, FID, PP);
191 html::HighlightMacros(R, FID, PP);
Mike Stump11289f42009-09-09 15:08:12 +0000192
Ted Kremenek3276af42008-04-02 20:44:16 +0000193 // Get the full directory name of the analyzed file.
194
Chris Lattnerd32480d2009-01-17 06:22:33 +0000195 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Mike Stump11289f42009-09-09 15:08:12 +0000196
Ted Kremenek64fa3be2008-04-24 23:37:03 +0000197 // This is a cludge; basically we want to append either the full
198 // working directory if we have no directory information. This is
199 // a work in progress.
200
Ted Kremenek5f56cbb2008-05-02 22:04:53 +0000201 std::string DirName = "";
Mike Stump11289f42009-09-09 15:08:12 +0000202
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000203 if (llvm::sys::path::is_relative(Entry->getName())) {
Ted Kremenek5f56cbb2008-05-02 22:04:53 +0000204 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000205 DirName = P.str() + "/";
Ted Kremenek5f56cbb2008-05-02 22:04:53 +0000206 }
Mike Stump11289f42009-09-09 15:08:12 +0000207
208 // Add the name of the file as an <h1> tag.
209
Ted Kremenek8cc48422008-03-27 07:35:49 +0000210 {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000211 std::string s;
212 llvm::raw_string_ostream os(s);
Mike Stump11289f42009-09-09 15:08:12 +0000213
Ted Kremenekb76a3f442008-09-22 17:33:32 +0000214 os << "<!-- REPORTHEADER -->\n"
Ted Kremenek0bb09092009-04-01 06:13:56 +0000215 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek7d6219f2008-04-15 21:25:08 +0000216 "<tr><td class=\"rowname\">File:</td><td>"
Ted Kremenek0bb09092009-04-01 06:13:56 +0000217 << html::EscapeText(DirName)
218 << html::EscapeText(Entry->getName())
219 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
Mike Stump11289f42009-09-09 15:08:12 +0000220 "<a href=\"#EndPath\">line "
Ted Kremenek0bb09092009-04-01 06:13:56 +0000221 << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber()
222 << ", column "
223 << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber()
224 << "</a></td></tr>\n"
225 "<tr><td class=\"rowname\">Description:</td><td>"
226 << D.getDescription() << "</td></tr>\n";
Mike Stump11289f42009-09-09 15:08:12 +0000227
Ted Kremenekb0f87c42008-04-30 23:47:44 +0000228 // Output any other meta data.
Mike Stump11289f42009-09-09 15:08:12 +0000229
Ted Kremenekb0f87c42008-04-30 23:47:44 +0000230 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
231 I!=E; ++I) {
232 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
233 }
Mike Stump11289f42009-09-09 15:08:12 +0000234
Ted Kremenekb76a3f442008-09-22 17:33:32 +0000235 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
Mike Stump11289f42009-09-09 15:08:12 +0000236 "<h3>Annotated Source Code</h3>\n";
237
Daniel Dunbar62c850f2009-08-19 20:32:38 +0000238 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremeneke6eed292008-04-02 07:04:46 +0000239 }
Mike Stump11289f42009-09-09 15:08:12 +0000240
Ted Kremenek3276af42008-04-02 20:44:16 +0000241 // Embed meta-data tags.
Ted Kremenek3276af42008-04-02 20:44:16 +0000242 {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000243 std::string s;
244 llvm::raw_string_ostream os(s);
Mike Stump11289f42009-09-09 15:08:12 +0000245
246 const std::string& BugDesc = D.getDescription();
Ted Kremenek3724cde2009-08-03 23:44:55 +0000247 if (!BugDesc.empty())
248 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Mike Stump11289f42009-09-09 15:08:12 +0000249
Ted Kremenek3724cde2009-08-03 23:44:55 +0000250 const std::string& BugType = D.getBugType();
251 if (!BugType.empty())
252 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
Mike Stump11289f42009-09-09 15:08:12 +0000253
254 const std::string& BugCategory = D.getCategory();
Ted Kremenek3724cde2009-08-03 23:44:55 +0000255 if (!BugCategory.empty())
256 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
257
Ted Kremenek64fa3be2008-04-24 23:37:03 +0000258 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenek3724cde2009-08-03 23:44:55 +0000259
Chris Lattner8a425862009-01-16 07:36:28 +0000260 os << "\n<!-- BUGLINE "
Ted Kremenek0bb09092009-04-01 06:13:56 +0000261 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
262 << " -->\n";
Ted Kremenek3724cde2009-08-03 23:44:55 +0000263
264 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Mike Stump11289f42009-09-09 15:08:12 +0000265
Ted Kremenek3724cde2009-08-03 23:44:55 +0000266 // Mark the end of the tags.
267 os << "\n<!-- BUGMETAEND -->\n";
Mike Stump11289f42009-09-09 15:08:12 +0000268
Ted Kremenek3724cde2009-08-03 23:44:55 +0000269 // Insert the text.
Daniel Dunbar62c850f2009-08-19 20:32:38 +0000270 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek3276af42008-04-02 20:44:16 +0000271 }
Mike Stump11289f42009-09-09 15:08:12 +0000272
Ted Kremenek6efb0262008-03-27 06:17:42 +0000273 // Add CSS, header, and footer.
Mike Stump11289f42009-09-09 15:08:12 +0000274
Chris Lattnerd32480d2009-01-17 06:22:33 +0000275 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000276
Ted Kremenek6efb0262008-03-27 06:17:42 +0000277 // Get the rewrite buffer.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000278 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Mike Stump11289f42009-09-09 15:08:12 +0000279
Ted Kremenek6efb0262008-03-27 06:17:42 +0000280 if (!Buf) {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000281 llvm::errs() << "warning: no diagnostics generated for main file.\n";
Ted Kremenek6efb0262008-03-27 06:17:42 +0000282 return;
283 }
284
Ted Kremenek490b8c02009-10-08 17:44:41 +0000285 // Create a path for the target HTML file.
286 llvm::sys::Path F(FilePrefix);
287 F.makeUnique(false, NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000288
Ted Kremenek490b8c02009-10-08 17:44:41 +0000289 // Rename the file with an HTML extension.
290 llvm::sys::Path H(F);
291 H.appendSuffix("html");
292 F.renamePathOnDisk(H, NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000293
Ted Kremenek490b8c02009-10-08 17:44:41 +0000294 std::string ErrorMsg;
295 llvm::raw_fd_ostream os(H.c_str(), ErrorMsg);
Mike Stump11289f42009-09-09 15:08:12 +0000296
Ted Kremenek490b8c02009-10-08 17:44:41 +0000297 if (!ErrorMsg.empty()) {
Dan Gohman3bc1b0c2010-06-28 15:56:07 +0000298 llvm::errs() << "warning: could not create file '" << F.str()
299 << "'\n";
Ted Kremenek490b8c02009-10-08 17:44:41 +0000300 return;
Ted Kremenek6efb0262008-03-27 06:17:42 +0000301 }
Mike Stump11289f42009-09-09 15:08:12 +0000302
Ted Kremenek490b8c02009-10-08 17:44:41 +0000303 if (FilesMade)
Michael J. Spencere1696752010-12-18 00:19:12 +0000304 FilesMade->push_back(llvm::sys::path::filename(H.str()));
Ted Kremenek490b8c02009-10-08 17:44:41 +0000305
Ted Kremenek6efb0262008-03-27 06:17:42 +0000306 // Emit the HTML to disk.
Ted Kremenek076d1332008-04-20 01:02:33 +0000307 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000308 os << *I;
Ted Kremenek6efb0262008-03-27 06:17:42 +0000309}
310
Chris Lattnerd32480d2009-01-17 06:22:33 +0000311void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek6efb0262008-03-27 06:17:42 +0000312 const PathDiagnosticPiece& P,
Ted Kremeneka9590d12008-03-31 23:30:12 +0000313 unsigned num, unsigned max) {
Mike Stump11289f42009-09-09 15:08:12 +0000314
Ted Kremenek6efb0262008-03-27 06:17:42 +0000315 // For now, just draw a box above the line in question, and emit the
316 // warning.
Ted Kremenek0bb09092009-04-01 06:13:56 +0000317 FullSourceLoc Pos = P.getLocation().asLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000318
Ted Kremenek6efb0262008-03-27 06:17:42 +0000319 if (!Pos.isValid())
Mike Stump11289f42009-09-09 15:08:12 +0000320 return;
321
Chris Lattnerd32480d2009-01-17 06:22:33 +0000322 SourceManager &SM = R.getSourceMgr();
Chris Lattnere4ad4172009-02-04 00:55:58 +0000323 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
324 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Mike Stump11289f42009-09-09 15:08:12 +0000325
Chris Lattnere4ad4172009-02-04 00:55:58 +0000326 if (LPosInfo.first != BugFileID)
Ted Kremenek6efb0262008-03-27 06:17:42 +0000327 return;
Mike Stump11289f42009-09-09 15:08:12 +0000328
Chris Lattnere4ad4172009-02-04 00:55:58 +0000329 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Mike Stump11289f42009-09-09 15:08:12 +0000330 const char* FileStart = Buf->getBufferStart();
331
Ted Kremenek6efb0262008-03-27 06:17:42 +0000332 // Compute the column number. Rewind from the current position to the start
333 // of the line.
Chris Lattnere4ad4172009-02-04 00:55:58 +0000334 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
335 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattner8a425862009-01-16 07:36:28 +0000336 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremeneke6d24192008-05-06 23:42:18 +0000337
Ted Kremenekea3a9e22009-02-18 22:10:00 +0000338 // Compute LineEnd.
Chris Lattner8a425862009-01-16 07:36:28 +0000339 const char *LineEnd = TokInstantiationPtr;
Ted Kremenekea3a9e22009-02-18 22:10:00 +0000340 const char* FileEnd = Buf->getBufferEnd();
341 while (*LineEnd != '\n' && LineEnd != FileEnd)
342 ++LineEnd;
Mike Stump11289f42009-09-09 15:08:12 +0000343
Ted Kremenek01fa5d22008-03-31 21:40:14 +0000344 // Compute the margin offset by counting tabs and non-tabs.
Mike Stump11289f42009-09-09 15:08:12 +0000345 unsigned PosNo = 0;
Chris Lattner8a425862009-01-16 07:36:28 +0000346 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremeneke6d24192008-05-06 23:42:18 +0000347 PosNo += *c == '\t' ? 8 : 1;
Mike Stump11289f42009-09-09 15:08:12 +0000348
Ted Kremenek6efb0262008-03-27 06:17:42 +0000349 // Create the html for the message.
Ted Kremenekb4947e42009-03-10 05:16:17 +0000350
351 const char *Kind = 0;
352 switch (P.getKind()) {
Mike Stump281d6d72010-01-20 02:03:14 +0000353 case PathDiagnosticPiece::Event: Kind = "Event"; break;
354 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
355 // Setting Kind to "Control" is intentional.
356 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
Ted Kremenekb4947e42009-03-10 05:16:17 +0000357 }
Mike Stump11289f42009-09-09 15:08:12 +0000358
Ted Kremenekb4947e42009-03-10 05:16:17 +0000359 std::string sbuf;
360 llvm::raw_string_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +0000361
Ted Kremenekb4947e42009-03-10 05:16:17 +0000362 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
Mike Stump11289f42009-09-09 15:08:12 +0000363
Ted Kremenekb4947e42009-03-10 05:16:17 +0000364 if (num == max)
365 os << "EndPath";
366 else
367 os << "Path" << num;
Mike Stump11289f42009-09-09 15:08:12 +0000368
Ted Kremenekb4947e42009-03-10 05:16:17 +0000369 os << "\" class=\"msg";
370 if (Kind)
Mike Stump11289f42009-09-09 15:08:12 +0000371 os << " msg" << Kind;
Ted Kremenekb4947e42009-03-10 05:16:17 +0000372 os << "\" style=\"margin-left:" << PosNo << "ex";
Mike Stump11289f42009-09-09 15:08:12 +0000373
Ted Kremenekb4947e42009-03-10 05:16:17 +0000374 // Output a maximum size.
375 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremenek80b2b162008-09-21 18:52:59 +0000376 // Get the string and determining its maximum substring.
377 const std::string& Msg = P.getString();
378 unsigned max_token = 0;
379 unsigned cnt = 0;
380 unsigned len = Msg.size();
Mike Stump11289f42009-09-09 15:08:12 +0000381
Ted Kremenek80b2b162008-09-21 18:52:59 +0000382 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
383 switch (*I) {
Mike Stump281d6d72010-01-20 02:03:14 +0000384 default:
385 ++cnt;
386 continue;
387 case ' ':
388 case '\t':
389 case '\n':
390 if (cnt > max_token) max_token = cnt;
391 cnt = 0;
Ted Kremenek80b2b162008-09-21 18:52:59 +0000392 }
Mike Stump11289f42009-09-09 15:08:12 +0000393
Ted Kremenekb4947e42009-03-10 05:16:17 +0000394 if (cnt > max_token)
395 max_token = cnt;
Mike Stump11289f42009-09-09 15:08:12 +0000396
Ted Kremenekb4947e42009-03-10 05:16:17 +0000397 // Determine the approximate size of the message bubble in em.
Ted Kremenek80b2b162008-09-21 18:52:59 +0000398 unsigned em;
Ted Kremenek7f16ed42009-03-02 23:06:15 +0000399 const unsigned max_line = 120;
Mike Stump11289f42009-09-09 15:08:12 +0000400
Ted Kremenek80b2b162008-09-21 18:52:59 +0000401 if (max_token >= max_line)
402 em = max_token / 2;
403 else {
404 unsigned characters = max_line;
405 unsigned lines = len / max_line;
Mike Stump11289f42009-09-09 15:08:12 +0000406
Ted Kremenek80b2b162008-09-21 18:52:59 +0000407 if (lines > 0) {
408 for (; characters > max_token; --characters)
409 if (len / characters > lines) {
410 ++characters;
411 break;
412 }
413 }
Mike Stump11289f42009-09-09 15:08:12 +0000414
Ted Kremenek80b2b162008-09-21 18:52:59 +0000415 em = characters / 2;
416 }
Mike Stump11289f42009-09-09 15:08:12 +0000417
Ted Kremenekb4947e42009-03-10 05:16:17 +0000418 if (em < max_line/2)
Mike Stump11289f42009-09-09 15:08:12 +0000419 os << "; max-width:" << em << "em";
Ted Kremenekb4947e42009-03-10 05:16:17 +0000420 }
421 else
422 os << "; max-width:100em";
Mike Stump11289f42009-09-09 15:08:12 +0000423
Ted Kremenekb4947e42009-03-10 05:16:17 +0000424 os << "\">";
Mike Stump11289f42009-09-09 15:08:12 +0000425
Ted Kremenekb4947e42009-03-10 05:16:17 +0000426 if (max > 1) {
427 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
428 os << "<div class=\"PathIndex";
429 if (Kind) os << " PathIndex" << Kind;
430 os << "\">" << num << "</div>";
431 os << "</td><td>";
432 }
433
434 if (const PathDiagnosticMacroPiece *MP =
Mike Stump11289f42009-09-09 15:08:12 +0000435 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
Ted Kremenekb4947e42009-03-10 05:16:17 +0000436
437 os << "Within the expansion of the macro '";
Mike Stump11289f42009-09-09 15:08:12 +0000438
Ted Kremenekb4947e42009-03-10 05:16:17 +0000439 // Get the name of the macro by relexing it.
440 {
Ted Kremenek0bb09092009-04-01 06:13:56 +0000441 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenekb4947e42009-03-10 05:16:17 +0000442 assert(L.isFileID());
Benjamin Kramer0ca3c622010-03-16 14:48:07 +0000443 llvm::StringRef BufferInfo = L.getBufferData();
444 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.data();
445 Lexer rawLexer(L, PP.getLangOptions(), BufferInfo.begin(),
446 MacroName, BufferInfo.end());
Mike Stump11289f42009-09-09 15:08:12 +0000447
Ted Kremenekb4947e42009-03-10 05:16:17 +0000448 Token TheTok;
449 rawLexer.LexFromRawLexer(TheTok);
450 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
451 os << MacroName[i];
Ted Kremenekc62af6c2009-03-02 23:05:40 +0000452 }
Mike Stump11289f42009-09-09 15:08:12 +0000453
Ted Kremenekb4947e42009-03-10 05:16:17 +0000454 os << "':\n";
Mike Stump11289f42009-09-09 15:08:12 +0000455
Ted Kremenekb4947e42009-03-10 05:16:17 +0000456 if (max > 1)
Ted Kremenekc62af6c2009-03-02 23:05:40 +0000457 os << "</td></tr></table>";
Ted Kremenekb4947e42009-03-10 05:16:17 +0000458
459 // Within a macro piece. Write out each event.
460 ProcessMacroPiece(os, *MP, 0);
461 }
462 else {
463 os << html::EscapeText(P.getString());
Mike Stump11289f42009-09-09 15:08:12 +0000464
Ted Kremenekb4947e42009-03-10 05:16:17 +0000465 if (max > 1)
466 os << "</td></tr></table>";
Ted Kremenek80b2b162008-09-21 18:52:59 +0000467 }
Mike Stump11289f42009-09-09 15:08:12 +0000468
Ted Kremenekb4947e42009-03-10 05:16:17 +0000469 os << "</div></td></tr>";
470
471 // Insert the new html.
Mike Stump11289f42009-09-09 15:08:12 +0000472 unsigned DisplayPos = LineEnd - FileStart;
473 SourceLocation Loc =
Ted Kremenekb4947e42009-03-10 05:16:17 +0000474 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
475
Daniel Dunbar62c850f2009-08-19 20:32:38 +0000476 R.InsertTextBefore(Loc, os.str());
Ted Kremenekb4947e42009-03-10 05:16:17 +0000477
Mike Stump11289f42009-09-09 15:08:12 +0000478 // Now highlight the ranges.
Ted Kremenek6efb0262008-03-27 06:17:42 +0000479 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
480 I != E; ++I)
Chris Lattnere4ad4172009-02-04 00:55:58 +0000481 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000482
483#if 0
484 // If there is a code insertion hint, insert that code.
485 // FIXME: This code is disabled because it seems to mangle the HTML
486 // output. I'm leaving it here because it's generally the right idea,
487 // but needs some help from someone more familiar with the rewriter.
Douglas Gregora771f462010-03-31 17:46:05 +0000488 for (const FixItHint *Hint = P.fixit_begin(), *HintEnd = P.fixit_end();
Douglas Gregor87f95b02009-02-26 21:00:50 +0000489 Hint != HintEnd; ++Hint) {
490 if (Hint->RemoveRange.isValid()) {
491 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
492 "<span class=\"CodeRemovalHint\">", "</span>");
493 }
494 if (Hint->InsertionLoc.isValid()) {
495 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
496 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
497 + "</span>";
Daniel Dunbar62c850f2009-08-19 20:32:38 +0000498 R.InsertTextBefore(Hint->InsertionLoc, EscapedCode);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000499 }
500 }
501#endif
Ted Kremenek6efb0262008-03-27 06:17:42 +0000502}
503
Ted Kremenekb4947e42009-03-10 05:16:17 +0000504static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
Benjamin Kramer5ac3b0b2010-03-13 11:34:41 +0000505 unsigned x = n % ('z' - 'a');
506 n /= 'z' - 'a';
Ted Kremenekb4947e42009-03-10 05:16:17 +0000507
Benjamin Kramer5ac3b0b2010-03-13 11:34:41 +0000508 if (n > 0)
509 EmitAlphaCounter(os, n);
Mike Stump11289f42009-09-09 15:08:12 +0000510
Benjamin Kramer5ac3b0b2010-03-13 11:34:41 +0000511 os << char('a' + x);
Ted Kremenekb4947e42009-03-10 05:16:17 +0000512}
513
514unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
515 const PathDiagnosticMacroPiece& P,
516 unsigned num) {
Mike Stump11289f42009-09-09 15:08:12 +0000517
Ted Kremenekb4947e42009-03-10 05:16:17 +0000518 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
519 I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000520
Ted Kremenekb4947e42009-03-10 05:16:17 +0000521 if (const PathDiagnosticMacroPiece *MP =
522 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
523 num = ProcessMacroPiece(os, *MP, num);
524 continue;
525 }
526
527 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
528 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
529 "margin-left:5px\">"
530 "<table class=\"msgT\"><tr>"
531 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
532 EmitAlphaCounter(os, num++);
533 os << "</div></td><td valign=\"top\">"
534 << html::EscapeText(EP->getString())
535 << "</td></tr></table></div>\n";
536 }
537 }
Mike Stump11289f42009-09-09 15:08:12 +0000538
Ted Kremenekb4947e42009-03-10 05:16:17 +0000539 return num;
540}
541
Chris Lattnerd32480d2009-01-17 06:22:33 +0000542void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor87f95b02009-02-26 21:00:50 +0000543 SourceRange Range,
544 const char *HighlightStart,
545 const char *HighlightEnd) {
Chris Lattner184e65d2009-04-14 23:22:57 +0000546 SourceManager &SM = R.getSourceMgr();
547 const LangOptions &LangOpts = R.getLangOpts();
Mike Stump11289f42009-09-09 15:08:12 +0000548
Chris Lattner8a425862009-01-16 07:36:28 +0000549 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner88ea93e2009-02-04 01:06:56 +0000550 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Mike Stump11289f42009-09-09 15:08:12 +0000551
Chris Lattner8a425862009-01-16 07:36:28 +0000552 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner88ea93e2009-02-04 01:06:56 +0000553 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000554
Ted Kremenek6efb0262008-03-27 06:17:42 +0000555 if (EndLineNo < StartLineNo)
556 return;
Mike Stump11289f42009-09-09 15:08:12 +0000557
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000558 if (SM.getFileID(InstantiationStart) != BugFileID ||
559 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek6efb0262008-03-27 06:17:42 +0000560 return;
Mike Stump11289f42009-09-09 15:08:12 +0000561
Ted Kremenek6efb0262008-03-27 06:17:42 +0000562 // Compute the column number of the end.
Chris Lattnere4ad4172009-02-04 00:55:58 +0000563 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek6efb0262008-03-27 06:17:42 +0000564 unsigned OldEndColNo = EndColNo;
565
566 if (EndColNo) {
567 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner184e65d2009-04-14 23:22:57 +0000568 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
Ted Kremenek6efb0262008-03-27 06:17:42 +0000569 }
Mike Stump11289f42009-09-09 15:08:12 +0000570
Ted Kremenek6efb0262008-03-27 06:17:42 +0000571 // Highlight the range. Make the span tag the outermost tag for the
572 // selected range.
Mike Stump11289f42009-09-09 15:08:12 +0000573
Chris Lattner8a425862009-01-16 07:36:28 +0000574 SourceLocation E =
575 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Mike Stump11289f42009-09-09 15:08:12 +0000576
Douglas Gregor87f95b02009-02-26 21:00:50 +0000577 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek6efb0262008-03-27 06:17:42 +0000578}