xref: /llvm-project/lldb/test/API/lang/cpp/global_operators/TestCppGlobalOperators.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Test that global operators are found and evaluated.
3"""
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class TestCppGlobalOperators(TestBase):
11    def prepare_executable_and_get_frame(self):
12        self.build()
13
14        # Get main source file
15        src_file = "main.cpp"
16        src_file_spec = lldb.SBFileSpec(src_file)
17        self.assertTrue(src_file_spec.IsValid(), "Main source file")
18
19        # Get the path of the executable
20        exe_path = self.getBuildArtifact("a.out")
21
22        # Load the executable
23        target = self.dbg.CreateTarget(exe_path)
24        self.assertTrue(target.IsValid(), VALID_TARGET)
25
26        # Break on main function
27        main_breakpoint = target.BreakpointCreateBySourceRegex(
28            "// break here", src_file_spec
29        )
30        self.assertTrue(
31            main_breakpoint.IsValid() and main_breakpoint.GetNumLocations() >= 1,
32            VALID_BREAKPOINT,
33        )
34
35        # Launch the process
36        args = None
37        env = None
38        process = target.LaunchSimple(args, env, self.get_process_working_directory())
39        self.assertTrue(process.IsValid(), PROCESS_IS_VALID)
40
41        # Get the thread of the process
42        self.assertEqual(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)
43        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
44
45        return thread.GetSelectedFrame()
46
47    def test_equals_operator(self):
48        frame = self.prepare_executable_and_get_frame()
49
50        test_result = frame.EvaluateExpression("operator==(s1, s2)")
51        self.assertTrue(
52            test_result.IsValid() and test_result.GetValue() == "false",
53            "operator==(s1, s2) = false",
54        )
55
56        test_result = frame.EvaluateExpression("operator==(s1, s3)")
57        self.assertTrue(
58            test_result.IsValid() and test_result.GetValue() == "true",
59            "operator==(s1, s3) = true",
60        )
61
62        test_result = frame.EvaluateExpression("operator==(s2, s3)")
63        self.assertTrue(
64            test_result.IsValid() and test_result.GetValue() == "false",
65            "operator==(s2, s3) = false",
66        )
67
68    def do_new_test(self, frame, expr, expected_value_name):
69        """Evaluate a new expression, and check its result"""
70
71        expected_value = frame.FindValue(
72            expected_value_name, lldb.eValueTypeVariableGlobal
73        )
74        self.assertTrue(expected_value.IsValid())
75
76        expected_value_addr = expected_value.AddressOf()
77        self.assertTrue(expected_value_addr.IsValid())
78
79        got = frame.EvaluateExpression(expr)
80        self.assertTrue(got.IsValid())
81        self.assertEqual(
82            got.GetValueAsUnsigned(), expected_value_addr.GetValueAsUnsigned()
83        )
84        got_type = got.GetType()
85        self.assertTrue(got_type.IsPointerType())
86        self.assertEqual(got_type.GetPointeeType().GetName(), "Struct")
87
88    def test_operator_new(self):
89        frame = self.prepare_executable_and_get_frame()
90
91        self.do_new_test(frame, "new Struct()", "global_new_buf")
92        self.do_new_test(frame, "new(new_tag) Struct()", "tagged_new_buf")
93