1"""
2Test lldb data formatter subsystem.
3"""
4
5from __future__ import print_function
6
7
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class DataFormatterSynthValueTestCase(TestBase):
15
16    mydir = TestBase.compute_mydir(__file__)
17
18    def setUp(self):
19        # Call super's setUp().
20        TestBase.setUp(self)
21        # Find the line number to break at.
22        self.line = line_number('main.cpp', 'break here')
23
24    def test_with_run_command(self):
25        """Test using Python synthetic children provider to provide a value."""
26        self.build()
27        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
28
29        lldbutil.run_break_set_by_file_and_line(
30            self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
31
32        self.runCmd("run", RUN_SUCCEEDED)
33
34        # The stop reason of the thread should be breakpoint.
35        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
36                    substrs=['stopped',
37                             'stop reason = breakpoint'])
38
39        # This is the function to remove the custom formats in order to have a
40        # clean slate for the next test case.
41        def cleanup():
42            self.runCmd('type format clear', check=False)
43            self.runCmd('type summary clear', check=False)
44            self.runCmd('type filter clear', check=False)
45            self.runCmd('type synth clear', check=False)
46
47        # Execute the cleanup function during test case tear down.
48        self.addTearDownHook(cleanup)
49
50        x = self.frame().FindVariable("x")
51        x.SetPreferSyntheticValue(True)
52        y = self.frame().FindVariable("y")
53        y.SetPreferSyntheticValue(True)
54        z = self.frame().FindVariable("z")
55        z.SetPreferSyntheticValue(True)
56        q = self.frame().FindVariable("q")
57        z.SetPreferSyntheticValue(True)
58
59        x_val = x.GetValueAsUnsigned
60        y_val = y.GetValueAsUnsigned
61        z_val = z.GetValueAsUnsigned
62        q_val = q.GetValueAsUnsigned
63
64        if self.TraceOn():
65            print(
66                "x_val = %s; y_val = %s; z_val = %s; q_val = %s" %
67                (x_val(), y_val(), z_val(), q_val()))
68
69        self.assertNotEqual(x_val(), 3, "x == 3 before synthetics")
70        self.assertNotEqual(y_val(), 4, "y == 4 before synthetics")
71        self.assertNotEqual(z_val(), 7, "z == 7 before synthetics")
72        self.assertNotEqual(q_val(), 8, "q == 8 before synthetics")
73
74        # now set up the synth
75        self.runCmd("script from myIntSynthProvider import *")
76        self.runCmd("type synth add -l myIntSynthProvider myInt")
77        self.runCmd("type synth add -l myArraySynthProvider myArray")
78        self.runCmd("type synth add -l myIntSynthProvider myIntAndStuff")
79
80        if self.TraceOn():
81            print(
82                "x_val = %s; y_val = %s; z_val = %s; q_val = %s" %
83                (x_val(), y_val(), z_val(), q_val()))
84
85        self.assertEqual(x_val(), 3, "x != 3 after synthetics")
86        self.assertEqual(y_val(), 4, "y != 4 after synthetics")
87        self.assertEqual(z_val(), 7, "z != 7 after synthetics")
88        self.assertEqual(q_val(), 8, "q != 8 after synthetics")
89
90        self.expect("frame variable x", substrs=['3'])
91        self.expect(
92            "frame variable x",
93            substrs=['theValue = 3'],
94            matching=False)
95        self.expect("frame variable q", substrs=['8'])
96        self.expect(
97            "frame variable q",
98            substrs=['theValue = 8'],
99            matching=False)
100
101        # check that an aptly defined synthetic provider does not affect
102        # one-lining
103        self.expect(
104            "expression struct Struct { myInt theInt{12}; }; Struct()",
105            substrs=['(theInt = 12)'])
106
107        # check that we can use a synthetic value in a summary
108        self.runCmd("type summary add hasAnInt -s ${var.theInt}")
109        hi = self.frame().FindVariable("hi")
110        self.assertEqual(hi.GetSummary(), "42")
111
112        ma = self.frame().FindVariable("ma")
113        self.assertTrue(ma.IsValid())
114        self.assertEqual(ma.GetNumChildren(15), 15)
115        self.assertEqual(ma.GetNumChildren(16), 16)
116        self.assertEqual(ma.GetNumChildren(17), 16)
117