dexonsmith created this revision. dexonsmith added a reviewer: arphaman. Herald added a subscriber: ributzka. dexonsmith requested review of this revision.
Replace `SourceManager::getMemoryBufferForFile`, which returned a dereferenceable `MemoryBuffer*` and had a `bool*Invalid` out parameter, with `getMemoryBufferForFileOrNone` (returning `Optional<MemoryBufferRef>`) and `getMemoryBufferForFileOrFake` (returning `MemoryBufferRef`). https://reviews.llvm.org/D89429 Files: 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
Index: clang/lib/Lex/Preprocessor.cpp =================================================================== --- clang/lib/Lex/Preprocessor.cpp +++ clang/lib/Lex/Preprocessor.cpp @@ -395,12 +395,10 @@ 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. Index: clang/lib/Frontend/PrecompiledPreamble.cpp =================================================================== --- clang/lib/Frontend/PrecompiledPreamble.cpp +++ clang/lib/Frontend/PrecompiledPreamble.cpp @@ -463,7 +463,8 @@ 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 @@ 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 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; Index: clang/lib/Basic/SourceManager.cpp =================================================================== --- clang/lib/Basic/SourceManager.cpp +++ clang/lib/Basic/SourceManager.cpp @@ -701,14 +701,11 @@ 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, Index: clang/include/clang/Frontend/PrecompiledPreamble.h =================================================================== --- clang/include/clang/Frontend/PrecompiledPreamble.h +++ clang/include/clang/Frontend/PrecompiledPreamble.h @@ -26,6 +26,7 @@ namespace llvm { class MemoryBuffer; +class MemoryBufferRef; namespace vfs { class FileSystem; } @@ -216,7 +217,7 @@ 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) { Index: clang/include/clang/Basic/SourceManager.h =================================================================== --- clang/include/clang/Basic/SourceManager.h +++ clang/include/clang/Basic/SourceManager.h @@ -895,10 +895,18 @@ /// 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.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits