]> granicus.if.org Git - clang/commitdiff
[clang.py] Implement Type.element_count
authorGregory Szorc <gregory.szorc@gmail.com>
Fri, 17 Feb 2012 07:47:38 +0000 (07:47 +0000)
committerGregory Szorc <gregory.szorc@gmail.com>
Fri, 17 Feb 2012 07:47:38 +0000 (07:47 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150800 91177308-0d34-0410-b5e6-96231b3b80d8

bindings/python/clang/cindex.py
bindings/python/tests/cindex/test_type.py

index 0d41eaafc05d2dda1b93aa590e0e086063fb2eca..d38eb1bfec7c8c61c7b5b646e53799b170711983 100644 (file)
@@ -1150,6 +1150,20 @@ class Type(Structure):
 
         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)
@@ -1899,6 +1913,10 @@ 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
index 3eca780d046ff625327065d62a0554d756fabae4..ba6af56e8b73ed6a032b5bd6da36a9af2ed83792 100644 (file)
@@ -147,3 +147,25 @@ def test_invalid_element_type():
 
     ok_(i is not None)
     i.element_type
+
+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:
+        j.type.element_count
+        assert False
+    except:
+        assert True