[llvm] 4957c47 - [clang] Avoid reparsing VFS overlay files for module dep collector (#158372)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 15 09:37:22 PDT 2025
Author: Jan Svoboda
Date: 2025-09-15T09:37:19-07:00
New Revision: 4957c473bc73a3a0bf1037adec770547c4a0aa02
URL: https://github.com/llvm/llvm-project/commit/4957c473bc73a3a0bf1037adec770547c4a0aa02
DIFF: https://github.com/llvm/llvm-project/commit/4957c473bc73a3a0bf1037adec770547c4a0aa02.diff
LOG: [clang] Avoid reparsing VFS overlay files for module dep collector (#158372)
This PR uses the new-ish `llvm::vfs::FileSystem::visit()` interface to
collect VFS overlay entries from an existing `FileSystem` instance
rather than parsing the VFS YAML file anew. This prevents duplicate
diagnostics as observed by `clang/test/VFS/broken-vfs-module-dep.c`.
Added:
Modified:
clang/lib/Frontend/CompilerInstance.cpp
clang/test/VFS/broken-vfs-module-dep.c
llvm/include/llvm/Support/VirtualFileSystem.h
llvm/lib/Support/VirtualFileSystem.cpp
Removed:
################################################################################
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<ModuleDependencyCollector> MDC) {
- if (CI.getHeaderSearchOpts().VFSOverlayFiles.empty())
- return;
-
// Collect all VFS found.
SmallVector<llvm::vfs::YAMLVFSEntry, 16> VFSEntries;
- for (const std::string &VFSFile : CI.getHeaderSearchOpts().VFSOverlayFiles) {
- llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> 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<llvm::vfs::RedirectingFileSystem>(&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 <virtual path, real path> 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<llvm::MemoryBuffer> Buffer,
- llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
- SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
- void *DiagContext = nullptr,
- IntrusiveRefCntPtr<FileSystem> ExternalFS = getRealFileSystem());
+void collectVFSEntries(RedirectingFileSystem &VFS,
+ SmallVectorImpl<YAMLVFSEntry> &CollectedEntries);
class YAMLVFSWriter {
std::vector<YAMLVFSEntry> 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<MemoryBuffer> Buffer,
- SourceMgr::DiagHandlerTy DiagHandler,
- StringRef YAMLFilePath,
- SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
- void *DiagContext,
- IntrusiveRefCntPtr<FileSystem> ExternalFS) {
- std::unique_ptr<RedirectingFileSystem> VFS = RedirectingFileSystem::create(
- std::move(Buffer), DiagHandler, YAMLFilePath, DiagContext,
- std::move(ExternalFS));
- if (!VFS)
- return;
- ErrorOr<RedirectingFileSystem::LookupResult> RootResult =
- VFS->lookupPath("/");
+void vfs::collectVFSEntries(RedirectingFileSystem &VFS,
+ SmallVectorImpl<YAMLVFSEntry> &CollectedEntries) {
+ ErrorOr<RedirectingFileSystem::LookupResult> RootResult = VFS.lookupPath("/");
if (!RootResult)
return;
SmallVector<StringRef, 8> Components;
More information about the llvm-commits
mailing list