2008-03-27 14:17:42 +08:00
|
|
|
//===--- HTMLDiagnostics.cpp - HTML Diagnostics for Paths ----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the HTMLDiagnostics object.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "HTMLDiagnostics.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-03-27 15:35:49 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2008-03-27 14:17:42 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
|
|
|
#include "clang/Rewrite/Rewriter.h"
|
|
|
|
#include "clang/Rewrite/HTMLRewrite.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Streams.h"
|
|
|
|
#include "llvm/System/Path.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Boilerplate.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN HTMLDiagnostics : public PathDiagnosticClient {
|
|
|
|
llvm::sys::Path Directory, FilePrefix;
|
|
|
|
bool createdDir, noDir;
|
2008-04-17 00:39:56 +08:00
|
|
|
Preprocessor* PP;
|
2008-04-18 06:31:54 +08:00
|
|
|
PreprocessorFactory* PPF;
|
2008-04-23 00:15:03 +08:00
|
|
|
std::vector<const PathDiagnostic*> BatchedDiags;
|
2008-03-27 14:17:42 +08:00
|
|
|
public:
|
2008-04-18 06:31:54 +08:00
|
|
|
HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
|
|
|
|
PreprocessorFactory* ppf);
|
2008-03-27 14:17:42 +08:00
|
|
|
|
2008-04-23 00:15:03 +08:00
|
|
|
virtual ~HTMLDiagnostics();
|
2008-03-27 14:17:42 +08:00
|
|
|
|
2008-04-23 00:15:03 +08:00
|
|
|
virtual void HandlePathDiagnostic(const PathDiagnostic* D);
|
2008-03-27 14:17:42 +08:00
|
|
|
|
2008-04-01 07:30:12 +08:00
|
|
|
void HandlePiece(Rewriter& R, const PathDiagnosticPiece& P,
|
|
|
|
unsigned num, unsigned max);
|
|
|
|
|
2008-04-15 05:06:04 +08:00
|
|
|
void HighlightRange(Rewriter& R, SourceRange Range);
|
2008-04-23 00:15:03 +08:00
|
|
|
|
|
|
|
void ReportDiag(const PathDiagnostic& D);
|
2008-03-27 14:17:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-04-18 06:31:54 +08:00
|
|
|
HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp,
|
|
|
|
PreprocessorFactory* ppf)
|
2008-04-17 00:39:56 +08:00
|
|
|
: Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false),
|
2008-04-18 06:31:54 +08:00
|
|
|
PP(pp), PPF(ppf) {
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
// All html files begin with "report"
|
|
|
|
FilePrefix.appendComponent("report");
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticClient*
|
2008-04-18 06:31:54 +08:00
|
|
|
clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
|
|
|
|
PreprocessorFactory* PPF) {
|
2008-03-27 14:17:42 +08:00
|
|
|
|
2008-04-18 06:31:54 +08:00
|
|
|
return new HTMLDiagnostics(prefix, PP, PPF);
|
2008-03-27 14:17:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Report processing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-23 00:15:03 +08:00
|
|
|
void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) {
|
|
|
|
if (!D)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (D->empty()) {
|
|
|
|
delete D;
|
2008-03-27 14:17:42 +08:00
|
|
|
return;
|
2008-04-23 00:15:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BatchedDiags.push_back(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLDiagnostics::~HTMLDiagnostics() {
|
|
|
|
|
|
|
|
while (!BatchedDiags.empty()) {
|
|
|
|
const PathDiagnostic* D = BatchedDiags.back();
|
|
|
|
BatchedDiags.pop_back();
|
|
|
|
ReportDiag(*D);
|
|
|
|
delete D;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) {
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
// Create the HTML directory if it is missing.
|
|
|
|
|
|
|
|
if (!createdDir) {
|
|
|
|
createdDir = true;
|
2008-04-04 01:55:57 +08:00
|
|
|
std::string ErrorMsg;
|
|
|
|
Directory.createDirectoryOnDisk(true, &ErrorMsg);
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
if (!Directory.isDirectory()) {
|
|
|
|
llvm::cerr << "warning: could not create directory '"
|
2008-04-04 01:55:57 +08:00
|
|
|
<< Directory.toString() << "'\n"
|
|
|
|
<< "reason: " << ErrorMsg << '\n';
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
noDir = true;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noDir)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create a new rewriter to generate HTML.
|
|
|
|
SourceManager& SMgr = D.begin()->getLocation().getManager();
|
|
|
|
Rewriter R(SMgr);
|
|
|
|
|
|
|
|
// Process the path.
|
|
|
|
|
|
|
|
unsigned n = D.size();
|
2008-04-01 07:30:12 +08:00
|
|
|
unsigned max = n;
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend();
|
|
|
|
I!=E; ++I, --n) {
|
|
|
|
|
2008-04-01 07:30:12 +08:00
|
|
|
HandlePiece(R, *I, n, max);
|
2008-03-27 14:17:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add line numbers, header, footer, etc.
|
2008-03-27 15:35:49 +08:00
|
|
|
|
2008-03-27 14:17:42 +08:00
|
|
|
unsigned FileID = R.getSourceMgr().getMainFileID();
|
|
|
|
html::EscapeText(R, FileID);
|
|
|
|
html::AddLineNumbers(R, FileID);
|
|
|
|
|
2008-04-17 00:39:56 +08:00
|
|
|
// If we have a preprocessor, relex the file and syntax highlight.
|
|
|
|
// We might not have a preprocessor if we come from a deserialized AST file,
|
|
|
|
// for example.
|
|
|
|
|
2008-04-18 06:31:54 +08:00
|
|
|
if (PP) html::SyntaxHighlight(R, FileID, *PP);
|
2008-04-23 00:15:03 +08:00
|
|
|
|
|
|
|
// FIXME: We eventually want to use PPF to create a fresh Preprocessor,
|
|
|
|
// once we have worked out the bugs.
|
|
|
|
//
|
|
|
|
// if (PPF) html::HighlightMacros(R, FileID, *PPF);
|
|
|
|
//
|
|
|
|
if (PP) html::HighlightMacros(R, FileID, *PP);
|
2008-04-17 00:39:56 +08:00
|
|
|
|
2008-04-03 04:44:16 +08:00
|
|
|
// Get the full directory name of the analyzed file.
|
|
|
|
|
|
|
|
const FileEntry* Entry = SMgr.getFileEntryForID(FileID);
|
2008-03-27 15:35:49 +08:00
|
|
|
|
2008-04-25 07:37:03 +08:00
|
|
|
// This is a cludge; basically we want to append either the full
|
|
|
|
// working directory if we have no directory information. This is
|
|
|
|
// a work in progress.
|
|
|
|
|
2008-05-03 06:04:53 +08:00
|
|
|
std::string DirName = "";
|
|
|
|
|
|
|
|
if (!llvm::sys::Path(Entry->getName()).isAbsolute()) {
|
|
|
|
llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
|
|
|
|
DirName = P.toString() + "/";
|
|
|
|
}
|
2008-03-28 01:25:28 +08:00
|
|
|
|
2008-04-16 05:25:08 +08:00
|
|
|
// Add the name of the file as an <h1> tag.
|
|
|
|
|
2008-04-03 04:44:16 +08:00
|
|
|
{
|
|
|
|
std::ostringstream os;
|
2008-03-27 15:35:49 +08:00
|
|
|
|
2008-04-16 05:25:08 +08:00
|
|
|
os << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
|
|
|
|
"<tr><td class=\"rowname\">File:</td><td>"
|
|
|
|
<< html::EscapeText(DirName)
|
|
|
|
<< html::EscapeText(Entry->getName())
|
|
|
|
<< "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>"
|
|
|
|
"<a href=\"#EndPath\">line "
|
|
|
|
<< (*D.rbegin()).getLocation().getLogicalLineNumber()
|
|
|
|
<< ", column "
|
|
|
|
<< (*D.rbegin()).getLocation().getLogicalColumnNumber()
|
|
|
|
<< "</a></td></tr>\n"
|
|
|
|
"<tr><td class=\"rowname\">Description:</td><td>"
|
2008-05-01 07:47:44 +08:00
|
|
|
<< D.getDescription() << "</td></tr>\n";
|
|
|
|
|
|
|
|
// Output any other meta data.
|
|
|
|
|
|
|
|
for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end();
|
|
|
|
I!=E; ++I) {
|
|
|
|
os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
os << "</table>\n<h3>Annotated Source Code</h3>\n";
|
2008-04-16 05:25:08 +08:00
|
|
|
|
2008-04-02 15:04:46 +08:00
|
|
|
R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
|
|
|
|
}
|
|
|
|
|
2008-04-03 04:44:16 +08:00
|
|
|
// Embed meta-data tags.
|
2008-04-02 15:04:46 +08:00
|
|
|
|
|
|
|
const std::string& BugDesc = D.getDescription();
|
|
|
|
|
|
|
|
if (!BugDesc.empty()) {
|
|
|
|
std::ostringstream os;
|
2008-04-03 02:03:20 +08:00
|
|
|
os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
|
2008-03-27 15:35:49 +08:00
|
|
|
R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
|
2008-04-03 04:44:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
2008-04-25 07:37:03 +08:00
|
|
|
os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
|
2008-04-03 04:44:16 +08:00
|
|
|
R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
2008-04-04 01:55:57 +08:00
|
|
|
os << "\n<!-- BUGLINE " << D.back()->getLocation().getLogicalLineNumber()
|
2008-04-03 04:44:16 +08:00
|
|
|
<< " -->\n";
|
|
|
|
R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
|
|
|
|
R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
|
|
|
|
}
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
// Add CSS, header, and footer.
|
|
|
|
|
|
|
|
html::AddHeaderFooterInternalBuiltinCSS(R, FileID);
|
|
|
|
|
|
|
|
// Get the rewrite buffer.
|
|
|
|
const RewriteBuffer *Buf = R.getRewriteBufferFor(FileID);
|
|
|
|
|
|
|
|
if (!Buf) {
|
|
|
|
llvm::cerr << "warning: no diagnostics generated for main file.\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the stream to write out the HTML.
|
|
|
|
std::ofstream os;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Create a path for the target HTML file.
|
|
|
|
llvm::sys::Path F(FilePrefix);
|
|
|
|
F.makeUnique(false, NULL);
|
|
|
|
|
|
|
|
// Rename the file with an HTML extension.
|
|
|
|
llvm::sys::Path H(F);
|
|
|
|
H.appendSuffix("html");
|
|
|
|
F.renamePathOnDisk(H, NULL);
|
|
|
|
|
|
|
|
os.open(H.toString().c_str());
|
|
|
|
|
|
|
|
if (!os) {
|
|
|
|
llvm::cerr << "warning: could not create file '" << F.toString() << "'\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the HTML to disk.
|
|
|
|
|
2008-04-20 09:02:33 +08:00
|
|
|
for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
|
2008-04-09 06:37:58 +08:00
|
|
|
os << *I;
|
2008-03-27 14:17:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLDiagnostics::HandlePiece(Rewriter& R,
|
|
|
|
const PathDiagnosticPiece& P,
|
2008-04-01 07:30:12 +08:00
|
|
|
unsigned num, unsigned max) {
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
// For now, just draw a box above the line in question, and emit the
|
|
|
|
// warning.
|
|
|
|
|
|
|
|
FullSourceLoc Pos = P.getLocation();
|
|
|
|
|
|
|
|
if (!Pos.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SourceManager& SM = R.getSourceMgr();
|
|
|
|
FullSourceLoc LPos = Pos.getLogicalLoc();
|
2008-04-15 05:06:04 +08:00
|
|
|
unsigned FileID = SM.getCanonicalFileID(LPos.getLocation());
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
assert (&LPos.getManager() == &SM && "SourceManagers are different!");
|
|
|
|
|
2008-04-15 05:06:04 +08:00
|
|
|
if (!SM.isFromMainFile(LPos.getLocation()))
|
2008-03-27 14:17:42 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Compute the column number. Rewind from the current position to the start
|
|
|
|
// of the line.
|
|
|
|
|
|
|
|
unsigned ColNo = LPos.getColumnNumber();
|
|
|
|
const char *TokLogicalPtr = LPos.getCharacterData();
|
|
|
|
const char *LineStart = TokLogicalPtr-ColNo;
|
|
|
|
|
2008-04-01 05:40:14 +08:00
|
|
|
// Compute the margin offset by counting tabs and non-tabs.
|
|
|
|
|
|
|
|
unsigned PosNo = 0;
|
|
|
|
|
|
|
|
for (const char* c = LineStart; c != TokLogicalPtr; ++c)
|
2008-04-01 07:14:05 +08:00
|
|
|
PosNo += *c == '\t' ? 4 : 1;
|
2008-04-01 05:40:14 +08:00
|
|
|
|
2008-03-27 14:17:42 +08:00
|
|
|
// Create the html for the message.
|
|
|
|
|
|
|
|
std::ostringstream os;
|
|
|
|
|
|
|
|
os << "\n<tr><td class=\"num\"></td><td class=\"line\">"
|
2008-04-01 07:30:12 +08:00
|
|
|
<< "<div id=\"";
|
|
|
|
|
|
|
|
if (num == max)
|
|
|
|
os << "EndPath";
|
|
|
|
else
|
|
|
|
os << "Path" << num;
|
|
|
|
|
|
|
|
os << "\" class=\"msg\" style=\"margin-left:"
|
2008-04-01 05:40:14 +08:00
|
|
|
<< PosNo << "ex\">";
|
2008-03-27 14:17:42 +08:00
|
|
|
|
2008-04-03 05:04:20 +08:00
|
|
|
if (max > 1)
|
|
|
|
os << "<span class=\"PathIndex\">[" << num << "]</span> ";
|
|
|
|
|
2008-03-28 01:15:29 +08:00
|
|
|
os << html::EscapeText(P.getString()) << "</div></td></tr>";
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
// Insert the new html.
|
|
|
|
|
|
|
|
const llvm::MemoryBuffer *Buf = SM.getBuffer(FileID);
|
|
|
|
const char* FileStart = Buf->getBufferStart();
|
|
|
|
|
|
|
|
R.InsertStrBefore(SourceLocation::getFileLoc(FileID, LineStart - FileStart),
|
|
|
|
os.str());
|
|
|
|
|
|
|
|
// Now highlight the ranges.
|
|
|
|
|
|
|
|
for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
|
|
|
|
I != E; ++I)
|
2008-04-15 05:06:04 +08:00
|
|
|
HighlightRange(R, *I);
|
2008-03-27 14:17:42 +08:00
|
|
|
}
|
|
|
|
|
2008-04-15 05:06:04 +08:00
|
|
|
void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range) {
|
2008-03-27 14:17:42 +08:00
|
|
|
|
2008-04-15 05:06:04 +08:00
|
|
|
SourceManager& SM = R.getSourceMgr();
|
2008-03-27 14:17:42 +08:00
|
|
|
|
2008-04-15 05:06:04 +08:00
|
|
|
SourceLocation LogicalStart = SM.getLogicalLoc(Range.getBegin());
|
|
|
|
unsigned StartLineNo = SM.getLineNumber(LogicalStart);
|
2008-03-27 14:17:42 +08:00
|
|
|
|
2008-04-15 05:06:04 +08:00
|
|
|
SourceLocation LogicalEnd = SM.getLogicalLoc(Range.getEnd());
|
|
|
|
unsigned EndLineNo = SM.getLineNumber(LogicalEnd);
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
if (EndLineNo < StartLineNo)
|
|
|
|
return;
|
|
|
|
|
2008-04-15 05:06:04 +08:00
|
|
|
if (!SM.isFromMainFile(LogicalStart) ||
|
|
|
|
!SM.isFromMainFile(LogicalEnd))
|
2008-03-27 14:17:42 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Compute the column number of the end.
|
2008-04-15 05:06:04 +08:00
|
|
|
unsigned EndColNo = SM.getColumnNumber(LogicalEnd);
|
2008-03-27 14:17:42 +08:00
|
|
|
unsigned OldEndColNo = EndColNo;
|
|
|
|
|
|
|
|
if (EndColNo) {
|
|
|
|
// Add in the length of the token, so that we cover multi-char tokens.
|
2008-04-18 13:01:33 +08:00
|
|
|
EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1;
|
2008-03-27 14:17:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Highlight the range. Make the span tag the outermost tag for the
|
|
|
|
// selected range.
|
2008-04-09 05:29:14 +08:00
|
|
|
|
2008-04-18 13:01:33 +08:00
|
|
|
SourceLocation E = LogicalEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
|
2008-04-18 02:37:23 +08:00
|
|
|
|
|
|
|
html::HighlightRange(R, LogicalStart, E,
|
|
|
|
"<span class=\"mrange\">", "</span>");
|
2008-03-27 14:17:42 +08:00
|
|
|
}
|