]> granicus.if.org Git - clang/commitdiff
[python] Add Cursor.enum_value wrapping clang_getEnumConstantDeclValue
authorAnders Waldenborg <anders@0x63.nu>
Wed, 2 May 2012 20:57:33 +0000 (20:57 +0000)
committerAnders Waldenborg <anders@0x63.nu>
Wed, 2 May 2012 20:57:33 +0000 (20:57 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156017 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 6f0d25f1a8f595d8ec03a4cd9ba48349fafdb80f..d988e7685a6720cb86ba6e466056e4225f7156b5 100644 (file)
@@ -1007,6 +1007,30 @@ class Cursor(Structure):
 
         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."""
@@ -1937,6 +1961,14 @@ Cursor_enum_type.argtypes = [Cursor]
 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
index 5e8d1dc5dbcea2fe9d6d2c869f7ccb77d6998f65..c88aec19a86cb6e20358f3cacee526f51d6a1abd 100644 (file)
@@ -98,3 +98,40 @@ def test_objc_type_encoding():
 
     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
+