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