Files
carbon-lang/toolchain/sem_ir/cpp_file.cpp
T
Richard Smith 7d89ac98c7 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
2026-07-28 00:19:40 +00:00

58 lines
1.6 KiB
C++

// 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 "toolchain/sem_ir/cpp_file.h"
#include "clang/AST/Mangle.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Frontend/CompilerInstance.h"
#include "common/check.h"
namespace Carbon::SemIR {
CppFile::CppFile(std::shared_ptr<clang::CompilerInstance> clang,
llvm::LLVMContext* llvm_context)
: clang_(std::move(clang)), llvm_context_(llvm_context) {}
CppFile::~CppFile() = default;
auto CppFile::diagnostic_options() const -> const clang::DiagnosticOptions& {
return clang_->getDiagnostics().getDiagnosticOptions();
}
auto CppFile::lang_options() const -> const clang::LangOptions& {
return clang_->getLangOpts();
}
auto CppFile::source_manager() -> clang::SourceManager& {
return clang_->getSourceManager();
}
auto CppFile::source_manager() const -> const clang::SourceManager& {
return clang_->getSourceManager();
}
auto CppFile::diagnostics() const -> clang::DiagnosticsEngine& {
return clang_->getDiagnostics();
}
auto CppFile::ast_context() -> clang::ASTContext& {
return clang_->getASTContext();
}
auto CppFile::ast_context() const -> const clang::ASTContext& {
return clang_->getASTContext();
}
auto CppFile::CreateMangleContext() -> void {
CARBON_CHECK(!mangle_context_);
mangle_context_.reset(ast_context().createMangleContext());
}
auto CppFile::mangle_context() const -> clang::MangleContext& {
return *mangle_context_;
}
} // namespace Carbon::SemIR