Skip to content

Commit c28bb52

Browse files
committed
[Index] Force indexing from the swiftinterfaces of system modules
Use setIgnoreAdjacentModules before indexing any resilient system module to force indexing to read only from resilient modules. rdar://100644036
1 parent 2800768 commit c28bb52

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

lib/Index/IndexRecord.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,22 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
495495
IndexUnitWriter &parentUnitWriter,
496496
const PathRemapper &pathRemapper,
497497
SourceFile *initialFile) {
498+
// Reload resilient modules from swiftinterface to avoid indexing
499+
// internal details.
500+
if (module->getResilienceStrategy() == ResilienceStrategy::Resilient) {
501+
module->getASTContext().setIgnoreAdjacentModules(true);
502+
503+
ImportPath::Module::Builder builder(module->getName());
504+
auto reloadedModule = module->getASTContext().getModule(builder.get());
505+
506+
if (reloadedModule) {
507+
module = reloadedModule;
508+
} else {
509+
// If we can't rebuild from the swiftinterface, don't index this module.
510+
return true;
511+
}
512+
}
513+
498514
StringRef filename = module->getModuleFilename();
499515
std::string moduleName = module->getNameStr().str();
500516

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/modulecache)
3+
// RUN: split-file %s %t
4+
5+
/// Setup the SDK composed of SecretModule and SystemModule
6+
// RUN: %empty-directory(%t/SDK)
7+
// RUN: mkdir -p %t/SDK/Frameworks/SecretModule.framework/Modules/SecretModule.swiftmodule
8+
// RUN: %target-swift-frontend -emit-module -module-name SecretModule \
9+
// RUN: -swift-version 5 -enable-library-evolution -parse-stdlib \
10+
// RUN: -o %t/SDK/Frameworks/SecretModule.framework/Modules/SecretModule.swiftmodule/%module-target-triple.swiftmodule \
11+
// RUN: -emit-module-interface-path %t/SDK/Frameworks/SecretModule.framework/Modules/SecretModule.swiftmodule/%module-target-triple.swiftinterface \
12+
// RUN: %t/SecretModule.swift
13+
// RUN: mkdir -p %t/SDK/Frameworks/SystemModule.framework/Modules/SystemModule.swiftmodule
14+
// RUN: %target-swift-frontend -emit-module -module-name SystemModule \
15+
// RUN: -swift-version 5 -enable-library-evolution -parse-stdlib \
16+
// RUN: -o %t/SDK/Frameworks/SystemModule.framework/Modules/SystemModule.swiftmodule/%module-target-triple.swiftmodule \
17+
// RUN: -emit-module-interface-path %t/SDK/Frameworks/SystemModule.framework/Modules/SystemModule.swiftmodule/%module-target-triple.swiftinterface \
18+
// RUN: -Fsystem %t/SDK/Frameworks \
19+
// RUN: %t/SystemModule.swift
20+
21+
/// Index a client of SystemModule reading from the swiftinterface.
22+
/// Because of disable-deserialization-recovery and leakyFunc, reading from
23+
/// the swiftmodule would crash.
24+
// RUN: %empty-directory(%t/idx)
25+
// RUN: %target-swift-frontend -typecheck -parse-stdlib -swift-version 5 \
26+
// RUN: -index-system-modules \
27+
// RUN: -index-store-path %t/idx \
28+
// RUN: -index-ignore-stdlib \
29+
// RUN: -sdk %t/SDK \
30+
// RUN: -Fsystem %t/SDK/Frameworks \
31+
// RUN: -module-cache-path %t/modulecache \
32+
// RUN: %t/Client.swift -disable-deserialization-recovery
33+
34+
/// The index should have the public API of SystemModule
35+
// RUN: c-index-test core -print-unit %t/idx | %FileCheck -check-prefix=UNIT %s
36+
// UNIT: Unit | system | SystemModule |
37+
// UNIT: Record | system | SystemModule |
38+
// RUN: c-index-test core -print-record %t/idx | %FileCheck -check-prefix=RECORD %s
39+
// RECORD: function/Swift | systemFunc()
40+
41+
/// Index a client reading from a broken swiftinterface
42+
// RUN: %empty-directory(%t/idx)
43+
// RUN: %empty-directory(%t/modulecache)
44+
// RUN: echo "breaking_the_swifinterface" >> %t/SDK/Frameworks/SystemModule.framework/Modules/SystemModule.swiftmodule/%module-target-triple.swiftinterface
45+
// RUN: %target-swift-frontend -typecheck -parse-stdlib \
46+
// RUN: -index-system-modules \
47+
// RUN: -index-store-path %t/idx \
48+
// RUN: -index-ignore-stdlib \
49+
// RUN: -sdk %t/SDK \
50+
// RUN: -Fsystem %t/SDK/Frameworks \
51+
// RUN: -module-cache-path %t/modulecache \
52+
// RUN: %t/Client.swift \
53+
// RUN: 2>&1 | %FileCheck -check-prefix=BROKEN-BUILD --allow-empty %s
54+
55+
/// We don't expect so see the swiftinterface error for indexing
56+
// BROKEN-BUILD-NOT: error
57+
// BROKEN-BUILD-NOT: breaking_the_swifinterface
58+
59+
/// We don't expect SystemModule to be indexed with a broken swiftinterface
60+
// RUN: c-index-test core -print-unit %t/idx | %FileCheck -check-prefix=BROKEN-UNIT %s
61+
// BROKEN-UNIT: Unit | system | SystemModule |
62+
// BROKEN-UNIT-NOT: Record | system | SystemModule |
63+
// RUN: c-index-test core -print-record %t/idx | %FileCheck -check-prefix=BROKEN-RECORD %s
64+
// BROKEN-RECORD-NOT: function/Swift | systemFunc()
65+
66+
//--- SecretModule.swift
67+
public struct SecretType {}
68+
69+
//--- SystemModule.swift
70+
// Use this dependency to hit an easy deserialization failure when recovery is disabled.
71+
@_implementationOnly import SecretModule
72+
73+
public func systemFunc() { }
74+
func leakyFunc(_ a: SecretType) { }
75+
76+
//--- Client.swift
77+
import SystemModule
78+
79+
public func clientFunc() {}

0 commit comments

Comments
 (0)