[cfe-commits] r94389 - in /cfe/trunk/bindings/python: clang/cindex.py tests/cindex/test_cursor_kind.py tests/cindex/test_translation_unit.py

Daniel Dunbar daniel at zuster.org
Sun Jan 24 13:20:39 PST 2010


Author: ddunbar
Date: Sun Jan 24 15:20:39 2010
New Revision: 94389

URL: http://llvm.org/viewvc/llvm-project?rev=94389&view=rev
Log:
cindex/Python: Move Cursor.is_ methods to CursorKind, and add test.

Also, add CursorKind.get_all_kinds().

Added:
    cfe/trunk/bindings/python/tests/cindex/test_cursor_kind.py
Modified:
    cfe/trunk/bindings/python/clang/cindex.py
    cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py

Modified: cfe/trunk/bindings/python/clang/cindex.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=94389&r1=94388&r2=94389&view=diff

==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Sun Jan 24 15:20:39 2010
@@ -173,6 +173,30 @@
             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 @@
 # 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 @@
 # 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 @@
 # 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 @@
 # 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 @@
     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 @@
     @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.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.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

Added: cfe/trunk/bindings/python/tests/cindex/test_cursor_kind.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_cursor_kind.py?rev=94389&view=auto

==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_cursor_kind.py (added)
+++ cfe/trunk/bindings/python/tests/cindex/test_cursor_kind.py Sun Jan 24 15:20:39 2010
@@ -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

Modified: cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py?rev=94389&r1=94388&r2=94389&view=diff

==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py Sun Jan 24 15:20:39 2010
@@ -15,4 +15,4 @@
     tu = index.parse(path)
     c = tu.cursor
     assert isinstance(c, Cursor)
-    assert c.is_translation_unit
+    assert c.kind is CursorKind.TRANSLATION_UNIT





More information about the cfe-commits mailing list