blob: bd20d481a99d8a4f8be7b0b4770d76cba69bc4db [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
Ted Kremenekd99bd552010-12-23 19:38:26 +000014#include "clang/StaticAnalyzer/PathDiagnosticClients.h"
15#include "clang/StaticAnalyzer/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"
Michael J. Spencere503f892011-01-11 01:21:20 +000024#include "llvm/Support/FileSystem.h"
Ted Kremenek6efb0262008-03-27 06:17:42 +000025#include "llvm/Support/MemoryBuffer.h"
Ted Kremenek2d470fc2008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000027#include "llvm/Support/Path.h"
Ted Kremenek490b8c02009-10-08 17:44:41 +000028
Ted Kremenek6efb0262008-03-27 06:17:42 +000029using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000030using namespace ento;
Ted Kremenek6efb0262008-03-27 06:17:42 +000031
32//===----------------------------------------------------------------------===//
33// Boilerplate.
34//===----------------------------------------------------------------------===//
35
36namespace {
37
Benjamin Kramer16634c22009-11-28 10:07:24 +000038class HTMLDiagnostics : public PathDiagnosticClient {
Ted Kremenek6efb0262008-03-27 06:17:42 +000039 llvm::sys::Path Directory, FilePrefix;
40 bool createdDir, noDir;
Daniel Dunbarb5f20252009-11-05 02:41:58 +000041 const Preprocessor &PP;
Ted Kremenek04ade6f2009-07-27 22:13:39 +000042 std::vector<const PathDiagnostic*> BatchedDiags;
Ted Kremenek6efb0262008-03-27 06:17:42 +000043public:
Daniel Dunbarb5f20252009-11-05 02:41:58 +000044 HTMLDiagnostics(const std::string& prefix, const Preprocessor &pp);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000045
Ted Kremenek5e860442009-11-05 02:09:23 +000046 virtual ~HTMLDiagnostics() { FlushDiagnostics(NULL); }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000047
Ted Kremenek5e860442009-11-05 02:09:23 +000048 virtual void FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade);
Mike Stump11289f42009-09-09 15:08:12 +000049
Ted Kremenek9718c9e2008-04-22 16:15:03 +000050 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000051
Ted Kremenek5e860442009-11-05 02:09:23 +000052 virtual llvm::StringRef getName() const {
53 return "HTMLDiagnostics";
54 }
Mike Stump11289f42009-09-09 15:08:12 +000055
Ted Kremenekb4947e42009-03-10 05:16:17 +000056 unsigned ProcessMacroPiece(llvm::raw_ostream& os,
57 const PathDiagnosticMacroPiece& P,
58 unsigned num);
Mike Stump11289f42009-09-09 15:08:12 +000059
Chris Lattnerd32480d2009-01-17 06:22:33 +000060 void HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenekc99332c2008-07-23 23:18:15 +000061 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Mike Stump11289f42009-09-09 15:08:12 +000062
Douglas Gregor87f95b02009-02-26 21:00:50 +000063 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
64 const char *HighlightStart = "<span class=\"mrange\">",
65 const char *HighlightEnd = "</span>");
Ted Kremenek9718c9e2008-04-22 16:15:03 +000066
Ted Kremenek5e860442009-11-05 02:09:23 +000067 void ReportDiag(const PathDiagnostic& D,
68 llvm::SmallVectorImpl<std::string> *FilesMade);
Ted Kremenek6efb0262008-03-27 06:17:42 +000069};
Mike Stump11289f42009-09-09 15:08:12 +000070
Ted Kremenek6efb0262008-03-27 06:17:42 +000071} // end anonymous namespace
72
Daniel Dunbarb5f20252009-11-05 02:41:58 +000073HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix,
74 const Preprocessor &pp)
Ted Kremenekf2e6fcf2008-04-16 16:39:56 +000075 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenek5e860442009-11-05 02:09:23 +000076 PP(pp) {
Mike Stump11289f42009-09-09 15:08:12 +000077 // All html files begin with "report"
Ted Kremenek6efb0262008-03-27 06:17:42 +000078 FilePrefix.appendComponent("report");
79}
80
81PathDiagnosticClient*
Ted Kremenek98857c92010-12-23 07:20:52 +000082ento::createHTMLDiagnosticClient(const std::string& prefix,
83 const Preprocessor &PP) {
Ted Kremenek5e860442009-11-05 02:09:23 +000084 return new HTMLDiagnostics(prefix, PP);
Ted Kremenek6efb0262008-03-27 06:17:42 +000085}
86
87//===----------------------------------------------------------------------===//
88// Report processing.
89//===----------------------------------------------------------------------===//
90
Ted Kremenek9718c9e2008-04-22 16:15:03 +000091void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
92 if (!D)
Ted Kremenek6efb0262008-03-27 06:17:42 +000093 return;
Mike Stump11289f42009-09-09 15:08:12 +000094
Ted Kremenek9718c9e2008-04-22 16:15:03 +000095 if (D->empty()) {
96 delete D;
97 return;
98 }
Mike Stump11289f42009-09-09 15:08:12 +000099
Ted Kremeneka64bbc62009-04-02 05:17:38 +0000100 const_cast<PathDiagnostic*>(D)->flattenLocations();
Ted Kremenek9718c9e2008-04-22 16:15:03 +0000101 BatchedDiags.push_back(D);
102}
103
Ted Kremenek5e860442009-11-05 02:09:23 +0000104void
105HTMLDiagnostics::FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade)
106{
Ted Kremenek9718c9e2008-04-22 16:15:03 +0000107 while (!BatchedDiags.empty()) {
108 const PathDiagnostic* D = BatchedDiags.back();
109 BatchedDiags.pop_back();
Ted Kremenek5e860442009-11-05 02:09:23 +0000110 ReportDiag(*D, FilesMade);
Ted Kremenek9718c9e2008-04-22 16:15:03 +0000111 delete D;
Mike Stump11289f42009-09-09 15:08:12 +0000112 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000113
Ted Kremenekda002342009-11-13 03:02:57 +0000114 BatchedDiags.clear();
Ted Kremenek9718c9e2008-04-22 16:15:03 +0000115}
116
Ted Kremenek5e860442009-11-05 02:09:23 +0000117void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
118 llvm::SmallVectorImpl<std::string> *FilesMade){
Ted Kremenek6efb0262008-03-27 06:17:42 +0000119 // Create the HTML directory if it is missing.
Ted Kremenek6efb0262008-03-27 06:17:42 +0000120 if (!createdDir) {
121 createdDir = true;
Ted Kremenekb5c82252008-04-03 17:55:57 +0000122 std::string ErrorMsg;
123 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Mike Stump11289f42009-09-09 15:08:12 +0000124
Michael J. Spencere503f892011-01-11 01:21:20 +0000125 bool IsDirectory;
126 if (llvm::sys::fs::is_directory(Directory.str(), IsDirectory) ||
127 !IsDirectory) {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000128 llvm::errs() << "warning: could not create directory '"
Chris Lattner3441b4f2009-08-23 22:45:33 +0000129 << Directory.str() << "'\n"
Mike Stump11289f42009-09-09 15:08:12 +0000130 << "reason: " << ErrorMsg << '\n';
131
Ted Kremenek6efb0262008-03-27 06:17:42 +0000132 noDir = true;
Mike Stump11289f42009-09-09 15:08:12 +0000133
Ted Kremenek6efb0262008-03-27 06:17:42 +0000134 return;
135 }
136 }
Mike Stump11289f42009-09-09 15:08:12 +0000137
Ted Kremenek6efb0262008-03-27 06:17:42 +0000138 if (noDir)
139 return;
Mike Stump11289f42009-09-09 15:08:12 +0000140
Ted Kremenek0bb09092009-04-01 06:13:56 +0000141 const SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattnerd32480d2009-01-17 06:22:33 +0000142 FileID FID;
Mike Stump11289f42009-09-09 15:08:12 +0000143
Ted Kremenekc99332c2008-07-23 23:18:15 +0000144 // Verify that the entire path is from the same FileID.
Chris Lattner8a425862009-01-16 07:36:28 +0000145 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
Ted Kremenek0bb09092009-04-01 06:13:56 +0000146 FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc();
Mike Stump11289f42009-09-09 15:08:12 +0000147
Chris Lattnerd32480d2009-01-17 06:22:33 +0000148 if (FID.isInvalid()) {
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000149 FID = SMgr.getFileID(L);
150 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekc99332c2008-07-23 23:18:15 +0000151 return; // FIXME: Emit a warning?
Mike Stump11289f42009-09-09 15:08:12 +0000152
Ted Kremenekc99332c2008-07-23 23:18:15 +0000153 // Check the source ranges.
154 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
155 RE=I->ranges_end(); RI!=RE; ++RI) {
Mike Stump11289f42009-09-09 15:08:12 +0000156
Chris Lattner8a425862009-01-16 07:36:28 +0000157 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekc99332c2008-07-23 23:18:15 +0000158
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000159 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekc99332c2008-07-23 23:18:15 +0000160 return; // FIXME: Emit a warning?
Mike Stump11289f42009-09-09 15:08:12 +0000161
Chris Lattner8a425862009-01-16 07:36:28 +0000162 L = SMgr.getInstantiationLoc(RI->getEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000163
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000164 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Mike Stump11289f42009-09-09 15:08:12 +0000165 return; // FIXME: Emit a warning?
Ted Kremenekc99332c2008-07-23 23:18:15 +0000166 }
167 }
Mike Stump11289f42009-09-09 15:08:12 +0000168
Chris Lattnerd32480d2009-01-17 06:22:33 +0000169 if (FID.isInvalid())
Ted Kremenekc99332c2008-07-23 23:18:15 +0000170 return; // FIXME: Emit a warning?
Mike Stump11289f42009-09-09 15:08:12 +0000171
Ted Kremenekc99332c2008-07-23 23:18:15 +0000172 // Create a new rewriter to generate HTML.
Daniel Dunbarb5f20252009-11-05 02:41:58 +0000173 Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOptions());
Mike Stump11289f42009-09-09 15:08:12 +0000174
175 // Process the path.
Ted Kremenek6efb0262008-03-27 06:17:42 +0000176 unsigned n = D.size();
Ted Kremeneka9590d12008-03-31 23:30:12 +0000177 unsigned max = n;
Mike Stump11289f42009-09-09 15:08:12 +0000178
Ted Kremenek6efb0262008-03-27 06:17:42 +0000179 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
Chris Lattner25ef69a2009-04-17 20:40:01 +0000180 I!=E; ++I, --n)
Chris Lattnerd32480d2009-01-17 06:22:33 +0000181 HandlePiece(R, FID, *I, n, max);
Mike Stump11289f42009-09-09 15:08:12 +0000182
Ted Kremenek6efb0262008-03-27 06:17:42 +0000183 // Add line numbers, header, footer, etc.
Mike Stump11289f42009-09-09 15:08:12 +0000184
Chris Lattnerd32480d2009-01-17 06:22:33 +0000185 // unsigned FID = R.getSourceMgr().getMainFileID();
186 html::EscapeText(R, FID);
187 html::AddLineNumbers(R, FID);
Mike Stump11289f42009-09-09 15:08:12 +0000188
Ted Kremenekf2e6fcf2008-04-16 16:39:56 +0000189 // If we have a preprocessor, relex the file and syntax highlight.
190 // We might not have a preprocessor if we come from a deserialized AST file,
191 // for example.
Mike Stump11289f42009-09-09 15:08:12 +0000192
Daniel Dunbarb5f20252009-11-05 02:41:58 +0000193 html::SyntaxHighlight(R, FID, PP);
194 html::HighlightMacros(R, FID, PP);
Mike Stump11289f42009-09-09 15:08:12 +0000195
Ted Kremenek3276af42008-04-02 20:44:16 +0000196 // Get the full directory name of the analyzed file.
197
Chris Lattnerd32480d2009-01-17 06:22:33 +0000198 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Mike Stump11289f42009-09-09 15:08:12 +0000199
Ted Kremenek64fa3be2008-04-24 23:37:03 +0000200 // This is a cludge; basically we want to append either the full
201 // working directory if we have no directory information. This is
202 // a work in progress.
203
Ted Kremenek5f56cbb2008-05-02 22:04:53 +0000204 std::string DirName = "";
Mike Stump11289f42009-09-09 15:08:12 +0000205
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000206 if (llvm::sys::path::is_relative(Entry->getName())) {
Ted Kremenek5f56cbb2008-05-02 22:04:53 +0000207 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattner3441b4f2009-08-23 22:45:33 +0000208 DirName = P.str() + "/";
Ted Kremenek5f56cbb2008-05-02 22:04:53 +0000209 }
Mike Stump11289f42009-09-09 15:08:12 +0000210
211 // Add the name of the file as an <h1> tag.
212
Ted Kremenek8cc48422008-03-27 07:35:49 +0000213 {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000214 std::string s;
215 llvm::raw_string_ostream os(s);
Mike Stump11289f42009-09-09 15:08:12 +0000216
Ted Kremenekb76a3f442008-09-22 17:33:32 +0000217 os << "<!-- REPORTHEADER -->\n"
Ted Kremenek0bb09092009-04-01 06:13:56 +0000218 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek7d6219f2008-04-15 21:25:08 +0000219 "<tr><td class=\"rowname\">File:</td><td>"
Ted Kremenek0bb09092009-04-01 06:13:56 +0000220 << html::EscapeText(DirName)
221 << html::EscapeText(Entry->getName())
222 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
Mike Stump11289f42009-09-09 15:08:12 +0000223 "<a href=\"#EndPath\">line "
Ted Kremenek0bb09092009-04-01 06:13:56 +0000224 << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber()
225 << ", column "
226 << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber()
227 << "</a></td></tr>\n"
228 "<tr><td class=\"rowname\">Description:</td><td>"
229 << D.getDescription() << "</td></tr>\n";
Mike Stump11289f42009-09-09 15:08:12 +0000230
Ted Kremenekb0f87c42008-04-30 23:47:44 +0000231 // Output any other meta data.
Mike Stump11289f42009-09-09 15:08:12 +0000232
Ted Kremenekb0f87c42008-04-30 23:47:44 +0000233 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
234 I!=E; ++I) {
235 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
236 }
Mike Stump11289f42009-09-09 15:08:12 +0000237
Ted Kremenekb76a3f442008-09-22 17:33:32 +0000238 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
Mike Stump11289f42009-09-09 15:08:12 +0000239 "<h3>Annotated Source Code</h3>\n";
240
Daniel Dunbar62c850f2009-08-19 20:32:38 +0000241 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremeneke6eed292008-04-02 07:04:46 +0000242 }
Mike Stump11289f42009-09-09 15:08:12 +0000243
Ted Kremenek3276af42008-04-02 20:44:16 +0000244 // Embed meta-data tags.
Ted Kremenek3276af42008-04-02 20:44:16 +0000245 {
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000246 std::string s;
247 llvm::raw_string_ostream os(s);
Mike Stump11289f42009-09-09 15:08:12 +0000248
249 const std::string& BugDesc = D.getDescription();
Ted Kremenek3724cde2009-08-03 23:44:55 +0000250 if (!BugDesc.empty())
251 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Mike Stump11289f42009-09-09 15:08:12 +0000252
Ted Kremenek3724cde2009-08-03 23:44:55 +0000253 const std::string& BugType = D.getBugType();
254 if (!BugType.empty())
255 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
Mike Stump11289f42009-09-09 15:08:12 +0000256
257 const std::string& BugCategory = D.getCategory();
Ted Kremenek3724cde2009-08-03 23:44:55 +0000258 if (!BugCategory.empty())
259 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
260
Ted Kremenek64fa3be2008-04-24 23:37:03 +0000261 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenek3724cde2009-08-03 23:44:55 +0000262
Chris Lattner8a425862009-01-16 07:36:28 +0000263 os << "\n<!-- BUGLINE "
Ted Kremenek0bb09092009-04-01 06:13:56 +0000264 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
265 << " -->\n";
Ted Kremenek3724cde2009-08-03 23:44:55 +0000266
267 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Mike Stump11289f42009-09-09 15:08:12 +0000268
Ted Kremenek3724cde2009-08-03 23:44:55 +0000269 // Mark the end of the tags.
270 os << "\n<!-- BUGMETAEND -->\n";
Mike Stump11289f42009-09-09 15:08:12 +0000271
Ted Kremenek3724cde2009-08-03 23:44:55 +0000272 // Insert the text.
Daniel Dunbar62c850f2009-08-19 20:32:38 +0000273 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek3276af42008-04-02 20:44:16 +0000274 }
Mike Stump11289f42009-09-09 15:08:12 +0000275
Ted Kremenek6efb0262008-03-27 06:17:42 +0000276 // Add CSS, header, and footer.
Mike Stump11289f42009-09-09 15:08:12 +0000277
Chris Lattnerd32480d2009-01-17 06:22:33 +0000278 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000279
Ted Kremenek6efb0262008-03-27 06:17:42 +0000280 // Get the rewrite buffer.
Chris Lattnerd32480d2009-01-17 06:22:33 +0000281 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Mike Stump11289f42009-09-09 15:08:12 +0000282
Ted Kremenek6efb0262008-03-27 06:17:42 +0000283 if (!Buf) {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000284 llvm::errs() << "warning: no diagnostics generated for main file.\n";
Ted Kremenek6efb0262008-03-27 06:17:42 +0000285 return;
286 }
287
Ted Kremenek490b8c02009-10-08 17:44:41 +0000288 // Create a path for the target HTML file.
289 llvm::sys::Path F(FilePrefix);
290 F.makeUnique(false, NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000291
Ted Kremenek490b8c02009-10-08 17:44:41 +0000292 // Rename the file with an HTML extension.
293 llvm::sys::Path H(F);
294 H.appendSuffix("html");
295 F.renamePathOnDisk(H, NULL);
Mike Stump11289f42009-09-09 15:08:12 +0000296
Ted Kremenek490b8c02009-10-08 17:44:41 +0000297 std::string ErrorMsg;
298 llvm::raw_fd_ostream os(H.c_str(), ErrorMsg);
Mike Stump11289f42009-09-09 15:08:12 +0000299
Ted Kremenek490b8c02009-10-08 17:44:41 +0000300 if (!ErrorMsg.empty()) {
Dan Gohman3bc1b0c2010-06-28 15:56:07 +0000301 llvm::errs() << "warning: could not create file '" << F.str()
302 << "'\n";
Ted Kremenek490b8c02009-10-08 17:44:41 +0000303 return;
Ted Kremenek6efb0262008-03-27 06:17:42 +0000304 }
Mike Stump11289f42009-09-09 15:08:12 +0000305
Ted Kremenek490b8c02009-10-08 17:44:41 +0000306 if (FilesMade)
Michael J. Spencere1696752010-12-18 00:19:12 +0000307 FilesMade->push_back(llvm::sys::path::filename(H.str()));
Ted Kremenek490b8c02009-10-08 17:44:41 +0000308
Ted Kremenek6efb0262008-03-27 06:17:42 +0000309 // Emit the HTML to disk.
Ted Kremenek076d1332008-04-20 01:02:33 +0000310 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000311 os << *I;
Ted Kremenek6efb0262008-03-27 06:17:42 +0000312}
313
Chris Lattnerd32480d2009-01-17 06:22:33 +0000314void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek6efb0262008-03-27 06:17:42 +0000315 const PathDiagnosticPiece& P,
Ted Kremeneka9590d12008-03-31 23:30:12 +0000316 unsigned num, unsigned max) {
Mike Stump11289f42009-09-09 15:08:12 +0000317
Ted Kremenek6efb0262008-03-27 06:17:42 +0000318 // For now, just draw a box above the line in question, and emit the
319 // warning.
Ted Kremenek0bb09092009-04-01 06:13:56 +0000320 FullSourceLoc Pos = P.getLocation().asLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000321
Ted Kremenek6efb0262008-03-27 06:17:42 +0000322 if (!Pos.isValid())
Mike Stump11289f42009-09-09 15:08:12 +0000323 return;
324
Chris Lattnerd32480d2009-01-17 06:22:33 +0000325 SourceManager &SM = R.getSourceMgr();
Chris Lattnere4ad4172009-02-04 00:55:58 +0000326 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
327 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Mike Stump11289f42009-09-09 15:08:12 +0000328
Chris Lattnere4ad4172009-02-04 00:55:58 +0000329 if (LPosInfo.first != BugFileID)
Ted Kremenek6efb0262008-03-27 06:17:42 +0000330 return;
Mike Stump11289f42009-09-09 15:08:12 +0000331
Chris Lattnere4ad4172009-02-04 00:55:58 +0000332 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Mike Stump11289f42009-09-09 15:08:12 +0000333 const char* FileStart = Buf->getBufferStart();
334
Ted Kremenek6efb0262008-03-27 06:17:42 +0000335 // Compute the column number. Rewind from the current position to the start
336 // of the line.
Chris Lattnere4ad4172009-02-04 00:55:58 +0000337 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
338 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattner8a425862009-01-16 07:36:28 +0000339 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremeneke6d24192008-05-06 23:42:18 +0000340
Ted Kremenekea3a9e22009-02-18 22:10:00 +0000341 // Compute LineEnd.
Chris Lattner8a425862009-01-16 07:36:28 +0000342 const char *LineEnd = TokInstantiationPtr;
Ted Kremenekea3a9e22009-02-18 22:10:00 +0000343 const char* FileEnd = Buf->getBufferEnd();
344 while (*LineEnd != '\n' && LineEnd != FileEnd)
345 ++LineEnd;
Mike Stump11289f42009-09-09 15:08:12 +0000346
Ted Kremenek01fa5d22008-03-31 21:40:14 +0000347 // Compute the margin offset by counting tabs and non-tabs.
Mike Stump11289f42009-09-09 15:08:12 +0000348 unsigned PosNo = 0;
Chris Lattner8a425862009-01-16 07:36:28 +0000349 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremeneke6d24192008-05-06 23:42:18 +0000350 PosNo += *c == '\t' ? 8 : 1;
Mike Stump11289f42009-09-09 15:08:12 +0000351
Ted Kremenek6efb0262008-03-27 06:17:42 +0000352 // Create the html for the message.
Ted Kremenekb4947e42009-03-10 05:16:17 +0000353
354 const char *Kind = 0;
355 switch (P.getKind()) {
Mike Stump281d6d72010-01-20 02:03:14 +0000356 case PathDiagnosticPiece::Event: Kind = "Event"; break;
357 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
358 // Setting Kind to "Control" is intentional.
359 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
Ted Kremenekb4947e42009-03-10 05:16:17 +0000360 }
Mike Stump11289f42009-09-09 15:08:12 +0000361
Ted Kremenekb4947e42009-03-10 05:16:17 +0000362 std::string sbuf;
363 llvm::raw_string_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +0000364
Ted Kremenekb4947e42009-03-10 05:16:17 +0000365 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
Mike Stump11289f42009-09-09 15:08:12 +0000366
Ted Kremenekb4947e42009-03-10 05:16:17 +0000367 if (num == max)
368 os << "EndPath";
369 else
370 os << "Path" << num;
Mike Stump11289f42009-09-09 15:08:12 +0000371
Ted Kremenekb4947e42009-03-10 05:16:17 +0000372 os << "\" class=\"msg";
373 if (Kind)
Mike Stump11289f42009-09-09 15:08:12 +0000374 os << " msg" << Kind;
Ted Kremenekb4947e42009-03-10 05:16:17 +0000375 os << "\" style=\"margin-left:" << PosNo << "ex";
Mike Stump11289f42009-09-09 15:08:12 +0000376
Ted Kremenekb4947e42009-03-10 05:16:17 +0000377 // Output a maximum size.
378 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremenek80b2b162008-09-21 18:52:59 +0000379 // Get the string and determining its maximum substring.
380 const std::string& Msg = P.getString();
381 unsigned max_token = 0;
382 unsigned cnt = 0;
383 unsigned len = Msg.size();
Mike Stump11289f42009-09-09 15:08:12 +0000384
Ted Kremenek80b2b162008-09-21 18:52:59 +0000385 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
386 switch (*I) {
Mike Stump281d6d72010-01-20 02:03:14 +0000387 default:
388 ++cnt;
389 continue;
390 case ' ':
391 case '\t':
392 case '\n':
393 if (cnt > max_token) max_token = cnt;
394 cnt = 0;
Ted Kremenek80b2b162008-09-21 18:52:59 +0000395 }
Mike Stump11289f42009-09-09 15:08:12 +0000396
Ted Kremenekb4947e42009-03-10 05:16:17 +0000397 if (cnt > max_token)
398 max_token = cnt;
Mike Stump11289f42009-09-09 15:08:12 +0000399
Ted Kremenekb4947e42009-03-10 05:16:17 +0000400 // Determine the approximate size of the message bubble in em.
Ted Kremenek80b2b162008-09-21 18:52:59 +0000401 unsigned em;
Ted Kremenek7f16ed42009-03-02 23:06:15 +0000402 const unsigned max_line = 120;
Mike Stump11289f42009-09-09 15:08:12 +0000403
Ted Kremenek80b2b162008-09-21 18:52:59 +0000404 if (max_token >= max_line)
405 em = max_token / 2;
406 else {
407 unsigned characters = max_line;
408 unsigned lines = len / max_line;
Mike Stump11289f42009-09-09 15:08:12 +0000409
Ted Kremenek80b2b162008-09-21 18:52:59 +0000410 if (lines > 0) {
411 for (; characters > max_token; --characters)
412 if (len / characters > lines) {
413 ++characters;
414 break;
415 }
416 }
Mike Stump11289f42009-09-09 15:08:12 +0000417
Ted Kremenek80b2b162008-09-21 18:52:59 +0000418 em = characters / 2;
419 }
Mike Stump11289f42009-09-09 15:08:12 +0000420
Ted Kremenekb4947e42009-03-10 05:16:17 +0000421 if (em < max_line/2)
Mike Stump11289f42009-09-09 15:08:12 +0000422 os << "; max-width:" << em << "em";
Ted Kremenekb4947e42009-03-10 05:16:17 +0000423 }
424 else
425 os << "; max-width:100em";
Mike Stump11289f42009-09-09 15:08:12 +0000426
Ted Kremenekb4947e42009-03-10 05:16:17 +0000427 os << "\">";
Mike Stump11289f42009-09-09 15:08:12 +0000428
Ted Kremenekb4947e42009-03-10 05:16:17 +0000429 if (max > 1) {
430 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
431 os << "<div class=\"PathIndex";
432 if (Kind) os << " PathIndex" << Kind;
433 os << "\">" << num << "</div>";
434 os << "</td><td>";
435 }
436
437 if (const PathDiagnosticMacroPiece *MP =
Mike Stump11289f42009-09-09 15:08:12 +0000438 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
Ted Kremenekb4947e42009-03-10 05:16:17 +0000439
440 os << "Within the expansion of the macro '";
Mike Stump11289f42009-09-09 15:08:12 +0000441
Ted Kremenekb4947e42009-03-10 05:16:17 +0000442 // Get the name of the macro by relexing it.
443 {
Ted Kremenek0bb09092009-04-01 06:13:56 +0000444 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenekb4947e42009-03-10 05:16:17 +0000445 assert(L.isFileID());
Benjamin Kramer0ca3c622010-03-16 14:48:07 +0000446 llvm::StringRef BufferInfo = L.getBufferData();
447 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.data();
448 Lexer rawLexer(L, PP.getLangOptions(), BufferInfo.begin(),
449 MacroName, BufferInfo.end());
Mike Stump11289f42009-09-09 15:08:12 +0000450
Ted Kremenekb4947e42009-03-10 05:16:17 +0000451 Token TheTok;
452 rawLexer.LexFromRawLexer(TheTok);
453 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
454 os << MacroName[i];
Ted Kremenekc62af6c2009-03-02 23:05:40 +0000455 }
Mike Stump11289f42009-09-09 15:08:12 +0000456
Ted Kremenekb4947e42009-03-10 05:16:17 +0000457 os << "':\n";
Mike Stump11289f42009-09-09 15:08:12 +0000458
Ted Kremenekb4947e42009-03-10 05:16:17 +0000459 if (max > 1)
Ted Kremenekc62af6c2009-03-02 23:05:40 +0000460 os << "</td></tr></table>";
Ted Kremenekb4947e42009-03-10 05:16:17 +0000461
462 // Within a macro piece. Write out each event.
463 ProcessMacroPiece(os, *MP, 0);
464 }
465 else {
466 os << html::EscapeText(P.getString());
Mike Stump11289f42009-09-09 15:08:12 +0000467
Ted Kremenekb4947e42009-03-10 05:16:17 +0000468 if (max > 1)
469 os << "</td></tr></table>";
Ted Kremenek80b2b162008-09-21 18:52:59 +0000470 }
Mike Stump11289f42009-09-09 15:08:12 +0000471
Ted Kremenekb4947e42009-03-10 05:16:17 +0000472 os << "</div></td></tr>";
473
474 // Insert the new html.
Mike Stump11289f42009-09-09 15:08:12 +0000475 unsigned DisplayPos = LineEnd - FileStart;
476 SourceLocation Loc =
Ted Kremenekb4947e42009-03-10 05:16:17 +0000477 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
478
Daniel Dunbar62c850f2009-08-19 20:32:38 +0000479 R.InsertTextBefore(Loc, os.str());
Ted Kremenekb4947e42009-03-10 05:16:17 +0000480
Mike Stump11289f42009-09-09 15:08:12 +0000481 // Now highlight the ranges.
Ted Kremenek6efb0262008-03-27 06:17:42 +0000482 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
483 I != E; ++I)
Chris Lattnere4ad4172009-02-04 00:55:58 +0000484 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000485
486#if 0
487 // If there is a code insertion hint, insert that code.
488 // FIXME: This code is disabled because it seems to mangle the HTML
489 // output. I'm leaving it here because it's generally the right idea,
490 // but needs some help from someone more familiar with the rewriter.
Douglas Gregora771f462010-03-31 17:46:05 +0000491 for (const FixItHint *Hint = P.fixit_begin(), *HintEnd = P.fixit_end();
Douglas Gregor87f95b02009-02-26 21:00:50 +0000492 Hint != HintEnd; ++Hint) {
493 if (Hint->RemoveRange.isValid()) {
494 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
495 "<span class=\"CodeRemovalHint\">", "</span>");
496 }
497 if (Hint->InsertionLoc.isValid()) {
498 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
499 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
500 + "</span>";
Daniel Dunbar62c850f2009-08-19 20:32:38 +0000501 R.InsertTextBefore(Hint->InsertionLoc, EscapedCode);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000502 }
503 }
504#endif
Ted Kremenek6efb0262008-03-27 06:17:42 +0000505}
506
Ted Kremenekb4947e42009-03-10 05:16:17 +0000507static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
Benjamin Kramer5ac3b0b2010-03-13 11:34:41 +0000508 unsigned x = n % ('z' - 'a');
509 n /= 'z' - 'a';
Ted Kremenekb4947e42009-03-10 05:16:17 +0000510
Benjamin Kramer5ac3b0b2010-03-13 11:34:41 +0000511 if (n > 0)
512 EmitAlphaCounter(os, n);
Mike Stump11289f42009-09-09 15:08:12 +0000513
Benjamin Kramer5ac3b0b2010-03-13 11:34:41 +0000514 os << char('a' + x);
Ted Kremenekb4947e42009-03-10 05:16:17 +0000515}
516
517unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
518 const PathDiagnosticMacroPiece& P,
519 unsigned num) {
Mike Stump11289f42009-09-09 15:08:12 +0000520
Ted Kremenekb4947e42009-03-10 05:16:17 +0000521 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
522 I!=E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000523
Ted Kremenekb4947e42009-03-10 05:16:17 +0000524 if (const PathDiagnosticMacroPiece *MP =
525 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
526 num = ProcessMacroPiece(os, *MP, num);
527 continue;
528 }
529
530 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
531 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
532 "margin-left:5px\">"
533 "<table class=\"msgT\"><tr>"
534 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
535 EmitAlphaCounter(os, num++);
536 os << "</div></td><td valign=\"top\">"
537 << html::EscapeText(EP->getString())
538 << "</td></tr></table></div>\n";
539 }
540 }
Mike Stump11289f42009-09-09 15:08:12 +0000541
Ted Kremenekb4947e42009-03-10 05:16:17 +0000542 return num;
543}
544
Chris Lattnerd32480d2009-01-17 06:22:33 +0000545void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor87f95b02009-02-26 21:00:50 +0000546 SourceRange Range,
547 const char *HighlightStart,
548 const char *HighlightEnd) {
Chris Lattner184e65d2009-04-14 23:22:57 +0000549 SourceManager &SM = R.getSourceMgr();
550 const LangOptions &LangOpts = R.getLangOpts();
Mike Stump11289f42009-09-09 15:08:12 +0000551
Chris Lattner8a425862009-01-16 07:36:28 +0000552 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner88ea93e2009-02-04 01:06:56 +0000553 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Mike Stump11289f42009-09-09 15:08:12 +0000554
Chris Lattner8a425862009-01-16 07:36:28 +0000555 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner88ea93e2009-02-04 01:06:56 +0000556 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000557
Ted Kremenek6efb0262008-03-27 06:17:42 +0000558 if (EndLineNo < StartLineNo)
559 return;
Mike Stump11289f42009-09-09 15:08:12 +0000560
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000561 if (SM.getFileID(InstantiationStart) != BugFileID ||
562 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek6efb0262008-03-27 06:17:42 +0000563 return;
Mike Stump11289f42009-09-09 15:08:12 +0000564
Ted Kremenek6efb0262008-03-27 06:17:42 +0000565 // Compute the column number of the end.
Chris Lattnere4ad4172009-02-04 00:55:58 +0000566 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek6efb0262008-03-27 06:17:42 +0000567 unsigned OldEndColNo = EndColNo;
568
569 if (EndColNo) {
570 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner184e65d2009-04-14 23:22:57 +0000571 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
Ted Kremenek6efb0262008-03-27 06:17:42 +0000572 }
Mike Stump11289f42009-09-09 15:08:12 +0000573
Ted Kremenek6efb0262008-03-27 06:17:42 +0000574 // Highlight the range. Make the span tag the outermost tag for the
575 // selected range.
Mike Stump11289f42009-09-09 15:08:12 +0000576
Chris Lattner8a425862009-01-16 07:36:28 +0000577 SourceLocation E =
578 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Mike Stump11289f42009-09-09 15:08:12 +0000579
Douglas Gregor87f95b02009-02-26 21:00:50 +0000580 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek6efb0262008-03-27 06:17:42 +0000581}