1 //===-- DWARFASTParserClangTests.cpp --------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
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/YAMLModuleTester.h"
13 #include "gmock/gmock.h"
14 #include "gtest/gtest.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 namespace {
20 class DWARFASTParserClangTests : public testing::Test {};
21 
22 class DWARFASTParserClangStub : public DWARFASTParserClang {
23 public:
24   using DWARFASTParserClang::DWARFASTParserClang;
25   using DWARFASTParserClang::LinkDeclContextToDIE;
26 
27   std::vector<const clang::DeclContext *> GetDeclContextToDIEMapKeys() {
28     std::vector<const clang::DeclContext *> keys;
29     for (const auto &it : m_decl_ctx_to_die)
30       keys.push_back(it.first);
31     return keys;
32   }
33 };
34 } // namespace
35 
36 // If your implementation needs to dereference the dummy pointers we are
37 // defining here, causing this test to fail, feel free to delete it.
38 TEST_F(DWARFASTParserClangTests,
39        EnsureAllDIEsInDeclContextHaveBeenParsedParsesOnlyMatchingEntries) {
40 
41   /// Auxiliary debug info.
42   const char *yamldata =
43       "debug_abbrev:\n"
44       "  - Code:            0x00000001\n"
45       "    Tag:             DW_TAG_compile_unit\n"
46       "    Children:        DW_CHILDREN_yes\n"
47       "    Attributes:\n"
48       "      - Attribute:       DW_AT_language\n"
49       "        Form:            DW_FORM_data2\n"
50       "  - Code:            0x00000002\n"
51       "    Tag:             DW_TAG_base_type\n"
52       "    Children:        DW_CHILDREN_no\n"
53       "    Attributes:\n"
54       "      - Attribute:       DW_AT_encoding\n"
55       "        Form:            DW_FORM_data1\n"
56       "      - Attribute:       DW_AT_byte_size\n"
57       "        Form:            DW_FORM_data1\n"
58       "debug_info:\n"
59       "  - Length:\n"
60       "      TotalLength:     0\n"
61       "    Version:         4\n"
62       "    AbbrOffset:      0\n"
63       "    AddrSize:        8\n"
64       "    Entries:\n"
65       "      - AbbrCode:        0x00000001\n"
66       "        Values:\n"
67       "          - Value:           0x000000000000000C\n"
68       // 0x0000000e:
69       "      - AbbrCode:        0x00000002\n"
70       "        Values:\n"
71       "          - Value:           0x0000000000000007\n" // DW_ATE_unsigned
72       "          - Value:           0x0000000000000004\n"
73       // 0x00000011:
74       "      - AbbrCode:        0x00000002\n"
75       "        Values:\n"
76       "          - Value:           0x0000000000000007\n" // DW_ATE_unsigned
77       "          - Value:           0x0000000000000008\n"
78       // 0x00000014:
79       "      - AbbrCode:        0x00000002\n"
80       "        Values:\n"
81       "          - Value:           0x0000000000000005\n" // DW_ATE_signed
82       "          - Value:           0x0000000000000008\n"
83       // 0x00000017:
84       "      - AbbrCode:        0x00000002\n"
85       "        Values:\n"
86       "          - Value:           0x0000000000000008\n" // DW_ATE_unsigned_char
87       "          - Value:           0x0000000000000001\n"
88       ""
89       "      - AbbrCode:        0x00000000\n"
90       "        Values:          []\n";
91 
92   YAMLModuleTester t(yamldata, "i386-unknown-linux");
93   ASSERT_TRUE((bool)t.GetDwarfUnit());
94 
95   TypeSystemClang ast_ctx("dummy ASTContext", HostInfoBase::GetTargetTriple());
96   DWARFASTParserClangStub ast_parser(ast_ctx);
97 
98   DWARFUnit *unit = t.GetDwarfUnit().get();
99   const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE();
100   const DWARFDebugInfoEntry *die_child0 = die_first->GetFirstChild();
101   const DWARFDebugInfoEntry *die_child1 = die_child0->GetSibling();
102   const DWARFDebugInfoEntry *die_child2 = die_child1->GetSibling();
103   const DWARFDebugInfoEntry *die_child3 = die_child2->GetSibling();
104   std::vector<DWARFDIE> dies = {
105       DWARFDIE(unit, die_child0), DWARFDIE(unit, die_child1),
106       DWARFDIE(unit, die_child2), DWARFDIE(unit, die_child3)};
107   std::vector<clang::DeclContext *> decl_ctxs = {
108       (clang::DeclContext *)1LL, (clang::DeclContext *)2LL,
109       (clang::DeclContext *)2LL, (clang::DeclContext *)3LL};
110   for (int i = 0; i < 4; ++i)
111     ast_parser.LinkDeclContextToDIE(decl_ctxs[i], dies[i]);
112   ast_parser.EnsureAllDIEsInDeclContextHaveBeenParsed(
113       CompilerDeclContext(nullptr, decl_ctxs[1]));
114 
115   EXPECT_THAT(ast_parser.GetDeclContextToDIEMapKeys(),
116               testing::UnorderedElementsAre(decl_ctxs[0], decl_ctxs[3]));
117 }
118 
119