From a885796d5fd85e4c7c71407feb48553ef7d4e258 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Sun, 26 Oct 2014 22:44:13 +0000 Subject: [PATCH] Make VFS and FileManager match the current MemoryBuffer API. This eliminates converting back and forth between the 3 formats and gives us a more homogeneous interface. llvm-svn: 220657 --- clang/include/clang/Basic/FileManager.h | 10 +-- clang/include/clang/Basic/VirtualFileSystem.h | 24 +++---- clang/lib/Basic/FileManager.cpp | 52 +++++--------- clang/lib/Basic/FileSystemStatCache.cpp | 9 ++- clang/lib/Basic/SourceManager.cpp | 21 +++--- clang/lib/Basic/VirtualFileSystem.cpp | 83 ++++++++++------------- clang/lib/CodeGen/CodeGenAction.cpp | 9 +-- clang/lib/Frontend/ASTUnit.cpp | 7 +- clang/lib/Frontend/CompilerInstance.cpp | 12 ++-- clang/lib/Frontend/FrontendActions.cpp | 8 +-- clang/lib/Frontend/SerializedDiagnosticReader.cpp | 6 +- clang/lib/Lex/HeaderMap.cpp | 7 +- clang/lib/Serialization/ASTReader.cpp | 19 +++--- clang/lib/Serialization/GlobalModuleIndex.cpp | 8 +-- clang/lib/Serialization/ModuleManager.cpp | 25 ++++--- clang/unittests/Basic/VirtualFileSystemTest.cpp | 10 +-- clang/unittests/Tooling/RefactoringTest.cpp | 5 +- clang/unittests/Tooling/RewriterTestContext.h | 5 +- 18 files changed, 134 insertions(+), 186 deletions(-) diff --git a/clang/include/clang/Basic/FileManager.h b/clang/include/clang/Basic/FileManager.h index 5b2b022..132bf37 100644 --- a/clang/include/clang/Basic/FileManager.h +++ b/clang/include/clang/Basic/FileManager.h @@ -241,11 +241,11 @@ public: /// \brief Open the specified file as a MemoryBuffer, returning a new /// MemoryBuffer if successful, otherwise returning null. - std::unique_ptr - getBufferForFile(const FileEntry *Entry, std::string *ErrorStr = nullptr, - bool isVolatile = false, bool ShouldCloseOpenFile = true); - std::unique_ptr - getBufferForFile(StringRef Filename, std::string *ErrorStr = nullptr); + llvm::ErrorOr> + getBufferForFile(const FileEntry *Entry, bool isVolatile = false, + bool ShouldCloseOpenFile = true); + llvm::ErrorOr> + getBufferForFile(StringRef Filename); /// \brief Get the 'stat' information for the given \p Path. /// diff --git a/clang/include/clang/Basic/VirtualFileSystem.h b/clang/include/clang/Basic/VirtualFileSystem.h index 86f8dd6..7086596 100644 --- a/clang/include/clang/Basic/VirtualFileSystem.h +++ b/clang/include/clang/Basic/VirtualFileSystem.h @@ -89,11 +89,9 @@ public: /// \brief Get the status of the file. virtual llvm::ErrorOr status() = 0; /// \brief Get the contents of the file as a \p MemoryBuffer. - virtual std::error_code getBuffer(const Twine &Name, - std::unique_ptr &Result, - int64_t FileSize = -1, - bool RequiresNullTerminator = true, - bool IsVolatile = false) = 0; + virtual llvm::ErrorOr> + getBuffer(const Twine &Name, int64_t FileSize = -1, + bool RequiresNullTerminator = true, bool IsVolatile = false) = 0; /// \brief Closes the file. virtual std::error_code close() = 0; /// \brief Sets the name to use for this file. @@ -188,16 +186,14 @@ public: /// \brief Get the status of the entry at \p Path, if one exists. virtual llvm::ErrorOr status(const Twine &Path) = 0; /// \brief Get a \p File object for the file at \p Path, if one exists. - virtual std::error_code openFileForRead(const Twine &Path, - std::unique_ptr &Result) = 0; + virtual llvm::ErrorOr> + openFileForRead(const Twine &Path) = 0; /// This is a convenience method that opens a file, gets its content and then /// closes the file. - std::error_code getBufferForFile(const Twine &Name, - std::unique_ptr &Result, - int64_t FileSize = -1, - bool RequiresNullTerminator = true, - bool IsVolatile = false); + llvm::ErrorOr> + getBufferForFile(const Twine &Name, int64_t FileSize = -1, + bool RequiresNullTerminator = true, bool IsVolatile = false); /// \brief Get a directory_iterator for \p Dir. /// \note The 'end' iterator is directory_iterator(). @@ -231,8 +227,8 @@ public: void pushOverlay(IntrusiveRefCntPtr FS); llvm::ErrorOr status(const Twine &Path) override; - std::error_code openFileForRead(const Twine &Path, - std::unique_ptr &Result) override; + llvm::ErrorOr> + openFileForRead(const Twine &Path) override; directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; typedef FileSystemList::reverse_iterator iterator; diff --git a/clang/lib/Basic/FileManager.cpp b/clang/lib/Basic/FileManager.cpp index 8784486..91681ed 100644 --- a/clang/lib/Basic/FileManager.cpp +++ b/clang/lib/Basic/FileManager.cpp @@ -399,12 +399,9 @@ void FileManager::FixupRelativePath(SmallVectorImpl &path) const { path = NewPath; } -std::unique_ptr -FileManager::getBufferForFile(const FileEntry *Entry, std::string *ErrorStr, - bool isVolatile, bool ShouldCloseOpenFile) { - std::unique_ptr Result; - std::error_code ec; - +llvm::ErrorOr> +FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile, + bool ShouldCloseOpenFile) { uint64_t FileSize = Entry->getSize(); // If there's a high enough chance that the file have changed since we // got its size, force a stat before opening it. @@ -414,10 +411,9 @@ FileManager::getBufferForFile(const FileEntry *Entry, std::string *ErrorStr, const char *Filename = Entry->getName(); // If the file is already open, use the open file descriptor. if (Entry->File) { - ec = Entry->File->getBuffer(Filename, Result, FileSize, - /*RequiresNullTerminator=*/true, isVolatile); - if (ErrorStr) - *ErrorStr = ec.message(); + auto Result = + Entry->File->getBuffer(Filename, FileSize, + /*RequiresNullTerminator=*/true, isVolatile); // FIXME: we need a set of APIs that can make guarantees about whether a // FileEntry is open or not. if (ShouldCloseOpenFile) @@ -427,40 +423,24 @@ FileManager::getBufferForFile(const FileEntry *Entry, std::string *ErrorStr, // Otherwise, open the file. - if (FileSystemOpts.WorkingDir.empty()) { - ec = FS->getBufferForFile(Filename, Result, FileSize, - /*RequiresNullTerminator=*/true, isVolatile); - if (ec && ErrorStr) - *ErrorStr = ec.message(); - return Result; - } + if (FileSystemOpts.WorkingDir.empty()) + return FS->getBufferForFile(Filename, FileSize, + /*RequiresNullTerminator=*/true, isVolatile); SmallString<128> FilePath(Entry->getName()); FixupRelativePath(FilePath); - ec = FS->getBufferForFile(FilePath.str(), Result, FileSize, - /*RequiresNullTerminator=*/true, isVolatile); - if (ec && ErrorStr) - *ErrorStr = ec.message(); - return Result; + return FS->getBufferForFile(FilePath.str(), FileSize, + /*RequiresNullTerminator=*/true, isVolatile); } -std::unique_ptr -FileManager::getBufferForFile(StringRef Filename, std::string *ErrorStr) { - std::unique_ptr Result; - std::error_code ec; - if (FileSystemOpts.WorkingDir.empty()) { - ec = FS->getBufferForFile(Filename, Result); - if (ec && ErrorStr) - *ErrorStr = ec.message(); - return Result; - } +llvm::ErrorOr> +FileManager::getBufferForFile(StringRef Filename) { + if (FileSystemOpts.WorkingDir.empty()) + return FS->getBufferForFile(Filename); SmallString<128> FilePath(Filename); FixupRelativePath(FilePath); - ec = FS->getBufferForFile(FilePath.c_str(), Result); - if (ec && ErrorStr) - *ErrorStr = ec.message(); - return Result; + return FS->getBufferForFile(FilePath.c_str()); } /// getStatValue - Get the 'stat' information for the specified path, diff --git a/clang/lib/Basic/FileSystemStatCache.cpp b/clang/lib/Basic/FileSystemStatCache.cpp index 7515cfb..83e42bd 100644 --- a/clang/lib/Basic/FileSystemStatCache.cpp +++ b/clang/lib/Basic/FileSystemStatCache.cpp @@ -78,21 +78,20 @@ bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile, // // Because of this, check to see if the file exists with 'open'. If the // open succeeds, use fstat to get the stat info. - std::unique_ptr OwnedFile; - std::error_code EC = FS.openFileForRead(Path, OwnedFile); + auto OwnedFile = FS.openFileForRead(Path); - if (EC) { + if (!OwnedFile) { // If the open fails, our "stat" fails. R = CacheMissing; } else { // Otherwise, the open succeeded. Do an fstat to get the information // about the file. We'll end up returning the open file descriptor to the // client to do what they please with it. - llvm::ErrorOr Status = OwnedFile->status(); + llvm::ErrorOr Status = (*OwnedFile)->status(); if (Status) { R = CacheExists; copyStatusToFileData(*Status, Data); - *F = std::move(OwnedFile); + *F = std::move(*OwnedFile); } else { // fstat rarely fails. If it does, claim the initial open didn't // succeed. diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 6450406..6991783 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -94,11 +94,9 @@ llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag, return Buffer.getPointer(); } - std::string ErrorStr; bool isVolatile = SM.userFilesAreVolatile() && !IsSystemFile; - Buffer.setPointer(SM.getFileManager() - .getBufferForFile(ContentsEntry, &ErrorStr, isVolatile) - .release()); + auto BufferOrError = + SM.getFileManager().getBufferForFile(ContentsEntry, isVolatile); // If we were unable to open the file, then we are in an inconsistent // situation where the content cache referenced a file which no longer @@ -110,7 +108,7 @@ llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag, // currently handle returning a null entry here. Ideally we should detect // that we are in an inconsistent situation and error out as quickly as // possible. - if (!Buffer.getPointer()) { + if (!BufferOrError) { StringRef FillStr("<<>>\n"); Buffer.setPointer(MemoryBuffer::getNewMemBuffer(ContentsEntry->getSize(), "").release()); @@ -119,18 +117,21 @@ llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag, Ptr[i] = FillStr[i % FillStr.size()]; if (Diag.isDiagnosticInFlight()) - Diag.SetDelayedDiagnostic(diag::err_cannot_open_file, - ContentsEntry->getName(), ErrorStr); - else + Diag.SetDelayedDiagnostic(diag::err_cannot_open_file, + ContentsEntry->getName(), + BufferOrError.getError().message()); + else Diag.Report(Loc, diag::err_cannot_open_file) - << ContentsEntry->getName() << ErrorStr; + << ContentsEntry->getName() << BufferOrError.getError().message(); Buffer.setInt(Buffer.getInt() | InvalidFlag); if (Invalid) *Invalid = true; return Buffer.getPointer(); } - + + Buffer.setPointer(BufferOrError->release()); + // Check that the file's size is the same as in the file entry (which may // have come from a stat cache). if (getRawBuffer()->getBufferSize() != (size_t)ContentsEntry->getSize()) { diff --git a/clang/lib/Basic/VirtualFileSystem.cpp b/clang/lib/Basic/VirtualFileSystem.cpp index df446bb..8dc7006 100644 --- a/clang/lib/Basic/VirtualFileSystem.cpp +++ b/clang/lib/Basic/VirtualFileSystem.cpp @@ -67,16 +67,14 @@ File::~File() {} FileSystem::~FileSystem() {} -std::error_code FileSystem::getBufferForFile( - const llvm::Twine &Name, std::unique_ptr &Result, - int64_t FileSize, bool RequiresNullTerminator, bool IsVolatile) { - std::unique_ptr F; - if (std::error_code EC = openFileForRead(Name, F)) - return EC; - - std::error_code EC = - F->getBuffer(Name, Result, FileSize, RequiresNullTerminator, IsVolatile); - return EC; +ErrorOr> +FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize, + bool RequiresNullTerminator, bool IsVolatile) { + auto F = openFileForRead(Name); + if (!F) + return F.getError(); + + return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile); } //===-----------------------------------------------------------------------===/ @@ -96,11 +94,10 @@ class RealFile : public File { public: ~RealFile(); ErrorOr status() override; - std::error_code getBuffer(const Twine &Name, - std::unique_ptr &Result, - int64_t FileSize = -1, - bool RequiresNullTerminator = true, - bool IsVolatile = false) override; + ErrorOr> + getBuffer(const Twine &Name, int64_t FileSize = -1, + bool RequiresNullTerminator = true, + bool IsVolatile = false) override; std::error_code close() override; void setName(StringRef Name) override; }; @@ -120,19 +117,12 @@ ErrorOr RealFile::status() { return S; } -std::error_code RealFile::getBuffer(const Twine &Name, - std::unique_ptr &Result, - int64_t FileSize, - bool RequiresNullTerminator, - bool IsVolatile) { +ErrorOr> +RealFile::getBuffer(const Twine &Name, int64_t FileSize, + bool RequiresNullTerminator, bool IsVolatile) { assert(FD != -1 && "cannot get buffer for closed file"); - ErrorOr> BufferOrErr = - MemoryBuffer::getOpenFile(FD, Name.str().c_str(), FileSize, - RequiresNullTerminator, IsVolatile); - if (std::error_code EC = BufferOrErr.getError()) - return EC; - Result = std::move(BufferOrErr.get()); - return std::error_code(); + return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator, + IsVolatile); } // FIXME: This is terrible, we need this for ::close. @@ -161,8 +151,7 @@ namespace { class RealFileSystem : public FileSystem { public: ErrorOr status(const Twine &Path) override; - std::error_code openFileForRead(const Twine &Path, - std::unique_ptr &Result) override; + ErrorOr> openFileForRead(const Twine &Path) override; directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; }; } // end anonymous namespace @@ -176,14 +165,14 @@ ErrorOr RealFileSystem::status(const Twine &Path) { return Result; } -std::error_code RealFileSystem::openFileForRead(const Twine &Name, - std::unique_ptr &Result) { +ErrorOr> +RealFileSystem::openFileForRead(const Twine &Name) { int FD; if (std::error_code EC = sys::fs::openFileForRead(Name, FD)) return EC; - Result.reset(new RealFile(FD)); + std::unique_ptr Result(new RealFile(FD)); Result->setName(Name.str()); - return std::error_code(); + return std::move(Result); } IntrusiveRefCntPtr vfs::getRealFileSystem() { @@ -252,14 +241,13 @@ ErrorOr OverlayFileSystem::status(const Twine &Path) { return make_error_code(llvm::errc::no_such_file_or_directory); } -std::error_code -OverlayFileSystem::openFileForRead(const llvm::Twine &Path, - std::unique_ptr &Result) { +ErrorOr> +OverlayFileSystem::openFileForRead(const llvm::Twine &Path) { // FIXME: handle symlinks that cross file systems for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) { - std::error_code EC = (*I)->openFileForRead(Path, Result); - if (!EC || EC != llvm::errc::no_such_file_or_directory) - return EC; + auto Result = (*I)->openFileForRead(Path); + if (Result || Result.getError() != llvm::errc::no_such_file_or_directory) + return Result; } return make_error_code(llvm::errc::no_such_file_or_directory); } @@ -520,8 +508,7 @@ public: IntrusiveRefCntPtr ExternalFS); ErrorOr status(const Twine &Path) override; - std::error_code openFileForRead(const Twine &Path, - std::unique_ptr &Result) override; + ErrorOr> openFileForRead(const Twine &Path) override; directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override{ ErrorOr E = lookupPath(Dir); @@ -969,9 +956,7 @@ ErrorOr VFSFromYAML::status(const Twine &Path) { return status(Path, *Result); } -std::error_code -VFSFromYAML::openFileForRead(const Twine &Path, - std::unique_ptr &Result) { +ErrorOr> VFSFromYAML::openFileForRead(const Twine &Path) { ErrorOr E = lookupPath(Path); if (!E) return E.getError(); @@ -980,14 +965,14 @@ VFSFromYAML::openFileForRead(const Twine &Path, if (!F) // FIXME: errc::not_a_file? return make_error_code(llvm::errc::invalid_argument); - if (std::error_code EC = - ExternalFS->openFileForRead(F->getExternalContentsPath(), Result)) - return EC; + auto Result = ExternalFS->openFileForRead(F->getExternalContentsPath()); + if (!Result) + return Result; if (!F->useExternalName(UseExternalNames)) - Result->setName(Path.str()); + (*Result)->setName(Path.str()); - return std::error_code(); + return Result; } IntrusiveRefCntPtr diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp index 146c550..044eec6 100644 --- a/clang/lib/CodeGen/CodeGenAction.cpp +++ b/clang/lib/CodeGen/CodeGenAction.cpp @@ -647,18 +647,15 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { // loaded from bitcode, do so now. const std::string &LinkBCFile = CI.getCodeGenOpts().LinkBitcodeFile; if (!LinkModuleToUse && !LinkBCFile.empty()) { - std::string ErrorStr; - - std::unique_ptr BCBuf = - CI.getFileManager().getBufferForFile(LinkBCFile, &ErrorStr); + auto BCBuf = CI.getFileManager().getBufferForFile(LinkBCFile); if (!BCBuf) { CI.getDiagnostics().Report(diag::err_cannot_open_file) - << LinkBCFile << ErrorStr; + << LinkBCFile << BCBuf.getError().message(); return nullptr; } ErrorOr ModuleOrErr = - getLazyBitcodeModule(std::move(BCBuf), *VMContext); + getLazyBitcodeModule(std::move(*BCBuf), *VMContext); if (std::error_code EC = ModuleOrErr.getError()) { CI.getDiagnostics().Report(diag::err_cannot_open_file) << LinkBCFile << EC.message(); diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp index 19fafa3..1d8c6a1 100644 --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -638,7 +638,12 @@ ASTDeserializationListener *ASTUnit::getDeserializationListener() { std::unique_ptr ASTUnit::getBufferForFile(StringRef Filename, std::string *ErrorStr) { assert(FileMgr); - return FileMgr->getBufferForFile(Filename, ErrorStr); + auto Buffer = FileMgr->getBufferForFile(Filename); + if (Buffer) + return std::move(*Buffer); + if (ErrorStr) + *ErrorStr = Buffer.getError().message(); + return nullptr; } /// \brief Configure the diagnostics object for use with ASTUnit. diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index 1f336b4..03ab20b 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -717,14 +717,14 @@ bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input, // pick up the correct size, and simply override their contents as we do for // STDIN. if (File->isNamedPipe()) { - std::string ErrorStr; - if (std::unique_ptr MB = - FileMgr.getBufferForFile(File, &ErrorStr, /*isVolatile=*/true)) { + auto MB = FileMgr.getBufferForFile(File, /*isVolatile=*/true); + if (MB) { // Create a new virtual file that will have the correct size. - File = FileMgr.getVirtualFile(InputFile, MB->getBufferSize(), 0); - SourceMgr.overrideFileContents(File, std::move(MB)); + File = FileMgr.getVirtualFile(InputFile, (*MB)->getBufferSize(), 0); + SourceMgr.overrideFileContents(File, std::move(*MB)); } else { - Diags.Report(diag::err_cannot_open_file) << InputFile << ErrorStr; + Diags.Report(diag::err_cannot_open_file) << InputFile + << MB.getError().message(); return false; } } diff --git a/clang/lib/Frontend/FrontendActions.cpp b/clang/lib/Frontend/FrontendActions.cpp index 99cd4a2..6634e19 100644 --- a/clang/lib/Frontend/FrontendActions.cpp +++ b/clang/lib/Frontend/FrontendActions.cpp @@ -683,10 +683,10 @@ void PrintPreambleAction::ExecuteAction() { } CompilerInstance &CI = getCompilerInstance(); - if (std::unique_ptr Buffer = - CI.getFileManager().getBufferForFile(getCurrentFile())) { + auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile()); + if (Buffer) { unsigned Preamble = - Lexer::ComputePreamble(Buffer->getBuffer(), CI.getLangOpts()).first; - llvm::outs().write(Buffer->getBufferStart(), Preamble); + Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).first; + llvm::outs().write((*Buffer)->getBufferStart(), Preamble); } } diff --git a/clang/lib/Frontend/SerializedDiagnosticReader.cpp b/clang/lib/Frontend/SerializedDiagnosticReader.cpp index 6d692e4..197132d 100644 --- a/clang/lib/Frontend/SerializedDiagnosticReader.cpp +++ b/clang/lib/Frontend/SerializedDiagnosticReader.cpp @@ -21,13 +21,13 @@ std::error_code SerializedDiagnosticReader::readDiagnostics(StringRef File) { FileSystemOptions FO; FileManager FileMgr(FO); - std::unique_ptr Buffer = FileMgr.getBufferForFile(File); + auto Buffer = FileMgr.getBufferForFile(File); if (!Buffer) return SDError::CouldNotLoad; llvm::BitstreamReader StreamFile; - StreamFile.init((const unsigned char *)Buffer->getBufferStart(), - (const unsigned char *)Buffer->getBufferEnd()); + StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(), + (const unsigned char *)(*Buffer)->getBufferEnd()); llvm::BitstreamCursor Stream; Stream.init(StreamFile); diff --git a/clang/lib/Lex/HeaderMap.cpp b/clang/lib/Lex/HeaderMap.cpp index c86569f..09d5384 100644 --- a/clang/lib/Lex/HeaderMap.cpp +++ b/clang/lib/Lex/HeaderMap.cpp @@ -81,10 +81,9 @@ const HeaderMap *HeaderMap::Create(const FileEntry *FE, FileManager &FM) { unsigned FileSize = FE->getSize(); if (FileSize <= sizeof(HMapHeader)) return nullptr; - std::unique_ptr FileBuffer = - FM.getBufferForFile(FE); + auto FileBuffer = FM.getBufferForFile(FE); if (!FileBuffer) return nullptr; // Unreadable file? - const char *FileStart = FileBuffer->getBufferStart(); + const char *FileStart = (*FileBuffer)->getBufferStart(); // We know the file is at least as big as the header, check it now. const HMapHeader *Header = reinterpret_cast(FileStart); @@ -104,7 +103,7 @@ const HeaderMap *HeaderMap::Create(const FileEntry *FE, FileManager &FM) { if (Header->Reserved != 0) return nullptr; // Okay, everything looks good, create the header map. - return new HeaderMap(std::move(FileBuffer), NeedsByteSwap); + return new HeaderMap(std::move(*FileBuffer), NeedsByteSwap); } //===----------------------------------------------------------------------===// diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index 1d7efb8..29fa263 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -4074,19 +4074,18 @@ std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName, FileManager &FileMgr, DiagnosticsEngine &Diags) { // Open the AST file. - std::string ErrStr; - std::unique_ptr Buffer = - FileMgr.getBufferForFile(ASTFileName, &ErrStr); + auto Buffer = FileMgr.getBufferForFile(ASTFileName); if (!Buffer) { - Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr; + Diags.Report(diag::err_fe_unable_to_read_pch_file) + << ASTFileName << Buffer.getError().message(); return std::string(); } // Initialize the stream llvm::BitstreamReader StreamFile; BitstreamCursor Stream; - StreamFile.init((const unsigned char *)Buffer->getBufferStart(), - (const unsigned char *)Buffer->getBufferEnd()); + StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(), + (const unsigned char *)(*Buffer)->getBufferEnd()); Stream.init(StreamFile); // Sniff for the signature. @@ -4163,9 +4162,7 @@ bool ASTReader::readASTFileControlBlock(StringRef Filename, FileManager &FileMgr, ASTReaderListener &Listener) { // Open the AST file. - std::string ErrStr; - std::unique_ptr Buffer = - FileMgr.getBufferForFile(Filename, &ErrStr); + auto Buffer = FileMgr.getBufferForFile(Filename); if (!Buffer) { return true; } @@ -4173,8 +4170,8 @@ bool ASTReader::readASTFileControlBlock(StringRef Filename, // Initialize the stream llvm::BitstreamReader StreamFile; BitstreamCursor Stream; - StreamFile.init((const unsigned char *)Buffer->getBufferStart(), - (const unsigned char *)Buffer->getBufferEnd()); + StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(), + (const unsigned char *)(*Buffer)->getBufferEnd()); Stream.init(StreamFile); // Sniff for the signature. diff --git a/clang/lib/Serialization/GlobalModuleIndex.cpp b/clang/lib/Serialization/GlobalModuleIndex.cpp index 121478c..a67f9f3 100644 --- a/clang/lib/Serialization/GlobalModuleIndex.cpp +++ b/clang/lib/Serialization/GlobalModuleIndex.cpp @@ -494,9 +494,7 @@ namespace { bool GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) { // Open the module file. - std::string ErrorStr; - std::unique_ptr Buffer = - FileMgr.getBufferForFile(File, &ErrorStr, /*isVolatile=*/true); + auto Buffer = FileMgr.getBufferForFile(File, /*isVolatile=*/true); if (!Buffer) { return true; } @@ -504,8 +502,8 @@ bool GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) { // Initialize the input stream llvm::BitstreamReader InStreamFile; llvm::BitstreamCursor InStream; - InStreamFile.init((const unsigned char *)Buffer->getBufferStart(), - (const unsigned char *)Buffer->getBufferEnd()); + InStreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(), + (const unsigned char *)(*Buffer)->getBufferEnd()); InStream.init(InStreamFile); // Sniff for the signature. diff --git a/clang/lib/Serialization/ModuleManager.cpp b/clang/lib/Serialization/ModuleManager.cpp index c76a318..3baaa0a 100644 --- a/clang/lib/Serialization/ModuleManager.cpp +++ b/clang/lib/Serialization/ModuleManager.cpp @@ -107,27 +107,26 @@ ModuleManager::addModule(StringRef FileName, ModuleKind Type, New->Buffer = std::move(Buffer); } else { // Open the AST file. - std::error_code ec; + llvm::ErrorOr> Buf( + (std::error_code())); if (FileName == "-") { - llvm::ErrorOr> Buf = - llvm::MemoryBuffer::getSTDIN(); - ec = Buf.getError(); - if (ec) - ErrorStr = ec.message(); - else - New->Buffer = std::move(Buf.get()); + Buf = llvm::MemoryBuffer::getSTDIN(); } else { // Leave the FileEntry open so if it gets read again by another // ModuleManager it must be the same underlying file. // FIXME: Because FileManager::getFile() doesn't guarantee that it will // give us an open file, this may not be 100% reliable. - New->Buffer = FileMgr.getBufferForFile(New->File, &ErrorStr, - /*IsVolatile*/ false, - /*ShouldClose*/ false); + Buf = FileMgr.getBufferForFile(New->File, + /*IsVolatile=*/false, + /*ShouldClose=*/false); } - - if (!New->Buffer) + + if (!Buf) { + ErrorStr = Buf.getError().message(); return Missing; + } + + New->Buffer = std::move(*Buf); } // Initialize the stream diff --git a/clang/unittests/Basic/VirtualFileSystemTest.cpp b/clang/unittests/Basic/VirtualFileSystemTest.cpp index e6c8b27..67beb92 100644 --- a/clang/unittests/Basic/VirtualFileSystemTest.cpp +++ b/clang/unittests/Basic/VirtualFileSystemTest.cpp @@ -39,14 +39,8 @@ public: return make_error_code(llvm::errc::no_such_file_or_directory); return I->second; } - std::error_code openFileForRead(const Twine &Path, - std::unique_ptr &Result) override { - llvm_unreachable("unimplemented"); - } - std::error_code getBufferForFile(const Twine &Name, - std::unique_ptr &Result, - int64_t FileSize = -1, - bool RequiresNullTerminator = true) { + ErrorOr> + openFileForRead(const Twine &Path) override { llvm_unreachable("unimplemented"); } diff --git a/clang/unittests/Tooling/RefactoringTest.cpp b/clang/unittests/Tooling/RefactoringTest.cpp index 73c53aa..4935bdd 100644 --- a/clang/unittests/Tooling/RefactoringTest.cpp +++ b/clang/unittests/Tooling/RefactoringTest.cpp @@ -251,9 +251,8 @@ public: // descriptor, which might not see the changes made. // FIXME: Figure out whether there is a way to get the SourceManger to // reopen the file. - std::unique_ptr FileBuffer( - Context.Files.getBufferForFile(Path, nullptr)); - return FileBuffer->getBuffer(); + auto FileBuffer = Context.Files.getBufferForFile(Path); + return (*FileBuffer)->getBuffer(); } llvm::StringMap TemporaryFiles; diff --git a/clang/unittests/Tooling/RewriterTestContext.h b/clang/unittests/Tooling/RewriterTestContext.h index 82c0058..78ee788 100644 --- a/clang/unittests/Tooling/RewriterTestContext.h +++ b/clang/unittests/Tooling/RewriterTestContext.h @@ -101,9 +101,8 @@ class RewriterTestContext { // descriptor, which might not see the changes made. // FIXME: Figure out whether there is a way to get the SourceManger to // reopen the file. - std::unique_ptr FileBuffer( - Files.getBufferForFile(Path, nullptr)); - return FileBuffer->getBuffer(); + auto FileBuffer = Files.getBufferForFile(Path); + return (*FileBuffer)->getBuffer(); } IntrusiveRefCntPtr DiagOpts; -- 2.7.4