diff --git a/llvm/include/llvm/Support/VirtualFileSystem.h b/llvm/include/llvm/Support/VirtualFileSystem.h --- a/llvm/include/llvm/Support/VirtualFileSystem.h +++ b/llvm/include/llvm/Support/VirtualFileSystem.h @@ -427,6 +427,15 @@ Optional Perms, const detail::InMemoryFile *HardLinkTarget); + // The UniqueID of in-memory files is derived from path and content. + // This avoids difficulties in creating exactly equivalent in-memory FSes, + // as often needed in multithreaded programs. + sys::fs::UniqueID getUniqueID(hash_code) const; + sys::fs::UniqueID getFileID(sys::fs::UniqueID Parent, llvm::StringRef Name, + llvm::StringRef Contents) const; + sys::fs::UniqueID getDirectoryID(sys::fs::UniqueID Parent, + llvm::StringRef Name) const; + public: explicit InMemoryFileSystem(bool UseNormalizedPaths = true); ~InMemoryFileSystem() override; diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp --- a/llvm/lib/Support/VirtualFileSystem.cpp +++ b/llvm/lib/Support/VirtualFileSystem.cpp @@ -32,6 +32,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/FileSystem/UniqueID.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/Process.h" @@ -655,6 +656,9 @@ Status getStatus(const Twine &RequestedName) const { return Status::copyWithNewName(Stat, RequestedName); } + + UniqueID getUniqueID() const { return Stat.getUniqueID(); } + InMemoryNode *getChild(StringRef Name) { auto I = Entries.find(Name); if (I != Entries.end()) @@ -700,8 +704,9 @@ InMemoryFileSystem::InMemoryFileSystem(bool UseNormalizedPaths) : Root(new detail::InMemoryDirectory( - Status("", getNextVirtualUniqueID(), llvm::sys::TimePoint<>(), 0, 0, - 0, llvm::sys::fs::file_type::directory_file, + Status("", getDirectoryID(llvm::sys::fs::UniqueID(), "(root)"), + llvm::sys::TimePoint<>(), 0, 0, 0, + llvm::sys::fs::file_type::directory_file, llvm::sys::fs::perms::all_all))), UseNormalizedPaths(UseNormalizedPaths) {} @@ -711,6 +716,21 @@ return Root->toString(/*Indent=*/0); } +sys::fs::UniqueID InMemoryFileSystem::getUniqueID(hash_code Hash) const { + return sys::fs::UniqueID(std::numeric_limits::max(), + uint64_t(size_t(Hash))); +} +sys::fs::UniqueID +InMemoryFileSystem::getFileID(sys::fs::UniqueID Parent, llvm::StringRef Name, + llvm::StringRef Contents) const { + return getUniqueID(llvm::hash_combine(Parent.getFile(), Name, Contents)); +} +sys::fs::UniqueID +InMemoryFileSystem::getDirectoryID(sys::fs::UniqueID Parent, + llvm::StringRef Name) const { + return getUniqueID(llvm::hash_combine(Parent.getFile(), Name, 1)); +} + bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime, std::unique_ptr Buffer, Optional User, @@ -754,10 +774,14 @@ Child.reset(new detail::InMemoryHardLink(P.str(), *HardLinkTarget)); else { // Create a new file or directory. - Status Stat(P.str(), getNextVirtualUniqueID(), - llvm::sys::toTimePoint(ModificationTime), ResolvedUser, - ResolvedGroup, Buffer->getBufferSize(), ResolvedType, - ResolvedPerms); + Status Stat( + P.str(), + (ResolvedType == sys::fs::file_type::directory_file) + ? getDirectoryID(Dir->getUniqueID(), Name) + : getFileID(Dir->getUniqueID(), Name, Buffer->getBuffer()), + llvm::sys::toTimePoint(ModificationTime), ResolvedUser, + ResolvedGroup, Buffer->getBufferSize(), ResolvedType, + ResolvedPerms); if (ResolvedType == sys::fs::file_type::directory_file) { Child.reset(new detail::InMemoryDirectory(std::move(Stat))); } else { @@ -772,9 +796,9 @@ // Create a new directory. Use the path up to here. Status Stat( StringRef(Path.str().begin(), Name.end() - Path.str().begin()), - getNextVirtualUniqueID(), llvm::sys::toTimePoint(ModificationTime), - ResolvedUser, ResolvedGroup, 0, sys::fs::file_type::directory_file, - NewDirectoryPerms); + getDirectoryID(Dir->getUniqueID(), Name), + llvm::sys::toTimePoint(ModificationTime), ResolvedUser, ResolvedGroup, + 0, sys::fs::file_type::directory_file, NewDirectoryPerms); Dir = cast(Dir->addChild( Name, std::make_unique(std::move(Stat)))); continue; diff --git a/llvm/unittests/Support/VirtualFileSystemTest.cpp b/llvm/unittests/Support/VirtualFileSystemTest.cpp --- a/llvm/unittests/Support/VirtualFileSystemTest.cpp +++ b/llvm/unittests/Support/VirtualFileSystemTest.cpp @@ -1279,6 +1279,26 @@ EXPECT_THAT(Nodes, testing::UnorderedElementsAre("/a", "/a/b", "/c", "/c/d")); } +TEST_F(InMemoryFileSystemTest, UniqueID) { + ASSERT_TRUE(FS.addFile("/a/b", 0, MemoryBuffer::getMemBuffer("text"))); + ASSERT_TRUE(FS.addFile("/c/d", 0, MemoryBuffer::getMemBuffer("text"))); + ASSERT_TRUE(FS.addHardLink("/e/f", "/a/b")); + + EXPECT_EQ(FS.status("/a/b")->getUniqueID(), FS.status("/a/b")->getUniqueID()); + EXPECT_NE(FS.status("/a/b")->getUniqueID(), FS.status("/c/d")->getUniqueID()); + EXPECT_EQ(FS.status("/a/b")->getUniqueID(), FS.status("/e/f")->getUniqueID()); + EXPECT_EQ(FS.status("/a")->getUniqueID(), FS.status("/a")->getUniqueID()); + EXPECT_NE(FS.status("/a")->getUniqueID(), FS.status("/c")->getUniqueID()); + EXPECT_NE(FS.status("/a")->getUniqueID(), FS.status("/e")->getUniqueID()); + + // Recreating the "same" FS yields the same UniqueIDs. + vfs::InMemoryFileSystem FS2; + ASSERT_TRUE(FS2.addFile("/a/b", 0, MemoryBuffer::getMemBuffer("text"))); + EXPECT_EQ(FS.status("/a/b")->getUniqueID(), + FS2.status("/a/b")->getUniqueID()); + EXPECT_EQ(FS.status("/a")->getUniqueID(), FS2.status("/a")->getUniqueID()); +} + // NOTE: in the tests below, we use '//root/' as our root directory, since it is // a legal *absolute* path on Windows as well as *nix. class VFSFromYAMLTest : public ::testing::Test {