[cfe-commits] r150800 - in /cfe/trunk/bindings/python: clang/cindex.py tests/cindex/test_type.py

Gregory Szorc gregory.szorc at gmail.com
Thu Feb 16 23:47:38 PST 2012


Author: gps
Date: Fri Feb 17 01:47:38 2012
New Revision: 150800

URL: http://llvm.org/viewvc/llvm-project?rev=150800&view=rev
Log:
[clang.py] Implement Type.element_count

Modified:
    cfe/trunk/bindings/python/clang/cindex.py
    cfe/trunk/bindings/python/tests/cindex/test_type.py

Modified: cfe/trunk/bindings/python/clang/cindex.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=150800&r1=150799&r2=150800&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Fri Feb 17 01:47:38 2012
@@ -1150,6 +1150,20 @@
 
         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.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

Modified: cfe/trunk/bindings/python/tests/cindex/test_type.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_type.py?rev=150800&r1=150799&r2=150800&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_type.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_type.py Fri Feb 17 01:47:38 2012
@@ -147,3 +147,25 @@
 
     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





More information about the cfe-commits mailing list