Skip to content

Commit 89caa91

Browse files
authored
Merge pull request #7467 from akyrtzi/index-extension-usr
2 parents 01021b2 + 560bb99 commit 89caa91

File tree

5 files changed

+45
-20
lines changed

5 files changed

+45
-20
lines changed

lib/Index/Index.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,20 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
132132
llvm::DenseMap<DeclAccessorPair, NameAndUSR> accessorNameAndUSRCache;
133133
StringScratchSpace stringStorage;
134134

135-
bool getNameAndUSR(ValueDecl *D, StringRef &name, StringRef &USR) {
136-
auto &result = nameAndUSRCache[D];
135+
bool getNameAndUSR(ValueDecl *D, ExtensionDecl *ExtD,
136+
StringRef &name, StringRef &USR) {
137+
auto &result = nameAndUSRCache[ExtD ? (Decl*)ExtD : D];
137138
if (result.USR.empty()) {
138139
SmallString<128> storage;
139140
{
140141
llvm::raw_svector_ostream OS(storage);
141-
if (ide::printDeclUSR(D, OS))
142-
return true;
142+
if (ExtD) {
143+
if (ide::printExtensionUSR(ExtD, OS))
144+
return true;
145+
} else {
146+
if (ide::printDeclUSR(D, OS))
147+
return true;
148+
}
143149
result.USR = stringStorage.copyString(OS.str());
144150
}
145151

@@ -190,7 +196,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
190196

191197
if (SymInfo.Kind == SymbolKind::Unknown)
192198
return true;
193-
if (getNameAndUSR(D, Name, USR))
199+
if (getNameAndUSR(D, /*ExtD=*/nullptr, Name, USR))
194200
return true;
195201

196202
Info.Relations.push_back(IndexRelation(RelationRoles, D, SymInfo, Name, USR));
@@ -361,6 +367,8 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
361367

362368
bool initIndexSymbol(ValueDecl *D, SourceLoc Loc, bool IsRef,
363369
IndexSymbol &Info);
370+
bool initIndexSymbol(ExtensionDecl *D, ValueDecl *ExtendedD, SourceLoc Loc,
371+
IndexSymbol &Info);
364372
bool initFuncDeclIndexSymbol(FuncDecl *D, IndexSymbol &Info);
365373
bool initFuncRefIndexSymbol(Expr *CurrentE, Expr *ParentE, ValueDecl *D,
366374
SourceLoc Loc, IndexSymbol &Info);
@@ -720,11 +728,9 @@ bool IndexSwiftASTWalker::reportExtension(ExtensionDecl *D) {
720728
return true;
721729

722730
IndexSymbol Info;
723-
if (initIndexSymbol(NTD, Loc, /*IsRef=*/false, Info))
731+
if (initIndexSymbol(D, NTD, Loc, Info))
724732
return true;
725733

726-
Info.symInfo = getSymbolInfoForDecl(D);
727-
728734
if (!startEntity(D, Info))
729735
return false;
730736

@@ -857,7 +863,7 @@ bool IndexSwiftASTWalker::initIndexSymbol(ValueDecl *D, SourceLoc Loc,
857863
else
858864
Info.roles |= (unsigned)SymbolRole::Definition;
859865

860-
if (getNameAndUSR(D, Info.name, Info.USR))
866+
if (getNameAndUSR(D, /*ExtD=*/nullptr, Info.name, Info.USR))
861867
return true;
862868

863869
std::tie(Info.line, Info.column) = getLineCol(Loc);
@@ -868,6 +874,25 @@ bool IndexSwiftASTWalker::initIndexSymbol(ValueDecl *D, SourceLoc Loc,
868874
return false;
869875
}
870876

877+
bool IndexSwiftASTWalker::initIndexSymbol(ExtensionDecl *ExtD, ValueDecl *ExtendedD,
878+
SourceLoc Loc, IndexSymbol &Info) {
879+
assert(ExtD && ExtendedD);
880+
Info.decl = ExtendedD;
881+
Info.symInfo = getSymbolInfoForDecl(ExtD);
882+
if (Info.symInfo.Kind == SymbolKind::Unknown)
883+
return true;
884+
885+
Info.roles |= (unsigned)SymbolRole::Definition;
886+
887+
if (getNameAndUSR(ExtendedD, ExtD, Info.name, Info.USR))
888+
return true;
889+
890+
std::tie(Info.line, Info.column) = getLineCol(Loc);
891+
if (auto Group = ExtD->getGroupName())
892+
Info.group = Group.getValue();
893+
return false;
894+
}
895+
871896
static NominalTypeDecl *getNominalParent(ValueDecl *D) {
872897
Type Ty = D->getDeclContext()->getDeclaredTypeOfContext();
873898
if (!Ty)

test/Index/kinds.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,23 @@ protocol AProtocol {
122122
}
123123

124124
// Extension
125-
extension AnEnumeration {}
126-
// CHECK: [[@LINE-1]]:11 | extension/ext-enum/Swift | AnEnumeration | s:14swift_ide_test13AnEnumerationO | Def | rel: 0
125+
extension AnEnumeration { func extFn() {} }
126+
// CHECK: [[@LINE-1]]:11 | extension/ext-enum/Swift | AnEnumeration | 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
128128
// CHECK-NEXT: RelExt | AnEnumeration | s:14swift_ide_test13AnEnumerationO
129129

130-
extension AStruct {}
131-
// CHECK: [[@LINE-1]]:11 | extension/ext-struct/Swift | AStruct | s:14swift_ide_test7AStructV | Def | rel: 0
130+
extension AStruct { func extFn() {} }
131+
// CHECK: [[@LINE-1]]:11 | extension/ext-struct/Swift | AStruct | 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
133133
// CHECK-NEXT: RelExt | AStruct | s:14swift_ide_test7AStructV
134134

135-
extension AClass {}
136-
// CHECK: [[@LINE-1]]:11 | extension/ext-class/Swift | AClass | s:14swift_ide_test6AClassC | Def | rel: 0
135+
extension AClass { func extFn() {} }
136+
// CHECK: [[@LINE-1]]:11 | extension/ext-class/Swift | AClass | 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
138138
// CHECK-NEXT: RelExt | AClass | s:14swift_ide_test6AClassC
139139

140-
extension AProtocol {}
141-
// CHECK: [[@LINE-1]]:11 | extension/ext-protocol/Swift | AProtocol | s:14swift_ide_test9AProtocolP | Def | rel: 0
140+
extension AProtocol { func extFn() }
141+
// CHECK: [[@LINE-1]]:11 | extension/ext-protocol/Swift | AProtocol | 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
143143
// CHECK-NEXT: RelExt | AProtocol | s:14swift_ide_test9AProtocolP
144144

test/Index/roles.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class ASubClass : AClass, AProtocol {
159159
// RelationExtendedBy
160160
// FIXME give extensions their own USR like ObjC?
161161
extension AClass {
162-
// CHECK: [[@LINE-1]]:11 | extension/ext-class/Swift | AClass | s:C14swift_ide_test6AClass | Def | rel: 0
162+
// CHECK: [[@LINE-1]]:11 | extension/ext-class/Swift | AClass | s:e:s:FC14swift_ide_test6AClass3barFT_Si | Def | rel: 0
163163
// CHECK: [[@LINE-2]]:11 | class/Swift | AClass | s:C14swift_ide_test6AClass | Ref,RelExt | rel: 1
164164
// CHECK-NEXT: RelExt | AClass | s:C14swift_ide_test6AClass
165165

test/SourceKit/Indexing/Inputs/test_module.index.response

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
{
9797
key.kind: source.lang.swift.decl.extension.class,
9898
key.name: "C2",
99-
key.usr: "s:C11test_module2C2",
99+
key.usr: "s:e:s:C11test_module2C2s:P11test_module5Prot3",
100100
key.related: [
101101
{
102102
key.kind: source.lang.swift.ref.protocol,

test/SourceKit/Indexing/index.swift.response

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@
362362
{
363363
key.kind: source.lang.swift.decl.extension.class,
364364
key.name: "CC",
365-
key.usr: "s:C5index2CC",
365+
key.usr: "s:e:s:FC5index2CC5meth2FS0_T_",
366366
key.line: 49,
367367
key.column: 11,
368368
key.related: [

0 commit comments

Comments
 (0)