Author: Jan Svoboda
Date: 2026-11-12T04:50:03Z
New Revision: 62ac48c44f922bf60e91d7e43f522223aa7d1eb9

URL: 
https://github.com/llvm/llvm-project/commit/62ac48c44f922bf60e91d7e43f522223aa7d1eb9
DIFF: 
https://github.com/llvm/llvm-project/commit/62ac48c44f922bf60e91d7e43f522223aa7d1eb9.diff

LOG: [clang][deps] Give `LocalFS` precedence in the scanning VFS overlay 
(#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.

Added: 
    

Modified: 
    clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp
    clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp 
b/clang/lib/DependencyScanning/DependencyScanningFilesystem.cpp
index fb1a04402f223..133c02a0cf264 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 0f2292ca26219..e1c9783b7022d 100644
--- a/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp
+++ b/clang/unittests/DependencyScanning/DependencyScanningFilesystemTest.cpp
@@ -679,3 +679,34 @@ 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[llvm::sys::path::filename(It->path())] = It->type();
+  ASSERT_FALSE(EC);
+  EXPECT_EQ(Entries.size(), 2u);
+  EXPECT_EQ(Entries.lookup("both"), llvm::sys::fs::file_type::directory_file);
+  EXPECT_EQ(Entries.lookup("glob.c"), llvm::sys::fs::file_type::regular_file);
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to