[python] Add Cursor.enum_value wrapping clang_getEnumConstantDeclValue
llvm-svn: 156017
This commit is contained in:
parent
cd997e02b2
commit
94c71052e7
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue