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