[Lldb-commits] [lldb] b6db0a5 - Add python enumerators for SBTypeEnumMemberList, and some tests for this API.

Jim Ingham via lldb-commits lldb-commits at lists.llvm.org
Fri Aug 14 09:57:56 PDT 2020


Author: Jim Ingham
Date: 2020-08-14T09:57:46-07:00
New Revision: b6db0a544df1f28e7fa53b74e19839e55e63c8c9

URL: https://github.com/llvm/llvm-project/commit/b6db0a544df1f28e7fa53b74e19839e55e63c8c9
DIFF: https://github.com/llvm/llvm-project/commit/b6db0a544df1f28e7fa53b74e19839e55e63c8c9.diff

LOG: Add python enumerators for SBTypeEnumMemberList, and some tests for this API.

Differential Revision: https://reviews.llvm.org/D85951

Added: 
    

Modified: 
    lldb/bindings/interface/SBTypeEnumMember.i
    lldb/test/API/lang/c/enum_types/TestEnumTypes.py
    lldb/test/API/lang/c/enum_types/main.c

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/interface/SBTypeEnumMember.i b/lldb/bindings/interface/SBTypeEnumMember.i
index 006bdeaa8cee..518e2bf90115 100644
--- a/lldb/bindings/interface/SBTypeEnumMember.i
+++ b/lldb/bindings/interface/SBTypeEnumMember.i
@@ -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
 {
@@ -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;

diff  --git a/lldb/test/API/lang/c/enum_types/TestEnumTypes.py b/lldb/test/API/lang/c/enum_types/TestEnumTypes.py
index cb5bb8eccaa2..0442dd34196a 100644
--- a/lldb/test/API/lang/c/enum_types/TestEnumTypes.py
+++ b/lldb/test/API/lang/c/enum_types/TestEnumTypes.py
@@ -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"))
@@ -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',
@@ -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)
+

diff  --git a/lldb/test/API/lang/c/enum_types/main.c b/lldb/test/API/lang/c/enum_types/main.c
index b866044e5e12..0aa73c970ec6 100644
--- a/lldb/test/API/lang/c/enum_types/main.c
+++ b/lldb/test/API/lang/c/enum_types/main.c
@@ -25,7 +25,7 @@ int main (int argc, char const *argv[])
         Beta = 4
     };
 
-    enum days {
+    enum enum_test_days {
         Monday = -3,
         Tuesday,
         Wednesday,
@@ -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++)


        


More information about the lldb-commits mailing list