r324914 - [libclang] Add `CXSymbolRole role` to CXIdxEntityRefInfo

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 12 09:42:09 PST 2018


Author: maskray
Date: Mon Feb 12 09:42:09 2018
New Revision: 324914

URL: http://llvm.org/viewvc/llvm-project?rev=324914&view=rev
Log:
[libclang] Add `CXSymbolRole role` to CXIdxEntityRefInfo

Summary:
CXIdxEntityRefInfo contains the member `CXIdxEntityRefKind kind;` to
differentiate implicit and direct calls. However, there are more roles
defined in SymbolRole. Among them, `Read/Write` are probably the most
useful ones as they can be used to differentiate Read/Write occurrences
of a symbol for document highlight in a text document.

See `export namespace DocumentHighlightKind`
on https://microsoft.github.io/language-server-protocol/specification

Subscribers: cfe-commits

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

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/include/clang/Index/IndexSymbol.h
    cfe/trunk/test/Index/index-refs.cpp
    cfe/trunk/test/Index/index-subscripting-literals.m
    cfe/trunk/tools/c-index-test/c-index-test.c
    cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp
    cfe/trunk/tools/libclang/CXIndexDataConsumer.h

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=324914&r1=324913&r2=324914&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Mon Feb 12 09:42:09 2018
@@ -32,7 +32,7 @@
  * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
  */
 #define CINDEX_VERSION_MAJOR 0
-#define CINDEX_VERSION_MINOR 47
+#define CINDEX_VERSION_MINOR 48
 
 #define CINDEX_VERSION_ENCODE(major, minor) ( \
       ((major) * 10000)                       \
