[clang] 3dad31f - [clang] Fix VFS creation crash with missing `DiagnosticConsumer` (#201397)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 5 08:14:02 PDT 2026
Author: Jan Svoboda
Date: 2026-06-05T08:13:57-07:00
New Revision: 3dad31f753a9950ac1841dea1a9b03f32e8b6997
URL: https://github.com/llvm/llvm-project/commit/3dad31f753a9950ac1841dea1a9b03f32e8b6997
DIFF: https://github.com/llvm/llvm-project/commit/3dad31f753a9950ac1841dea1a9b03f32e8b6997.diff
LOG: [clang] Fix VFS creation crash with missing `DiagnosticConsumer` (#201397)
For convenience, the `CompilerInstance::createVirtualFileSystem()` API
allows omitting the diagnostic consumer for clients that don't care
about missing overlay files and other VFS creation errors. However, even
in that case, the temporary `DiagnosticsEngine` created internally
within the function does need a consumer. This PR sets it up.
rdar://176754115
Added:
Modified:
clang/lib/Frontend/CompilerInstance.cpp
clang/unittests/Frontend/CompilerInstanceTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index 9e88abbece7f2..8aee45b5dc644 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -287,9 +287,15 @@ static void collectVFSEntries(CompilerInstance &CI,
void CompilerInstance::createVirtualFileSystem(
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS, DiagnosticConsumer *DC) {
+ bool ShouldOwnClient = false;
+ if (!DC) {
+ DC = new DiagnosticConsumer;
+ ShouldOwnClient = true;
+ }
+
DiagnosticOptions DiagOpts;
DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts, DC,
- /*ShouldOwnClient=*/false);
+ ShouldOwnClient);
VFS = createVFSFromCompilerInvocation(getInvocation(), Diags,
std::move(BaseFS));
diff --git a/clang/unittests/Frontend/CompilerInstanceTest.cpp b/clang/unittests/Frontend/CompilerInstanceTest.cpp
index 68578a93d75b5..41908e9c0f254 100644
--- a/clang/unittests/Frontend/CompilerInstanceTest.cpp
+++ b/clang/unittests/Frontend/CompilerInstanceTest.cpp
@@ -82,6 +82,17 @@ TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) {
ASSERT_TRUE(Instance.getFileManager().getOptionalFileRef("vfs-virtual.file"));
}
+TEST(CompilerInstance, CreateVFSWithoutDiagnosticConsumer) {
+ auto Invocation = std::make_shared<CompilerInvocation>();
+ Invocation->getHeaderSearchOpts().VFSOverlayFiles.push_back("/missing.yaml");
+ auto BaseFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
+ CompilerInstance Instance(std::move(Invocation));
+ // Check that omitting the DiagnosticConsumer doesn't crash (e.g. by
+ // dereferencing the null pointer).
+ ASSERT_NO_FATAL_FAILURE(
+ Instance.createVirtualFileSystem(std::move(BaseFS), /*DC=*/nullptr));
+}
+
TEST(CompilerInstance, AllowDiagnosticLogWithUnownedDiagnosticConsumer) {
DiagnosticOptions DiagOpts;
// Tell the diagnostics engine to emit the diagnostic log to STDERR. This
More information about the cfe-commits
mailing list