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

Anders Waldenborg anders at 0x63.nu
Wed May 2 13:57:34 PDT 2012


Author: andersg
Date: Wed May  2 15:57:33 2012
New Revision: 156017

URL: http://llvm.org/viewvc/llvm-project?rev=156017&view=rev
Log:
[python] Add Cursor.enum_value wrapping clang_getEnumConstantDeclValue

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

Modified: cfe/trunk/bindings/python/clang/cindex.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=156017&r1=156016&r2=156017&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Wed May  2 15:57:33 2012
@@ -1008,6 +1008,30 @@
         return self._enum_type
 
     @property
+    def enum_value(self):
+        """Return the value of an enum constant."""
+        if not hasattr(self, '_enum_value'):
+            assert self.kind == CursorKind.ENUM_CONSTANT_DECL
+            # Figure out the underlying type of the enum to know if it
+            # is a signed or unsigned quantity.
+            underlying_type = self.type
+            if underlying_type.kind == TypeKind.ENUM:
+                underlying_type = underlying_type.get_declaration().enum_type
+            if underlying_type.kind in (TypeKind.CHAR_U,
+                                        TypeKind.UCHAR,
+                                        TypeKind.CHAR16,
+                                        TypeKind.CHAR32,
+                                        TypeKind.USHORT,
+                                        TypeKind.UINT,
+                                        TypeKind.ULONG,
+                                        TypeKind.ULONGLONG,
+                                        TypeKind.UINT128):
+                self._enum_value = Cursor_enum_const_decl_unsigned(self)
+            else:
+                self._enum_value = Cursor_enum_const_decl(self)
+        return self._enum_value
+
+    @property
     def objc_type_encoding(self):
         """Return the Objective-C type encoding as a str."""
         if not hasattr(self, '_objc_type_encoding'):
@@ -1937,6 +1961,14 @@
 Cursor_enum_type.restype = Type
 Cursor_enum_type.errcheck = Type.from_result
 
+Cursor_enum_const_decl = lib.clang_getEnumConstantDeclValue
+Cursor_enum_const_decl.argtypes = [Cursor]
+Cursor_enum_const_decl.restype = c_longlong
+
+Cursor_enum_const_decl_unsigned = lib.clang_getEnumConstantDeclUnsignedValue
+Cursor_enum_const_decl_unsigned.argtypes = [Cursor]
+Cursor_enum_const_decl_unsigned.restype = c_ulonglong
+
 Cursor_objc_type_encoding = lib.clang_getDeclObjCTypeEncoding
 Cursor_objc_type_encoding.argtypes = [Cursor]
 Cursor_objc_type_encoding.restype = _CXString

Modified: cfe/trunk/bindings/python/tests/cindex/test_cursor.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_cursor.py?rev=156017&r1=156016&r2=156017&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_cursor.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_cursor.py Wed May  2 15:57:33 2012
@@ -98,3 +98,40 @@
 
     assert i is not None
     assert i.objc_type_encoding == 'i'
+
+def test_enum_values():
+    tu = get_tu('enum TEST { SPAM=1, EGG, HAM = EGG * 20};')
+    enum = get_cursor(tu, 'TEST')
+    assert enum is not None
+
+    assert enum.kind == CursorKind.ENUM_DECL
+
+    enum_constants = list(enum.get_children())
+    assert len(enum_constants) == 3
+
+    spam, egg, ham = enum_constants
+
+    assert spam.kind == CursorKind.ENUM_CONSTANT_DECL
+    assert spam.enum_value == 1
+    assert egg.kind == CursorKind.ENUM_CONSTANT_DECL
+    assert egg.enum_value == 2
+    assert ham.kind == CursorKind.ENUM_CONSTANT_DECL
+    assert ham.enum_value == 40
+
+def test_enum_values_cpp():
+    tu = get_tu('enum TEST : long long { SPAM = -1, HAM = 0x10000000000};', lang="cpp")
+    enum = get_cursor(tu, 'TEST')
+    assert enum is not None
+
+    assert enum.kind == CursorKind.ENUM_DECL
+
+    enum_constants = list(enum.get_children())
+    assert len(enum_constants) == 2
+
+    spam, ham = enum_constants
+
+    assert spam.kind == CursorKind.ENUM_CONSTANT_DECL
+    assert spam.enum_value == -1
+    assert ham.kind == CursorKind.ENUM_CONSTANT_DECL
+    assert ham.enum_value == 0x10000000000
+





More information about the cfe-commits mailing list