Rename cpp_ast to clang_ast_unit (#5926)

Followup of #5924.
This commit is contained in:
Boaz Brickner
2025-08-08 08:11:49 +00:00
committed by GitHub
parent d5cb9a7144
commit 9f108bad6e
13 changed files with 42 additions and 36 deletions
+2 -2
View File
@@ -400,10 +400,10 @@ static auto MaybeDumpCppAST(llvm::ArrayRef<Unit> units,
}
for (const Unit& unit : units) {
if (!unit.cpp_ast || !*unit.cpp_ast) {
if (!unit.clang_ast_unit || !*unit.clang_ast_unit) {
continue;
}
clang::ASTContext& ast_context = (*unit.cpp_ast)->getASTContext();
clang::ASTContext& ast_context = (*unit.clang_ast_unit)->getASTContext();
ast_context.getTranslationUnitDecl()->dump(*options.dump_cpp_ast_stream);
}
}
+1 -1
View File
@@ -30,7 +30,7 @@ struct Unit {
// Storage for the unit's Clang AST. The unique_ptr should start empty, and
// can be assigned as part of checking.
std::unique_ptr<clang::ASTUnit>* cpp_ast;
std::unique_ptr<clang::ASTUnit>* clang_ast_unit;
};
struct CheckParseTreesOptions {
+5 -4
View File
@@ -154,10 +154,11 @@ auto CheckUnit::InitPackageScopeAndImports() -> void {
const auto& cpp_imports = unit_and_imports_->cpp_imports;
if (!cpp_imports.empty()) {
auto* cpp_ast = unit_and_imports_->unit->cpp_ast;
CARBON_CHECK(cpp_ast);
CARBON_CHECK(!cpp_ast->get());
*cpp_ast = ImportCppFiles(context_, cpp_imports, fs_, clang_invocation_);
auto* clang_ast_unit = unit_and_imports_->unit->clang_ast_unit;
CARBON_CHECK(clang_ast_unit);
CARBON_CHECK(!clang_ast_unit->get());
*clang_ast_unit =
ImportCppFiles(context_, cpp_imports, fs_, clang_invocation_);
}
}
+1 -1
View File
@@ -269,7 +269,7 @@ class Context {
return sem_ir().import_ir_insts();
}
auto ast_context() -> clang::ASTContext& {
return sem_ir().cpp_ast()->getASTContext();
return sem_ir().clang_ast_unit()->getASTContext();
}
auto names() -> SemIR::NameStoreWrapper { return sem_ir().names(); }
auto name_scopes() -> SemIR::NameScopeStore& {
+1 -1
View File
@@ -290,7 +290,7 @@ auto BuildCppThunk(Context& context, const SemIR::Function& callee_function)
context, *callee_function_decl, thunk_param_types);
// Build the thunk function body.
clang::Sema& sema = context.sem_ir().cpp_ast()->getSema();
clang::Sema& sema = context.sem_ir().clang_ast_unit()->getSema();
clang::Sema::ContextRAII context_raii(sema, thunk_function_decl);
sema.ActOnStartOfFunctionDef(nullptr, thunk_function_decl);
+7 -7
View File
@@ -230,7 +230,7 @@ class CarbonClangDiagnosticConsumer : public clang::DiagnosticConsumer {
// Outputs Carbon diagnostics based on the collected Clang diagnostics. Must
// be called after the AST is set in the context.
auto EmitDiagnostics() -> void {
CARBON_CHECK(sem_ir_->cpp_ast(),
CARBON_CHECK(sem_ir_->clang_ast_unit(),
"Attempted to emit diagnostics before the AST Unit is loaded");
for (size_t i = 0; i != diagnostic_infos_.size(); ++i) {
@@ -386,7 +386,7 @@ static auto GenerateAst(
// Attach the AST to SemIR. This needs to be done before we can emit any
// diagnostics, so their locations can be properly interpreted by our
// diagnostics machinery.
context.sem_ir().set_cpp_ast(ast.get());
context.sem_ir().set_clang_ast_unit(ast.get());
// Emit any diagnostics we queued up while building the AST.
context.emitter().Flush();
@@ -431,7 +431,7 @@ auto ImportCppFiles(Context& context,
return nullptr;
}
CARBON_CHECK(!context.sem_ir().cpp_ast());
CARBON_CHECK(!context.sem_ir().clang_ast_unit());
PackageNameId package_id = imports.front().package_id;
CARBON_CHECK(
@@ -468,7 +468,7 @@ static auto ClangLookupName(Context& context, SemIR::NameScopeId scope_id,
return std::nullopt;
}
clang::ASTUnit* ast = context.sem_ir().cpp_ast();
clang::ASTUnit* ast = context.sem_ir().clang_ast_unit();
CARBON_CHECK(ast);
clang::Sema& sema = ast->getSema();
@@ -504,7 +504,7 @@ static auto ClangConstructorLookup(const Context& context,
-> clang::DeclContextLookupResult {
const SemIR::NameScope& scope = context.sem_ir().name_scopes().Get(scope_id);
clang::Sema& sema = context.sem_ir().cpp_ast()->getSema();
clang::Sema& sema = context.sem_ir().clang_ast_unit()->getSema();
clang::Decl* decl =
context.sem_ir().clang_decls().Get(scope.clang_decl_context_id()).decl;
return sema.LookupConstructors(cast<clang::CXXRecordDecl>(decl));
@@ -531,7 +531,7 @@ static auto IsDeclInjectedClassName(const Context& context,
const auto* scope_record_decl = cast<clang::CXXRecordDecl>(clang_decl.decl);
const clang::ASTContext& ast_context =
context.sem_ir().cpp_ast()->getASTContext();
context.sem_ir().clang_ast_unit()->getASTContext();
CARBON_CHECK(
ast_context.getCanonicalType(
ast_context.getRecordType(scope_record_decl)) ==
@@ -974,7 +974,7 @@ static auto BuildClassDefinition(Context& context,
auto ImportCppClassDefinition(Context& context, SemIR::LocId loc_id,
SemIR::ClassId class_id,
SemIR::ClangDeclId clang_decl_id) -> bool {
clang::ASTUnit* ast = context.sem_ir().cpp_ast();
clang::ASTUnit* ast = context.sem_ir().clang_ast_unit();
CARBON_CHECK(ast);
auto* clang_decl = cast<clang::CXXRecordDecl>(
+2 -2
View File
@@ -532,7 +532,7 @@ class CompilationUnit {
std::optional<std::function<auto()->const Parse::TreeAndSubtrees&>>
tree_and_subtrees_getter_;
std::optional<SemIR::File> sem_ir_;
std::unique_ptr<clang::ASTUnit> cpp_ast_;
std::unique_ptr<clang::ASTUnit> clang_ast_unit_;
std::unique_ptr<llvm::LLVMContext> llvm_context_;
std::unique_ptr<llvm::Module> module_;
};
@@ -703,7 +703,7 @@ auto CompilationUnit::GetCheckUnit() -> Check::Unit {
.value_stores = &value_stores_,
.timings = timings_ ? &*timings_ : nullptr,
.sem_ir = &*sem_ir_,
.cpp_ast = &cpp_ast_};
.clang_ast_unit = &clang_ast_unit_};
}
auto CompilationUnit::PostCheck() -> void {
+2 -2
View File
@@ -148,13 +148,13 @@ auto Context::File::SetText(Context& context, std::optional<int64_t> version,
SemIR::File sem_ir(tree_.get(), SemIR::CheckIRId(0), tree_->packaging_decl(),
*value_stores_, uri_.file().str());
std::unique_ptr<clang::ASTUnit> cpp_ast;
std::unique_ptr<clang::ASTUnit> clang_ast_unt;
// TODO: Support cross-file checking when multiple files have edits.
llvm::SmallVector<Check::Unit> units = {{{.consumer = &consumer,
.value_stores = value_stores_.get(),
.timings = nullptr,
.sem_ir = &sem_ir,
.cpp_ast = &cpp_ast}}};
.clang_ast_unit = &clang_ast_unt}}};
auto getter = [this]() -> const Parse::TreeAndSubtrees& {
return *tree_and_subtrees_;
+4 -4
View File
@@ -71,7 +71,7 @@ auto FileContext::PrepareToLower() -> void {
// Clang code generation should not actually modify the AST, but isn't
// const-correct.
cpp_code_generator_->Initialize(
const_cast<clang::ASTContext&>(cpp_ast()->getASTContext()));
const_cast<clang::ASTContext&>(clang_ast_unit()->getASTContext()));
}
// Lower all types that were required to be complete.
@@ -158,7 +158,7 @@ auto FileContext::Finalize() -> void {
// Clang code generation should not actually modify the AST, but isn't
// const-correct.
cpp_code_generator_->HandleTranslationUnit(
const_cast<clang::ASTContext&>(cpp_ast()->getASTContext()));
const_cast<clang::ASTContext&>(clang_ast_unit()->getASTContext()));
bool link_error = llvm::Linker::linkModules(
/*Dest=*/llvm_module(),
/*Src=*/std::unique_ptr<llvm::Module>(
@@ -174,7 +174,7 @@ auto FileContext::Finalize() -> void {
auto FileContext::CreateCppCodeGenerator()
-> std::unique_ptr<clang::CodeGenerator> {
if (!cpp_ast()) {
if (!clang_ast_unit()) {
return nullptr;
}
@@ -185,7 +185,7 @@ auto FileContext::CreateCppCodeGenerator()
cpp_code_gen_options_.EmitVersionIdentMetadata = false;
return std::unique_ptr<clang::CodeGenerator>(clang::CreateLLVMCodeGen(
cpp_ast()->getASTContext().getDiagnostics(),
clang_ast_unit()->getASTContext().getDiagnostics(),
clang_module_name_stream.TakeStr(), context().file_system(),
cpp_header_search_options_, cpp_preprocessor_options_,
cpp_code_gen_options_, llvm_context()));
+3 -1
View File
@@ -106,7 +106,9 @@ class FileContext {
return *cpp_code_generator_;
}
auto sem_ir() const -> const SemIR::File& { return *sem_ir_; }
auto cpp_ast() -> const clang::ASTUnit* { return sem_ir().cpp_ast(); }
auto clang_ast_unit() -> const clang::ASTUnit* {
return sem_ir().clang_ast_unit();
}
auto inst_namer() -> const SemIR::InstNamer* { return inst_namer_; }
auto global_variables() -> const Map<SemIR::InstId, llvm::GlobalVariable*>& {
return global_variables_;
@@ -126,7 +126,7 @@ auto DiagnosticLocConverter::ConvertWithImports(LocId loc_id,
// Convert the C++ import locations.
if (final_node_id.check_ir_id() == SemIR::CheckIRId::Cpp) {
const clang::ASTUnit* ast = sem_ir_->cpp_ast();
const clang::ASTUnit* ast = sem_ir_->clang_ast_unit();
// Collect the location backtrace that Clang would use for an error here.
ClangImportCollector(ast->getLangOpts(),
ast->getDiagnostics().getDiagnosticOptions(),
@@ -174,8 +174,8 @@ auto DiagnosticLocConverter::ConvertImpl(
clang::SourceLocation clang_loc =
sem_ir_->clang_source_locs().Get(clang_source_loc_id);
CARBON_CHECK(sem_ir_->cpp_ast());
const auto& src_mgr = sem_ir_->cpp_ast()->getSourceManager();
CARBON_CHECK(sem_ir_->clang_ast_unit());
const auto& src_mgr = sem_ir_->clang_ast_unit()->getSourceManager();
clang::PresumedLoc presumed_loc = src_mgr.getPresumedLoc(clang_loc);
if (presumed_loc.isInvalid()) {
return Diagnostics::ConvertedLoc();
+4 -3
View File
@@ -150,9 +150,10 @@ auto File::CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
mem_usage.Collect(MemUsage::ConcatLabel(label, "types_"), types_);
}
auto File::set_cpp_ast(clang::ASTUnit* cpp_ast) -> void {
cpp_ast_ = cpp_ast;
clang_mangle_context_.reset(cpp_ast->getASTContext().createMangleContext());
auto File::set_clang_ast_unit(clang::ASTUnit* clang_ast_unit) -> void {
clang_ast_unit_ = clang_ast_unit;
clang_mangle_context_.reset(
clang_ast_unit->getASTContext().createMangleContext());
}
} // namespace Carbon::SemIR
+7 -5
View File
@@ -194,12 +194,14 @@ class File : public Printable<File> {
}
auto import_cpps() -> ImportCppStore& { return import_cpps_; }
auto import_cpps() const -> const ImportCppStore& { return import_cpps_; }
auto cpp_ast() -> clang::ASTUnit* { return cpp_ast_; }
auto cpp_ast() const -> const clang::ASTUnit* { return cpp_ast_; }
auto clang_ast_unit() -> clang::ASTUnit* { return clang_ast_unit_; }
auto clang_ast_unit() const -> const clang::ASTUnit* {
return clang_ast_unit_;
}
// TODO: When the AST can be created before creating `File`, initialize the
// pointer in the constructor and remove this function. This is part of
// https://github.com/carbon-language/carbon-lang/issues/4666
auto set_cpp_ast(clang::ASTUnit* cpp_ast) -> void;
auto set_clang_ast_unit(clang::ASTUnit* clang_ast_unit) -> void;
auto clang_mangle_context() -> clang::MangleContext* {
return clang_mangle_context_.get();
}
@@ -335,10 +337,10 @@ class File : public Printable<File> {
// The Clang AST to use when looking up `Cpp` names. Null if there are no
// `Cpp` imports.
clang::ASTUnit* cpp_ast_ = nullptr;
clang::ASTUnit* clang_ast_unit_ = nullptr;
// The Clang mangle context for the target in the ASTContext. Initialized
// together with `cpp_ast_`.
// together with `clang_ast_unit_`.
std::unique_ptr<clang::MangleContext> clang_mangle_context_;
// Clang AST declarations pointing to the AST and their mapped Carbon