[PATCH] D67029: SourceManager: Prefer Optional<MemoryBufferRef> over MemoryBuffer*

Duncan P. N. Exon Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 30 17:29:35 PDT 2019


dexonsmith created this revision.
dexonsmith added reviewers: arphaman, Bigcheese.
Herald added subscribers: llvm-commits, ributzka, kadircet, jkorous, hiraditya.
Herald added a reviewer: martong.
Herald added a reviewer: shafik.
Herald added a project: LLVM.

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


https://reviews.llvm.org/D67029

Files:
  clang-tools-extra/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp
  clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp
  clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
  clang-tools-extra/clangd/ClangdUnit.cpp
  clang-tools-extra/clangd/Format.cpp
  clang-tools-extra/clangd/SourceCode.cpp
  clang-tools-extra/modularize/PreprocessorTracker.cpp
  clang/include/clang/Basic/SourceManager.h
  clang/include/clang/Frontend/FrontendAction.h
  clang/include/clang/Frontend/FrontendOptions.h
  clang/include/clang/Frontend/PrecompiledPreamble.h
  clang/include/clang/Lex/Lexer.h
  clang/lib/ARCMigrate/Transforms.cpp
  clang/lib/AST/ASTImporter.cpp
  clang/lib/Basic/Diagnostic.cpp
  clang/lib/Basic/SourceLocation.cpp
  clang/lib/Basic/SourceManager.cpp
  clang/lib/CodeGen/CGDebugInfo.cpp
  clang/lib/CodeGen/CodeGenAction.cpp
  clang/lib/Format/FormatTokenLexer.cpp
  clang/lib/Frontend/ASTUnit.cpp
  clang/lib/Frontend/CompilerInstance.cpp
  clang/lib/Frontend/FrontendAction.cpp
  clang/lib/Frontend/FrontendActions.cpp
  clang/lib/Frontend/PrecompiledPreamble.cpp
  clang/lib/Frontend/Rewrite/HTMLPrint.cpp
  clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
  clang/lib/Frontend/Rewrite/RewriteMacros.cpp
  clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp
  clang/lib/Frontend/Rewrite/RewriteObjC.cpp
  clang/lib/Frontend/VerifyDiagnosticConsumer.cpp
  clang/lib/Lex/Lexer.cpp
  clang/lib/Lex/ModuleMap.cpp
  clang/lib/Lex/PPLexerChange.cpp
  clang/lib/Lex/Preprocessor.cpp
  clang/lib/Rewrite/HTMLRewrite.cpp
  clang/lib/Rewrite/TokenRewriter.cpp
  clang/lib/Serialization/ASTWriter.cpp
  clang/lib/StaticAnalyzer/Checkers/LocalizationChecker.cpp
  clang/lib/StaticAnalyzer/Core/BugReporter.cpp
  clang/lib/StaticAnalyzer/Core/HTMLDiagnostics.cpp
  clang/lib/StaticAnalyzer/Core/IssueHash.cpp
  clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp
  clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
  clang/lib/Tooling/Inclusions/HeaderIncludes.cpp
  clang/lib/Tooling/Syntax/Tokens.cpp
  clang/tools/clang-diff/ClangDiff.cpp
  clang/tools/clang-import-test/clang-import-test.cpp
  clang/tools/libclang/CIndex.cpp
  llvm/include/llvm/Support/LineIterator.h
  llvm/include/llvm/Support/MemoryBuffer.h
  llvm/lib/Support/LineIterator.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67029.218194.patch
Type: text/x-patch
Size: 77694 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190831/93a97f12/attachment.bin>


More information about the llvm-commits mailing list