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