mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
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:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user