1 //===-- DWARFDIETest.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/DWARFDIE.h"
10 #include "TestingSupport/Symbol/YAMLModuleTester.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "gmock/gmock.h"
13 #include "gtest/gtest.h"
14 
15 using namespace lldb;
16 using namespace lldb_private;
17 
TEST(DWARFDIETest,ChildIteration)18 TEST(DWARFDIETest, ChildIteration) {
19   // Tests DWARFDIE::child_iterator.
20 
21   const char *yamldata = R"(
22 --- !ELF
23 FileHeader:
24   Class:   ELFCLASS64
25   Data:    ELFDATA2LSB
26   Type:    ET_EXEC
27   Machine: EM_386
28 DWARF:
29   debug_abbrev:
30     - Table:
31         - Code:            0x00000001
32           Tag:             DW_TAG_compile_unit
33           Children:        DW_CHILDREN_yes
34           Attributes:
35             - Attribute:       DW_AT_language
36               Form:            DW_FORM_data2
37         - Code:            0x00000002
38           Tag:             DW_TAG_base_type
39           Children:        DW_CHILDREN_no
40           Attributes:
41             - Attribute:       DW_AT_encoding
42               Form:            DW_FORM_data1
43             - Attribute:       DW_AT_byte_size
44               Form:            DW_FORM_data1
45   debug_info:
46     - Version:         4
47       AddrSize:        8
48       Entries:
49         - AbbrCode:        0x00000001
50           Values:
51             - Value:           0x000000000000000C
52         - AbbrCode:        0x00000002
53           Values:
54             - Value:           0x0000000000000007 # DW_ATE_unsigned
55             - Value:           0x0000000000000004
56         - AbbrCode:        0x00000002
57           Values:
58             - Value:           0x0000000000000007 # DW_ATE_unsigned
59             - Value:           0x0000000000000008
60         - AbbrCode:        0x00000002
61           Values:
62             - Value:           0x0000000000000005 # DW_ATE_signed
63             - Value:           0x0000000000000008
64         - AbbrCode:        0x00000000
65 )";
66 
67   YAMLModuleTester t(yamldata);
68   ASSERT_TRUE((bool)t.GetDwarfUnit());
69 
70   DWARFUnit *unit = t.GetDwarfUnit();
71   const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE();
72 
73   // Create a DWARFDIE that has three DW_TAG_base_type children.
74   DWARFDIE top_die(unit, die_first);
75 
76   // Create the iterator range that has the three tags as elements.
77   llvm::iterator_range<DWARFDIE::child_iterator> children = top_die.children();
78 
79   // Compare begin() to the first child DIE.
80   DWARFDIE::child_iterator child_iter = children.begin();
81   ASSERT_NE(child_iter, children.end());
82   const DWARFDebugInfoEntry *die_child0 = die_first->GetFirstChild();
83   EXPECT_EQ((*child_iter).GetDIE(), die_child0);
84 
85   // Step to the second child DIE.
86   ++child_iter;
87   ASSERT_NE(child_iter, children.end());
88   const DWARFDebugInfoEntry *die_child1 = die_child0->GetSibling();
89   EXPECT_EQ((*child_iter).GetDIE(), die_child1);
90 
91   // Step to the third child DIE.
92   ++child_iter;
93   ASSERT_NE(child_iter, children.end());
94   const DWARFDebugInfoEntry *die_child2 = die_child1->GetSibling();
95   EXPECT_EQ((*child_iter).GetDIE(), die_child2);
96 
97   // Step to the end of the range.
98   ++child_iter;
99   EXPECT_EQ(child_iter, children.end());
100 
101   // Take one of the DW_TAG_base_type DIEs (which has no children) and make
102   // sure the children range is now empty.
103   DWARFDIE no_children_die(unit, die_child0);
104   EXPECT_TRUE(no_children_die.children().empty());
105 }
106