]> granicus.if.org Git - clang/commitdiff
Index: expose is_mutable_field
authorSaleem Abdulrasool <compnerd@compnerd.org>
Tue, 27 Oct 2015 15:50:22 +0000 (15:50 +0000)
committerSaleem Abdulrasool <compnerd@compnerd.org>
Tue, 27 Oct 2015 15:50:22 +0000 (15:50 +0000)
Expose isMutable via libClang and python bindings.

Patch by Jonathan B Coe!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@251410 91177308-0d34-0410-b5e6-96231b3b80d8

bindings/python/clang/cindex.py
bindings/python/tests/cindex/test_cursor.py
include/clang-c/Index.h
test/Index/index-file.cpp
tools/c-index-test/c-index-test.c
tools/libclang/CIndex.cpp
tools/libclang/libclang.exports

index 62b8596deac35a6a11b460c81f6e6e79b8d74fa6..49b2fd873f90fc5e249715c3a43fa65d1e96492d 100644 (file)
@@ -1170,6 +1170,12 @@ class Cursor(Structure):
         """
         return conf.lib.clang_CXXMethod_isConst(self)
 
+    def is_mutable_field(self):
+        """Returns True if the cursor refers to a C++ field that is declared
+        'mutable'.
+        """
+        return conf.lib.clang_CXXField_isMutable(self)
+
     def is_pure_virtual_method(self):
         """Returns True if the cursor refers to a C++ member function or member
         function template that is declared pure virtual.
@@ -2897,6 +2903,10 @@ functionList = [
    [Index, c_char_p],
    c_object_p),
 
+  ("clang_CXXField_isMutable",
+   [Cursor],
+   bool),
+
   ("clang_CXXMethod_isConst",
    [Cursor],
    bool),
index 68fc8fa4aca382bb13115e6f358e6bf96c69d231..c5ea50516ad21350e979d8d26b2cafba6dcfe5ab 100644 (file)
@@ -112,6 +112,21 @@ def test_is_const_method():
     assert foo.is_const_method()
     assert not bar.is_const_method()
 
+def test_is_mutable_field():
+    """Ensure Cursor.is_mutable_field works."""
+    source = 'class X { int x_; mutable int y_; };'
+    tu = get_tu(source, lang='cpp')
+
+    cls = get_cursor(tu, 'X')
+    x_ = get_cursor(tu, 'x_')
+    y_ = get_cursor(tu, 'y_')
+    assert cls is not None
+    assert x_ is not None
+    assert y_ is not None
+
+    assert not x_.is_mutable_field()
+    assert y_.is_mutable_field()
+
 def test_is_static_method():
     """Ensure Cursor.is_static_method works."""
 
index af5b93c733c9ad68af99381325022308b75c43c0..cd5eb4e4d2237cb11bfde92c1d422ee3561e6fd7 100644 (file)
@@ -3956,6 +3956,11 @@ CXFile clang_Module_getTopLevelHeader(CXTranslationUnit,
  * @{
  */
 
+/**
+ * \brief Determine if a C++ field is declared 'mutable'.
+ */
+CINDEX_LINKAGE unsigned clang_CXXField_isMutable(CXCursor C);
+
 /**
  * \brief Determine if a C++ member function or member function template is
  * pure virtual.
index d42b4b2de7d8f6ec18cff7d3ad86f07f402ba834..f1ae68a2508dead487a16258f31a5adfe1565a50 100644 (file)
@@ -24,6 +24,10 @@ template <> void A<int>::meth();
 template class A<int>;
 }
 
+class B {
+  mutable int x_;
+  int y_;
+};
 // RUN: c-index-test -index-file %s > %t
 // RUN: FileCheck %s -input-file=%t
 
@@ -31,3 +35,5 @@ template class A<int>;
 // CHECK: [indexDeclaration]: kind: struct-template-spec | name: TS | {{.*}} | loc: 11:8
 // CHECK: [indexDeclaration]: kind: function-template-spec | name: tfoo | {{.*}} | loc: 15:6
 // CHECK: [indexDeclaration]: kind: c++-instance-method | name: meth | {{.*}} | loc: 23:26
+// CHECK: [indexDeclaration]: kind: field | name: x_ | USR: c:@S@B@FI@x_ | lang: C++ | cursor: FieldDecl=x_:28:15 (Definition) (mutable) | loc: 28:15 | semantic-container: [B:27:7] | lexical-container: [B:27:7] | isRedecl: 0 | isDef: 1 | isContainer: 0 | isImplicit: 0
+// CHECK: [indexDeclaration]: kind: field | name: y_ | USR: c:@S@B@FI@y_ | lang: C++ | cursor: FieldDecl=y_:29:7 (Definition) | loc: 29:7 | semantic-container: [B:27:7] | lexical-container: [B:27:7] | isRedecl: 0 | isDef: 1 | isContainer: 0 | isImplicit: 0
index 7155d8c7808bec4df6ff6a4cc02dd70474c786a3..26ef5f204b600aa75b5a0310df0b5911556b8059 100644 (file)
@@ -767,6 +767,8 @@ static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) {
     clang_disposeString(DeprecatedMessage);
     clang_disposeString(UnavailableMessage);
     
+    if (clang_CXXField_isMutable(Cursor))
+      printf(" (mutable)");
     if (clang_CXXMethod_isStatic(Cursor))
       printf(" (static)");
     if (clang_CXXMethod_isVirtual(Cursor))
index 96923dc7def60d621098802ee85cfe54f88110f5..be53b8227443e5a46027d9caa9fc65f47223428f 100644 (file)
@@ -6888,6 +6888,16 @@ CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU,
 //===----------------------------------------------------------------------===//
 
 extern "C" {
+unsigned clang_CXXField_isMutable(CXCursor C) {
+  if (!clang_isDeclaration(C.kind))
+    return 0;
+
+  if (const auto D = cxcursor::getCursorDecl(C))
+    if (const auto FD = dyn_cast_or_null<FieldDecl>(D))
+      return FD->isMutable() ? 1 : 0;
+  return 0;
+}
+
 unsigned clang_CXXMethod_isPureVirtual(CXCursor C) {
   if (!clang_isDeclaration(C.kind))
     return 0;
index 5179b96fc61badd087a9e95444c9c3292c0a174a..fd157bc4951eee47495d6ac28db5b2cecfe03ff3 100644 (file)
@@ -2,6 +2,7 @@ clang_CXCursorSet_contains
 clang_CXCursorSet_insert
 clang_CXIndex_getGlobalOptions
 clang_CXIndex_setGlobalOptions
+clang_CXXField_isMutable
 clang_CXXMethod_isConst
 clang_CXXMethod_isPureVirtual
 clang_CXXMethod_isStatic