[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

llvm-svn: 324914
This commit is contained in:
Fangrui Song 2018-02-12 17:42:09 +00:00
parent b0a17edff7
commit 31b97194e6
7 changed files with 85 additions and 15 deletions

View File

@ -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 {
/**
@ -6105,6 +6108,25 @@ typedef enum {
CXIdxEntityRef_Implicit = 2
} 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.
*/
@ -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;
/**

View File

@ -89,6 +89,8 @@ enum class SymbolProperty : SymbolPropertySet {
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,

View File

@ -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@TS>#t0.0#I | {{.*}} | loc: 50:8
// CHECK-NEXT: [indexDeclaration]: kind: typedef | name: MyInt | USR: c:index-refs.cpp@SP>1#T@TS>#t0.0#I@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@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@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@TS>#I | {{.*}} | loc: 50:8
[indexDeclaration]: kind: typedef | name: MyInt | USR: c:index-refs.cpp@593@S@TS>#I@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@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

View File

@ -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@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@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:

View File

@ -3326,6 +3326,27 @@ static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
}
}
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(CXClientData client_data,
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");
}

View File

@ -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::handleDeclOccurence(const Decl *D,
if (Roles & (unsigned)SymbolRole::Implicit) {
Kind = CXIdxEntityRef_Implicit;
}
CXSymbolRole CXRole = getSymbolRole(Roles);
CXCursor Cursor;
if (ASTNode.OrigE) {
@ -202,7 +208,7 @@ bool CXIndexDataConsumer::handleDeclOccurence(const Decl *D,
}
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::handleReference(const NamedDecl *D, SourceLocation Loc
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::handleReference(const NamedDecl *D, SourceLocation Loc
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::handleReference(const NamedDecl *D, SourceLocation Loc
getIndexLoc(Loc),
&RefEntity,
Parent ? &ParentEntity : nullptr,
&Container };
&Container,
Role };
CB.indexEntityReference(ClientData, &Info);
return true;
}

View File

@ -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;