Add a flag to build a single ASTContext shared across all compilations (#7567)

Instead of building one Clang `ASTContext` per compilation, the
`--share-cpp-ast` flag causes us to build a single `ASTContext` and
share it across all contexts. One new abstraction is added: `CppDomain`
represents the Carbon-side view of a Clang AST that might be shared
across multiple `SemIR::File`s. This object owns the Clang instance and
the AST.

For now, we have no isolation between the C++ state exposed to different
Carbon compilations, and we have no multiplexing of generated LLVM IR
from C++ into different Carbon compilations, so the mode is not usable
yet. The plan is to keep it behind a flag until it's ready.

Assisted-by: Gemini via Antigravity
This commit is contained in:
Richard Smith
2026-07-28 00:19:40 +00:00
committed by GitHub
parent 4e77f9b6c8
commit 7d89ac98c7
25 changed files with 449 additions and 211 deletions
@@ -21,7 +21,7 @@ import Cpp inline '''
// CHECK:STDERR: 13 | export module Foo;
// CHECK:STDERR: | ^
// CHECK:STDERR: <carbon Cpp imports>:1:1: note: add 'module;' to the start of the file to introduce a global module fragment [CppInteropParseNote]
// CHECK:STDERR: 1 | # 6 "fail_export_module_in_inline_cpp.carbon"
// CHECK:STDERR: 1 | ;
// CHECK:STDERR: | ^
// CHECK:STDERR:
export module Foo;
@@ -37,7 +37,7 @@ import Cpp inline '''
// CHECK:STDERR: 17 | module Foo;
// CHECK:STDERR: | ^
// CHECK:STDERR: <carbon Cpp imports>:1:1: note: add 'module;' to the start of the file to introduce a global module fragment [CppInteropParseNote]
// CHECK:STDERR: 1 | # 6 "fail_module_in_inline_cpp.carbon"
// CHECK:STDERR: 1 | ;
// CHECK:STDERR: | ^
// CHECK:STDERR:
// CHECK:STDERR: fail_module_in_inline_cpp.carbon:[[@LINE+4]]:8: error: module 'Foo' not found [CppInteropParseError]
@@ -65,7 +65,7 @@ int n;
// CHECK:STDERR: 25 | module Foo;
// CHECK:STDERR: | ^
// CHECK:STDERR: <carbon Cpp imports>:1:1: note: add 'module;' to the start of the file to introduce a global module fragment [CppInteropParseNote]
// CHECK:STDERR: 1 | # 6 "fail_global_module_in_inline_cpp.carbon"
// CHECK:STDERR: 1 | ;
// CHECK:STDERR: | ^
// CHECK:STDERR:
// CHECK:STDERR: fail_global_module_in_inline_cpp.carbon:[[@LINE+4]]:8: error: module 'Foo' not found [CppInteropParseError]
@@ -0,0 +1,64 @@
// 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
//
// ARGS: compile --phase=check --share-cpp-ast %s
//
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
//
// 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();
}
// --- todo_a_leaks.carbon
library "[[@TEST_NAME]]";
import Cpp;
fn CallLeakedA() -> i32 {
// TODO: This should not be found as we have not imported "a.h".
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();
}