From 305c07703d166c112c857dd8bd5e96f2a84e3147 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Mon, 5 Oct 2015 13:15:33 +0000 Subject: [PATCH] [VFS] Remove setName from the file interface. This streamlines the interface a bit and makes Status more immutable. No functional change intended. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@249310 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/VirtualFileSystem.h | 10 ++- lib/Basic/VirtualFileSystem.cpp | 98 ++++++++++++++--------- unittests/Basic/VirtualFileSystemTest.cpp | 12 +-- 3 files changed, 74 insertions(+), 46 deletions(-) diff --git a/include/clang/Basic/VirtualFileSystem.h b/include/clang/Basic/VirtualFileSystem.h index e576db9bd8..c458c070b4 100644 --- a/include/clang/Basic/VirtualFileSystem.h +++ b/include/clang/Basic/VirtualFileSystem.h @@ -45,14 +45,18 @@ public: public: Status() : Type(llvm::sys::fs::file_type::status_error) {} Status(const llvm::sys::fs::file_status &Status); - Status(StringRef Name, StringRef RealName, llvm::sys::fs::UniqueID UID, + Status(StringRef Name, llvm::sys::fs::UniqueID UID, llvm::sys::TimeValue MTime, uint32_t User, uint32_t Group, uint64_t Size, llvm::sys::fs::file_type Type, llvm::sys::fs::perms Perms); + /// Get a copy of a Status with a different name. + static Status copyWithNewName(const Status &In, StringRef NewName); + static Status copyWithNewName(const llvm::sys::fs::file_status &In, + StringRef NewName); + /// \brief Returns the name that should be used for this file or directory. StringRef getName() const { return Name; } - void setName(StringRef N) { Name = N; } /// @name Status interface from llvm::sys::fs /// @{ @@ -92,8 +96,6 @@ public: 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. - virtual void setName(StringRef Name) = 0; }; namespace detail { diff --git a/lib/Basic/VirtualFileSystem.cpp b/lib/Basic/VirtualFileSystem.cpp index a36102cf0f..804fe1dae4 100644 --- a/lib/Basic/VirtualFileSystem.cpp +++ b/lib/Basic/VirtualFileSystem.cpp @@ -35,12 +35,24 @@ Status::Status(const file_status &Status) User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()), Type(Status.type()), Perms(Status.permissions()), IsVFSMapped(false) {} -Status::Status(StringRef Name, StringRef ExternalName, UniqueID UID, - sys::TimeValue MTime, uint32_t User, uint32_t Group, - uint64_t Size, file_type Type, perms Perms) +Status::Status(StringRef Name, UniqueID UID, sys::TimeValue MTime, + uint32_t User, uint32_t Group, uint64_t Size, file_type Type, + perms Perms) : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size), Type(Type), Perms(Perms), IsVFSMapped(false) {} +Status Status::copyWithNewName(const Status &In, StringRef NewName) { + return Status(NewName, In.getUniqueID(), In.getLastModificationTime(), + In.getUser(), In.getGroup(), In.getSize(), In.getType(), + In.getPermissions()); +} + +Status Status::copyWithNewName(const file_status &In, StringRef NewName) { + return Status(NewName, In.getUniqueID(), In.getLastModificationTime(), + In.getUser(), In.getGroup(), In.getSize(), In.type(), + In.permissions()); +} + bool Status::equivalent(const Status &Other) const { return getUniqueID() == Other.getUniqueID(); } @@ -87,7 +99,9 @@ class RealFile : public File { int FD; Status S; friend class RealFileSystem; - RealFile(int FD) : FD(FD) { + RealFile(int FD, StringRef NewName) + : FD(FD), S(NewName, {}, {}, {}, {}, {}, + llvm::sys::fs::file_type::status_error, {}) { assert(FD >= 0 && "Invalid or inactive file descriptor"); } @@ -99,7 +113,6 @@ public: bool RequiresNullTerminator = true, bool IsVolatile = false) override; std::error_code close() override; - void setName(StringRef Name) override; }; } // end anonymous namespace RealFile::~RealFile() { close(); } @@ -110,9 +123,7 @@ ErrorOr RealFile::status() { file_status RealStatus; if (std::error_code EC = sys::fs::status(FD, RealStatus)) return EC; - Status NewS(RealStatus); - NewS.setName(S.getName()); - S = std::move(NewS); + S = Status::copyWithNewName(RealStatus, S.getName()); } return S; } @@ -142,10 +153,6 @@ std::error_code RealFile::close() { return std::error_code(); } -void RealFile::setName(StringRef Name) { - S.setName(Name); -} - namespace { /// \brief The file system according to your operating system. class RealFileSystem : public FileSystem { @@ -160,9 +167,7 @@ ErrorOr RealFileSystem::status(const Twine &Path) { sys::fs::file_status RealStatus; if (std::error_code EC = sys::fs::status(Path, RealStatus)) return EC; - Status Result(RealStatus); - Result.setName(Path.str()); - return Result; + return Status::copyWithNewName(RealStatus, Path.str()); } ErrorOr> @@ -170,9 +175,7 @@ RealFileSystem::openFileForRead(const Twine &Name) { int FD; if (std::error_code EC = sys::fs::openFileForRead(Name, FD)) return EC; - std::unique_ptr Result(new RealFile(FD)); - Result->setName(Name.str()); - return std::move(Result); + return std::unique_ptr(new RealFile(FD, Name.str())); } IntrusiveRefCntPtr vfs::getRealFileSystem() { @@ -190,10 +193,8 @@ public: if (!EC && Iter != llvm::sys::fs::directory_iterator()) { llvm::sys::fs::file_status S; EC = Iter->status(S); - if (!EC) { - CurrentEntry = Status(S); - CurrentEntry.setName(Iter->path()); - } + if (!EC) + CurrentEntry = Status::copyWithNewName(S, Iter->path()); } } @@ -207,8 +208,7 @@ public: } else { llvm::sys::fs::file_status S; EC = Iter->status(S); - CurrentEntry = Status(S); - CurrentEntry.setName(Iter->path()); + CurrentEntry = Status::copyWithNewName(S, Iter->path()); } return EC; } @@ -725,9 +725,10 @@ class VFSFromYAMLParser { UseExternalName); break; case EK_Directory: - Result = new DirectoryEntry(LastComponent, std::move(EntryArrayContents), - Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, - 0, file_type::directory_file, sys::fs::all_all)); + Result = new DirectoryEntry( + LastComponent, std::move(EntryArrayContents), + Status("", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, 0, + file_type::directory_file, sys::fs::all_all)); break; } @@ -739,9 +740,10 @@ class VFSFromYAMLParser { for (sys::path::reverse_iterator I = sys::path::rbegin(Parent), E = sys::path::rend(Parent); I != E; ++I) { - Result = new DirectoryEntry(*I, llvm::makeArrayRef(Result), - Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, - 0, file_type::directory_file, sys::fs::all_all)); + Result = new DirectoryEntry( + *I, llvm::makeArrayRef(Result), + Status("", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, 0, + file_type::directory_file, sys::fs::all_all)); } return Result; } @@ -923,15 +925,13 @@ ErrorOr VFSFromYAML::status(const Twine &Path, Entry *E) { ErrorOr S = ExternalFS->status(F->getExternalContentsPath()); assert(!S || S->getName() == F->getExternalContentsPath()); if (S && !F->useExternalName(UseExternalNames)) - S->setName(PathStr); + *S = Status::copyWithNewName(*S, PathStr); if (S) S->IsVFSMapped = true; return S; } else { // directory DirectoryEntry *DE = cast(E); - Status S = DE->getStatus(); - S.setName(PathStr); - return S; + return Status::copyWithNewName(DE->getStatus(), PathStr); } } @@ -955,8 +955,34 @@ ErrorOr> VFSFromYAML::openFileForRead(const Twine &Path) { if (!Result) return Result; - if (!F->useExternalName(UseExternalNames)) - (*Result)->setName(Path.str()); + if (!F->useExternalName(UseExternalNames)) { + // Provide a file wrapper that returns the external name when asked. + class NamedFileAdaptor : public File { + std::unique_ptr InnerFile; + std::string NewName; + + public: + NamedFileAdaptor(std::unique_ptr InnerFile, std::string NewName) + : InnerFile(std::move(InnerFile)), NewName(std::move(NewName)) {} + + llvm::ErrorOr status() override { + auto InnerStatus = InnerFile->status(); + if (InnerStatus) + return Status::copyWithNewName(*InnerStatus, NewName); + return InnerStatus.getError(); + } + llvm::ErrorOr> + getBuffer(const Twine &Name, int64_t FileSize = -1, + bool RequiresNullTerminator = true, + bool IsVolatile = false) override { + return InnerFile->getBuffer(Name, FileSize, RequiresNullTerminator, + IsVolatile); + } + std::error_code close() override { return InnerFile->close(); } + }; + return std::unique_ptr( + new NamedFileAdaptor(std::move(*Result), Path.str())); + } return Result; } diff --git a/unittests/Basic/VirtualFileSystemTest.cpp b/unittests/Basic/VirtualFileSystemTest.cpp index 71d2d2b60c..13ae501235 100644 --- a/unittests/Basic/VirtualFileSystemTest.cpp +++ b/unittests/Basic/VirtualFileSystemTest.cpp @@ -92,20 +92,20 @@ public: } void addRegularFile(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) { - vfs::Status S(Path, Path, UniqueID(FSID, FileID++), sys::TimeValue::now(), - 0, 0, 1024, sys::fs::file_type::regular_file, Perms); + vfs::Status S(Path, UniqueID(FSID, FileID++), sys::TimeValue::now(), 0, 0, + 1024, sys::fs::file_type::regular_file, Perms); addEntry(Path, S); } void addDirectory(StringRef Path, sys::fs::perms Perms = sys::fs::all_all) { - vfs::Status S(Path, Path, UniqueID(FSID, FileID++), sys::TimeValue::now(), - 0, 0, 0, sys::fs::file_type::directory_file, Perms); + vfs::Status S(Path, UniqueID(FSID, FileID++), sys::TimeValue::now(), 0, 0, + 0, sys::fs::file_type::directory_file, Perms); addEntry(Path, S); } void addSymlink(StringRef Path) { - vfs::Status S(Path, Path, UniqueID(FSID, FileID++), sys::TimeValue::now(), - 0, 0, 0, sys::fs::file_type::symlink_file, sys::fs::all_all); + vfs::Status S(Path, UniqueID(FSID, FileID++), sys::TimeValue::now(), 0, 0, + 0, sys::fs::file_type::symlink_file, sys::fs::all_all); addEntry(Path, S); } }; -- 2.40.0