Change the APIs in SourceManager returning const MemoryBuffer* to
return Optional<MemoryBufferRef> instead, and remove the `bool
*Invalid` flag from same. This removes the need to create dummy
invalid buffers in SrcMgr::ContentCache and clarifies buffer ownership.
A few APIs changed, and a few were added.
- SrcMgr::ContentCache::getBuffer returns Optional<MemoryBufferRef>.
- SrcMgr::ContentCache::getBufferIdentifier returns Optional<StringRef>.
- SrcMgr::ContentCache::getBufferSize returns Optional<unsigned>.
- SourceManager::getMemoryBufferForFile returns Optional<MemoryBufferRef>.
- SourceManager::getBuffer returns Optional<MemoryBufferRef>.
- SourceManager::getBufferOrFake returns MemoryBufferRef, replacing None with fake data.
- SourceManager::getBufferIdentifier returns Optional<StringRef>.
- SourceManager::getBufferIdentifierOrEmpty returns StringRef, replacing None with "".
- SourceManager::getBufferDataOrNone returns Optional<StringRef>
SourceManager::getBufferData did not change to return
Optional<StringRef> because its usage is too pervasive. A possible
follow-up would be to add SourceManager::getBufferDataOrFake (to match
the other APIs) and move components over but I'm not sure it's urgent.
Threading MemoryBufferRef (instead of const MemoryBuffer*) through had
a few small side effects:
- FrontendInputFile now stores a MemoryBufferRef.
- SourceManager::createFileID now takes a MemoryBufferRef.
- llvm::line_iterator now uses an Optional<StringRef>.
- MemoryBufferRef::operator== now exists and compares pointer identity for comparing Optional<MemoryBufferRef>.
I feel like all buffer requests for the main file should succeed and shouldn't need the fake buffer, unless something gone terribly wrong, Do you agree? Do you think it might be valuable to add a method like SM.getMainFileBuffer which has an llvm_unreachable if buffer is invalid?