\n";
R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
}
// Embed meta-data tags.
const std::string& BugDesc = D.getDescription();
if (!BugDesc.empty()) {
std::ostringstream os;
os << "\n\n";
R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
}
{
std::ostringstream os;
os << "\n\n";
R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
}
{
std::ostringstream os;
os << "\n\n";
R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
}
{
std::ostringstream os;
os << "\n\n";
R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
}
// 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.
for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I)
os << *I;
}
void HTMLDiagnostics::HandlePiece(Rewriter& R,
const PathDiagnosticPiece& P,
unsigned num, unsigned max) {
// 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();
unsigned FileID = LPos.getLocation().getFileID();
assert (&LPos.getManager() == &SM && "SourceManagers are different!");
unsigned MainFileID = SM.getMainFileID();
if (FileID != MainFileID)
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;
// Compute the margin offset by counting tabs and non-tabs.
unsigned PosNo = 0;
for (const char* c = LineStart; c != TokLogicalPtr; ++c)
PosNo += *c == '\t' ? 4 : 1;
// Create the html for the message.
std::ostringstream os;
os << "\n
"
<< "
";
os << html::EscapeText(P.getString()) << "
";
// 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)
HighlightRange(R, *I, MainFileID);
}
void HTMLDiagnostics::HighlightRange(Rewriter& R, SourceRange Range,
unsigned MainFileID) {
SourceManager& SourceMgr = R.getSourceMgr();
SourceLocation LogicalStart = SourceMgr.getLogicalLoc(Range.getBegin());
unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(Range.getEnd());
unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
if (EndLineNo < StartLineNo)
return;
if (LogicalStart.getFileID() != MainFileID ||
LogicalEnd.getFileID() != MainFileID)
return;
// Compute the column number of the end.
unsigned EndColNo = SourceMgr.getColumnNumber(LogicalEnd);
unsigned OldEndColNo = EndColNo;
if (EndColNo) {
// Add in the length of the token, so that we cover multi-char tokens.
EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SourceMgr);
}
// Highlight the range. Make the span tag the outermost tag for the
// selected range.
SourceLocation E =
LogicalEnd.getFileLocWithOffset(OldEndColNo > EndColNo
? -(OldEndColNo - EndColNo)
: EndColNo - OldEndColNo);
R.InsertCStrBefore(LogicalStart, "");
R.InsertCStrAfter(E, "");
}