blob: 145d53f3fc6e1a38b12c75c1715831210bf00ff8 [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 Kremenek55851142008-04-22 16:15:03 +0000112}
113
Ted Kremenekb697a4e2009-11-05 02:09:23 +0000114void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
115 llvm::SmallVectorImpl<std::string> *FilesMade){
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000116 // Create the HTML directory if it is missing.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000117 if (!createdDir) {
118 createdDir = true;
Ted Kremenek344f7e32008-04-03 17:55:57 +0000119 std::string ErrorMsg;
120 Directory.createDirectoryOnDisk(true, &ErrorMsg);
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000122 if (!Directory.isDirectory()) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000123 llvm::errs() << "warning: could not create directory '"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000124 << Directory.str() << "'\n"
Mike Stump1eb44332009-09-09 15:08:12 +0000125 << "reason: " << ErrorMsg << '\n';
126
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000127 noDir = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000129 return;
130 }
131 }
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000133 if (noDir)
134 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000136 const SourceManager &SMgr = D.begin()->getLocation().getManager();
Chris Lattner2b2453a2009-01-17 06:22:33 +0000137 FileID FID;
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000139 // Verify that the entire path is from the same FileID.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000140 for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000141 FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc();
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Chris Lattner2b2453a2009-01-17 06:22:33 +0000143 if (FID.isInvalid()) {
Chris Lattnera11d6172009-01-19 07:46:45 +0000144 FID = SMgr.getFileID(L);
145 } else if (SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000146 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000148 // Check the source ranges.
149 for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
150 RE=I->ranges_end(); RI!=RE; ++RI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000152 SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000153
Chris Lattnera11d6172009-01-19 07:46:45 +0000154 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000155 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000157 L = SMgr.getInstantiationLoc(RI->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Chris Lattnera11d6172009-01-19 07:46:45 +0000159 if (!L.isFileID() || SMgr.getFileID(L) != FID)
Mike Stump1eb44332009-09-09 15:08:12 +0000160 return; // FIXME: Emit a warning?
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000161 }
162 }
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Chris Lattner2b2453a2009-01-17 06:22:33 +0000164 if (FID.isInvalid())
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000165 return; // FIXME: Emit a warning?
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenekfd8fc4e2008-07-23 23:18:15 +0000167 // Create a new rewriter to generate HTML.
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000168 Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOptions());
Mike Stump1eb44332009-09-09 15:08:12 +0000169
170 // Process the path.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000171 unsigned n = D.size();
Ted Kremenek33bd9422008-03-31 23:30:12 +0000172 unsigned max = n;
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000174 for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
Chris Lattner409d4e72009-04-17 20:40:01 +0000175 I!=E; ++I, --n)
Chris Lattner2b2453a2009-01-17 06:22:33 +0000176 HandlePiece(R, FID, *I, n, max);
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000178 // Add line numbers, header, footer, etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattner2b2453a2009-01-17 06:22:33 +0000180 // unsigned FID = R.getSourceMgr().getMainFileID();
181 html::EscapeText(R, FID);
182 html::AddLineNumbers(R, FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Ted Kremenek47abe762008-04-16 16:39:56 +0000184 // If we have a preprocessor, relex the file and syntax highlight.
185 // We might not have a preprocessor if we come from a deserialized AST file,
186 // for example.
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000188 html::SyntaxHighlight(R, FID, PP);
189 html::HighlightMacros(R, FID, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Ted Kremenekb9476392008-04-02 20:44:16 +0000191 // Get the full directory name of the analyzed file.
192
Chris Lattner2b2453a2009-01-17 06:22:33 +0000193 const FileEntry* Entry = SMgr.getFileEntryForID(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenek7fc89572008-04-24 23:37:03 +0000195 // This is a cludge; basically we want to append either the full
196 // working directory if we have no directory information. This is
197 // a work in progress.
198
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000199 std::string DirName = "";
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000201 if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
202 llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000203 DirName = P.str() + "/";
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
206 // Add the name of the file as an <h1> tag.
207
Ted Kremenek2e939812008-03-27 07:35:49 +0000208 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000209 std::string s;
210 llvm::raw_string_ostream os(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenek778246a2008-09-22 17:33:32 +0000212 os << "<!-- REPORTHEADER -->\n"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000213 << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000214 "<tr><td class=\"rowname\">File:</td><td>"
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000215 << html::EscapeText(DirName)
216 << html::EscapeText(Entry->getName())
217 << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
Mike Stump1eb44332009-09-09 15:08:12 +0000218 "<a href=\"#EndPath\">line "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000219 << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber()
220 << ", column "
221 << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber()
222 << "</a></td></tr>\n"
223 "<tr><td class=\"rowname\">Description:</td><td>"
224 << D.getDescription() << "</td></tr>\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Ted Kremenek072192b2008-04-30 23:47:44 +0000226 // Output any other meta data.
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Ted Kremenek072192b2008-04-30 23:47:44 +0000228 for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
229 I!=E; ++I) {
230 os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
231 }
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Ted Kremenek778246a2008-09-22 17:33:32 +0000233 os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
Mike Stump1eb44332009-09-09 15:08:12 +0000234 "<h3>Annotated Source Code</h3>\n";
235
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000236 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenek07615402008-04-02 07:04:46 +0000237 }
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Ted Kremenekb9476392008-04-02 20:44:16 +0000239 // Embed meta-data tags.
Ted Kremenekb9476392008-04-02 20:44:16 +0000240 {
Ted Kremeneka95d3752008-09-13 05:16:45 +0000241 std::string s;
242 llvm::raw_string_ostream os(s);
Mike Stump1eb44332009-09-09 15:08:12 +0000243
244 const std::string& BugDesc = D.getDescription();
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000245 if (!BugDesc.empty())
246 os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000248 const std::string& BugType = D.getBugType();
249 if (!BugType.empty())
250 os << "\n<!-- BUGTYPE " << BugType << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000251
252 const std::string& BugCategory = D.getCategory();
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000253 if (!BugCategory.empty())
254 os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
255
Ted Kremenek7fc89572008-04-24 23:37:03 +0000256 os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000257
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000258 os << "\n<!-- BUGLINE "
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000259 << D.back()->getLocation().asLocation().getInstantiationLineNumber()
260 << " -->\n";
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000261
262 os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000264 // Mark the end of the tags.
265 os << "\n<!-- BUGMETAEND -->\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Ted Kremenek9b4d45c2009-08-03 23:44:55 +0000267 // Insert the text.
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000268 R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
Ted Kremenekb9476392008-04-02 20:44:16 +0000269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000271 // Add CSS, header, and footer.
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Chris Lattner2b2453a2009-01-17 06:22:33 +0000273 html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000275 // Get the rewrite buffer.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000276 const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000278 if (!Buf) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000279 llvm::errs() << "warning: no diagnostics generated for main file.\n";
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000280 return;
281 }
282
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000283 // Create a path for the target HTML file.
284 llvm::sys::Path F(FilePrefix);
285 F.makeUnique(false, NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000287 // Rename the file with an HTML extension.
288 llvm::sys::Path H(F);
289 H.appendSuffix("html");
290 F.renamePathOnDisk(H, NULL);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000292 std::string ErrorMsg;
293 llvm::raw_fd_ostream os(H.c_str(), ErrorMsg);
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000295 if (!ErrorMsg.empty()) {
296 (llvm::errs() << "warning: could not create file '" << F.str()
297 << "'\n").flush();
298 return;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000299 }
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Ted Kremenekb8fc3252009-10-08 17:44:41 +0000301 if (FilesMade)
302 FilesMade->push_back(H.getLast());
303
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000304 // Emit the HTML to disk.
Ted Kremenek7414dc02008-04-20 01:02:33 +0000305 for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000306 os << *I;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000307}
308
Chris Lattner2b2453a2009-01-17 06:22:33 +0000309void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000310 const PathDiagnosticPiece& P,
Ted Kremenek33bd9422008-03-31 23:30:12 +0000311 unsigned num, unsigned max) {
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000313 // For now, just draw a box above the line in question, and emit the
314 // warning.
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000315 FullSourceLoc Pos = P.getLocation().asLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000317 if (!Pos.isValid())
Mike Stump1eb44332009-09-09 15:08:12 +0000318 return;
319
Chris Lattner2b2453a2009-01-17 06:22:33 +0000320 SourceManager &SM = R.getSourceMgr();
Chris Lattner7da5aea2009-02-04 00:55:58 +0000321 assert(&Pos.getManager() == &SM && "SourceManagers are different!");
322 std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner7da5aea2009-02-04 00:55:58 +0000324 if (LPosInfo.first != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000325 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Chris Lattner7da5aea2009-02-04 00:55:58 +0000327 const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
Mike Stump1eb44332009-09-09 15:08:12 +0000328 const char* FileStart = Buf->getBufferStart();
329
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000330 // Compute the column number. Rewind from the current position to the start
331 // of the line.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000332 unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
333 const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000334 const char *LineStart = TokInstantiationPtr-ColNo;
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000335
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000336 // Compute LineEnd.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000337 const char *LineEnd = TokInstantiationPtr;
Ted Kremenek3f0b6562009-02-18 22:10:00 +0000338 const char* FileEnd = Buf->getBufferEnd();
339 while (*LineEnd != '\n' && LineEnd != FileEnd)
340 ++LineEnd;
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Ted Kremenek2aa13b52008-03-31 21:40:14 +0000342 // Compute the margin offset by counting tabs and non-tabs.
Mike Stump1eb44332009-09-09 15:08:12 +0000343 unsigned PosNo = 0;
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000344 for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
Ted Kremenekea17d6a2008-05-06 23:42:18 +0000345 PosNo += *c == '\t' ? 8 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000347 // Create the html for the message.
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000348
349 const char *Kind = 0;
350 switch (P.getKind()) {
351 case PathDiagnosticPiece::Event: Kind = "Event"; break;
352 case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
353 // Setting Kind to "Control" is intentional.
354 case PathDiagnosticPiece::Macro: Kind = "Control"; break;
355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000357 std::string sbuf;
358 llvm::raw_string_ostream os(sbuf);
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000360 os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000362 if (num == max)
363 os << "EndPath";
364 else
365 os << "Path" << num;
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000367 os << "\" class=\"msg";
368 if (Kind)
Mike Stump1eb44332009-09-09 15:08:12 +0000369 os << " msg" << Kind;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000370 os << "\" style=\"margin-left:" << PosNo << "ex";
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000372 // Output a maximum size.
373 if (!isa<PathDiagnosticMacroPiece>(P)) {
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000374 // Get the string and determining its maximum substring.
375 const std::string& Msg = P.getString();
376 unsigned max_token = 0;
377 unsigned cnt = 0;
378 unsigned len = Msg.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000380 for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
381 switch (*I) {
382 default:
383 ++cnt;
Mike Stump1eb44332009-09-09 15:08:12 +0000384 continue;
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000385 case ' ':
386 case '\t':
387 case '\n':
388 if (cnt > max_token) max_token = cnt;
389 cnt = 0;
390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000392 if (cnt > max_token)
393 max_token = cnt;
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000395 // Determine the approximate size of the message bubble in em.
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000396 unsigned em;
Ted Kremenek4e25ee32009-03-02 23:06:15 +0000397 const unsigned max_line = 120;
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000399 if (max_token >= max_line)
400 em = max_token / 2;
401 else {
402 unsigned characters = max_line;
403 unsigned lines = len / max_line;
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000405 if (lines > 0) {
406 for (; characters > max_token; --characters)
407 if (len / characters > lines) {
408 ++characters;
409 break;
410 }
411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000413 em = characters / 2;
414 }
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000416 if (em < max_line/2)
Mike Stump1eb44332009-09-09 15:08:12 +0000417 os << "; max-width:" << em << "em";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000418 }
419 else
420 os << "; max-width:100em";
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000422 os << "\">";
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000424 if (max > 1) {
425 os << "<table class=\"msgT\"><tr><td valign=\"top\">";
426 os << "<div class=\"PathIndex";
427 if (Kind) os << " PathIndex" << Kind;
428 os << "\">" << num << "</div>";
429 os << "</td><td>";
430 }
431
432 if (const PathDiagnosticMacroPiece *MP =
Mike Stump1eb44332009-09-09 15:08:12 +0000433 dyn_cast<PathDiagnosticMacroPiece>(&P)) {
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000434
435 os << "Within the expansion of the macro '";
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000437 // Get the name of the macro by relexing it.
438 {
Ted Kremenek5fb5dfb2009-04-01 06:13:56 +0000439 FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc();
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000440 assert(L.isFileID());
441 std::pair<const char*, const char*> BufferInfo = L.getBufferData();
442 const char* MacroName = L.getDecomposedLoc().second + BufferInfo.first;
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000443 Lexer rawLexer(L, PP.getLangOptions(), BufferInfo.first,
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000444 MacroName, BufferInfo.second);
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000446 Token TheTok;
447 rawLexer.LexFromRawLexer(TheTok);
448 for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i)
449 os << MacroName[i];
Ted Kremenek80bae762009-03-02 23:05:40 +0000450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000452 os << "':\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000454 if (max > 1)
Ted Kremenek80bae762009-03-02 23:05:40 +0000455 os << "</td></tr></table>";
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000456
457 // Within a macro piece. Write out each event.
458 ProcessMacroPiece(os, *MP, 0);
459 }
460 else {
461 os << html::EscapeText(P.getString());
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000463 if (max > 1)
464 os << "</td></tr></table>";
Ted Kremeneka6aa83e2008-09-21 18:52:59 +0000465 }
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000467 os << "</div></td></tr>";
468
469 // Insert the new html.
Mike Stump1eb44332009-09-09 15:08:12 +0000470 unsigned DisplayPos = LineEnd - FileStart;
471 SourceLocation Loc =
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000472 SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
473
Daniel Dunbar44ba7bf2009-08-19 20:32:38 +0000474 R.InsertTextBefore(Loc, os.str());
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000475
Mike Stump1eb44332009-09-09 15:08:12 +0000476 // Now highlight the ranges.
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000477 for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
478 I != E; ++I)
Chris Lattner7da5aea2009-02-04 00:55:58 +0000479 HighlightRange(R, LPosInfo.first, *I);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000480
481#if 0
482 // If there is a code insertion hint, insert that code.
483 // FIXME: This code is disabled because it seems to mangle the HTML
484 // output. I'm leaving it here because it's generally the right idea,
485 // but needs some help from someone more familiar with the rewriter.
486 for (const CodeModificationHint *Hint = P.code_modifications_begin(),
487 *HintEnd = P.code_modifications_end();
488 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) {
504 llvm::SmallVector<char, 10> buf;
505
506 do {
507 unsigned x = n % ('z' - 'a');
508 buf.push_back('a' + x);
509 n = n / ('z' - 'a');
510 } while (n);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000512 assert(!buf.empty());
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000514 for (llvm::SmallVectorImpl<char>::reverse_iterator I=buf.rbegin(),
515 E=buf.rend(); I!=E; ++I)
516 os << *I;
517}
518
519unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os,
520 const PathDiagnosticMacroPiece& P,
521 unsigned num) {
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000523 for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end();
524 I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000526 if (const PathDiagnosticMacroPiece *MP =
527 dyn_cast<PathDiagnosticMacroPiece>(*I)) {
528 num = ProcessMacroPiece(os, *MP, num);
529 continue;
530 }
531
532 if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) {
533 os << "<div class=\"msg msgEvent\" style=\"width:94%; "
534 "margin-left:5px\">"
535 "<table class=\"msgT\"><tr>"
536 "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">";
537 EmitAlphaCounter(os, num++);
538 os << "</div></td><td valign=\"top\">"
539 << html::EscapeText(EP->getString())
540 << "</td></tr></table></div>\n";
541 }
542 }
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Ted Kremenek0e5c8d42009-03-10 05:16:17 +0000544 return num;
545}
546
Chris Lattner2b2453a2009-01-17 06:22:33 +0000547void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000548 SourceRange Range,
549 const char *HighlightStart,
550 const char *HighlightEnd) {
Chris Lattner2c78b872009-04-14 23:22:57 +0000551 SourceManager &SM = R.getSourceMgr();
552 const LangOptions &LangOpts = R.getLangOpts();
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000554 SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
Chris Lattner30fc9332009-02-04 01:06:56 +0000555 unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000557 SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
Chris Lattner30fc9332009-02-04 01:06:56 +0000558 unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000560 if (EndLineNo < StartLineNo)
561 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Chris Lattnera11d6172009-01-19 07:46:45 +0000563 if (SM.getFileID(InstantiationStart) != BugFileID ||
564 SM.getFileID(InstantiationEnd) != BugFileID)
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000565 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000567 // Compute the column number of the end.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000568 unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000569 unsigned OldEndColNo = EndColNo;
570
571 if (EndColNo) {
572 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +0000573 EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1;
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000574 }
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000576 // Highlight the range. Make the span tag the outermost tag for the
577 // selected range.
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000579 SourceLocation E =
580 InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000582 html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
Ted Kremenek88f5cde2008-03-27 06:17:42 +0000583}