Hi,

first time patch submission here. The attached patch adds
clang_Type_isAbstractCXXClass, which forwards to
CXXRecordDecl::isAbstract. Reason is that figuring out whether a class
is abstract or not is very hard from the AST. I've also added the
appropriate Python binding.

If there is interest to cover other CXXRecordDecl methods (like
hasTrivialDefaultConstructor, etc.), I can also add them.

I'm also not sure about the nomenclature; I added this as
isAbstractCXXClass so it's easy to add the remaining CXXRecordDecl
accessors without name clashes; however, in the Python bindings, I used
is_abstract_class() without CXX (similar to how the CXXReference is
handled in the bindings.)

Cheers,
  Matthäus


Index: bindings/python/clang/cindex.py
===================================================================
--- bindings/python/clang/cindex.py	(revision 221340)
+++ bindings/python/clang/cindex.py	(working copy)
@@ -1823,6 +1823,10 @@
         """Determine whether this Type represents plain old data (POD)."""
         return conf.lib.clang_isPODType(self)
 
+    def is_abstract_class(self):
+        """Determine whether this Type represents an abstract C++ class."""
+        return conf.lib.clang_Type_isAbstractCXXClass(self)
+
     def get_pointee(self):
         """
         For pointer types, returns the type of the pointee.
@@ -3286,6 +3290,10 @@
    [Type],
    bool),
 
+  ("clang_Type_isAbstractCXXClass",
+   [Type],
+   bool),
+
   ("clang_isPreprocessing",
    [CursorKind],
    bool),
Index: bindings/python/tests/cindex/test_type.py
===================================================================
--- bindings/python/tests/cindex/test_type.py	(revision 221340)
+++ bindings/python/tests/cindex/test_type.py	(working copy)
@@ -395,3 +395,24 @@
     assert a.kind == TypeKind.INCOMPLETEARRAY
     assert a.element_type.kind == TypeKind.INT
     assert a.get_canonical().kind == TypeKind.INCOMPLETEARRAY
+
+def test_isAbstractCXXClass():
+    """Ensure type.is_abstract_class() works"""
+
+    tu = get_tu("""
+    class A_0 { virtual void f () = 0; };
+    class A_Derived : A_0 {};
+    class N_Derived : A_0 {void f () {}};
+    class N_0 {};
+    """, flags=['-x', 'c++'])
+    a_0 = get_cursor(tu, 'A_0')
+    assert a_0.type.is_abstract_class()
+
+    a_derived = get_cursor(tu, 'A_Derived')
+    assert a_derived.type.is_abstract_class()
+
+    n_derived = get_cursor(tu, 'N_Derived')
+    assert not n_derived.type.is_abstract_class()
+
+    n_0 = get_cursor(tu, 'N_0')
+    assert not n_0.type.is_abstract_class()
Index: include/clang-c/Index.h
===================================================================
--- include/clang-c/Index.h	(revision 221340)
+++ include/clang-c/Index.h	(working copy)
@@ -3169,6 +3169,12 @@
 CINDEX_LINKAGE unsigned clang_isPODType(CXType T);
 
 /**
+ * \brief Return 1 if the CXType is an abstract C++ class, and 0
+ *  otherwise.
+ */
+CINDEX_LINKAGE unsigned clang_Type_isAbstractCXXClass(CXType T);
+
+/**
  * \brief Return the element type of an array, complex, or vector type.
  *
  * If a type is passed in that is not an array, complex, or vector type,
@@ -5678,4 +5684,3 @@
 }
 #endif
 #endif
-
Index: tools/libclang/CXType.cpp
===================================================================
--- tools/libclang/CXType.cpp	(revision 221340)
+++ tools/libclang/CXType.cpp	(working copy)
@@ -599,6 +599,18 @@
   return T.isPODType(cxtu::getASTUnit(TU)->getASTContext()) ? 1 : 0;
 }
 
+unsigned clang_Type_isAbstractCXXClass(CXType X) {
+  QualType T = GetQualType(X);
+  if (T.isNull())
+    return 0;
+
+  const CXXRecordDecl *RecordDecl = T->getAsCXXRecordDecl();
+  if (!RecordDecl)
+      return 0;
+
+  return RecordDecl->isAbstract() ? 1 : 0;
+}
+
 CXType clang_getElementType(CXType CT) {
   QualType ET = QualType();
   QualType T = GetQualType(CT);
Index: tools/libclang/libclang.exports
===================================================================
--- tools/libclang/libclang.exports	(revision 221340)
+++ tools/libclang/libclang.exports	(working copy)
@@ -77,6 +77,7 @@
 clang_Type_getNumTemplateArguments
 clang_Type_getTemplateArgumentAsType
 clang_Type_getCXXRefQualifier
+clang_Type_isAbstractCXXClass
 clang_VerbatimBlockLineComment_getText
 clang_VerbatimLineComment_getText
 clang_HTMLTagComment_getAsString

_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to