diff --git a/lib/AST/PrettyStackTrace.cpp b/lib/AST/PrettyStackTrace.cpp index ef4ca3250b9..99a2f3ebaa5 100644 --- a/lib/AST/PrettyStackTrace.cpp +++ b/lib/AST/PrettyStackTrace.cpp @@ -25,60 +25,45 @@ using namespace swift; -static Optional decompose(SourceManager &SM, SourceLoc Loc) { - if (!Loc.isValid()) - return Nothing; - - return SM.decompose(Loc); -} - static void printDecomposedLoc(llvm::raw_ostream &out, const DecomposedLoc &loc) { out << loc.Buffer->getBufferIdentifier() << ":" << loc.Line << ':' << loc.Column; } -static void printDecomposedLoc(llvm::raw_ostream &out, - const Optional &loc) { - if (!loc) { - out << "<>"; - return; - } - - return printDecomposedLoc(out, loc.getValue()); -} - -void swift::printSourceLoc(llvm::raw_ostream &out, SourceLoc loc, +void swift::printSourceLoc(llvm::raw_ostream &OS, SourceLoc Loc, ASTContext &Context) { - printDecomposedLoc(out, decompose(Context.SourceMgr, loc)); -} - -static void printSourceRange(llvm::raw_ostream &out, SourceRange range, - ASTContext &Context) { - Optional start = decompose(Context.SourceMgr, range.Start); - Optional end = decompose(Context.SourceMgr, range.End); - - // Use a unified message if both locations are invalid. - if (!start && !end) { - out << "<>"; + if (Loc.isInvalid()) { + OS << "<>"; return; } + return printDecomposedLoc(OS, Context.SourceMgr.decompose(Loc)); +} + +static void printSourceRange(llvm::raw_ostream &OS, SourceRange SR, + ASTContext &Context) { + if (SR.isInvalid()) { + OS << "<>"; + return; + } + + DecomposedLoc Start = Context.SourceMgr.decompose(SR.Start); + DecomposedLoc End = Context.SourceMgr.decompose(SR.End); + // Print the start location as normal. - printDecomposedLoc(out, start); - out << '-'; + printDecomposedLoc(OS, Start); + OS << '-'; // Only print the non-matching information from the second location. - if (!start || !end || start.getValue().Buffer != end.getValue().Buffer) { - printDecomposedLoc(out, end); + if (Start.Buffer != End.Buffer) { + printDecomposedLoc(OS, End); return; } - - if (start.getValue().Line != end.getValue().Line) { - out << end.getValue().Line << ':'; + if (Start.Line != End.Line) { + OS << End.Line << ':'; } - - out << end.getValue().Column; + OS << End.Column; } void PrettyStackTraceDecl::print(llvm::raw_ostream &out) const { diff --git a/lib/Basic/SourceLoc.cpp b/lib/Basic/SourceLoc.cpp index 20859f5b74a..948c42f860a 100644 --- a/lib/Basic/SourceLoc.cpp +++ b/lib/Basic/SourceLoc.cpp @@ -45,14 +45,8 @@ DecomposedLoc SourceManager::decompose(SourceLoc Loc) const { DecomposedLoc Result; Result.Buffer = LLVMSourceMgr.getMemoryBuffer(BufferID); - Result.Line = LLVMSourceMgr.FindLineNumber(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; + std::tie(Result.Line, Result.Column) = + LLVMSourceMgr.getLineAndColumn(Loc.Value, BufferID); return Result; }