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/DWARFDIE.h"
11 #include "TestingSupport/Symbol/YAMLModuleTester.h"
12 #include "gmock/gmock.h"
13 #include "gtest/gtest.h"
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
18 namespace {
19 class DWARFASTParserClangTests : public testing::Test {};
20 
21 class DWARFASTParserClangStub : public DWARFASTParserClang {
22 public:
23   using DWARFASTParserClang::DWARFASTParserClang;
24   using DWARFASTParserClang::LinkDeclContextToDIE;
25 
26   std::vector<const clang::DeclContext *> GetDeclContextToDIEMapKeys() {
27     std::vector<const clang::DeclContext *> keys;
28     for (const auto &it : m_decl_ctx_to_die)
29       keys.push_back(it.first);
30     return keys;
31   }
32 };
33 } // namespace
34 
35 // If your implementation needs to dereference the dummy pointers we are
36 // defining here, causing this test to fail, feel free to delete it.
37 TEST_F(DWARFASTParserClangTests,
38        EnsureAllDIEsInDeclContextHaveBeenParsedParsesOnlyMatchingEntries) {
39 
40   /// Auxiliary debug info.
41   const char *yamldata =
42       "debug_abbrev:\n"
43       "  - Code:            0x00000001\n"
44       "    Tag:             DW_TAG_compile_unit\n"
45       "    Children:        DW_CHILDREN_yes\n"
46       "    Attributes:\n"
47       "      - Attribute:       DW_AT_language\n"
48       "        Form:            DW_FORM_data2\n"
49       "  - Code:            0x00000002\n"
50       "    Tag:             DW_TAG_base_type\n"
51       "    Children:        DW_CHILDREN_no\n"
52       "    Attributes:\n"
53       "      - Attribute:       DW_AT_encoding\n"
54       "        Form:            DW_FORM_data1\n"
55       "      - Attribute:       DW_AT_byte_size\n"
56       "        Form:            DW_FORM_data1\n"
57       "debug_info:\n"
58       "  - Length:\n"
59       "      TotalLength:     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