Skip to content

Commit da0c70f

Browse files
authored
Merge pull request swiftlang#76512 from DougGregor/sourcefile-nonopt-buffer
Ensure that SourceFiles always have a backing buffer in the SourceManager
2 parents 2eea17a + 0bd5195 commit da0c70f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+109
-162
lines changed

include/swift/AST/SourceFile.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@ class SourceFile final : public FileUnit {
156156
std::optional<StableHasher> InterfaceHasher;
157157

158158
/// The ID for the memory buffer containing this file's source.
159-
///
160-
/// May be -1, to indicate no association with a buffer.
161-
int BufferID;
159+
unsigned BufferID;
162160

163161
/// The parsing options for the file.
164162
ParsingOptions ParsingOpts;
@@ -370,7 +368,7 @@ class SourceFile final : public FileUnit {
370368
/// \c #sourceLocation(file:) declarations.
371369
llvm::StringMap<SourceFilePathInfo> getInfoForUsedFilePaths() const;
372370

373-
SourceFile(ModuleDecl &M, SourceFileKind K, std::optional<unsigned> bufferID,
371+
SourceFile(ModuleDecl &M, SourceFileKind K, unsigned bufferID,
374372
ParsingOptions parsingOpts = {}, bool isPrimary = false);
375373

376374
~SourceFile();
@@ -590,9 +588,7 @@ class SourceFile final : public FileUnit {
590588

591589
/// The buffer ID for the file that was imported, or None if there
592590
/// is no associated buffer.
593-
std::optional<unsigned> getBufferID() const {
594-
if (BufferID == -1)
595-
return std::nullopt;
591+
unsigned getBufferID() const {
596592
return BufferID;
597593
}
598594

include/swift/Frontend/Frontend.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ class CompilerInstance {
789789
/// Creates a new source file for the main module.
790790
SourceFile *createSourceFileForMainModule(ModuleDecl *mod,
791791
SourceFileKind FileKind,
792-
std::optional<unsigned> BufferID,
792+
unsigned BufferID,
793793
bool isMainBuffer = false) const;
794794

795795
/// Creates all the files to be added to the main module, appending them to

include/swift/IDE/IDERequests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct CursorInfoOwner {
5050
return !(lhs == rhs);
5151
}
5252
bool isValid() const {
53-
return File && File->getBufferID() && Loc.isValid();
53+
return File && Loc.isValid();
5454
}
5555
};
5656

@@ -110,7 +110,7 @@ struct RangeInfoOwner {
110110
}
111111

112112
bool isValid() const {
113-
return File && File->getBufferID() && StartLoc.isValid() && EndLoc.isValid();
113+
return File && StartLoc.isValid() && EndLoc.isValid();
114114
}
115115
};
116116

include/swift/Migrator/ASTMigratorPass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ASTMigratorPass {
4141
ASTMigratorPass(EditorAdapter &Editor, SourceFile *SF,
4242
const MigratorOptions &Opts)
4343
: Editor(Editor), SF(SF), Opts(Opts), Filename(SF->getFilename()),
44-
BufferID(SF->getBufferID().value()),
44+
BufferID(SF->getBufferID()),
4545
SM(SF->getASTContext().SourceMgr), Diags(SF->getASTContext().Diags) {}
4646
};
4747

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6622,9 +6622,8 @@ void VarDecl::setOriginalWrappedProperty(VarDecl *originalProperty) {
66226622
static bool isSourceLocInOrignalBuffer(const Decl *D, SourceLoc Loc) {
66236623
assert(Loc.isValid());
66246624
auto bufferID = D->getDeclContext()->getParentSourceFile()->getBufferID();
6625-
assert(bufferID.has_value() && "Source buffer ID must be set");
66266625
auto &SM = D->getASTContext().SourceMgr;
6627-
return SM.getRangeForBuffer(*bufferID).contains(Loc);
6626+
return SM.getRangeForBuffer(bufferID).contains(Loc);
66286627
}
66296628
#endif
66306629

lib/AST/ASTScopeCreation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
272272

273273
if (SF->Kind == SourceFileKind::DefaultArgument) {
274274
auto genInfo = *SF->getASTContext().SourceMgr.getGeneratedSourceInfo(
275-
*SF->getBufferID());
275+
SF->getBufferID());
276276
parentLoc = ASTNode::getFromOpaqueValue(genInfo.astNode).getStartLoc();
277277
if (auto parentScope =
278278
findStartingScopeForLookup(enclosingSF, parentLoc)) {

lib/AST/ASTScopePrinting.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,8 @@ void ASTScopeImpl::dump() const { print(llvm::errs(), 0, false); }
4343
void ASTScopeImpl::dumpOneScopeMapLocation(
4444
std::pair<unsigned, unsigned> lineColumn) {
4545
auto bufferID = getSourceFile()->getBufferID();
46-
if (!bufferID) {
47-
llvm::errs() << "***No buffer, dumping all scopes***";
48-
print(llvm::errs());
49-
return;
50-
}
5146
SourceLoc loc = getSourceManager().getLocForLineCol(
52-
*bufferID, lineColumn.first, lineColumn.second);
47+
bufferID, lineColumn.first, lineColumn.second);
5348
if (loc.isInvalid())
5449
return;
5550

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,9 @@ SourceRange GenericParamScope::getSourceRangeOfThisASTNode(
203203

204204
SourceRange ASTSourceFileScope::getSourceRangeOfThisASTNode(
205205
const bool omitAssertions) const {
206-
if (auto bufferID = SF->getBufferID()) {
207-
auto charRange = getSourceManager().getRangeForBuffer(*bufferID);
208-
return SourceRange(charRange.getStart(), charRange.getEnd());
209-
}
210-
211-
if (SF->getTopLevelItems().empty())
212-
return SourceRange();
213-
214-
// Use the source ranges of the declarations in the file.
215-
return SourceRange(SF->getTopLevelItems().front().getStartLoc(),
216-
SF->getTopLevelItems().back().getEndLoc());
206+
auto bufferID = SF->getBufferID();
207+
auto charRange = getSourceManager().getRangeForBuffer(bufferID);
208+
return SourceRange(charRange.getStart(), charRange.getEnd());
217209
}
218210

219211
SourceRange GenericTypeOrExtensionScope::getSourceRangeOfThisASTNode(

lib/AST/DiagnosticEngine.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,9 +1759,7 @@ BufferIndirectlyCausingDiagnosticRAII::BufferIndirectlyCausingDiagnosticRAII(
17591759
const SourceFile &SF)
17601760
: Diags(SF.getASTContext().Diags) {
17611761
auto id = SF.getBufferID();
1762-
if (!id)
1763-
return;
1764-
auto loc = SF.getASTContext().SourceMgr.getLocForBufferStart(*id);
1762+
auto loc = SF.getASTContext().SourceMgr.getLocForBufferStart(id);
17651763
if (loc.isValid())
17661764
Diags.setBufferIndirectlyCausingDiagnosticToInput(loc);
17671765
}

lib/AST/Module.cpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,8 @@ namespace {
775775
SourceManager *sourceMgr;
776776

777777
bool operator()(SourceFile *lhs, SourceFile *rhs) const {
778-
auto lhsRange = sourceMgr->getRangeForBuffer(*lhs->getBufferID());
779-
auto rhsRange = sourceMgr->getRangeForBuffer(*rhs->getBufferID());
778+
auto lhsRange = sourceMgr->getRangeForBuffer(lhs->getBufferID());
779+
auto rhsRange = sourceMgr->getRangeForBuffer(rhs->getBufferID());
780780

781781
std::less<const char *> pointerCompare;
782782
return pointerCompare(
@@ -785,7 +785,7 @@ namespace {
785785
}
786786

787787
bool operator()(SourceFile *lhs, SourceLoc rhsLoc) const {
788-
auto lhsRange = sourceMgr->getRangeForBuffer(*lhs->getBufferID());
788+
auto lhsRange = sourceMgr->getRangeForBuffer(lhs->getBufferID());
789789

790790
std::less<const char *> pointerCompare;
791791
return pointerCompare(
@@ -794,7 +794,7 @@ namespace {
794794
}
795795

796796
bool operator()(SourceLoc lhsLoc, SourceFile *rhs) const {
797-
auto rhsRange = sourceMgr->getRangeForBuffer(*rhs->getBufferID());
797+
auto rhsRange = sourceMgr->getRangeForBuffer(rhs->getBufferID());
798798

799799
std::less<const char *> pointerCompare;
800800
return pointerCompare(
@@ -835,8 +835,7 @@ void ModuleDecl::updateSourceFileLocationMap() {
835835
// First, add all of the source files with a backing buffer.
836836
for (auto *fileUnit : files) {
837837
if (auto sourceFile = dyn_cast<SourceFile>(fileUnit)) {
838-
if (sourceFile->getBufferID())
839-
sourceFileLocationMap->allSourceFiles.push_back(sourceFile);
838+
sourceFileLocationMap->allSourceFiles.push_back(sourceFile);
840839
}
841840
}
842841

@@ -872,7 +871,7 @@ SourceFile *ModuleDecl::getSourceFileContainingLocation(SourceLoc loc) {
872871
// in to see if it contains this.
873872
if (sourceFileLocationMap) {
874873
if (auto lastSourceFile = sourceFileLocationMap->lastSourceFile) {
875-
auto range = sourceMgr.getRangeForBuffer(*lastSourceFile->getBufferID());
874+
auto range = sourceMgr.getRangeForBuffer(lastSourceFile->getBufferID());
876875
if (range.contains(adjustedLoc))
877876
return lastSourceFile;
878877
}
@@ -888,7 +887,7 @@ SourceFile *ModuleDecl::getSourceFileContainingLocation(SourceLoc loc) {
888887
return nullptr;
889888

890889
auto foundSourceFile = *found;
891-
auto foundRange = sourceMgr.getRangeForBuffer(*foundSourceFile->getBufferID());
890+
auto foundRange = sourceMgr.getRangeForBuffer(foundSourceFile->getBufferID());
892891
// Positions inside an empty file or at EOF should still be considered within
893892
// this file.
894893
if (!foundRange.contains(adjustedLoc) && adjustedLoc != foundRange.getEnd())
@@ -1192,7 +1191,7 @@ SourceRange SourceFile::getMacroInsertionRange() const {
11921191
return SourceRange();
11931192

11941193
auto generatedInfo =
1195-
*getASTContext().SourceMgr.getGeneratedSourceInfo(*getBufferID());
1194+
*getASTContext().SourceMgr.getGeneratedSourceInfo(getBufferID());
11961195
auto origRange = generatedInfo.originalSourceRange;
11971196
return {origRange.getStart(), origRange.getEnd()};
11981197
}
@@ -1202,7 +1201,7 @@ CustomAttr *SourceFile::getAttachedMacroAttribute() const {
12021201
return nullptr;
12031202

12041203
auto genInfo =
1205-
*getASTContext().SourceMgr.getGeneratedSourceInfo(*getBufferID());
1204+
*getASTContext().SourceMgr.getGeneratedSourceInfo(getBufferID());
12061205
return genInfo.attachedMacroCustomAttr;
12071206
}
12081207

@@ -1211,7 +1210,7 @@ std::optional<MacroRole> SourceFile::getFulfilledMacroRole() const {
12111210
return std::nullopt;
12121211

12131212
auto genInfo =
1214-
*getASTContext().SourceMgr.getGeneratedSourceInfo(*getBufferID());
1213+
*getASTContext().SourceMgr.getGeneratedSourceInfo(getBufferID());
12151214
switch (genInfo.kind) {
12161215
#define MACRO_ROLE(Name, Description) \
12171216
case GeneratedSourceInfo::Name##MacroExpansion: \
@@ -1231,7 +1230,7 @@ SourceFile *SourceFile::getEnclosingSourceFile() const {
12311230
return nullptr;
12321231

12331232
auto genInfo =
1234-
*getASTContext().SourceMgr.getGeneratedSourceInfo(*getBufferID());
1233+
*getASTContext().SourceMgr.getGeneratedSourceInfo(getBufferID());
12351234
auto sourceLoc = genInfo.originalSourceRange.getStart();
12361235
return getParentModule()->getSourceFileContainingLocation(sourceLoc);
12371236
}
@@ -1242,7 +1241,7 @@ ASTNode SourceFile::getNodeInEnclosingSourceFile() const {
12421241
return nullptr;
12431242

12441243
auto genInfo =
1245-
*getASTContext().SourceMgr.getGeneratedSourceInfo(*getBufferID());
1244+
*getASTContext().SourceMgr.getGeneratedSourceInfo(getBufferID());
12461245
return ASTNode::getFromOpaqueValue(genInfo.astNode);
12471246
}
12481247

@@ -1470,7 +1469,7 @@ SourceFile::getExternalRawLocsForDecl(const Decl *D) const {
14701469
bool InGeneratedBuffer =
14711470
!SM.rangeContainsTokenLoc(SM.getRangeForBuffer(BufferID), MainLoc);
14721471
if (InGeneratedBuffer) {
1473-
int UnderlyingBufferID;
1472+
unsigned UnderlyingBufferID;
14741473
std::tie(UnderlyingBufferID, MainLoc) =
14751474
D->getModuleContext()->getOriginalLocation(MainLoc);
14761475
if (BufferID != UnderlyingBufferID)
@@ -2108,8 +2107,8 @@ bool ModuleDecl::registerEntryPointFile(
21082107
if (existingDecl) {
21092108
existingDiagLoc = sourceFile->getMainDeclDiagLoc();
21102109
} else {
2111-
if (auto bufID = sourceFile->getBufferID())
2112-
existingDiagLoc = getASTContext().SourceMgr.getLocForBufferStart(*bufID);
2110+
auto bufID = sourceFile->getBufferID();
2111+
existingDiagLoc = getASTContext().SourceMgr.getLocForBufferStart(bufID);
21132112
}
21142113
}
21152114

@@ -3296,10 +3295,8 @@ llvm::StringMap<SourceFilePathInfo>
32963295
SourceFile::getInfoForUsedFilePaths() const {
32973296
llvm::StringMap<SourceFilePathInfo> result;
32983297

3299-
if (BufferID != -1) {
3300-
result[getFilename()].physicalFileLoc =
3301-
getASTContext().SourceMgr.getLocForBufferStart(BufferID);
3302-
}
3298+
result[getFilename()].physicalFileLoc =
3299+
getASTContext().SourceMgr.getLocForBufferStart(BufferID);
33033300

33043301
for (auto &vpath : VirtualFilePaths) {
33053302
result[vpath.Item].virtualFileLocs.insert(vpath.Loc);
@@ -3433,10 +3430,11 @@ ModuleDecl::computeFileIDMap(bool shouldDiagnose) const {
34333430
}
34343431

34353432
SourceFile::SourceFile(ModuleDecl &M, SourceFileKind K,
3436-
std::optional<unsigned> bufferID,
3433+
unsigned bufferID,
34373434
ParsingOptions parsingOpts, bool isPrimary)
3438-
: FileUnit(FileUnitKind::Source, M), BufferID(bufferID ? *bufferID : -1),
3435+
: FileUnit(FileUnitKind::Source, M), BufferID(bufferID),
34393436
ParsingOpts(parsingOpts), IsPrimary(isPrimary), Kind(K) {
3437+
assert(BufferID != (unsigned)~0);
34403438
M.getASTContext().addDestructorCleanup(*this);
34413439

34423440
assert(!IsPrimary || M.isMainModule() &&
@@ -3643,8 +3641,6 @@ bool SourceFile::walk(ASTWalker &walker) {
36433641
}
36443642

36453643
StringRef SourceFile::getFilename() const {
3646-
if (BufferID == -1)
3647-
return "";
36483644
SourceManager &SM = getASTContext().SourceMgr;
36493645
return SM.getIdentifierForBuffer(BufferID);
36503646
}

0 commit comments

Comments
 (0)