Basic: Untie swift::SourceLoc from llvm::SMLoc

Storing a `llvm::SMLoc` is a superfluous indirection, and getting rid of
it enables us to unconditionally import `SourceLoc` into Swift.
This commit is contained in:
Anthony Latsis
2025-06-25 17:01:36 +01:00
parent 8cbf5a26e4
commit 06a5670c8f
16 changed files with 117 additions and 117 deletions

View File

@@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
@@ -120,7 +120,7 @@ void SourceManager::createVirtualFile(SourceLoc Loc, StringRef Name,
CharSourceRange Range = CharSourceRange(Loc, Length);
// Skip if this range has already been added
VirtualFile &File = VirtualFiles[Range.getEnd().Value.getPointer()];
VirtualFile &File = VirtualFiles[Range.getEnd().getPointer()];
if (File.Range.isValid()) {
assert(Name == StringRef(File.Name));
assert(LineOffset == File.LineOffset);
@@ -132,8 +132,8 @@ void SourceManager::createVirtualFile(SourceLoc Loc, StringRef Name,
File.Name = Name.str();
File.LineOffset = LineOffset;
if (CachedVFile.first && Range.contains(SourceLoc(llvm::SMLoc::getFromPointer(
CachedVFile.first)))) {
if (CachedVFile.first &&
Range.contains(SourceLoc::getFromPointer(CachedVFile.first))) {
CachedVFile = {nullptr, nullptr};
}
}
@@ -143,7 +143,7 @@ bool SourceManager::openVirtualFile(SourceLoc loc, StringRef name,
CharSourceRange fullRange = getRangeForBuffer(findBufferContainingLoc(loc));
SourceLoc end;
auto nextRangeIter = VirtualFiles.upper_bound(loc.Value.getPointer());
auto nextRangeIter = VirtualFiles.upper_bound(loc.getPointer());
if (nextRangeIter != VirtualFiles.end() &&
fullRange.contains(nextRangeIter->second.Range.getStart())) {
const VirtualFile &existingFile = nextRangeIter->second;
@@ -160,7 +160,7 @@ bool SourceManager::openVirtualFile(SourceLoc loc, StringRef name,
}
CharSourceRange range = CharSourceRange(*this, loc, end);
VirtualFiles[end.Value.getPointer()] = {range, name.str(), lineOffset};
VirtualFiles[end.getPointer()] = {range, name.str(), lineOffset};
CachedVFile = {nullptr, nullptr};
return true;
}
@@ -183,16 +183,16 @@ void SourceManager::closeVirtualFile(SourceLoc end) {
CharSourceRange oldRange = virtualFile->Range;
virtualFile->Range = CharSourceRange(*this, virtualFile->Range.getStart(),
end);
VirtualFiles[end.Value.getPointer()] = std::move(*virtualFile);
VirtualFiles[end.getPointer()] = std::move(*virtualFile);
bool existed = VirtualFiles.erase(oldRange.getEnd().Value.getPointer());
bool existed = VirtualFiles.erase(oldRange.getEnd().getPointer());
assert(existed);
(void)existed;
}
const SourceManager::VirtualFile *
SourceManager::getVirtualFile(SourceLoc Loc) const {
const char *p = Loc.Value.getPointer();
const char *p = Loc.getPointer();
if (CachedVFile.first == p)
return CachedVFile.second;
@@ -328,7 +328,7 @@ StringRef SourceManager::getIdentifierForBuffer(
CharSourceRange SourceManager::getRangeForBuffer(unsigned bufferID) const {
auto *buffer = LLVMSourceMgr.getMemoryBuffer(bufferID);
SourceLoc start{llvm::SMLoc::getFromPointer(buffer->getBufferStart())};
auto start = SourceLoc::getFromPointer(buffer->getBufferStart());
return CharSourceRange(start, buffer->getBufferSize());
}
@@ -336,10 +336,10 @@ unsigned SourceManager::getLocOffsetInBuffer(SourceLoc Loc,
unsigned BufferID) const {
assert(Loc.isValid() && "location should be valid");
auto *Buffer = LLVMSourceMgr.getMemoryBuffer(BufferID);
assert(Loc.Value.getPointer() >= Buffer->getBuffer().begin() &&
Loc.Value.getPointer() <= Buffer->getBuffer().end() &&
assert(Loc.getPointer() >= Buffer->getBuffer().begin() &&
Loc.getPointer() <= Buffer->getBuffer().end() &&
"Location is not from the specified buffer");
return Loc.Value.getPointer() - Buffer->getBuffer().begin();
return Loc.getPointer() - Buffer->getBuffer().begin();
}
unsigned SourceManager::getByteDistance(SourceLoc Start, SourceLoc End) const {
@@ -348,13 +348,13 @@ 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() &&
assert(End.getPointer() >= Buffer->getBuffer().begin() &&
End.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();
return End.getPointer() - Start.getPointer();
}
unsigned SourceManager::getColumnInBuffer(SourceLoc Loc,
@@ -512,10 +512,10 @@ SourceManager::findBufferContainingLocInternal(SourceLoc Loc) const {
auto less_equal = std::less_equal<const char *>();
auto buffer = LLVMSourceMgr.getMemoryBuffer(bufferID);
return less_equal(buffer->getBufferStart(), Loc.Value.getPointer()) &&
return less_equal(buffer->getBufferStart(), Loc.getPointer()) &&
// Use <= here so that a pointer to the null at the end of the
// buffer is included as part of the buffer.
less_equal(Loc.Value.getPointer(), buffer->getBufferEnd());
less_equal(Loc.getPointer(), buffer->getBufferEnd());
};
// Check the last buffer we looked in.
@@ -569,15 +569,15 @@ SourceRange SourceRange::combine(ArrayRef<SourceRange> ranges) {
}
void SourceRange::widen(SourceRange Other) {
if (Other.Start.Value.getPointer() < Start.Value.getPointer())
if (Other.Start.getPointer() < Start.getPointer())
Start = Other.Start;
if (Other.End.Value.getPointer() > End.Value.getPointer())
if (Other.End.getPointer() > End.getPointer())
End = Other.End;
}
bool SourceRange::contains(SourceLoc Loc) const {
return Start.Value.getPointer() <= Loc.Value.getPointer() &&
Loc.Value.getPointer() <= End.Value.getPointer();
return Start.getPointer() <= Loc.getPointer() &&
Loc.getPointer() <= End.getPointer();
}
bool SourceRange::overlaps(SourceRange Other) const {