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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-03-02 14:16:29 +08:00
|
|
|
#include "clang/Frontend/PathDiagnosticClients.h"
|
2008-08-11 12:54:23 +08:00
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
2008-03-27 14:17:42 +08:00
|
|
|
#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/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"
|
2008-09-13 13:16:45 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-03-27 14:17:42 +08:00
|
|
|
#include "llvm/System/Path.h"
|
|
|
|
#include <fstream>
|
|
|
|
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
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
void HandlePiece(Rewriter& R, FileID BugFileID,
|
2008-07-24 07:18:15 +08:00
|
|
|
const PathDiagnosticPiece& P, unsigned num, unsigned max);
|
2008-04-01 07:30:12 +08:00
|
|
|
|
Introduce code modification hints into the diagnostics system. When we
know how to recover from an error, we can attach a hint to the
diagnostic that states how to modify the code, which can be one of:
- Insert some new code (a text string) at a particular source
location
- Remove the code within a given range
- Replace the code within a given range with some new code (a text
string)
Right now, we use these hints to annotate diagnostic information. For
example, if one uses the '>>' in a template argument in C++98, as in
this code:
template<int I> class B { };
B<1000 >> 2> *b1;
we'll warn that the behavior will change in C++0x. The fix is to
insert parenthese, so we use code insertion annotations to illustrate
where the parentheses go:
test.cpp:10:10: warning: use of right-shift operator ('>>') in template
argument will require parentheses in C++0x
B<1000 >> 2> *b1;
^
( )
Use of these annotations is partially implemented for HTML
diagnostics, but it's not (yet) producing valid HTML, which may be
related to PR2386, so it has been #if 0'd out.
In this future, we could consider hooking this mechanism up to the
rewriter to actually try to fix these problems during compilation (or,
after a compilation whose only errors have fixes). For now, however, I
suggest that we use these code modification hints whenever we can, so
that we get better diagnostics now and will have better coverage when
we find better ways to use this information.
This also fixes PR3410 by placing the complaint about missing tokens
just after the previous token (rather than at the location of the next
token).
llvm-svn: 65570
2009-02-27 05:00:50 +08:00
|
|
|
void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range,
|
|
|
|
const char *HighlightStart = "<span class=\"mrange\">",
|
|
|
|
const char *HighlightEnd = "</span>");
|
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;
|
|
|
|
|
2009-01-17 06:59:51 +08:00
|
|
|
SourceManager &SMgr = D.begin()->getLocation().getManager();
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID FID;
|
2008-07-24 07:18:15 +08:00
|
|
|
|
|
|
|
// Verify that the entire path is from the same FileID.
|
2009-01-16 15:36:28 +08:00
|
|
|
for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) {
|
|
|
|
FullSourceLoc L = I->getLocation().getInstantiationLoc();
|
2008-07-24 07:18:15 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
if (FID.isInvalid()) {
|
2009-01-19 15:46:45 +08:00
|
|
|
FID = SMgr.getFileID(L);
|
|
|
|
} else if (SMgr.getFileID(L) != FID)
|
2008-07-24 07:18:15 +08:00
|
|
|
return; // FIXME: Emit a warning?
|
|
|
|
|
|
|
|
// Check the source ranges.
|
|
|
|
for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(),
|
|
|
|
RE=I->ranges_end(); RI!=RE; ++RI) {
|
|
|
|
|
2009-01-16 15:36:28 +08:00
|
|
|
SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin());
|
2008-07-24 07:18:15 +08:00
|
|
|
|
2009-01-19 15:46:45 +08:00
|
|
|
if (!L.isFileID() || SMgr.getFileID(L) != FID)
|
2008-07-24 07:18:15 +08:00
|
|
|
return; // FIXME: Emit a warning?
|
|
|
|
|
2009-01-16 15:36:28 +08:00
|
|
|
L = SMgr.getInstantiationLoc(RI->getEnd());
|
2008-07-24 07:18:15 +08:00
|
|
|
|
2009-01-19 15:46:45 +08:00
|
|
|
if (!L.isFileID() || SMgr.getFileID(L) != FID)
|
2008-07-24 07:18:15 +08:00
|
|
|
return; // FIXME: Emit a warning?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
if (FID.isInvalid())
|
2008-07-24 07:18:15 +08:00
|
|
|
return; // FIXME: Emit a warning?
|
|
|
|
|
|
|
|
// Create a new rewriter to generate HTML.
|
2008-03-27 14:17:42 +08:00
|
|
|
Rewriter R(SMgr);
|
|
|
|
|
2009-03-10 10:49:29 +08:00
|
|
|
// Process the path.
|
2008-03-27 14:17:42 +08:00
|
|
|
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) {
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
HandlePiece(R, FID, *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
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
// unsigned FID = R.getSourceMgr().getMainFileID();
|
|
|
|
html::EscapeText(R, FID);
|
|
|
|
html::AddLineNumbers(R, FID);
|
2008-03-27 14:17:42 +08:00
|
|
|
|
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.
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
if (PP) html::SyntaxHighlight(R, FID, *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.
|
|
|
|
//
|
2009-01-17 14:22:33 +08:00
|
|
|
// if (PPF) html::HighlightMacros(R, FID, *PPF);
|
2008-04-23 00:15:03 +08:00
|
|
|
//
|
2009-01-17 14:22:33 +08:00
|
|
|
if (PP) html::HighlightMacros(R, FID, *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.
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
const FileEntry* Entry = SMgr.getFileEntryForID(FID);
|
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
|
|
|
{
|
2008-09-13 13:16:45 +08:00
|
|
|
std::string s;
|
|
|
|
llvm::raw_string_ostream os(s);
|
2008-03-27 15:35:49 +08:00
|
|
|
|
2008-09-23 01:33:32 +08:00
|
|
|
os << "<!-- REPORTHEADER -->\n"
|
|
|
|
<< "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
|
2008-04-16 05:25:08 +08:00
|
|
|
"<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 "
|
2009-01-16 15:36:28 +08:00
|
|
|
<< (*D.rbegin()).getLocation().getInstantiationLineNumber()
|
2008-04-16 05:25:08 +08:00
|
|
|
<< ", column "
|
2009-01-16 15:36:28 +08:00
|
|
|
<< (*D.rbegin()).getLocation().getInstantiationColumnNumber()
|
2008-04-16 05:25:08 +08:00
|
|
|
<< "</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";
|
|
|
|
}
|
|
|
|
|
2008-09-23 01:33:32 +08:00
|
|
|
os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n"
|
|
|
|
"<h3>Annotated Source Code</h3>\n";
|
2008-04-16 05:25:08 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
|
2008-04-02 15:04:46 +08:00
|
|
|
}
|
|
|
|
|
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()) {
|
2008-09-13 13:16:45 +08:00
|
|
|
std::string s;
|
|
|
|
llvm::raw_string_ostream os(s);
|
2008-04-03 02:03:20 +08:00
|
|
|
os << "\n<!-- BUGDESC " << BugDesc << " -->\n";
|
2009-01-17 14:22:33 +08:00
|
|
|
R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
|
2008-04-03 04:44:16 +08:00
|
|
|
}
|
|
|
|
|
2009-01-27 09:53:39 +08:00
|
|
|
const std::string& BugType = D.getBugType();
|
|
|
|
if (!BugType.empty()) {
|
|
|
|
std::string s;
|
|
|
|
llvm::raw_string_ostream os(s);
|
|
|
|
os << "\n<!-- BUGTYPE " << BugType << " -->\n";
|
|
|
|
R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
|
|
|
|
}
|
|
|
|
|
2008-09-20 12:23:38 +08:00
|
|
|
const std::string& BugCategory = D.getCategory();
|
|
|
|
|
|
|
|
if (!BugCategory.empty()) {
|
|
|
|
std::string s;
|
|
|
|
llvm::raw_string_ostream os(s);
|
|
|
|
os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
|
2009-01-17 14:22:33 +08:00
|
|
|
R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
|
2008-09-20 12:23:38 +08:00
|
|
|
}
|
|
|
|
|
2008-04-03 04:44:16 +08:00
|
|
|
{
|
2008-09-13 13:16:45 +08:00
|
|
|
std::string s;
|
|
|
|
llvm::raw_string_ostream os(s);
|
2008-04-25 07:37:03 +08:00
|
|
|
os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
|
2009-01-17 14:22:33 +08:00
|
|
|
R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
|
2008-04-03 04:44:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2008-09-13 13:16:45 +08:00
|
|
|
std::string s;
|
|
|
|
llvm::raw_string_ostream os(s);
|
2009-01-16 15:36:28 +08:00
|
|
|
os << "\n<!-- BUGLINE "
|
|
|
|
<< D.back()->getLocation().getInstantiationLineNumber() << " -->\n";
|
2009-01-17 14:22:33 +08:00
|
|
|
R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
|
2008-04-03 04:44:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2008-09-13 13:16:45 +08:00
|
|
|
std::string s;
|
|
|
|
llvm::raw_string_ostream os(s);
|
2008-04-03 04:44:16 +08:00
|
|
|
os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n";
|
2009-01-17 14:22:33 +08:00
|
|
|
R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str());
|
2008-04-03 04:44:16 +08:00
|
|
|
}
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
// Add CSS, header, and footer.
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
// Get the rewrite buffer.
|
2009-01-17 14:22:33 +08:00
|
|
|
const RewriteBuffer *Buf = R.getRewriteBufferFor(FID);
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID,
|
2008-03-27 14:17:42 +08:00
|
|
|
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;
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
SourceManager &SM = R.getSourceMgr();
|
2009-02-04 08:55:58 +08:00
|
|
|
assert(&Pos.getManager() == &SM && "SourceManagers are different!");
|
|
|
|
std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos);
|
2008-03-27 14:17:42 +08:00
|
|
|
|
2009-02-04 08:55:58 +08:00
|
|
|
if (LPosInfo.first != BugFileID)
|
2008-03-27 14:17:42 +08:00
|
|
|
return;
|
|
|
|
|
2009-02-04 08:55:58 +08:00
|
|
|
const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first);
|
2008-05-07 07:42:18 +08:00
|
|
|
const char* FileStart = Buf->getBufferStart();
|
|
|
|
|
2008-03-27 14:17:42 +08:00
|
|
|
// Compute the column number. Rewind from the current position to the start
|
|
|
|
// of the line.
|
2009-02-04 08:55:58 +08:00
|
|
|
unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second);
|
|
|
|
const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData();
|
2009-01-16 15:36:28 +08:00
|
|
|
const char *LineStart = TokInstantiationPtr-ColNo;
|
2008-05-07 07:42:18 +08:00
|
|
|
|
2009-02-19 06:10:00 +08:00
|
|
|
// Compute LineEnd.
|
2009-01-16 15:36:28 +08:00
|
|
|
const char *LineEnd = TokInstantiationPtr;
|
2009-02-19 06:10:00 +08:00
|
|
|
const char* FileEnd = Buf->getBufferEnd();
|
|
|
|
while (*LineEnd != '\n' && LineEnd != FileEnd)
|
|
|
|
++LineEnd;
|
2008-03-27 14:17:42 +08:00
|
|
|
|
2008-04-01 05:40:14 +08:00
|
|
|
// Compute the margin offset by counting tabs and non-tabs.
|
2009-02-19 06:10:00 +08:00
|
|
|
unsigned PosNo = 0;
|
2009-01-16 15:36:28 +08:00
|
|
|
for (const char* c = LineStart; c != TokInstantiationPtr; ++c)
|
2008-05-07 07:42:18 +08:00
|
|
|
PosNo += *c == '\t' ? 8 : 1;
|
2008-04-01 05:40:14 +08:00
|
|
|
|
2008-03-27 14:17:42 +08:00
|
|
|
// Create the html for the message.
|
2008-09-22 02:52:59 +08:00
|
|
|
{
|
|
|
|
// Get the string and determining its maximum substring.
|
|
|
|
const std::string& Msg = P.getString();
|
|
|
|
unsigned max_token = 0;
|
|
|
|
unsigned cnt = 0;
|
|
|
|
unsigned len = Msg.size();
|
|
|
|
|
|
|
|
for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I)
|
|
|
|
switch (*I) {
|
|
|
|
default:
|
|
|
|
++cnt;
|
2009-03-10 10:49:29 +08:00
|
|
|
continue;
|
2008-09-22 02:52:59 +08:00
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
if (cnt > max_token) max_token = cnt;
|
|
|
|
cnt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cnt > max_token) max_token = cnt;
|
|
|
|
|
|
|
|
// Next, determine the approximate size of the message bubble in em.
|
|
|
|
unsigned em;
|
2009-03-03 07:06:15 +08:00
|
|
|
const unsigned max_line = 120;
|
2008-09-22 02:52:59 +08:00
|
|
|
|
|
|
|
if (max_token >= max_line)
|
|
|
|
em = max_token / 2;
|
|
|
|
else {
|
|
|
|
unsigned characters = max_line;
|
|
|
|
unsigned lines = len / max_line;
|
|
|
|
|
|
|
|
if (lines > 0) {
|
|
|
|
for (; characters > max_token; --characters)
|
|
|
|
if (len / characters > lines) {
|
|
|
|
++characters;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
em = characters / 2;
|
|
|
|
}
|
|
|
|
|
2009-03-03 07:05:40 +08:00
|
|
|
// Now generate the message bubble.
|
|
|
|
const char *Kind = 0;
|
|
|
|
switch (P.getKind()) {
|
|
|
|
case PathDiagnosticPiece::Event: Kind = "Event"; break;
|
|
|
|
case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break;
|
2009-03-10 10:49:29 +08:00
|
|
|
case PathDiagnosticPiece::Macro: Kind = "Macro"; break;
|
2009-03-03 07:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string sbuf;
|
|
|
|
llvm::raw_string_ostream os(sbuf);
|
2008-09-22 02:52:59 +08:00
|
|
|
|
|
|
|
os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\"";
|
|
|
|
|
|
|
|
if (num == max)
|
|
|
|
os << "EndPath";
|
|
|
|
else
|
|
|
|
os << "Path" << num;
|
2008-05-07 07:42:18 +08:00
|
|
|
|
2009-03-03 05:42:01 +08:00
|
|
|
os << "\" class=\"msg";
|
2009-03-03 07:05:40 +08:00
|
|
|
if (Kind) os << " msg" << Kind;
|
2009-03-03 05:42:01 +08:00
|
|
|
os << "\" style=\"margin-left:" << PosNo << "ex";
|
2008-09-22 02:52:59 +08:00
|
|
|
if (em < max_line/2) os << "; max-width:" << em << "em";
|
|
|
|
os << "\">";
|
|
|
|
|
2009-03-03 07:05:40 +08:00
|
|
|
if (max > 1) {
|
|
|
|
os << "<table class=\"msgT\"><tr><td valign=\"top\">";
|
|
|
|
os << "<div class=\"PathIndex";
|
|
|
|
if (Kind) os << " PathIndex" << Kind;
|
|
|
|
os << "\">" << num << "</div>";
|
|
|
|
os << "</td><td>";
|
|
|
|
}
|
|
|
|
|
|
|
|
os << html::EscapeText(Msg);
|
|
|
|
|
|
|
|
if (max > 1) {
|
|
|
|
os << "</td></tr></table>";
|
|
|
|
}
|
2008-09-22 02:52:59 +08:00
|
|
|
|
2009-03-03 07:05:40 +08:00
|
|
|
os << "</div></td></tr>";
|
2008-09-22 02:52:59 +08:00
|
|
|
|
|
|
|
// Insert the new html.
|
2009-02-19 06:10:00 +08:00
|
|
|
unsigned DisplayPos = LineEnd - FileStart;
|
2009-01-17 14:22:33 +08:00
|
|
|
SourceLocation Loc =
|
2009-02-04 08:55:58 +08:00
|
|
|
SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos);
|
2009-02-19 06:10:00 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
R.InsertStrBefore(Loc, os.str());
|
2008-09-22 02:52:59 +08:00
|
|
|
}
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
// Now highlight the ranges.
|
|
|
|
|
|
|
|
for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end();
|
|
|
|
I != E; ++I)
|
2009-02-04 08:55:58 +08:00
|
|
|
HighlightRange(R, LPosInfo.first, *I);
|
Introduce code modification hints into the diagnostics system. When we
know how to recover from an error, we can attach a hint to the
diagnostic that states how to modify the code, which can be one of:
- Insert some new code (a text string) at a particular source
location
- Remove the code within a given range
- Replace the code within a given range with some new code (a text
string)
Right now, we use these hints to annotate diagnostic information. For
example, if one uses the '>>' in a template argument in C++98, as in
this code:
template<int I> class B { };
B<1000 >> 2> *b1;
we'll warn that the behavior will change in C++0x. The fix is to
insert parenthese, so we use code insertion annotations to illustrate
where the parentheses go:
test.cpp:10:10: warning: use of right-shift operator ('>>') in template
argument will require parentheses in C++0x
B<1000 >> 2> *b1;
^
( )
Use of these annotations is partially implemented for HTML
diagnostics, but it's not (yet) producing valid HTML, which may be
related to PR2386, so it has been #if 0'd out.
In this future, we could consider hooking this mechanism up to the
rewriter to actually try to fix these problems during compilation (or,
after a compilation whose only errors have fixes). For now, however, I
suggest that we use these code modification hints whenever we can, so
that we get better diagnostics now and will have better coverage when
we find better ways to use this information.
This also fixes PR3410 by placing the complaint about missing tokens
just after the previous token (rather than at the location of the next
token).
llvm-svn: 65570
2009-02-27 05:00:50 +08:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
// If there is a code insertion hint, insert that code.
|
|
|
|
// FIXME: This code is disabled because it seems to mangle the HTML
|
|
|
|
// output. I'm leaving it here because it's generally the right idea,
|
|
|
|
// but needs some help from someone more familiar with the rewriter.
|
|
|
|
for (const CodeModificationHint *Hint = P.code_modifications_begin(),
|
|
|
|
*HintEnd = P.code_modifications_end();
|
|
|
|
Hint != HintEnd; ++Hint) {
|
|
|
|
if (Hint->RemoveRange.isValid()) {
|
|
|
|
HighlightRange(R, LPosInfo.first, Hint->RemoveRange,
|
|
|
|
"<span class=\"CodeRemovalHint\">", "</span>");
|
|
|
|
}
|
|
|
|
if (Hint->InsertionLoc.isValid()) {
|
|
|
|
std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true);
|
|
|
|
EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode
|
|
|
|
+ "</span>";
|
|
|
|
R.InsertStrBefore(Hint->InsertionLoc, EscapedCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2008-03-27 14:17:42 +08:00
|
|
|
}
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID,
|
Introduce code modification hints into the diagnostics system. When we
know how to recover from an error, we can attach a hint to the
diagnostic that states how to modify the code, which can be one of:
- Insert some new code (a text string) at a particular source
location
- Remove the code within a given range
- Replace the code within a given range with some new code (a text
string)
Right now, we use these hints to annotate diagnostic information. For
example, if one uses the '>>' in a template argument in C++98, as in
this code:
template<int I> class B { };
B<1000 >> 2> *b1;
we'll warn that the behavior will change in C++0x. The fix is to
insert parenthese, so we use code insertion annotations to illustrate
where the parentheses go:
test.cpp:10:10: warning: use of right-shift operator ('>>') in template
argument will require parentheses in C++0x
B<1000 >> 2> *b1;
^
( )
Use of these annotations is partially implemented for HTML
diagnostics, but it's not (yet) producing valid HTML, which may be
related to PR2386, so it has been #if 0'd out.
In this future, we could consider hooking this mechanism up to the
rewriter to actually try to fix these problems during compilation (or,
after a compilation whose only errors have fixes). For now, however, I
suggest that we use these code modification hints whenever we can, so
that we get better diagnostics now and will have better coverage when
we find better ways to use this information.
This also fixes PR3410 by placing the complaint about missing tokens
just after the previous token (rather than at the location of the next
token).
llvm-svn: 65570
2009-02-27 05:00:50 +08:00
|
|
|
SourceRange Range,
|
|
|
|
const char *HighlightStart,
|
|
|
|
const char *HighlightEnd) {
|
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
|
|
|
|
2009-01-16 15:36:28 +08:00
|
|
|
SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin());
|
2009-02-04 09:06:56 +08:00
|
|
|
unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart);
|
2008-03-27 14:17:42 +08:00
|
|
|
|
2009-01-16 15:36:28 +08:00
|
|
|
SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd());
|
2009-02-04 09:06:56 +08:00
|
|
|
unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd);
|
2008-03-27 14:17:42 +08:00
|
|
|
|
|
|
|
if (EndLineNo < StartLineNo)
|
|
|
|
return;
|
|
|
|
|
2009-01-19 15:46:45 +08:00
|
|
|
if (SM.getFileID(InstantiationStart) != BugFileID ||
|
|
|
|
SM.getFileID(InstantiationEnd) != BugFileID)
|
2008-03-27 14:17:42 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Compute the column number of the end.
|
2009-02-04 08:55:58 +08:00
|
|
|
unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd);
|
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
|
|
|
|
2009-01-16 15:36:28 +08:00
|
|
|
SourceLocation E =
|
|
|
|
InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo);
|
2008-04-18 02:37:23 +08:00
|
|
|
|
Introduce code modification hints into the diagnostics system. When we
know how to recover from an error, we can attach a hint to the
diagnostic that states how to modify the code, which can be one of:
- Insert some new code (a text string) at a particular source
location
- Remove the code within a given range
- Replace the code within a given range with some new code (a text
string)
Right now, we use these hints to annotate diagnostic information. For
example, if one uses the '>>' in a template argument in C++98, as in
this code:
template<int I> class B { };
B<1000 >> 2> *b1;
we'll warn that the behavior will change in C++0x. The fix is to
insert parenthese, so we use code insertion annotations to illustrate
where the parentheses go:
test.cpp:10:10: warning: use of right-shift operator ('>>') in template
argument will require parentheses in C++0x
B<1000 >> 2> *b1;
^
( )
Use of these annotations is partially implemented for HTML
diagnostics, but it's not (yet) producing valid HTML, which may be
related to PR2386, so it has been #if 0'd out.
In this future, we could consider hooking this mechanism up to the
rewriter to actually try to fix these problems during compilation (or,
after a compilation whose only errors have fixes). For now, however, I
suggest that we use these code modification hints whenever we can, so
that we get better diagnostics now and will have better coverage when
we find better ways to use this information.
This also fixes PR3410 by placing the complaint about missing tokens
just after the previous token (rather than at the location of the next
token).
llvm-svn: 65570
2009-02-27 05:00:50 +08:00
|
|
|
html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd);
|
2008-03-27 14:17:42 +08:00
|
|
|
}
|