1"""Test that lldb can invoke blocks and access variables inside them"""
2
3
4
5import unittest2
6import lldb
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test.decorators import *
9import lldbsuite.test.lldbutil as lldbutil
10
11
12class BlocksTestCase(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15    lines = []
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20        # Find the line numbers to break at.
21        self.lines.append(line_number('main.c', '// Set breakpoint 0 here.'))
22        self.lines.append(line_number('main.c', '// Set breakpoint 1 here.'))
23
24    def launch_common(self):
25        self.build()
26        exe = self.getBuildArtifact("a.out")
27        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
28
29        self.is_started = False
30
31        # Break inside the foo function which takes a bar_ptr argument.
32        for line in self.lines:
33            lldbutil.run_break_set_by_file_and_line(
34                self, "main.c", line, num_expected_locations=1, loc_exact=True)
35
36        self.wait_for_breakpoint()
37
38    @skipUnlessDarwin
39    def test_expr(self):
40        self.launch_common()
41
42        self.expect("expression a + b", VARIABLES_DISPLAYED_CORRECTLY,
43                    substrs=["= 7"])
44
45        self.expect("expression c", VARIABLES_DISPLAYED_CORRECTLY,
46                    substrs=["= 1"])
47
48        self.wait_for_breakpoint()
49
50        # This should display correctly.
51        self.expect("expression (int)neg (-12)", VARIABLES_DISPLAYED_CORRECTLY,
52                    substrs=["= 12"])
53
54    @skipUnlessDarwin
55    def test_define(self):
56        self.launch_common()
57
58        self.runCmd(
59            "expression int (^$add)(int, int) = ^int(int a, int b) { return a + b; };")
60        self.expect(
61            "expression $add(2,3)",
62            VARIABLES_DISPLAYED_CORRECTLY,
63            substrs=[" = 5"])
64
65        self.runCmd("expression int $a = 3")
66        self.expect(
67            "expression int (^$addA)(int) = ^int(int b) { return $a + b; };",
68            "Proper error is reported on capture",
69            error=True)
70
71    def wait_for_breakpoint(self):
72        if not self.is_started:
73            self.is_started = True
74            self.runCmd("process launch", RUN_SUCCEEDED)
75        else:
76            self.runCmd("process continue", RUN_SUCCEEDED)
77
78        # The stop reason of the thread should be breakpoint.
79        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
80                    substrs=['stopped',
81                             'stop reason = breakpoint'])
82