Skip to content

Add python enumerators for SBTypeEnumMemberList, and some tests for t… #1843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions lldb/bindings/interface/SBTypeEnumMember.i
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ protected:
SBTypeEnumMember (const lldb::TypeEnumMemberImplSP &);
};

%feature(
"docstring",
"Represents a list of SBTypeEnumMembers."
) SBTypeEnumMemberList;
%feature("docstring",
"Represents a list of SBTypeEnumMembers.
SBTypeEnumMemberList supports SBTypeEnumMember iteration.
It also supports [] access either by index, or by enum
element name by doing:

myType = target.FindFirstType('MyEnumWithElementA')
members = myType.GetEnumMembers()
first_elem = members[0]
elem_A = members['A']

") SBTypeEnumMemberList;

class SBTypeEnumMemberList
{
Expand All @@ -99,6 +107,29 @@ public:
uint32_t
GetSize();

#ifdef SWIGPYTHON
%pythoncode %{
def __iter__(self):
'''Iterate over all members in a lldb.SBTypeEnumMemberList object.'''
return lldb_iter(self, 'GetSize', 'GetTypeEnumMemberAtIndex')

def __len__(self):
'''Return the number of members in a lldb.SBTypeEnumMemberList object.'''
return self.GetSize()

def __getitem__(self, key):
num_elements = self.GetSize()
if type(key) is int:
if key < num_elements:
return self.GetTypeEnumMemberAtIndex(key)
elif type(key) is str:
for idx in range(num_elements):
item = self.GetTypeEnumMemberAtIndex(idx)
if item.name == key:
return item
return None
%}
#endif

private:
std::unique_ptr<lldb_private::TypeEnumMemberListImpl> m_opaque_ap;
Expand Down
50 changes: 43 additions & 7 deletions lldb/test/API/lang/c/enum_types/TestEnumTypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ def setUp(self):
# Find the line number to break inside main().
self.line = line_number('main.c', '// Set break point at this line.')

def test(self):
"""Test 'image lookup -t days' and check for correct display and enum value printing."""
def test_command_line(self):
"""Test 'image lookup -t enum_test_days' and check for correct display and enum value printing."""
self.build()
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

lldbutil.run_to_source_breakpoint(
self, '// Breakpoint for bitfield', lldb.SBFileSpec("main.c"))
Expand Down Expand Up @@ -63,10 +61,10 @@ def test(self):
self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
substrs=[' resolved, hit count = 1'])

# Look up information about the 'days' enum type.
# Look up information about the 'enum_test_days' enum type.
# Check for correct display.
self.expect("image lookup -t days", DATA_TYPES_DISPLAYED_CORRECTLY,
substrs=['enum days {',
self.expect("image lookup -t enum_test_days", DATA_TYPES_DISPLAYED_CORRECTLY,
substrs=['enum enum_test_days {',
'Monday',
'Tuesday',
'Wednesday',
Expand Down Expand Up @@ -124,3 +122,41 @@ def test(self):
'check for valid enumeration value',
substrs=[enum_value])
lldbutil.continue_to_breakpoint(self.process(), bkpt)

def check_enum_members(self, members):
name_matches = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "kNumDays"]
value_matches = [-3, -2, -1, 0, 1, 2, 3, 4]

# First test that the list of members from the type works
num_matches = len(name_matches)
self.assertEqual(len(members), num_matches, "enum_members returns the right number of elements")
for idx in range(0, num_matches):
member = members[idx]
self.assertTrue(member.IsValid(), "Got a valid member for idx: %d"%(idx))
self.assertEqual(member.name, name_matches[idx], "Name matches for %d"%(idx))
self.assertEqual(member.signed, value_matches[idx], "Value matches for %d"%(idx))

def test_api(self):
"""Test the the SBTypeEnumMember API's work correctly for enum_test_days"""
self.build()
target = lldbutil.run_to_breakpoint_make_target(self)

types = target.FindTypes("enum_test_days")
self.assertEqual(len(types), 1, "Found more than one enum_test_days type...")
type = types.GetTypeAtIndex(0)

# First check using the Python list returned by the type:
self.check_enum_members(type.enum_members)

# Now use the SBTypeEnumMemberList.
member_list = type.GetEnumMembers()
self.check_enum_members(member_list)

# Now check that the by name accessor works:
for member in member_list:
name = member.name
check_member = member_list[name]
self.assertTrue(check_member.IsValid(), "Got a valid member for %s."%(name))
self.assertEqual(name, check_member.name, "Got back the right name")
self.assertEqual(member.unsigned, check_member.unsigned)

4 changes: 2 additions & 2 deletions lldb/test/API/lang/c/enum_types/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main (int argc, char const *argv[])
Beta = 4
};

enum days {
enum enum_test_days {
Monday = -3,
Tuesday,
Wednesday,
Expand All @@ -40,7 +40,7 @@ int main (int argc, char const *argv[])
int nonsense = a + b + c + ab + ac + all;
enum non_bitfield omega = Alpha | Beta;

enum days day;
enum enum_test_days day;
struct foo f;
f.op = NULL; // Breakpoint for bitfield
for (day = Monday - 1; day <= kNumDays + 1; day++)
Expand Down