xref: /llvm-project/lldb/test/API/lang/cpp/namespace/TestNamespaceLookup.py (revision a575e6e5ca1eb7b2ae4b906f9bf3be2ba20a80a0)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest the printing of anonymous and named namespace variables.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprecht
65b386158SJordan Rupprechtimport unittest
799451b44SJordan Rupprechtimport lldb
899451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
999451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
1099451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
11*a575e6e5Sjiminghamfrom lldbsuite.test import lldbplatformutil
1299451b44SJordan Rupprecht
1399451b44SJordan Rupprechtclass NamespaceLookupTestCase(TestBase):
1499451b44SJordan Rupprecht    def setUp(self):
1599451b44SJordan Rupprecht        # Call super's setUp().
1699451b44SJordan Rupprecht        TestBase.setUp(self)
1799451b44SJordan Rupprecht        # Break inside different scopes and evaluate value
182238dcc3SJonas Devlieghere        self.line_break_global_scope = line_number("ns.cpp", "// BP_global_scope")
192238dcc3SJonas Devlieghere        self.line_break_file_scope = line_number("ns2.cpp", "// BP_file_scope")
202238dcc3SJonas Devlieghere        self.line_break_ns_scope = line_number("ns2.cpp", "// BP_ns_scope")
2199451b44SJordan Rupprecht        self.line_break_nested_ns_scope = line_number(
222238dcc3SJonas Devlieghere            "ns2.cpp", "// BP_nested_ns_scope"
232238dcc3SJonas Devlieghere        )
2499451b44SJordan Rupprecht        self.line_break_nested_ns_scope_after_using = line_number(
252238dcc3SJonas Devlieghere            "ns2.cpp", "// BP_nested_ns_scope_after_using"
262238dcc3SJonas Devlieghere        )
2799451b44SJordan Rupprecht        self.line_break_before_using_directive = line_number(
282238dcc3SJonas Devlieghere            "ns3.cpp", "// BP_before_using_directive"
292238dcc3SJonas Devlieghere        )
3099451b44SJordan Rupprecht        self.line_break_after_using_directive = line_number(
312238dcc3SJonas Devlieghere            "ns3.cpp", "// BP_after_using_directive"
322238dcc3SJonas Devlieghere        )
3399451b44SJordan Rupprecht
3499451b44SJordan Rupprecht    def runToBkpt(self, command):
3599451b44SJordan Rupprecht        self.runCmd(command, RUN_SUCCEEDED)
3699451b44SJordan Rupprecht        # The stop reason of the thread should be breakpoint.
372238dcc3SJonas Devlieghere        self.expect(
382238dcc3SJonas Devlieghere            "thread list",
392238dcc3SJonas Devlieghere            STOPPED_DUE_TO_BREAKPOINT,
402238dcc3SJonas Devlieghere            substrs=["stopped", "stop reason = breakpoint"],
412238dcc3SJonas Devlieghere        )
4299451b44SJordan Rupprecht
4399451b44SJordan Rupprecht    @skipIfWindows  # This is flakey on Windows: llvm.org/pr38373
445b386158SJordan Rupprecht    @unittest.expectedFailure  # CU-local objects incorrectly scoped
4509f608fdSMichael Buch    def test_scope_lookup_with_run_command_globals(self):
4609f608fdSMichael Buch        """Test scope lookup of functions in lldb."""
4709f608fdSMichael Buch        self.build()
4809f608fdSMichael Buch
4909f608fdSMichael Buch        lldbutil.run_to_source_breakpoint(
502238dcc3SJonas Devlieghere            self, self.line_break_global_scope, lldb.SBFileSpec("ns.cpp")
512238dcc3SJonas Devlieghere        )
5209f608fdSMichael Buch
5309f608fdSMichael Buch        lldbutil.run_break_set_by_file_and_line(
5409f608fdSMichael Buch            self,
5509f608fdSMichael Buch            "ns3.cpp",
5609f608fdSMichael Buch            self.line_break_before_using_directive,
5709f608fdSMichael Buch            num_expected_locations=1,
582238dcc3SJonas Devlieghere            loc_exact=False,
592238dcc3SJonas Devlieghere        )
6009f608fdSMichael Buch
615955edf8SMichael Buch        lldbutil.run_break_set_by_file_and_line(
625955edf8SMichael Buch            self,
635955edf8SMichael Buch            "ns3.cpp",
645955edf8SMichael Buch            self.line_break_after_using_directive,
655955edf8SMichael Buch            num_expected_locations=1,
662238dcc3SJonas Devlieghere            loc_exact=False,
672238dcc3SJonas Devlieghere        )
685955edf8SMichael Buch
6909f608fdSMichael Buch        # Run to BP_global_scope at file scope
7009f608fdSMichael Buch        self.runToBkpt("run")
7109f608fdSMichael Buch
7209f608fdSMichael Buch        # FIXME: LLDB does not correctly scope CU-local objects.
7309f608fdSMichael Buch        # LLDB currently lumps functions from all files into
7409f608fdSMichael Buch        # a single AST and depending on the order with which
7509f608fdSMichael Buch        # functions are considered, LLDB can incorrectly call
7609f608fdSMichael Buch        # the static local ns.cpp::func() instead of the expected
7709f608fdSMichael Buch        # ::func()
7809f608fdSMichael Buch
7909f608fdSMichael Buch        # Evaluate func() - should call ::func()
8009f608fdSMichael Buch        self.expect_expr("func()", expect_type="int", expect_value="1")
8109f608fdSMichael Buch
8209f608fdSMichael Buch        # Evaluate ::func() - should call A::func()
8309f608fdSMichael Buch        self.expect_expr("::func()", result_type="int", result_value="1")
8409f608fdSMichael Buch
8509f608fdSMichael Buch        # Continue to BP_before_using_directive at file scope
8609f608fdSMichael Buch        self.runToBkpt("continue")
8709f608fdSMichael Buch
8809f608fdSMichael Buch        # Evaluate func() - should call ::func()
8909f608fdSMichael Buch        self.expect_expr("func()", result_type="int", result_value="1")
9009f608fdSMichael Buch
9109f608fdSMichael Buch        # Evaluate ::func() - should call ::func()
9209f608fdSMichael Buch        self.expect_expr("::func()", result_type="int", result_value="1")
9309f608fdSMichael Buch
9409f608fdSMichael Buch        # Continue to BP_after_using_directive at file scope
9509f608fdSMichael Buch        self.runToBkpt("continue")
9609f608fdSMichael Buch
9709f608fdSMichael Buch        # Evaluate ::func() - should call ::func()
9809f608fdSMichael Buch        self.expect_expr("::func()", result_type="int", result_value="1")
9909f608fdSMichael Buch
10009f608fdSMichael Buch    @skipIfWindows  # This is flakey on Windows: llvm.org/pr38373
10199451b44SJordan Rupprecht    def test_scope_lookup_with_run_command(self):
10299451b44SJordan Rupprecht        """Test scope lookup of functions in lldb."""
10399451b44SJordan Rupprecht        self.build()
10499451b44SJordan Rupprecht        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
10599451b44SJordan Rupprecht
10699451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
10799451b44SJordan Rupprecht            self,
10899451b44SJordan Rupprecht            "ns.cpp",
10999451b44SJordan Rupprecht            self.line_break_global_scope,
11099451b44SJordan Rupprecht            num_expected_locations=1,
1112238dcc3SJonas Devlieghere            loc_exact=False,
1122238dcc3SJonas Devlieghere        )
11399451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
11499451b44SJordan Rupprecht            self,
11599451b44SJordan Rupprecht            "ns2.cpp",
11699451b44SJordan Rupprecht            self.line_break_ns_scope,
11799451b44SJordan Rupprecht            num_expected_locations=1,
1182238dcc3SJonas Devlieghere            loc_exact=False,
1192238dcc3SJonas Devlieghere        )
12099451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
12199451b44SJordan Rupprecht            self,
12299451b44SJordan Rupprecht            "ns2.cpp",
12399451b44SJordan Rupprecht            self.line_break_nested_ns_scope,
12499451b44SJordan Rupprecht            num_expected_locations=1,
1252238dcc3SJonas Devlieghere            loc_exact=False,
1262238dcc3SJonas Devlieghere        )
12799451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
12899451b44SJordan Rupprecht            self,
12999451b44SJordan Rupprecht            "ns2.cpp",
13099451b44SJordan Rupprecht            self.line_break_nested_ns_scope_after_using,
13199451b44SJordan Rupprecht            num_expected_locations=1,
1322238dcc3SJonas Devlieghere            loc_exact=False,
1332238dcc3SJonas Devlieghere        )
13499451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
13599451b44SJordan Rupprecht            self,
13609f608fdSMichael Buch            "ns2.cpp",
13709f608fdSMichael Buch            self.line_break_file_scope,
13809f608fdSMichael Buch            num_expected_locations=1,
1392238dcc3SJonas Devlieghere            loc_exact=False,
1402238dcc3SJonas Devlieghere        )
14109f608fdSMichael Buch        lldbutil.run_break_set_by_file_and_line(
14209f608fdSMichael Buch            self,
14399451b44SJordan Rupprecht            "ns3.cpp",
14499451b44SJordan Rupprecht            self.line_break_before_using_directive,
14599451b44SJordan Rupprecht            num_expected_locations=1,
1462238dcc3SJonas Devlieghere            loc_exact=False,
1472238dcc3SJonas Devlieghere        )
14899451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
14999451b44SJordan Rupprecht            self,
15099451b44SJordan Rupprecht            "ns3.cpp",
15199451b44SJordan Rupprecht            self.line_break_after_using_directive,
15299451b44SJordan Rupprecht            num_expected_locations=1,
1532238dcc3SJonas Devlieghere            loc_exact=False,
1542238dcc3SJonas Devlieghere        )
15599451b44SJordan Rupprecht
15699451b44SJordan Rupprecht        # Run to BP_global_scope at global scope
15799451b44SJordan Rupprecht        self.runToBkpt("run")
15809f608fdSMichael Buch
15999451b44SJordan Rupprecht        # Evaluate A::B::func() - should call A::B::func()
1606f19f987SMichael Buch        self.expect_expr("A::B::func()", result_type="int", result_value="4")
16199451b44SJordan Rupprecht        # Evaluate func(10) - should call ::func(int)
1626f19f987SMichael Buch        self.expect_expr("func(10)", result_type="int", result_value="11")
16399451b44SJordan Rupprecht        # Evaluate A::foo() - should call A::foo()
1646f19f987SMichael Buch        self.expect_expr("A::foo()", result_type="int", result_value="42")
16599451b44SJordan Rupprecht
16609f608fdSMichael Buch        # Continue to BP_file_scope at file scope
16709f608fdSMichael Buch        self.runToBkpt("continue")
1686890ad3fSFelipe de Azevedo Piovezan        # FIXME: In DWARF 5 with dsyms, the ordering of functions is slightly
1696890ad3fSFelipe de Azevedo Piovezan        # different, which also hits the same issues mentioned previously.
170*a575e6e5Sjimingham        if (
171*a575e6e5Sjimingham            int(lldbplatformutil.getDwarfVersion()) <= 4
172*a575e6e5Sjimingham            or self.getDebugInfo() == "dwarf"
173*a575e6e5Sjimingham        ):
17409f608fdSMichael Buch            self.expect_expr("func()", result_type="int", result_value="2")
17509f608fdSMichael Buch
17699451b44SJordan Rupprecht        # Continue to BP_ns_scope at ns scope
17799451b44SJordan Rupprecht        self.runToBkpt("continue")
17899451b44SJordan Rupprecht        # Evaluate func(10) - should call A::func(int)
1796f19f987SMichael Buch        self.expect_expr("func(10)", result_type="int", result_value="13")
18099451b44SJordan Rupprecht        # Evaluate B::func() - should call B::func()
1816f19f987SMichael Buch        self.expect_expr("B::func()", result_type="int", result_value="4")
18299451b44SJordan Rupprecht        # Evaluate func() - should call A::func()
1836f19f987SMichael Buch        self.expect_expr("func()", result_type="int", result_value="3")
18499451b44SJordan Rupprecht
18599451b44SJordan Rupprecht        # Continue to BP_nested_ns_scope at nested ns scope
18699451b44SJordan Rupprecht        self.runToBkpt("continue")
18799451b44SJordan Rupprecht        # Evaluate func() - should call A::B::func()
1886f19f987SMichael Buch        self.expect_expr("func()", result_type="int", result_value="4")
18999451b44SJordan Rupprecht        # Evaluate A::func() - should call A::func()
1906f19f987SMichael Buch        self.expect_expr("A::func()", result_type="int", result_value="3")
19199451b44SJordan Rupprecht
19299451b44SJordan Rupprecht        # Evaluate func(10) - should call A::func(10)
19399451b44SJordan Rupprecht        # NOTE: Under the rules of C++, this test would normally get an error
19499451b44SJordan Rupprecht        # because A::B::func() hides A::func(), but lldb intentionally
19599451b44SJordan Rupprecht        # disobeys these rules so that the intended overload can be found
19699451b44SJordan Rupprecht        # by only removing duplicates if they have the same type.
1976f19f987SMichael Buch        self.expect_expr("func(10)", result_type="int", result_value="13")
19899451b44SJordan Rupprecht
19999451b44SJordan Rupprecht        # Continue to BP_nested_ns_scope_after_using at nested ns scope after
20099451b44SJordan Rupprecht        # using declaration
20199451b44SJordan Rupprecht        self.runToBkpt("continue")
20299451b44SJordan Rupprecht        # Evaluate A::func(10) - should call A::func(int)
2036f19f987SMichael Buch        self.expect_expr("A::func(10)", result_type="int", result_value="13")
20499451b44SJordan Rupprecht
20599451b44SJordan Rupprecht        # Continue to BP_before_using_directive at global scope before using
20699451b44SJordan Rupprecht        # declaration
20799451b44SJordan Rupprecht        self.runToBkpt("continue")
20899451b44SJordan Rupprecht        # Evaluate B::func() - should call B::func()
2096f19f987SMichael Buch        self.expect_expr("B::func()", result_type="int", result_value="4")
21099451b44SJordan Rupprecht
21199451b44SJordan Rupprecht        # Continue to BP_after_using_directive at global scope after using
21299451b44SJordan Rupprecht        # declaration
21399451b44SJordan Rupprecht        self.runToBkpt("continue")
21499451b44SJordan Rupprecht        # Evaluate B::func() - should call B::func()
2156f19f987SMichael Buch        self.expect_expr("B::func()", result_type="int", result_value="4")
21699451b44SJordan Rupprecht
2175b386158SJordan Rupprecht    @unittest.expectedFailure  # lldb scope lookup of functions bugs
21899451b44SJordan Rupprecht    def test_function_scope_lookup_with_run_command(self):
21999451b44SJordan Rupprecht        """Test scope lookup of functions in lldb."""
22099451b44SJordan Rupprecht        self.build()
22199451b44SJordan Rupprecht        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
22299451b44SJordan Rupprecht
22399451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
22499451b44SJordan Rupprecht            self,
22599451b44SJordan Rupprecht            "ns.cpp",
22699451b44SJordan Rupprecht            self.line_break_global_scope,
22799451b44SJordan Rupprecht            num_expected_locations=1,
2282238dcc3SJonas Devlieghere            loc_exact=False,
2292238dcc3SJonas Devlieghere        )
23099451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
23199451b44SJordan Rupprecht            self,
23299451b44SJordan Rupprecht            "ns2.cpp",
23399451b44SJordan Rupprecht            self.line_break_ns_scope,
23499451b44SJordan Rupprecht            num_expected_locations=1,
2352238dcc3SJonas Devlieghere            loc_exact=False,
2362238dcc3SJonas Devlieghere        )
23799451b44SJordan Rupprecht
23899451b44SJordan Rupprecht        # Run to BP_global_scope at global scope
23999451b44SJordan Rupprecht        self.runToBkpt("run")
24099451b44SJordan Rupprecht        # Evaluate foo() - should call ::foo()
24199451b44SJordan Rupprecht        # FIXME: lldb finds Y::foo because lookup for variables is done
24299451b44SJordan Rupprecht        # before functions.
2436f19f987SMichael Buch        self.expect_expr("foo()", result_type="int", result_value="42")
24499451b44SJordan Rupprecht        # Evaluate ::foo() - should call ::foo()
24599451b44SJordan Rupprecht        # FIXME: lldb finds Y::foo because lookup for variables is done
24699451b44SJordan Rupprecht        # before functions and :: is ignored.
2476f19f987SMichael Buch        self.expect_expr("::foo()", result_type="int", result_value="42")
24899451b44SJordan Rupprecht
24999451b44SJordan Rupprecht        # Continue to BP_ns_scope at ns scope
25099451b44SJordan Rupprecht        self.runToBkpt("continue")
25199451b44SJordan Rupprecht        # Evaluate foo() - should call A::foo()
25299451b44SJordan Rupprecht        # FIXME: lldb finds Y::foo because lookup for variables is done
25399451b44SJordan Rupprecht        # before functions.
2546f19f987SMichael Buch        self.expect_expr("foo()", result_type="int", result_value="42")
25599451b44SJordan Rupprecht
25699451b44SJordan Rupprecht    # NOTE: this test may fail on older systems that don't emit import
25799451b44SJordan Rupprecht    # entries in DWARF - may need to add checks for compiler versions here.
2582238dcc3SJonas Devlieghere    @skipIf(compiler="gcc", oslist=["linux"], debug_info=["dwo"])  # Skip to avoid crash
25999451b44SJordan Rupprecht    def test_scope_after_using_directive_lookup_with_run_command(self):
26099451b44SJordan Rupprecht        """Test scope lookup after using directive in lldb."""
26199451b44SJordan Rupprecht        self.build()
26299451b44SJordan Rupprecht        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
26399451b44SJordan Rupprecht
26499451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
26599451b44SJordan Rupprecht            self,
26699451b44SJordan Rupprecht            "ns3.cpp",
26799451b44SJordan Rupprecht            self.line_break_after_using_directive,
26899451b44SJordan Rupprecht            num_expected_locations=1,
2692238dcc3SJonas Devlieghere            loc_exact=False,
2702238dcc3SJonas Devlieghere        )
27199451b44SJordan Rupprecht
27299451b44SJordan Rupprecht        # Run to BP_after_using_directive at global scope after using
27399451b44SJordan Rupprecht        # declaration
27499451b44SJordan Rupprecht        self.runToBkpt("run")
27599451b44SJordan Rupprecht        # Evaluate func2() - should call A::func2()
2766f19f987SMichael Buch        self.expect_expr("func2()", result_type="int", result_value="3")
27799451b44SJordan Rupprecht
2785b386158SJordan Rupprecht    @unittest.expectedFailure  # lldb scope lookup after using declaration bugs
27999451b44SJordan Rupprecht    # NOTE: this test may fail on older systems that don't emit import
28099451b44SJordan Rupprecht    # emtries in DWARF - may need to add checks for compiler versions here.
28199451b44SJordan Rupprecht    def test_scope_after_using_declaration_lookup_with_run_command(self):
28299451b44SJordan Rupprecht        """Test scope lookup after using declaration in lldb."""
28399451b44SJordan Rupprecht        self.build()
28499451b44SJordan Rupprecht        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
28599451b44SJordan Rupprecht
28699451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
28799451b44SJordan Rupprecht            self,
28899451b44SJordan Rupprecht            "ns2.cpp",
28999451b44SJordan Rupprecht            self.line_break_nested_ns_scope_after_using,
29099451b44SJordan Rupprecht            num_expected_locations=1,
2912238dcc3SJonas Devlieghere            loc_exact=False,
2922238dcc3SJonas Devlieghere        )
29399451b44SJordan Rupprecht
29499451b44SJordan Rupprecht        # Run to BP_nested_ns_scope_after_using at nested ns scope after using
29599451b44SJordan Rupprecht        # declaration
29699451b44SJordan Rupprecht        self.runToBkpt("run")
29799451b44SJordan Rupprecht        # Evaluate func() - should call A::func()
2986f19f987SMichael Buch        self.expect_expr("func()", result_type="int", result_value="3")
29999451b44SJordan Rupprecht
3005b386158SJordan Rupprecht    @unittest.expectedFailure  # lldb scope lookup ambiguity after using bugs
30199451b44SJordan Rupprecht    def test_scope_ambiguity_after_using_lookup_with_run_command(self):
30299451b44SJordan Rupprecht        """Test scope lookup ambiguity after using in lldb."""
30399451b44SJordan Rupprecht        self.build()
30499451b44SJordan Rupprecht        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
30599451b44SJordan Rupprecht
30699451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
30799451b44SJordan Rupprecht            self,
30899451b44SJordan Rupprecht            "ns3.cpp",
30999451b44SJordan Rupprecht            self.line_break_after_using_directive,
31099451b44SJordan Rupprecht            num_expected_locations=1,
3112238dcc3SJonas Devlieghere            loc_exact=False,
3122238dcc3SJonas Devlieghere        )
31399451b44SJordan Rupprecht
31499451b44SJordan Rupprecht        # Run to BP_after_using_directive at global scope after using
31599451b44SJordan Rupprecht        # declaration
31699451b44SJordan Rupprecht        self.runToBkpt("run")
31799451b44SJordan Rupprecht        # Evaluate func() - should get error: ambiguous
31899451b44SJordan Rupprecht        # FIXME: This test fails because lldb removes duplicates if they have
31999451b44SJordan Rupprecht        # the same type.
32099451b44SJordan Rupprecht        self.expect("expr -- func()", startstr="error")
32199451b44SJordan Rupprecht
32299451b44SJordan Rupprecht    def test_scope_lookup_shadowed_by_using_with_run_command(self):
32399451b44SJordan Rupprecht        """Test scope lookup shadowed by using in lldb."""
32499451b44SJordan Rupprecht        self.build()
32599451b44SJordan Rupprecht        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
32699451b44SJordan Rupprecht
32799451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
32899451b44SJordan Rupprecht            self,
32999451b44SJordan Rupprecht            "ns2.cpp",
33099451b44SJordan Rupprecht            self.line_break_nested_ns_scope,
33199451b44SJordan Rupprecht            num_expected_locations=1,
3322238dcc3SJonas Devlieghere            loc_exact=False,
3332238dcc3SJonas Devlieghere        )
33499451b44SJordan Rupprecht
33599451b44SJordan Rupprecht        # Run to BP_nested_ns_scope at nested ns scope
33699451b44SJordan Rupprecht        self.runToBkpt("run")
33799451b44SJordan Rupprecht        # Evaluate func(10) - should call A::func(10)
33899451b44SJordan Rupprecht        # NOTE: Under the rules of C++, this test would normally get an error
33999451b44SJordan Rupprecht        # because A::B::func() shadows A::func(), but lldb intentionally
34099451b44SJordan Rupprecht        # disobeys these rules so that the intended overload can be found
34199451b44SJordan Rupprecht        # by only removing duplicates if they have the same type.
3426f19f987SMichael Buch        self.expect_expr("func(10)", result_type="int", result_value="13")
343