diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -860,13 +860,11 @@ 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 @@ 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 @@ /// 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 --- a/clang/include/clang/Frontend/FrontendAction.h +++ b/clang/include/clang/Frontend/FrontendAction.h @@ -145,7 +145,7 @@ 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 --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -188,7 +188,7 @@ /// 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 @@ 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 @@ 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 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -443,14 +443,13 @@ /// 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 @@ 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 --- a/clang/lib/Format/MacroExpander.cpp +++ b/clang/lib/Format/MacroExpander.cpp @@ -136,8 +136,7 @@ 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 --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -1468,7 +1468,7 @@ 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 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -831,8 +831,7 @@ : 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 --- a/clang/lib/Frontend/FrontendAction.cpp +++ b/clang/lib/Frontend/FrontendAction.cpp @@ -624,7 +624,7 @@ 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 --- a/clang/lib/Frontend/FrontendActions.cpp +++ b/clang/lib/Frontend/FrontendActions.cpp @@ -261,7 +261,7 @@ 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 @@ // 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 --- a/clang/unittests/Format/TestLexer.h +++ b/clang/unittests/Format/TestLexer.h @@ -62,8 +62,8 @@ 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();