xref: /llvm-project/lldb/test/API/lang/cpp/nsimport/TestCppNsImport.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTests imported namespaces in C++.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprechtimport lldb
599451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
699451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
799451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
899451b44SJordan Rupprecht
999451b44SJordan Rupprecht
1099451b44SJordan Rupprechtclass TestCppNsImport(TestBase):
1199451b44SJordan Rupprecht    def test_with_run_command(self):
1299451b44SJordan Rupprecht        """Tests imported namespaces in C++."""
1399451b44SJordan Rupprecht        self.build()
1499451b44SJordan Rupprecht
1599451b44SJordan Rupprecht        # Get main source file
1699451b44SJordan Rupprecht        src_file = os.path.join(self.getSourceDir(), "main.cpp")
1799451b44SJordan Rupprecht        src_file_spec = lldb.SBFileSpec(src_file)
1899451b44SJordan Rupprecht        self.assertTrue(src_file_spec.IsValid(), "Main source file")
1999451b44SJordan Rupprecht
2099451b44SJordan Rupprecht        # Get the path of the executable
2199451b44SJordan Rupprecht        exe_path = self.getBuildArtifact("a.out")
2299451b44SJordan Rupprecht
2399451b44SJordan Rupprecht        # Load the executable
2499451b44SJordan Rupprecht        target = self.dbg.CreateTarget(exe_path)
2599451b44SJordan Rupprecht        self.assertTrue(target.IsValid(), VALID_TARGET)
2699451b44SJordan Rupprecht
2799451b44SJordan Rupprecht        # Break on main function
28*2238dcc3SJonas Devlieghere        break_0 = target.BreakpointCreateBySourceRegex("// break 0", src_file_spec)
2999451b44SJordan Rupprecht        self.assertTrue(
30*2238dcc3SJonas Devlieghere            break_0.IsValid() and break_0.GetNumLocations() >= 1, VALID_BREAKPOINT
31*2238dcc3SJonas Devlieghere        )
32*2238dcc3SJonas Devlieghere        break_1 = target.BreakpointCreateBySourceRegex("// break 1", src_file_spec)
3399451b44SJordan Rupprecht        self.assertTrue(
34*2238dcc3SJonas Devlieghere            break_1.IsValid() and break_1.GetNumLocations() >= 1, VALID_BREAKPOINT
35*2238dcc3SJonas Devlieghere        )
3699451b44SJordan Rupprecht
3799451b44SJordan Rupprecht        # Launch the process
3899451b44SJordan Rupprecht        args = None
3999451b44SJordan Rupprecht        env = None
40*2238dcc3SJonas Devlieghere        process = target.LaunchSimple(args, env, self.get_process_working_directory())
4199451b44SJordan Rupprecht        self.assertTrue(process.IsValid(), PROCESS_IS_VALID)
4299451b44SJordan Rupprecht
4399451b44SJordan Rupprecht        # Get the thread of the process
44*2238dcc3SJonas Devlieghere        self.assertEqual(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)
45*2238dcc3SJonas Devlieghere        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
4699451b44SJordan Rupprecht
4799451b44SJordan Rupprecht        # Get current fream of the thread at the breakpoint
4899451b44SJordan Rupprecht        frame = thread.GetSelectedFrame()
4999451b44SJordan Rupprecht
5099451b44SJordan Rupprecht        # Test imported namespaces
5199451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("n")
5299451b44SJordan Rupprecht        self.assertTrue(
53*2238dcc3SJonas Devlieghere            test_result.IsValid() and test_result.GetValueAsSigned() == 1, "n = 1"
54*2238dcc3SJonas Devlieghere        )
5599451b44SJordan Rupprecht
5699451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("N::n")
5799451b44SJordan Rupprecht        self.assertTrue(
58*2238dcc3SJonas Devlieghere            test_result.IsValid() and test_result.GetValueAsSigned() == 1, "N::n = 1"
59*2238dcc3SJonas Devlieghere        )
6099451b44SJordan Rupprecht
6199451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("nested")
6299451b44SJordan Rupprecht        self.assertTrue(
63*2238dcc3SJonas Devlieghere            test_result.IsValid() and test_result.GetValueAsSigned() == 3, "nested = 3"
64*2238dcc3SJonas Devlieghere        )
6599451b44SJordan Rupprecht
6699451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("anon")
6799451b44SJordan Rupprecht        self.assertTrue(
68*2238dcc3SJonas Devlieghere            test_result.IsValid() and test_result.GetValueAsSigned() == 2, "anon = 2"
69*2238dcc3SJonas Devlieghere        )
7099451b44SJordan Rupprecht
7199451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("global")
7299451b44SJordan Rupprecht        self.assertTrue(
73*2238dcc3SJonas Devlieghere            test_result.IsValid() and test_result.GetValueAsSigned() == 4, "global = 4"
74*2238dcc3SJonas Devlieghere        )
7599451b44SJordan Rupprecht
7699451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("fun_var")
7799451b44SJordan Rupprecht        self.assertTrue(
78*2238dcc3SJonas Devlieghere            test_result.IsValid() and test_result.GetValueAsSigned() == 9, "fun_var = 9"
79*2238dcc3SJonas Devlieghere        )
8099451b44SJordan Rupprecht
8199451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("Fun::fun_var")
8299451b44SJordan Rupprecht        self.assertTrue(
8399451b44SJordan Rupprecht            test_result.IsValid() and test_result.GetValueAsSigned() == 0,
84*2238dcc3SJonas Devlieghere            "Fun::fun_var = 0",
85*2238dcc3SJonas Devlieghere        )
8699451b44SJordan Rupprecht
8799451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("not_imported")
8899451b44SJordan Rupprecht        self.assertTrue(
8999451b44SJordan Rupprecht            test_result.IsValid() and test_result.GetValueAsSigned() == 35,
90*2238dcc3SJonas Devlieghere            "not_imported = 35",
91*2238dcc3SJonas Devlieghere        )
9299451b44SJordan Rupprecht
9399451b44SJordan Rupprecht        # Currently there is no way to distinguish between "::imported" and "imported" in ClangExpressionDeclMap so this fails
9499451b44SJordan Rupprecht        # test_result = frame.EvaluateExpression("::imported")
9599451b44SJordan Rupprecht        # self.assertTrue(test_result.IsValid() and test_result.GetValueAsSigned() == 89, "::imported = 89")
9699451b44SJordan Rupprecht
9799451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("Imported::imported")
9899451b44SJordan Rupprecht        self.assertTrue(
9999451b44SJordan Rupprecht            test_result.IsValid() and test_result.GetValueAsSigned() == 99,
100*2238dcc3SJonas Devlieghere            "Imported::imported = 99",
101*2238dcc3SJonas Devlieghere        )
10299451b44SJordan Rupprecht
10399451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("imported")
10499451b44SJordan Rupprecht        self.assertTrue(
10599451b44SJordan Rupprecht            test_result.IsValid() and test_result.GetValueAsSigned() == 99,
106*2238dcc3SJonas Devlieghere            "imported = 99",
107*2238dcc3SJonas Devlieghere        )
10899451b44SJordan Rupprecht
10999451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("single")
11099451b44SJordan Rupprecht        self.assertTrue(
111*2238dcc3SJonas Devlieghere            test_result.IsValid() and test_result.GetValueAsSigned() == 3, "single = 3"
112*2238dcc3SJonas Devlieghere        )
11399451b44SJordan Rupprecht
11499451b44SJordan Rupprecht        # Continue to second breakpoint
11599451b44SJordan Rupprecht        process.Continue()
11699451b44SJordan Rupprecht
11799451b44SJordan Rupprecht        # Get the thread of the process
118*2238dcc3SJonas Devlieghere        self.assertEqual(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)
119*2238dcc3SJonas Devlieghere        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
12099451b44SJordan Rupprecht
12199451b44SJordan Rupprecht        # Get current fream of the thread at the breakpoint
12299451b44SJordan Rupprecht        frame = thread.GetSelectedFrame()
12399451b44SJordan Rupprecht
12499451b44SJordan Rupprecht        # Test function inside namespace
12599451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("fun_var")
12699451b44SJordan Rupprecht        self.assertTrue(
127*2238dcc3SJonas Devlieghere            test_result.IsValid() and test_result.GetValueAsSigned() == 5, "fun_var = 5"
128*2238dcc3SJonas Devlieghere        )
129