diff --git a/testing/file_test/autoupdate.cpp b/testing/file_test/autoupdate.cpp index a5644aa1f81a..5217bfaf23fa 100644 --- a/testing/file_test/autoupdate.cpp +++ b/testing/file_test/autoupdate.cpp @@ -201,6 +201,9 @@ auto FileTestAutoupdater::BuildCheckLines(llvm::StringRef output, } do_extra_check_replacements_(check_line); + if (check_line.empty()) { + continue; + } if (default_file_re_) { absl::string_view filename; diff --git a/testing/file_test/file_test_base.cpp b/testing/file_test/file_test_base.cpp index 0d34f98e072f..f127cc63c24d 100644 --- a/testing/file_test/file_test_base.cpp +++ b/testing/file_test/file_test_base.cpp @@ -246,15 +246,23 @@ auto FileTestCase::TestBody() -> void { } if (test_file.check_subset) { EXPECT_THAT(SplitOutput(test_file.actual_stdout), - IsSupersetOf(test_file.expected_stdout)); + IsSupersetOf(test_file.expected_stdout)) + << "Actual text:\n" + << test_file.actual_stdout; EXPECT_THAT(SplitOutput(test_file.actual_stderr), - IsSupersetOf(test_file.expected_stderr)); + IsSupersetOf(test_file.expected_stderr)) + << "Actual text:\n" + << test_file.actual_stderr; } else { EXPECT_THAT(SplitOutput(test_file.actual_stdout), - ElementsAreArray(test_file.expected_stdout)); + ElementsAreArray(test_file.expected_stdout)) + << "Actual text:\n" + << test_file.actual_stdout; EXPECT_THAT(SplitOutput(test_file.actual_stderr), - ElementsAreArray(test_file.expected_stderr)); + ElementsAreArray(test_file.expected_stderr)) + << "Actual text:\n" + << test_file.actual_stderr; } if (HasFailure()) { diff --git a/toolchain/check/check.cpp b/toolchain/check/check.cpp index 0c8e07a7b161..5e39e00d5213 100644 --- a/toolchain/check/check.cpp +++ b/toolchain/check/check.cpp @@ -392,6 +392,22 @@ static auto MaybeDumpSemIR( } } +// Handles options for dumping C++ AST. +static auto MaybeDumpCppAST(llvm::ArrayRef units, + const CheckParseTreesOptions& options) -> void { + if (!options.dump_cpp_ast_stream) { + return; + } + + for (const Unit& unit : units) { + if (!unit.cpp_ast || !*unit.cpp_ast) { + continue; + } + clang::ASTContext& ast_context = (*unit.cpp_ast)->getASTContext(); + ast_context.getTranslationUnitDecl()->dump(*options.dump_cpp_ast_stream); + } +} + auto CheckParseTrees( llvm::MutableArrayRef units, const Parse::GetTreeAndSubtreesStore& tree_and_subtrees_getters, @@ -509,6 +525,7 @@ auto CheckParseTrees( } MaybeDumpSemIR(units, tree_and_subtrees_getters, options); + MaybeDumpCppAST(units, options); } } // namespace Carbon::Check diff --git a/toolchain/check/check.h b/toolchain/check/check.h index 674cf5163030..952635b9355f 100644 --- a/toolchain/check/check.h +++ b/toolchain/check/check.h @@ -60,6 +60,9 @@ struct CheckParseTreesOptions { // If set, SemIR will be dumped to this. llvm::raw_ostream* dump_stream = nullptr; + // If set, C++ AST will be dumped to this. + llvm::raw_ostream* dump_cpp_ast_stream = nullptr; + // When dumping textual SemIR (or printing it to for verbose output), whether // to use ranges. enum class DumpSemIRRanges : int8_t { diff --git a/toolchain/check/cpp_thunk.cpp b/toolchain/check/cpp_thunk.cpp index 8db03d43d0bf..dedef2a678ad 100644 --- a/toolchain/check/cpp_thunk.cpp +++ b/toolchain/check/cpp_thunk.cpp @@ -195,11 +195,13 @@ static auto CreateThunkFunctionDecl( callee_function_decl.getReturnType(), thunk_param_types, callee_function_type->getExtProtoInfo()); + clang::DeclContext* decl_context = ast_context.getTranslationUnitDecl(); // TODO: Thunks should not have external linkage, consider using `SC_Static`. clang::FunctionDecl* thunk_function_decl = clang::FunctionDecl::Create( - ast_context, ast_context.getTranslationUnitDecl(), clang_loc, clang_loc, + ast_context, decl_context, clang_loc, clang_loc, clang::DeclarationName(&identifier_info), thunk_function_type, /*TInfo=*/nullptr, clang::SC_Extern); + decl_context->addDecl(thunk_function_decl); thunk_function_decl->setParams(BuildThunkParameters( ast_context, callee_function_decl, thunk_function_decl)); diff --git a/toolchain/check/testdata/interop/cpp/function/full_semir.carbon b/toolchain/check/testdata/interop/cpp/function/full_semir.carbon index a21b94407e64..d32e8272c854 100644 --- a/toolchain/check/testdata/interop/cpp/function/full_semir.carbon +++ b/toolchain/check/testdata/interop/cpp/function/full_semir.carbon @@ -26,7 +26,6 @@ library "[[@TEST_NAME]]"; import Cpp library "short_param.h"; fn F() { - // TODO: Find a way to test the full C++ thunk AST. Cpp.foo(1 as i16); } @@ -168,21 +167,21 @@ fn F() { // CHECK:STDOUT: %int_16: Core.IntLiteral = int_value 16 [concrete = constants.%int_16] // CHECK:STDOUT: %i16: type = class_type @Int, @Int(constants.%int_16) [concrete = constants.%i16] // CHECK:STDOUT: %impl.elem0: %.91d = impl_witness_access constants.%As.impl_witness.0ef, element0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.489] -// CHECK:STDOUT: %bound_method.loc8_13.1: = bound_method %int_1, %impl.elem0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.bound] +// CHECK:STDOUT: %bound_method.loc7_13.1: = bound_method %int_1, %impl.elem0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.bound] // CHECK:STDOUT: %specific_fn: = specific_function %impl.elem0, @Core.IntLiteral.as.As.impl.Convert(constants.%int_16) [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.specific_fn] -// CHECK:STDOUT: %bound_method.loc8_13.2: = bound_method %int_1, %specific_fn [concrete = constants.%bound_method] -// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.call: init %i16 = call %bound_method.loc8_13.2(%int_1) [concrete = constants.%int_1.f90] -// CHECK:STDOUT: %.loc8_13.1: %i16 = value_of_initializer %Core.IntLiteral.as.As.impl.Convert.call [concrete = constants.%int_1.f90] -// CHECK:STDOUT: %.loc8_13.2: %i16 = converted %int_1, %.loc8_13.1 [concrete = constants.%int_1.f90] -// CHECK:STDOUT: %.loc8_19.1: ref %i16 = temporary_storage -// CHECK:STDOUT: %.loc8_19.2: init %i16 = initialize_from %.loc8_13.2 to %.loc8_19.1 [concrete = constants.%int_1.f90] -// CHECK:STDOUT: %addr.loc8_19.1: %ptr.251 = addr_of %.loc8_19.1 -// CHECK:STDOUT: %foo__carbon_thunk.call: init %empty_tuple.type = call imports.%foo__carbon_thunk.decl(%addr.loc8_19.1) -// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound: = bound_method %.loc8_19.1, constants.%T.as.Destroy.impl.Op.507 +// CHECK:STDOUT: %bound_method.loc7_13.2: = bound_method %int_1, %specific_fn [concrete = constants.%bound_method] +// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.call: init %i16 = call %bound_method.loc7_13.2(%int_1) [concrete = constants.%int_1.f90] +// CHECK:STDOUT: %.loc7_13.1: %i16 = value_of_initializer %Core.IntLiteral.as.As.impl.Convert.call [concrete = constants.%int_1.f90] +// CHECK:STDOUT: %.loc7_13.2: %i16 = converted %int_1, %.loc7_13.1 [concrete = constants.%int_1.f90] +// CHECK:STDOUT: %.loc7_19.1: ref %i16 = temporary_storage +// CHECK:STDOUT: %.loc7_19.2: init %i16 = initialize_from %.loc7_13.2 to %.loc7_19.1 [concrete = constants.%int_1.f90] +// CHECK:STDOUT: %addr.loc7_19.1: %ptr.251 = addr_of %.loc7_19.1 +// CHECK:STDOUT: %foo__carbon_thunk.call: init %empty_tuple.type = call imports.%foo__carbon_thunk.decl(%addr.loc7_19.1) +// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound: = bound_method %.loc7_19.1, constants.%T.as.Destroy.impl.Op.507 // CHECK:STDOUT: %T.as.Destroy.impl.Op.specific_fn: = specific_function constants.%T.as.Destroy.impl.Op.507, @T.as.Destroy.impl.Op(constants.%i16) [concrete = constants.%T.as.Destroy.impl.Op.specific_fn] -// CHECK:STDOUT: %bound_method.loc8_19: = bound_method %.loc8_19.1, %T.as.Destroy.impl.Op.specific_fn -// CHECK:STDOUT: %addr.loc8_19.2: %ptr.251 = addr_of %.loc8_19.1 -// CHECK:STDOUT: %T.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method.loc8_19(%addr.loc8_19.2) +// CHECK:STDOUT: %bound_method.loc7_19: = bound_method %.loc7_19.1, %T.as.Destroy.impl.Op.specific_fn +// CHECK:STDOUT: %addr.loc7_19.2: %ptr.251 = addr_of %.loc7_19.1 +// CHECK:STDOUT: %T.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method.loc7_19(%addr.loc7_19.2) // CHECK:STDOUT: return // CHECK:STDOUT: } // CHECK:STDOUT: diff --git a/toolchain/check/testdata/interop/cpp/function/thunk_ast.carbon b/toolchain/check/testdata/interop/cpp/function/thunk_ast.carbon new file mode 100644 index 000000000000..699310071f79 --- /dev/null +++ b/toolchain/check/testdata/interop/cpp/function/thunk_ast.carbon @@ -0,0 +1,42 @@ +// 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: --dump-cpp-ast +// SET-CHECK-SUBSET +// +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/function/thunk_ast.carbon +// TIP: To dump output, run: +// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/function/thunk_ast.carbon +// CHECK:STDOUT: TranslationUnitDecl {{0x[a-f0-9]+}} <> + +// --- thunk_required.h + +auto foo(short a) -> void; +// CHECK:STDOUT: |-FunctionDecl {{0x[a-f0-9]+}} <./thunk_required.h:[[@LINE-1]]:1, col:22> col:6 used foo 'auto (short) -> void' +// CHECK:STDOUT: | `-ParmVarDecl {{0x[a-f0-9]+}} col:16 a 'short' +// CHECK:STDOUT: `-FunctionDecl {{0x[a-f0-9]+}} col:6 foo__carbon_thunk 'auto (short * _Nonnull) -> void' extern +// CHECK:STDOUT: |-ParmVarDecl {{0x[a-f0-9]+}} col:6 used a 'short * _Nonnull':'short *' +// CHECK:STDOUT: |-ReturnStmt {{0x[a-f0-9]+}} +// CHECK:STDOUT: | `-CallExpr {{0x[a-f0-9]+}} 'void' +// CHECK:STDOUT: | |-ImplicitCastExpr {{0x[a-f0-9]+}} 'auto (*)(short) -> void' +// CHECK:STDOUT: | | `-DeclRefExpr {{0x[a-f0-9]+}} 'auto (short) -> void' Function {{0x[a-f0-9]+}} 'foo' 'auto (short) -> void' +// CHECK:STDOUT: | `-ImplicitCastExpr {{0x[a-f0-9]+}} 'short' +// CHECK:STDOUT: | `-UnaryOperator {{0x[a-f0-9]+}} 'short' lvalue prefix '*' cannot overflow +// CHECK:STDOUT: | `-ImplicitCastExpr {{0x[a-f0-9]+}} 'short * _Nonnull':'short *' +// CHECK:STDOUT: | `-DeclRefExpr {{0x[a-f0-9]+}} 'short * _Nonnull':'short *' lvalue ParmVar {{0x[a-f0-9]+}} 'a' 'short * _Nonnull':'short *' +// CHECK:STDOUT: |-AlwaysInlineAttr {{0x[a-f0-9]+}} <> Implicit always_inline +// CHECK:STDOUT: `-AsmLabelAttr {{0x[a-f0-9]+}} Implicit "_Z3foos.carbon_thunk" + +// --- import_thunk_required.carbon + +library "[[@TEST_NAME]]"; + +import Cpp library "thunk_required.h"; + +fn F() { + Cpp.foo(1 as i16); +} diff --git a/toolchain/driver/compile_subcommand.cpp b/toolchain/driver/compile_subcommand.cpp index ef5227093c67..20609541c3f9 100644 --- a/toolchain/driver/compile_subcommand.cpp +++ b/toolchain/driver/compile_subcommand.cpp @@ -208,6 +208,14 @@ Dump the full SemIR to stdout when built. )""", }, [&](auto& arg_b) { arg_b.Set(&dump_sem_ir); }); + b.AddFlag( + { + .name = "dump-cpp-ast", + .help = R"""( +Dump the full C++ AST to stdout when built. +)""", + }, + [&](auto& arg_b) { arg_b.Set(&dump_cpp_ast); }); b.AddOneOfOption( { @@ -399,6 +407,11 @@ auto CompileSubcommand::ValidateOptions( PhaseToString(options_.phase)); return false; } + if (options_.dump_cpp_ast) { + emitter.Emit(CompilePhaseFlagConflict, "C++ AST", + PhaseToString(options_.phase)); + return false; + } [[fallthrough]]; case Phase::Check: if (options_.dump_llvm_ir) { @@ -1015,11 +1028,15 @@ auto CompileSubcommand::Run(DriverEnv& driver_env) -> DriverResult { options.gen_implicit_type_impls = options_.gen_implicit_type_impls; options.vlog_stream = driver_env.vlog_stream; options.fuzzing = driver_env.fuzzing; - if (options.vlog_stream || options_.dump_sem_ir || options_.dump_raw_sem_ir) { + if (options.vlog_stream || options_.dump_sem_ir || options_.dump_cpp_ast || + options_.dump_raw_sem_ir) { options.include_in_dumps = &cache.include_in_dumps(); if (options_.dump_sem_ir) { options.dump_stream = driver_env.output_stream; } + if (options_.dump_cpp_ast) { + options.dump_cpp_ast_stream = driver_env.output_stream; + } if (options.vlog_stream || options_.dump_sem_ir) { options.dump_sem_ir_ranges = options_.dump_sem_ir_ranges; } diff --git a/toolchain/driver/compile_subcommand.h b/toolchain/driver/compile_subcommand.h index 87b65c317629..ff820acf01cb 100644 --- a/toolchain/driver/compile_subcommand.h +++ b/toolchain/driver/compile_subcommand.h @@ -52,6 +52,7 @@ struct CompileOptions { bool dump_parse_tree = false; bool dump_raw_sem_ir = false; bool dump_sem_ir = false; + bool dump_cpp_ast = false; bool dump_llvm_ir = false; bool dump_asm = false; bool dump_mem_usage = false; diff --git a/toolchain/testing/file_test.cpp b/toolchain/testing/file_test.cpp index 1c8c1458f4dd..14671edc0f8c 100644 --- a/toolchain/testing/file_test.cpp +++ b/toolchain/testing/file_test.cpp @@ -263,6 +263,32 @@ auto ToolchainFileTest::GetLineNumberReplacements( return replacements; } +// For Clang AST dump lines, we remove references to builtins because they're +// inconsistent between systems and replace the ids since they're inconsistent +// between runs. +static auto DoClangASTCheckReplacements(std::string& check_line) -> void { + static constexpr llvm::StringRef ClangDeclIdRegex = "0x[a-f0-9]+"; + static const RE2 is_clang_ast_line_re( + R"(^// CHECK:STDOUT: (TranslationUnitDecl|[ |]*`?\-))"); + if (!RE2::PartialMatch(check_line, is_clang_ast_line_re)) { + return; + } + + // Filter out references to builtins. + static const RE2 is_builtin_referring_re( + R"(`-BuiltinType |[ ']__[a-zA-Z]|\| `\-PointerType 0x[a-f0-9]+ 'char \*'$)"); + if (RE2::PartialMatch(check_line, is_builtin_referring_re)) { + check_line.clear(); + return; + } + + // Replace the ids. + static const RE2 clang_decl_id_re(llvm::formatv(" {0} ", ClangDeclIdRegex)); + static const std::string& clang_decl_id_replacement = + *new std::string(llvm::formatv(" {{{{{0}}} ", ClangDeclIdRegex)); + RE2::GlobalReplace(&check_line, clang_decl_id_re, clang_decl_id_replacement); +} + auto ToolchainFileTest::DoExtraCheckReplacements(std::string& check_line) const -> void { if (component_ == "driver") { @@ -286,6 +312,9 @@ auto ToolchainFileTest::DoExtraCheckReplacements(std::string& check_line) const // package to the VFS with a fixed name. absl::StrReplaceAll({{data_->installation.core_package(), "{{.*}}"}}, &check_line); + if (component_ == "check") { + DoClangASTCheckReplacements(check_line); + } } else { FileTestBase::DoExtraCheckReplacements(check_line); }