blob: 3ba7abf381682b82c0a04e6e058dd285199d2a35 [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;
Daniel Dunbarefceabd2009-11-05 02:41:58 +000040 const Preprocessor &PP;
Ted Kremenekf7556062009-07-27 22:13:39 +000041 std::vector<const PathDiagnostic*> BatchedDiags;
Ted Kremenek88f5cde2008-03-27 06:17:42 +000042public:
Daniel Dunbarefceabd2009-11-05 02:41:58 +000043 HTMLDiagnostics(const std::string& prefix, const Preprocessor &pp);
Ted Kremenekb697a4e2009-11-05 02:09:23 +000044
45 virtual ~HTMLDiagnostics() { FlushDiagnostics(NULL); }
46
47 virtual void FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade);
Mike Stump1eb44332009-09-09 15:08:12 +000048
Ted Kremenek55851142008-04-22 16:15:03 +000049 virtual void HandlePathDiagnostic(const PathDiagnostic* D);
Ted Kremenekb697a4e2009-11-05 02:09:23 +000050
51 virtual llvm::StringRef getName() const {
52 return "HTMLDiagnostics";
53 }
Mike Stump1eb44332009-09-09 15:08:12 +000054
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000055 unsigned ProcessMacroPiece(llvm::raw_ostream& os,
56 const PathDiagnosticMacroPiece& P,
57 unsigned num);
Mike Stump1eb44332009-09-09 15:08:12 +000058
Chris Lattner2b2453a2009-01-17 06:22:33 +000059 void HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +000060 const PathDiagnosticPiece& P, unsigned num, unsigned max);
Mike Stump1eb44332009-09-09 15:08:12 +000061
Douglas Gregor4b2d3f72009-02-26 21:00:50 +000062 void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
63 const char *HighlightStart = "<span class=\"mrange\">",
64 const char *HighlightEnd = "</span>");
Ted Kremenek55851142008-04-22 16:15:03 +000065
Ted Kremenekb697a4e2009-11-05 02:09:23 +000066 void ReportDiag(const PathDiagnostic& D,
67 llvm::SmallVectorImpl<std::string> *FilesMade);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000068};
Mike Stump1eb44332009-09-09 15:08:12 +000069
Ted Kremenek88f5cde2008-03-27 06:17:42 +000070} // end anonymous namespace
71
Daniel Dunbarefceabd2009-11-05 02:41:58 +000072HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix,
73 const Preprocessor &pp)
Ted Kremenek47abe762008-04-16 16:39:56 +000074 : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
Ted Kremenekb697a4e2009-11-05 02:09:23 +000075 PP(pp) {
Mike Stump1eb44332009-09-09 15:08:12 +000076 // All html files begin with "report"
Ted Kremenek88f5cde2008-03-27 06:17:42 +000077 FilePrefix.appendComponent("report");
78}
79
80PathDiagnosticClient*
Daniel Dunbarefceabd2009-11-05 02:41:58 +000081clang::CreateHTMLDiagnosticClient(const std::string& prefix,
82 const Preprocessor &PP) {
Ted Kremenekb697a4e2009-11-05 02:09:23 +000083 return new HTMLDiagnostics(prefix, PP);
Ted Kremenek88f5cde2008-03-27 06:17:42 +000084}
85
86//===----------------------------------------------------------------------===//
87// Report processing.
88//===----------------------------------------------------------------------===//
89
Ted Kremenek55851142008-04-22 16:15:03 +000090void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
91 if (!D)
Ted Kremenek88f5cde2008-03-27 06:17:42 +000092 return;
Mike Stump1eb44332009-09-09 15:08:12 +000093
Ted Kremenek55851142008-04-22 16:15:03 +000094 if (D->empty()) {
95 delete D;
96 return;
97 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenek7db0a942009-04-02 05:17:38 +000099 const_cast<PathDiagnostic*>(D)->flattenLocations();
Ted Kremenek55851142008-04-22 16:15:03 +0000100 BatchedDiags.push_back(D);
101}
102
Ted Kremenekb697a4e2009-11-05 02:09:23 +0000103void
104HTMLDiagnostics::FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade)
105{
Ted Kremenek55851142008-04-22 16:15:03 +0000106 while (!BatchedDiags.empty()) {
107 const PathDiagnostic* D = BatchedDiags.back();
108 BatchedDiags.pop_back();
Ted Kremenekb697a4e2009-11-05 02:09:23 +0000109 ReportDiag(*D, FilesMade);
Ted Kremenek55851142008-04-22 16:15:03 +0000110 delete D;
Mike Stump1eb44332009-09-09 15:08:12 +0000111 }
Ted Kremenekcca5e1e2009-11-13 03:02:57 +0000112
113 BatchedDiags.clear();
Ted Kremenek55851142008-04-22 16:15:03 +0000114}
115
Ted Kremenekb697a4e2009-11-05 02:09:23 +0000116void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
117 llvm::SmallVectorImpl<std::string> *FilesMade){
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000118 // Create the HTML directory if it is missing.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000119 if (!createdDir) {
120 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000121 std::string ErrorMsg;
122 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000124 if (!Directory.isDirectory()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000125 llvm::errs() << "warning: could not create directory '"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000126 << Directory.str() << "'\n"
Mike Stump1eb44332009-09-09 15:08:12 +0000127 << "reason: " << ErrorMsg << '\n';
128
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000129 noDir = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000131 return;
132 }
133 }
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000135 if (noDir)
136 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000138 const SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000139 FileID FID;
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000141 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000142 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000143 FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc();
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Chris Lattner2b2453a2009-01-17 06:22:33 +0000145 if (FID.isInvalid()) {
Chris Lattnera11d6172009-01-19 07:46:45 +0000146 FID = SMgr.getFileID(L);
147 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000148 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000150 // Check the source ranges.
151 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
152 RE=I->ranges_end(); RI!=RE; ++RI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000154 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000155
Chris Lattnera11d6172009-01-19 07:46:45 +0000156 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000157 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000159 L = SMgr.getInstantiationLoc(RI->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Chris Lattnera11d6172009-01-19 07:46:45 +0000161 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Mike Stump1eb44332009-09-09 15:08:12 +0000162 return; // FIXME: Emit a warning?
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000163 }
164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Chris Lattner2b2453a2009-01-17 06:22:33 +0000166 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000167 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000169 // Create a new rewriter to generate HTML.
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000170 Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOptions());
Mike Stump1eb44332009-09-09 15:08:12 +0000171
172 // Process the path.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000173 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000174 unsigned max = n;
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000176 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
Chris Lattner409d4e72009-04-17 20:40:01 +0000177 I!=E; ++I, --n)
Chris Lattner2b2453a2009-01-17 06:22:33 +0000178 HandlePiece(R, FID, *I, n, max);
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000180 // Add line numbers, header, footer, etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Chris Lattner2b2453a2009-01-17 06:22:33 +0000182 // unsigned FID = R.getSourceMgr().getMainFileID();
183 html::EscapeText(R, FID);
184 html::AddLineNumbers(R, FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremenek47abe762008-04-16 16:39:56 +0000186 // If we have a preprocessor, relex the file and syntax highlight.
187 // We might not have a preprocessor if we come from a deserialized AST file,
188 // for example.
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000190 html::SyntaxHighlight(R, FID, PP);
191 html::HighlightMacros(R, FID, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Ted Kremenekb9476392008-04-02 20:44:16 +0000193 // Get the full directory name of the analyzed file.
194
Chris Lattner2b2453a2009-01-17 06:22:33 +0000195 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremenek7fc89572008-04-24 23:37:03 +0000197 // This is a cludge; basically we want to append either the full
198 // working directory if we have no directory information. This is
199 // a work in progress.
200
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000201 std::string DirName = "";
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000203 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
204 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000205 DirName = P.str() + "/";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000206 }
Mike Stump1eb44332009-09-09 15:08:12 +0000207
208 // Add the name of the file as an <h1> tag.
209
Ted Kremenek2e939812008-03-27 07:35:49 +0000210 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000211 std::string s;
212 llvm::raw_string_ostream os(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Ted Kremenek778246a2008-09-22 17:33:32 +0000214 os << "<!-- REPORTHEADER -->\n"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000215 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000216 "<tr><td class=\"rowname\">File:</td><td>"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000217 << html::EscapeText(DirName)
218 << html::EscapeText(Entry->getName())
219 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
Mike Stump1eb44332009-09-09 15:08:12 +0000220 "<a href=\"#EndPath\">line "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000221 << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber()
222 << ", column "
223 << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber()
224 << "</a></td></tr>\n"
225 "<tr><td class=\"rowname\">Description:</td><td>"
226 << D.getDescription() << "</td></tr>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Ted Kremenek072192b2008-04-30 23:47:44 +0000228 // Output any other meta data.
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Ted Kremenek072192b2008-04-30 23:47:44 +0000230 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
231 I!=E; ++I) {
232 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
233 }
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Ted Kremenek778246a2008-09-22 17:33:32 +0000235 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
Mike Stump1eb44332009-09-09 15:08:12 +0000236 "<h3>Annotated Source Code</h3>\n";
237
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000238 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000239 }
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremenekb9476392008-04-02 20:44:16 +0000241 // Embed meta-data tags.
Ted Kremenekb9476392008-04-02 20:44:16 +0000242 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000243 std::string s;
244 llvm::raw_string_ostream os(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000245
246 const std::string& BugDesc = D.getDescription();
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000247 if (!BugDesc.empty())
248 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000250 const std::string& BugType = D.getBugType();
251 if (!BugType.empty())
252 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000253
254 const std::string& BugCategory = D.getCategory();
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000255 if (!BugCategory.empty())
256 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
257
Ted Kremenek7fc89572008-04-24 23:37:03 +0000258 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000259
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000260 os << "\n<!-- BUGLINE "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000261 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
262 << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000263
264 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000266 // Mark the end of the tags.
267 os << "\n<!-- BUGMETAEND -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000269 // Insert the text.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000270 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000271 }
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000273 // Add CSS, header, and footer.
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattner2b2453a2009-01-17 06:22:33 +0000275 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000277 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000278 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000280 if (!Buf) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000281 llvm::errs() << "warning: no diagnostics generated for main file.\n";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000282 return;
283 }
284
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000285 // Create a path for the target HTML file.
286 llvm::sys::Path F(FilePrefix);
287 F.makeUnique(false, NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000289 // Rename the file with an HTML extension.
290 llvm::sys::Path H(F);
291 H.appendSuffix("html");
292 F.renamePathOnDisk(H, NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000294 std::string ErrorMsg;
295 llvm::raw_fd_ostream os(H.c_str(), ErrorMsg);
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000297 if (!ErrorMsg.empty()) {
298 (llvm::errs() << "warning: could not create file '" << F.str()
299 << "'\n").flush();
300 return;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000303 if (FilesMade)
304 FilesMade->push_back(H.getLast());
305
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000306 // Emit the HTML to disk.
Ted Kremenek7414dc02008-04-20 01:02:33 +0000307 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000308 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000309}
310
Chris Lattner2b2453a2009-01-17 06:22:33 +0000311void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000312 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000313 unsigned num, unsigned max) {
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000315 // For now, just draw a box above the line in question, and emit the
316 // warning.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000317 FullSourceLoc Pos = P.getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000319 if (!Pos.isValid())
Mike Stump1eb44332009-09-09 15:08:12 +0000320 return;
321
Chris Lattner2b2453a2009-01-17 06:22:33 +0000322 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000323 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
324 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattner7da5aea2009-02-04 00:55:58 +0000326 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000327 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Chris Lattner7da5aea2009-02-04 00:55:58 +0000329 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Mike Stump1eb44332009-09-09 15:08:12 +0000330 const char* FileStart = Buf->getBufferStart();
331
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000332 // Compute the column number. Rewind from the current position to the start
333 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000334 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
335 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000336 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000337
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000338 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000339 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000340 const char* FileEnd = Buf->getBufferEnd();
341 while (*LineEnd != '\n' && LineEnd != FileEnd)
342 ++LineEnd;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000344 // Compute the margin offset by counting tabs and non-tabs.
Mike Stump1eb44332009-09-09 15:08:12 +0000345 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000346 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000347 PosNo += *c == '\t' ? 8 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000349 // Create the html for the message.
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000350
351 const char *Kind = 0;
352 switch (P.getKind()) {
353 case PathDiagnosticPiece::Event: Kind = "Event"; break;
354 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
355 // Setting Kind to "Control" is intentional.
356 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000359 std::string sbuf;
360 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000362 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000364 if (num == max)
365 os << "EndPath";
366 else
367 os << "Path" << num;
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000369 os << "\" class=\"msg";
370 if (Kind)
Mike Stump1eb44332009-09-09 15:08:12 +0000371 os << " msg" << Kind;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000372 os << "\" style=\"margin-left:" << PosNo << "ex";
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000374 // Output a maximum size.
375 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000376 // Get the string and determining its maximum substring.
377 const std::string& Msg = P.getString();
378 unsigned max_token = 0;
379 unsigned cnt = 0;
380 unsigned len = Msg.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000382 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
383 switch (*I) {
384 default:
385 ++cnt;
Mike Stump1eb44332009-09-09 15:08:12 +0000386 continue;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000387 case ' ':
388 case '\t':
389 case '\n':
390 if (cnt > max_token) max_token = cnt;
391 cnt = 0;
392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000394 if (cnt > max_token)
395 max_token = cnt;
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000397 // Determine the approximate size of the message bubble in em.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000398 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000399 const unsigned max_line = 120;
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000401 if (max_token >= max_line)
402 em = max_token / 2;
403 else {
404 unsigned characters = max_line;
405 unsigned lines = len / max_line;
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000407 if (lines > 0) {
408 for (; characters > max_token; --characters)
409 if (len / characters > lines) {
410 ++characters;
411 break;
412 }
413 }
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000415 em = characters / 2;
416 }
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000418 if (em < max_line/2)
Mike Stump1eb44332009-09-09 15:08:12 +0000419 os << "; max-width:" << em << "em";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000420 }
421 else
422 os << "; max-width:100em";
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000424 os << "\">";
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000426 if (max > 1) {
427 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
428 os << "<div class=\"PathIndex";
429 if (Kind) os << " PathIndex" << Kind;
430 os << "\">" << num << "</div>";
431 os << "</td><td>";
432 }
433
434 if (const PathDiagnosticMacroPiece *MP =
Mike Stump1eb44332009-09-09 15:08:12 +0000435 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000436
437 os << "Within the expansion of the macro '";
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000439 // Get the name of the macro by relexing it.
440 {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000441 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000442 assert(L.isFileID());
443 std::pair<const char*, const char*> BufferInfo = L.getBufferData();
444 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.first;
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000445 Lexer rawLexer(L, PP.getLangOptions(), BufferInfo.first,
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000446 MacroName, BufferInfo.second);
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000448 Token TheTok;
449 rawLexer.LexFromRawLexer(TheTok);
450 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
451 os << MacroName[i];
Ted Kremenek80bae762009-03-02 23:05:40 +0000452 }
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000454 os << "':\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000456 if (max > 1)
Ted Kremenek80bae762009-03-02 23:05:40 +0000457 os << "</td></tr></table>";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000458
459 // Within a macro piece. Write out each event.
460 ProcessMacroPiece(os, *MP, 0);
461 }
462 else {
463 os << html::EscapeText(P.getString());
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000465 if (max > 1)
466 os << "</td></tr></table>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000467 }
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000469 os << "</div></td></tr>";
470
471 // Insert the new html.
Mike Stump1eb44332009-09-09 15:08:12 +0000472 unsigned DisplayPos = LineEnd - FileStart;
473 SourceLocation Loc =
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000474 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
475
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000476 R.InsertTextBefore(Loc, os.str());
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000477
Mike Stump1eb44332009-09-09 15:08:12 +0000478 // Now highlight the ranges.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000479 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
480 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000481 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000482
483#if 0
484 // If there is a code insertion hint, insert that code.
485 // FIXME: This code is disabled because it seems to mangle the HTML
486 // output. I'm leaving it here because it's generally the right idea,
487 // but needs some help from someone more familiar with the rewriter.
488 for (const CodeModificationHint *Hint = P.code_modifications_begin(),
489 *HintEnd = P.code_modifications_end();
490 Hint != HintEnd; ++Hint) {
491 if (Hint->RemoveRange.isValid()) {
492 HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
493 "<span class=\"CodeRemovalHint\">", "</span>");
494 }
495 if (Hint->InsertionLoc.isValid()) {
496 std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
497 EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
498 + "</span>";
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000499 R.InsertTextBefore(Hint->InsertionLoc, EscapedCode);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000500 }
501 }
502#endif
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000503}
504
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000505static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) {
506 llvm::SmallVector<char, 10> buf;
507
508 do {
509 unsigned x = n % ('z' - 'a');
510 buf.push_back('a' + x);
511 n = n / ('z' - 'a');
512 } while (n);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000514 assert(!buf.empty());
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000516 for (llvm::SmallVectorImpl<char>::reverse_iterator I=buf.rbegin(),
517 E=buf.rend(); I!=E; ++I)
518 os << *I;
519}
520
521unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
522 const PathDiagnosticMacroPiece& P,
523 unsigned num) {
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000525 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
526 I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000528 if (const PathDiagnosticMacroPiece *MP =
529 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
530 num = ProcessMacroPiece(os, *MP, num);
531 continue;
532 }
533
534 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
535 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
536 "margin-left:5px\">"
537 "<table class=\"msgT\"><tr>"
538 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
539 EmitAlphaCounter(os, num++);
540 os << "</div></td><td valign=\"top\">"
541 << html::EscapeText(EP->getString())
542 << "</td></tr></table></div>\n";
543 }
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000546 return num;
547}
548
Chris Lattner2b2453a2009-01-17 06:22:33 +0000549void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000550 SourceRange Range,
551 const char *HighlightStart,
552 const char *HighlightEnd) {
Chris Lattner2c78b872009-04-14 23:22:57 +0000553 SourceManager &SM = R.getSourceMgr();
554 const LangOptions &LangOpts = R.getLangOpts();
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000556 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000557 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000559 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000560 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000562 if (EndLineNo < StartLineNo)
563 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Chris Lattnera11d6172009-01-19 07:46:45 +0000565 if (SM.getFileID(InstantiationStart) != BugFileID ||
566 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000567 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000569 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000570 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000571 unsigned OldEndColNo = EndColNo;
572
573 if (EndColNo) {
574 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +0000575 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000576 }
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000578 // Highlight the range. Make the span tag the outermost tag for the
579 // selected range.
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000581 SourceLocation E =
582 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000584 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000585}