mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Add CharSourceRange -- a half-open character range, which will be used in IDE
integration Motivation: libIDE clients should be simple, and they should not have to translate token-based SourceRanges to character locations. This also allows us to remove the dependency of DiagnosticConsumer on the Lexer. Now the DiagnosticEngine translates the diagnostics to CharSourceRanges and passes character-based ranges to the DiagnosticConsumer. Swift SVN r7173
This commit is contained in:
@@ -36,6 +36,19 @@ unsigned SourceManager::getLocOffsetInBuffer(SourceLoc Loc,
|
||||
return Loc.Value.getPointer() - Buffer->getBuffer().begin();
|
||||
}
|
||||
|
||||
unsigned SourceManager::getByteDistance(SourceLoc Start, SourceLoc End) const {
|
||||
#ifndef NDEBUG
|
||||
unsigned BufferID = findBufferContainingLoc(Start);
|
||||
auto *Buffer = LLVMSourceMgr.getMemoryBuffer(BufferID);
|
||||
assert(End.Value.getPointer() >= Buffer->getBuffer().begin() &&
|
||||
End.Value.getPointer() <= Buffer->getBuffer().end() &&
|
||||
"End location is not from the same buffer");
|
||||
#endif
|
||||
// When we have a rope buffer, could be implemented in terms of
|
||||
// getLocOffsetInBuffer().
|
||||
return End.Value.getPointer() - Start.Value.getPointer();
|
||||
}
|
||||
|
||||
void SourceLoc::printLineAndColumn(raw_ostream &OS,
|
||||
const SourceManager &SM) const {
|
||||
if (isInvalid()) {
|
||||
@@ -93,3 +106,12 @@ void SourceRange::dump(const SourceManager &SM) const {
|
||||
print(llvm::errs(), SM);
|
||||
}
|
||||
|
||||
CharSourceRange::CharSourceRange(SourceManager &SM, SourceLoc Start,
|
||||
SourceLoc End)
|
||||
: Start(Start) {
|
||||
assert(Start.isValid() == End.isValid() &&
|
||||
"Start and end should either both be valid or both be invalid!");
|
||||
if (Start.isValid())
|
||||
ByteLength = SM.getByteDistance(Start, End);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user