blob: ff9867fb5f1683efd925e5609df9702fe98b6ad1 [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 Dunbar9b414d32010-06-15 17:48:49 +000014#include "clang/Checker/PathDiagnosticClients.h"
Ted Kremenek6b676302010-01-25 17:10:22 +000015#include "clang/Checker/BugReporter/PathDiagnostic.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000016#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"
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000023#include "clang/Lex/Preprocessor.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000024#include "llvm/Support/MemoryBuffer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000025#include "llvm/Support/raw_ostream.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000026#include "llvm/System/Path.h"
Ted Kremenekb8fc3252009-10-08 17:44:41 +000027
Ted Kremenek88f5cde2008-03-27 06:17:42 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// Boilerplate.
32//===----------------------------------------------------------------------===//
33
34namespace {
35
Benjamin Kramerbd218282009-11-28 10:07:24 +000036class HTMLDiagnostics : public PathDiagnosticClient {
Ted Kremenek88f5cde2008-03-27 06:17:42 +000037 llvm::sys::Path Directory, FilePrefix;
38 bool createdDir, noDir;
Daniel Dunbarefceabd2009-11-05 02:41:58 +000039 const Preprocessor &PP;
Ted Kremenekf7556062009-07-27 22:13:39 +000040 std::vector<const PathDiagnostic*> BatchedDiags;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000041public:
Daniel Dunbarefceabd2009-11-05 02:41:58 +000042 HTMLDiagnostics(const std::string& prefix, const Preprocessor &pp);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000043
Ted Kremenekb697a4e2009-11-05 02:09:23 +000044 virtual ~HTMLDiagnostics() { FlushDiagnostics(NULL); }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000045
Ted Kremenekb697a4e2009-11-05 02:09:23 +000046 virtual void FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade);
Mike Stump1eb44332009-09-09 15:08:12 +000047
Ted Kremenek55851142008-04-22 16:15:03 +000048 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000049
Ted Kremenekb697a4e2009-11-05 02:09:23 +000050 virtual llvm::StringRef getName() const {
51 return "HTMLDiagnostics";
52 }
Mike Stump1eb44332009-09-09 15:08:12 +000053
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000054 unsigned ProcessMacroPiece(llvm::raw_ostream& os,
55 const PathDiagnosticMacroPiece& P,
56 unsigned num);
Mike Stump1eb44332009-09-09 15:08:12 +000057
Chris Lattner2b2453a2009-01-17 06:22:33 +000058 void HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +000059 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Mike Stump1eb44332009-09-09 15:08:12 +000060
Douglas Gregor4b2d3f72009-02-26 21:00:50 +000061 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
62 const char *HighlightStart = "<span class=\"mrange\">",
63 const char *HighlightEnd = "</span>");
Ted Kremenek55851142008-04-22 16:15:03 +000064
Ted Kremenekb697a4e2009-11-05 02:09:23 +000065 void ReportDiag(const PathDiagnostic& D,
66 llvm::SmallVectorImpl<std::string> *FilesMade);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000067};
Mike Stump1eb44332009-09-09 15:08:12 +000068
Ted Kremenek88f5cde2008-03-27 06:17:42 +000069} // end anonymous namespace
70
Daniel Dunbarefceabd2009-11-05 02:41:58 +000071HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix,
72 const Preprocessor &pp)
Ted Kremenek47abe762008-04-16 16:39:56 +000073 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenekb697a4e2009-11-05 02:09:23 +000074 PP(pp) {
Mike Stump1eb44332009-09-09 15:08:12 +000075 // All html files begin with "report"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000076 FilePrefix.appendComponent("report");
77}
78
79PathDiagnosticClient*
Daniel Dunbarefceabd2009-11-05 02:41:58 +000080clang::CreateHTMLDiagnosticClient(const std::string& prefix,
81 const Preprocessor &PP) {
Ted Kremenekb697a4e2009-11-05 02:09:23 +000082 return new HTMLDiagnostics(prefix, PP);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000083}
84
85//===----------------------------------------------------------------------===//
86// Report processing.
87//===----------------------------------------------------------------------===//
88
Ted Kremenek55851142008-04-22 16:15:03 +000089void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
90 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +000091 return;
Mike Stump1eb44332009-09-09 15:08:12 +000092
Ted Kremenek55851142008-04-22 16:15:03 +000093 if (D->empty()) {
94 delete D;
95 return;
96 }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Ted Kremenek7db0a942009-04-02 05:17:38 +000098 const_cast<PathDiagnostic*>(D)->flattenLocations();
Ted Kremenek55851142008-04-22 16:15:03 +000099 BatchedDiags.push_back(D);
100}
101
Ted Kremenekb697a4e2009-11-05 02:09:23 +0000102void
103HTMLDiagnostics::FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade)
104{
Ted Kremenek55851142008-04-22 16:15:03 +0000105 while (!BatchedDiags.empty()) {
106 const PathDiagnostic* D = BatchedDiags.back();
107 BatchedDiags.pop_back();
Ted Kremenekb697a4e2009-11-05 02:09:23 +0000108 ReportDiag(*D, FilesMade);
Ted Kremenek55851142008-04-22 16:15:03 +0000109 delete D;
Mike Stump1eb44332009-09-09 15:08:12 +0000110 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000111
Ted Kremenekcca5e1e2009-11-13 03:02:57 +0000112 BatchedDiags.clear();
Ted Kremenek55851142008-04-22 16:15:03 +0000113}
114
Ted Kremenekb697a4e2009-11-05 02:09:23 +0000115void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
116 llvm::SmallVectorImpl<std::string> *FilesMade){
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000117 // Create the HTML directory if it is missing.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000118 if (!createdDir) {
119 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000120 std::string ErrorMsg;
121 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000123 if (!Directory.isDirectory()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000124 llvm::errs() << "warning: could not create directory '"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000125 << Directory.str() << "'\n"
Mike Stump1eb44332009-09-09 15:08:12 +0000126 << "reason: " << ErrorMsg << '\n';
127
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000128 noDir = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000130 return;
131 }
132 }
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000134 if (noDir)
135 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000137 const SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000138 FileID FID;
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000140 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000141 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000142 FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc();
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Chris Lattner2b2453a2009-01-17 06:22:33 +0000144 if (FID.isInvalid()) {
Chris Lattnera11d6172009-01-19 07:46:45 +0000145 FID = SMgr.getFileID(L);
146 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000147 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000149 // Check the source ranges.
150 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
151 RE=I->ranges_end(); RI!=RE; ++RI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000153 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000154
Chris Lattnera11d6172009-01-19 07:46:45 +0000155 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000156 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000158 L = SMgr.getInstantiationLoc(RI->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Chris Lattnera11d6172009-01-19 07:46:45 +0000160 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Mike Stump1eb44332009-09-09 15:08:12 +0000161 return; // FIXME: Emit a warning?
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000162 }
163 }
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Chris Lattner2b2453a2009-01-17 06:22:33 +0000165 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000166 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000168 // Create a new rewriter to generate HTML.
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000169 Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOptions());
Mike Stump1eb44332009-09-09 15:08:12 +0000170
171 // Process the path.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000172 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000173 unsigned max = n;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000175 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
Chris Lattner409d4e72009-04-17 20:40:01 +0000176 I!=E; ++I, --n)
Chris Lattner2b2453a2009-01-17 06:22:33 +0000177 HandlePiece(R, FID, *I, n, max);
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000179 // Add line numbers, header, footer, etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner2b2453a2009-01-17 06:22:33 +0000181 // unsigned FID = R.getSourceMgr().getMainFileID();
182 html::EscapeText(R, FID);
183 html::AddLineNumbers(R, FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Ted Kremenek47abe762008-04-16 16:39:56 +0000185 // If we have a preprocessor, relex the file and syntax highlight.
186 // We might not have a preprocessor if we come from a deserialized AST file,
187 // for example.
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000189 html::SyntaxHighlight(R, FID, PP);
190 html::HighlightMacros(R, FID, PP);
Mike Stump1eb44332009-09-09 15:08:12 +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);
Mike Stump1eb44332009-09-09 15:08:12 +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 = "";
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000202 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
203 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000204 DirName = P.str() + "/";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
207 // 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);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenek778246a2008-09-22 17:33:32 +0000213 os << "<!-- REPORTHEADER -->\n"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000214 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000215 "<tr><td class=\"rowname\">File:</td><td>"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000216 << html::EscapeText(DirName)
217 << html::EscapeText(Entry->getName())
218 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
Mike Stump1eb44332009-09-09 15:08:12 +0000219 "<a href=\"#EndPath\">line "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000220 << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber()
221 << ", column "
222 << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber()
223 << "</a></td></tr>\n"
224 "<tr><td class=\"rowname\">Description:</td><td>"
225 << D.getDescription() << "</td></tr>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremenek072192b2008-04-30 23:47:44 +0000227 // Output any other meta data.
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Ted Kremenek072192b2008-04-30 23:47:44 +0000229 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Ted Kremenek778246a2008-09-22 17:33:32 +0000234 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
Mike Stump1eb44332009-09-09 15:08:12 +0000235 "<h3>Annotated Source Code</h3>\n";
236
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000237 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000238 }
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Ted Kremenekb9476392008-04-02 20:44:16 +0000240 // Embed meta-data tags.
Ted Kremenekb9476392008-04-02 20:44:16 +0000241 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000242 std::string s;
243 llvm::raw_string_ostream os(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
245 const std::string& BugDesc = D.getDescription();
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000246 if (!BugDesc.empty())
247 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000249 const std::string& BugType = D.getBugType();
250 if (!BugType.empty())
251 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000252
253 const std::string& BugCategory = D.getCategory();
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000254 if (!BugCategory.empty())
255 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
256
Ted Kremenek7fc89572008-04-24 23:37:03 +0000257 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000258
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000259 os << "\n<!-- BUGLINE "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000260 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
261 << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000262
263 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000265 // Mark the end of the tags.
266 os << "\n<!-- BUGMETAEND -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000268 // Insert the text.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000269 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000272 // Add CSS, header, and footer.
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Chris Lattner2b2453a2009-01-17 06:22:33 +0000274 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000276 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000277 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000279 if (!Buf) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000280 llvm::errs() << "warning: no diagnostics generated for main file.\n";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000281 return;
282 }
283
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000284 // Create a path for the target HTML file.
285 llvm::sys::Path F(FilePrefix);
286 F.makeUnique(false, NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000288 // Rename the file with an HTML extension.
289 llvm::sys::Path H(F);
290 H.appendSuffix("html");
291 F.renamePathOnDisk(H, NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000293 std::string ErrorMsg;
294 llvm::raw_fd_ostream os(H.c_str(), ErrorMsg);
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000296 if (!ErrorMsg.empty()) {
Dan Gohmand784d4d2010-06-28 15:56:07 +0000297 llvm::errs() << "warning: could not create file '" << F.str()
298 << "'\n";
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000299 return;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000302 if (FilesMade)
303 FilesMade->push_back(H.getLast());
304
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000305 // Emit the HTML to disk.
Ted Kremenek7414dc02008-04-20 01:02:33 +0000306 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000307 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000308}
309
Chris Lattner2b2453a2009-01-17 06:22:33 +0000310void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000311 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000312 unsigned num, unsigned max) {
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000314 // For now, just draw a box above the line in question, and emit the
315 // warning.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000316 FullSourceLoc Pos = P.getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000318 if (!Pos.isValid())
Mike Stump1eb44332009-09-09 15:08:12 +0000319 return;
320
Chris Lattner2b2453a2009-01-17 06:22:33 +0000321 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000322 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
323 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Chris Lattner7da5aea2009-02-04 00:55:58 +0000325 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000326 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Chris Lattner7da5aea2009-02-04 00:55:58 +0000328 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Mike Stump1eb44332009-09-09 15:08:12 +0000329 const char* FileStart = Buf->getBufferStart();
330
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000331 // Compute the column number. Rewind from the current position to the start
332 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000333 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
334 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000335 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000336
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000337 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000338 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000339 const char* FileEnd = Buf->getBufferEnd();
340 while (*LineEnd != '\n' && LineEnd != FileEnd)
341 ++LineEnd;
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000343 // Compute the margin offset by counting tabs and non-tabs.
Mike Stump1eb44332009-09-09 15:08:12 +0000344 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000345 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000346 PosNo += *c == '\t' ? 8 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000348 // Create the html for the message.
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000349
350 const char *Kind = 0;
351 switch (P.getKind()) {
Mike Stumpb7166332010-01-20 02:03:14 +0000352 case PathDiagnosticPiece::Event: Kind = "Event"; break;
353 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
354 // Setting Kind to "Control" is intentional.
355 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000358 std::string sbuf;
359 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000361 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000363 if (num == max)
364 os << "EndPath";
365 else
366 os << "Path" << num;
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000368 os << "\" class=\"msg";
369 if (Kind)
Mike Stump1eb44332009-09-09 15:08:12 +0000370 os << " msg" << Kind;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000371 os << "\" style=\"margin-left:" << PosNo << "ex";
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000373 // Output a maximum size.
374 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000375 // Get the string and determining its maximum substring.
376 const std::string& Msg = P.getString();
377 unsigned max_token = 0;
378 unsigned cnt = 0;
379 unsigned len = Msg.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000381 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
382 switch (*I) {
Mike Stumpb7166332010-01-20 02:03:14 +0000383 default:
384 ++cnt;
385 continue;
386 case ' ':
387 case '\t':
388 case '\n':
389 if (cnt > max_token) max_token = cnt;
390 cnt = 0;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000393 if (cnt > max_token)
394 max_token = cnt;
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000396 // Determine the approximate size of the message bubble in em.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000397 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000398 const unsigned max_line = 120;
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000400 if (max_token >= max_line)
401 em = max_token / 2;
402 else {
403 unsigned characters = max_line;
404 unsigned lines = len / max_line;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000406 if (lines > 0) {
407 for (; characters > max_token; --characters)
408 if (len / characters > lines) {
409 ++characters;
410 break;
411 }
412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000414 em = characters / 2;
415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000417 if (em < max_line/2)
Mike Stump1eb44332009-09-09 15:08:12 +0000418 os << "; max-width:" << em << "em";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000419 }
420 else
421 os << "; max-width:100em";
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000423 os << "\">";
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000425 if (max > 1) {
426 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
427 os << "<div class=\"PathIndex";
428 if (Kind) os << " PathIndex" << Kind;
429 os << "\">" << num << "</div>";
430 os << "</td><td>";
431 }
432
433 if (const PathDiagnosticMacroPiece *MP =
Mike Stump1eb44332009-09-09 15:08:12 +0000434 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000435
436 os << "Within the expansion of the macro '";
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000438 // Get the name of the macro by relexing it.
439 {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000440 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000441 assert(L.isFileID());
Benjamin Kramerceafc4b2010-03-16 14:48:07 +0000442 llvm::StringRef BufferInfo = L.getBufferData();
443 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.data();
444 Lexer rawLexer(L, PP.getLangOptions(), BufferInfo.begin(),
445 MacroName, BufferInfo.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000447 Token TheTok;
448 rawLexer.LexFromRawLexer(TheTok);
449 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
450 os << MacroName[i];
Ted Kremenek80bae762009-03-02 23:05:40 +0000451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000453 os << "':\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000455 if (max > 1)
Ted Kremenek80bae762009-03-02 23:05:40 +0000456 os << "</td></tr></table>";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000457
458 // Within a macro piece. Write out each event.
459 ProcessMacroPiece(os, *MP, 0);
460 }
461 else {
462 os << html::EscapeText(P.getString());
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000464 if (max > 1)
465 os << "</td></tr></table>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000468 os << "</div></td></tr>";
469
470 // Insert the new html.
Mike Stump1eb44332009-09-09 15:08:12 +0000471 unsigned DisplayPos = LineEnd - FileStart;
472 SourceLocation Loc =
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000473 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
474
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000475 R.InsertTextBefore(Loc, os.str());
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000476
Mike Stump1eb44332009-09-09 15:08:12 +0000477 // Now highlight the ranges.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000478 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
479 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000480 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000481
482#if 0
483 // If there is a code insertion hint, insert that code.
484 // FIXME: This code is disabled because it seems to mangle the HTML
485 // output. I'm leaving it here because it's generally the right idea,
486 // but needs some help from someone more familiar with the rewriter.
Douglas Gregor849b2432010-03-31 17:46:05 +0000487 for (const FixItHint *Hint = P.fixit_begin(), *HintEnd = P.fixit_end();
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000488 Hint != HintEnd; ++Hint) {
489 if (Hint->RemoveRange.isValid()) {
490 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
491 "<span class=\"CodeRemovalHint\">", "</span>");
492 }
493 if (Hint->InsertionLoc.isValid()) {
494 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
495 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
496 + "</span>";
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000497 R.InsertTextBefore(Hint->InsertionLoc, EscapedCode);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000498 }
499 }
500#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000501}
502
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000503static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
Benjamin Kramer92d6cd22010-03-13 11:34:41 +0000504 unsigned x = n % ('z' - 'a');
505 n /= 'z' - 'a';
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000506
Benjamin Kramer92d6cd22010-03-13 11:34:41 +0000507 if (n > 0)
508 EmitAlphaCounter(os, n);
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Benjamin Kramer92d6cd22010-03-13 11:34:41 +0000510 os << char('a' + x);
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000511}
512
513unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
514 const PathDiagnosticMacroPiece& P,
515 unsigned num) {
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000517 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
518 I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000520 if (const PathDiagnosticMacroPiece *MP =
521 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
522 num = ProcessMacroPiece(os, *MP, num);
523 continue;
524 }
525
526 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
527 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
528 "margin-left:5px\">"
529 "<table class=\"msgT\"><tr>"
530 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
531 EmitAlphaCounter(os, num++);
532 os << "</div></td><td valign=\"top\">"
533 << html::EscapeText(EP->getString())
534 << "</td></tr></table></div>\n";
535 }
536 }
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000538 return num;
539}
540
Chris Lattner2b2453a2009-01-17 06:22:33 +0000541void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000542 SourceRange Range,
543 const char *HighlightStart,
544 const char *HighlightEnd) {
Chris Lattner2c78b872009-04-14 23:22:57 +0000545 SourceManager &SM = R.getSourceMgr();
546 const LangOptions &LangOpts = R.getLangOpts();
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000548 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000549 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000551 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000552 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000554 if (EndLineNo < StartLineNo)
555 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Chris Lattnera11d6172009-01-19 07:46:45 +0000557 if (SM.getFileID(InstantiationStart) != BugFileID ||
558 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000559 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000561 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000562 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000563 unsigned OldEndColNo = EndColNo;
564
565 if (EndColNo) {
566 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +0000567 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000568 }
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000570 // Highlight the range. Make the span tag the outermost tag for the
571 // selected range.
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000573 SourceLocation E =
574 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000576 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000577}