Files
carbon-lang/toolchain/check/testdata/interop/cpp/basics/share_ast.carbon
T
Richard Smith 34a2e270f4 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
2026-08-04 17:59:06 +00:00

68 lines
1.7 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
// EXTRA-ARGS: --share-cpp-ast
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/basics/share_ast.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/basics/share_ast.carbon
// --- a.h
#ifndef A_H
#define A_H
inline int GetA() { return 42; }
#endif
// --- b.h
#ifndef B_H
#define B_H
#include "a.h"
inline int GetB() { return 100; }
#endif
// --- a.carbon
library "[[@TEST_NAME]]";
import Cpp library "a.h";
fn CallA() -> i32 {
return Cpp.GetA();
}
// --- fail_a_does_not_leak.carbon
library "[[@TEST_NAME]]";
import Cpp;
fn CallLeakedA() -> i32 {
// This should not be found as we have not imported "a.h".
// CHECK:STDERR: fail_a_does_not_leak.carbon:[[@LINE+4]]:10: error: member name `GetA` not found in `Cpp` [MemberNameNotFoundInInstScope]
// CHECK:STDERR: return Cpp.GetA();
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR:
return Cpp.GetA();
}
// --- a_again.carbon
library "[[@TEST_NAME]]";
import Cpp library "a.h";
fn CallAAgain() -> i32 {
// This should be accepted as we have imported "a.h", even though we will just reuse the version from `a.carbon`.
return Cpp.GetA();
}
// --- b.carbon
library "[[@TEST_NAME]]";
import Cpp library "b.h";
fn CallAIndirect() -> i32 {
return Cpp.GetA();
}
fn CallB() -> i32 {
return Cpp.GetB();
}