blob: 648ecac3bf42a61a9ac10b9b8aeaf6afcc2d22dc [file] [log] [blame]
Ted Kremenek88f5cde2008-03-27 06:17:42 +00001//===--- HTMLDiagnostics.cpp - HTML Diagnostics for Paths ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the HTMLDiagnostics object.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000014#include "clang/Frontend/PathDiagnosticClients.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000015#include "clang/Analysis/PathDiagnostic.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000018#include "clang/Basic/SourceManager.h"
Ted Kremenek2e939812008-03-27 07:35:49 +000019#include "clang/Basic/FileManager.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000020#include "clang/Rewrite/Rewriter.h"
21#include "clang/Rewrite/HTMLRewrite.h"
22#include "clang/Lex/Lexer.h"
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000023#include "clang/Lex/Preprocessor.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000024#include "llvm/Support/Compiler.h"
25#include "llvm/Support/MemoryBuffer.h"
Ted Kremeneka95d3752008-09-13 05:16:45 +000026#include "llvm/Support/raw_ostream.h"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000027#include "llvm/System/Path.h"
Ted Kremenekb8fc3252009-10-08 17:44:41 +000028
Ted Kremenek88f5cde2008-03-27 06:17:42 +000029using namespace clang;
30
31//===----------------------------------------------------------------------===//
32// Boilerplate.
33//===----------------------------------------------------------------------===//
34
35namespace {
36
37class VISIBILITY_HIDDEN HTMLDiagnostics : public PathDiagnosticClient {
38 llvm::sys::Path Directory, FilePrefix;
39 bool createdDir, noDir;
Ted Kremenek47abe762008-04-16 16:39:56 +000040 Preprocessor* PP;
Ted Kremenekf7556062009-07-27 22:13:39 +000041 std::vector<const PathDiagnostic*> BatchedDiags;
Mike Stump1eb44332009-09-09 15:08:12 +000042 llvm::SmallVectorImpl<std::string> *FilesMade;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000043public:
Ted Kremenekf7556062009-07-27 22:13:39 +000044 HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
45 llvm::SmallVectorImpl<std::string> *filesMade = 0);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000046
Ted Kremenek55851142008-04-22 16:15:03 +000047 virtual ~HTMLDiagnostics();
Mike Stump1eb44332009-09-09 15:08:12 +000048
Chris Lattner409d4e72009-04-17 20:40:01 +000049 virtual void SetPreprocessor(Preprocessor *pp) { PP = pp; }
Mike Stump1eb44332009-09-09 15:08:12 +000050
Ted Kremenek55851142008-04-22 16:15:03 +000051 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Mike Stump1eb44332009-09-09 15:08:12 +000052
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000053 unsigned ProcessMacroPiece(llvm::raw_ostream& os,
54 const PathDiagnosticMacroPiece& P,
55 unsigned num);
Mike Stump1eb44332009-09-09 15:08:12 +000056
Chris Lattner2b2453a2009-01-17 06:22:33 +000057 void HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +000058 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Mike Stump1eb44332009-09-09 15:08:12 +000059
Douglas Gregor4b2d3f72009-02-26 21:00:50 +000060 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
61 const char *HighlightStart = "<span class=\"mrange\">",
62 const char *HighlightEnd = "</span>");
Ted Kremenek55851142008-04-22 16:15:03 +000063
64 void ReportDiag(const PathDiagnostic& D);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000065};
Mike Stump1eb44332009-09-09 15:08:12 +000066
Ted Kremenek88f5cde2008-03-27 06:17:42 +000067} // end anonymous namespace
68
Ted Kremenekf7556062009-07-27 22:13:39 +000069HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
70 llvm::SmallVectorImpl<std::string>* filesMade)
Ted Kremenek47abe762008-04-16 16:39:56 +000071 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenekf7556062009-07-27 22:13:39 +000072 PP(pp), FilesMade(filesMade) {
Mike Stump1eb44332009-09-09 15:08:12 +000073
74 // All html files begin with "report"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000075 FilePrefix.appendComponent("report");
76}
77
78PathDiagnosticClient*
Ted Kremenek339b9c22008-04-17 22:31:54 +000079clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
Ted Kremenekf7556062009-07-27 22:13:39 +000080 llvm::SmallVectorImpl<std::string>* FilesMade)
81{
82 return new HTMLDiagnostics(prefix, PP, FilesMade);
83}
84
85//===----------------------------------------------------------------------===//
86// Factory for HTMLDiagnosticClients
87//===----------------------------------------------------------------------===//
88
89namespace {
90class VISIBILITY_HIDDEN HTMLDiagnosticsFactory
91 : public PathDiagnosticClientFactory {
92
93 std::string Prefix;
94 Preprocessor *PP;
95public:
96 HTMLDiagnosticsFactory(const std::string& prefix, Preprocessor* pp)
97 : Prefix(prefix), PP(pp) {}
98
99 virtual ~HTMLDiagnosticsFactory() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Ted Kremenekf7556062009-07-27 22:13:39 +0000101 const char *getName() const { return "HTMLDiagnostics"; }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenekf7556062009-07-27 22:13:39 +0000103 PathDiagnosticClient*
104 createPathDiagnosticClient(llvm::SmallVectorImpl<std::string> *FilesMade) {
105
106 return new HTMLDiagnostics(Prefix, PP, FilesMade);
107 }
108};
109} // end anonymous namespace
110
111PathDiagnosticClientFactory*
112clang::CreateHTMLDiagnosticClientFactory(const std::string& prefix,
Daniel Dunbar90b18272009-11-04 23:56:25 +0000113 Preprocessor* PP) {
Ted Kremenekf7556062009-07-27 22:13:39 +0000114 return new HTMLDiagnosticsFactory(prefix, PP);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000115}
116
117//===----------------------------------------------------------------------===//
118// Report processing.
119//===----------------------------------------------------------------------===//
120
Ted Kremenek55851142008-04-22 16:15:03 +0000121void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
122 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000123 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenek55851142008-04-22 16:15:03 +0000125 if (D->empty()) {
126 delete D;
127 return;
128 }
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Ted Kremenek7db0a942009-04-02 05:17:38 +0000130 const_cast<PathDiagnostic*>(D)->flattenLocations();
Ted Kremenek55851142008-04-22 16:15:03 +0000131 BatchedDiags.push_back(D);
132}
133
134HTMLDiagnostics::~HTMLDiagnostics() {
Ted Kremenek55851142008-04-22 16:15:03 +0000135 while (!BatchedDiags.empty()) {
136 const PathDiagnostic* D = BatchedDiags.back();
137 BatchedDiags.pop_back();
138 ReportDiag(*D);
139 delete D;
Mike Stump1eb44332009-09-09 15:08:12 +0000140 }
Ted Kremenek55851142008-04-22 16:15:03 +0000141}
142
143void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) {
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000144 // Create the HTML directory if it is missing.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000145 if (!createdDir) {
146 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000147 std::string ErrorMsg;
148 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000150 if (!Directory.isDirectory()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000151 llvm::errs() << "warning: could not create directory '"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000152 << Directory.str() << "'\n"
Mike Stump1eb44332009-09-09 15:08:12 +0000153 << "reason: " << ErrorMsg << '\n';
154
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000155 noDir = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000157 return;
158 }
159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000161 if (noDir)
162 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000164 const SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000165 FileID FID;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000167 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000168 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000169 FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc();
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Chris Lattner2b2453a2009-01-17 06:22:33 +0000171 if (FID.isInvalid()) {
Chris Lattnera11d6172009-01-19 07:46:45 +0000172 FID = SMgr.getFileID(L);
173 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000174 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000176 // Check the source ranges.
177 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
178 RE=I->ranges_end(); RI!=RE; ++RI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000180 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000181
Chris Lattnera11d6172009-01-19 07:46:45 +0000182 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000183 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000185 L = SMgr.getInstantiationLoc(RI->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Chris Lattnera11d6172009-01-19 07:46:45 +0000187 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Mike Stump1eb44332009-09-09 15:08:12 +0000188 return; // FIXME: Emit a warning?
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000189 }
190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Chris Lattner2b2453a2009-01-17 06:22:33 +0000192 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000193 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000195 // Create a new rewriter to generate HTML.
Chris Lattner2c78b872009-04-14 23:22:57 +0000196 Rewriter R(const_cast<SourceManager&>(SMgr), PP->getLangOptions());
Mike Stump1eb44332009-09-09 15:08:12 +0000197
198 // Process the path.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000199 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000200 unsigned max = n;
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000202 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
Chris Lattner409d4e72009-04-17 20:40:01 +0000203 I!=E; ++I, --n)
Chris Lattner2b2453a2009-01-17 06:22:33 +0000204 HandlePiece(R, FID, *I, n, max);
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000206 // Add line numbers, header, footer, etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Chris Lattner2b2453a2009-01-17 06:22:33 +0000208 // unsigned FID = R.getSourceMgr().getMainFileID();
209 html::EscapeText(R, FID);
210 html::AddLineNumbers(R, FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenek47abe762008-04-16 16:39:56 +0000212 // If we have a preprocessor, relex the file and syntax highlight.
213 // We might not have a preprocessor if we come from a deserialized AST file,
214 // for example.
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Chris Lattner2b2453a2009-01-17 06:22:33 +0000216 if (PP) html::SyntaxHighlight(R, FID, *PP);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000217 if (PP) html::HighlightMacros(R, FID, *PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Ted Kremenekb9476392008-04-02 20:44:16 +0000219 // Get the full directory name of the analyzed file.
220
Chris Lattner2b2453a2009-01-17 06:22:33 +0000221 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Ted Kremenek7fc89572008-04-24 23:37:03 +0000223 // This is a cludge; basically we want to append either the full
224 // working directory if we have no directory information. This is
225 // a work in progress.
226
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000227 std::string DirName = "";
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000229 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
230 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000231 DirName = P.str() + "/";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000232 }
Mike Stump1eb44332009-09-09 15:08:12 +0000233
234 // Add the name of the file as an <h1> tag.
235
Ted Kremenek2e939812008-03-27 07:35:49 +0000236 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000237 std::string s;
238 llvm::raw_string_ostream os(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Ted Kremenek778246a2008-09-22 17:33:32 +0000240 os << "<!-- REPORTHEADER -->\n"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000241 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000242 "<tr><td class=\"rowname\">File:</td><td>"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000243 << html::EscapeText(DirName)
244 << html::EscapeText(Entry->getName())
245 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
Mike Stump1eb44332009-09-09 15:08:12 +0000246 "<a href=\"#EndPath\">line "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000247 << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber()
248 << ", column "
249 << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber()
250 << "</a></td></tr>\n"
251 "<tr><td class=\"rowname\">Description:</td><td>"
252 << D.getDescription() << "</td></tr>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Ted Kremenek072192b2008-04-30 23:47:44 +0000254 // Output any other meta data.
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Ted Kremenek072192b2008-04-30 23:47:44 +0000256 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
257 I!=E; ++I) {
258 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
259 }
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Ted Kremenek778246a2008-09-22 17:33:32 +0000261 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
Mike Stump1eb44332009-09-09 15:08:12 +0000262 "<h3>Annotated Source Code</h3>\n";
263
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000264 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Ted Kremenekb9476392008-04-02 20:44:16 +0000267 // Embed meta-data tags.
Ted Kremenekb9476392008-04-02 20:44:16 +0000268 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000269 std::string s;
270 llvm::raw_string_ostream os(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000271
272 const std::string& BugDesc = D.getDescription();
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000273 if (!BugDesc.empty())
274 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000276 const std::string& BugType = D.getBugType();
277 if (!BugType.empty())
278 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000279
280 const std::string& BugCategory = D.getCategory();
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000281 if (!BugCategory.empty())
282 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
283
Ted Kremenek7fc89572008-04-24 23:37:03 +0000284 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000285
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000286 os << "\n<!-- BUGLINE "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000287 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
288 << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000289
290 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000292 // Mark the end of the tags.
293 os << "\n<!-- BUGMETAEND -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000295 // Insert the text.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000296 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000297 }
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000299 // Add CSS, header, and footer.
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Chris Lattner2b2453a2009-01-17 06:22:33 +0000301 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000303 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000304 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000306 if (!Buf) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000307 llvm::errs() << "warning: no diagnostics generated for main file.\n";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000308 return;
309 }
310
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000311 // Create a path for the target HTML file.
312 llvm::sys::Path F(FilePrefix);
313 F.makeUnique(false, NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000315 // Rename the file with an HTML extension.
316 llvm::sys::Path H(F);
317 H.appendSuffix("html");
318 F.renamePathOnDisk(H, NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000320 std::string ErrorMsg;
321 llvm::raw_fd_ostream os(H.c_str(), ErrorMsg);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000323 if (!ErrorMsg.empty()) {
324 (llvm::errs() << "warning: could not create file '" << F.str()
325 << "'\n").flush();
326 return;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000327 }
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000329 if (FilesMade)
330 FilesMade->push_back(H.getLast());
331
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000332 // Emit the HTML to disk.
Ted Kremenek7414dc02008-04-20 01:02:33 +0000333 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000334 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000335}
336
Chris Lattner2b2453a2009-01-17 06:22:33 +0000337void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000338 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000339 unsigned num, unsigned max) {
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000341 // For now, just draw a box above the line in question, and emit the
342 // warning.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000343 FullSourceLoc Pos = P.getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000345 if (!Pos.isValid())
Mike Stump1eb44332009-09-09 15:08:12 +0000346 return;
347
Chris Lattner2b2453a2009-01-17 06:22:33 +0000348 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000349 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
350 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattner7da5aea2009-02-04 00:55:58 +0000352 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000353 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattner7da5aea2009-02-04 00:55:58 +0000355 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Mike Stump1eb44332009-09-09 15:08:12 +0000356 const char* FileStart = Buf->getBufferStart();
357
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000358 // Compute the column number. Rewind from the current position to the start
359 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000360 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
361 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000362 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000363
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000364 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000365 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000366 const char* FileEnd = Buf->getBufferEnd();
367 while (*LineEnd != '\n' && LineEnd != FileEnd)
368 ++LineEnd;
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000370 // Compute the margin offset by counting tabs and non-tabs.
Mike Stump1eb44332009-09-09 15:08:12 +0000371 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000372 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000373 PosNo += *c == '\t' ? 8 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000375 // Create the html for the message.
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000376
377 const char *Kind = 0;
378 switch (P.getKind()) {
379 case PathDiagnosticPiece::Event: Kind = "Event"; break;
380 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
381 // Setting Kind to "Control" is intentional.
382 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
383 }
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000385 std::string sbuf;
386 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000388 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000390 if (num == max)
391 os << "EndPath";
392 else
393 os << "Path" << num;
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000395 os << "\" class=\"msg";
396 if (Kind)
Mike Stump1eb44332009-09-09 15:08:12 +0000397 os << " msg" << Kind;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000398 os << "\" style=\"margin-left:" << PosNo << "ex";
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000400 // Output a maximum size.
401 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000402 // Get the string and determining its maximum substring.
403 const std::string& Msg = P.getString();
404 unsigned max_token = 0;
405 unsigned cnt = 0;
406 unsigned len = Msg.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000408 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
409 switch (*I) {
410 default:
411 ++cnt;
Mike Stump1eb44332009-09-09 15:08:12 +0000412 continue;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000413 case ' ':
414 case '\t':
415 case '\n':
416 if (cnt > max_token) max_token = cnt;
417 cnt = 0;
418 }
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000420 if (cnt > max_token)
421 max_token = cnt;
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000423 // Determine the approximate size of the message bubble in em.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000424 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000425 const unsigned max_line = 120;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000427 if (max_token >= max_line)
428 em = max_token / 2;
429 else {
430 unsigned characters = max_line;
431 unsigned lines = len / max_line;
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000433 if (lines > 0) {
434 for (; characters > max_token; --characters)
435 if (len / characters > lines) {
436 ++characters;
437 break;
438 }
439 }
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000441 em = characters / 2;
442 }
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000444 if (em < max_line/2)
Mike Stump1eb44332009-09-09 15:08:12 +0000445 os << "; max-width:" << em << "em";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000446 }
447 else
448 os << "; max-width:100em";
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000450 os << "\">";
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000452 if (max > 1) {
453 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
454 os << "<div class=\"PathIndex";
455 if (Kind) os << " PathIndex" << Kind;
456 os << "\">" << num << "</div>";
457 os << "</td><td>";
458 }
459
460 if (const PathDiagnosticMacroPiece *MP =
Mike Stump1eb44332009-09-09 15:08:12 +0000461 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000462
463 os << "Within the expansion of the macro '";
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000465 // Get the name of the macro by relexing it.
466 {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000467 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000468 assert(L.isFileID());
469 std::pair<const char*, const char*> BufferInfo = L.getBufferData();
470 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.first;
471 Lexer rawLexer(L, PP->getLangOptions(), BufferInfo.first,
472 MacroName, BufferInfo.second);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000474 Token TheTok;
475 rawLexer.LexFromRawLexer(TheTok);
476 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
477 os << MacroName[i];
Ted Kremenek80bae762009-03-02 23:05:40 +0000478 }
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000480 os << "':\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000482 if (max > 1)
Ted Kremenek80bae762009-03-02 23:05:40 +0000483 os << "</td></tr></table>";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000484
485 // Within a macro piece. Write out each event.
486 ProcessMacroPiece(os, *MP, 0);
487 }
488 else {
489 os << html::EscapeText(P.getString());
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000491 if (max > 1)
492 os << "</td></tr></table>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000493 }
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000495 os << "</div></td></tr>";
496
497 // Insert the new html.
Mike Stump1eb44332009-09-09 15:08:12 +0000498 unsigned DisplayPos = LineEnd - FileStart;
499 SourceLocation Loc =
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000500 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
501
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000502 R.InsertTextBefore(Loc, os.str());
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000503
Mike Stump1eb44332009-09-09 15:08:12 +0000504 // Now highlight the ranges.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000505 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
506 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000507 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000508
509#if 0
510 // If there is a code insertion hint, insert that code.
511 // FIXME: This code is disabled because it seems to mangle the HTML
512 // output. I'm leaving it here because it's generally the right idea,
513 // but needs some help from someone more familiar with the rewriter.
514 for (const CodeModificationHint *Hint = P.code_modifications_begin(),
515 *HintEnd = P.code_modifications_end();
516 Hint != HintEnd; ++Hint) {
517 if (Hint->RemoveRange.isValid()) {
518 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
519 "<span class=\"CodeRemovalHint\">", "</span>");
520 }
521 if (Hint->InsertionLoc.isValid()) {
522 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
523 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
524 + "</span>";
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000525 R.InsertTextBefore(Hint->InsertionLoc, EscapedCode);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000526 }
527 }
528#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000529}
530
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000531static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
532 llvm::SmallVector<char, 10> buf;
533
534 do {
535 unsigned x = n % ('z' - 'a');
536 buf.push_back('a' + x);
537 n = n / ('z' - 'a');
538 } while (n);
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000540 assert(!buf.empty());
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000542 for (llvm::SmallVectorImpl<char>::reverse_iterator I=buf.rbegin(),
543 E=buf.rend(); I!=E; ++I)
544 os << *I;
545}
546
547unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
548 const PathDiagnosticMacroPiece& P,
549 unsigned num) {
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000551 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
552 I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000554 if (const PathDiagnosticMacroPiece *MP =
555 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
556 num = ProcessMacroPiece(os, *MP, num);
557 continue;
558 }
559
560 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
561 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
562 "margin-left:5px\">"
563 "<table class=\"msgT\"><tr>"
564 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
565 EmitAlphaCounter(os, num++);
566 os << "</div></td><td valign=\"top\">"
567 << html::EscapeText(EP->getString())
568 << "</td></tr></table></div>\n";
569 }
570 }
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000572 return num;
573}
574
Chris Lattner2b2453a2009-01-17 06:22:33 +0000575void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000576 SourceRange Range,
577 const char *HighlightStart,
578 const char *HighlightEnd) {
Chris Lattner2c78b872009-04-14 23:22:57 +0000579 SourceManager &SM = R.getSourceMgr();
580 const LangOptions &LangOpts = R.getLangOpts();
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000582 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000583 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000585 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000586 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000588 if (EndLineNo < StartLineNo)
589 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Chris Lattnera11d6172009-01-19 07:46:45 +0000591 if (SM.getFileID(InstantiationStart) != BugFileID ||
592 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000593 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000595 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000596 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000597 unsigned OldEndColNo = EndColNo;
598
599 if (EndColNo) {
600 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +0000601 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000602 }
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000604 // Highlight the range. Make the span tag the outermost tag for the
605 // selected range.
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000607 SourceLocation E =
608 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000610 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000611}