Skip to content

Commit 52ed262

Browse files
authored
Add a flag to dump the C++ AST (#5918)
Use it to dump the AST that includes a generated C++ thunk. Based on #5917. Also added printing of the full actual text when check fails to make debugging easier. Changed line replacement to allow removing complete lines. Part of #5514.
1 parent 0f17161 commit 52ed262

File tree

10 files changed

+141
-20
lines changed

10 files changed

+141
-20
lines changed

testing/file_test/autoupdate.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ auto FileTestAutoupdater::BuildCheckLines(llvm::StringRef output,
201201
}
202202

203203
do_extra_check_replacements_(check_line);
204+
if (check_line.empty()) {
205+
continue;
206+
}
204207

205208
if (default_file_re_) {
206209
absl::string_view filename;

testing/file_test/file_test_base.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,23 @@ auto FileTestCase::TestBody() -> void {
246246
}
247247
if (test_file.check_subset) {
248248
EXPECT_THAT(SplitOutput(test_file.actual_stdout),
249-
IsSupersetOf(test_file.expected_stdout));
249+
IsSupersetOf(test_file.expected_stdout))
250+
<< "Actual text:\n"
251+
<< test_file.actual_stdout;
250252
EXPECT_THAT(SplitOutput(test_file.actual_stderr),
251-
IsSupersetOf(test_file.expected_stderr));
253+
IsSupersetOf(test_file.expected_stderr))
254+
<< "Actual text:\n"
255+
<< test_file.actual_stderr;
252256

253257
} else {
254258
EXPECT_THAT(SplitOutput(test_file.actual_stdout),
255-
ElementsAreArray(test_file.expected_stdout));
259+
ElementsAreArray(test_file.expected_stdout))
260+
<< "Actual text:\n"
261+
<< test_file.actual_stdout;
256262
EXPECT_THAT(SplitOutput(test_file.actual_stderr),
257-
ElementsAreArray(test_file.expected_stderr));
263+
ElementsAreArray(test_file.expected_stderr))
264+
<< "Actual text:\n"
265+
<< test_file.actual_stderr;
258266
}
259267

260268
if (HasFailure()) {

toolchain/check/check.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,22 @@ static auto MaybeDumpSemIR(
392392
}
393393
}
394394

395+
// Handles options for dumping C++ AST.
396+
static auto MaybeDumpCppAST(llvm::ArrayRef<Unit> units,
397+
const CheckParseTreesOptions& options) -> void {
398+
if (!options.dump_cpp_ast_stream) {
399+
return;
400+
}
401+
402+
for (const Unit& unit : units) {
403+
if (!unit.cpp_ast || !*unit.cpp_ast) {
404+
continue;
405+
}
406+
clang::ASTContext& ast_context = (*unit.cpp_ast)->getASTContext();
407+
ast_context.getTranslationUnitDecl()->dump(*options.dump_cpp_ast_stream);
408+
}
409+
}
410+
395411
auto CheckParseTrees(
396412
llvm::MutableArrayRef<Unit> units,
397413
const Parse::GetTreeAndSubtreesStore& tree_and_subtrees_getters,
@@ -509,6 +525,7 @@ auto CheckParseTrees(
509525
}
510526

511527
MaybeDumpSemIR(units, tree_and_subtrees_getters, options);
528+
MaybeDumpCppAST(units, options);
512529
}
513530

514531
} // namespace Carbon::Check

