From aacef757ed3e34dba974a046a51e18c152a3428e Mon Sep 17 00:00:00 2001 From: Dmitri Hrybenko Date: Fri, 9 Aug 2013 22:50:58 +0000 Subject: [PATCH] Move decompose from PrettyStackTrace.cpp to the SourceManager It requires direct access to the SourceLoc internals and is generally useful anyway. Swift SVN r7111 --- include/swift/Basic/SourceManager.h | 10 ++++++++++ lib/AST/PrettyStackTrace.cpp | 26 ++++---------------------- lib/Basic/SourceLoc.cpp | 21 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/include/swift/Basic/SourceManager.h b/include/swift/Basic/SourceManager.h index 2f19f563f82..a66df8a2075 100644 --- a/include/swift/Basic/SourceManager.h +++ b/include/swift/Basic/SourceManager.h @@ -18,6 +18,14 @@ namespace swift { +/// \brief A decomposed representation of a source location, useful for +/// printing diagnostics. +struct DecomposedLoc { + const llvm::MemoryBuffer *Buffer; + unsigned Line; + unsigned Column; +}; + /// \brief This class manages and owns source buffers. class SourceManager { llvm::SourceMgr LLVMSourceMgr; @@ -91,6 +99,8 @@ public: /// \brief Returns the offset in bytes for the given source location. unsigned getLocOffsetInBuffer(SourceLoc Loc, unsigned BufferID) const; + + DecomposedLoc decompose(SourceLoc Loc) const; }; } // namespace swift diff --git a/lib/AST/PrettyStackTrace.cpp b/lib/AST/PrettyStackTrace.cpp index 524d046b305..ef4ca3250b9 100644 --- a/lib/AST/PrettyStackTrace.cpp +++ b/lib/AST/PrettyStackTrace.cpp @@ -25,29 +25,11 @@ using namespace swift; -struct DecomposedLoc { - llvm::MemoryBuffer *Buffer; - unsigned Line; - unsigned Column; -}; +static Optional decompose(SourceManager &SM, SourceLoc Loc) { + if (!Loc.isValid()) + return Nothing; -static Optional decompose(SourceManager &SM, SourceLoc loc) { - if (!loc.isValid()) return Nothing; - - int bufferIndex = SM->FindBufferContainingLoc(loc.Value); - if (bufferIndex == -1) return Nothing; - - DecomposedLoc result; - result.Buffer = SM->getBufferInfo(bufferIndex).Buffer; - result.Line = SM->FindLineNumber(loc.Value, bufferIndex); - - const char *lineStart = loc.Value.getPointer(); - while (lineStart != result.Buffer->getBufferStart() && - lineStart[-1] != '\n' && lineStart[-1] != '\r') - --lineStart; - result.Column = loc.Value.getPointer() - lineStart; - - return result; + return SM.decompose(Loc); } static void printDecomposedLoc(llvm::raw_ostream &out, diff --git a/lib/Basic/SourceLoc.cpp b/lib/Basic/SourceLoc.cpp index 1c821aec4a9..20859f5b74a 100644 --- a/lib/Basic/SourceLoc.cpp +++ b/lib/Basic/SourceLoc.cpp @@ -36,6 +36,27 @@ unsigned SourceManager::getLocOffsetInBuffer(SourceLoc Loc, return Loc.Value.getPointer() - Buffer->getBuffer().begin(); } +DecomposedLoc SourceManager::decompose(SourceLoc Loc) const { + assert(Loc.isValid()); + + unsigned BufferID = + unsigned(LLVMSourceMgr.FindBufferContainingLoc(Loc.Value)); + assert(BufferID != ~0U); + + 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; + + return Result; +} + void SourceLoc::printLineAndColon(raw_ostream &OS, const SourceManager &SM) const { int BufferIndex = SM->FindBufferContainingLoc(Value);