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