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 = R"(
43 --- !ELF
44 FileHeader:
45   Class:   ELFCLASS64
46   Data:    ELFDATA2LSB
47   Type:    ET_EXEC
48   Machine: EM_386
49 DWARF:
50   debug_abbrev:
51     - Table:
52         - Code:            0x00000001
53           Tag:             DW_TAG_compile_unit
54           Children:        DW_CHILDREN_yes
55           Attributes:
56             - Attribute:       DW_AT_language
57               Form:            DW_FORM_data2
58         - Code:            0x00000002
59           Tag:             DW_TAG_base_type
60           Children:        DW_CHILDREN_no
61           Attributes:
62             - Attribute:       DW_AT_encoding
63               Form:            DW_FORM_data1
64             - Attribute:       DW_AT_byte_size
65               Form:            DW_FORM_data1
66   debug_info:
67     - Version:         4
68       AddrSize:        8
69       Entries:
70         - AbbrCode:        0x00000001
71           Values:
72             - Value:           0x000000000000000C
73         - AbbrCode:        0x00000002
74           Values:
75             - Value:           0x0000000000000007 # DW_ATE_unsigned
76             - Value:           0x0000000000000004
77         - AbbrCode:        0x00000002
78           Values:
79             - Value:           0x0000000000000007 # DW_ATE_unsigned
80             - Value:           0x0000000000000008
81         - AbbrCode:        0x00000002
82           Values:
83             - Value:           0x0000000000000005 # DW_ATE_signed
84             - Value:           0x0000000000000008
85         - AbbrCode:        0x00000002
86           Values:
87             - Value:           0x0000000000000008 # DW_ATE_unsigned_char
88             - Value:           0x0000000000000001
89         - AbbrCode:        0x00000000
90 )";
91 
92   YAMLModuleTester t(yamldata);
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();
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