[clang] 2dc7e0c - clang/Basic: Replace SourceManager::getMemoryBufferForFile, NFC
Duncan P. N. Exon Smith via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 20 14:01:37 PDT 2020
Author: Duncan P. N. Exon Smith
Date: 2020-10-20T17:01:23-04:00
New Revision: 2dc7e0c6a586135fe236cd7ebf065617a7638bdb
URL: https://github.com/llvm/llvm-project/commit/2dc7e0c6a586135fe236cd7ebf065617a7638bdb
DIFF: https://github.com/llvm/llvm-project/commit/2dc7e0c6a586135fe236cd7ebf065617a7638bdb.diff
LOG: clang/Basic: Replace SourceManager::getMemoryBufferForFile, NFC
Replace `SourceManager::getMemoryBufferForFile`, which returned a
dereferenceable `MemoryBuffer*` and had a `bool*Invalid` out parameter,
with `getMemoryBufferForFileOrNone` (returning
`Optional<MemoryBufferRef>`) and `getMemoryBufferForFileOrFake`
(returning `MemoryBufferRef`).
Differential Revision: https://reviews.llvm.org/D89429
Added:
Modified:
clang/include/clang/Basic/SourceManager.h
clang/include/clang/Frontend/PrecompiledPreamble.h
clang/lib/Basic/SourceManager.cpp
clang/lib/Frontend/PrecompiledPreamble.cpp
clang/lib/Lex/Preprocessor.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index e1b510bab07c..905b706971d5 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -901,10 +901,18 @@ class SourceManager : public RefCountedBase<SourceManager> {
/// Retrieve the memory buffer associated with the given file.
///
- /// \param Invalid If non-NULL, will be set \c true if an error
- /// occurs while retrieving the memory buffer.
- const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
- bool *Invalid = nullptr);
+ /// Returns None if the buffer is not valid.
+ llvm::Optional<llvm::MemoryBufferRef>
+ getMemoryBufferForFileOrNone(const FileEntry *File);
+
+ /// Retrieve the memory buffer associated with the given file.
+ ///
+ /// Returns a fake buffer if there isn't a real one.
+ llvm::MemoryBufferRef getMemoryBufferForFileOrFake(const FileEntry *File) {
+ if (auto B = getMemoryBufferForFileOrNone(File))
+ return *B;
+ return getFakeBufferForRecovery()->getMemBufferRef();
+ }
/// Override the contents of the given source file by providing an
/// already-allocated buffer.
diff --git a/clang/include/clang/Frontend/PrecompiledPreamble.h b/clang/include/clang/Frontend/PrecompiledPreamble.h
index 0f7e9d895a00..99faf60f1a44 100644
--- a/clang/include/clang/Frontend/PrecompiledPreamble.h
+++ b/clang/include/clang/Frontend/PrecompiledPreamble.h
@@ -26,6 +26,7 @@
namespace llvm {
class MemoryBuffer;
+class MemoryBufferRef;
namespace vfs {
class FileSystem;
}
@@ -216,7 +217,7 @@ class PrecompiledPreamble {
static PreambleFileHash createForFile(off_t Size, time_t ModTime);
static PreambleFileHash
- createForMemoryBuffer(const llvm::MemoryBuffer *Buffer);
+ createForMemoryBuffer(const llvm::MemoryBufferRef &Buffer);
friend bool operator==(const PreambleFileHash &LHS,
const PreambleFileHash &RHS) {
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 9902709c4e63..51772d5aa335 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -701,14 +701,11 @@ SourceManager::createExpansionLocImpl(const ExpansionInfo &Info,
return SourceLocation::getMacroLoc(NextLocalOffset - (TokLength + 1));
}
-const llvm::MemoryBuffer *
-SourceManager::getMemoryBufferForFile(const FileEntry *File, bool *Invalid) {
+llvm::Optional<llvm::MemoryBufferRef>
+SourceManager::getMemoryBufferForFileOrNone(const FileEntry *File) {
const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
assert(IR && "getOrCreateContentCache() cannot return NULL");
- auto *B = IR->getBufferPointer(Diag, getFileManager(), SourceLocation());
- if (Invalid)
- *Invalid = !B;
- return B ? B : getFakeBufferForRecovery();
+ return IR->getBufferOrNone(Diag, getFileManager(), SourceLocation());
}
void SourceManager::overrideFileContents(const FileEntry *SourceFile,
diff --git a/clang/lib/Frontend/PrecompiledPreamble.cpp b/clang/lib/Frontend/PrecompiledPreamble.cpp
index 3fb61f37ae2b..87cd9169c78c 100644
--- a/clang/lib/Frontend/PrecompiledPreamble.cpp
+++ b/clang/lib/Frontend/PrecompiledPreamble.cpp
@@ -463,7 +463,8 @@ llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build(
PrecompiledPreamble::PreambleFileHash::createForFile(File->getSize(),
ModTime);
} else {
- const llvm::MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File);
+ llvm::MemoryBufferRef Buffer =
+ SourceMgr.getMemoryBufferForFileOrFake(File);
FilesInPreamble[File->getName()] =
PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(Buffer);
}
@@ -549,7 +550,7 @@ bool PrecompiledPreamble::CanReuse(const CompilerInvocation &Invocation,
llvm::StringMap<PreambleFileHash> OverridenFileBuffers;
for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) {
const PrecompiledPreamble::PreambleFileHash PreambleHash =
- PreambleFileHash::createForMemoryBuffer(RB.second);
+ PreambleFileHash::createForMemoryBuffer(RB.second->getMemBufferRef());
llvm::vfs::Status Status;
if (moveOnNoError(VFS->status(RB.first), Status))
OverriddenFiles[Status.getUniqueID()] = PreambleHash;
@@ -783,13 +784,13 @@ PrecompiledPreamble::PreambleFileHash::createForFile(off_t Size,
PrecompiledPreamble::PreambleFileHash
PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(
- const llvm::MemoryBuffer *Buffer) {
+ const llvm::MemoryBufferRef &Buffer) {
PreambleFileHash Result;
- Result.Size = Buffer->getBufferSize();
+ Result.Size = Buffer.getBufferSize();
Result.ModTime = 0;
llvm::MD5 MD5Ctx;
- MD5Ctx.update(Buffer->getBuffer().data());
+ MD5Ctx.update(Buffer.getBuffer().data());
MD5Ctx.final(Result.MD5);
return Result;
diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index 5f362ed0754d..94f1ce91f884 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -395,12 +395,10 @@ bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File,
assert(CompleteLine && CompleteColumn && "Starts from 1:1");
assert(!CodeCompletionFile && "Already set");
- using llvm::MemoryBuffer;
-
// Load the actual file's contents.
- bool Invalid = false;
- const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid);
- if (Invalid)
+ Optional<llvm::MemoryBufferRef> Buffer =
+ SourceMgr.getMemoryBufferForFileOrNone(File);
+ if (!Buffer)
return true;
// Find the byte position of the truncation point.
More information about the cfe-commits
mailing list