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:          0\n"
60       "    Version:         4\n"
61       "    AbbrOffset:      0\n"
62       "    AddrSize:        8\n"
63       "    Entries:\n"
64       "      - AbbrCode:        0x00000001\n"
65       "        Values:\n"
66       "          - Value:           0x000000000000000C\n"
67       // 0x0000000e:
68       "      - AbbrCode:        0x00000002\n"
69       "        Values:\n"
70       "          - Value:           0x0000000000000007\n" // DW_ATE_unsigned
71       "          - Value:           0x0000000000000004\n"
72       // 0x00000011:
73       "      - AbbrCode:        0x00000002\n"
74       "        Values:\n"
75       "          - Value:           0x0000000000000007\n" // DW_ATE_unsigned
76       "          - Value:           0x0000000000000008\n"
77       // 0x00000014:
78       "      - AbbrCode:        0x00000002\n"
79       "        Values:\n"
80       "          - Value:           0x0000000000000005\n" // DW_ATE_signed
81       "          - Value:           0x0000000000000008\n"
82       // 0x00000017:
83       "      - AbbrCode:        0x00000002\n"
84       "        Values:\n"
85       "          - Value:           0x0000000000000008\n" // DW_ATE_unsigned_char
86       "          - Value:           0x0000000000000001\n"
87       ""
88       "      - AbbrCode:        0x00000000\n"
89       "        Values:          []\n";
90 
91   YAMLModuleTester t(yamldata, "i386-unknown-linux");
92   ASSERT_TRUE((bool)t.GetDwarfUnit());
93 
94   TypeSystemClang ast_ctx("dummy ASTContext", HostInfoBase::GetTargetTriple());
95   DWARFASTParserClangStub ast_parser(ast_ctx);
96 
97   DWARFUnit *unit = t.GetDwarfUnit().get();
98   const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE();
99   const DWARFDebugInfoEntry *die_child0 = die_first->GetFirstChild();
100   const DWARFDebugInfoEntry *die_child1 = die_child0->GetSibling();
101   const DWARFDebugInfoEntry *die_child2 = die_child1->GetSibling();
102   const DWARFDebugInfoEntry *die_child3 = die_child2->GetSibling();
103   std::vector<DWARFDIE> dies = {
104       DWARFDIE(unit, die_child0), DWARFDIE(unit, die_child1),
105       DWARFDIE(unit, die_child2), DWARFDIE(unit, die_child3)};
106   std::vector<clang::DeclContext *> decl_ctxs = {
107       (clang::DeclContext *)1LL, (clang::DeclContext *)2LL,
108       (clang::DeclContext *)2LL, (clang::DeclContext *)3LL};
109   for (int i = 0; i < 4; ++i)
110     ast_parser.LinkDeclContextToDIE(decl_ctxs[i], dies[i]);
111   ast_parser.EnsureAllDIEsInDeclContextHaveBeenParsed(
112       CompilerDeclContext(nullptr, decl_ctxs[1]));
113 
114   EXPECT_THAT(ast_parser.GetDeclContextToDIEMapKeys(),
115               testing::UnorderedElementsAre(decl_ctxs[0], decl_ctxs[3]));
116 }
117 
118