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
This commit is contained in:
Duncan P. N. Exon Smith 2020-10-14 17:17:34 -04:00
parent 59286b36df
commit 51d1d585e5
10 changed files with 26 additions and 62 deletions

View File

@ -860,13 +860,11 @@ public:
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 @@ public:
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 @@ private:
/// 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;

View File

@ -145,7 +145,7 @@ public:
assert(!CurrentInput.isEmpty() && "No current file!");
return CurrentInput.isFile()
? CurrentInput.getFile()
: CurrentInput.getBuffer()->getBufferIdentifier();
: CurrentInput.getBuffer().getBufferIdentifier();
}
InputKind getCurrentFileKind() const {

View File

@ -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 @@ public:
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 @@ public:
return File;
}
const llvm::MemoryBuffer *getBuffer() const {
llvm::MemoryBufferRef getBuffer() const {
assert(isBuffer());
return Buffer;
return *Buffer;
}
};

View File

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

View File

@ -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();

View File

@ -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) {

View File

@ -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;

View File

@ -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));
}

View File

@ -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);
}

View File

@ -62,8 +62,8 @@ public:
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();