Fix C++ code generation in --share-cpp-ast mode (#7605)

Instead of creating a CodeGenerator per CppDomain, and then crashing in
lowering when we try to consume the same llvm Module multiple times,
create a CodeGenerator for each CppFile within the domain.

For now, we mulitplex all of Clang's ASTConsumer output to all code
generators, which means that any strong external definitions within a
Carbon file (for example, in an inline `Cpp` fragment) will be emitted
to all output files in the same `CppDomain`, resulting in link errors
due to symbol redefinitions. This will be addressed later. But this
should be sufficient for Carbon compilations in which such symbols are
not defined.

We also don't yet attempt to classify which compilations will need C++
code generation, and instead create a clang `CodeGenerator` for every
Carbon file that has C++ imports. For `carbom compile`, only one Carbon
file will need code generation, and yet we still build multiple
`CodeGenerator` objects in general. Fixing this requires more plumbing
from the driver, and this will also be handled in a follow-up.

Assisted-by: Gemini via Antigravity
This commit is contained in:
Richard Smith
2026-08-04 17:59:06 +00:00
committed by GitHub
parent a9cc7bf490
commit 34a2e270f4
32 changed files with 641 additions and 232 deletions
@@ -458,7 +458,7 @@ fn G() {
// CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
// CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
// CHECK:STDOUT:
// CHECK:STDOUT: %"class.Carbon::C" = type { i32, i32 }
// CHECK:STDOUT: %"class.Carbon::C.0" = type { i32, i32 }
// CHECK:STDOUT:
// CHECK:STDOUT: @C.val.loc19_3 = internal constant { i32, i32 } zeroinitializer
// CHECK:STDOUT:
@@ -468,10 +468,10 @@ fn G() {
// CHECK:STDOUT: %c.addr = alloca ptr, align 8
// CHECK:STDOUT: store ptr %c, ptr %c.addr, align 8, !tbaa !12
// CHECK:STDOUT: %0 = load ptr, ptr %c.addr, align 8, !tbaa !12, !nonnull !15, !align !16
// CHECK:STDOUT: %a = getelementptr inbounds nuw %"class.Carbon::C", ptr %0, i32 0, i32 0
// CHECK:STDOUT: %a = getelementptr inbounds nuw %"class.Carbon::C.0", ptr %0, i32 0, i32 0
// CHECK:STDOUT: store i32 12, ptr %a, align 4, !tbaa !17
// CHECK:STDOUT: %1 = load ptr, ptr %c.addr, align 8, !tbaa !12, !nonnull !15, !align !16
// CHECK:STDOUT: %b = getelementptr inbounds nuw %"class.Carbon::C", ptr %1, i32 0, i32 1
// CHECK:STDOUT: %b = getelementptr inbounds nuw %"class.Carbon::C.0", ptr %1, i32 0, i32 1
// CHECK:STDOUT: store i32 34, ptr %b, align 4, !tbaa !19
// CHECK:STDOUT: ret void
// CHECK:STDOUT: }
@@ -188,14 +188,14 @@ fn CallCallF() { Cpp.CallF(); }
// CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
// CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
// CHECK:STDOUT:
// CHECK:STDOUT: %"class.Carbon::A" = type {}
// CHECK:STDOUT: %"class.Carbon::A.0" = type {}
// CHECK:STDOUT:
// CHECK:STDOUT: $_ZN6Carbon1AD2Ev = comdat any
// CHECK:STDOUT:
// CHECK:STDOUT: ; Function Attrs: mustprogress nounwind uwtable
// CHECK:STDOUT: define dso_local noundef i32 @_Z5CallFv() #0 {
// CHECK:STDOUT: entry:
// CHECK:STDOUT: %ref.tmp = alloca %"class.Carbon::A", align 1
// CHECK:STDOUT: %ref.tmp = alloca %"class.Carbon::A.0", align 1
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %ref.tmp) #4
// CHECK:STDOUT: %call = call noundef i32 @_ZNK6Carbon1A1FEi(ptr noundef nonnull align 1 %ref.tmp, i32 noundef 123)
// CHECK:STDOUT: call void @_ZN6Carbon1AD2Ev(ptr noundef nonnull align 1 %ref.tmp) #4