Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / test / API / lang / cpp / namespace / TestNamespaceLookup.py
blobf88667b9dfa2ae9911e558d127c622600af8fb0b
1 """
2 Test the printing of anonymous and named namespace variables.
3 """
6 import lldb
7 from lldbsuite.test.decorators import *
8 from lldbsuite.test.lldbtest import *
9 from lldbsuite.test import lldbutil
12 class NamespaceLookupTestCase(TestBase):
13 def setUp(self):
14 # Call super's setUp().
15 TestBase.setUp(self)
16 # Break inside different scopes and evaluate value
17 self.line_break_global_scope = line_number("ns.cpp", "// BP_global_scope")
18 self.line_break_file_scope = line_number("ns2.cpp", "// BP_file_scope")
19 self.line_break_ns_scope = line_number("ns2.cpp", "// BP_ns_scope")
20 self.line_break_nested_ns_scope = line_number(
21 "ns2.cpp", "// BP_nested_ns_scope"
23 self.line_break_nested_ns_scope_after_using = line_number(
24 "ns2.cpp", "// BP_nested_ns_scope_after_using"
26 self.line_break_before_using_directive = line_number(
27 "ns3.cpp", "// BP_before_using_directive"
29 self.line_break_after_using_directive = line_number(
30 "ns3.cpp", "// BP_after_using_directive"
33 def runToBkpt(self, command):
34 self.runCmd(command, RUN_SUCCEEDED)
35 # The stop reason of the thread should be breakpoint.
36 self.expect(
37 "thread list",
38 STOPPED_DUE_TO_BREAKPOINT,
39 substrs=["stopped", "stop reason = breakpoint"],
42 @skipIfWindows # This is flakey on Windows: llvm.org/pr38373
43 @expectedFailure("CU-local objects incorrectly scoped")
44 def test_scope_lookup_with_run_command_globals(self):
45 """Test scope lookup of functions in lldb."""
46 self.build()
48 lldbutil.run_to_source_breakpoint(
49 self, self.line_break_global_scope, lldb.SBFileSpec("ns.cpp")
52 lldbutil.run_break_set_by_file_and_line(
53 self,
54 "ns3.cpp",
55 self.line_break_before_using_directive,
56 num_expected_locations=1,
57 loc_exact=False,
60 lldbutil.run_break_set_by_file_and_line(
61 self,
62 "ns3.cpp",
63 self.line_break_after_using_directive,
64 num_expected_locations=1,
65 loc_exact=False,
68 # Run to BP_global_scope at file scope
69 self.runToBkpt("run")
71 # FIXME: LLDB does not correctly scope CU-local objects.
72 # LLDB currently lumps functions from all files into
73 # a single AST and depending on the order with which
74 # functions are considered, LLDB can incorrectly call
75 # the static local ns.cpp::func() instead of the expected
76 # ::func()
78 # Evaluate func() - should call ::func()
79 self.expect_expr("func()", expect_type="int", expect_value="1")
81 # Evaluate ::func() - should call A::func()
82 self.expect_expr("::func()", result_type="int", result_value="1")
84 # Continue to BP_before_using_directive at file scope
85 self.runToBkpt("continue")
87 # Evaluate func() - should call ::func()
88 self.expect_expr("func()", result_type="int", result_value="1")
90 # Evaluate ::func() - should call ::func()
91 self.expect_expr("::func()", result_type="int", result_value="1")
93 # Continue to BP_after_using_directive at file scope
94 self.runToBkpt("continue")
96 # Evaluate ::func() - should call ::func()
97 self.expect_expr("::func()", result_type="int", result_value="1")
99 @skipIfWindows # This is flakey on Windows: llvm.org/pr38373
100 def test_scope_lookup_with_run_command(self):
101 """Test scope lookup of functions in lldb."""
102 self.build()
103 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
105 lldbutil.run_break_set_by_file_and_line(
106 self,
107 "ns.cpp",
108 self.line_break_global_scope,
109 num_expected_locations=1,
110 loc_exact=False,
112 lldbutil.run_break_set_by_file_and_line(
113 self,
114 "ns2.cpp",
115 self.line_break_ns_scope,
116 num_expected_locations=1,
117 loc_exact=False,
119 lldbutil.run_break_set_by_file_and_line(
120 self,
121 "ns2.cpp",
122 self.line_break_nested_ns_scope,
123 num_expected_locations=1,
124 loc_exact=False,
126 lldbutil.run_break_set_by_file_and_line(
127 self,
128 "ns2.cpp",
129 self.line_break_nested_ns_scope_after_using,
130 num_expected_locations=1,
131 loc_exact=False,
133 lldbutil.run_break_set_by_file_and_line(
134 self,
135 "ns2.cpp",
136 self.line_break_file_scope,
137 num_expected_locations=1,
138 loc_exact=False,
140 lldbutil.run_break_set_by_file_and_line(
141 self,
142 "ns3.cpp",
143 self.line_break_before_using_directive,
144 num_expected_locations=1,
145 loc_exact=False,
147 lldbutil.run_break_set_by_file_and_line(
148 self,
149 "ns3.cpp",
150 self.line_break_after_using_directive,
151 num_expected_locations=1,
152 loc_exact=False,
155 # Run to BP_global_scope at global scope
156 self.runToBkpt("run")
158 # Evaluate A::B::func() - should call A::B::func()
159 self.expect_expr("A::B::func()", result_type="int", result_value="4")
160 # Evaluate func(10) - should call ::func(int)
161 self.expect_expr("func(10)", result_type="int", result_value="11")
162 # Evaluate A::foo() - should call A::foo()
163 self.expect_expr("A::foo()", result_type="int", result_value="42")
165 # Continue to BP_file_scope at file scope
166 self.runToBkpt("continue")
167 # FIXME: In DWARF 5 with dsyms, the ordering of functions is slightly
168 # different, which also hits the same issues mentioned previously.
169 if configuration.dwarf_version <= 4 or self.getDebugInfo() == "dwarf":
170 self.expect_expr("func()", result_type="int", result_value="2")
172 # Continue to BP_ns_scope at ns scope
173 self.runToBkpt("continue")
174 # Evaluate func(10) - should call A::func(int)
175 self.expect_expr("func(10)", result_type="int", result_value="13")
176 # Evaluate B::func() - should call B::func()
177 self.expect_expr("B::func()", result_type="int", result_value="4")
178 # Evaluate func() - should call A::func()
179 self.expect_expr("func()", result_type="int", result_value="3")
181 # Continue to BP_nested_ns_scope at nested ns scope
182 self.runToBkpt("continue")
183 # Evaluate func() - should call A::B::func()
184 self.expect_expr("func()", result_type="int", result_value="4")
185 # Evaluate A::func() - should call A::func()
186 self.expect_expr("A::func()", result_type="int", result_value="3")
188 # Evaluate func(10) - should call A::func(10)
189 # NOTE: Under the rules of C++, this test would normally get an error
190 # because A::B::func() hides A::func(), but lldb intentionally
191 # disobeys these rules so that the intended overload can be found
192 # by only removing duplicates if they have the same type.
193 self.expect_expr("func(10)", result_type="int", result_value="13")
195 # Continue to BP_nested_ns_scope_after_using at nested ns scope after
196 # using declaration
197 self.runToBkpt("continue")
198 # Evaluate A::func(10) - should call A::func(int)
199 self.expect_expr("A::func(10)", result_type="int", result_value="13")
201 # Continue to BP_before_using_directive at global scope before using
202 # declaration
203 self.runToBkpt("continue")
204 # Evaluate B::func() - should call B::func()
205 self.expect_expr("B::func()", result_type="int", result_value="4")
207 # Continue to BP_after_using_directive at global scope after using
208 # declaration
209 self.runToBkpt("continue")
210 # Evaluate B::func() - should call B::func()
211 self.expect_expr("B::func()", result_type="int", result_value="4")
213 @expectedFailure("lldb scope lookup of functions bugs")
214 def test_function_scope_lookup_with_run_command(self):
215 """Test scope lookup of functions in lldb."""
216 self.build()
217 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
219 lldbutil.run_break_set_by_file_and_line(
220 self,
221 "ns.cpp",
222 self.line_break_global_scope,
223 num_expected_locations=1,
224 loc_exact=False,
226 lldbutil.run_break_set_by_file_and_line(
227 self,
228 "ns2.cpp",
229 self.line_break_ns_scope,
230 num_expected_locations=1,
231 loc_exact=False,
234 # Run to BP_global_scope at global scope
235 self.runToBkpt("run")
236 # Evaluate foo() - should call ::foo()
237 # FIXME: lldb finds Y::foo because lookup for variables is done
238 # before functions.
239 self.expect_expr("foo()", result_type="int", result_value="42")
240 # Evaluate ::foo() - should call ::foo()
241 # FIXME: lldb finds Y::foo because lookup for variables is done
242 # before functions and :: is ignored.
243 self.expect_expr("::foo()", result_type="int", result_value="42")
245 # Continue to BP_ns_scope at ns scope
246 self.runToBkpt("continue")
247 # Evaluate foo() - should call A::foo()
248 # FIXME: lldb finds Y::foo because lookup for variables is done
249 # before functions.
250 self.expect_expr("foo()", result_type="int", result_value="42")
252 # NOTE: this test may fail on older systems that don't emit import
253 # entries in DWARF - may need to add checks for compiler versions here.
254 @skipIf(compiler="gcc", oslist=["linux"], debug_info=["dwo"]) # Skip to avoid crash
255 def test_scope_after_using_directive_lookup_with_run_command(self):
256 """Test scope lookup after using directive in lldb."""
257 self.build()
258 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
260 lldbutil.run_break_set_by_file_and_line(
261 self,
262 "ns3.cpp",
263 self.line_break_after_using_directive,
264 num_expected_locations=1,
265 loc_exact=False,
268 # Run to BP_after_using_directive at global scope after using
269 # declaration
270 self.runToBkpt("run")
271 # Evaluate func2() - should call A::func2()
272 self.expect_expr("func2()", result_type="int", result_value="3")
274 @expectedFailure("lldb scope lookup after using declaration bugs")
275 # NOTE: this test may fail on older systems that don't emit import
276 # emtries in DWARF - may need to add checks for compiler versions here.
277 def test_scope_after_using_declaration_lookup_with_run_command(self):
278 """Test scope lookup after using declaration in lldb."""
279 self.build()
280 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
282 lldbutil.run_break_set_by_file_and_line(
283 self,
284 "ns2.cpp",
285 self.line_break_nested_ns_scope_after_using,
286 num_expected_locations=1,
287 loc_exact=False,
290 # Run to BP_nested_ns_scope_after_using at nested ns scope after using
291 # declaration
292 self.runToBkpt("run")
293 # Evaluate func() - should call A::func()
294 self.expect_expr("func()", result_type="int", result_value="3")
296 @expectedFailure("lldb scope lookup ambiguity after using bugs")
297 def test_scope_ambiguity_after_using_lookup_with_run_command(self):
298 """Test scope lookup ambiguity after using in lldb."""
299 self.build()
300 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
302 lldbutil.run_break_set_by_file_and_line(
303 self,
304 "ns3.cpp",
305 self.line_break_after_using_directive,
306 num_expected_locations=1,
307 loc_exact=False,
310 # Run to BP_after_using_directive at global scope after using
311 # declaration
312 self.runToBkpt("run")
313 # Evaluate func() - should get error: ambiguous
314 # FIXME: This test fails because lldb removes duplicates if they have
315 # the same type.
316 self.expect("expr -- func()", startstr="error")
318 def test_scope_lookup_shadowed_by_using_with_run_command(self):
319 """Test scope lookup shadowed by using in lldb."""
320 self.build()
321 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
323 lldbutil.run_break_set_by_file_and_line(
324 self,
325 "ns2.cpp",
326 self.line_break_nested_ns_scope,
327 num_expected_locations=1,
328 loc_exact=False,
331 # Run to BP_nested_ns_scope at nested ns scope
332 self.runToBkpt("run")
333 # Evaluate func(10) - should call A::func(10)
334 # NOTE: Under the rules of C++, this test would normally get an error
335 # because A::B::func() shadows A::func(), but lldb intentionally
336 # disobeys these rules so that the intended overload can be found
337 # by only removing duplicates if they have the same type.
338 self.expect_expr("func(10)", result_type="int", result_value="13")