[clang] Fix VFS creation crash with missing DiagnosticConsumer#201397
Merged
Conversation
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.
|
@llvm/pr-subscribers-clang Author: Jan Svoboda (jansvoboda11) ChangesFor convenience, the rdar://176754115 Full diff: https://github.com/llvm/llvm-project/pull/201397.diff 2 Files Affected:
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
|
benlangmuir
approved these changes
Jun 4, 2026
ahatanak
approved these changes
Jun 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 temporaryDiagnosticsEnginecreated internally within the function does need a consumer. This PR sets it up.rdar://176754115