r338816 - [libclang 7/8] Add support for getting property setter and getter names

Michael Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 2 22:38:30 PDT 2018


Author: mwu
Date: Thu Aug  2 22:38:29 2018
New Revision: 338816

URL: http://llvm.org/viewvc/llvm-project?rev=338816&view=rev
Log:
[libclang 7/8] Add support for getting property setter and getter names

Summary: This allows libclang to access the actual names of property setters and getters without needing to go through the indexer API. Usually default names are used, but the property can specify a different name.

Reviewers: yvvan, jbcoe

Reviewed By: yvvan

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D49634

Added:
    cfe/trunk/test/Index/property-getter-setter.m
Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/c-index-test/c-index-test.c
    cfe/trunk/tools/libclang/CIndex.cpp
    cfe/trunk/tools/libclang/libclang.exports

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=338816&r1=338815&r2=338816&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Thu Aug  2 22:38:29 2018
@@ -4449,6 +4449,18 @@ CINDEX_LINKAGE unsigned clang_Cursor_get
                                                              unsigned reserved);
 
 /**
+ * Given a cursor that represents a property declaration, return the
+ * name of the method that implements the getter.
+ */
+CINDEX_LINKAGE CXString clang_Cursor_getObjCPropertyGetterName(CXCursor C);
+
+/**
+ * Given a cursor that represents a property declaration, return the
+ * name of the method that implements the setter, if any.
+ */
+CINDEX_LINKAGE CXString clang_Cursor_getObjCPropertySetterName(CXCursor C);
+
+/**
  * 'Qualifiers' written next to the return and parameter types in
  * Objective-C method declarations.
  */

Added: cfe/trunk/test/Index/property-getter-setter.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/property-getter-setter.m?rev=338816&view=auto
==============================================================================
--- cfe/trunk/test/Index/property-getter-setter.m (added)
+++ cfe/trunk/test/Index/property-getter-setter.m Thu Aug  2 22:38:29 2018
@@ -0,0 +1,10 @@
+ at interface Foo
+ at property (assign,readwrite,getter=b,setter=c:) id a;
+ at property (assign,readonly,getter=e) id d;
+ at property (assign,readwrite) id f;
+ at end
+
+// RUN: c-index-test -test-print-type-declaration %s | FileCheck %s
+// CHECK: ObjCPropertyDecl=a:2:52 [getter,assign,readwrite,setter,] (getter=b) (setter=c:) [typedeclaration=id] [typekind=ObjCId]
+// CHECK: ObjCPropertyDecl=d:3:41 [readonly,getter,assign,] (getter=e) [typedeclaration=id] [typekind=ObjCId]
+// CHECK: ObjCPropertyDecl=f:4:33 [assign,readwrite,] [typedeclaration=id] [typekind=ObjCId]

Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=338816&r1=338815&r2=338816&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Thu Aug  2 22:38:29 2018
@@ -1103,6 +1103,34 @@ static void PrintCursor(CXCursor Cursor,
       }
     }
 
+    if (Cursor.kind == CXCursor_ObjCPropertyDecl) {
+      CXString Name = clang_Cursor_getObjCPropertyGetterName(Cursor);
+      CXString Spelling = clang_getCursorSpelling(Cursor);
+      const char *CName = clang_getCString(Name);
+      const char *CSpelling = clang_getCString(Spelling);
+      if (CName && strcmp(CName, CSpelling)) {
+        printf(" (getter=%s)", CName);
+      }
+      clang_disposeString(Spelling);
+      clang_disposeString(Name);
+    }
+
+    if (Cursor.kind == CXCursor_ObjCPropertyDecl) {
+      CXString Name = clang_Cursor_getObjCPropertySetterName(Cursor);
+      CXString Spelling = clang_getCursorSpelling(Cursor);
+      const char *CName = clang_getCString(Name);
+      const char *CSpelling = clang_getCString(Spelling);
+      char *DefaultSetter = malloc(strlen(CSpelling) + 5);
+      sprintf(DefaultSetter, "set%s:", CSpelling);
+      DefaultSetter[3] &= ~(1 << 5); /* Make uppercase */
+      if (CName && strcmp(CName, DefaultSetter)) {
+        printf(" (setter=%s)", CName);
+      }
+      free(DefaultSetter);
+      clang_disposeString(Spelling);
+      clang_disposeString(Name);
+    }
+
     {
       unsigned QT = clang_Cursor_getObjCDeclQualifiers(Cursor);
       if (QT != CXObjCDeclQualifier_None) {

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=338816&r1=338815&r2=338816&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Thu Aug  2 22:38:29 2018
@@ -7913,6 +7913,30 @@ unsigned clang_Cursor_getObjCPropertyAtt
   return Result;
 }
 
+CXString clang_Cursor_getObjCPropertyGetterName(CXCursor C) {
+  if (C.kind != CXCursor_ObjCPropertyDecl)
+    return cxstring::createNull();
+
+  const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C));
+  Selector sel = PD->getGetterName();
+  if (sel.isNull())
+    return cxstring::createNull();
+
+  return cxstring::createDup(sel.getAsString());
+}
+
+CXString clang_Cursor_getObjCPropertySetterName(CXCursor C) {
+  if (C.kind != CXCursor_ObjCPropertyDecl)
+    return cxstring::createNull();
+
+  const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(getCursorDecl(C));
+  Selector sel = PD->getSetterName();
+  if (sel.isNull())
+    return cxstring::createNull();
+
+  return cxstring::createDup(sel.getAsString());
+}
+
 unsigned clang_Cursor_getObjCDeclQualifiers(CXCursor C) {
   if (!clang_isDeclaration(C.kind))
     return CXObjCDeclQualifier_None;

Modified: cfe/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=338816&r1=338815&r2=338816&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Thu Aug  2 22:38:29 2018
@@ -31,6 +31,8 @@ clang_Cursor_getRawCommentText
 clang_Cursor_getNumArguments
 clang_Cursor_getObjCDeclQualifiers
 clang_Cursor_getObjCPropertyAttributes
+clang_Cursor_getObjCPropertyGetterName
+clang_Cursor_getObjCPropertySetterName
 clang_Cursor_getObjCSelectorIndex
 clang_Cursor_getOffsetOfField
 clang_Cursor_getSpellingNameRange




More information about the cfe-commits mailing list