1 """Test that anonymous structs/unions are transparent to member access"""
3 from __future__ import print_function
9 from lldbsuite.test.decorators import *
10 from lldbsuite.test.lldbtest import *
11 from lldbsuite.test import lldbutil
14 class AnonymousTestCase(TestBase):
16 mydir = TestBase.compute_mydir(__file__)
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):
23 self.common_setup(self.line0)
25 # These should display correctly.
26 self.expect("expression n->foo.d", VARIABLES_DISPLAYED_CORRECTLY,
29 self.expect("expression n->b", VARIABLES_DISPLAYED_CORRECTLY,
32 def test_expr_child(self):
34 self.common_setup(self.line1)
36 # These should display correctly.
37 self.expect("expression c->foo.d", VARIABLES_DISPLAYED_CORRECTLY,
41 "expression c->grandchild.b",
42 VARIABLES_DISPLAYED_CORRECTLY,
47 bugnumber="llvm.org/pr15036: This particular regression was introduced by r181498")
48 def test_expr_grandchild(self):
50 self.common_setup(self.line2)
52 # These should display correctly.
53 self.expect("expression g.child.foo.d", VARIABLES_DISPLAYED_CORRECTLY,
56 self.expect("expression g.child.b", VARIABLES_DISPLAYED_CORRECTLY,
59 def test_expr_parent(self):
61 if "clang" in self.getCompiler() and "3.4" in self.getCompilerVersion():
63 "llvm.org/pr16214 -- clang emits partial DWARF for structures referenced via typedef")
64 self.common_setup(self.line2)
66 # These should display correctly.
67 self.expect("expression pz", VARIABLES_DISPLAYED_CORRECTLY,
68 substrs=["(type_z *) $", " = 0x0000"])
70 self.expect("expression z.y", VARIABLES_DISPLAYED_CORRECTLY,
71 substrs=["(type_y) $", "dummy = 2"])
73 @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr21550")
74 def test_expr_null(self):
76 self.common_setup(self.line2)
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)
84 def test_child_by_name(self):
87 # Set debugger into synchronous mode
88 self.dbg.SetAsync(False)
91 exe = os.path.join(os.getcwd(), "a.out")
92 target = self.dbg.CreateTarget(exe)
93 self.assertTrue(target, VALID_TARGET)
95 break_in_main = target.BreakpointCreateBySourceRegex(
96 '// Set breakpoint 2 here.', lldb.SBFileSpec(self.source))
97 self.assertTrue(break_in_main, VALID_BREAKPOINT)
99 process = target.LaunchSimple(
100 None, None, self.get_process_working_directory())
101 self.assertTrue(process, PROCESS_IS_VALID)
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.")
109 frame = thread.frames[0]
111 if not frame.IsValid():
112 self.fail("Failed to get frame 0.")
114 var_n = frame.FindVariable("n")
115 if not var_n.IsValid():
116 self.fail("Failed to get the variable 'n'")
118 elem_a = var_n.GetChildMemberWithName("a")
119 if not elem_a.IsValid():
120 self.fail("Failed to get the element a in n")
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")
127 def test_nest_flat(self):
129 self.common_setup(self.line2)
131 # These should display correctly.
132 self.expect('frame variable n --flat',
139 # Call super's setUp().
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.')
147 def common_setup(self, line):
149 # Set debugger into synchronous mode
150 self.dbg.SetAsync(False)
153 exe = os.path.join(os.getcwd(), "a.out")
154 target = self.dbg.CreateTarget(exe)
155 self.assertTrue(target, VALID_TARGET)
157 # Set breakpoints inside and outside methods that take pointers to the
159 lldbutil.run_break_set_by_file_and_line(
160 self, self.source, line, num_expected_locations=1, loc_exact=True)
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)
167 # The stop reason of the thread should be breakpoint.
168 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
170 'stop reason = breakpoint'])
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'])