llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Jan Svoboda (jansvoboda11) <details> <summary>Changes</summary> The overlay of `LocalFS` and the global FS that `DependencyScanningWorkerFilesystem` forwards unhandled queries to (e.g. directory iteration) put the global FS on top. That's inconsistent with `status()`, `openFileForRead()` and `getRealPath()`, which look at `LocalFS` first. Put `LocalFS` on top of the overlay instead. Depends on #<!-- -->241463, only review the second commit. --- Full diff: https://github.com/llvm/llvm-project/pull/241464.diff 3 Files Affected: - (modified) clang/include/clang/DependencyScanning/DependencyScanningFilesystem.h (+6-2) - (modified) clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp (+17-4) - (modified) clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp (+100) ``````````diff diff --git a/clang/include/clang/DependencyScanning/DependencyScanningFilesystem.h b/clang/include/clang/DependencyScanning/DependencyScanningFilesystem.h index 69a6bb85fd560e..177c4f354a0086 100644 --- a/clang/include/clang/DependencyScanning/DependencyScanningFilesystem.h +++ b/clang/include/clang/DependencyScanning/DependencyScanningFilesystem.h @@ -415,8 +415,9 @@ class DependencyScanningWorkerFilesystem /// Per-filename state cached locally by this worker thread, so repeated /// queries can be served without touching the shared cache. The entry and - /// real path are arena-owned by the shared cache and outlive this worker, so - /// borrowing raw pointers here is safe. + /// real path are arena-owned by the shared cache (or by this worker for + /// results from \c LocalFS) and outlive the local cache, so borrowing raw + /// pointers here is safe. struct LocalEntry { const CachedFileSystemEntry *File = nullptr; const CachedRealPath *RealPath = nullptr; @@ -444,6 +445,9 @@ class DependencyScanningWorkerFilesystem /// The backing storage for cached contents resolved from local FS only. llvm::SpecificBumpPtrAllocator<CachedFileContents> LocalContentsStorage; + /// The backing storage for cached real paths resolved from local FS only. + llvm::SpecificBumpPtrAllocator<CachedRealPath> LocalRealPathStorage; + static const CachedFileSystemEntry *createEntry( llvm::ErrorOr<TentativeEntry> &Entry, llvm::SpecificBumpPtrAllocator<CachedFileContents> &ContentsStorage, diff --git a/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp b/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp index 0648867f613bf1..133c02a0cf2641 100644 --- a/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp +++ b/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp @@ -228,10 +228,12 @@ static IntrusiveRefCntPtr<llvm::vfs::FileSystem> combineGlobalAndLocalFS(IntrusiveRefCntPtr<llvm::vfs::FileSystem> GlobalFS, IntrusiveRefCntPtr<llvm::vfs::FileSystem> LocalFS) { if (LocalFS) { + // LocalFS takes precedence over GlobalFS, like it does for the queries that + // don't go through this overlay. auto OverlayVFS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>( - std::move(LocalFS)); - OverlayVFS->pushOverlay(std::move(GlobalFS)); - GlobalFS = std::move(OverlayVFS); + std::move(GlobalFS)); + OverlayVFS->pushOverlay(std::move(LocalFS)); + return OverlayVFS; } return GlobalFS; } @@ -488,6 +490,17 @@ DependencyScanningWorkerFilesystem::getRealPath(const Twine &Path, if (Local.RealPath) return HandleCachedRealPath(*Local.RealPath); + // Resolve through the local VFS without going through the shared cache. + if (LocalFS && LocalFS->exists(OriginalFilename)) { + std::error_code EC = LocalFS->getRealPath(OriginalFilename, Output); + CachedRealPath RealPath = EC; + if (!EC) + RealPath = std::string(Output.begin(), Output.end()); + Local.RealPath = new (LocalRealPathStorage.Allocate()) + CachedRealPath(std::move(RealPath)); + return HandleCachedRealPath(*Local.RealPath); + } + // If we have the result in the shared cache, cache it locally. auto &Shard = Service.getSharedCache().getShardForFilename(*FilenameForLookup); @@ -498,7 +511,7 @@ DependencyScanningWorkerFilesystem::getRealPath(const Twine &Path, } // If we don't know the real path, compute it... - std::error_code EC = getUnderlyingFS().getRealPath(OriginalFilename, Output); + std::error_code EC = BaseFS->getRealPath(OriginalFilename, Output); llvm::ErrorOr<llvm::StringRef> ComputedRealPath = EC; if (!EC) ComputedRealPath = StringRef{Output.data(), Output.size()}; diff --git a/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp b/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp index 9f1c966eb34d2d..b5ad1c7687d3d8 100644 --- a/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp +++ b/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp @@ -610,3 +610,103 @@ TEST(DependencyScanningWorkerFilesystem, LocalFS) { EXPECT_EQ(InstrLocalFS->NumStatusCalls, 2u); // not using global cache. } } + +namespace { +/// Returns real paths prefixed with "/local-real", to tell them apart from real +/// paths returned by other filesystems. +class LocalRealPathFileSystem : public llvm::vfs::ProxyFileSystem { +public: + using ProxyFileSystem::ProxyFileSystem; + + std::error_code getRealPath(const llvm::Twine &Path, + llvm::SmallVectorImpl<char> &Output) override { + llvm::SmallString<128> RealPath; + if (std::error_code EC = ProxyFileSystem::getRealPath(Path, RealPath)) + return EC; + llvm::StringRef Prefix = "/local-real"; + Output.assign(Prefix.begin(), Prefix.end()); + Output.append(RealPath.begin(), RealPath.end()); + return {}; + } +}; +} // namespace + +TEST(DependencyScanningWorkerFilesystem, LocalFSRealPath) { + auto GlobalFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>(); + GlobalFS->setCurrentWorkingDirectory("/"); + GlobalFS->addFile("/both.c", 0, llvm::MemoryBuffer::getMemBuffer("")); + GlobalFS->addFile("/glob.c", 0, llvm::MemoryBuffer::getMemBuffer("")); + + auto LocalInMemoryFS = + llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>(); + LocalInMemoryFS->setCurrentWorkingDirectory("/"); + LocalInMemoryFS->addFile("/both.c", 0, llvm::MemoryBuffer::getMemBuffer("")); + LocalInMemoryFS->addFile("/loc.c", 0, llvm::MemoryBuffer::getMemBuffer("")); + auto LocalFS = + llvm::makeIntrusiveRefCnt<LocalRealPathFileSystem>(LocalInMemoryFS); + + DependencyScanningServiceOptions Opts; + DependencyScanningService Service(std::move(Opts)); + + auto GetRealPath = [](DependencyScanningWorkerFilesystem &DepFS, + llvm::StringRef Path) -> std::string { + llvm::SmallString<128> Result; + if (DepFS.getRealPath(Path, Result)) + return "<error>"; + return std::string(Result); + }; + + // Populate the shared cache for "/glob.c" from a worker without LocalFS. + { + DependencyScanningWorkerFilesystem DepFS(Service, GlobalFS); + EXPECT_EQ(GetRealPath(DepFS, "/glob.c"), "/glob.c"); + } + + // LocalFS takes precedence over the global FS, like for status(). + { + DependencyScanningWorkerFilesystem DepFS(Service, GlobalFS, LocalFS); + EXPECT_EQ(GetRealPath(DepFS, "/loc.c"), "/local-real/loc.c"); + EXPECT_EQ(GetRealPath(DepFS, "/both.c"), "/local-real/both.c"); + EXPECT_EQ(GetRealPath(DepFS, "/glob.c"), "/glob.c"); + } + + // Real paths from LocalFS don't leak to other workers via the shared cache. + { + DependencyScanningWorkerFilesystem DepFS(Service, GlobalFS); + EXPECT_EQ(GetRealPath(DepFS, "/loc.c"), "/loc.c"); + EXPECT_EQ(GetRealPath(DepFS, "/both.c"), "/both.c"); + } +} + +TEST(DependencyScanningWorkerFilesystem, LocalFSDirectoryIteration) { + auto GlobalFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>(); + GlobalFS->setCurrentWorkingDirectory("/"); + GlobalFS->addFile("/dir/both", 0, llvm::MemoryBuffer::getMemBuffer("")); + GlobalFS->addFile("/dir/glob.c", 0, llvm::MemoryBuffer::getMemBuffer("")); + + auto LocalFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>(); + LocalFS->setCurrentWorkingDirectory("/"); + LocalFS->addFile("/dir/both/loc.c", 0, llvm::MemoryBuffer::getMemBuffer("")); + + DependencyScanningServiceOptions Opts; + DependencyScanningService Service(std::move(Opts)); + DependencyScanningWorkerFilesystem DepFS(Service, GlobalFS, LocalFS); + + // "/dir/both" is a directory in LocalFS, which takes precedence. + auto Stat = DepFS.status("/dir/both"); + ASSERT_TRUE(Stat); + EXPECT_TRUE(Stat->isDirectory()); + + // Directory iteration is consistent with status(). + std::error_code EC; + llvm::StringMap<llvm::sys::fs::file_type> Entries; + for (llvm::vfs::directory_iterator It = DepFS.dir_begin("/dir", EC), End; + !EC && It != End; It.increment(EC)) + Entries[It->path()] = It->type(); + ASSERT_FALSE(EC); + EXPECT_EQ(Entries.size(), 2u); + EXPECT_EQ(Entries.lookup("/dir/both"), + llvm::sys::fs::file_type::directory_file); + EXPECT_EQ(Entries.lookup("/dir/glob.c"), + llvm::sys::fs::file_type::regular_file); +} `````````` </details> https://github.com/llvm/llvm-project/pull/241464 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
