xref: /llvm-project/lldb/test/API/lang/cpp/global_operators/TestCppGlobalOperators.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest that global operators are found and evaluated.
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 TestCppGlobalOperators(TestBase):
1199451b44SJordan Rupprecht    def prepare_executable_and_get_frame(self):
1299451b44SJordan Rupprecht        self.build()
1399451b44SJordan Rupprecht
1499451b44SJordan Rupprecht        # Get main source file
1599451b44SJordan Rupprecht        src_file = "main.cpp"
1699451b44SJordan Rupprecht        src_file_spec = lldb.SBFileSpec(src_file)
1799451b44SJordan Rupprecht        self.assertTrue(src_file_spec.IsValid(), "Main source file")
1899451b44SJordan Rupprecht
1999451b44SJordan Rupprecht        # Get the path of the executable
2099451b44SJordan Rupprecht        exe_path = self.getBuildArtifact("a.out")
2199451b44SJordan Rupprecht
2299451b44SJordan Rupprecht        # Load the executable
2399451b44SJordan Rupprecht        target = self.dbg.CreateTarget(exe_path)
2499451b44SJordan Rupprecht        self.assertTrue(target.IsValid(), VALID_TARGET)
2599451b44SJordan Rupprecht
2699451b44SJordan Rupprecht        # Break on main function
2799451b44SJordan Rupprecht        main_breakpoint = target.BreakpointCreateBySourceRegex(
28*2238dcc3SJonas Devlieghere            "// break here", src_file_spec
29*2238dcc3SJonas Devlieghere        )
3099451b44SJordan Rupprecht        self.assertTrue(
3199451b44SJordan Rupprecht            main_breakpoint.IsValid() and main_breakpoint.GetNumLocations() >= 1,
32*2238dcc3SJonas Devlieghere            VALID_BREAKPOINT,
33*2238dcc3SJonas Devlieghere        )
3499451b44SJordan Rupprecht
3599451b44SJordan Rupprecht        # Launch the process
3699451b44SJordan Rupprecht        args = None
3799451b44SJordan Rupprecht        env = None
38*2238dcc3SJonas Devlieghere        process = target.LaunchSimple(args, env, self.get_process_working_directory())
3999451b44SJordan Rupprecht        self.assertTrue(process.IsValid(), PROCESS_IS_VALID)
4099451b44SJordan Rupprecht
4199451b44SJordan Rupprecht        # Get the thread of the process
42*2238dcc3SJonas Devlieghere        self.assertEqual(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)
43*2238dcc3SJonas Devlieghere        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
4499451b44SJordan Rupprecht
4599451b44SJordan Rupprecht        return thread.GetSelectedFrame()
4699451b44SJordan Rupprecht
4799451b44SJordan Rupprecht    def test_equals_operator(self):
4899451b44SJordan Rupprecht        frame = self.prepare_executable_and_get_frame()
4999451b44SJordan Rupprecht
5099451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("operator==(s1, s2)")
5199451b44SJordan Rupprecht        self.assertTrue(
5299451b44SJordan Rupprecht            test_result.IsValid() and test_result.GetValue() == "false",
53*2238dcc3SJonas Devlieghere            "operator==(s1, s2) = false",
54*2238dcc3SJonas Devlieghere        )
5599451b44SJordan Rupprecht
5699451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("operator==(s1, s3)")
5799451b44SJordan Rupprecht        self.assertTrue(
5899451b44SJordan Rupprecht            test_result.IsValid() and test_result.GetValue() == "true",
59*2238dcc3SJonas Devlieghere            "operator==(s1, s3) = true",
60*2238dcc3SJonas Devlieghere        )
6199451b44SJordan Rupprecht
6299451b44SJordan Rupprecht        test_result = frame.EvaluateExpression("operator==(s2, s3)")
6399451b44SJordan Rupprecht        self.assertTrue(
6499451b44SJordan Rupprecht            test_result.IsValid() and test_result.GetValue() == "false",
65*2238dcc3SJonas Devlieghere            "operator==(s2, s3) = false",
66*2238dcc3SJonas Devlieghere        )
6799451b44SJordan Rupprecht
6899451b44SJordan Rupprecht    def do_new_test(self, frame, expr, expected_value_name):
6999451b44SJordan Rupprecht        """Evaluate a new expression, and check its result"""
7099451b44SJordan Rupprecht
7199451b44SJordan Rupprecht        expected_value = frame.FindValue(
72*2238dcc3SJonas Devlieghere            expected_value_name, lldb.eValueTypeVariableGlobal
73*2238dcc3SJonas Devlieghere        )
7499451b44SJordan Rupprecht        self.assertTrue(expected_value.IsValid())
7599451b44SJordan Rupprecht
7699451b44SJordan Rupprecht        expected_value_addr = expected_value.AddressOf()
7799451b44SJordan Rupprecht        self.assertTrue(expected_value_addr.IsValid())
7899451b44SJordan Rupprecht
7999451b44SJordan Rupprecht        got = frame.EvaluateExpression(expr)
8099451b44SJordan Rupprecht        self.assertTrue(got.IsValid())
8199451b44SJordan Rupprecht        self.assertEqual(
82*2238dcc3SJonas Devlieghere            got.GetValueAsUnsigned(), expected_value_addr.GetValueAsUnsigned()
83*2238dcc3SJonas Devlieghere        )
8499451b44SJordan Rupprecht        got_type = got.GetType()
8599451b44SJordan Rupprecht        self.assertTrue(got_type.IsPointerType())
8699451b44SJordan Rupprecht        self.assertEqual(got_type.GetPointeeType().GetName(), "Struct")
8799451b44SJordan Rupprecht
8899451b44SJordan Rupprecht    def test_operator_new(self):
8999451b44SJordan Rupprecht        frame = self.prepare_executable_and_get_frame()
9099451b44SJordan Rupprecht
9199451b44SJordan Rupprecht        self.do_new_test(frame, "new Struct()", "global_new_buf")
9299451b44SJordan Rupprecht        self.do_new_test(frame, "new(new_tag) Struct()", "tagged_new_buf")
93