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