]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/lang/c/anonymous/TestAnonymous.py
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / lang / c / anonymous / TestAnonymous.py
1 """Test that anonymous structs/unions are transparent to member access"""
2
3 from __future__ import print_function
4
5
6 import os
7 import time
8 import lldb
9 from lldbsuite.test.decorators import *
10 from lldbsuite.test.lldbtest import *
11 from lldbsuite.test import lldbutil
12
13
14 class AnonymousTestCase(TestBase):
15
16     mydir = TestBase.compute_mydir(__file__)
17
18     @skipIf(
19         compiler="icc",
20         bugnumber="llvm.org/pr15036: LLDB generates an incorrect AST layout for an anonymous struct when DWARF is generated by ICC")
21     def test_expr_nest(self):
22         self.build()
23         self.common_setup(self.line0)
24
25         # These should display correctly.
26         self.expect("expression n->foo.d", VARIABLES_DISPLAYED_CORRECTLY,
27                     substrs=["= 4"])
28
29         self.expect("expression n->b", VARIABLES_DISPLAYED_CORRECTLY,
30                     substrs=["= 2"])
31
32     def test_expr_child(self):
33         self.build()
34         self.common_setup(self.line1)
35
36         # These should display correctly.
37         self.expect("expression c->foo.d", VARIABLES_DISPLAYED_CORRECTLY,
38                     substrs=["= 4"])
39
40         self.expect(
41             "expression c->grandchild.b",
42             VARIABLES_DISPLAYED_CORRECTLY,
43             substrs=["= 2"])
44
45     @skipIf(
46         compiler="icc",
47         bugnumber="llvm.org/pr15036: This particular regression was introduced by r181498")
48     def test_expr_grandchild(self):
49         self.build()
50         self.common_setup(self.line2)
51
52         # These should display correctly.
53         self.expect("expression g.child.foo.d", VARIABLES_DISPLAYED_CORRECTLY,
54                     substrs=["= 4"])
55
56         self.expect("expression g.child.b", VARIABLES_DISPLAYED_CORRECTLY,
57                     substrs=["= 2"])
58
59     def test_expr_parent(self):
60         self.build()
61         if "clang" in self.getCompiler() and "3.4" in self.getCompilerVersion():
62             self.skipTest(
63                 "llvm.org/pr16214 -- clang emits partial DWARF for structures referenced via typedef")
64         self.common_setup(self.line2)
65
66         # These should display correctly.
67         self.expect("expression pz", VARIABLES_DISPLAYED_CORRECTLY,
68                     substrs=["(type_z *) $", " = 0x0000"])
69
70         self.expect("expression z.y", VARIABLES_DISPLAYED_CORRECTLY,
71                     substrs=["(type_y) $", "dummy = 2"])
72
73     @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr21550")
74     def test_expr_null(self):
75         self.build()
76         self.common_setup(self.line2)
77
78         # This should fail because pz is 0, but it succeeds on OS/X.
79         # This fails on Linux with an upstream error "Couldn't dematerialize struct", as does "p *n" with "int *n = 0".
80         # Note that this can also trigger llvm.org/pr15036 when run
81         # interactively at the lldb command prompt.
82         self.expect("expression *(type_z *)pz", error=True)
83
84     def test_child_by_name(self):
85         self.build()
86
87         # Set debugger into synchronous mode
88         self.dbg.SetAsync(False)
89
90         # Create a target
91         exe = os.path.join(os.getcwd(), "a.out")
92         target = self.dbg.CreateTarget(exe)
93         self.assertTrue(target, VALID_TARGET)
94
95         break_in_main = target.BreakpointCreateBySourceRegex(
96             '// Set breakpoint 2 here.', lldb.SBFileSpec(self.source))
97         self.assertTrue(break_in_main, VALID_BREAKPOINT)
98
99         process = target.LaunchSimple(
100             None, None, self.get_process_working_directory())
101         self.assertTrue(process, PROCESS_IS_VALID)
102
103         threads = lldbutil.get_threads_stopped_at_breakpoint(
104             process, break_in_main)
105         if len(threads) != 1:
106             self.fail("Failed to stop at breakpoint in main.")
107
108         thread = threads[0]
109         frame = thread.frames[0]
110
111         if not frame.IsValid():
112             self.fail("Failed to get frame 0.")
113
114         var_n = frame.FindVariable("n")
115         if not var_n.IsValid():
116             self.fail("Failed to get the variable 'n'")
117
118         elem_a = var_n.GetChildMemberWithName("a")
119         if not elem_a.IsValid():
120             self.fail("Failed to get the element a in n")
121
122         error = lldb.SBError()
123         value = elem_a.GetValueAsSigned(error, 1000)
124         if not error.Success() or value != 0:
125             self.fail("failed to get the correct value for element a in n")
126
127     def test_nest_flat(self):
128         self.build()
129         self.common_setup(self.line2)
130
131         # These should display correctly.
132         self.expect('frame variable n --flat',
133                     substrs=['n.a = 0',
134                              'n.b = 2',
135                              'n.foo.c = 0',
136                              'n.foo.d = 4'])
137
138     def setUp(self):
139         # Call super's setUp().
140         TestBase.setUp(self)
141         # Find the line numbers to break in main.c.
142         self.source = 'main.c'
143         self.line0 = line_number(self.source, '// Set breakpoint 0 here.')
144         self.line1 = line_number(self.source, '// Set breakpoint 1 here.')
145         self.line2 = line_number(self.source, '// Set breakpoint 2 here.')
146
147     def common_setup(self, line):
148
149         # Set debugger into synchronous mode
150         self.dbg.SetAsync(False)
151
152         # Create a target
153         exe = os.path.join(os.getcwd(), "a.out")
154         target = self.dbg.CreateTarget(exe)
155         self.assertTrue(target, VALID_TARGET)
156
157         # Set breakpoints inside and outside methods that take pointers to the
158         # containing struct.
159         lldbutil.run_break_set_by_file_and_line(
160             self, self.source, line, num_expected_locations=1, loc_exact=True)
161
162         # Now launch the process, and do not stop at entry point.
163         process = target.LaunchSimple(
164             None, None, self.get_process_working_directory())
165         self.assertTrue(process, PROCESS_IS_VALID)
166
167         # The stop reason of the thread should be breakpoint.
168         self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
169                     substrs=['stopped',
170                              'stop reason = breakpoint'])
171
172         # The breakpoint should have a hit count of 1.
173         self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
174                     substrs=[' resolved, hit count = 1'])