2007-07-11 21:01:13 +04:00
|
|
|
//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This diagnostic client prints out their diagnostic messages.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "TextDiagnosticPrinter.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
NoShowColumn("fno-show-column",
|
|
|
|
llvm::cl::desc("Do not include column number on diagnostics"));
|
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
NoCaretDiagnostics("fno-caret-diagnostics",
|
|
|
|
llvm::cl::desc("Do not include source line and caret with"
|
|
|
|
" diagnostics"));
|
|
|
|
|
|
|
|
void TextDiagnosticPrinter::
|
2007-12-13 01:39:36 +03:00
|
|
|
PrintIncludeStack(FullSourceLoc Pos) {
|
2007-07-20 20:37:10 +04:00
|
|
|
if (Pos.isInvalid()) return;
|
|
|
|
|
2007-12-13 01:39:36 +03:00
|
|
|
Pos = Pos.getLogicalLoc();
|
2007-07-20 20:37:10 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Print out the other include frames first.
|
2007-12-13 01:39:36 +03:00
|
|
|
PrintIncludeStack(Pos.getIncludeLoc());
|
|
|
|
unsigned LineNo = Pos.getLineNumber();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-12-13 01:39:36 +03:00
|
|
|
std::cerr << "In file included from " << Pos.getSourceName()
|
2007-07-11 21:01:13 +04:00
|
|
|
<< ":" << LineNo << ":\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
|
|
|
|
/// any characters in LineNo that intersect the SourceRange.
|
2007-12-12 00:27:55 +03:00
|
|
|
void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
|
2007-12-13 01:39:36 +03:00
|
|
|
SourceManager& SourceMgr,
|
2007-07-11 21:01:13 +04:00
|
|
|
unsigned LineNo,
|
|
|
|
std::string &CaratLine,
|
2007-12-12 01:57:35 +03:00
|
|
|
const std::string &SourceLine) {
|
2007-07-11 21:01:13 +04:00
|
|
|
assert(CaratLine.size() == SourceLine.size() &&
|
|
|
|
"Expect a correspondence between source and carat line!");
|
|
|
|
if (!R.isValid()) return;
|
|
|
|
|
2007-12-13 01:39:36 +03:00
|
|
|
unsigned StartLineNo = SourceMgr.getLogicalLineNumber(R.getBegin());
|
2007-07-11 21:01:13 +04:00
|
|
|
if (StartLineNo > LineNo) return; // No intersection.
|
|
|
|
|
2007-12-13 01:39:36 +03:00
|
|
|
unsigned EndLineNo = SourceMgr.getLogicalLineNumber(R.getEnd());
|
2007-07-11 21:01:13 +04:00
|
|
|
if (EndLineNo < LineNo) return; // No intersection.
|
|
|
|
|
|
|
|
// Compute the column number of the start.
|
|
|
|
unsigned StartColNo = 0;
|
|
|
|
if (StartLineNo == LineNo) {
|
2007-12-13 01:39:36 +03:00
|
|
|
StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin());
|
2007-07-11 21:01:13 +04:00
|
|
|
if (StartColNo) --StartColNo; // Zero base the col #.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick the first non-whitespace column.
|
|
|
|
while (StartColNo < SourceLine.size() &&
|
|
|
|
(SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
|
|
|
|
++StartColNo;
|
|
|
|
|
|
|
|
// Compute the column number of the end.
|
|
|
|
unsigned EndColNo = CaratLine.size();
|
|
|
|
if (EndLineNo == LineNo) {
|
2007-12-13 01:39:36 +03:00
|
|
|
EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd());
|
2007-07-11 21:01:13 +04:00
|
|
|
if (EndColNo) {
|
|
|
|
--EndColNo; // Zero base the col #.
|
|
|
|
|
|
|
|
// Add in the length of the token, so that we cover multi-char tokens.
|
2007-12-13 01:39:36 +03:00
|
|
|
EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
|
2007-07-11 21:01:13 +04:00
|
|
|
} else {
|
|
|
|
EndColNo = CaratLine.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick the last non-whitespace column.
|
|
|
|
while (EndColNo-1 &&
|
|
|
|
(SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
|
|
|
|
--EndColNo;
|
|
|
|
|
|
|
|
// Fill the range with ~'s.
|
|
|
|
assert(StartColNo <= EndColNo && "Invalid range!");
|
|
|
|
for (unsigned i = StartColNo; i != EndColNo; ++i)
|
|
|
|
CaratLine[i] = '~';
|
|
|
|
}
|
|
|
|
|
2007-12-01 01:53:43 +03:00
|
|
|
void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic &Diags,
|
|
|
|
Diagnostic::Level Level,
|
2007-12-13 01:39:36 +03:00
|
|
|
FullSourceLoc Pos,
|
2007-07-11 21:01:13 +04:00
|
|
|
diag::kind ID,
|
|
|
|
const std::string *Strs,
|
|
|
|
unsigned NumStrs,
|
|
|
|
const SourceRange *Ranges,
|
|
|
|
unsigned NumRanges) {
|
2007-07-20 20:37:10 +04:00
|
|
|
unsigned LineNo = 0, ColNo = 0;
|
|
|
|
const char *LineStart = 0, *LineEnd = 0;
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
if (Pos.isValid()) {
|
2007-12-13 01:39:36 +03:00
|
|
|
FullSourceLoc LPos = Pos.getLogicalLoc();
|
|
|
|
LineNo = LPos.getLineNumber();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// First, if this diagnostic is not in the main file, print out the
|
|
|
|
// "included from" lines.
|
2007-12-13 01:39:36 +03:00
|
|
|
if (LastWarningLoc != LPos.getIncludeLoc()) {
|
|
|
|
LastWarningLoc = LPos.getIncludeLoc();
|
|
|
|
PrintIncludeStack(LastWarningLoc);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the column number. Rewind from the current position to the start
|
|
|
|
// of the line.
|
2007-12-13 01:39:36 +03:00
|
|
|
ColNo = LPos.getColumnNumber();
|
|
|
|
const char *TokLogicalPtr = LPos.getCharacterData();
|
2007-07-20 20:37:10 +04:00
|
|
|
LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Compute the line end. Scan forward from the error position to the end of
|
|
|
|
// the line.
|
2007-12-13 01:39:36 +03:00
|
|
|
const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
|
2007-07-11 21:01:13 +04:00
|
|
|
const char *BufEnd = Buffer->getBufferEnd();
|
2007-07-20 20:37:10 +04:00
|
|
|
LineEnd = TokLogicalPtr;
|
|
|
|
while (LineEnd != BufEnd &&
|
|
|
|
*LineEnd != '\n' && *LineEnd != '\r')
|
2007-07-11 21:01:13 +04:00
|
|
|
++LineEnd;
|
|
|
|
|
|
|
|
std::cerr << Buffer->getBufferIdentifier()
|
|
|
|
<< ":" << LineNo << ":";
|
|
|
|
if (ColNo && !NoShowColumn)
|
|
|
|
std::cerr << ColNo << ":";
|
|
|
|
std::cerr << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Level) {
|
|
|
|
default: assert(0 && "Unknown diagnostic type!");
|
|
|
|
case Diagnostic::Note: std::cerr << "note: "; break;
|
|
|
|
case Diagnostic::Warning: std::cerr << "warning: "; break;
|
|
|
|
case Diagnostic::Error: std::cerr << "error: "; break;
|
|
|
|
case Diagnostic::Fatal: std::cerr << "fatal error: "; break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-12-01 01:53:43 +03:00
|
|
|
std::cerr << FormatDiagnostic(Diags, Level, ID, Strs, NumStrs) << "\n";
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
if (!NoCaretDiagnostics && Pos.isValid()) {
|
|
|
|
// Get the line of the source file.
|
2007-07-20 20:37:10 +04:00
|
|
|
std::string SourceLine(LineStart, LineEnd);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Create a line for the carat that is filled with spaces that is the same
|
|
|
|
// length as the line of source code.
|
|
|
|
std::string CaratLine(LineEnd-LineStart, ' ');
|
|
|
|
|
|
|
|
// Highlight all of the characters covered by Ranges with ~ characters.
|
|
|
|
for (unsigned i = 0; i != NumRanges; ++i)
|
2007-12-13 01:39:36 +03:00
|
|
|
HighlightRange(Ranges[i], Pos.getManager(),LineNo, CaratLine, SourceLine);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Next, insert the carat itself.
|
|
|
|
if (ColNo-1 < CaratLine.size())
|
|
|
|
CaratLine[ColNo-1] = '^';
|
|
|
|
else
|
|
|
|
CaratLine.push_back('^');
|
|
|
|
|
|
|
|
// Scan the source line, looking for tabs. If we find any, manually expand
|
|
|
|
// them to 8 characters and update the CaratLine to match.
|
|
|
|
for (unsigned i = 0; i != SourceLine.size(); ++i) {
|
|
|
|
if (SourceLine[i] != '\t') continue;
|
|
|
|
|
|
|
|
// Replace this tab with at least one space.
|
|
|
|
SourceLine[i] = ' ';
|
|
|
|
|
|
|
|
// Compute the number of spaces we need to insert.
|
|
|
|
unsigned NumSpaces = ((i+8)&~7) - (i+1);
|
|
|
|
assert(NumSpaces < 8 && "Invalid computation of space amt");
|
|
|
|
|
|
|
|
// Insert spaces into the SourceLine.
|
|
|
|
SourceLine.insert(i+1, NumSpaces, ' ');
|
|
|
|
|
|
|
|
// Insert spaces or ~'s into CaratLine.
|
|
|
|
CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, remove any blank spaces from the end of CaratLine.
|
|
|
|
while (CaratLine[CaratLine.size()-1] == ' ')
|
|
|
|
CaratLine.erase(CaratLine.end()-1);
|
|
|
|
|
|
|
|
// Emit what we have computed.
|
|
|
|
std::cerr << SourceLine << "\n";
|
|
|
|
std::cerr << CaratLine << "\n";
|
|
|
|
}
|
|
|
|
}
|