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