cindex/Python: Move Cursor.is_ methods to CursorKind, and add test.

Also, add CursorKind.get_all_kinds().

llvm-svn: 94389
This commit is contained in:
Daniel Dunbar 2010-01-24 21:20:39 +00:00
parent 195cdeceaf
commit 3e555fd85f
3 changed files with 88 additions and 53 deletions

View File

@ -173,6 +173,30 @@ class CursorKind(object):
raise ValueError,'Unknown cursor kind'
return CursorKind._kinds[id]
@staticmethod
def get_all_kinds():
return filter(None, CursorKind._kinds)
def is_declaration(self):
"""Test if this is a declaration kind."""
return CursorKind_is_decl(self)
def is_reference(self):
"""Test if this is a reference kind."""
return CursorKind_is_ref(self)
def is_expression(self):
"""Test if this is an expression kind."""
return CursorKind_is_expr(self)
def is_statement(self):
"""Test if this is a statement kind."""
return CursorKind_is_stmt(self)
def is_invalid(self):
"""Test if this is an invalid kind."""
return CursorKind_is_inv(self)
def __repr__(self):
return 'CursorKind.%s' % (self.name,)
@ -181,6 +205,9 @@ class CursorKind(object):
# things we want for sure are (a) simple external access to kinds, (b) a place
# to hang a description and name, (c) easy to keep in sync with Index.h.
###
# Declaration Kinds
# A declaration whose specific kind is not exposed via this interface.
#
# Unexposed declarations have the same operations as any other kind of
@ -244,10 +271,11 @@ CursorKind.OBJC_IMPLEMENTATION_DECL = CursorKind(18)
# An Objective-C @implementation for a category.
CursorKind.OBJC_CATEGORY_IMPL_DECL = CursorKind(19)
# A typedef
# A typedef.
CursorKind.TYPEDEF_DECL = CursorKind(20)
# References.
###
# Reference Kinds
CursorKind.OBJC_SUPER_CLASS_REF = CursorKind(40)
CursorKind.OBJC_PROTOCOL_REF = CursorKind(41)
@ -265,12 +293,16 @@ CursorKind.OBJC_CLASS_REF = CursorKind(42)
# referenced by the type of size is the typedef for size_type.
CursorKind.TYPE_REF = CursorKind(43)
###
# Invalid/Error Kinds
# Error conditions.
CursorKind.INVALID_FILE = CursorKind(70)
CursorKind.NO_DECL_FOUND = CursorKind(71)
CursorKind.NOT_IMPLEMENTED = CursorKind(72)
###
# Expression Kinds
# An expression whose specific kind is not exposed via this interface.
#
# Unexposed expressions have the same operations as any other kind of
@ -299,6 +331,9 @@ CursorKind.OBJC_MESSAGE_EXPR = CursorKind(104)
# the specific kind of the statement is not reported.
CursorKind.UNEXPOSED_STMT = CursorKind(200)
###
# Other Kinds
# Cursor that represents the translation unit itself.
#
# The translation unit cursor exists primarily to act as the root cursor for
@ -320,30 +355,6 @@ class Cursor(Structure):
def __ne__(self, other):
return not Cursor_eq(self, other)
def is_declaration(self):
"""Return True if the cursor points to a declaration."""
return Cursor_is_decl(self.kind)
def is_reference(self):
"""Return True if the cursor points to a reference."""
return Cursor_is_ref(self.kind)
def is_expression(self):
"""Return True if the cursor points to an expression."""
return Cursor_is_expr(self.kind)
def is_statement(self):
"""Return True if the cursor points to a statement."""
return Cursor_is_stmt(self.kind)
def is_translation_unit(self):
"""Return True if the cursor points to a translation unit."""
return Cursor_is_tu(self.kind)
def is_invalid(self):
"""Return True if the cursor points to an invalid entity."""
return Cursor_is_inv(self.kind)
def is_definition(self):
"""
Returns true if the declaration pointed at by the cursor is also a
@ -380,7 +391,7 @@ class Cursor(Structure):
@property
def spelling(self):
"""Return the spelling of the entity pointed at by the cursor."""
if not self.is_declaration():
if not self.kind.is_declaration():
# FIXME: clang_getCursorSpelling should be fixed to not assert on
# this, for consistency with clang_getCursorUSR.
return None
@ -554,6 +565,27 @@ SourceRange_end = lib.clang_getRangeEnd
SourceRange_end.argtypes = [SourceRange]
SourceRange_end.restype = SourceLocation
# CursorKind Functions
CursorKind_is_decl = lib.clang_isDeclaration
CursorKind_is_decl.argtypes = [CursorKind]
CursorKind_is_decl.restype = bool
CursorKind_is_ref = lib.clang_isReference
CursorKind_is_ref.argtypes = [CursorKind]
CursorKind_is_ref.restype = bool
CursorKind_is_expr = lib.clang_isExpression
CursorKind_is_expr.argtypes = [CursorKind]
CursorKind_is_expr.restype = bool
CursorKind_is_stmt = lib.clang_isStatement
CursorKind_is_stmt.argtypes = [CursorKind]
CursorKind_is_stmt.restype = bool
CursorKind_is_inv = lib.clang_isInvalid
CursorKind_is_inv.argtypes = [CursorKind]
CursorKind_is_inv.restype = bool
# Cursor Functions
# TODO: Implement this function
Cursor_get = lib.clang_getCursor
@ -568,30 +600,6 @@ Cursor_usr.argtypes = [Cursor]
Cursor_usr.restype = _CXString
Cursor_usr.errcheck = _CXString.from_result
Cursor_is_decl = lib.clang_isDeclaration
Cursor_is_decl.argtypes = [CursorKind]
Cursor_is_decl.restype = bool
Cursor_is_ref = lib.clang_isReference
Cursor_is_ref.argtypes = [CursorKind]
Cursor_is_ref.restype = bool
Cursor_is_expr = lib.clang_isExpression
Cursor_is_expr.argtypes = [CursorKind]
Cursor_is_expr.restype = bool
Cursor_is_stmt = lib.clang_isStatement
Cursor_is_stmt.argtypes = [CursorKind]
Cursor_is_stmt.restype = bool
Cursor_is_inv = lib.clang_isInvalid
Cursor_is_inv.argtypes = [CursorKind]
Cursor_is_inv.restype = bool
Cursor_is_tu = lib.clang_isTranslationUnit
Cursor_is_tu.argtypes = [CursorKind]
Cursor_is_tu.restype = bool
Cursor_is_def = lib.clang_isCursorDefinition
Cursor_is_def.argtypes = [Cursor]
Cursor_is_def.restype = bool

View File

@ -0,0 +1,27 @@
from clang.cindex import CursorKind
def test_name():
assert CursorKind.UNEXPOSED_DECL.name is 'UNEXPOSED_DECL'
def test_get_all_kinds():
assert CursorKind.UNEXPOSED_DECL in CursorKind.get_all_kinds()
assert CursorKind.TRANSLATION_UNIT in CursorKind.get_all_kinds()
def test_kind_groups():
"""Check that every kind classifies to exactly one group."""
assert CursorKind.UNEXPOSED_DECL.is_declaration()
assert CursorKind.TYPE_REF.is_reference()
assert CursorKind.DECL_REF_EXPR.is_expression()
assert CursorKind.UNEXPOSED_STMT.is_statement()
assert CursorKind.INVALID_FILE.is_invalid()
for k in CursorKind.get_all_kinds():
group = [n for n in ('is_declaration', 'is_reference', 'is_expression',
'is_statement', 'is_invalid')
if getattr(k, n)()]
if k == CursorKind.TRANSLATION_UNIT:
assert len(group) == 0
else:
assert len(group) == 1

View File

@ -15,4 +15,4 @@ def test_cursor():
tu = index.parse(path)
c = tu.cursor
assert isinstance(c, Cursor)
assert c.is_translation_unit
assert c.kind is CursorKind.TRANSLATION_UNIT