From ec313e741d2f57d51785a1a64e5ede29be17174c Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Fri, 12 Sep 2025 14:30:06 -0700 Subject: [PATCH] [clang] Avoid reparsing VFS overlay files for module dep collector --- clang/lib/Frontend/CompilerInstance.cpp | 15 ++++----------- clang/test/VFS/broken-vfs-module-dep.c | 1 - llvm/include/llvm/Support/VirtualFileSystem.h | 10 +++------- llvm/lib/Support/VirtualFileSystem.cpp | 16 +++------------- 4 files changed, 10 insertions(+), 32 deletions(-) diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index 31a8d75fec4bd..3e67cf157cb1c 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -271,19 +271,12 @@ static void collectIncludePCH(CompilerInstance &CI, static void collectVFSEntries(CompilerInstance &CI, std::shared_ptr MDC) { - if (CI.getHeaderSearchOpts().VFSOverlayFiles.empty()) - return; - // Collect all VFS found. SmallVector VFSEntries; - for (const std::string &VFSFile : CI.getHeaderSearchOpts().VFSOverlayFiles) { - llvm::ErrorOr> Buffer = - llvm::MemoryBuffer::getFile(VFSFile); - if (!Buffer) - return; - llvm::vfs::collectVFSFromYAML(std::move(Buffer.get()), - /*DiagHandler*/ nullptr, VFSFile, VFSEntries); - } + CI.getVirtualFileSystem().visit([&](llvm::vfs::FileSystem &VFS) { + if (auto *RedirectingVFS = dyn_cast(&VFS)) + llvm::vfs::collectVFSEntries(*RedirectingVFS, VFSEntries); + }); for (auto &E : VFSEntries) MDC->addFile(E.VPath, E.RPath); diff --git a/clang/test/VFS/broken-vfs-module-dep.c b/clang/test/VFS/broken-vfs-module-dep.c index 2336306de8c6d..1c371a13e85c9 100644 --- a/clang/test/VFS/broken-vfs-module-dep.c +++ b/clang/test/VFS/broken-vfs-module-dep.c @@ -2,6 +2,5 @@ // RUN: mkdir -p %t // RUN: not %clang_cc1 -module-dependency-dir %t -ivfsoverlay %S/Inputs/invalid-yaml.yaml %s 2>&1 | FileCheck %s -// CHECK: error: Unexpected token // CHECK: error: Unexpected token // CHECK: 1 error generated diff --git a/llvm/include/llvm/Support/VirtualFileSystem.h b/llvm/include/llvm/Support/VirtualFileSystem.h index d97677305a39f..8007f3c853f20 100644 --- a/llvm/include/llvm/Support/VirtualFileSystem.h +++ b/llvm/include/llvm/Support/VirtualFileSystem.h @@ -1114,14 +1114,10 @@ class LLVM_ABI RedirectingFileSystem }; /// Collect all pairs of entries from the -/// \p YAMLFilePath. This is used by the module dependency collector to forward +/// \p VFS. This is used by the module dependency collector to forward /// the entries into the reproducer output VFS YAML file. -LLVM_ABI void collectVFSFromYAML( - std::unique_ptr Buffer, - llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath, - SmallVectorImpl &CollectedEntries, - void *DiagContext = nullptr, - IntrusiveRefCntPtr ExternalFS = getRealFileSystem()); +void collectVFSEntries(RedirectingFileSystem &VFS, + SmallVectorImpl &CollectedEntries); class YAMLVFSWriter { std::vector Mappings; diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp index 5d4248819f1fb..cf784595c2f1c 100644 --- a/llvm/lib/Support/VirtualFileSystem.cpp +++ b/llvm/lib/Support/VirtualFileSystem.cpp @@ -2707,19 +2707,9 @@ static void getVFSEntries(RedirectingFileSystem::Entry *SrcE, Entries.push_back(YAMLVFSEntry(VPath.c_str(), FE->getExternalContentsPath())); } -void vfs::collectVFSFromYAML(std::unique_ptr Buffer, - SourceMgr::DiagHandlerTy DiagHandler, - StringRef YAMLFilePath, - SmallVectorImpl &CollectedEntries, - void *DiagContext, - IntrusiveRefCntPtr ExternalFS) { - std::unique_ptr VFS = RedirectingFileSystem::create( - std::move(Buffer), DiagHandler, YAMLFilePath, DiagContext, - std::move(ExternalFS)); - if (!VFS) - return; - ErrorOr RootResult = - VFS->lookupPath("/"); +void vfs::collectVFSEntries(RedirectingFileSystem &VFS, + SmallVectorImpl &CollectedEntries) { + ErrorOr RootResult = VFS.lookupPath("/"); if (!RootResult) return; SmallVector Components;