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 NamespaceLookupTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20        # Break inside different scopes and evaluate value
21        self.line_break_global_scope = line_number(
22            'ns.cpp', '// BP_global_scope')
23        self.line_break_file_scope = line_number('ns2.cpp', '// BP_file_scope')
24        self.line_break_ns_scope = line_number('ns2.cpp', '// BP_ns_scope')
25        self.line_break_nested_ns_scope = line_number(
26            'ns2.cpp', '// BP_nested_ns_scope')
27        self.line_break_nested_ns_scope_after_using = line_number(
28            'ns2.cpp', '// BP_nested_ns_scope_after_using')
29        self.line_break_before_using_directive = line_number(
30            'ns3.cpp', '// BP_before_using_directive')
31        self.line_break_after_using_directive = line_number(
32            'ns3.cpp', '// BP_after_using_directive')
33
34    def runToBkpt(self, command):
35        self.runCmd(command, RUN_SUCCEEDED)
36        # The stop reason of the thread should be breakpoint.
37        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
38                    substrs=['stopped',
39                             'stop reason = breakpoint'])
40
41    @skipIfWindows # This is flakey on Windows: llvm.org/pr38373
42    def test_scope_lookup_with_run_command(self):
43        """Test scope lookup of functions in lldb."""
44        self.build()
45        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
46
47        lldbutil.run_break_set_by_file_and_line(
48            self,
49            "ns.cpp",
50            self.line_break_global_scope,
51            num_expected_locations=1,
52            loc_exact=False)
53        lldbutil.run_break_set_by_file_and_line(
54            self,
55            "ns2.cpp",
56            self.line_break_ns_scope,
57            num_expected_locations=1,
58            loc_exact=False)
59        lldbutil.run_break_set_by_file_and_line(
60            self,
61            "ns2.cpp",
62            self.line_break_nested_ns_scope,
63            num_expected_locations=1,
64            loc_exact=False)
65        lldbutil.run_break_set_by_file_and_line(
66            self,
67            "ns2.cpp",
68            self.line_break_nested_ns_scope_after_using,
69            num_expected_locations=1,
70            loc_exact=False)
71        lldbutil.run_break_set_by_file_and_line(
72            self,
73            "ns3.cpp",
74            self.line_break_before_using_directive,
75            num_expected_locations=1,
76            loc_exact=False)
77        lldbutil.run_break_set_by_file_and_line(
78            self,
79            "ns3.cpp",
80            self.line_break_after_using_directive,
81            num_expected_locations=1,
82            loc_exact=False)
83
84        # Run to BP_global_scope at global scope
85        self.runToBkpt("run")
86        # Evaluate func() - should call ::func()
87        self.expect("expr -- func()", startstr="(int) $0 = 1")
88        # Evaluate A::B::func() - should call A::B::func()
89        self.expect("expr -- A::B::func()", startstr="(int) $1 = 4")
90        # Evaluate func(10) - should call ::func(int)
91        self.expect("expr -- func(10)", startstr="(int) $2 = 11")
92        # Evaluate ::func() - should call A::func()
93        self.expect("expr -- ::func()", startstr="(int) $3 = 1")
94        # Evaluate A::foo() - should call A::foo()
95        self.expect("expr -- A::foo()", startstr="(int) $4 = 42")
96
97        # Continue to BP_ns_scope at ns scope
98        self.runToBkpt("continue")
99        # Evaluate func(10) - should call A::func(int)
100        self.expect("expr -- func(10)", startstr="(int) $5 = 13")
101        # Evaluate B::func() - should call B::func()
102        self.expect("expr -- B::func()", startstr="(int) $6 = 4")
103        # Evaluate func() - should call A::func()
104        self.expect("expr -- func()", startstr="(int) $7 = 3")
105
106        # Continue to BP_nested_ns_scope at nested ns scope
107        self.runToBkpt("continue")
108        # Evaluate func() - should call A::B::func()
109        self.expect("expr -- func()", startstr="(int) $8 = 4")
110        # Evaluate A::func() - should call A::func()
111        self.expect("expr -- A::func()", startstr="(int) $9 = 3")
112
113        # Evaluate func(10) - should call A::func(10)
114        # NOTE: Under the rules of C++, this test would normally get an error
115        # because A::B::func() hides A::func(), but lldb intentionally
116        # disobeys these rules so that the intended overload can be found
117        # by only removing duplicates if they have the same type.
118        self.expect("expr -- func(10)", startstr="(int) $10 = 13")
119
120        # Continue to BP_nested_ns_scope_after_using at nested ns scope after
121        # using declaration
122        self.runToBkpt("continue")
123        # Evaluate A::func(10) - should call A::func(int)
124        self.expect("expr -- A::func(10)", startstr="(int) $11 = 13")
125
126        # Continue to BP_before_using_directive at global scope before using
127        # declaration
128        self.runToBkpt("continue")
129        # Evaluate ::func() - should call ::func()
130        self.expect("expr -- ::func()", startstr="(int) $12 = 1")
131        # Evaluate B::func() - should call B::func()
132        self.expect("expr -- B::func()", startstr="(int) $13 = 4")
133
134        # Continue to BP_after_using_directive at global scope after using
135        # declaration
136        self.runToBkpt("continue")
137        # Evaluate ::func() - should call ::func()
138        self.expect("expr -- ::func()", startstr="(int) $14 = 1")
139        # Evaluate B::func() - should call B::func()
140        self.expect("expr -- B::func()", startstr="(int) $15 = 4")
141
142    @expectedFailure("lldb scope lookup of functions bugs")
143    def test_function_scope_lookup_with_run_command(self):
144        """Test scope lookup of functions in lldb."""
145        self.build()
146        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
147
148        lldbutil.run_break_set_by_file_and_line(
149            self,
150            "ns.cpp",
151            self.line_break_global_scope,
152            num_expected_locations=1,
153            loc_exact=False)
154        lldbutil.run_break_set_by_file_and_line(
155            self,
156            "ns2.cpp",
157            self.line_break_ns_scope,
158            num_expected_locations=1,
159            loc_exact=False)
160
161        # Run to BP_global_scope at global scope
162        self.runToBkpt("run")
163        # Evaluate foo() - should call ::foo()
164        # FIXME: lldb finds Y::foo because lookup for variables is done
165        # before functions.
166        self.expect("expr -- foo()", startstr="(int) $0 = 42")
167        # Evaluate ::foo() - should call ::foo()
168        # FIXME: lldb finds Y::foo because lookup for variables is done
169        # before functions and :: is ignored.
170        self.expect("expr -- ::foo()", startstr="(int) $1 = 42")
171
172        # Continue to BP_ns_scope at ns scope
173        self.runToBkpt("continue")
174        # Evaluate foo() - should call A::foo()
175        # FIXME: lldb finds Y::foo because lookup for variables is done
176        # before functions.
177        self.expect("expr -- foo()", startstr="(int) $2 = 42")
178
179    @expectedFailure("lldb file scope lookup bugs")
180    @skipIfWindows # This is flakey on Windows: llvm.org/pr38373
181    def test_file_scope_lookup_with_run_command(self):
182        """Test file scope lookup in lldb."""
183        self.build()
184        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
185
186        lldbutil.run_break_set_by_file_and_line(
187            self,
188            "ns2.cpp",
189            self.line_break_file_scope,
190            num_expected_locations=1,
191            loc_exact=False)
192
193        # Run to BP_file_scope at file scope
194        self.runToBkpt("run")
195        # Evaluate func() - should call static ns2.cpp:func()
196        # FIXME: This test fails because lldb doesn't know about file scopes so
197        # finds the global ::func().
198        self.expect("expr -- func()", startstr="(int) $0 = 2")
199
200    @skipIfWindows # This is flakey on Windows: llvm.org/pr38373
201    def test_scope_lookup_before_using_with_run_command(self):
202        """Test scope lookup before using in lldb."""
203        self.build()
204        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
205
206        lldbutil.run_break_set_by_file_and_line(
207            self,
208            "ns3.cpp",
209            self.line_break_before_using_directive,
210            num_expected_locations=1,
211            loc_exact=False)
212
213        # Run to BP_before_using_directive at global scope before using
214        # declaration
215        self.runToBkpt("run")
216        # Evaluate func() - should call ::func()
217        self.expect("expr -- func()", startstr="(int) $0 = 1")
218
219    # NOTE: this test may fail on older systems that don't emit import
220    # entries in DWARF - may need to add checks for compiler versions here.
221    @skipIf(
222        compiler="gcc",
223        oslist=["linux"],
224        debug_info=["dwo"])  # Skip to avoid crash
225    def test_scope_after_using_directive_lookup_with_run_command(self):
226        """Test scope lookup after using directive in lldb."""
227        self.build()
228        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
229
230        lldbutil.run_break_set_by_file_and_line(
231            self,
232            "ns3.cpp",
233            self.line_break_after_using_directive,
234            num_expected_locations=1,
235            loc_exact=False)
236
237        # Run to BP_after_using_directive at global scope after using
238        # declaration
239        self.runToBkpt("run")
240        # Evaluate func2() - should call A::func2()
241        self.expect("expr -- func2()", startstr="(int) $0 = 3")
242
243    @expectedFailure(
244        "lldb scope lookup after using declaration bugs")
245    # NOTE: this test may fail on older systems that don't emit import
246    # emtries in DWARF - may need to add checks for compiler versions here.
247    def test_scope_after_using_declaration_lookup_with_run_command(self):
248        """Test scope lookup after using declaration in lldb."""
249        self.build()
250        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
251
252        lldbutil.run_break_set_by_file_and_line(
253            self,
254            "ns2.cpp",
255            self.line_break_nested_ns_scope_after_using,
256            num_expected_locations=1,
257            loc_exact=False)
258
259        # Run to BP_nested_ns_scope_after_using at nested ns scope after using
260        # declaration
261        self.runToBkpt("run")
262        # Evaluate func() - should call A::func()
263        self.expect("expr -- func()", startstr="(int) $0 = 3")
264
265    @expectedFailure("lldb scope lookup ambiguity after using bugs")
266    def test_scope_ambiguity_after_using_lookup_with_run_command(self):
267        """Test scope lookup ambiguity after using in lldb."""
268        self.build()
269        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
270
271        lldbutil.run_break_set_by_file_and_line(
272            self,
273            "ns3.cpp",
274            self.line_break_after_using_directive,
275            num_expected_locations=1,
276            loc_exact=False)
277
278        # Run to BP_after_using_directive at global scope after using
279        # declaration
280        self.runToBkpt("run")
281        # Evaluate func() - should get error: ambiguous
282        # FIXME: This test fails because lldb removes duplicates if they have
283        # the same type.
284        self.expect("expr -- func()", startstr="error")
285
286    def test_scope_lookup_shadowed_by_using_with_run_command(self):
287        """Test scope lookup shadowed by using in lldb."""
288        self.build()
289        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
290
291        lldbutil.run_break_set_by_file_and_line(
292            self,
293            "ns2.cpp",
294            self.line_break_nested_ns_scope,
295            num_expected_locations=1,
296            loc_exact=False)
297
298        # Run to BP_nested_ns_scope at nested ns scope
299        self.runToBkpt("run")
300        # Evaluate func(10) - should call A::func(10)
301        # NOTE: Under the rules of C++, this test would normally get an error
302        # because A::B::func() shadows A::func(), but lldb intentionally
303        # disobeys these rules so that the intended overload can be found
304        # by only removing duplicates if they have the same type.
305        self.expect("expr -- func(10)", startstr="(int) $0 = 13")
306