@@ -6092,6 +6092,9 @@ typedef struct {
 
 /**
  * \brief Data for IndexerCallbacks#indexEntityReference.
+ *
+ * This may be deprecated in a future version as this duplicates
+ * the \c CXSymbolRole_Implicit bit in \c CXSymbolRole.
  */
 typedef enum {
   /**
@@ -6106,6 +6109,25 @@ typedef enum {
 } CXIdxEntityRefKind;
 
 /**
+ * \brief Roles that are attributed to symbol occurrences.
+ *
+ * Internal: this currently mirrors low 9 bits of clang::index::SymbolRole with
+ * higher bits zeroed. These high bits may be exposed in the future.
+ */
+typedef enum {
+  CXSymbolRole_None = 0,
+  CXSymbolRole_Declaration = 1 << 0,
+  CXSymbolRole_Definition = 1 << 1,
+  CXSymbolRole_Reference = 1 << 2,
+  CXSymbolRole_Read = 1 << 3,
+  CXSymbolRole_Write = 1 << 4,
+  CXSymbolRole_Call = 1 << 5,
+  CXSymbolRole_Dynamic = 1 << 6,
+  CXSymbolRole_AddressOf = 1 << 7,
+  CXSymbolRole_Implicit = 1 << 8
+} CXSymbolRole;
+
+/**
  * \brief Data for IndexerCallbacks#indexEntityReference.
  */
 typedef struct {
@@ -6135,6 +6157,10 @@ typedef struct {
    * \brief Lexical container context of the reference.
    */
   const CXIdxContainerInfo *container;
+  /**
+   * \brief Sets of symbol roles of the reference.
+   */
+  CXSymbolRole role;
 } CXIdxEntityRefInfo;
 
 /**

Modified: cfe/trunk/include/clang/Index/IndexSymbol.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/IndexSymbol.h?rev=324914&r1=324913&r2=324914&view=diff
==============================================================================
--- cfe/trunk/include/clang/Index/IndexSymbol.h (original)
+++ cfe/trunk/include/clang/Index/IndexSymbol.h Mon Feb 12 09:42:09 2018
@@ -89,6 +89,8 @@ enum class SymbolProperty : SymbolProper
 static const unsigned SymbolPropertyBitNum = 8;
 
 /// Set of roles that are attributed to symbol occurrences.
+///
+/// Low 9 bits of clang-c/include/Index.h CXSymbolRole mirrors this enum.
 enum class SymbolRole : uint32_t {
   Declaration = 1 << 0,
   Definition  = 1 << 1,

Modified: cfe/trunk/test/Index/index-refs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/index-refs.cpp?rev=324914&r1=324913&r2=324914&view=diff
==============================================================================
--- cfe/trunk/test/Index/index-refs.cpp (original)
+++ cfe/trunk/test/Index/index-refs.cpp Mon Feb 12 09:42:09 2018
@@ -67,6 +67,9 @@ struct S2 {
 
 void foo5() {
   struct S2 s = { .y = 1, .x = 4};
+  s.y = s.x + 1;
+  (void)&foo3;
+  foo4(s.y);
 }
 
 int ginitlist[] = {EnumVal};
@@ -105,7 +108,7 @@ int ginitlist[] = {EnumVal};
 // CHECK:      [indexDeclaration]: kind: c++-class-template | name: TS | {{.*}} | loc: 47:8
 // CHECK-NEXT: [indexDeclaration]: kind: struct-template-partial-spec | name: TS | USR: c:@SP>1#T at TS>#t0.0#I | {{.*}} | loc: 50:8
 // CHECK-NEXT: [indexDeclaration]: kind: typedef | name: MyInt | USR: c:index-refs.cpp at SP>1#T at TS>#t0.0#I at T@MyInt | {{.*}} | loc: 51:15 | semantic-container: [TS:50:8] | lexical-container: [TS:50:8]
-// CHECK-NEXT: [indexEntityReference]: kind: c++-class-template | name: TS | USR: c:@ST>2#T#T at TS | lang: C++ | cursor: TemplateRef=TS:47:8 | loc: 50:8 | <parent>:: <<NULL>> | container: [TU] | refkind: direct
+// CHECK-NEXT: [indexEntityReference]: kind: c++-class-template | name: TS | USR: c:@ST>2#T#T at TS | lang: C++ | cursor: TemplateRef=TS:47:8 | loc: 50:8 | <parent>:: <<NULL>> | container: [TU] | refkind: direct | role: ref
 /* when indexing implicit instantiations
   [indexDeclaration]: kind: struct-template-spec | name: TS | USR: c:@S at TS>#I | {{.*}} | loc: 50:8
   [indexDeclaration]: kind: typedef | name: MyInt | USR: c:index-refs.cpp at 593@S at TS>#I at T@MyInt | {{.*}} | loc: 51:15 | semantic-container: [TS:50:8] | lexical-container: [TS:50:8]
@@ -117,7 +120,7 @@ int ginitlist[] = {EnumVal};
 // CHECK-NEXT: [indexEntityReference]: kind: c++-class-template | name: TS | USR: c:@ST>2#T#T at TS | {{.*}} | loc: 55:3
 
 // CHECK:      [indexEntityReference]: kind: variable | name: array_size | {{.*}} | loc: 59:22
-// CHECK:      [indexEntityReference]: kind: variable | name: default_param | {{.*}} | loc: 62:19
+// CHECK:      [indexEntityReference]: kind: variable | name: default_param | {{.*}} | loc: 62:19 | {{.*}} | role: ref read
 // CHECK-NOT:  [indexEntityReference]: kind: variable | name: default_param | {{.*}} | loc: 62:19
 
 // CHECK:      [indexEntityReference]: kind: field | name: y | {{.*}} | loc: 69:20
@@ -125,6 +128,11 @@ int ginitlist[] = {EnumVal};
 // CHECK-NOT:  [indexEntityReference]: kind: field | name: y | {{.*}} | loc: 69:20
 // CHECK-NOT:  [indexEntityReference]: kind: field | name: x | {{.*}} | loc: 69:28
 
+// CHECK:      [indexEntityReference]: kind: field | name: y | {{.*}} | loc: 70:5 | {{.*}} | role: ref write
+// CHECK:      [indexEntityReference]: kind: field | name: x | {{.*}} | loc: 70:11 | {{.*}} | role: ref read
+// CHECK:      [indexEntityReference]: kind: function | name: foo3 | {{.*}} | loc: 71:10 | {{.*}} | role: ref addr
+// CHECK:      [indexEntityReference]: kind: function | name: foo4 | {{.*}} | loc: 72:3 | {{.*}} | role: ref call
+
 // CHECK:      [indexDeclaration]: kind: variable | name: ginitlist |
-// CHECK:      [indexEntityReference]: kind: enumerator | name: EnumVal | {{.*}} | loc: 72:20
-// CHECK-NOT:  [indexEntityReference]: kind: enumerator | name: EnumVal | {{.*}} | loc: 72:20
+// CHECK:      [indexEntityReference]: kind: enumerator | name: EnumVal | {{.*}} | loc: 75:20
+// CHECK-NOT:  [indexEntityReference]: kind: enumerator | name: EnumVal | {{.*}} | loc: 75:20

Modified: cfe/trunk/test/Index/index-subscripting-literals.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/index-subscripting-literals.m?rev=324914&r1=324913&r2=324914&view=diff
==============================================================================
--- cfe/trunk/test/Index/index-subscripting-literals.m (original)
+++ cfe/trunk/test/Index/index-subscripting-literals.m Mon Feb 12 09:42:09 2018
@@ -42,7 +42,7 @@ void testDict() {
 // RUN: c-index-test -index-file -target x86_64-apple-macosx10.7 %s | FileCheck %s
 
 // CHECK:      [indexEntityReference]: kind: variable | name: idx | USR: c:@idx | lang: C | cursor: DeclRefExpr=idx:22:5 | loc: 27:9
-// CHECK-NEXT: [indexEntityReference]: kind: variable | name: p | USR: c:@p | lang: C | cursor: DeclRefExpr=p:23:4 | loc: 27:16 | <parent>:: kind: function | name: testArray | USR: c:@F at testArray | lang: C | container: [testArray:25:4] | refkind: direct
+// CHECK-NEXT: [indexEntityReference]: kind: variable | name: p | USR: c:@p | lang: C | cursor: DeclRefExpr=p:23:4 | loc: 27:16 | <parent>:: kind: function | name: testArray | USR: c:@F at testArray | lang: C | container: [testArray:25:4] | refkind: direct | role: ref
 // CHECK-NEXT: [indexEntityReference]: kind: objc-instance-method | name: setObject:atIndexedSubscript:
 // CHECK-NEXT: [indexEntityReference]: kind: objc-class | name: NSArray
 // CHECK-NEXT: [indexEntityReference]: kind: objc-class-method | name: arrayWithObjects:count: 

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=324914&r1=324913&r2=324914&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Mon Feb 12 09:42:09 2018
@@ -3326,6 +3326,27 @@ static void printProtocolList(const CXId
   }
 }
 
+static void printSymbolRole(CXSymbolRole role) {
+  if (role & CXSymbolRole_Declaration)
+    printf(" decl");
+  if (role & CXSymbolRole_Definition)
+    printf(" def");
+  if (role & CXSymbolRole_Reference)
+    printf(" ref");
+  if (role & CXSymbolRole_Read)
+    printf(" read");
+  if (role & CXSymbolRole_Write)
+    printf(" write");
+  if (role & CXSymbolRole_Call)
+    printf(" call");
+  if (role & CXSymbolRole_Dynamic)
+    printf(" dyn");
+  if (role & CXSymbolRole_AddressOf)
+    printf(" addr");
+  if (role & CXSymbolRole_Implicit)
+    printf(" implicit");
+}
+
 static void index_diagnostic(CXClientData client_data,
                              CXDiagnosticSet diagSet, void *reserved) {
   CXString str;
@@ -3544,9 +3565,11 @@ static void index_indexEntityReference(C
   printCXIndexContainer(info->container);
   printf(" | refkind: ");
   switch (info->kind) {
-  case CXIdxEntityRef_Direct: printf("direct"); break;
-  case CXIdxEntityRef_Implicit: printf("implicit"); break;
+    case CXIdxEntityRef_Direct: printf("direct"); break;
+    case CXIdxEntityRef_Implicit: printf("implicit"); break;
   }
+  printf(" | role:");
+  printSymbolRole(info->role);
   printf("\n");
 }
 

Modified: cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp?rev=324914&r1=324913&r2=324914&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp (original)
+++ cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp Mon Feb 12 09:42:09 2018
@@ -148,6 +148,11 @@ public:
     return true;
   }
 };
+
+CXSymbolRole getSymbolRole(SymbolRoleSet Role) {
+  // CXSymbolRole mirrors low 9 bits of clang::index::SymbolRole.
+  return CXSymbolRole(static_cast<uint32_t>(Role) & ((1 << 9) - 1));
+}
 }
 
 bool CXIndexDataConsumer::handleDeclOccurence(const Decl *D,
@@ -184,6 +189,7 @@ bool CXIndexDataConsumer::handleDeclOccu
     if (Roles & (unsigned)SymbolRole::Implicit) {
       Kind = CXIdxEntityRef_Implicit;
     }
+    CXSymbolRole CXRole = getSymbolRole(Roles);
 
     CXCursor Cursor;
     if (ASTNode.OrigE) {
@@ -202,7 +208,7 @@ bool CXIndexDataConsumer::handleDeclOccu
     }
     handleReference(ND, Loc, Cursor,
                     dyn_cast_or_null<NamedDecl>(ASTNode.Parent),
-                    ASTNode.ContainerDC, ASTNode.OrigE, Kind);
+                    ASTNode.ContainerDC, ASTNode.OrigE, Kind, CXRole);
 
   } else {
     const DeclContext *LexicalDC = ASTNode.ContainerDC;
@@ -889,13 +895,14 @@ bool CXIndexDataConsumer::handleReferenc
                                       const NamedDecl *Parent,
                                       const DeclContext *DC,
                                       const Expr *E,
-                                      CXIdxEntityRefKind Kind) {
+                                      CXIdxEntityRefKind Kind,
+                                      CXSymbolRole Role) {
   if (!D || !DC)
     return false;
 
   CXCursor Cursor = E ? MakeCXCursor(E, cast<Decl>(DC), CXTU)
                       : getRefCursor(D, Loc);
-  return handleReference(D, Loc, Cursor, Parent, DC, E, Kind);
+  return handleReference(D, Loc, Cursor, Parent, DC, E, Kind, Role);
 }
 
 bool CXIndexDataConsumer::handleReference(const NamedDecl *D, SourceLocation Loc,
@@ -903,7 +910,8 @@ bool CXIndexDataConsumer::handleReferenc
                                       const NamedDecl *Parent,
                                       const DeclContext *DC,
                                       const Expr *E,
-                                      CXIdxEntityRefKind Kind) {
+                                      CXIdxEntityRefKind Kind,
+                                      CXSymbolRole Role) {
   if (!CB.indexEntityReference)
     return false;
 
@@ -939,7 +947,8 @@ bool CXIndexDataConsumer::handleReferenc
                               getIndexLoc(Loc),
                               &RefEntity,
                               Parent ? &ParentEntity : nullptr,
-                              &Container };
+                              &Container,
+                              Role };
   CB.indexEntityReference(ClientData, &Info);
   return true;
 }

Modified: cfe/trunk/tools/libclang/CXIndexDataConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXIndexDataConsumer.h?rev=324914&r1=324913&r2=324914&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXIndexDataConsumer.h (original)
+++ cfe/trunk/tools/libclang/CXIndexDataConsumer.h Mon Feb 12 09:42:09 2018
@@ -436,13 +436,15 @@ public:
                        const NamedDecl *Parent,
                        const DeclContext *DC,
                        const Expr *E = nullptr,
-                       CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct);
+                       CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct,
+                       CXSymbolRole Role = CXSymbolRole_None);
 
   bool handleReference(const NamedDecl *D, SourceLocation Loc,
                        const NamedDecl *Parent,
                        const DeclContext *DC,
                        const Expr *E = nullptr,
-                       CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct);
+                       CXIdxEntityRefKind Kind = CXIdxEntityRef_Direct,
+                       CXSymbolRole Role = CXSymbolRole_None);
 
   bool isNotFromSourceFile(SourceLocation Loc) const;
 




More information about the cfe-commits mailing list