1"""
2Test lldb data formatter subsystem.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class StdMapDataFormatterTestCase(TestBase):
14
15    def setUp(self):
16        # Call super's setUp().
17        TestBase.setUp(self)
18        # Find the line number to break at.
19        self.line = line_number('main.cpp', '// Set break point at this line.')
20
21    @add_test_categories(["libstdcxx"])
22    @expectedFailureAll(bugnumber="llvm.org/pr50861", compiler="gcc")
23    def test_with_run_command(self):
24        """Test that that file and class static variables display correctly."""
25        self.build()
26        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
27
28        lldbutil.run_break_set_by_source_regexp(
29            self, "Set break point at this line.")
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            self.runCmd(
46                "settings set target.max-children-count 256",
47                check=False)
48
49        # Execute the cleanup function during test case tear down.
50        self.addTearDownHook(cleanup)
51
52        self.runCmd("frame variable ii --show-types")
53
54        self.runCmd(
55            "type summary add -x \"std::map<\" --summary-string \"map has ${svar%#} items\" -e")
56
57        self.expect('frame variable ii',
58                    substrs=['map has 0 items',
59                             '{}'])
60
61        self.runCmd("c")
62
63        self.expect('frame variable ii',
64                    substrs=['map has 2 items',
65                             '[0] = ',
66                             'first = 0',
67                             'second = 0',
68                             '[1] = ',
69                             'first = 1',
70                             'second = 1'])
71
72        self.runCmd("c")
73
74        self.expect('frame variable ii',
75                    substrs=['map has 4 items',
76                             '[2] = ',
77                             'first = 2',
78                             'second = 0',
79                             '[3] = ',
80                             'first = 3',
81                             'second = 1'])
82
83        self.runCmd("c")
84
85        self.expect("frame variable ii",
86                    substrs=['map has 9 items',
87                             '[5] = ',
88                             'first = 5',
89                             'second = 0',
90                             '[7] = ',
91                             'first = 7',
92                             'second = 1'])
93
94        self.expect("p ii",
95                    substrs=['map has 9 items',
96                             '[5] = ',
97                             'first = 5',
98                             'second = 0',
99                             '[7] = ',
100                             'first = 7',
101                             'second = 1'])
102
103        # check access-by-index
104        self.expect("frame variable ii[0]",
105                    substrs=['first = 0',
106                             'second = 0'])
107        self.expect("frame variable ii[3]",
108                    substrs=['first =',
109                             'second ='])
110
111        self.expect("frame variable ii[8]", matching=True,
112                    substrs=['1234567'])
113
114        # check that MightHaveChildren() gets it right
115        self.assertTrue(
116            self.frame().FindVariable("ii").MightHaveChildren(),
117            "ii.MightHaveChildren() says False for non empty!")
118
119        # check that the expression parser does not make use of
120        # synthetic children instead of running code
121        # TOT clang has a fix for this, which makes the expression command here succeed
122        # since this would make the test fail or succeed depending on clang version in use
123        # this is safer commented for the time being
124        # self.expect("expression ii[8]", matching=False, error=True,
125        #            substrs = ['1234567'])
126
127        self.runCmd("c")
128
129        self.expect('frame variable ii',
130                    substrs=['map has 0 items',
131                             '{}'])
132
133        self.runCmd("frame variable si --show-types")
134
135        self.expect('frame variable si',
136                    substrs=['map has 0 items',
137                             '{}'])
138
139        self.runCmd("c")
140
141        self.expect('frame variable si',
142                    substrs=['map has 1 items',
143                             '[0] = ',
144                             'first = \"zero\"',
145                             'second = 0'])
146
147        self.runCmd("c")
148
149        self.expect(
150            "frame variable si",
151            substrs=[
152                'map has 5 items',
153                '[0] = (first = "four", second = 4)',
154                '[1] = (first = "one", second = 1)',
155                '[2] = (first = "three", second = 3)',
156                '[3] = (first = "two", second = 2)',
157                '[4] = (first = "zero", second = 0)',
158            ])
159
160        self.expect(
161            "p si",
162            substrs=[
163                'map has 5 items',
164                '[0] = (first = "four", second = 4)',
165                '[1] = (first = "one", second = 1)',
166                '[2] = (first = "three", second = 3)',
167                '[3] = (first = "two", second = 2)',
168                '[4] = (first = "zero", second = 0)',
169            ])
170
171        # check access-by-index
172        self.expect("frame variable si[0]",
173                    substrs=['first = ', 'four',
174                             'second = 4'])
175
176        # check that MightHaveChildren() gets it right
177        self.assertTrue(
178            self.frame().FindVariable("si").MightHaveChildren(),
179            "si.MightHaveChildren() says False for non empty!")
180
181        # check that the expression parser does not make use of
182        # synthetic children instead of running code
183        # TOT clang has a fix for this, which makes the expression command here succeed
184        # since this would make the test fail or succeed depending on clang version in use
185        # this is safer commented for the time being
186        # self.expect("expression si[0]", matching=False, error=True,
187        #            substrs = ['first = ', 'zero'])
188
189        self.runCmd("c")
190
191        self.expect('frame variable si',
192                    substrs=['map has 0 items',
193                             '{}'])
194
195        self.runCmd("frame variable is --show-types")
196
197        self.expect('frame variable is',
198                    substrs=['map has 0 items',
199                             '{}'])
200
201        self.runCmd("c")
202
203        self.expect(
204            "frame variable is",
205            substrs=[
206                'map has 4 items', '[0] = (first = 1, second = "is")',
207                '[1] = (first = 2, second = "smart")',
208                '[2] = (first = 3, second = "!!!")',
209                '[3] = (first = 85, second = "goofy")'
210            ])
211
212        self.expect(
213            "p is",
214            substrs=[
215                'map has 4 items', '[0] = (first = 1, second = "is")',
216                '[1] = (first = 2, second = "smart")',
217                '[2] = (first = 3, second = "!!!")',
218                '[3] = (first = 85, second = "goofy")'
219            ])
220
221        # check access-by-index
222        self.expect("frame variable is[0]",
223                    substrs=['first = ',
224                             'second ='])
225
226        # check that MightHaveChildren() gets it right
227        self.assertTrue(
228            self.frame().FindVariable("is").MightHaveChildren(),
229            "is.MightHaveChildren() says False for non empty!")
230
231        # check that the expression parser does not make use of
232        # synthetic children instead of running code
233        # TOT clang has a fix for this, which makes the expression command here succeed
234        # since this would make the test fail or succeed depending on clang version in use
235        # this is safer commented for the time being
236        # self.expect("expression is[0]", matching=False, error=True,
237        #            substrs = ['first = ', 'goofy'])
238
239        self.runCmd("c")
240
241        self.expect('frame variable is',
242                    substrs=['map has 0 items',
243                             '{}'])
244
245        self.runCmd("frame variable ss --show-types")
246
247        self.expect('frame variable ss',
248                    substrs=['map has 0 items',
249                             '{}'])
250
251        self.runCmd("c")
252
253        self.expect(
254            "frame variable ss",
255            substrs=[
256                'map has 4 items',
257                '[0] = (first = "a Mac..", second = "..is always a Mac!")',
258                '[1] = (first = "casa", second = "house")',
259                '[2] = (first = "ciao", second = "hello")',
260                '[3] = (first = "gatto", second = "cat")'
261            ])
262
263        self.expect(
264            "p ss",
265            substrs=[
266                'map has 4 items',
267                '[0] = (first = "a Mac..", second = "..is always a Mac!")',
268                '[1] = (first = "casa", second = "house")',
269                '[2] = (first = "ciao", second = "hello")',
270                '[3] = (first = "gatto", second = "cat")'
271            ])
272
273        # check access-by-index
274        self.expect("frame variable ss[3]",
275                    substrs=['gatto', 'cat'])
276
277        # check that MightHaveChildren() gets it right
278        self.assertTrue(
279            self.frame().FindVariable("ss").MightHaveChildren(),
280            "ss.MightHaveChildren() says False for non empty!")
281
282        # check that the expression parser does not make use of
283        # synthetic children instead of running code
284        # TOT clang has a fix for this, which makes the expression command here succeed
285        # since this would make the test fail or succeed depending on clang version in use
286        # this is safer commented for the time being
287        # self.expect("expression ss[3]", matching=False, error=True,
288        #            substrs = ['gatto'])
289
290        self.runCmd("c")
291
292        self.expect('frame variable ss',
293                    substrs=['map has 0 items',
294                             '{}'])
295