1"""
2Test the printing of anonymous and named namespace variables.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class NamespaceBreakpointTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    @expectedFailureAll(bugnumber="llvm.org/pr28548", compiler="gcc")
18    @expectedFailureAll(oslist=["windows"])
19    def test_breakpoints_func_auto(self):
20        """Test that we can set breakpoints correctly by basename to find all functions whose basename is "func"."""
21        self.build()
22
23        names = [
24            "func()",
25            "func(int)",
26            "A::B::func()",
27            "A::func()",
28            "A::func(int)"]
29
30        # Create a target by the debugger.
31        exe = self.getBuildArtifact("a.out")
32        target = self.dbg.CreateTarget(exe)
33        self.assertTrue(target, VALID_TARGET)
34        module_list = lldb.SBFileSpecList()
35        module_list.Append(lldb.SBFileSpec(exe, False))
36        cu_list = lldb.SBFileSpecList()
37        # Set a breakpoint by name "func" which should pick up all functions
38        # whose basename is "func"
39        bp = target.BreakpointCreateByName(
40            "func", lldb.eFunctionNameTypeAuto, module_list, cu_list)
41        for bp_loc in bp:
42            name = bp_loc.GetAddress().GetFunction().GetName()
43            self.assertTrue(
44                name in names,
45                "make sure breakpoint locations are correct for 'func' with eFunctionNameTypeAuto")
46
47    @expectedFailureAll(bugnumber="llvm.org/pr28548", compiler="gcc")
48    def test_breakpoints_func_full(self):
49        """Test that we can set breakpoints correctly by fullname to find all functions whose fully qualified name is "func"
50           (no namespaces)."""
51        self.build()
52
53        names = ["func()", "func(int)"]
54
55        # Create a target by the debugger.
56        exe = self.getBuildArtifact("a.out")
57        target = self.dbg.CreateTarget(exe)
58        self.assertTrue(target, VALID_TARGET)
59        module_list = lldb.SBFileSpecList()
60        module_list.Append(lldb.SBFileSpec(exe, False))
61        cu_list = lldb.SBFileSpecList()
62
63        # Set a breakpoint by name "func" whose fullly qualified named matches "func" which
64        # should pick up only functions whose basename is "func" and has no
65        # containing context
66        bp = target.BreakpointCreateByName(
67            "func", lldb.eFunctionNameTypeFull, module_list, cu_list)
68        for bp_loc in bp:
69            name = bp_loc.GetAddress().GetFunction().GetName()
70            self.assertTrue(
71                name in names,
72                "make sure breakpoint locations are correct for 'func' with eFunctionNameTypeFull")
73
74    def test_breakpoints_a_func_full(self):
75        """Test that we can set breakpoints correctly by fullname to find all functions whose fully qualified name is "A::func"."""
76        self.build()
77
78        names = ["A::func()", "A::func(int)"]
79
80        # Create a target by the debugger.
81        exe = self.getBuildArtifact("a.out")
82        target = self.dbg.CreateTarget(exe)
83        self.assertTrue(target, VALID_TARGET)
84        module_list = lldb.SBFileSpecList()
85        module_list.Append(lldb.SBFileSpec(exe, False))
86        cu_list = lldb.SBFileSpecList()
87
88        # Set a breakpoint by name "A::func" whose fullly qualified named matches "A::func" which
89        # should pick up only functions whose basename is "func" and is
90        # contained in the "A" namespace
91        bp = target.BreakpointCreateByName(
92            "A::func", lldb.eFunctionNameTypeFull, module_list, cu_list)
93        for bp_loc in bp:
94            name = bp_loc.GetAddress().GetFunction().GetName()
95            self.assertTrue(
96                name in names,
97                "make sure breakpoint locations are correct for 'A::func' with eFunctionNameTypeFull")
98
99
100class NamespaceTestCase(TestBase):
101
102    mydir = TestBase.compute_mydir(__file__)
103
104    def setUp(self):
105        # Call super's setUp().
106        TestBase.setUp(self)
107        # Find the line numbers for declarations of namespace variables i and
108        # j.
109        self.line_var_i = line_number(
110            'main.cpp', '// Find the line number for anonymous namespace variable i.')
111        self.line_var_j = line_number(
112            'main.cpp', '// Find the line number for named namespace variable j.')
113        # And the line number to break at.
114        self.line_break = line_number('main.cpp',
115                                      '// Set break point at this line.')
116        # Break inside do {} while and evaluate value
117        self.line_break_ns1 = line_number('main.cpp', '// Evaluate ns1::value')
118        self.line_break_ns2 = line_number('main.cpp', '// Evaluate ns2::value')
119
120    def runToBkpt(self, command):
121        self.runCmd(command, RUN_SUCCEEDED)
122        # The stop reason of the thread should be breakpoint.
123        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
124                    substrs=['stopped',
125                             'stop reason = breakpoint'])
126
127    # rdar://problem/8668674
128    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764")
129    def test_with_run_command(self):
130        """Test that anonymous and named namespace variables display correctly."""
131        self.build()
132        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
133
134        lldbutil.run_break_set_by_file_and_line(
135            self,
136            "main.cpp",
137            self.line_break_ns1,
138            num_expected_locations=1,
139            loc_exact=True)
140        lldbutil.run_break_set_by_file_and_line(
141            self,
142            "main.cpp",
143            self.line_break_ns2,
144            num_expected_locations=1,
145            loc_exact=True)
146        lldbutil.run_break_set_by_file_and_line(
147            self,
148            "main.cpp",
149            self.line_break,
150            num_expected_locations=1,
151            loc_exact=True)
152
153        self.runToBkpt("run")
154        # Evaluate ns1::value
155        self.expect_expr("value", result_value="100")
156
157        self.runToBkpt("continue")
158        # Evaluate ns2::value
159        self.expect_expr("value", result_value="200")
160
161        self.runToBkpt("continue")
162        # On Mac OS X, gcc 4.2 emits the wrong debug info with respect to
163        # types.
164        slist = ['(int) a = 12', 'anon_uint', 'a_uint', 'b_uint', 'y_uint']
165        if self.platformIsDarwin() and self.getCompiler() in [
166                'clang', 'llvm-gcc']:
167            slist = ['(int) a = 12',
168                     '::my_uint_t', 'anon_uint = 0',
169                     '(A::uint_t) a_uint = 1',
170                     '(A::B::uint_t) b_uint = 2',
171                     '(Y::uint_t) y_uint = 3']
172
173        # 'frame variable' displays the local variables with type information.
174        self.expect('frame variable', VARIABLES_DISPLAYED_CORRECTLY,
175                    substrs=slist)
176
177        # 'frame variable' with basename 'i' should work.
178        self.expect(
179            "frame variable --show-declaration --show-globals i",
180            startstr="main.cpp:%d: (int) (anonymous namespace)::i = 3" %
181            self.line_var_i)
182        # main.cpp:12: (int) (anonymous namespace)::i = 3
183
184        # 'frame variable' with basename 'j' should work, too.
185        self.expect(
186            "frame variable --show-declaration --show-globals j",
187            startstr="main.cpp:%d: (int) A::B::j = 4" %
188            self.line_var_j)
189        # main.cpp:19: (int) A::B::j = 4
190
191        # 'frame variable' should support address-of operator.
192        self.runCmd("frame variable &i")
193
194        # 'frame variable' with fully qualified name 'A::B::j' should work.
195        self.expect("frame variable A::B::j", VARIABLES_DISPLAYED_CORRECTLY,
196                    startstr='(int) A::B::j = 4',
197                    patterns=[' = 4'])
198
199        # So should the anonymous namespace case.
200        self.expect(
201            "frame variable '(anonymous namespace)::i'",
202            VARIABLES_DISPLAYED_CORRECTLY,
203            startstr='(int) (anonymous namespace)::i = 3',
204            patterns=[' = 3'])
205
206        # rdar://problem/8660275
207        # test/namespace: 'expression -- i+j' not working
208        # This has been fixed.
209        self.expect_expr("i + j", result_type="int", result_value="7")
210        # (int) $2 = 7
211
212        self.expect_expr("i", result_value="3")
213        self.expect_expr("j", result_value="4")
214
215        # rdar://problem/8668674
216        # expression command with fully qualified namespace for a variable does
217        # not work
218        self.expect_expr("::i", result_value="3")
219        self.expect_expr("A::B::j", result_value="4")
220
221        # expression command with function in anonymous namespace
222        self.expect_expr("myanonfunc(3)", result_value="6")
223
224        # global namespace qualification with function in anonymous namespace
225        self.expect_expr("myanonfunc(4)", result_value="8")
226
227        self.expect("p myanonfunc",
228                    patterns=['\(anonymous namespace\)::myanonfunc\(int\)'])
229
230        self.expect("p variadic_sum", patterns=[
231                    '\(anonymous namespace\)::variadic_sum\(int, ...\)'])
232