Skip to content

Commit 243baa1

Browse files
authored
Merge pull request #7622 from akyrtzi/index-extension-relation
2 parents 8ae1f92 + a132898 commit 243baa1

File tree

4 files changed

+50
-26
lines changed

4 files changed

+50
-26
lines changed

include/swift/Index/IndexSymbol.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline SymbolPropertySet &operator|=(SymbolPropertySet &SKSet, SymbolProperty SK
4545
}
4646

4747
struct IndexRelation {
48-
const ValueDecl *decl;
48+
const Decl *decl;
4949
SymbolInfo symInfo;
5050
SymbolRoleSet roles = SymbolRoleSet(0);
5151

@@ -55,7 +55,7 @@ struct IndexRelation {
5555
StringRef USR; // USR may be safely compared by pointer.
5656
StringRef group;
5757

58-
IndexRelation(SymbolRoleSet Roles, const ValueDecl *Sym, SymbolInfo SymInfo, StringRef Name, StringRef USR)
58+
IndexRelation(SymbolRoleSet Roles, const Decl *Sym, SymbolInfo SymInfo, StringRef Name, StringRef USR)
5959
: decl(Sym), symInfo(SymInfo), roles(Roles), name(Name), USR(USR) {}
6060

6161
IndexRelation() = default;

lib/Index/Index.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,21 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
189189
return false;
190190
}
191191

192-
bool addRelation(IndexSymbol &Info, SymbolRoleSet RelationRoles, ValueDecl *D) {
192+
bool addRelation(IndexSymbol &Info, SymbolRoleSet RelationRoles, Decl *D) {
193193
assert(D);
194194
StringRef Name, USR;
195195
SymbolInfo SymInfo = getSymbolInfoForDecl(D);
196196

197197
if (SymInfo.Kind == SymbolKind::Unknown)
198198
return true;
199-
if (getNameAndUSR(D, /*ExtD=*/nullptr, Name, USR))
200-
return true;
199+
if (auto *ExtD = dyn_cast<ExtensionDecl>(D)) {
200+
NominalTypeDecl *NTD = ExtD->getExtendedType()->getAnyNominal();
201+
if (getNameAndUSR(NTD, ExtD, Name, USR))
202+
return true;
203+
} else {
204+
if (getNameAndUSR(cast<ValueDecl>(D), /*ExtD=*/nullptr, Name, USR))
205+
return true;
206+
}
201207

202208
Info.Relations.push_back(IndexRelation(RelationRoles, D, SymInfo, Name, USR));
203209
Info.roles |= RelationRoles;
@@ -339,9 +345,9 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
339345
bool startEntity(Decl *D, IndexSymbol &Info);
340346
bool startEntityDecl(ValueDecl *D);
341347

342-
bool reportRelatedRef(ValueDecl *D, SourceLoc Loc, SymbolRoleSet Relations, ValueDecl *Related);
343-
bool reportRelatedTypeRef(const TypeLoc &Ty, SymbolRoleSet Relations, ValueDecl *Related);
344-
bool reportInheritedTypeRefs(ArrayRef<TypeLoc> Inherited, ValueDecl *Inheritee);
348+
bool reportRelatedRef(ValueDecl *D, SourceLoc Loc, SymbolRoleSet Relations, Decl *Related);
349+
bool reportRelatedTypeRef(const TypeLoc &Ty, SymbolRoleSet Relations, Decl *Related);
350+
bool reportInheritedTypeRefs(ArrayRef<TypeLoc> Inherited, Decl *Inheritee);
345351
NominalTypeDecl *getTypeLocAsNominalTypeDecl(const TypeLoc &Ty);
346352

347353
bool reportPseudoGetterDecl(VarDecl *D) {
@@ -609,7 +615,7 @@ bool IndexSwiftASTWalker::startEntityDecl(ValueDecl *D) {
609615
return startEntity(D, Info);
610616
}
611617

612-
bool IndexSwiftASTWalker::reportRelatedRef(ValueDecl *D, SourceLoc Loc, SymbolRoleSet Relations, ValueDecl *Related) {
618+
bool IndexSwiftASTWalker::reportRelatedRef(ValueDecl *D, SourceLoc Loc, SymbolRoleSet Relations, Decl *Related) {
613619
if (!shouldIndex(D))
614620
return true;
615621

@@ -628,15 +634,15 @@ bool IndexSwiftASTWalker::reportRelatedRef(ValueDecl *D, SourceLoc Loc, SymbolRo
628634
return !Cancelled;
629635
}
630636

631-
bool IndexSwiftASTWalker::reportInheritedTypeRefs(ArrayRef<TypeLoc> Inherited, ValueDecl *Inheritee) {
637+
bool IndexSwiftASTWalker::reportInheritedTypeRefs(ArrayRef<TypeLoc> Inherited, Decl *Inheritee) {
632638
for (auto Base : Inherited) {
633639
if(!reportRelatedTypeRef(Base, (SymbolRoleSet) SymbolRole::RelationBaseOf, Inheritee))
634640
return false;
635641
}
636642
return true;
637643
}
638644

639-
bool IndexSwiftASTWalker::reportRelatedTypeRef(const TypeLoc &Ty, SymbolRoleSet Relations, ValueDecl *Related) {
645+
bool IndexSwiftASTWalker::reportRelatedTypeRef(const TypeLoc &Ty, SymbolRoleSet Relations, Decl *Related) {
640646

641647
if (IdentTypeRepr *T = dyn_cast_or_null<IdentTypeRepr>(Ty.getTypeRepr())) {
642648
auto Comps = T->getComponentRange();
@@ -718,7 +724,10 @@ IndexSwiftASTWalker::getTypeLocAsNominalTypeDecl(const TypeLoc &Ty) {
718724
}
719725

720726
bool IndexSwiftASTWalker::reportExtension(ExtensionDecl *D) {
721-
SourceLoc Loc = D->getExtendedTypeLoc().getSourceRange().Start;
727+
// Use the 'End' token of the range, in case it is a compound name, e.g.
728+
// extension A.B {}
729+
// we want the location of 'B' token.
730+
SourceLoc Loc = D->getExtendedTypeLoc().getSourceRange().End;
722731
if (!D->getExtendedType())
723732
return true;
724733
NominalTypeDecl *NTD = D->getExtendedType()->getAnyNominal();
@@ -734,11 +743,10 @@ bool IndexSwiftASTWalker::reportExtension(ExtensionDecl *D) {
734743
if (!startEntity(D, Info))
735744
return false;
736745

737-
// FIXME: make extensions their own entity
738746
if (!reportRelatedRef(NTD, Loc, (SymbolRoleSet)SymbolRole::RelationExtendedBy,
739-
NTD))
747+
D))
740748
return false;
741-
if (!reportInheritedTypeRefs(D->getInherited(), NTD))
749+
if (!reportInheritedTypeRefs(D->getInherited(), D))
742750
return false;
743751

744752
return true;

test/Index/kinds.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,24 +123,24 @@ protocol AProtocol {
123123

124124
// Extension
125125
extension AnEnumeration { func extFn() {} }
126-
// CHECK: [[@LINE-1]]:11 | extension/ext-enum/Swift | AnEnumeration | s:e:s:14swift_ide_test13AnEnumerationO5extFnyyF | Def | rel: 0
126+
// CHECK: [[@LINE-1]]:11 | extension/ext-enum/Swift | AnEnumeration | [[EXT_AnEnumeration_USR:s:e:s:14swift_ide_test13AnEnumerationO5extFnyyF]] | Def | rel: 0
127127
// CHECK: [[@LINE-2]]:11 | enum/Swift | AnEnumeration | s:14swift_ide_test13AnEnumerationO | Ref,RelExt | rel: 1
128-
// CHECK-NEXT: RelExt | AnEnumeration | s:14swift_ide_test13AnEnumerationO
128+
// CHECK-NEXT: RelExt | AnEnumeration | [[EXT_AnEnumeration_USR]]
129129

130130
extension AStruct { func extFn() {} }
131-
// CHECK: [[@LINE-1]]:11 | extension/ext-struct/Swift | AStruct | s:e:s:14swift_ide_test7AStructV5extFnyyF | Def | rel: 0
131+
// CHECK: [[@LINE-1]]:11 | extension/ext-struct/Swift | AStruct | [[EXT_AStruct_USR:s:e:s:14swift_ide_test7AStructV5extFnyyF]] | Def | rel: 0
132132
// CHECK: [[@LINE-2]]:11 | struct/Swift | AStruct | s:14swift_ide_test7AStructV | Ref,RelExt | rel: 1
133-
// CHECK-NEXT: RelExt | AStruct | s:14swift_ide_test7AStructV
133+
// CHECK-NEXT: RelExt | AStruct | [[EXT_AStruct_USR]]
134134

135135
extension AClass { func extFn() {} }
136-
// CHECK: [[@LINE-1]]:11 | extension/ext-class/Swift | AClass | s:e:s:14swift_ide_test6AClassC5extFnyyF | Def | rel: 0
136+
// CHECK: [[@LINE-1]]:11 | extension/ext-class/Swift | AClass | [[EXT_AClass_USR:s:e:s:14swift_ide_test6AClassC5extFnyyF]] | Def | rel: 0
137137
// CHECK: [[@LINE-2]]:11 | class/Swift | AClass | s:14swift_ide_test6AClassC | Ref,RelExt | rel: 1
138-
// CHECK-NEXT: RelExt | AClass | s:14swift_ide_test6AClassC
138+
// CHECK-NEXT: RelExt | AClass | [[EXT_AClass_USR]]
139139

140140
extension AProtocol { func extFn() }
141-
// CHECK: [[@LINE-1]]:11 | extension/ext-protocol/Swift | AProtocol | s:e:s:14swift_ide_test9AProtocolPAAE5extFnyyF | Def | rel: 0
141+
// CHECK: [[@LINE-1]]:11 | extension/ext-protocol/Swift | AProtocol | [[EXT_AProtocol_USR:s:e:s:14swift_ide_test9AProtocolPAAE5extFnyyF]] | Def | rel: 0
142142
// CHECK: [[@LINE-2]]:11 | protocol/Swift | AProtocol | s:14swift_ide_test9AProtocolP | Ref,RelExt | rel: 1
143-
// CHECK-NEXT: RelExt | AProtocol | s:14swift_ide_test9AProtocolP
143+
// CHECK-NEXT: RelExt | AProtocol | [[EXT_AProtocol_USR]]
144144

145145
// TypeAlias
146146
typealias SomeAlias = AStruct

test/Index/roles.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class AClass {
134134
}
135135

136136
protocol AProtocol {
137+
// CHECK: [[@LINE-1]]:10 | protocol/Swift | AProtocol | [[AProtocol_USR:.*]] | Def | rel: 0
137138
func foo() -> Int
138139
// CHECK: [[@LINE-1]]:8 | instance-method/Swift | foo() | s:FP14swift_ide_test9AProtocol3fooFT_Si | Def,RelChild | rel: 1
139140
// CHECK-NEXT: RelChild | AProtocol | s:P14swift_ide_test9AProtocol
@@ -157,17 +158,32 @@ class ASubClass : AClass, AProtocol {
157158
}
158159

159160
// RelationExtendedBy
160-
// FIXME give extensions their own USR like ObjC?
161161
extension AClass {
162-
// CHECK: [[@LINE-1]]:11 | extension/ext-class/Swift | AClass | s:e:s:FC14swift_ide_test6AClass3barFT_Si | Def | rel: 0
162+
// CHECK: [[@LINE-1]]:11 | extension/ext-class/Swift | AClass | [[EXT_ACLASS_USR:.*]] | Def | rel: 0
163163
// CHECK: [[@LINE-2]]:11 | class/Swift | AClass | s:C14swift_ide_test6AClass | Ref,RelExt | rel: 1
164-
// CHECK-NEXT: RelExt | AClass | s:C14swift_ide_test6AClass
164+
// CHECK-NEXT: RelExt | AClass | [[EXT_ACLASS_USR]]
165165

166166
func bar() -> Int { return 2 }
167167
// CHECK: [[@LINE-1]]:8 | instance-method/Swift | bar() | s:FC14swift_ide_test6AClass3barFT_Si | Def,RelChild | rel: 1
168168
// CHECK-NEXT: RelChild | AClass | s:C14swift_ide_test6AClass
169169
}
170170

171+
struct OuterS {
172+
// CHECK: [[@LINE-1]]:8 | struct/Swift | OuterS | [[OUTERS_USR:.*]] | Def | rel: 0
173+
struct InnerS {}
174+
// CHECK: [[@LINE-1]]:10 | struct/Swift | InnerS | [[INNERS_USR:.*]] | Def,RelChild | rel: 1
175+
// CHECK-NEXT: RelChild | OuterS | [[OUTERS_USR]]
176+
}
177+
extension OuterS.InnerS : AProtocol {
178+
// CHECK: [[@LINE-1]]:18 | extension/ext-struct/Swift | InnerS | [[EXT_INNERS_USR:.*]] | Def | rel: 0
179+
// CHECK: [[@LINE-2]]:18 | struct/Swift | InnerS | [[INNERS_USR]] | Ref,RelExt | rel: 1
180+
// CHECK-NEXT: RelExt | InnerS | [[EXT_INNERS_USR]]
181+
// CHECK: [[@LINE-4]]:27 | protocol/Swift | AProtocol | [[AProtocol_USR]] | Ref,RelBase | rel: 1
182+
// CHECK-NEXT: RelBase | InnerS | [[EXT_INNERS_USR]]
183+
// CHECK: [[@LINE-6]]:11 | struct/Swift | OuterS | [[OUTERS_USR]] | Ref | rel: 0
184+
func foo() {}
185+
}
186+
171187
var anInstance = AClass(x: 1)
172188
// CHECK: [[@LINE-1]]:18 | class/Swift | AClass | s:C14swift_ide_test6AClass | Ref | rel: 0
173189
// CHECK: [[@LINE-2]]:18 | constructor/Swift | init(x:) | s:FC14swift_ide_test6AClasscFT1xSi_S0_ | Ref,Call | rel: 0

0 commit comments

Comments
 (0)