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 CppDataFormatterTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20        # Find the line number to break at.
21        self.line = line_number('main.cpp', '// Set break point at this line.')
22
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_file_and_line(
29            self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
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        self.expect("frame variable",
39                    substrs=['(Speed) SPILookHex = 5.55'  # Speed by default is 5.55.
40                             ])
41
42        # This is the function to remove the custom formats in order to have a
43        # clean slate for the next test case.
44        def cleanup():
45            self.runCmd('type format clear', check=False)
46            self.runCmd('type summary clear', check=False)
47
48        # Execute the cleanup function during test case tear down.
49        self.addTearDownHook(cleanup)
50
51        self.runCmd("type format add -C yes -f x Speed BitField")
52        self.runCmd("type format add -C no -f c RealNumber")
53        self.runCmd("type format add -C no -f x Type2")
54        self.runCmd("type format add -C yes -f c Type1")
55
56        # The type format list should show our custom formats.
57        self.expect(
58            "type format list",
59            substrs=['Speed', 'BitField', 'RealNumber', 'Type2', 'Type1'])
60
61        self.expect("frame variable",
62                    patterns=['\(Speed\) SPILookHex = 0x[0-9a-f]+'  # Speed should look hex-ish now.
63                              ])
64
65        # gcc4.2 on Mac OS X skips typedef chains in the DWARF output
66        if self.getCompiler() in ['clang', 'llvm-gcc']:
67            self.expect("frame variable",
68                        patterns=['\(SignalMask\) SMILookHex = 0x[0-9a-f]+'  # SignalMask should look hex-ish now.
69                                  ])
70            self.expect("frame variable", matching=False,
71                        patterns=['\(Type4\) T4ILookChar = 0x[0-9a-f]+'  # Type4 should NOT look hex-ish now.
72                                  ])
73
74        # Now let's delete the 'Speed' custom format.
75        self.runCmd("type format delete Speed")
76
77        # The type format list should not show 'Speed' at this point.
78        self.expect("type format list", matching=False,
79                    substrs=['Speed'])
80
81        # Delete type format for 'Speed', we should expect an error message.
82        self.expect("type format delete Speed", error=True,
83                    substrs=['no custom formatter for Speed'])
84
85        self.runCmd(
86            "type summary add --summary-string \"arr = ${var%s}\" -x \"char\\[[0-9]+\\]\" -v")
87
88        self.expect("frame variable strarr",
89                    substrs=['arr = "Hello world!"'])
90
91        self.runCmd("type summary clear")
92
93        self.runCmd(
94            "type summary add --summary-string \"ptr = ${var%s}\" \"char *\" -v")
95
96        self.expect("frame variable strptr",
97                    substrs=['ptr = "Hello world!"'])
98
99        self.runCmd(
100            "type summary add --summary-string \"arr = ${var%s}\" -x \"char\\[[0-9]+\\]\" -v")
101
102        self.expect("frame variable strarr",
103                    substrs=['arr = "Hello world!'])
104
105        # check that rdar://problem/10011145 (Standard summary format for
106        # char[] doesn't work as the result of "expr".) is solved
107        self.expect("p strarr",
108                    substrs=['arr = "Hello world!'])
109
110        self.expect("frame variable strptr",
111                    substrs=['ptr = "Hello world!"'])
112
113        self.expect("p strptr",
114                    substrs=['ptr = "Hello world!"'])
115
116        self.expect(
117            "p (char*)\"1234567890123456789012345678901234567890123456789012345678901234ABC\"",
118            substrs=[
119                '(char *) $',
120                ' = ptr = ',
121                ' "1234567890123456789012345678901234567890123456789012345678901234ABC"'])
122
123        self.runCmd("type summary add -c TestPoint")
124
125        self.expect("frame variable iAmSomewhere",
126                    substrs=['x = 4',
127                             'y = 6'])
128
129        self.expect("type summary list",
130                    substrs=['TestPoint',
131                             'one-line'])
132
133        self.runCmd("type summary add --summary-string \"y=${var.y%x}\" TestPoint")
134
135        self.expect("frame variable iAmSomewhere",
136                    substrs=['y=0x'])
137
138        self.runCmd(
139            "type summary add --summary-string \"y=${var.y},x=${var.x}\" TestPoint")
140
141        self.expect("frame variable iAmSomewhere",
142                    substrs=['y=6',
143                             'x=4'])
144
145        self.runCmd("type summary add --summary-string \"hello\" TestPoint -e")
146
147        self.expect("type summary list",
148                    substrs=['TestPoint',
149                             'show children'])
150
151        self.expect("frame variable iAmSomewhere",
152                    substrs=['hello',
153                             'x = 4',
154                             '}'])
155
156        self.runCmd(
157            "type summary add --summary-string \"Sign: ${var[31]%B} Exponent: ${var[23-30]%x} Mantissa: ${var[0-22]%u}\" ShowMyGuts")
158
159        self.expect("frame variable cool_pointer->floating",
160                    substrs=['Sign: true',
161                             'Exponent: 0x',
162                             '80'])
163
164        self.runCmd("type summary add --summary-string \"a test\" i_am_cool")
165
166        self.expect("frame variable cool_pointer",
167                    substrs=['a test'])
168
169        self.runCmd(
170            "type summary add --summary-string \"a test\" i_am_cool --skip-pointers")
171
172        self.expect("frame variable cool_pointer",
173                    substrs=['a test'],
174                    matching=False)
175
176        self.runCmd(
177            "type summary add --summary-string \"${var[1-3]}\" \"int[5]\"")
178
179        self.expect("frame variable int_array",
180                    substrs=['2',
181                             '3',
182                             '4'])
183
184        self.runCmd("type summary clear")
185
186        self.runCmd(
187            "type summary add --summary-string \"${var[0-2].integer}\" \"i_am_cool *\"")
188        self.runCmd(
189            "type summary add --summary-string \"${var[2-4].integer}\" \"i_am_cool[5]\"")
190
191        self.expect("frame variable cool_array",
192                    substrs=['1,1,6'])
193
194        self.expect("frame variable cool_pointer",
195                    substrs=['3,0,0'])
196
197        # test special symbols for formatting variables into summaries
198        self.runCmd(
199            "type summary add --summary-string \"cool object @ ${var%L}\" i_am_cool")
200        self.runCmd("type summary delete \"i_am_cool[5]\"")
201
202        # this test might fail if the compiler tries to store
203        # these values into registers.. hopefully this is not
204        # going to be the case
205        self.expect("frame variable cool_array",
206                    substrs=['[0] = cool object @ 0x',
207                             '[1] = cool object @ 0x',
208                             '[2] = cool object @ 0x',
209                             '[3] = cool object @ 0x',
210                             '[4] = cool object @ 0x'])
211
212        # test getting similar output by exploiting ${var} = 'type @ location'
213        # for aggregates
214        self.runCmd("type summary add --summary-string \"${var}\" i_am_cool")
215
216        # this test might fail if the compiler tries to store
217        # these values into registers.. hopefully this is not
218        # going to be the case
219        self.expect("frame variable cool_array",
220                    substrs=['[0] = i_am_cool @ 0x',
221                             '[1] = i_am_cool @ 0x',
222                             '[2] = i_am_cool @ 0x',
223                             '[3] = i_am_cool @ 0x',
224                             '[4] = i_am_cool @ 0x'])
225
226        # test getting same output by exploiting %T and %L together for
227        # aggregates
228        self.runCmd(
229            "type summary add --summary-string \"${var%T} @ ${var%L}\" i_am_cool")
230
231        # this test might fail if the compiler tries to store
232        # these values into registers.. hopefully this is not
233        # going to be the case
234        self.expect("frame variable cool_array",
235                    substrs=['[0] = i_am_cool @ 0x',
236                             '[1] = i_am_cool @ 0x',
237                             '[2] = i_am_cool @ 0x',
238                             '[3] = i_am_cool @ 0x',
239                             '[4] = i_am_cool @ 0x'])
240
241        self.runCmd("type summary add --summary-string \"goofy\" i_am_cool")
242        self.runCmd(
243            "type summary add --summary-string \"${var.second_cool%S}\" i_am_cooler")
244
245        self.expect("frame variable the_coolest_guy",
246                    substrs=['(i_am_cooler) the_coolest_guy = goofy'])
247
248        # check that unwanted type specifiers are removed
249        self.runCmd("type summary delete i_am_cool")
250        self.runCmd(
251            "type summary add --summary-string \"goofy\" \"class i_am_cool\"")
252        self.expect("frame variable the_coolest_guy",
253                    substrs=['(i_am_cooler) the_coolest_guy = goofy'])
254
255        self.runCmd("type summary delete i_am_cool")
256        self.runCmd(
257            "type summary add --summary-string \"goofy\" \"enum i_am_cool\"")
258        self.expect("frame variable the_coolest_guy",
259                    substrs=['(i_am_cooler) the_coolest_guy = goofy'])
260
261        self.runCmd("type summary delete i_am_cool")
262        self.runCmd(
263            "type summary add --summary-string \"goofy\" \"struct i_am_cool\"")
264        self.expect("frame variable the_coolest_guy",
265                    substrs=['(i_am_cooler) the_coolest_guy = goofy'])
266
267        # many spaces, but we still do the right thing
268        self.runCmd("type summary delete i_am_cool")
269        self.runCmd(
270            "type summary add --summary-string \"goofy\" \"union     i_am_cool\"")
271        self.expect("frame variable the_coolest_guy",
272                    substrs=['(i_am_cooler) the_coolest_guy = goofy'])
273
274        # but that not *every* specifier is removed
275        self.runCmd("type summary delete i_am_cool")
276        self.runCmd(
277            "type summary add --summary-string \"goofy\" \"wrong i_am_cool\"")
278        self.expect("frame variable the_coolest_guy", matching=False,
279                    substrs=['(i_am_cooler) the_coolest_guy = goofy'])
280
281        # check that formats are not sticking since that is the behavior we
282        # want
283        self.expect("frame variable iAmInt --format hex",
284                    substrs=['(int) iAmInt = 0x00000001'])
285        self.expect(
286            "frame variable iAmInt",
287            matching=False,
288            substrs=['(int) iAmInt = 0x00000001'])
289        self.expect("frame variable iAmInt", substrs=['(int) iAmInt = 1'])
290