Move decompose from PrettyStackTrace.cpp to the SourceManager

It requires direct access to the SourceLoc internals and is generally useful
anyway.


Swift SVN r7111
This commit is contained in:
Dmitri Hrybenko
2013-08-09 22:50:58 +00:00
parent dc655eb1cb
commit aacef757ed
3 changed files with 35 additions and 22 deletions

View File

@@ -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

View File

@@ -25,29 +25,11 @@
using namespace swift;
struct DecomposedLoc {
llvm::MemoryBuffer *Buffer;
unsigned Line;
unsigned Column;
};
static Optional<DecomposedLoc> decompose(SourceManager &SM, SourceLoc Loc) {
if (!Loc.isValid())
return Nothing;
static Optional<DecomposedLoc> 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,

View File

@@ -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);