1"""
2Test lldb data formatter subsystem.
3"""
4
5
6
7import lldb
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13USE_LIBSTDCPP = "USE_LIBSTDCPP"
14USE_LIBCPP = "USE_LIBCPP"
15
16class GenericMultiMapDataFormatterTestCase(TestBase):
17
18    def setUp(self):
19        TestBase.setUp(self)
20        self.namespace = 'std'
21
22    def findVariable(self, name):
23        var = self.frame().FindVariable(name)
24        self.assertTrue(var.IsValid())
25        return var
26
27    def getVariableType(self, name):
28        var = self.findVariable(name)
29        return var.GetType().GetDisplayTypeName()
30
31    def check(self, var_name, size):
32        var = self.findVariable(var_name)
33        self.assertEqual(var.GetNumChildren(), size)
34        children = []
35        for i in range(size):
36            child = var.GetChildAtIndex(i)
37            children.append(ValueCheck(value=child.GetValue()))
38        self.expect_var_path(var_name, type=self.getVariableType(var_name), children=children)
39
40    def do_test_with_run_command(self, stdlib_type):
41        """Test that that file and class static variables display correctly."""
42        self.build(dictionary={stdlib_type: "1"})
43        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
44
45        bkpt = self.target().FindBreakpointByID(
46            lldbutil.run_break_set_by_source_regexp(
47                self, "Set break point at this line."))
48
49        self.runCmd("run", RUN_SUCCEEDED)
50
51        # The stop reason of the thread should be breakpoint.
52        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
53                    substrs=['stopped',
54                             'stop reason = breakpoint'])
55
56        # This is the function to remove the custom formats in order to have a
57        # clean slate for the next test case.
58        def cleanup():
59            self.runCmd('type format clear', check=False)
60            self.runCmd('type summary clear', check=False)
61            self.runCmd('type filter clear', check=False)
62            self.runCmd('type synth clear', check=False)
63            self.runCmd(
64                "settings set target.max-children-count 256",
65                check=False)
66
67        # Execute the cleanup function during test case tear down.
68        self.addTearDownHook(cleanup)
69
70        multimap = self.namespace + "::multimap"
71
72        # We expect that in some malformed cases the map shows size 0
73        self.expect('frame variable corrupt_map',
74                    substrs=[multimap, 'size=0',
75                             '{}'])
76
77        lldbutil.continue_to_breakpoint(self.process(), bkpt)
78
79        self.expect('frame variable ii',
80                    substrs=[multimap, 'size=0',
81                             '{}'])
82
83        lldbutil.continue_to_breakpoint(self.process(), bkpt)
84
85        self.expect(
86            'frame variable ii',
87            substrs=[
88                multimap,
89                'size=2',
90                '[0] = (first = 0, second = 0)',
91                '[1] = (first = 1, second = 1)',
92            ])
93
94        self.check("ii", 2)
95
96        lldbutil.continue_to_breakpoint(self.process(), bkpt)
97
98        self.expect('frame variable ii',
99                    substrs=[multimap, 'size=4',
100                             '[2] = ',
101                             'first = 2',
102                             'second = 0',
103                             '[3] = ',
104                             'first = 3',
105                             'second = 1'])
106
107        self.check("ii", 4)
108
109        lldbutil.continue_to_breakpoint(self.process(), bkpt)
110
111        self.expect("frame variable ii",
112                    substrs=[multimap, 'size=8',
113                             '[5] = ',
114                             'first = 5',
115                             'second = 0',
116                             '[7] = ',
117                             'first = 7',
118                             'second = 1'])
119
120        self.check("ii", 8)
121
122        self.expect("p ii",
123                    substrs=[multimap, 'size=8',
124                             '[5] = ',
125                             'first = 5',
126                             'second = 0',
127                             '[7] = ',
128                             'first = 7',
129                             'second = 1'])
130
131        # check access-by-index
132        self.expect("frame variable ii[0]",
133                    substrs=['first = 0',
134                             'second = 0'])
135        self.expect("frame variable ii[3]",
136                    substrs=['first =',
137                             'second ='])
138
139        # check that MightHaveChildren() gets it right
140        self.assertTrue(
141            self.frame().FindVariable("ii").MightHaveChildren(),
142            "ii.MightHaveChildren() says False for non empty!")
143
144        # check that the expression parser does not make use of
145        # synthetic children instead of running code
146        # TOT clang has a fix for this, which makes the expression command here succeed
147        # since this would make the test fail or succeed depending on clang version in use
148        # this is safer commented for the time being
149        # self.expect("expression ii[8]", matching=False, error=True,
150        #            substrs = ['1234567'])
151
152        lldbutil.continue_to_breakpoint(self.process(), bkpt)
153
154        self.expect('frame variable ii',
155                    substrs=[multimap, 'size=0',
156                             '{}'])
157
158        self.expect('frame variable si',
159                    substrs=[multimap, 'size=0',
160                             '{}'])
161
162        lldbutil.continue_to_breakpoint(self.process(), bkpt)
163
164        self.expect('frame variable si',
165                    substrs=[multimap, 'size=1',
166                             '[0] = ',
167                             'first = \"zero\"',
168                             'second = 0'])
169
170        lldbutil.continue_to_breakpoint(self.process(), bkpt)
171
172        self.expect(
173            "frame variable si",
174            substrs=[
175                multimap,
176                'size=4',
177                '[0] = (first = "one", second = 1)',
178                '[1] = (first = "three", second = 3)',
179                '[2] = (first = "two", second = 2)',
180                '[3] = (first = "zero", second = 0)',
181            ])
182
183        self.expect("p si",
184                    substrs=[multimap, 'size=4',
185                '[0] = (first = "one", second = 1)',
186                '[1] = (first = "three", second = 3)',
187                '[2] = (first = "two", second = 2)',
188                '[3] = (first = "zero", second = 0)',
189            ])
190
191        # check that MightHaveChildren() gets it right
192        self.assertTrue(
193            self.frame().FindVariable("si").MightHaveChildren(),
194            "si.MightHaveChildren() says False for non empty!")
195
196        # check access-by-index
197        self.expect("frame variable si[0]",
198                    substrs=['first = ', 'one',
199                             'second = 1'])
200
201        # check that the expression parser does not make use of
202        # synthetic children instead of running code
203        # TOT clang has a fix for this, which makes the expression command here succeed
204        # since this would make the test fail or succeed depending on clang version in use
205        # this is safer commented for the time being
206        # self.expect("expression si[0]", matching=False, error=True,
207        #            substrs = ['first = ', 'zero'])
208
209        lldbutil.continue_to_breakpoint(self.process(), bkpt)
210
211        self.expect('frame variable si',
212                    substrs=[multimap, 'size=0',
213                             '{}'])
214
215        lldbutil.continue_to_breakpoint(self.process(), bkpt)
216
217        self.expect('frame variable is',
218                    substrs=[multimap, 'size=0',
219                             '{}'])
220
221        lldbutil.continue_to_breakpoint(self.process(), bkpt)
222
223        self.expect(
224            "frame variable is",
225            substrs=[
226                multimap,
227                'size=4',
228                '[0] = (first = 1, second = "is")',
229                '[1] = (first = 2, second = "smart")',
230                '[2] = (first = 3, second = "!!!")',
231                '[3] = (first = 85, second = "goofy")',
232            ])
233
234        self.expect(
235            "p is",
236            substrs=[
237                multimap,
238                'size=4',
239                '[0] = (first = 1, second = "is")',
240                '[1] = (first = 2, second = "smart")',
241                '[2] = (first = 3, second = "!!!")',
242                '[3] = (first = 85, second = "goofy")',
243            ])
244
245        # check that MightHaveChildren() gets it right
246        self.assertTrue(
247            self.frame().FindVariable("is").MightHaveChildren(),
248            "is.MightHaveChildren() says False for non empty!")
249
250        # check access-by-index
251        self.expect("frame variable is[0]",
252                    substrs=['first = ',
253                             'second ='])
254
255        # check that the expression parser does not make use of
256        # synthetic children instead of running code
257        # TOT clang has a fix for this, which makes the expression command here succeed
258        # since this would make the test fail or succeed depending on clang version in use
259        # this is safer commented for the time being
260        # self.expect("expression is[0]", matching=False, error=True,
261        #            substrs = ['first = ', 'goofy'])
262
263        lldbutil.continue_to_breakpoint(self.process(), bkpt)
264
265        self.expect('frame variable is',
266                    substrs=[multimap, 'size=0',
267                             '{}'])
268
269        self.check("is", 0)
270
271        lldbutil.continue_to_breakpoint(self.process(), bkpt)
272
273        self.expect('frame variable ss',
274                    substrs=[multimap, 'size=0',
275                             '{}'])
276
277        self.check("ss", 0)
278
279        lldbutil.continue_to_breakpoint(self.process(), bkpt)
280
281        self.expect(
282            "frame variable ss",
283            substrs=[
284                multimap,
285                'size=3',
286                '[0] = (first = "casa", second = "house")',
287                '[1] = (first = "ciao", second = "hello")',
288                '[2] = (first = "gatto", second = "cat")',
289            ])
290
291        self.check("ss", 3)
292
293        self.expect(
294            "p ss",
295            substrs=[
296                multimap,
297                'size=3',
298                '[0] = (first = "casa", second = "house")',
299                '[1] = (first = "ciao", second = "hello")',
300                '[2] = (first = "gatto", second = "cat")',
301            ])
302
303        # check that MightHaveChildren() gets it right
304        self.assertTrue(
305            self.frame().FindVariable("ss").MightHaveChildren(),
306            "ss.MightHaveChildren() says False for non empty!")
307
308        # check access-by-index
309        self.expect("frame variable ss[2]",
310                    substrs=['gatto', 'cat'])
311
312        # check that the expression parser does not make use of
313        # synthetic children instead of running code
314        # TOT clang has a fix for this, which makes the expression command here succeed
315        # since this would make the test fail or succeed depending on clang version in use
316        # this is safer commented for the time being
317        # self.expect("expression ss[3]", matching=False, error=True,
318        #            substrs = ['gatto'])
319
320        lldbutil.continue_to_breakpoint(self.process(), bkpt)
321
322        self.expect('frame variable ss',
323                    substrs=[multimap, 'size=0',
324                             '{}'])
325
326        self.check("ss", 0)
327
328    @add_test_categories(["libstdcxx"])
329    @skipIf(compiler="clang", compiler_version=['<', '9.0'])
330    def test_with_run_command_libstdcpp(self):
331        self.do_test_with_run_command(USE_LIBSTDCPP)
332
333    @skipIf(compiler="clang", compiler_version=['<', '9.0'])
334    @add_test_categories(["libc++"])
335    def test_with_run_command_libcpp(self):
336        self.do_test_with_run_command(USE_LIBCPP)
337