diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2014-10-26 22:44:13 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2014-10-26 22:44:13 +0000 |
commit | 0273eb0d7a5e729f299341d608b41a4a896735af (patch) | |
tree | a51c83113a4d2b3e46b6be2342d64b5c286fd8fd /lib/Basic | |
parent | a877a4c5d70706adf2ab0e3402fb875019553c75 (diff) |
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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@220657 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Basic')
-rw-r--r-- | lib/Basic/FileManager.cpp | 52 | ||||
-rw-r--r-- | lib/Basic/FileSystemStatCache.cpp | 9 | ||||
-rw-r--r-- | lib/Basic/SourceManager.cpp | 21 | ||||
-rw-r--r-- | lib/Basic/VirtualFileSystem.cpp | 83 |
4 files changed, 65 insertions, 100 deletions
diff --git a/lib/Basic/FileManager.cpp b/lib/Basic/FileManager.cpp index 87844864a8..91681edcc5 100644 --- a/lib/Basic/FileManager.cpp +++ b/lib/Basic/FileManager.cpp @@ -399,12 +399,9 @@ void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const { path = NewPath; } -std::unique_ptr<llvm::MemoryBuffer> -FileManager::getBufferForFile(const FileEntry *Entry, std::string *ErrorStr, - bool isVolatile, bool ShouldCloseOpenFile) { - std::unique_ptr<llvm::MemoryBuffer> Result; - std::error_code ec; - +llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> +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<llvm::MemoryBuffer> -FileManager::getBufferForFile(StringRef Filename, std::string *ErrorStr) { - std::unique_ptr<llvm::MemoryBuffer> Result; - std::error_code ec; - if (FileSystemOpts.WorkingDir.empty()) { - ec = FS->getBufferForFile(Filename, Result); - if (ec && ErrorStr) - *ErrorStr = ec.message(); - return Result; - } +llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> +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/lib/Basic/FileSystemStatCache.cpp b/lib/Basic/FileSystemStatCache.cpp index 7515cfb440..83e42bdb84 100644 --- a/lib/Basic/FileSystemStatCache.cpp +++ b/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<vfs::File> 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<vfs::Status> Status = OwnedFile->status(); + llvm::ErrorOr<vfs::Status> 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/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp index 6450406453..6991783056 100644 --- a/lib/Basic/SourceManager.cpp +++ b/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("<<<MISSING SOURCE FILE>>>\n"); Buffer.setPointer(MemoryBuffer::getNewMemBuffer(ContentsEntry->getSize(), "<invalid>").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/lib/Basic/VirtualFileSystem.cpp b/lib/Basic/VirtualFileSystem.cpp index df446bbcd5..8dc7006f2d 100644 --- a/lib/Basic/VirtualFileSystem.cpp +++ b/lib/Basic/VirtualFileSystem.cpp @@ -67,16 +67,14 @@ File::~File() {} FileSystem::~FileSystem() {} -std::error_code FileSystem::getBufferForFile( - const llvm::Twine &Name, std::unique_ptr<MemoryBuffer> &Result, - int64_t FileSize, bool RequiresNullTerminator, bool IsVolatile) { - std::unique_ptr<File> 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<std::unique_ptr<MemoryBuffer>> +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> status() override; - std::error_code getBuffer(const Twine &Name, - std::unique_ptr<MemoryBuffer> &Result, - int64_t FileSize = -1, - bool RequiresNullTerminator = true, - bool IsVolatile = false) override; + ErrorOr<std::unique_ptr<MemoryBuffer>> + 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<Status> RealFile::status() { return S; } -std::error_code RealFile::getBuffer(const Twine &Name, - std::unique_ptr<MemoryBuffer> &Result, - int64_t FileSize, - bool RequiresNullTerminator, - bool IsVolatile) { +ErrorOr<std::unique_ptr<MemoryBuffer>> +RealFile::getBuffer(const Twine &Name, int64_t FileSize, + bool RequiresNullTerminator, bool IsVolatile) { assert(FD != -1 && "cannot get buffer for closed file"); - ErrorOr<std::unique_ptr<MemoryBuffer>> 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> status(const Twine &Path) override; - std::error_code openFileForRead(const Twine &Path, - std::unique_ptr<File> &Result) override; + ErrorOr<std::unique_ptr<File>> 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<Status> RealFileSystem::status(const Twine &Path) { return Result; } -std::error_code RealFileSystem::openFileForRead(const Twine &Name, - std::unique_ptr<File> &Result) { +ErrorOr<std::unique_ptr<File>> +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<File> Result(new RealFile(FD)); Result->setName(Name.str()); - return std::error_code(); + return std::move(Result); } IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() { @@ -252,14 +241,13 @@ ErrorOr<Status> 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<File> &Result) { +ErrorOr<std::unique_ptr<File>> +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<FileSystem> ExternalFS); ErrorOr<Status> status(const Twine &Path) override; - std::error_code openFileForRead(const Twine &Path, - std::unique_ptr<File> &Result) override; + ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override; directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override{ ErrorOr<Entry *> E = lookupPath(Dir); @@ -969,9 +956,7 @@ ErrorOr<Status> VFSFromYAML::status(const Twine &Path) { return status(Path, *Result); } -std::error_code -VFSFromYAML::openFileForRead(const Twine &Path, - std::unique_ptr<vfs::File> &Result) { +ErrorOr<std::unique_ptr<File>> VFSFromYAML::openFileForRead(const Twine &Path) { ErrorOr<Entry *> 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<FileSystem> |