toolchain/check/check.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ struct CheckParseTreesOptions {
6060
// If set, SemIR will be dumped to this.
6161
llvm::raw_ostream* dump_stream = nullptr;
6262

63+
// If set, C++ AST will be dumped to this.
64+
llvm::raw_ostream* dump_cpp_ast_stream = nullptr;
65+
6366
// When dumping textual SemIR (or printing it to for verbose output), whether
6467
// to use ranges.
6568
enum class DumpSemIRRanges : int8_t {

toolchain/check/cpp_thunk.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,13 @@ static auto CreateThunkFunctionDecl(
195195
callee_function_decl.getReturnType(), thunk_param_types,
196196
callee_function_type->getExtProtoInfo());
197197

198+
clang::DeclContext* decl_context = ast_context.getTranslationUnitDecl();
198199
// TODO: Thunks should not have external linkage, consider using `SC_Static`.
199200
clang::FunctionDecl* thunk_function_decl = clang::FunctionDecl::Create(
200-
ast_context, ast_context.getTranslationUnitDecl(), clang_loc, clang_loc,
201+
ast_context, decl_context, clang_loc, clang_loc,
201202
clang::DeclarationName(&identifier_info), thunk_function_type,
202203
/*TInfo=*/nullptr, clang::SC_Extern);
204+
decl_context->addDecl(thunk_function_decl);
203205

204206
thunk_function_decl->setParams(BuildThunkParameters(
205207
ast_context, callee_function_decl, thunk_function_decl));

toolchain/check/testdata/interop/cpp/function/full_semir.carbon

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ library "[[@TEST_NAME]]";
2626
import Cpp library "short_param.h";
2727

2828
fn F() {
29-
// TODO: Find a way to test the full C++ thunk AST.
3029
Cpp.foo(1 as i16);
3130
}
3231

@@ -168,21 +167,21 @@ fn F() {
168167
// CHECK:STDOUT: %int_16: Core.IntLiteral = int_value 16 [concrete = constants.%int_16]
169168
// CHECK:STDOUT: %i16: type = class_type @Int, @Int(constants.%int_16) [concrete = constants.%i16]
170169
// CHECK:STDOUT: %impl.elem0: %.91d = impl_witness_access constants.%As.impl_witness.0ef, element0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.489]
171-
// CHECK:STDOUT: %bound_method.loc8_13.1: <bound method> = bound_method %int_1, %impl.elem0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.bound]
170+
// CHECK:STDOUT: %bound_method.loc7_13.1: <bound method> = bound_method %int_1, %impl.elem0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.bound]
172171
// CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Core.IntLiteral.as.As.impl.Convert(constants.%int_16) [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.specific_fn]
173-
// CHECK:STDOUT: %bound_method.loc8_13.2: <bound method> = bound_method %int_1, %specific_fn [concrete = constants.%bound_method]
174-
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.call: init %i16 = call %bound_method.loc8_13.2(%int_1) [concrete = constants.%int_1.f90]
175-
// CHECK:STDOUT: %.loc8_13.1: %i16 = value_of_initializer %Core.IntLiteral.as.As.impl.Convert.call [concrete = constants.%int_1.f90]
176-
// CHECK:STDOUT: %.loc8_13.2: %i16 = converted %int_1, %.loc8_13.1 [concrete = constants.%int_1.f90]
177-
// CHECK:STDOUT: %.loc8_19.1: ref %i16 = temporary_storage
178-
// CHECK:STDOUT: %.loc8_19.2: init %i16 = initialize_from %.loc8_13.2 to %.loc8_19.1 [concrete = constants.%int_1.f90]
179-
// CHECK:STDOUT: %addr.loc8_19.1: %ptr.251 = addr_of %.loc8_19.1
180-
// CHECK:STDOUT: %foo__carbon_thunk.call: init %empty_tuple.type = call imports.%foo__carbon_thunk.decl(%addr.loc8_19.1)
181-
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound: <bound method> = bound_method %.loc8_19.1, constants.%T.as.Destroy.impl.Op.507
172+
// CHECK:STDOUT: %bound_method.loc7_13.2: <bound method> = bound_method %int_1, %specific_fn [concrete = constants.%bound_method]
173+
// CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.call: init %i16 = call %bound_method.loc7_13.2(%int_1) [concrete = constants.%int_1.f90]
174+
// CHECK:STDOUT: %.loc7_13.1: %i16 = value_of_initializer %Core.IntLiteral.as.As.impl.Convert.call [concrete = constants.%int_1.f90]
175+
// CHECK:STDOUT: %.loc7_13.2: %i16 = converted %int_1, %.loc7_13.1 [concrete = constants.%int_1.f90]
176+
// CHECK:STDOUT: %.loc7_19.1: ref %i16 = temporary_storage
177+
// CHECK:STDOUT: %.loc7_19.2: init %i16 = initialize_from %.loc7_13.2 to %.loc7_19.1 [concrete = constants.%int_1.f90]
178+
// CHECK:STDOUT: %addr.loc7_19.1: %ptr.251 = addr_of %.loc7_19.1
179+
// CHECK:STDOUT: %foo__carbon_thunk.call: init %empty_tuple.type = call imports.%foo__carbon_thunk.decl(%addr.loc7_19.1)
180+
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound: <bound method> = bound_method %.loc7_19.1, constants.%T.as.Destroy.impl.Op.507
182181
// CHECK:STDOUT: %T.as.Destroy.impl.Op.specific_fn: <specific function> = 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]
183-
// CHECK:STDOUT: %bound_method.loc8_19: <bound method> = bound_method %.loc8_19.1, %T.as.Destroy.impl.Op.specific_fn
184-
// CHECK:STDOUT: %addr.loc8_19.2: %ptr.251 = addr_of %.loc8_19.1
185-
// CHECK:STDOUT: %T.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method.loc8_19(%addr.loc8_19.2)
182+
// CHECK:STDOUT: %bound_method.loc7_19: <bound method> = bound_method %.loc7_19.1, %T.as.Destroy.impl.Op.specific_fn
183+
// CHECK:STDOUT: %addr.loc7_19.2: %ptr.251 = addr_of %.loc7_19.1
184+
// CHECK:STDOUT: %T.as.Destroy.impl.Op.call: init %empty_tuple.type = call %bound_method.loc7_19(%addr.loc7_19.2)
186185
// CHECK:STDOUT: return
187186
// CHECK:STDOUT: }
188187
// CHECK:STDOUT:
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
//
5+
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/int.carbon
6+
// EXTRA-ARGS: --dump-cpp-ast
7+
// SET-CHECK-SUBSET
8+
//
9+
// AUTOUPDATE
10+
// TIP: To test this file alone, run:
11+
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/function/thunk_ast.carbon
12+
// TIP: To dump output, run:
13+
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/function/thunk_ast.carbon
14+
// CHECK:STDOUT: TranslationUnitDecl {{0x[a-f0-9]+}} <<invalid sloc>> <invalid sloc>
15+
16+
// --- thunk_required.h
17+
18+
auto foo(short a) -> void;
19+
// CHECK:STDOUT: |-FunctionDecl {{0x[a-f0-9]+}} <./thunk_required.h:[[@LINE-1]]:1, col:22> col:6 used foo 'auto (short) -> void'
20+
// CHECK:STDOUT: | `-ParmVarDecl {{0x[a-f0-9]+}} <col:10, col:16> col:16 a 'short'
21+
// CHECK:STDOUT: `-FunctionDecl {{0x[a-f0-9]+}} <col:6> col:6 foo__carbon_thunk 'auto (short * _Nonnull) -> void' extern
22+
// CHECK:STDOUT: |-ParmVarDecl {{0x[a-f0-9]+}} <col:6> col:6 used a 'short * _Nonnull':'short *'
23+
// CHECK:STDOUT: |-ReturnStmt {{0x[a-f0-9]+}} <col:6>
24+
// CHECK:STDOUT: | `-CallExpr {{0x[a-f0-9]+}} <col:6> 'void'
25+
// CHECK:STDOUT: | |-ImplicitCastExpr {{0x[a-f0-9]+}} <col:6> 'auto (*)(short) -> void' <FunctionToPointerDecay>
26+
// CHECK:STDOUT: | | `-DeclRefExpr {{0x[a-f0-9]+}} <col:6> 'auto (short) -> void' Function {{0x[a-f0-9]+}} 'foo' 'auto (short) -> void'
27+
// CHECK:STDOUT: | `-ImplicitCastExpr {{0x[a-f0-9]+}} <col:6> 'short' <LValueToRValue>
28+
// CHECK:STDOUT: | `-UnaryOperator {{0x[a-f0-9]+}} <col:6> 'short' lvalue prefix '*' cannot overflow
29+
// CHECK:STDOUT: | `-ImplicitCastExpr {{0x[a-f0-9]+}} <col:6> 'short * _Nonnull':'short *' <LValueToRValue>
30+
// CHECK:STDOUT: | `-DeclRefExpr {{0x[a-f0-9]+}} <col:6> 'short * _Nonnull':'short *' lvalue ParmVar {{0x[a-f0-9]+}} 'a' 'short * _Nonnull':'short *'
31+
// CHECK:STDOUT: |-AlwaysInlineAttr {{0x[a-f0-9]+}} <<invalid sloc>> Implicit always_inline
32+
// CHECK:STDOUT: `-AsmLabelAttr {{0x[a-f0-9]+}} <col:6> Implicit "_Z3foos.carbon_thunk"
33+
34+
// --- import_thunk_required.carbon
35+
36+
library "[[@TEST_NAME]]";
37+
38+
import Cpp library "thunk_required.h";
39+
40+
fn F() {
41+
Cpp.foo(1 as i16);
42+
}

toolchain/driver/compile_subcommand.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ Dump the full SemIR to stdout when built.
208208
)""",
209209
},
210210
[&](auto& arg_b) { arg_b.Set(&dump_sem_ir); });
211+
b.AddFlag(
212+
{
213+
.name = "dump-cpp-ast",
214+
.help = R"""(
215+
Dump the full C++ AST to stdout when built.
216+
)""",
217+
},
218+
[&](auto& arg_b) { arg_b.Set(&dump_cpp_ast); });
211219

212220
b.AddOneOfOption(
213221
{
@@ -399,6 +407,11 @@ auto CompileSubcommand::ValidateOptions(
399407
PhaseToString(options_.phase));
400408
return false;
401409
}
410+
if (options_.dump_cpp_ast) {
411+
emitter.Emit(CompilePhaseFlagConflict, "C++ AST",
412+
PhaseToString(options_.phase));
413+
return false;
414+
}
402415
[[fallthrough]];
403416
case Phase::Check:
404417
if (options_.dump_llvm_ir) {
@@ -1015,11 +1028,15 @@ auto CompileSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
10151028
options.gen_implicit_type_impls = options_.gen_implicit_type_impls;
10161029
options.vlog_stream = driver_env.vlog_stream;
10171030
options.fuzzing = driver_env.fuzzing;
1018-
if (options.vlog_stream || options_.dump_sem_ir || options_.dump_raw_sem_ir) {
1031+
if (options.vlog_stream || options_.dump_sem_ir || options_.dump_cpp_ast ||
1032+
options_.dump_raw_sem_ir) {
10191033
options.include_in_dumps = &cache.include_in_dumps();
10201034
if (options_.dump_sem_ir) {
10211035
options.dump_stream = driver_env.output_stream;
10221036
}
1037+
if (options_.dump_cpp_ast) {
1038+
options.dump_cpp_ast_stream = driver_env.output_stream;
1039+
}
10231040
if (options.vlog_stream || options_.dump_sem_ir) {
10241041
options.dump_sem_ir_ranges = options_.dump_sem_ir_ranges;
10251042
}

toolchain/driver/compile_subcommand.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ struct CompileOptions {
5252
bool dump_parse_tree = false;
5353
bool dump_raw_sem_ir = false;
5454
bool dump_sem_ir = false;
55+
bool dump_cpp_ast = false;
5556
bool dump_llvm_ir = false;
5657
bool dump_asm = false;
5758
bool dump_mem_usage = false;

toolchain/testing/file_test.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,32 @@ auto ToolchainFileTest::GetLineNumberReplacements(
263263
return replacements;
264264
}
265265

266+
// For Clang AST dump lines, we remove references to builtins because they're
267+
// inconsistent between systems and replace the ids since they're inconsistent
268+
// between runs.
269+
static auto DoClangASTCheckReplacements(std::string& check_line) -> void {
270+
static constexpr llvm::StringRef ClangDeclIdRegex = "0x[a-f0-9]+";
271+
static const RE2 is_clang_ast_line_re(
272+
R"(^// CHECK:STDOUT: (TranslationUnitDecl|[ |]*`?\-))");
273+
if (!RE2::PartialMatch(check_line, is_clang_ast_line_re)) {
274+
return;
275+
}
276+
277+
// Filter out references to builtins.
278+
static const RE2 is_builtin_referring_re(
279+
R"(`-BuiltinType |[ ']__[a-zA-Z]|\| `\-PointerType 0x[a-f0-9]+ 'char \*'$)");
280+
if (RE2::PartialMatch(check_line, is_builtin_referring_re)) {
281+
check_line.clear();
282+
return;
283+
}
284+
285+
// Replace the ids.
286+
static const RE2 clang_decl_id_re(llvm::formatv(" {0} ", ClangDeclIdRegex));
287+
static const std::string& clang_decl_id_replacement =
288+
*new std::string(llvm::formatv(" {{{{{0}}} ", ClangDeclIdRegex));
289+
RE2::GlobalReplace(&check_line, clang_decl_id_re, clang_decl_id_replacement);
290+
}
291+
266292
auto ToolchainFileTest::DoExtraCheckReplacements(std::string& check_line) const
267293
-> void {
268294
if (component_ == "driver") {
@@ -286,6 +312,9 @@ auto ToolchainFileTest::DoExtraCheckReplacements(std::string& check_line) const
286312
// package to the VFS with a fixed name.
287313
absl::StrReplaceAll({{data_->installation.core_package(), "{{.*}}"}},
288314
&check_line);
315+
if (component_ == "check") {
316+
DoClangASTCheckReplacements(check_line);
317+
}
289318
} else {
290319
FileTestBase::DoExtraCheckReplacements(check_line);
291320
}

0 commit comments

Comments
 (0)