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