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