1 //===-- DWARFASTParserClangTests.cpp --------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
10 #include "Plugins/SymbolFile/DWARF/DWARFCompileUnit.h"
11 #include "Plugins/SymbolFile/DWARF/DWARFDIE.h"
12 #include "TestingSupport/Symbol/ClangTestUtils.h"
13 #include "TestingSupport/Symbol/YAMLModuleTester.h"
14 #include "lldb/Core/Debugger.h"
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
19 using namespace lldb_private
;
20 using namespace lldb_private::dwarf
;
21 using namespace lldb_private::plugin::dwarf
;
24 static std::once_flag debugger_initialize_flag
;
26 class DWARFASTParserClangTests
: public testing::Test
{
27 void SetUp() override
{
28 std::call_once(debugger_initialize_flag
,
29 []() { Debugger::Initialize(nullptr); });
33 class DWARFASTParserClangStub
: public DWARFASTParserClang
{
35 using DWARFASTParserClang::DWARFASTParserClang
;
36 using DWARFASTParserClang::LinkDeclContextToDIE
;
38 std::vector
<const clang::DeclContext
*> GetDeclContextToDIEMapKeys() {
39 std::vector
<const clang::DeclContext
*> keys
;
40 for (const auto &it
: m_decl_ctx_to_die
)
41 keys
.push_back(it
.first
);
47 // If your implementation needs to dereference the dummy pointers we are
48 // defining here, causing this test to fail, feel free to delete it.
49 TEST_F(DWARFASTParserClangTests
,
50 EnsureAllDIEsInDeclContextHaveBeenParsedParsesOnlyMatchingEntries
) {
52 /// Auxiliary debug info.
53 const char *yamldata
= R
"(
64 Tag: DW_TAG_compile_unit
65 Children: DW_CHILDREN_yes
67 - Attribute: DW_AT_language
71 Children: DW_CHILDREN_no
73 - Attribute: DW_AT_encoding
75 - Attribute: DW_AT_byte_size
81 - AbbrCode: 0x00000001
83 - Value: 0x000000000000000C
84 - AbbrCode: 0x00000002
86 - Value: 0x0000000000000007 # DW_ATE_unsigned
87 - Value: 0x0000000000000004
88 - AbbrCode: 0x00000002
90 - Value: 0x0000000000000007 # DW_ATE_unsigned
91 - Value: 0x0000000000000008
92 - AbbrCode: 0x00000002
94 - Value: 0x0000000000000005 # DW_ATE_signed
95 - Value: 0x0000000000000008
96 - AbbrCode: 0x00000002
98 - Value: 0x0000000000000008 # DW_ATE_unsigned_char
99 - Value: 0x0000000000000001
100 - AbbrCode: 0x00000000
103 YAMLModuleTester
t(yamldata
);
104 ASSERT_TRUE((bool)t
.GetDwarfUnit());
106 auto holder
= std::make_unique
<clang_utils::TypeSystemClangHolder
>("ast");
107 auto &ast_ctx
= *holder
->GetAST();
109 DWARFASTParserClangStub
ast_parser(ast_ctx
);
111 DWARFUnit
*unit
= t
.GetDwarfUnit();
112 const DWARFDebugInfoEntry
*die_first
= unit
->DIE().GetDIE();
113 const DWARFDebugInfoEntry
*die_child0
= die_first
->GetFirstChild();
114 const DWARFDebugInfoEntry
*die_child1
= die_child0
->GetSibling();
115 const DWARFDebugInfoEntry
*die_child2
= die_child1
->GetSibling();
116 const DWARFDebugInfoEntry
*die_child3
= die_child2
->GetSibling();
117 std::vector
<DWARFDIE
> dies
= {
118 DWARFDIE(unit
, die_child0
), DWARFDIE(unit
, die_child1
),
119 DWARFDIE(unit
, die_child2
), DWARFDIE(unit
, die_child3
)};
120 std::vector
<clang::DeclContext
*> decl_ctxs
= {
121 (clang::DeclContext
*)1LL, (clang::DeclContext
*)2LL,
122 (clang::DeclContext
*)2LL, (clang::DeclContext
*)3LL};
123 for (int i
= 0; i
< 4; ++i
)
124 ast_parser
.LinkDeclContextToDIE(decl_ctxs
[i
], dies
[i
]);
125 ast_parser
.EnsureAllDIEsInDeclContextHaveBeenParsed(
126 CompilerDeclContext(nullptr, decl_ctxs
[1]));
128 EXPECT_THAT(ast_parser
.GetDeclContextToDIEMapKeys(),
129 testing::UnorderedElementsAre(decl_ctxs
[0], decl_ctxs
[3]));
132 TEST_F(DWARFASTParserClangTests
, TestCallingConventionParsing
) {
133 // Tests parsing DW_AT_calling_convention values.
135 // The DWARF below just declares a list of function types with
136 // DW_AT_calling_convention on them.
137 const char *yamldata
= R
"(
159 Tag: DW_TAG_compile_unit
160 Children: DW_CHILDREN_yes
162 - Attribute: DW_AT_language
165 Tag: DW_TAG_subprogram
166 Children: DW_CHILDREN_no
168 - Attribute: DW_AT_low_pc
170 - Attribute: DW_AT_high_pc
172 - Attribute: DW_AT_name
174 - Attribute: DW_AT_calling_convention
176 - Attribute: DW_AT_external
177 Form: DW_FORM_flag_present
251 YAMLModuleTester
t(yamldata
);
253 DWARFUnit
*unit
= t
.GetDwarfUnit();
254 ASSERT_NE(unit
, nullptr);
255 const DWARFDebugInfoEntry
*cu_entry
= unit
->DIE().GetDIE();
256 ASSERT_EQ(cu_entry
->Tag(), DW_TAG_compile_unit
);
257 DWARFDIE
cu_die(unit
, cu_entry
);
259 auto holder
= std::make_unique
<clang_utils::TypeSystemClangHolder
>("ast");
260 auto &ast_ctx
= *holder
->GetAST();
261 DWARFASTParserClangStub
ast_parser(ast_ctx
);
263 std::vector
<std::string
> found_function_types
;
264 // The DWARF above is just a list of functions. Parse all of them to
265 // extract the function types and their calling convention values.
266 for (DWARFDIE func
: cu_die
.children()) {
267 ASSERT_EQ(func
.Tag(), DW_TAG_subprogram
);
269 bool new_type
= false;
270 lldb::TypeSP type
= ast_parser
.ParseTypeFromDWARF(sc
, func
, &new_type
);
271 found_function_types
.push_back(
272 type
->GetForwardCompilerType().GetTypeName().AsCString());
275 // Compare the parsed function types against the expected list of types.
276 const std::vector
<std::string
> expected_function_types
= {
277 "void () __attribute__((regcall))",
278 "void () __attribute__((fastcall))",
279 "void () __attribute__((stdcall))",
280 "void () __attribute__((vectorcall))",
281 "void () __attribute__((pascal))",
282 "void () __attribute__((ms_abi))",
283 "void () __attribute__((sysv_abi))",
284 "void ()", // invalid calling convention.
285 "void ()", // DW_CC_normal -> no attribute
287 ASSERT_EQ(found_function_types
, expected_function_types
);
290 TEST_F(DWARFASTParserClangTests
, TestPtrAuthParsing
) {
291 // Tests parsing values with type DW_TAG_LLVM_ptrauth_type corresponding to
292 // explicitly signed raw function pointers
294 // This is Dwarf for the following C code:
296 // void (*__ptrauth(0, 0, 42) a)();
299 const char *yamldata
= R
"(
313 Tag: DW_TAG_compile_unit
314 Children: DW_CHILDREN_yes
316 - Attribute: DW_AT_language
320 Children: DW_CHILDREN_no
322 - Attribute: DW_AT_name
324 - Attribute: DW_AT_type
326 - Attribute: DW_AT_external
327 Form: DW_FORM_flag_present
329 Tag: DW_TAG_LLVM_ptrauth_type
330 Children: DW_CHILDREN_no
332 - Attribute: DW_AT_type
334 - Attribute: DW_AT_LLVM_ptrauth_key
336 - Attribute: DW_AT_LLVM_ptrauth_extra_discriminator
339 Tag: DW_TAG_pointer_type
340 Children: DW_CHILDREN_no
342 - Attribute: DW_AT_type
345 Tag: DW_TAG_subroutine_type
346 Children: DW_CHILDREN_yes
348 Tag: DW_TAG_unspecified_parameters
349 Children: DW_CHILDREN_no
353 UnitType: DW_UT_compile
356 # 0x0c: DW_TAG_compile_unit
357 # DW_AT_language [DW_FORM_data2] (DW_LANG_C99)
362 # 0x0f: DW_TAG_variable
363 # DW_AT_name [DW_FORM_strp] (\"a
\")
364 # DW_AT_type [DW_FORM_ref4] (0x00000018 \"void (*__ptrauth(0, 0, 0x02a)\")
365 # DW_AT_external [DW_FORM_flag_present] (true)
371 # 0x18: DW_TAG_LLVM_ptrauth_type
372 # DW_AT_type [DW_FORM_ref4] (0x00000020 \"void (*)(...)\")
373 # DW_AT_LLVM_ptrauth_key [DW_FORM_data1] (0x00)
374 # DW_AT_LLVM_ptrauth_extra_discriminator [DW_FORM_data2] (0x002a)
381 # 0x20: DW_TAG_pointer_type
382 # DW_AT_type [DW_AT_type [DW_FORM_ref4] (0x00000025 \"void (...)\")
387 # 0x25: DW_TAG_subroutine_type
390 # 0x26: DW_TAG_unspecified_parameters
393 - AbbrCode
: 0x00 # end of child tags of 0x25
394 - AbbrCode
: 0x00 # end of child tags of 0x0c
397 YAMLModuleTester t(yamldata);
399 DWARFUnit *unit = t.GetDwarfUnit();
400 ASSERT_NE(unit, nullptr);
401 const DWARFDebugInfoEntry *cu_entry = unit->DIE().GetDIE();
402 ASSERT_EQ(cu_entry->Tag(), DW_TAG_compile_unit);
403 DWARFDIE cu_die(unit, cu_entry);
405 auto holder = std::make_unique<clang_utils::TypeSystemClangHolder>("ast
");
406 auto &ast_ctx = *holder->GetAST();
407 DWARFASTParserClangStub ast_parser(ast_ctx);
409 DWARFDIE ptrauth_variable = cu_die.GetFirstChild();
410 ASSERT_EQ(ptrauth_variable.Tag(), DW_TAG_variable);
411 DWARFDIE ptrauth_type =
412 ptrauth_variable.GetAttributeValueAsReferenceDIE(DW_AT_type);
413 ASSERT_EQ(ptrauth_type.Tag(), DW_TAG_LLVM_ptrauth_type);
416 bool new_type = false;
417 lldb::TypeSP type_sp =
418 ast_parser.ParseTypeFromDWARF(sc, ptrauth_type, &new_type);
419 CompilerType compiler_type = type_sp->GetForwardCompilerType();
420 ASSERT_EQ(compiler_type.GetPtrAuthKey(), 0U);
421 ASSERT_EQ(compiler_type.GetPtrAuthAddressDiversity(), false);
422 ASSERT_EQ(compiler_type.GetPtrAuthDiscriminator(), 42U);
425 struct ExtractIntFromFormValueTest : public testing::Test {
426 SubsystemRAII<FileSystem, HostInfo> subsystems;
427 clang_utils::TypeSystemClangHolder holder;
430 DWARFASTParserClang parser;
431 ExtractIntFromFormValueTest()
432 : holder("dummy ASTContext
"), ts(*holder.GetAST()), parser(ts) {}
434 /// Takes the given integer value, stores it in a DWARFFormValue and then
435 /// tries to extract the value back via
436 /// DWARFASTParserClang::ExtractIntFromFormValue.
437 /// Returns the string representation of the extracted value or the error
438 /// that was returned from ExtractIntFromFormValue.
439 llvm::Expected<std::string> Extract(clang::QualType qt, uint64_t value) {
440 DWARFFormValue form_value;
441 form_value.SetUnsigned(value);
442 llvm::Expected<llvm::APInt> result =
443 parser.ExtractIntFromFormValue(ts.GetType(qt), form_value);
445 return result.takeError();
446 llvm::SmallString<16> result_str;
447 result->toStringUnsigned(result_str);
448 return std::string(result_str.str());
451 /// Same as ExtractIntFromFormValueTest::Extract but takes a signed integer
452 /// and treats the result as a signed integer.
453 llvm::Expected<std::string> ExtractS(clang::QualType qt, int64_t value) {
454 DWARFFormValue form_value;
455 form_value.SetSigned(value);
456 llvm::Expected<llvm::APInt> result =
457 parser.ExtractIntFromFormValue(ts.GetType(qt), form_value);
459 return result.takeError();
460 llvm::SmallString<16> result_str;
461 result->toStringSigned(result_str);
462 return std::string(result_str.str());
466 TEST_F(ExtractIntFromFormValueTest, TestBool) {
467 using namespace llvm;
468 clang::ASTContext &ast = ts.getASTContext();
470 EXPECT_THAT_EXPECTED(Extract(ast.BoolTy, 0), HasValue("0"));
471 EXPECT_THAT_EXPECTED(Extract(ast.BoolTy, 1), HasValue("1"));
472 EXPECT_THAT_EXPECTED(Extract(ast.BoolTy, 2), Failed());
473 EXPECT_THAT_EXPECTED(Extract(ast.BoolTy, 3), Failed());
476 TEST_F(ExtractIntFromFormValueTest, TestInt) {
477 using namespace llvm;
479 clang::ASTContext &ast = ts.getASTContext();
481 // Find the min/max values for 'int' on the current host target.
482 constexpr int64_t int_max = std::numeric_limits<int>::max();
483 constexpr int64_t int_min = std::numeric_limits<int>::min();
485 // Check that the bit width of int matches the int width in our type system.
486 ASSERT_EQ(sizeof(int) * 8, ast.getIntWidth(ast.IntTy));
488 // Check values around int_min.
489 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_min - 2), llvm::Failed());
490 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_min - 1), llvm::Failed());
491 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_min),
492 HasValue(std::to_string(int_min)));
493 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_min + 1),
494 HasValue(std::to_string(int_min + 1)));
495 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_min + 2),
496 HasValue(std::to_string(int_min + 2)));
498 // Check values around 0.
499 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, -128), HasValue("-128"));
500 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, -10), HasValue("-10"));
501 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, -1), HasValue("-1"));
502 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, 0), HasValue("0"));
503 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, 1), HasValue("1"));
504 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, 10), HasValue("10"));
505 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, 128), HasValue("128"));
507 // Check values around int_max.
508 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_max - 2),
509 HasValue(std::to_string(int_max - 2)));
510 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_max - 1),
511 HasValue(std::to_string(int_max - 1)));
512 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_max),
513 HasValue(std::to_string(int_max)));
514 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_max + 1), llvm::Failed());
515 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_max + 5), llvm::Failed());
517 // Check some values not near an edge case.
518 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_max / 2),
519 HasValue(std::to_string(int_max / 2)));
520 EXPECT_THAT_EXPECTED(ExtractS(ast.IntTy, int_min / 2),
521 HasValue(std::to_string(int_min / 2)));
524 TEST_F(ExtractIntFromFormValueTest, TestUnsignedInt) {
525 using namespace llvm;
527 clang::ASTContext &ast = ts.getASTContext();
528 constexpr uint64_t uint_max = std::numeric_limits<uint32_t>::max();
530 // Check values around 0.
531 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, 0), HasValue("0"));
532 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, 1), HasValue("1"));
533 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, 1234), HasValue("1234"));
535 // Check some values not near an edge case.
536 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, uint_max / 2),
537 HasValue(std::to_string(uint_max / 2)));
539 // Check values around uint_max.
540 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, uint_max - 2),
541 HasValue(std::to_string(uint_max - 2)));
542 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, uint_max - 1),
543 HasValue(std::to_string(uint_max - 1)));
544 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, uint_max),
545 HasValue(std::to_string(uint_max)));
546 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, uint_max + 1),
548 EXPECT_THAT_EXPECTED(Extract(ast.UnsignedIntTy, uint_max + 2),
552 TEST_F(DWARFASTParserClangTests, TestDefaultTemplateParamParsing) {
553 // Tests parsing DW_AT_default_value for template parameters.
554 auto BufferOrError = llvm::MemoryBuffer::getFile(
555 GetInputFilePath("DW_AT_default_value
-test
.yaml
"), /*IsText=*/true);
556 ASSERT_TRUE(BufferOrError);
557 YAMLModuleTester t(BufferOrError.get()->getBuffer());
559 DWARFUnit *unit = t.GetDwarfUnit();
560 ASSERT_NE(unit, nullptr);
561 const DWARFDebugInfoEntry *cu_entry = unit->DIE().GetDIE();
562 ASSERT_EQ(cu_entry->Tag(), DW_TAG_compile_unit);
563 DWARFDIE cu_die(unit, cu_entry);
565 auto holder = std::make_unique<clang_utils::TypeSystemClangHolder>("ast
");
566 auto &ast_ctx = *holder->GetAST();
567 DWARFASTParserClangStub ast_parser(ast_ctx);
569 llvm::SmallVector<lldb::TypeSP, 2> types;
570 for (DWARFDIE die : cu_die.children()) {
571 if (die.Tag() == DW_TAG_class_type) {
573 bool new_type = false;
574 types.push_back(ast_parser.ParseTypeFromDWARF(sc, die, &new_type));
578 ASSERT_EQ(types.size(), 3U);
580 auto check_decl = [](auto const *decl) {
581 clang::ClassTemplateSpecializationDecl const *ctsd =
582 llvm::dyn_cast_or_null<clang::ClassTemplateSpecializationDecl>(decl);
583 ASSERT_NE(ctsd, nullptr);
585 auto const &args = ctsd->getTemplateArgs();
586 ASSERT_GT(args.size(), 0U);
588 for (auto const &arg : args.asArray()) {
589 EXPECT_TRUE(arg.getIsDefaulted());
593 for (auto const &type_sp : types) {
594 ASSERT_NE(type_sp, nullptr);
595 auto const *decl = ClangUtil::GetAsTagDecl(type_sp->GetFullCompilerType());
596 if (decl->getName() == "bar
" || decl->getName() == "baz
") {