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

@@ -49,7 +49,7 @@ collectRefactoringsAtCursor(SourceFile *SF, unsigned Line, unsigned Column,
DiagnosticEngine DiagEngine(SM);
std::for_each(DiagConsumers.begin(), DiagConsumers.end(),
[&](DiagnosticConsumer *Con) { DiagEngine.addConsumer(*Con); });
SourceLoc Loc = SM.getLocForLineCol(SF->getBufferID().value(), Line, Column);
SourceLoc Loc = SM.getLocForLineCol(SF->getBufferID(), Line, Column);
if (Loc.isInvalid())
return {};