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

@@ -751,9 +751,7 @@ static SourceLoc getDeclStartPosition(SourceFile &File) {
}
static void printUntilFirstDeclStarts(SourceFile &File, ASTPrinter &Printer) {
if (!File.getBufferID().has_value())
return;
auto BufferID = *File.getBufferID();
auto BufferID = File.getBufferID();
auto &SM = File.getASTContext().SourceMgr;
CharSourceRange TextRange = SM.getRangeForBuffer(BufferID);