From 51d1d585e5838ea0f02f1271f7543c4e43639969 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Duncan=20P=2E=20N=2E=20Exon=C2=A0Smith?= Date: Wed, 14 Oct 2020 17:17:34 -0400 Subject: [PATCH] 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`. 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 --- clang/include/clang/Basic/SourceManager.h | 36 ++------------------------ clang/include/clang/Frontend/FrontendAction.h | 2 +- clang/include/clang/Frontend/FrontendOptions.h | 12 ++++----- clang/lib/Basic/SourceManager.cpp | 19 ++++++-------- clang/lib/Format/MacroExpander.cpp | 3 +-- clang/lib/Frontend/ASTUnit.cpp | 2 +- clang/lib/Frontend/CompilerInstance.cpp | 3 +-- clang/lib/Frontend/FrontendAction.cpp | 2 +- clang/lib/Frontend/FrontendActions.cpp | 5 ++-- clang/unittests/Format/TestLexer.h | 4 +-- 10 files changed, 26 insertions(+), 62 deletions(-) diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h index 2156a01..e1b510b 100644 --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -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 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 c9f9f08..319b3bc 100644 --- a/clang/include/clang/Frontend/FrontendAction.h +++ b/clang/include/clang/Frontend/FrontendAction.h @@ -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 { diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h index b2be330..51dc3a0 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 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; } }; diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 61e186e..9902709 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 Buffer) { // Add a new ContentCache to the MemBufferInfos list and return it. ContentCache *Entry = ContentCacheAlloc.Allocate(); 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 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 c00fc20..e50c804 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, "")); - 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 57d025b..7fca190 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 4613ed8..3aa456a 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 384c504..2317873 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 c651a1a..0993e5e 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 2b56f10..1176cf2 100644 --- a/clang/unittests/Format/TestLexer.h +++ b/clang/unittests/Format/TestLexer.h @@ -62,8 +62,8 @@ public: TokenList lex(llvm::StringRef Code) { Buffers.push_back( llvm::MemoryBuffer::getMemBufferCopy(Code, "")); - 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(); -- 2.7.4