make SourceMgr tolerate empty SMLoc()'s better.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@156260 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2012-05-06 16:20:49 +00:00
parent 77c4ef8a47
commit eb034f4af3

View File

@ -149,11 +149,18 @@ SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
ArrayRef<SMRange> Ranges) const {
// First thing to do: find the current buffer containing the specified
// location.
// location to pull out the source line.
SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges;
std::pair<unsigned, unsigned> LineAndCol;
const char *BufferID = "<unknown>";
std::string LineStr;
if (Loc.isValid()) {
int CurBuf = FindBufferContainingLoc(Loc);
assert(CurBuf != -1 && "Invalid or unspecified location!");
MemoryBuffer *CurMB = getBufferInfo(CurBuf).Buffer;
BufferID = CurMB->getBufferIdentifier();
// Scan backward to find the start of the line.
const char *LineStart = Loc.getPointer();
@ -167,11 +174,10 @@ SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
const char *BufEnd = CurMB->getBufferEnd();
while (LineEnd != BufEnd && LineEnd[0] != '\n' && LineEnd[0] != '\r')
++LineEnd;
std::string LineStr(LineStart, LineEnd);
LineStr = std::string(LineStart, LineEnd);
// Convert any ranges to column ranges that only intersect the line of the
// location.
SmallVector<std::pair<unsigned, unsigned>, 4> ColRanges;
for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
SMRange R = Ranges[i];
if (!R.isValid()) continue;
@ -191,9 +197,10 @@ SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
R.End.getPointer()-LineStart));
}
std::pair<unsigned, unsigned> LineAndCol = getLineAndColumn(Loc, CurBuf);
return SMDiagnostic(*this, Loc,
CurMB->getBufferIdentifier(), LineAndCol.first,
LineAndCol = getLineAndColumn(Loc, CurBuf);
}
return SMDiagnostic(*this, Loc, BufferID, LineAndCol.first,
LineAndCol.second-1, Kind, Msg.str(),
LineStr, ColRanges);
}
@ -211,9 +218,11 @@ void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
raw_ostream &OS = errs();
if (Loc != SMLoc()) {
int CurBuf = FindBufferContainingLoc(Loc);
assert(CurBuf != -1 && "Invalid or unspecified location!");
PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
}
Diagnostic.print(0, OS, ShowColors);
}