---
 bindings/python/clang/cindex.py           |   18 ++++++++++++++++++
 bindings/python/tests/cindex/test_type.py |   22 ++++++++++++++++++++++
 2 files changed, 40 insertions(+), 0 deletions(-)
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index d4b2712..8afdee0 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -1160,16 +1160,30 @@ class Type(Structure):
         be raised.
         """
         result = Type_get_element_type(self)
         if result.kind == TypeKind.INVALID:
             raise Exception('Element type not available on this type.')
 
         return result
 
+    @property
+    def element_count(self):
+        """Retrieve the number of elements in this type.
+
+        Returns an int.
+
+        If the Type is not an array or vector, this raises.
+        """
+        result = Type_get_num_elements(self)
+        if result < 0:
+            raise Exception('Type does not have elements.')
+
+        return result
+
     @staticmethod
     def from_result(res, fn, args):
         assert isinstance(res, Type)
         return res
 
     def get_canonical(self):
         """
         Return the canonical type for a Type.
@@ -1946,16 +1960,20 @@ Type_get_arg_type.argtypes = [Type, c_uint]
 Type_get_arg_type.restype = Type
 Type_get_arg_type.errcheck = Type.from_result
 
 Type_get_element_type = lib.clang_getElementType
 Type_get_element_type.argtypes = [Type]
 Type_get_element_type.restype = Type
 Type_get_element_type.errcheck = Type.from_result
 
+Type_get_num_elements = lib.clang_getNumElements
+Type_get_num_elements.argtypes = [Type]
+Type_get_num_elements.restype = c_longlong
+
 Type_get_array_element = lib.clang_getArrayElementType
 Type_get_array_element.argtypes = [Type]
 Type_get_array_element.restype = Type
 Type_get_array_element.errcheck = Type.from_result
 
 Type_get_array_size = lib.clang_getArraySize
 Type_get_array_size.argtype = [Type]
 Type_get_array_size.restype = c_longlong
diff --git a/bindings/python/tests/cindex/test_type.py b/bindings/python/tests/cindex/test_type.py
index 9454e32..479e30a 100644
--- a/bindings/python/tests/cindex/test_type.py
+++ b/bindings/python/tests/cindex/test_type.py
@@ -161,8 +161,30 @@ def test_element_type():
 
     for cursor in tu.cursor.get_children():
         if cursor.spelling == 'i':
             i = cursor
             break
 
     assert i.type.kind == TypeKind.CONSTANTARRAY
     assert i.type.element_type.kind == TypeKind.INT
+
+def test_element_count():
+    index = Index.create()
+    tu = index.parse('t.c', unsaved_files=[('t.c', 'int i[5]; int j;')])
+    assert tu is not None
+
+    for cursor in tu.cursor.get_children():
+        if cursor.spelling == 'i':
+            i = cursor
+        elif cursor.spelling == 'j':
+            j = cursor
+
+    assert i is not None
+    assert j is not None
+
+    assert i.type.element_count == 5
+
+    try:
+        count = j.type.element_count
+        assert False
+    except:
+        assert True
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to