[llvm-branch-commits] [llvm] [Support] Add vfs::findFileInParentDirectories() (PR #242492)

Steven Wu via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Nov 14 15:19:02 PST 2026


https://github.com/cachemeifyoucan updated https://github.com/llvm/llvm-project/pull/242492

>From ac1e1a0655c52c983a76b338bb356531b03fdbb3 Mon Sep 17 00:00:00 2001
From: Steven Wu <stevenwu at apple.com>
Date: Fri, 13 Nov 2026 13:14:24 -0800
Subject: [PATCH] [Support] Add vfs::findFileInParentDirectories()

Several tools look for a file in a directory and its parent
directories, for example clang-format for .clang-format and clang-tidy
for .clang-tidy, and each has its own loop for that. Add a function for
it, which dsymutil will use to find the compilation-prefix-map.json
files that compilation caching writes.
---
 llvm/include/llvm/Support/VirtualFileSystem.h | 11 +++++++
 llvm/lib/Support/VirtualFileSystem.cpp        | 16 ++++++++++
 .../Support/VirtualFileSystemTest.cpp         | 30 +++++++++++++++++++
 3 files changed, 57 insertions(+)

diff --git a/llvm/include/llvm/Support/VirtualFileSystem.h b/llvm/include/llvm/Support/VirtualFileSystem.h
index c2a84963204405..6eaccff6da2bc0 100644
--- a/llvm/include/llvm/Support/VirtualFileSystem.h
+++ b/llvm/include/llvm/Support/VirtualFileSystem.h
@@ -14,6 +14,7 @@
 #ifndef LLVM_SUPPORT_VIRTUALFILESYSTEM_H
 #define LLVM_SUPPORT_VIRTUALFILESYSTEM_H
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/ADT/STLFunctionalExtras.h"
 #include "llvm/ADT/SmallVector.h"
@@ -392,6 +393,16 @@ LLVM_ABI IntrusiveRefCntPtr<FileSystem> getRealFileSystem();
 /// This may only be called outside the IO sandbox.
 LLVM_ABI std::unique_ptr<FileSystem> createPhysicalFileSystem();
 
+/// Searches \p Dir and then each of its parent directories for a regular file
+/// named one of \p FileNames, trying the names in order in each directory. A
+/// relative \p Dir is only searched up to its first component.
+///
+/// \returns the path of the first file found, or std::nullopt if there is
+/// none.
+LLVM_ABI std::optional<std::string>
+findFileInParentDirectories(FileSystem &FS, StringRef Dir,
+                            ArrayRef<StringRef> FileNames);
+
 /// A file system that allows overlaying one \p AbstractFileSystem on top
 /// of another.
 ///
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp
index 231866671249b6..3f527d71bacbda 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -437,6 +437,22 @@ std::unique_ptr<FileSystem> vfs::createPhysicalFileSystem() {
   return std::make_unique<RealFileSystem>(false);
 }
 
+std::optional<std::string>
+vfs::findFileInParentDirectories(FileSystem &FS, StringRef Dir,
+                                 ArrayRef<StringRef> FileNames) {
+  SmallString<256> Path;
+  for (; !Dir.empty(); Dir = sys::path::parent_path(Dir)) {
+    for (StringRef FileName : FileNames) {
+      Path = Dir;
+      sys::path::append(Path, FileName);
+      ErrorOr<Status> S = FS.status(Path);
+      if (S && S->isRegularFile())
+        return std::string(Path);
+    }
+  }
+  return std::nullopt;
+}
+
 namespace {
 
 class RealFSDirIter : public llvm::vfs::detail::DirIterImpl {
diff --git a/llvm/unittests/Support/VirtualFileSystemTest.cpp b/llvm/unittests/Support/VirtualFileSystemTest.cpp
index 7ebb0822e0d25f..d692b4037511d8 100644
--- a/llvm/unittests/Support/VirtualFileSystemTest.cpp
+++ b/llvm/unittests/Support/VirtualFileSystemTest.cpp
@@ -1044,6 +1044,36 @@ TEST(ProxyFileSystemTest, Basic) {
   EXPECT_FALSE(Local);
 }
 
+TEST(VirtualFileSystemTest, FindFileInParentDirectories) {
+  vfs::InMemoryFileSystem FS;
+  for (StringRef Path :
+       {"/.cfg", "/a/b/.cfg", "/a/b/_cfg", "/a/b/c/.other", "/a/b/c/.dir/x"})
+    FS.addFile(Path, 0, MemoryBuffer::getMemBuffer(""));
+  auto Find = [&](StringRef Dir, ArrayRef<StringRef> FileNames) {
+    return vfs::findFileInParentDirectories(FS, Dir, FileNames);
+  };
+  auto PathTo = [](StringRef Dir, StringRef FileName) {
+    SmallString<128> Path(Dir);
+    sys::path::append(Path, FileName);
+    return std::string(Path);
+  };
+
+  // The closest file wins, including the one in the directory itself.
+  EXPECT_EQ(Find("/a/b", {".cfg"}), PathTo("/a/b", ".cfg"));
+  EXPECT_EQ(Find("/a/b/c/d", {".cfg"}), PathTo("/a/b", ".cfg"));
+  EXPECT_EQ(Find("/a", {".cfg"}), PathTo("/", ".cfg"));
+
+  // The names are tried in order in each directory, but a closer directory
+  // takes precedence over the order of the names.
+  EXPECT_EQ(Find("/a/b/c", {".cfg", "_cfg"}), PathTo("/a/b", ".cfg"));
+  EXPECT_EQ(Find("/a/b/c", {"_cfg", ".cfg"}), PathTo("/a/b", "_cfg"));
+  EXPECT_EQ(Find("/a/b/c", {".cfg", ".other"}), PathTo("/a/b/c", ".other"));
+
+  // Only regular files are found.
+  EXPECT_EQ(Find("/a/b/c", {".dir"}), std::nullopt);
+  EXPECT_EQ(Find("/a/b/c", {".missing"}), std::nullopt);
+}
+
 class InMemoryFileSystemTest : public ::testing::Test {
 protected:
   llvm::vfs::InMemoryFileSystem FS;



More information about the llvm-branch-commits mailing list