[python] Add Cursor.enum_value wrapping clang_getEnumConstantDeclValue


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156017 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index 6f0d25f..d988e76 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -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
diff --git a/bindings/python/tests/cindex/test_cursor.py b/bindings/python/tests/cindex/test_cursor.py
index 5e8d1dc..c88aec1 100644
--- a/bindings/python/tests/cindex/test_cursor.py
+++ b/bindings/python/tests/cindex/test_cursor.py
@@ -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
+