mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Simplify the implementation of SourceManager::decompose() and its client
Swift SVN r7119
This commit is contained in:
@@ -25,60 +25,45 @@
|
|||||||
|
|
||||||
using namespace swift;
|
using namespace swift;
|
||||||
|
|
||||||
static Optional<DecomposedLoc> decompose(SourceManager &SM, SourceLoc Loc) {
|
|
||||||
if (!Loc.isValid())
|
|
||||||
return Nothing;
|
|
||||||
|
|
||||||
return SM.decompose(Loc);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void printDecomposedLoc(llvm::raw_ostream &out,
|
static void printDecomposedLoc(llvm::raw_ostream &out,
|
||||||
const DecomposedLoc &loc) {
|
const DecomposedLoc &loc) {
|
||||||
out << loc.Buffer->getBufferIdentifier()
|
out << loc.Buffer->getBufferIdentifier()
|
||||||
<< ":" << loc.Line << ':' << loc.Column;
|
<< ":" << loc.Line << ':' << loc.Column;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void printDecomposedLoc(llvm::raw_ostream &out,
|
void swift::printSourceLoc(llvm::raw_ostream &OS, SourceLoc Loc,
|
||||||
const Optional<DecomposedLoc> &loc) {
|
|
||||||
if (!loc) {
|
|
||||||
out << "<<invalid location>>";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return printDecomposedLoc(out, loc.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
void swift::printSourceLoc(llvm::raw_ostream &out, SourceLoc loc,
|
|
||||||
ASTContext &Context) {
|
ASTContext &Context) {
|
||||||
printDecomposedLoc(out, decompose(Context.SourceMgr, loc));
|
if (Loc.isInvalid()) {
|
||||||
}
|
OS << "<<invalid location>>";
|
||||||
|
|
||||||
static void printSourceRange(llvm::raw_ostream &out, SourceRange range,
|
|
||||||
ASTContext &Context) {
|
|
||||||
Optional<DecomposedLoc> start = decompose(Context.SourceMgr, range.Start);
|
|
||||||
Optional<DecomposedLoc> end = decompose(Context.SourceMgr, range.End);
|
|
||||||
|
|
||||||
// Use a unified message if both locations are invalid.
|
|
||||||
if (!start && !end) {
|
|
||||||
out << "<<invalid source range>>";
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return printDecomposedLoc(OS, Context.SourceMgr.decompose(Loc));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void printSourceRange(llvm::raw_ostream &OS, SourceRange SR,
|
||||||
|
ASTContext &Context) {
|
||||||
|
if (SR.isInvalid()) {
|
||||||
|
OS << "<<invalid source range>>";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DecomposedLoc Start = Context.SourceMgr.decompose(SR.Start);
|
||||||
|
DecomposedLoc End = Context.SourceMgr.decompose(SR.End);
|
||||||
|
|
||||||
// Print the start location as normal.
|
// Print the start location as normal.
|
||||||
printDecomposedLoc(out, start);
|
printDecomposedLoc(OS, Start);
|
||||||
out << '-';
|
OS << '-';
|
||||||
|
|
||||||
// Only print the non-matching information from the second location.
|
// Only print the non-matching information from the second location.
|
||||||
if (!start || !end || start.getValue().Buffer != end.getValue().Buffer) {
|
if (Start.Buffer != End.Buffer) {
|
||||||
printDecomposedLoc(out, end);
|
printDecomposedLoc(OS, End);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (Start.Line != End.Line) {
|
||||||
if (start.getValue().Line != end.getValue().Line) {
|
OS << End.Line << ':';
|
||||||
out << end.getValue().Line << ':';
|
|
||||||
}
|
}
|
||||||
|
OS << End.Column;
|
||||||
out << end.getValue().Column;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrettyStackTraceDecl::print(llvm::raw_ostream &out) const {
|
void PrettyStackTraceDecl::print(llvm::raw_ostream &out) const {
|
||||||
|
|||||||
@@ -45,14 +45,8 @@ DecomposedLoc SourceManager::decompose(SourceLoc Loc) const {
|
|||||||
|
|
||||||
DecomposedLoc Result;
|
DecomposedLoc Result;
|
||||||
Result.Buffer = LLVMSourceMgr.getMemoryBuffer(BufferID);
|
Result.Buffer = LLVMSourceMgr.getMemoryBuffer(BufferID);
|
||||||
Result.Line = LLVMSourceMgr.FindLineNumber(Loc.Value, BufferID);
|
std::tie(Result.Line, Result.Column) =
|
||||||
|
LLVMSourceMgr.getLineAndColumn(Loc.Value, BufferID);
|
||||||
const char *BufferStart = Result.Buffer->getBufferStart();
|
|
||||||
const char *LineStart = Loc.Value.getPointer();
|
|
||||||
while (LineStart != BufferStart &&
|
|
||||||
LineStart[-1] != '\n' && LineStart[-1] != '\r')
|
|
||||||
--LineStart;
|
|
||||||
Result.Column = Loc.Value.getPointer() - LineStart;
|
|
||||||
|
|
||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user