Skip to content

Commit 68b790f

Browse files
authored
Merge pull request #34338 from xymus/indexing-empty-extensions
[Index] Don't report extensions with nothing to index
2 parents 5d30503 + f6931c8 commit 68b790f

File tree

5 files changed

+116
-3
lines changed

5 files changed

+116
-3
lines changed

lib/Index/Index.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,22 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
631631
return true;
632632
}
633633

634+
// Are there members or conformances in \c D that should be indexed?
635+
bool shouldIndexMembers(ExtensionDecl *D) {
636+
for (auto Member : D->getMembers())
637+
if (auto VD = dyn_cast<ValueDecl>(Member))
638+
if (shouldIndex(VD, /*IsRef=*/false))
639+
return true;
640+
641+
for (auto Inherit : D->getInherited())
642+
if (auto T = Inherit.getType())
643+
if (T->getAnyNominal() &&
644+
shouldIndex(T->getAnyNominal(), /*IsRef=*/false))
645+
return true;
646+
647+
return false;
648+
}
649+
634650
/// Reports all implicit member value decl conformances that \p D introduces
635651
/// as implicit overrides at the source location of \p D, and returns the
636652
/// explicit ones so we can check against them later on when visiting them as
@@ -1068,6 +1084,10 @@ bool IndexSwiftASTWalker::reportExtension(ExtensionDecl *D) {
10681084
if (!shouldIndex(NTD, /*IsRef=*/false))
10691085
return true;
10701086

1087+
// Don't index "empty" extensions in imported modules.
1088+
if (IsModuleFile && !shouldIndexMembers(D))
1089+
return true;
1090+
10711091
IndexSymbol Info;
10721092
if (initIndexSymbol(D, NTD, Loc, Info))
10731093
return true;

test/Index/Store/record-sourcefile.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// CHECK: type-alias/Swift | TA | s:{{.*}} | <no-cgname> | Def,RelChild -
3131
// CHECK: class/Swift | C1 | s:{{.*}} | <no-cgname> | Def,Ref,RelBase,RelCont -
3232
// CHECK: instance-method/Swift | method() | s:{{.*}} | <no-cgname> | Def,Ref,Call,Dyn,RelChild,RelRec,RelCall,RelCont -
33-
// CHECK: class/Swift | C2 | s:{{.*}} | <no-cgname> | Def -
33+
// CHECK: class/Swift | C2 | s:{{.*}} | <no-cgname> | Def,Ref - RelChild,RelBase
3434
// CHECK: instance-method/Swift | method() | s:{{.*}} | <no-cgname> | Def,Dyn,RelChild,RelOver -
3535
// CHECK: function/Swift | takeC1(x:) | s:{{.*}} | <no-cgname> | Def -
3636
// CHECK: instance-method(test)/Swift | testFoo() | s:{{.*}} | <no-cgname> | Def,Dyn,RelChild -
@@ -151,3 +151,6 @@ class MyTestCase: XCTestCase {
151151
// CHECK-NEXT: RelChild | s:4file10MyTestCaseC
152152
func testFoo() { test1() }
153153
}
154+
155+
// CHECK: [[@LINE+1]]:11 | class/Swift | s:4file2C2C | Ref | rel: 0
156+
extension C2 {}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// --- Prepare SDK (.swiftmodule).
2+
// RUN: %empty-directory(%t)
3+
// RUN: %empty-directory(%t/SDK)
4+
// RUN: mkdir -p %t/SDK/Frameworks/SomeModule.framework/Modules/SomeModule.swiftmodule
5+
// RUN: %target-swift-frontend \
6+
// RUN: -emit-module \
7+
// RUN: -module-name SomeModule \
8+
// RUN: -o %t/SDK/Frameworks/SomeModule.framework/Modules/SomeModule.swiftmodule/%module-target-triple.swiftmodule \
9+
// RUN: -swift-version 5 \
10+
// RUN: -D SOME_MODULE \
11+
// RUN: %s
12+
13+
#if SOME_MODULE
14+
15+
public func someFunc() {}
16+
17+
public class C2 {}
18+
19+
extension C2 {
20+
public func publicFunc() {}
21+
}
22+
23+
// Don't record extensions with nothing to index.
24+
extension C2 {}
25+
26+
extension C2 {
27+
internal func SECRET() {}
28+
private func SECRET1() {}
29+
fileprivate func SECRET2() {}
30+
}
31+
32+
internal protocol SECRETProto {}
33+
extension C2: SECRETProto {}
34+
35+
// -----------------------------------------------------------------------------
36+
// Test-1 - '.swiftmodule' - Normal index-while-building.
37+
//
38+
// RUN: %empty-directory(%t/idx)
39+
// RUN: %empty-directory(%t/modulecache)
40+
//
41+
// --- Built with indexing
42+
// RUN: %target-swift-frontend \
43+
// RUN: -typecheck \
44+
// RUN: -index-system-modules \
45+
// RUN: -index-ignore-stdlib \
46+
// RUN: -index-store-path %t/idx \
47+
// RUN: -sdk %t/SDK \
48+
// RUN: -Fsystem %t/SDK/Frameworks \
49+
// RUN: -module-cache-path %t/modulecache \
50+
// RUN: -D CLIENT \
51+
// RUN: %s
52+
53+
#elseif CLIENT
54+
55+
import SomeModule
56+
print(someFunc())
57+
58+
#endif
59+
60+
// -----------------------------------------------------------------------------
61+
// --- Check the records.
62+
// RUN: c-index-test core -print-record %t/idx | %FileCheck %s
63+
64+
// CHECK: 0:0 | function/Swift | s:10SomeModule8someFuncyyF | Def | rel: 0
65+
// CHECK-NEXT: 0:0 | class/Swift | [[class_USR:s:10SomeModule2C2C]] | Def | rel: 0
66+
// CHECK-NEXT: 0:0 | class/Swift | [[class_USR]] | Ref,RelExt | rel: 1
67+
// CHECK-NEXT: RelExt | s:e:[[publicFunc_USR:s:10SomeModule2C2C10publicFuncyyF]]
68+
// CHECK-NEXT: 0:0 | instance-method/Swift | [[publicFunc_USR]] | Def,Dyn,RelChild | rel: 1
69+
// CHECK-NEXT: RelChild | s:e:[[publicFunc_USR]]
70+
// CHECK-NEXT: 0:0 | extension/ext-class/Swift | s:e:[[publicFunc_USR]] | Def | rel: 0
71+
// CHECK-NOT: SECRET

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,16 @@
172172
{
173173
key.kind: source.lang.swift.decl.extension.class,
174174
key.name: "C2",
175-
key.usr: "s:e:s:11test_module2C2C6SECRETyyF"
175+
key.usr: "s:e:s:11test_module2C2C10publicFuncyyF",
176+
key.entities: [
177+
{
178+
key.kind: source.lang.swift.decl.function.method.instance,
179+
key.name: "publicFunc()",
180+
key.usr: "s:11test_module2C2C10publicFuncyyF",
181+
key.is_dynamic: 1,
182+
key.effective_access: source.decl.effective_access.public
183+
}
184+
]
176185
}
177186
]
178187
}

test/SourceKit/Indexing/Inputs/test_module.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class TwoInts {
1212
public class ComputedProperty {
1313
public var value : Int {
1414
get {
15-
var result = 0
15+
let result = 0
1616
return result
1717
}
1818
set(newVal) {
@@ -33,6 +33,16 @@ public func globalFunc() {}
3333

3434
private func SECRET() {}
3535

36+
extension C2 {
37+
public func publicFunc() {}
38+
}
39+
40+
// Don't record extensions with nothing to index.
3641
extension C2 {
3742
internal func SECRET() {}
43+
private func SECRET1() {}
44+
fileprivate func SECRET2() {}
3845
}
46+
47+
internal protocol InternalProto {}
48+
extension C2: InternalProto {}

0 commit comments

Comments
 (0)