1 """Test that anonymous structs/unions are transparent to member access"""
3 from __future__ import print_function
9 from lldbsuite.test.lldbtest import *
10 import lldbsuite.test.lldbutil as lldbutil
12 class AnonymousTestCase(TestBase):
14 mydir = TestBase.compute_mydir(__file__)
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):
19 self.common_setup(self.line0)
21 # These should display correctly.
22 self.expect("expression n->foo.d", VARIABLES_DISPLAYED_CORRECTLY,
25 self.expect("expression n->b", VARIABLES_DISPLAYED_CORRECTLY,
28 def test_expr_child(self):
30 self.common_setup(self.line1)
32 # These should display correctly.
33 self.expect("expression c->foo.d", VARIABLES_DISPLAYED_CORRECTLY,
36 self.expect("expression c->grandchild.b", VARIABLES_DISPLAYED_CORRECTLY,
39 @skipIfIcc # llvm.org/pr15036: This particular regression was introduced by r181498
40 def test_expr_grandchild(self):
42 self.common_setup(self.line2)
44 # These should display correctly.
45 self.expect("expression g.child.foo.d", VARIABLES_DISPLAYED_CORRECTLY,
48 self.expect("expression g.child.b", VARIABLES_DISPLAYED_CORRECTLY,
51 def test_expr_parent(self):
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)
57 # These should display correctly.
58 self.expect("expression pz", VARIABLES_DISPLAYED_CORRECTLY,
59 substrs = ["(type_z *) $", " = 0x0000"])
61 self.expect("expression z.y", VARIABLES_DISPLAYED_CORRECTLY,
62 substrs = ["(type_y) $", "dummy = 2"])
64 @expectedFailureWindows('llvm.org/pr21550')
65 def test_expr_null(self):
67 self.common_setup(self.line2)
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)
74 def test_child_by_name(self):
77 # Set debugger into synchronous mode
78 self.dbg.SetAsync(False)
81 exe = os.path.join (os.getcwd(), "a.out")
82 target = self.dbg.CreateTarget(exe)
83 self.assertTrue(target, VALID_TARGET)
85 break_in_main = target.BreakpointCreateBySourceRegex ('// Set breakpoint 2 here.', lldb.SBFileSpec(self.source))
86 self.assertTrue(break_in_main, VALID_BREAKPOINT)
88 process = target.LaunchSimple (None, None, self.get_process_working_directory())
89 self.assertTrue (process, PROCESS_IS_VALID)
91 threads = lldbutil.get_threads_stopped_at_breakpoint (process, break_in_main)
93 self.fail ("Failed to stop at breakpoint in main.")
96 frame = thread.frames[0]
98 if not frame.IsValid():
99 self.fail ("Failed to get frame 0.")
101 var_n = frame.FindVariable("n")
102 if not var_n.IsValid():
103 self.fail ("Failed to get the variable 'n'")
105 elem_a = var_n.GetChildMemberWithName("a")
106 if not elem_a.IsValid():
107 self.fail ("Failed to get the element a in n")
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")
115 # Call super's setUp().
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.')
123 def common_setup(self, line):
125 # Set debugger into synchronous mode
126 self.dbg.SetAsync(False)
129 exe = os.path.join(os.getcwd(), "a.out")
130 target = self.dbg.CreateTarget(exe)
131 self.assertTrue(target, VALID_TARGET)
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)
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)
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'])
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'])