Ensure that SourceFiles always have a backing buffer in the SourceManager

The "buffer ID" in a SourceFile, which is used to find the source file's
contents in the SourceManager, has always been optional. However, the
effectively every SourceFile actually does have a buffer ID, and the
vast majority of accesses to this information dereference the optional
without checking.

Update the handful of call sites that provided `nullopt` as the buffer
ID to provide a proper buffer instead. These were mostly unit tests
and testing programs, with a few places that passed a never-empty
optional through to the SourceFile constructor.

Then, remove optionality from the representation and accessors. It is
now the case that every SourceFile has a buffer ID, simplying a bunch
of code.
This commit is contained in:
Doug Gregor
2024-09-16 21:32:41 -07:00
parent 4e7fb73b0c
commit 49aa0e966f
41 changed files with 100 additions and 158 deletions

View File

@@ -1102,11 +1102,7 @@ public:
return;
}
if (!AstUnit->getPrimarySourceFile().getBufferID().has_value()) {
LOG_WARN_FUNC("Primary SourceFile is expected to have a BufferID");
return;
}
unsigned BufferID = AstUnit->getPrimarySourceFile().getBufferID().value();
unsigned BufferID = AstUnit->getPrimarySourceFile().getBufferID();
SemanticAnnotator Annotator(CompIns.getSourceMgr(), BufferID);
Annotator.walk(AstUnit->getPrimarySourceFile());
@@ -2388,7 +2384,7 @@ void SwiftEditorDocument::reportDocumentStructure(SourceFile &SrcFile,
EditorConsumer &Consumer) {
ide::SyntaxModelContext ModelContext(SrcFile);
SwiftDocumentStructureWalker Walker(SrcFile.getASTContext().SourceMgr,
*SrcFile.getBufferID(),
SrcFile.getBufferID(),
Consumer);
ModelContext.walk(Walker);
}
@@ -2612,7 +2608,7 @@ void SwiftLangSupport::getSemanticTokens(
"Unable to find input file"));
return;
}
SemanticAnnotator Annotator(CompIns.getSourceMgr(), *SF->getBufferID());
SemanticAnnotator Annotator(CompIns.getSourceMgr(), SF->getBufferID());
Annotator.walk(SF);
Receiver(
RequestResult<SemanticTokensResult>::fromResult(Annotator.SemaToks));