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