[PATCH] D36973: [libclang] Add support for querying cursor availability

Johann Klähn via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 21 11:41:01 PDT 2017


jklaehn created this revision.
jklaehn added a project: clang.

This patch allows checking the availability of cursors through libclang and clang.cindex (Python).
This e.g. allows to check whether a C++ member function has been marked as deleted.


https://reviews.llvm.org/D36973

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


Index: bindings/python/tests/cindex/test_cursor.py
===================================================================
--- bindings/python/tests/cindex/test_cursor.py
+++ bindings/python/tests/cindex/test_cursor.py
@@ -1,6 +1,7 @@
 import ctypes
 import gc
 
+from clang.cindex import AvailabilityKind
 from clang.cindex import CursorKind
 from clang.cindex import TemplateArgumentKind
 from clang.cindex import TranslationUnit
@@ -385,6 +386,18 @@
     t = foo.result_type
     assert t.kind == TypeKind.INT
 
+def test_availability():
+    tu = get_tu('class A { A(A const&) = delete; };', lang='cpp')
+    cursors = get_cursors(tu, "A")
+    for c in cursors:
+        if c.kind == CursorKind.CLASS_DECL:
+            assert c.availability == AvailabilityKind.AVAILABLE
+        if c.kind == CursorKind.CONSTRUCTOR:
+            assert c.availability == AvailabilityKind.NOT_AVAILABLE
+            break
+    else:
+        assert False, "Could not find cursor for deleted constructor"
+
 def test_get_tokens():
     """Ensure we can map cursors back to tokens."""
     tu = get_tu('int foo(int i);')
Index: bindings/python/clang/cindex.py
===================================================================
--- bindings/python/clang/cindex.py
+++ bindings/python/clang/cindex.py
@@ -1572,6 +1572,16 @@
 
         return StorageClass.from_id(self._storage_class)
 
+    @property
+    def availability(self):
+        """
+        Retrieves the availability of the entity pointed at by the cursor.
+        """
+        if not hasattr(self, '_availability'):
+            self._availability = conf.lib.clang_getCursorAvailability(self)
+
+        return AvailabilityKind.from_id(self._availability)
+
     @property
     def access_specifier(self):
         """
@@ -1909,6 +1919,25 @@
 StorageClass.AUTO = StorageClass(6)
 StorageClass.REGISTER = StorageClass(7)
 
+### Availability Kinds ###
+
+class AvailabilityKind(BaseEnumeration):
+    """
+    Describes the availability of an entity.
+    """
+
+    # The unique kind objects, indexed by id.
+    _kinds = []
+    _name_map = None
+
+    def __repr__(self):
+        return 'AvailabilityKind.%s' % (self.name,)
+
+AvailabilityKind.AVAILABLE = AvailabilityKind(0)
+AvailabilityKind.DEPRECATED = AvailabilityKind(1)
+AvailabilityKind.NOT_AVAILABLE = AvailabilityKind(2)
+AvailabilityKind.NOT_ACCESSIBLE = AvailabilityKind(3)
+
 
 ### C++ access specifiers ###
 
@@ -3440,6 +3469,10 @@
    [TranslationUnit, SourceLocation],
    Cursor),
 
+  ("clang_getCursorAvailability",
+   [Cursor],
+   c_int),
+
   ("clang_getCursorDefinition",
    [Cursor],
    Cursor,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36973.112020.patch
Type: text/x-patch
Size: 2620 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170821/d28419be/attachment.bin>


More information about the cfe-commits mailing list