Skip to content

[clang] Fix VFS creation crash with missing DiagnosticConsumer#201397

Merged
jansvoboda11 merged 1 commit into
llvm:mainfrom
jansvoboda11:create-vfs-crash
Jun 5, 2026
Merged

[clang] Fix VFS creation crash with missing DiagnosticConsumer#201397
jansvoboda11 merged 1 commit into
llvm:mainfrom
jansvoboda11:create-vfs-crash

Conversation

@jansvoboda11

Copy link
Copy Markdown
Contributor

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

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` the function creates internally does need a consumer. This PR sets it up.
@jansvoboda11 jansvoboda11 requested a review from ahatanak June 3, 2026 16:15
@llvmorg-github-actions llvmorg-github-actions Bot added the clang Clang issues not falling into any other category label Jun 3, 2026
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-clang

Author: Jan Svoboda (jansvoboda11)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/201397.diff

2 Files Affected:

  • (modified) clang/lib/Frontend/CompilerInstance.cpp (+7-1)
  • (modified) clang/unittests/Frontend/CompilerInstanceTest.cpp (+11)
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

@jansvoboda11 jansvoboda11 merged commit 3dad31f into llvm:main Jun 5, 2026
12 checks passed
@jansvoboda11 jansvoboda11 deleted the create-vfs-crash branch June 5, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants