Skip to content

Commit

Permalink
[lldb] Modernize and expand TestCppBitfields
Browse files Browse the repository at this point in the history
* clang-format test source.
* Removed the dead setup code.
* Using expect_expr etc. instead of raw expect.
* Slightly expanded with tests for vtable pointers (which mostly just crash atm.)
* Removed some other minor test guideline problems.
  • Loading branch information
Teemperor committed Oct 25, 2021
1 parent 16ceb44 commit 974c2f5
Show file tree
Hide file tree
Showing 2 changed files with 267 additions and 207 deletions.
264 changes: 143 additions & 121 deletions lldb/test/API/lang/cpp/bitfields/TestCppBitfields.py
Expand Up @@ -10,143 +10,165 @@ class CppBitfieldsTestCase(TestBase):

mydir = TestBase.compute_mydir(__file__)

def setUp(self):
# Call super's setUp().
TestBase.setUp(self)
# Find the line number to break inside main().
self.line = line_number('main.cpp', '// Set break point at this line.')

def test_and_run_command(self):
"""Test 'frame variable ...' on a variable with bitfields."""
@no_debug_info_test
def test_bitfields(self):
self.build()

lldbutil.run_to_source_breakpoint(self, '// Set break point at this line.',
lldb.SBFileSpec("main.cpp", False))

# The stop reason of the thread should be breakpoint.
self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
substrs=['stopped',
'stop reason = breakpoint'])

# The breakpoint should have a hit count of 1.
self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
substrs=[' resolved, hit count = 1'])

self.expect("expr (lba.a)", VARIABLES_DISPLAYED_CORRECTLY,
substrs=['unsigned int', '2'])
self.expect("expr (lbb.b)", VARIABLES_DISPLAYED_CORRECTLY,
substrs=['unsigned int', '3'])
self.expect("expr (lbc.c)", VARIABLES_DISPLAYED_CORRECTLY,
substrs=['unsigned int', '4'])
self.expect("expr (lbd.a)", VARIABLES_DISPLAYED_CORRECTLY,
substrs=['unsigned int', '5'])
self.expect("expr (clang_example.f.a)", VARIABLES_DISPLAYED_CORRECTLY,
substrs=['uint64_t', '1'])

self.expect("expr uwbf",
substrs=['a = 255',
'b = 65535',
'c = 4294967295',
'x = 4294967295'] )

self.expect("expr uwubf",
substrs=['a = 16777215',
'x = 4294967295'] )

self.expect(
"frame variable --show-types lba",
VARIABLES_DISPLAYED_CORRECTLY,
substrs=[
'(int:32) = ',
'(unsigned int:20) a = 2',
])

self.expect(
"frame variable --show-types lbb",
VARIABLES_DISPLAYED_CORRECTLY,
substrs=[
'(unsigned int:1) a = 1',
'(int:31) =',
'(unsigned int:20) b = 3',
])

self.expect(
"frame variable --show-types lbc",
VARIABLES_DISPLAYED_CORRECTLY,
substrs=[
'(int:22) =',
'(unsigned int:1) a = 1',
'(unsigned int:1) b = 0',
'(unsigned int:5) c = 4',
'(unsigned int:1) d = 1',
'(int:2) =',
'(unsigned int:20) e = 20',
])

self.expect(
"frame variable --show-types lbd",
VARIABLES_DISPLAYED_CORRECTLY,
substrs=[
'(char[3]) arr = "ab"',
'(int:32) =',
'(unsigned int:20) a = 5',
])

self.expect(
"frame variable --show-types clang_example",
VARIABLES_DISPLAYED_CORRECTLY,
substrs=[
'(int:22) =',
'(uint64_t:1) a = 1',
'(uint64_t:1) b = 0',
'(uint64_t:1) c = 1',
'(uint64_t:1) d = 0',
'(uint64_t:1) e = 1',
'(uint64_t:1) f = 0',
'(uint64_t:1) g = 1',
'(uint64_t:1) h = 0',
'(uint64_t:1) i = 1',
'(uint64_t:1) j = 0',
'(uint64_t:1) k = 1',
])

