This is an archive of the discontinued LLVM Phabricator instance.

SourceManager: Prefer Optional<MemoryBufferRef> over MemoryBuffer*
AbandonedPublic

Authored by dexonsmith on Aug 30 2019, 5:29 PM.

Details

Summary

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>.

Diff Detail

Event Timeline

dexonsmith created this revision.Aug 30 2019, 5:29 PM
Herald added a reviewer: shafik. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
dexonsmith abandoned this revision.Oct 6 2020, 2:44 PM

This appears to be a duplicate of https://reviews.llvm.org/D66782. Abandon the newer copy.