Add some encapsulation to CppDomain (#7579)

Start tracking the domain within `CppContext`s instead of having them
duplicate its fields. This allows us to remove the shared ownership of
the clang parser.

---------

Co-authored-by: Christopher Di Bella <cjdb.ns@gmail.com>
This commit is contained in:
Richard Smith
2026-07-31 18:08:13 +00:00
committed by GitHub
co-authored by Christopher Di Bella
parent 9b8a2124b9
commit 9bcee64b32
13 changed files with 141 additions and 66 deletions
+19 -15
View File
@@ -16,6 +16,7 @@
#include "common/pretty_stack_trace_function.h"
#include "toolchain/check/check_unit.h"
#include "toolchain/check/context.h"
#include "toolchain/check/cpp/domain.h"
#include "toolchain/check/cpp/generate_ast.h"
#include "toolchain/check/cpp/import.h"
#include "toolchain/check/diagnostic_emitter.h"
@@ -416,6 +417,12 @@ auto CheckParseTrees(
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
const CheckParseTreesOptions& options,
std::shared_ptr<clang::CompilerInvocation> clang_invocation) -> void {
// C++ domains used across files. When compiling with a single ASTContext
// (`options.share_cpp_ast`), there is only a single shared domain. This
// variable is created early so the domains outlive the `UnitAndImports` that
// reference them.
llvm::SmallVector<std::unique_ptr<CppDomain>> cpp_domains;
// UnitAndImports is big due to its SmallVectors, so we default to 0 on the
// stack.
llvm::SmallVector<UnitAndImports, 0> unit_infos(
@@ -498,19 +505,16 @@ auto CheckParseTrees(
}
}
// C++ domains used across files. When compiling with a single ASTContext
// (`options.share_cpp_ast`), there is only a single shared domain.
llvm::SmallVector<std::shared_ptr<CppDomain>> cpp_domains;
// Create C++ domains for Cpp imports.
if (options.share_cpp_ast) {
// TODO: Remove dependence on properties of the first unit here.
auto shared_cpp_domain = InitializeCppDomain(
unit_infos.front().err_tracker,
unit_infos.front().unit->sem_ir->filename(), fs,
unit_infos.front().unit->llvm_context, clang_invocation);
if (shared_cpp_domain) {
cpp_domains.push_back(shared_cpp_domain);
if (auto cpp_domain = InitializeCppDomain(
unit_infos.front().err_tracker,
unit_infos.front().unit->sem_ir->filename(), fs,
unit_infos.front().unit->llvm_context, clang_invocation)) {
cpp_domains.push_back(std::move(cpp_domain));
for (auto& target_info : unit_infos) {
target_info.cpp_domain = shared_cpp_domain;
target_info.cpp_domain = cpp_domains.back().get();
}
}
} else {
@@ -518,11 +522,11 @@ auto CheckParseTrees(
if (unit_info.cpp_imports.empty()) {
continue;
}
unit_info.cpp_domain = InitializeCppDomain(
unit_info.err_tracker, unit_info.unit->sem_ir->filename(), fs,
unit_info.unit->llvm_context, clang_invocation);
if (unit_info.cpp_domain) {
cpp_domains.push_back(unit_info.cpp_domain);
if (auto cpp_domain = InitializeCppDomain(
unit_info.err_tracker, unit_info.unit->sem_ir->filename(), fs,
unit_info.unit->llvm_context, clang_invocation)) {
cpp_domains.push_back(std::move(cpp_domain));
unit_info.cpp_domain = cpp_domains.back().get();
}
}
}