[clang] [clang][deps] Give `LocalFS` precedence in the scanning VFS overlay (PR #241464)
Jan Svoboda via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 11 11:47:57 PST 2026
https://github.com/jansvoboda11 created https://github.com/llvm/llvm-project/pull/241464
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.
>From 4b0c4686b93ff41bd30e47962f303ea690d99bf6 Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Wed, 11 Nov 2026 11:35:31 -0800
Subject: [PATCH 1/2] [clang][deps] Don't share real paths from LocalFS via the
shared cache
DependencyScanningWorkerFilesystem::getRealPath() computed uncached real
paths via the overlay of LocalFS and the global FS, but stored the result in
the shared cache. Since LocalFS is specific to the worker, this leaked its
real paths to other workers, and could hide LocalFS real paths behind results
cached by other workers.
Resolve real paths of files that exist in LocalFS through LocalFS and only
cache them locally, the same way getOrCreateFileSystemEntry() does for status
and contents. Compute all other real paths through the global FS.
---
.../DependencyScanningFilesystem.h | 8 ++-
.../DependencyScanningFilesystem.cpp | 13 +++-
.../DependencyScanningFilesystemTest.cpp | 67 +++++++++++++++++++
3 files changed, 85 insertions(+), 3 deletions(-)
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..fb1a04402f2230 100644
--- a/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp
+++ b/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp
@@ -488,6 +488,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 +509,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..f4ff4ec129dabb 100644
--- a/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp
+++ b/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp
@@ -610,3 +610,70 @@ 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");
+ }
+}
>From 461fbdd44752b6a5f28c409296ee0392110bd50e Mon Sep 17 00:00:00 2001
From: Jan Svoboda <jan_svoboda at apple.com>
Date: Wed, 11 Nov 2026 11:37:37 -0800
Subject: [PATCH 2/2] [clang][deps] Give LocalFS precedence in the scanning VFS
overlay
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.
---
.../DependencyScanningFilesystem.cpp | 8 +++--
.../DependencyScanningFilesystemTest.cpp | 33 +++++++++++++++++++
2 files changed, 38 insertions(+), 3 deletions(-)
diff --git a/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp b/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp
index fb1a04402f2230..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;
}
diff --git a/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp b/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp
index f4ff4ec129dabb..b5ad1c7687d3d8 100644
--- a/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp
+++ b/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp
@@ -677,3 +677,36 @@ TEST(DependencyScanningWorkerFilesystem, LocalFSRealPath) {
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);
+}
More information about the cfe-commits
mailing list