[PATCH] libclang: Add clang_CXXMethod_isConst
Kevin Funk
kfunk at kde.org
Mon Apr 7 06:46:51 PDT 2014
On Monday 07 April 2014 11:10:37 Dmitri Gribenko wrote:
> On Sun, Apr 6, 2014 at 1:20 AM, Kevin Funk <kfunk at kde.org> wrote:
> > Hey,
> >
> > Title says it all, this patch adds clang_CXXMethod_isConst to libclang.
> >
> > Manual testing done.
>
> Hi Kevin,
>
> Please add a test in test/Index/load-classes.cpp. It would be awesome
> if you also added test cases where c-index-test prints (static) and
> (virtual) as well. With that, the patch LGTM.
>
> Dmitri
Added another patch (see 2/2):
>From ccef03ce95f1766f3110b366a30ce46429275331 Mon Sep 17 00:00:00 2001
From: Kevin Funk <kfunk at kde.org>
Date: Sun, 6 Apr 2014 02:10:14 +0200
Subject: [PATCH 1/2] libclang: Add clang_CXXMethod_isConst
---
include/clang-c/Index.h | 6 ++++++
tools/c-index-test/c-index-test.c | 2 ++
tools/libclang/CIndex.cpp | 10 ++++++++++
tools/libclang/libclang.exports | 1 +
4 files changed, 19 insertions(+)
diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h
index 7eff0a4..386e0ff 100644
--- a/include/clang-c/Index.h
+++ b/include/clang-c/Index.h
@@ -4193,6 +4193,12 @@ CINDEX_LINKAGE unsigned
clang_CXXMethod_isStatic(CXCursor C);
CINDEX_LINKAGE unsigned clang_CXXMethod_isVirtual(CXCursor C);
/**
+ * \brief Determine if a C++ member function or member function template is
+ * declared 'const'.
+ */
+CINDEX_LINKAGE unsigned clang_CXXMethod_isConst(CXCursor C);
+
+/**
* \brief Given a cursor that represents a template, determine
* the cursor kind of the specializations would be generated by instantiating
* the template.
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-
test.c
index f6b5510..6a48196 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -768,6 +768,8 @@ static void PrintCursor(CXCursor Cursor,
printf(" (static)");
if (clang_CXXMethod_isVirtual(Cursor))
printf(" (virtual)");
+ if (clang_CXXMethod_isConst(Cursor))
+ printf(" (const)");
if (clang_CXXMethod_isPureVirtual(Cursor))
printf(" (pure)");
if (clang_Cursor_isVariadic(Cursor))
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index 04797a9..50e7c68 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -6403,6 +6403,16 @@ unsigned clang_CXXMethod_isPureVirtual(CXCursor C) {
return (Method && Method->isVirtual() && Method->isPure()) ? 1 : 0;
}
+unsigned clang_CXXMethod_isConst(CXCursor C) {
+ if (!clang_isDeclaration(C.kind))
+ return 0;
+
+ const Decl *D = cxcursor::getCursorDecl(C);
+ const CXXMethodDecl *Method =
+ D ? dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction()) : 0;
+ return (Method && (Method->getTypeQualifiers() & Qualifiers::Const)) ? 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 37b6159..df8d85c 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_isConst
clang_CXXMethod_isPureVirtual
clang_CXXMethod_isStatic
clang_CXXMethod_isVirtual
--
1.9.1
>From 8acef62e4934fbb50971be21588eab5e8ef1409b Mon Sep 17 00:00:00 2001
From: Kevin Funk <kfunk at kde.org>
Date: Mon, 7 Apr 2014 15:42:43 +0200
Subject: [PATCH 2/2] load-classes.cpp: Test {const,static,virtual} func
---
test/Index/load-classes.cpp | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/test/Index/load-classes.cpp b/test/Index/load-classes.cpp
index 9790d9e..adeff95 100644
--- a/test/Index/load-classes.cpp
+++ b/test/Index/load-classes.cpp
@@ -7,13 +7,17 @@ protected:
~X();
private:
operator X*();
+
+ void constMethod() const;
+ static void staticMethod();
+ virtual void virtualMethod();
};
X::X(int value) {
}
// RUN: c-index-test -test-load-source all %s | FileCheck %s
-// CHECK: load-classes.cpp:3:8: StructDecl=X:3:8 (Definition) Extent=[3:1 -
10:2]
+// CHECK: load-classes.cpp:3:8: StructDecl=X:3:8 (Definition) Extent=[3:1 -
14:2]
// CHECK: load-classes.cpp:4:3: CXXConstructor=X:4:3 Extent=[4:3 - 4:15]
[access=public]
// FIXME: missing TypeRef in the constructor name
// CHECK: load-classes.cpp:4:9: ParmDecl=value:4:9 (Definition) Extent=[4:5 -
4:14]
@@ -25,6 +29,10 @@ X::X(int value) {
// FIXME: missing TypeRef in the destructor name
// CHECK: load-classes.cpp:9:3: CXXConversion=operator X *:9:3 Extent=[9:3 -
9:16] [access=private]
// CHECK: load-classes.cpp:9:12: TypeRef=struct X:3:8 Extent=[9:12 - 9:13]
-// CHECK: load-classes.cpp:12:4: CXXConstructor=X:12:4 (Definition)
Extent=[12:1 - 13:2] [access=public]
-// CHECK: load-classes.cpp:12:1: TypeRef=struct X:3:8 Extent=[12:1 - 12:2]
-// CHECK: load-classes.cpp:12:10: ParmDecl=value:12:10 (Definition)
Extent=[12:6 - 12:15]
+// CHECK: load-classes.cpp:11:8: CXXMethod=constMethod:11:8 (const)
Extent=[11:3 - 11:27] [access=private]
+// CHECK: load-classes.cpp:12:15: CXXMethod=staticMethod:12:15 (static)
Extent=[12:3 - 12:29] [access=private]
+// CHECK: load-classes.cpp:13:16: CXXMethod=virtualMethod:13:16 (virtual)
Extent=[13:3 - 13:31] [access=private]
+// CHECK: load-classes.cpp:16:4: CXXConstructor=X:16:4 (Definition)
Extent=[16:1 - 17:2] [access=public]
+// CHECK: load-classes.cpp:16:1: TypeRef=struct X:3:8 Extent=[16:1 - 16:2]
+// CHECK: load-classes.cpp:16:10: ParmDecl=value:16:10 (Definition)
Extent=[16:6 - 16:15]
+// CHECK: load-classes.cpp:16:17: CompoundStmt= Extent=[16:17 - 17:2]
--
1.9.1
Greets
--
Kevin Funk
More information about the cfe-commits
mailing list