[clang] 51d1d58 - clang/Frontend: Use MemoryBufferRef in FrontendInputFile (and remove SourceManager::getBuffer)
Duncan P. N. Exon Smith via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 20 10:35:56 PDT 2020
Author: Duncan P. N. Exon Smith
Date: 2020-10-20T13:35:46-04:00
New Revision: 51d1d585e5838ea0f02f1271f7543c4e43639969
URL: https://github.com/llvm/llvm-project/commit/51d1d585e5838ea0f02f1271f7543c4e43639969
DIFF: https://github.com/llvm/llvm-project/commit/51d1d585e5838ea0f02f1271f7543c4e43639969.diff
LOG: clang/Frontend: Use MemoryBufferRef in FrontendInputFile (and remove SourceManager::getBuffer)
In order to drop the final callers to `SourceManager::getBuffer`, change
`FrontendInputFile` to use `Optional<MemoryBufferRef>`. Also updated
the "unowned" version of `SourceManager::createFileID` to take a
`MemoryBufferRef` (it now calls `MemoryBuffer::getMemBuffer`, which
creates a `MemoryBuffer` that does not own the buffer data).
Differential Revision: https://reviews.llvm.org/D89427
Added:
Modified:
clang/include/clang/Basic/SourceManager.h
clang/include/clang/Frontend/FrontendAction.h
clang/include/clang/Frontend/FrontendOptions.h
clang/lib/Basic/SourceManager.cpp
clang/lib/Format/MacroExpander.cpp
clang/lib/Frontend/ASTUnit.cpp
clang/lib/Frontend/CompilerInstance.cpp
clang/lib/Frontend/FrontendAction.cpp
clang/lib/Frontend/FrontendActions.cpp
clang/unittests/Format/TestLexer.h
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index 2156a013e53b..e1b510bab07c 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -860,13 +860,11 @@ class SourceManager : public RefCountedBase<SourceManager> {
int LoadedID = 0, unsigned LoadedOffset = 0,
SourceLocation IncludeLoc = SourceLocation());
- enum UnownedTag { Unowned };
-
/// Create a new FileID that represents the specified memory buffer.
///
/// This does not take ownership of the MemoryBuffer. The memory buffer must
/// outlive the SourceManager.
- FileID createFileID(UnownedTag, const llvm::MemoryBuffer *Buffer,
+ FileID createFileID(const llvm::MemoryBufferRef &Buffer,
SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
int LoadedID = 0, unsigned LoadedOffset = 0,
SourceLocation IncludeLoc = SourceLocation());
@@ -991,36 +989,6 @@ class SourceManager : public RefCountedBase<SourceManager> {
return getFakeBufferForRecovery()->getMemBufferRef();
}
- /// Return the buffer for the specified FileID.
- ///
- /// If there is an error opening this buffer the first time, this
- /// manufactures a temporary buffer and returns a non-empty error string.
- ///
- /// TODO: Update users of Invalid to call getBufferOrNone and change return
- /// type to MemoryBufferRef.
- const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
- bool *Invalid = nullptr) const {
- bool MyInvalid = false;
- const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
- if (MyInvalid || !Entry.isFile()) {
- if (Invalid)
- *Invalid = true;
-
- return getFakeBufferForRecovery();
- }
-
- auto *B = Entry.getFile().getContentCache()->getBufferPointer(
- Diag, getFileManager(), Loc);
- if (Invalid)
- *Invalid = !B;
- return B ? B : getFakeBufferForRecovery();
- }
-
- const llvm::MemoryBuffer *getBuffer(FileID FID,
- bool *Invalid = nullptr) const {
- return getBuffer(FID, SourceLocation(), Invalid);
- }
-
/// Returns the FileEntry record for the provided FileID.
const FileEntry *getFileEntryForID(FileID FID) const {
bool MyInvalid = false;
@@ -1844,7 +1812,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
/// Create a new ContentCache for the specified memory buffer.
const SrcMgr::ContentCache *
- createMemBufferContentCache(const llvm::MemoryBuffer *Buf, bool DoNotFree);
+ createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer> Buf);
FileID getFileIDSlow(unsigned SLocOffset) const;
FileID getFileIDLocal(unsigned SLocOffset) const;
diff --git a/clang/include/clang/Frontend/FrontendAction.h b/clang/include/clang/Frontend/FrontendAction.h
index c9f9f080c141..319b3bc62cc4 100644
--- a/clang/include/clang/Frontend/FrontendAction.h
+++ b/clang/include/clang/Frontend/FrontendAction.h
@@ -145,7 +145,7 @@ class FrontendAction {
assert(!CurrentInput.isEmpty() && "No current file!");
return CurrentInput.isFile()
? CurrentInput.getFile()
- : CurrentInput.getBuffer()->getBufferIdentifier();
+ : CurrentInput.getBuffer().getBufferIdentifier();
}
InputKind getCurrentFileKind() const {
diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h
index b2be33032c08..51dc3a0dc7f1 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -188,7 +188,7 @@ class FrontendInputFile {
/// The input, if it comes from a buffer rather than a file. This object
/// does not own the buffer, and the caller is responsible for ensuring
/// that it outlives any users.
- const llvm::MemoryBuffer *Buffer = nullptr;
+ llvm::Optional<llvm::MemoryBufferRef> Buffer;
/// The kind of input, e.g., C source, AST file, LLVM IR.
InputKind Kind;
@@ -200,16 +200,16 @@ class FrontendInputFile {
FrontendInputFile() = default;
FrontendInputFile(StringRef File, InputKind Kind, bool IsSystem = false)
: File(File.str()), Kind(Kind), IsSystem(IsSystem) {}
- FrontendInputFile(const llvm::MemoryBuffer *Buffer, InputKind Kind,
+ FrontendInputFile(llvm::MemoryBufferRef Buffer, InputKind Kind,
bool IsSystem = false)
: Buffer(Buffer), Kind(Kind), IsSystem(IsSystem) {}
InputKind getKind() const { return Kind; }
bool isSystem() const { return IsSystem; }
- bool isEmpty() const { return File.empty() && Buffer == nullptr; }
+ bool isEmpty() const { return File.empty() && Buffer == None; }
bool isFile() const { return !isBuffer(); }
- bool isBuffer() const { return Buffer != nullptr; }
+ bool isBuffer() const { return Buffer != None; }
bool isPreprocessed() const { return Kind.isPreprocessed(); }
StringRef getFile() const {
@@ -217,9 +217,9 @@ class FrontendInputFile {
return File;
}
- const llvm::MemoryBuffer *getBuffer() const {
+ llvm::MemoryBufferRef getBuffer() const {
assert(isBuffer());
- return Buffer;
+ return *Buffer;
}
};
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 61e186e6aa48..9902709c4e63 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -443,14 +443,13 @@ SourceManager::getOrCreateContentCache(const FileEntry *FileEnt,
/// Create a new ContentCache for the specified memory buffer.
/// This does no caching.
-const ContentCache *
-SourceManager::createMemBufferContentCache(const llvm::MemoryBuffer *Buffer,
- bool DoNotFree) {
+const ContentCache *SourceManager::createMemBufferContentCache(
+ std::unique_ptr<llvm::MemoryBuffer> Buffer) {
// Add a new ContentCache to the MemBufferInfos list and return it.
ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>();
new (Entry) ContentCache();
MemBufferInfos.push_back(Entry);
- Entry->replaceBuffer(Buffer, DoNotFree);
+ Entry->replaceBuffer(Buffer.release(), /*DoNotFree=*/false);
return Entry;
}
@@ -585,22 +584,20 @@ FileID SourceManager::createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer,
int LoadedID, unsigned LoadedOffset,
SourceLocation IncludeLoc) {
StringRef Name = Buffer->getBufferIdentifier();
- return createFileID(
- createMemBufferContentCache(Buffer.release(), /*DoNotFree*/ false),
- Name, IncludeLoc, FileCharacter, LoadedID, LoadedOffset);
+ return createFileID(createMemBufferContentCache(std::move(Buffer)), Name,
+ IncludeLoc, FileCharacter, LoadedID, LoadedOffset);
}
/// Create a new FileID that represents the specified memory buffer.
///
/// This does not take ownership of the MemoryBuffer. The memory buffer must
/// outlive the SourceManager.
-FileID SourceManager::createFileID(UnownedTag, const llvm::MemoryBuffer *Buffer,
+FileID SourceManager::createFileID(const llvm::MemoryBufferRef &Buffer,
SrcMgr::CharacteristicKind FileCharacter,
int LoadedID, unsigned LoadedOffset,
SourceLocation IncludeLoc) {
- return createFileID(createMemBufferContentCache(Buffer, /*DoNotFree*/ true),
- Buffer->getBufferIdentifier(), IncludeLoc,
- FileCharacter, LoadedID, LoadedOffset);
+ return createFileID(llvm::MemoryBuffer::getMemBuffer(Buffer), FileCharacter,
+ LoadedID, LoadedOffset, IncludeLoc);
}
/// Get the FileID for \p SourceFile if it exists. Otherwise, create a
diff --git a/clang/lib/Format/MacroExpander.cpp b/clang/lib/Format/MacroExpander.cpp
index c00fc209deb5..e50c80446963 100644
--- a/clang/lib/Format/MacroExpander.cpp
+++ b/clang/lib/Format/MacroExpander.cpp
@@ -136,8 +136,7 @@ MacroExpander::~MacroExpander() = default;
void MacroExpander::parseDefinition(const std::string &Macro) {
Buffers.push_back(
llvm::MemoryBuffer::getMemBufferCopy(Macro, "<scratch space>"));
- clang::FileID FID =
- SourceMgr.createFileID(SourceManager::Unowned, Buffers.back().get());
+ clang::FileID FID = SourceMgr.createFileID(Buffers.back()->getMemBufferRef());
FormatTokenLexer Lex(SourceMgr, FID, 0, Style, encoding::Encoding_UTF8,
Allocator, IdentTable);
const auto Tokens = Lex.lex();
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp
index 57d025b7c32e..7fca1900853b 100644
--- a/clang/lib/Frontend/ASTUnit.cpp
+++ b/clang/lib/Frontend/ASTUnit.cpp
@@ -1468,7 +1468,7 @@ StringRef ASTUnit::getMainFileName() const {
if (Input.isFile())
return Input.getFile();
else
- return Input.getBuffer()->getBufferIdentifier();
+ return Input.getBuffer().getBufferIdentifier();
}
if (SourceMgr) {
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index 4613ed8d7f61..3aa456a8726e 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -831,8 +831,7 @@ bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input,
: Input.isSystem() ? SrcMgr::C_System : SrcMgr::C_User;
if (Input.isBuffer()) {
- SourceMgr.setMainFileID(SourceMgr.createFileID(SourceManager::Unowned,
- Input.getBuffer(), Kind));
+ SourceMgr.setMainFileID(SourceMgr.createFileID(Input.getBuffer(), Kind));
assert(SourceMgr.getMainFileID().isValid() &&
"Couldn't establish MainFileID!");
return true;
diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp
index 384c504dadfd..231787326243 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -624,7 +624,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
if (auto *File = OldSM.getFileEntryForID(ID))
Input = FrontendInputFile(File->getName(), Kind);
else
- Input = FrontendInputFile(OldSM.getBuffer(ID), Kind);
+ Input = FrontendInputFile(OldSM.getBufferOrFake(ID), Kind);
}
setCurrentInput(Input, std::move(AST));
}
diff --git a/clang/lib/Frontend/FrontendActions.cpp b/clang/lib/Frontend/FrontendActions.cpp
index c651a1a940f8..0993e5eb033f 100644
--- a/clang/lib/Frontend/FrontendActions.cpp
+++ b/clang/lib/Frontend/FrontendActions.cpp
@@ -261,7 +261,7 @@ bool GenerateHeaderModuleAction::PrepareToExecuteAction(
if (FIF.getKind().getFormat() != InputKind::Source || !FIF.isFile()) {
CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
<< (FIF.isFile() ? FIF.getFile()
- : FIF.getBuffer()->getBufferIdentifier());
+ : FIF.getBuffer().getBufferIdentifier());
return true;
}
@@ -275,7 +275,8 @@ bool GenerateHeaderModuleAction::PrepareToExecuteAction(
// Set that buffer up as our "real" input.
Inputs.clear();
- Inputs.push_back(FrontendInputFile(Buffer.get(), Kind, /*IsSystem*/false));
+ Inputs.push_back(
+ FrontendInputFile(Buffer->getMemBufferRef(), Kind, /*IsSystem*/ false));
return GenerateModuleAction::PrepareToExecuteAction(CI);
}
diff --git a/clang/unittests/Format/TestLexer.h b/clang/unittests/Format/TestLexer.h
index 2b56f10dd379..1176cf258c3f 100644
--- a/clang/unittests/Format/TestLexer.h
+++ b/clang/unittests/Format/TestLexer.h
@@ -62,8 +62,8 @@ class TestLexer {
TokenList lex(llvm::StringRef Code) {
Buffers.push_back(
llvm::MemoryBuffer::getMemBufferCopy(Code, "<scratch space>"));
- clang::FileID FID = SourceMgr.get().createFileID(SourceManager::Unowned,
- Buffers.back().get());
+ clang::FileID FID =
+ SourceMgr.get().createFileID(Buffers.back()->getMemBufferRef());
FormatTokenLexer Lex(SourceMgr.get(), FID, 0, Style, Encoding, Allocator,
IdentTable);
auto Result = Lex.lex();
More information about the cfe-commits
mailing list