[PATCH] libclang: Add clang_CXXMethod_isConst

Kevin Funk kfunk at kde.org
Sat Apr 5 17:20:21 PDT 2014


Hey,

Title says it all, this patch adds clang_CXXMethod_isConst to libclang.

Manual testing done.

test.cpp:
class Foo { void foo() const {} };

$ ./bin/c-index-test -index-file test.cpp
...
[indexDeclaration]: kind: c++-class | name: Foo | USR: c:@C at Foo | lang: C++ | 
cursor: ClassDecl=Foo:1:7 (Definition) | loc: 1:7 | semantic-container: [TU] | 
lexical-container: [TU] | isRedecl: 0 | isDef: 1 | isContainer: 1 | 
isImplicit: 0
[indexDeclaration]: kind: c++-instance-method | name: foo | USR: 
c:@C at Foo@F at foo#1 | lang: C++ | cursor: CXXMethod=foo:1:18 (Definition) (const) 
| loc: 1:18 | semantic-container: [Foo:1:7] | lexical-container: [Foo:1:7] | 
isRedecl: 0 | isDef: 1 | isContainer: 1 | isImplicit: 0

=> " (const)" is appended.

Greets


Patch:
>From fef65cfa032ba0bc793d5f9e5dfc1cc80ed7362d Mon Sep 17 00:00:00 2001
Kevin FunkFrom:  <kfunk at kde.org>
Date: Sun, 6 Apr 2014 02:10:14 +0200
Subject: [PATCH] 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

-- 
Kevin Funk



More information about the cfe-commits mailing list