Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / test / API / python_api / formatters / TestFormattersSBAPI.py
blob8a811d25dac538747eed225b158c8e6ba6cbe110
1 """Test Python APIs for working with formatters"""
3 import lldb
4 from lldbsuite.test.decorators import *
5 from lldbsuite.test.lldbtest import *
6 from lldbsuite.test import lldbutil
9 class SBFormattersAPITestCase(TestBase):
10 NO_DEBUG_INFO_TESTCASE = True
12 def setUp(self):
13 # Call super's setUp().
14 TestBase.setUp(self)
15 self.line = line_number("main.cpp", "// Set break point at this line.")
17 def test_formatters_api(self):
18 """Test Python APIs for working with formatters"""
19 self.build()
20 self.setTearDownCleanup()
22 """Test Python APIs for working with formatters"""
23 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
25 lldbutil.run_break_set_by_file_and_line(
26 self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True
29 self.runCmd("run", RUN_SUCCEEDED)
31 # The stop reason of the thread should be breakpoint.
32 self.expect(
33 "thread list",
34 STOPPED_DUE_TO_BREAKPOINT,
35 substrs=["stopped", "stop reason = breakpoint"],
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 synthetic clear", check=False)
45 self.runCmd("type category delete foobar", check=False)
46 self.runCmd("type category delete JASSynth", check=False)
47 self.runCmd("type category delete newbar", check=False)
49 # Execute the cleanup function during test case tear down.
50 self.addTearDownHook(cleanup)
52 format = lldb.SBTypeFormat(lldb.eFormatHex)
53 category = self.dbg.GetDefaultCategory()
54 category.AddTypeFormat(lldb.SBTypeNameSpecifier("int"), format)
56 self.expect("frame variable foo.A", substrs=["0x00000001"])
57 self.expect("frame variable foo.E", matching=False, substrs=["b8cca70a"])
59 category.AddTypeFormat(lldb.SBTypeNameSpecifier("long"), format)
60 self.expect("frame variable foo.A", substrs=["0x00000001"])
61 self.expect("frame variable foo.E", substrs=["b8cca70a"])
63 format.SetFormat(lldb.eFormatOctal)
64 category.AddTypeFormat(lldb.SBTypeNameSpecifier("int"), format)
65 self.expect("frame variable foo.A", substrs=[" 01"])
66 self.expect("frame variable foo.E", substrs=["b8cca70a"])
68 category.DeleteTypeFormat(lldb.SBTypeNameSpecifier("int"))
69 category.DeleteTypeFormat(lldb.SBTypeNameSpecifier("long"))
70 self.expect("frame variable foo.A", matching=False, substrs=[" 01"])
71 self.expect("frame variable foo.E", matching=False, substrs=["b8cca70a"])
73 summary = lldb.SBTypeSummary.CreateWithSummaryString(
74 "the hello world you'll never see"
76 summary.SetSummaryString("hello world")
77 new_category = self.dbg.GetCategory("foobar")
78 self.assertFalse(
79 new_category.IsValid(), "getting a non-existing category worked"
81 new_category = self.dbg.CreateCategory("foobar")
82 new_category.SetEnabled(True)
83 new_category.AddTypeSummary(
84 lldb.SBTypeNameSpecifier(
85 "^.*t$",
86 True, # is_regexp
88 summary,
91 self.expect("frame variable foo.A", substrs=["hello world"])
92 self.expect("frame variable foo.E", matching=False, substrs=["hello world"])
93 self.expect("frame variable foo.B", substrs=["hello world"])
94 self.expect("frame variable foo.F", substrs=["hello world"])
95 new_category.SetEnabled(False)
96 self.expect("frame variable foo.A", matching=False, substrs=["hello world"])
97 self.expect("frame variable foo.E", matching=False, substrs=["hello world"])
98 self.expect("frame variable foo.B", matching=False, substrs=["hello world"])
99 self.expect("frame variable foo.F", matching=False, substrs=["hello world"])
100 self.dbg.DeleteCategory(new_category.GetName())
101 self.expect("frame variable foo.A", matching=False, substrs=["hello world"])
102 self.expect("frame variable foo.E", matching=False, substrs=["hello world"])
103 self.expect("frame variable foo.B", matching=False, substrs=["hello world"])
104 self.expect("frame variable foo.F", matching=False, substrs=["hello world"])
106 filter = lldb.SBTypeFilter(0)
107 filter.AppendExpressionPath("A")
108 filter.AppendExpressionPath("D")
109 self.assertEqual(
110 filter.GetNumberOfExpressionPaths(),
112 "filter with two items does not have two items",
115 category.AddTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct"), filter)
116 self.expect("frame variable foo", substrs=["A = 1", "D = 6.28"])
117 self.expect(
118 "frame variable foo",
119 matching=False,
120 substrs=["B = ", "C = ", "E = ", "F = "],
123 category.DeleteTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct", True))
124 self.expect("frame variable foo", substrs=["A = 1", "D = 6.28"])
125 self.expect(
126 "frame variable foo",
127 matching=False,
128 substrs=["B = ", "C = ", "E = ", "F = "],
131 category.DeleteTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct", False))
132 self.expect("frame variable foo", substrs=["A = 1", "D = 6.28"])
133 self.expect(
134 "frame variable foo",
135 matching=True,
136 substrs=["B = ", "C = ", "E = ", "F = "],
139 self.runCmd("command script import --allow-reload ./synth.py")
141 self.expect("frame variable foo", matching=False, substrs=["X = 1"])
143 self.dbg.GetCategory("JASSynth").SetEnabled(True)
144 self.expect("frame variable foo", matching=True, substrs=["X = 1"])
146 self.dbg.GetCategory("CCCSynth").SetEnabled(True)
147 self.expect(
148 "frame variable ccc",
149 matching=True,
150 substrs=[
151 "CCC object with leading value (int) a = 111",
152 "a = 111",
153 "b = 222",
154 "c = 333",
158 foo_var = (
159 self.dbg.GetSelectedTarget()
160 .GetProcess()
161 .GetSelectedThread()
162 .GetSelectedFrame()
163 .FindVariable("foo")
165 self.assertTrue(foo_var.IsValid(), "could not find foo")
166 self.assertTrue(
167 foo_var.GetDeclaration().IsValid(), "foo declaration is invalid"
170 self.assertEqual(
171 foo_var.GetNumChildren(),
173 "synthetic value has wrong number of child items (synth)",
175 self.assertEqual(
176 foo_var.GetChildMemberWithName("X").GetValueAsUnsigned(),
178 "foo_synth.X has wrong value (synth)",
180 self.assertFalse(
181 foo_var.GetChildMemberWithName("B").IsValid(),
182 "foo_synth.B is valid but should not (synth)",
185 self.dbg.GetCategory("JASSynth").SetEnabled(False)
186 foo_var = (
187 self.dbg.GetSelectedTarget()
188 .GetProcess()
189 .GetSelectedThread()
190 .GetSelectedFrame()
191 .FindVariable("foo")
193 self.assertTrue(foo_var.IsValid(), "could not find foo")
195 self.assertFalse(foo_var.GetNumChildren() == 2, "still seeing synthetic value")
197 filter = lldb.SBTypeFilter(0)
198 filter.AppendExpressionPath("A")
199 filter.AppendExpressionPath("D")
200 category.AddTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct"), filter)
201 self.expect("frame variable foo", substrs=["A = 1", "D = 6.28"])
203 foo_var = (
204 self.dbg.GetSelectedTarget()
205 .GetProcess()
206 .GetSelectedThread()
207 .GetSelectedFrame()
208 .FindVariable("foo")
210 self.assertTrue(foo_var.IsValid(), "could not find foo")
212 self.assertEqual(
213 foo_var.GetNumChildren(),
215 "synthetic value has wrong number of child items (filter)",
217 self.assertEqual(
218 foo_var.GetChildMemberWithName("X").GetValueAsUnsigned(),
220 "foo_synth.X has wrong value (filter)",
222 self.assertEqual(
223 foo_var.GetChildMemberWithName("A").GetValueAsUnsigned(),
225 "foo_synth.A has wrong value (filter)",
228 self.assertTrue(
229 filter.ReplaceExpressionPathAtIndex(0, "C"),
230 "failed to replace an expression path in filter",
232 self.expect("frame variable foo", substrs=["A = 1", "D = 6.28"])
233 category.AddTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct"), filter)
234 self.expect("frame variable foo", substrs=["C = 'e'", "D = 6.28"])
235 category.AddTypeFilter(lldb.SBTypeNameSpecifier("FooType"), filter)
236 filter.ReplaceExpressionPathAtIndex(1, "F")
237 self.expect("frame variable foo", substrs=["C = 'e'", "D = 6.28"])
238 category.AddTypeFilter(lldb.SBTypeNameSpecifier("JustAStruct"), filter)
239 self.expect("frame variable foo", substrs=["C = 'e'", "F = 0"])
240 self.expect("frame variable bar", substrs=["C = 'e'", "D = 6.28"])
242 foo_var = (
243 self.dbg.GetSelectedTarget()
244 .GetProcess()
245 .GetSelectedThread()
246 .GetSelectedFrame()
247 .FindVariable("foo")
249 self.assertTrue(foo_var.IsValid(), "could not find foo")
250 self.assertEqual(
251 foo_var.GetChildMemberWithName("C").GetValueAsUnsigned(),
252 ord("e"),
253 "foo_synth.C has wrong value (filter)",
256 chosen = self.dbg.GetFilterForType(lldb.SBTypeNameSpecifier("JustAStruct"))
257 self.assertEqual(chosen.count, 2, "wrong filter found for JustAStruct")
258 self.assertEqual(
259 chosen.GetExpressionPathAtIndex(0),
260 "C",
261 "wrong item at index 0 for JustAStruct",
263 self.assertEqual(
264 chosen.GetExpressionPathAtIndex(1),
265 "F",
266 "wrong item at index 1 for JustAStruct",
269 self.assertFalse(
270 category.DeleteTypeFilter(lldb.SBTypeNameSpecifier("NoSuchType")),
271 "deleting a non-existing filter worked",
273 self.assertFalse(
274 category.DeleteTypeSummary(lldb.SBTypeNameSpecifier("NoSuchType")),
275 "deleting a non-existing summary worked",
277 self.assertFalse(
278 category.DeleteTypeFormat(lldb.SBTypeNameSpecifier("NoSuchType")),
279 "deleting a non-existing format worked",
281 self.assertFalse(
282 category.DeleteTypeSynthetic(lldb.SBTypeNameSpecifier("NoSuchType")),
283 "deleting a non-existing synthetic worked",
286 self.assertFalse(
287 category.DeleteTypeFilter(lldb.SBTypeNameSpecifier("")),
288 "deleting a filter for '' worked",
290 self.assertFalse(
291 category.DeleteTypeSummary(lldb.SBTypeNameSpecifier("")),
292 "deleting a summary for '' worked",
294 self.assertFalse(
295 category.DeleteTypeFormat(lldb.SBTypeNameSpecifier("")),
296 "deleting a format for '' worked",
298 self.assertFalse(
299 category.DeleteTypeSynthetic(lldb.SBTypeNameSpecifier("")),
300 "deleting a synthetic for '' worked",
303 try:
304 self.assertFalse(
305 category.AddTypeSummary(lldb.SBTypeNameSpecifier("NoneSuchType"), None),
306 "adding a summary valued None worked",
308 except:
309 pass
310 else:
311 self.assertFalse(True, "adding a summary valued None worked")
313 try:
314 self.assertFalse(
315 category.AddTypeFilter(lldb.SBTypeNameSpecifier("NoneSuchType"), None),
316 "adding a filter valued None worked",
318 except:
319 pass
320 else:
321 self.assertFalse(True, "adding a filter valued None worked")
323 try:
324 self.assertFalse(
325 category.AddTypeSynthetic(
326 lldb.SBTypeNameSpecifier("NoneSuchType"), None
328 "adding a synthetic valued None worked",
330 except:
331 pass
332 else:
333 self.assertFalse(True, "adding a synthetic valued None worked")
335 try:
336 self.assertFalse(
337 category.AddTypeFormat(lldb.SBTypeNameSpecifier("NoneSuchType"), None),
338 "adding a format valued None worked",
340 except:
341 pass
342 else:
343 self.assertFalse(True, "adding a format valued None worked")
345 self.assertFalse(
346 category.AddTypeSummary(
347 lldb.SBTypeNameSpecifier("EmptySuchType"), lldb.SBTypeSummary()
349 "adding a summary without value worked",
351 self.assertFalse(
352 category.AddTypeFilter(
353 lldb.SBTypeNameSpecifier("EmptySuchType"), lldb.SBTypeFilter()
355 "adding a filter without value worked",
357 self.assertFalse(
358 category.AddTypeSynthetic(
359 lldb.SBTypeNameSpecifier("EmptySuchType"), lldb.SBTypeSynthetic()
361 "adding a synthetic without value worked",
363 self.assertFalse(
364 category.AddTypeFormat(
365 lldb.SBTypeNameSpecifier("EmptySuchType"), lldb.SBTypeFormat()
367 "adding a format without value worked",
370 self.assertFalse(
371 category.AddTypeSummary(
372 lldb.SBTypeNameSpecifier(""),
373 lldb.SBTypeSummary.CreateWithSummaryString(""),
375 "adding a summary for an invalid type worked",
377 self.assertFalse(
378 category.AddTypeFilter(lldb.SBTypeNameSpecifier(""), lldb.SBTypeFilter(0)),
379 "adding a filter for an invalid type worked",
381 self.assertFalse(
382 category.AddTypeSynthetic(
383 lldb.SBTypeNameSpecifier(""),
384 lldb.SBTypeSynthetic.CreateWithClassName(""),
386 "adding a synthetic for an invalid type worked",
388 self.assertFalse(
389 category.AddTypeFormat(
390 lldb.SBTypeNameSpecifier(""), lldb.SBTypeFormat(lldb.eFormatHex)
392 "adding a format for an invalid type worked",
395 new_category = self.dbg.CreateCategory("newbar")
396 new_category.AddTypeSummary(
397 lldb.SBTypeNameSpecifier("JustAStruct"),
398 lldb.SBTypeSummary.CreateWithScriptCode("return 'hello scripted world';"),
400 self.expect(
401 "frame variable foo", matching=False, substrs=["hello scripted world"]
403 new_category.SetEnabled(True)
404 self.expect(
405 "frame variable foo", matching=True, substrs=["hello scripted world"]
408 self.expect(
409 "frame variable foo_ptr", matching=True, substrs=["hello scripted world"]
411 new_category.AddTypeSummary(
412 lldb.SBTypeNameSpecifier("JustAStruct"),
413 lldb.SBTypeSummary.CreateWithScriptCode(
414 "return 'hello scripted world';", lldb.eTypeOptionSkipPointers
417 self.expect(
418 "frame variable foo", matching=True, substrs=["hello scripted world"]
421 frame = (
422 self.dbg.GetSelectedTarget()
423 .GetProcess()
424 .GetSelectedThread()
425 .GetSelectedFrame()
427 foo_ptr = frame.FindVariable("foo_ptr")
428 summary = foo_ptr.GetTypeSummary()
430 self.assertFalse(
431 summary.IsValid(), "summary found for foo* when none was planned"
434 self.expect(
435 "frame variable foo_ptr", matching=False, substrs=["hello scripted world"]
438 new_category.AddTypeSummary(
439 lldb.SBTypeNameSpecifier("JustAStruct"),
440 lldb.SBTypeSummary.CreateWithSummaryString(
441 "hello static world", lldb.eTypeOptionNone
445 summary = foo_ptr.GetTypeSummary()
447 self.assertTrue(
448 summary.IsValid(), "no summary found for foo* when one was in place"
450 self.assertEqual(
451 summary.GetData(), "hello static world", "wrong summary found for foo*"
454 self.expect("frame variable e1", substrs=["I am an empty Empty1 {}"])
455 self.expect("frame variable e2", substrs=["I am an empty Empty2"])
456 self.expect(
457 "frame variable e2", substrs=["I am an empty Empty2 {}"], matching=False
460 self.assertTrue(
461 self.dbg.GetCategory(lldb.eLanguageTypeObjC) is not None,
462 "ObjC category is None",
465 def test_force_synth_off(self):
466 """Test that one can have the public API return non-synthetic SBValues if desired"""
467 self.build(dictionary={"EXE": "no_synth"})
468 self.setTearDownCleanup()
470 self.runCmd("file " + self.getBuildArtifact("no_synth"), CURRENT_EXECUTABLE_SET)
472 lldbutil.run_break_set_by_file_and_line(
473 self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True
476 self.runCmd("run", RUN_SUCCEEDED)
478 # The stop reason of the thread should be breakpoint.
479 self.expect(
480 "thread list",
481 STOPPED_DUE_TO_BREAKPOINT,
482 substrs=["stopped", "stop reason = breakpoint"],
485 # This is the function to remove the custom formats in order to have a
486 # clean slate for the next test case.
487 def cleanup():
488 self.runCmd("type format clear", check=False)
489 self.runCmd("type summary clear", check=False)
490 self.runCmd("type filter clear", check=False)
491 self.runCmd("type synthetic clear", check=False)
492 self.runCmd("type category delete foobar", check=False)
493 self.runCmd("type category delete JASSynth", check=False)
494 self.runCmd("type category delete newbar", check=False)
495 self.runCmd("settings set target.enable-synthetic-value true")
497 # Execute the cleanup function during test case tear down.
498 self.addTearDownHook(cleanup)
500 frame = (
501 self.dbg.GetSelectedTarget()
502 .GetProcess()
503 .GetSelectedThread()
504 .GetSelectedFrame()
506 int_vector = frame.FindVariable("int_vector")
507 if self.TraceOn():
508 print(int_vector)
509 self.assertEqual(int_vector.GetNumChildren(), 0, "synthetic vector is empty")
511 self.runCmd("settings set target.enable-synthetic-value false")
512 frame = (
513 self.dbg.GetSelectedTarget()
514 .GetProcess()
515 .GetSelectedThread()
516 .GetSelectedFrame()
518 int_vector = frame.FindVariable("int_vector")
519 if self.TraceOn():
520 print(int_vector)
521 self.assertFalse(
522 int_vector.GetNumChildren() == 0, '"physical" vector is not empty'
525 self.runCmd("settings set target.enable-synthetic-value true")
526 frame = (
527 self.dbg.GetSelectedTarget()
528 .GetProcess()
529 .GetSelectedThread()
530 .GetSelectedFrame()
532 int_vector = frame.FindVariable("int_vector")
533 if self.TraceOn():
534 print(int_vector)
535 self.assertEqual(
536 int_vector.GetNumChildren(), 0, "synthetic vector is still empty"