self.expect(
"frame variable --show-types derived",
VARIABLES_DISPLAYED_CORRECTLY,
substrs=[
'(uint32_t) b_a = 2',
'(uint32_t:1) d_a = 1',
])

self.expect(
"frame variable --show-types bb",
VARIABLES_DISPLAYED_CORRECTLY,
substrs=[
'(bool:1) a = true',
'(bool:1) b = false',
'(bool:2) c = true',
'(bool:2) d = true',
])
lldbutil.run_to_source_breakpoint(self, '// break here',
lldb.SBFileSpec("main.cpp", False))

# Accessing LargeBitsA.
self.expect_expr("lba", result_children=[
ValueCheck(name="", type="int:32"),
ValueCheck(name="a", type="unsigned int:20", value="2")
])
self.expect_expr("lba.a", result_type="unsigned int", result_value="2")


# Accessing LargeBitsB.
self.expect_expr("lbb", result_children=[
ValueCheck(name="a", type="unsigned int:1", value="1"),
ValueCheck(name="", type="int:31"),
ValueCheck(name="b", type="unsigned int:20", value="3")
])
self.expect_expr("lbb.b", result_type="unsigned int", result_value="3")


# Accessing LargeBitsC.
self.expect_expr("lbc", result_children=[
ValueCheck(name="", type="int:22"),
ValueCheck(name="a", type="unsigned int:1", value="1"),
ValueCheck(name="b", type="unsigned int:1", value="0"),
ValueCheck(name="c", type="unsigned int:5", value="4"),
ValueCheck(name="d", type="unsigned int:1", value="1"),
ValueCheck(name="", type="int:2"),
ValueCheck(name="e", type="unsigned int:20", value="20"),
])
self.expect_expr("lbc.c", result_type="unsigned int", result_value="4")


# Accessing LargeBitsD.
self.expect_expr("lbd", result_children=[
ValueCheck(name="arr", type="char[3]", summary='"ab"'),
ValueCheck(name="", type="int:32"),
ValueCheck(name="a", type="unsigned int:20", value="5")
])
self.expect_expr("lbd.a", result_type="unsigned int", result_value="5")


# Test BitfieldsInStructInUnion.
# FIXME: This needs some more explanation for what it's actually testing.
nested_struct_children = [
ValueCheck(name="", type="int:22"),
ValueCheck(name="a", type="uint64_t:1", value="1"),
ValueCheck(name="b", type="uint64_t:1", value="0"),
ValueCheck(name="c", type="uint64_t:1", value="1"),
ValueCheck(name="d", type="uint64_t:1", value="0"),
ValueCheck(name="e", type="uint64_t:1", value="1"),
ValueCheck(name="f", type="uint64_t:1", value="0"),
ValueCheck(name="g", type="uint64_t:1", value="1"),
ValueCheck(name="h", type="uint64_t:1", value="0"),
ValueCheck(name="i", type="uint64_t:1", value="1"),
ValueCheck(name="j", type="uint64_t:1", value="0"),
ValueCheck(name="k", type="uint64_t:1", value="1")
]
self.expect_expr("bitfields_in_struct_in_union",
result_type="BitfieldsInStructInUnion",
result_children=[ValueCheck(name="", children=[
ValueCheck(name="f", children=nested_struct_children)
])]
)
self.expect_expr("bitfields_in_struct_in_union.f.a",
result_type="uint64_t", result_value="1")


# Unions with bitfields.
self.expect_expr("uwbf", result_type="UnionWithBitfields", result_children=[
ValueCheck(name="a", value="255"),
ValueCheck(name="b", value="65535"),
ValueCheck(name="c", value="4294967295"),
ValueCheck(name="x", value="4294967295")
])
self.expect_expr("uwubf", result_type="UnionWithUnnamedBitfield",
result_children=[
ValueCheck(name="a", value="16777215"),
ValueCheck(name="x", value="4294967295")
]
)

