[PATCH] Add a way to check if a method declaration is pure virtual to libclang

Seth Fowler mark.seth.fowler at gmail.com
Tue May 7 23:43:47 PDT 2013


Hi folks!

I'm working on a code navigation tool for C and C++ code based on libclang called Pygmalion. Libclang is a fantastic library, but occasionally I've run into some rough spots that make it hard to collect the kind of information I want.

One problem I've encountered is that there doesn't seem to be any way to determine if a method declaration is pure virtual (other than perhaps dropping to the token level). This information is readily available in the clang AST so it seems a shame not to expose it to libclang. Here's a patch to do just that. 

Let me know what you think.
- Seth Fowler

---
 bindings/python/clang/cindex.py   |  4 ++++
 include/clang-c/Index.h           |  6 ++++++
 tools/c-index-test/c-index-test.c |  3 ++-
 tools/libclang/CIndex.cpp         | 14 ++++++++++++++
 tools/libclang/libclang.exports   |  1 +
 5 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index 880a150..f2e752a 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -2590,6 +2590,10 @@ functionList = [
    [Index, c_char_p],
    c_object_p),
 
+  ("clang_CXXMethod_isPureVirtual",
+   [Cursor],
+   bool),
+
   ("clang_CXXMethod_isStatic",
    [Cursor],
    bool),
diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h
index d8c37eb..9e4f2f9 100644
--- a/include/clang-c/Index.h
+++ b/include/clang-c/Index.h
@@ -4036,6 +4036,12 @@ CINDEX_LINKAGE CXString clang_FullComment_getAsXML(CXComment Comment);
 
 /**
  * \brief Determine if a C++ member function or member function template is 
+ * pure virtual.
+ */
+CINDEX_LINKAGE unsigned clang_CXXMethod_isPureVirtual(CXCursor C);
+
+/**
+ * \brief Determine if a C++ member function or member function template is 
  * declared 'static'.
  */
 CINDEX_LINKAGE unsigned clang_CXXMethod_isStatic(CXCursor C);
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index e575234..a824a9f 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -694,7 +694,8 @@ static void PrintCursor(CXCursor Cursor,
       printf(" (static)");
     if (clang_CXXMethod_isVirtual(Cursor))
       printf(" (virtual)");
-
+    if (clang_CXXMethod_isPureVirtual(Cursor))
+      printf(" (pure)");
     if (clang_Cursor_isVariadic(Cursor))
       printf(" (variadic)");
     
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index a43c1ab..ec6c5e2 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -6114,6 +6114,20 @@ CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU,
 //===----------------------------------------------------------------------===//
 
 extern "C" {
+unsigned clang_CXXMethod_isPureVirtual(CXCursor C) {
+  if (!clang_isDeclaration(C.kind))
+    return 0;
+  
+  const CXXMethodDecl *Method = 0;
+  const Decl *D = cxcursor::getCursorDecl(C);
+  if (const FunctionTemplateDecl *FunTmpl =
+          dyn_cast_or_null<FunctionTemplateDecl>(D))
+    Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
+  else
+    Method = dyn_cast_or_null<CXXMethodDecl>(D);
+  return (Method && Method->isVirtual() && Method->isPure()) ? 1 : 0;
+}
+
 unsigned clang_CXXMethod_isStatic(CXCursor C) {
   if (!clang_isDeclaration(C.kind))
     return 0;
diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports
index 0c9912e..4a134d9 100644
--- a/tools/libclang/libclang.exports
+++ b/tools/libclang/libclang.exports
@@ -2,6 +2,7 @@ clang_CXCursorSet_contains
 clang_CXCursorSet_insert
 clang_CXIndex_getGlobalOptions
 clang_CXIndex_setGlobalOptions
+clang_CXXMethod_isPureVirtual
 clang_CXXMethod_isStatic
 clang_CXXMethod_isVirtual
 clang_Cursor_getArgument
-- 
1.8.0





More information about the cfe-commits mailing list