Fix crash if initializing Clang fails. (#7248)

Flush diagnostics before destroying Clang. If we see an `inline Cpp` and
`Cpp` initialization failed, recover by skipping the inline code rather
than CHECK-failing.
This commit is contained in:
Richard Smith
2026-05-21 21:21:50 +00:00
committed by GitHub
parent ad97b9e3a5
commit 805b3eebce
3 changed files with 44 additions and 8 deletions
+7 -4
View File
@@ -882,6 +882,13 @@ auto GenerateAst(Context& context,
context.sem_ir().set_cpp_file(std::make_unique<SemIR::CppFile>(
std::move(clang_instance_ptr), llvm_context));
// Register an annotation scope to flush any Clang diagnostics when we return.
// This ensures C++ diagnostics get flushed before `diags` is destroyed, and
// that diagnostics created here don't interleave with later Carbon
// diagnostics.
Diagnostics::AnnotationScope annotate_diagnostics(&context.emitter(),
[](auto& /*builder*/) {});
clang_instance.setDiagnostics(diags);
clang_instance.setVirtualFileSystem(fs);
clang_instance.createFileManager();
@@ -915,10 +922,6 @@ auto GenerateAst(Context& context,
return false;
}
// Flush any diagnostics. We know we're not part-way through emitting a
// diagnostic now.
context.emitter().Flush();
return true;
}