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

Gregory Szorc gregory.szorc at gmail.com
Thu Feb 16 23:44:46 PST 2012


Author: gps
Date: Fri Feb 17 01:44:46 2012
New Revision: 150799

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

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=150799&r1=150798&r2=150799&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Fri Feb 17 01:44:46 2012
@@ -1137,6 +1137,19 @@
         """Return the kind of this type."""
         return TypeKind.from_id(self._kind_id)
 
+    @property
+    def element_type(self):
+        """Retrieve the Type of elements within this Type.
+
+        If accessed on a type that is not an array, complex, or vector type, an
+        exception will be raised.
+        """
+        result = Type_get_element_type(self)
+        if result.kind == TypeKind.INVALID:
+            raise Exception('Element type not available on this type.')
+
+        return result
+
     @staticmethod
     def from_result(res, fn, args):
         assert isinstance(res, Type)
@@ -1881,6 +1894,11 @@
 Type_get_result.restype = Type
 Type_get_result.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_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=150799&r1=150798&r2=150799&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:44:46 2012
@@ -1,4 +1,8 @@
-from clang.cindex import Index, CursorKind, TypeKind
+from clang.cindex import CursorKind
+from clang.cindex import Index
+from clang.cindex import TypeKind
+from nose.tools import ok_
+from nose.tools import raises
 
 kInput = """\
 
@@ -115,3 +119,31 @@
 
     assert i.type.is_pod()
     assert not f.type.is_pod()
+
+def test_element_type():
+    index = Index.create()
+    tu = index.parse('t.c', unsaved_files=[('t.c', 'int i[5];')])
+    assert tu is not None
+
+    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
+
+ at raises(Exception)
+def test_invalid_element_type():
+    """Ensure Type.element_type raises if type doesn't have elements."""
+    index = Index.create()
+    tu = index.parse('t.c', unsaved_files=[('t.c', 'int i;')])
+
+    i = None
+    for cursor in tu.cursor.get_children():
+        if cursor.spelling == 'i':
+            i = cursor
+            break
+
+    ok_(i is not None)
+    i.element_type





More information about the cfe-commits mailing list