xref: /llvm-project/lldb/test/API/lang/objc/objc-checker/TestObjCCheckers.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Use lldb Python API to make sure the dynamic checkers are doing their jobs.
3"""
4
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class ObjCCheckerTestCase(TestBase):
13    NO_DEBUG_INFO_TESTCASE = True
14
15    def setUp(self):
16        # Call super's setUp().
17        TestBase.setUp(self)
18
19        # Find the line number to break for main.c.
20        self.source_name = "main.m"
21
22    @add_test_categories(["pyapi"])
23    def test_objc_checker(self):
24        """Test that checkers catch unrecognized selectors"""
25        if self.getArchitecture() == "i386":
26            self.skipTest("requires Objective-C 2.0 runtime")
27
28        self.build()
29        exe = self.getBuildArtifact("a.out")
30
31        # Create a target from the debugger.
32
33        target = self.dbg.CreateTarget(exe)
34        self.assertTrue(target, VALID_TARGET)
35
36        # Set up our breakpoints:
37
38        main_bkpt = target.BreakpointCreateBySourceRegex(
39            "Set a breakpoint here.", lldb.SBFileSpec(self.source_name)
40        )
41        self.assertTrue(
42            main_bkpt and main_bkpt.GetNumLocations() == 1, VALID_BREAKPOINT
43        )
44
45        # Now launch the process, and do not stop at the entry point.
46        process = target.LaunchSimple(None, None, self.get_process_working_directory())
47
48        self.assertState(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)
49
50        threads = lldbutil.get_threads_stopped_at_breakpoint(process, main_bkpt)
51        self.assertEqual(len(threads), 1)
52        thread = threads[0]
53
54        #
55        #  The class Simple doesn't have a count method.  Make sure that we don't
56        #  actually try to send count but catch it as an unrecognized selector.
57
58        frame = thread.GetFrameAtIndex(0)
59        expr_value = frame.EvaluateExpression("(int) [my_simple count]", False)
60        expr_error = expr_value.GetError()
61
62        self.assertTrue(expr_error.Fail())
63
64        # Make sure the call produced no NSLog stdout.
65        stdout = process.GetSTDOUT(100)
66        self.assertTrue(stdout is None or (len(stdout) == 0))
67
68        # Make sure the error is helpful:
69        err_string = expr_error.GetCString()
70        self.assertIn("selector", err_string)
71
72        #
73        # Check that we correctly insert the checker for an
74        # ObjC method with the struct return convention.
75        # Getting this wrong would cause us to call the checker
76        # with the wrong arguments, and the checker would crash
77        # So I'm just checking "expression runs successfully" here:
78        #
79        expr_value = frame.EvaluateExpression("[my_simple getBigStruct]", False)
80        expr_error = expr_value.GetError()
81
82        self.assertSuccess(expr_error)
83