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