# Class with a base class and a bitfield.
self.expect_expr("derived", result_type="Derived", result_children=[
ValueCheck(name="Base", children=[
ValueCheck(name="b_a", value="2", type="uint32_t")
]),
ValueCheck(name="d_a", value="1", type="uint32_t:1")
])


# Struct with bool bitfields.
self.expect_expr("bb", result_type="", result_children=[
ValueCheck(name="a", value="true", type="bool:1"),
ValueCheck(name="b", value="false", type="bool:1"),
ValueCheck(name="c", value="true", type="bool:2"),
ValueCheck(name="d", value="true", type="bool:2")
])

bb = self.frame().FindVariable('bb')
self.assertTrue(bb.IsValid())
self.assertSuccess(bb.GetError())

bb_a = bb.GetChildAtIndex(0)
self.assertTrue(bb_a.IsValid())
self.assertSuccess(bb_a.GetError())
self.assertEqual(bb_a.GetValueAsUnsigned(), 1)
self.assertEqual(bb_a.GetValueAsSigned(), 1)

bb_b = bb.GetChildAtIndex(1)
self.assertTrue(bb_b.IsValid())
self.assertSuccess(bb_b.GetError())
self.assertEqual(bb_b.GetValueAsUnsigned(), 0)
self.assertEqual(bb_b.GetValueAsSigned(), 0)

bb_c = bb.GetChildAtIndex(2)
self.assertTrue(bb_c.IsValid())
self.assertSuccess(bb_c.GetError())
self.assertEqual(bb_c.GetValueAsUnsigned(), 1)
self.assertEqual(bb_c.GetValueAsSigned(), 1)

bb_d = bb.GetChildAtIndex(3)
self.assertTrue(bb_d.IsValid())
self.assertSuccess(bb_d.GetError())
self.assertEqual(bb_d.GetValueAsUnsigned(), 1)
self.assertEqual(bb_d.GetValueAsSigned(), 1)

# Test a class with a base class that has a vtable ptr. The derived
# class has bitfields.
base_with_vtable_children = [
ValueCheck(name="a", type="unsigned int:4", value="5"),
ValueCheck(name="b", type="unsigned int:4", value="0"),
ValueCheck(name="c", type="unsigned int:4", value="5")
]
self.expect_expr("base_with_vtable", result_children=base_with_vtable_children)
self.expect_var_path("base_with_vtable", children=base_with_vtable_children)

# FIXME: These all crash due the vtable ptr.
@skipIf
@no_debug_info_test
def test_bitfield_behind_vtable_ptr(self):
self.build()
lldbutil.run_to_source_breakpoint(self, '// break here',
lldb.SBFileSpec("main.cpp", False))

# Test a class with a vtable ptr and bitfields.
with_vtable_children = [
ValueCheck(name="a", type="unsigned int:4", value="5"),
ValueCheck(name="b", type="unsigned int:4", value="0"),
ValueCheck(name="c", type="unsigned int:4", value="5")
]
self.expect_expr("with_vtable", result_children=with_vtable_children)
self.expect_var_path("with_vtable", children=with_vtable_children)

# Test a class with a vtable ptr and unnamed bitfield directly after.
with_vtable_and_unnamed_children = [
ValueCheck(name="", type="unsigned int:4", value="0"),
ValueCheck(name="b", type="unsigned int:4", value="0"),
ValueCheck(name="c", type="unsigned int:4", value="5")
]
self.expect_expr("with_vtable_and_unnamed",
result_children=with_vtable_and_unnamed_children)
self.expect_var_path("with_vtable_and_unnamed",
children=with_vtable_and_unnamed_children)

0 comments on commit 974c2f5

Please sign in to comment.