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