Skip to content

Commit bddd197

Browse files
authored
Merge pull request #62353 from xymus/dont-index-local-system-modules
[Index] Only index system modules in the SDK, not local ones
2 parents 276281d + 03708d2 commit bddd197

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed

include/swift/AST/Module.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,10 @@ class ModuleDecl
859859
/// applicable.
860860
StringRef getModuleLoadedFilename() const;
861861

862+
/// \returns true if this module is defined under the SDK path.
863+
/// If no SDK path is defined, this always returns false.
864+
bool isSDKModule() const;
865+
862866
/// \returns true if this module is the "swift" standard library module.
863867
bool isStdlibModule() const;
864868

lib/AST/Module.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,6 +1996,23 @@ StringRef ModuleDecl::getModuleLoadedFilename() const {
19961996
return StringRef();
19971997
}
19981998

1999+
bool ModuleDecl::isSDKModule() const {
2000+
auto sdkPath = getASTContext().SearchPathOpts.getSDKPath();
2001+
if (sdkPath.empty())
2002+
return false;
2003+
2004+
auto modulePath = getModuleSourceFilename();
2005+
auto si = llvm::sys::path::begin(sdkPath),
2006+
se = llvm::sys::path::end(sdkPath);
2007+
for (auto mi = llvm::sys::path::begin(modulePath),
2008+
me = llvm::sys::path::end(modulePath);
2009+
si != se && mi != me; ++si, ++mi) {
2010+
if (*si != *mi)
2011+
return false;
2012+
}
2013+
return si == se;
2014+
}
2015+
19992016
bool ModuleDecl::isStdlibModule() const {
20002017
return !getParent() && getName() == getASTContext().StdlibModuleName;
20012018
}

lib/Index/IndexRecord.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,13 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
447447
}
448448
} else {
449449
// Serialized AST file.
450-
// Only index system modules (essentially stdlib and overlays).
450+
// Only index distributed system modules, and the stdlib.
451451
// We don't officially support binary swift modules, so normally
452452
// the index data for user modules would get generated while
453453
// building them.
454+
bool isDistributedModule = mod->isSDKModule();
454455
if (mod->isSystemModule() && indexSystemModules &&
456+
(isDistributedModule || mod->isStdlibModule()) &&
455457
(!skipStdlib || !mod->isStdlibModule())) {
456458
emitDataForSwiftSerializedModule(mod, indexStorePath,
457459
indexClangModules,

test/Index/Store/cross-import-overlay.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RUN: cp -r %S/../Inputs/CrossImport %t/CrossImport
44
// RUN: %{python} %S/../../CrossImport/Inputs/rewrite-module-triples.py %t/CrossImport %module-target-triple
55

6-
// RUN: %target-swift-frontend -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -c -index-store-path %t/idx -module-cache-path %t/mcp -index-system-modules -index-ignore-stdlib -enable-cross-import-overlays %s -Fsystem %t/CrossImport -o %t/file1.o -module-name cross_import_overlay
6+
// RUN: %target-swift-frontend -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -c -index-store-path %t/idx -module-cache-path %t/mcp -index-system-modules -index-ignore-stdlib -enable-cross-import-overlays %s -Fsystem %t/CrossImport -o %t/file1.o -module-name cross_import_overlay -sdk %t
77
// RUN: c-index-test core -print-unit %t/idx > %t/units
88

99
import A
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/cache)
3+
// RUN: %empty-directory(%t/idx)
4+
// RUN: %empty-directory(%t/sdk)
5+
// RUN: split-file %s %t
6+
7+
/// Create a Swift module to import.
8+
// RUN: cp %t/module.modulemap %t/sdk/
9+
// RUN: %target-swift-frontend -emit-module \
10+
// RUN: -enable-library-evolution -swift-version 5 \
11+
// RUN: -emit-module-path %t/sdk/LocalSystemModule.swiftmodule \
12+
// RUN: -emit-module-interface-path %t/sdk/LocalSystemModule.swiftinterface \
13+
// RUN: %t/LocalSystemModule.swift -I %t/sdk \
14+
// RUN: -enable-testing
15+
16+
/// Build a test client once with indexing, not setting an SDK.
17+
// RUN: %target-swift-frontend -typecheck %t/TestClient.swift \
18+
// RUN: -I %t/sdk -module-cache-path %t/cache \
19+
// RUN: -index-system-modules \
20+
// RUN: -index-store-path %t/idx \
21+
// RUN: -index-ignore-stdlib \
22+
// RUN: -sdk %t/someothersdk -Rmodule-loading 2>&1 \
23+
// RUN: | %FileCheck -check-prefix=CHECK-SWIFTMODULE %s
24+
// CHECK-SWIFTMODULE: source: '{{.*}}LocalSystemModule.swiftmodule'
25+
26+
/// Build the test client again not setting an SDK, this one should still use
27+
/// the adjacent module that is built for testing.
28+
// RUN: %target-swift-frontend -typecheck %t/TestClient.swift \
29+
// RUN: -I %t/sdk -module-cache-path %t/cache \
30+
// RUN: -sdk %t/someothersdk -Rmodule-loading 2>&1 \
31+
// RUN: | %FileCheck -check-prefix=CHECK-SWIFTMODULE %s
32+
33+
/// @testable import of a module in the SDK works once but not after it's cached.
34+
// RUN: %empty-directory(%t/idx)
35+
// RUN: %target-swift-frontend -typecheck %t/TestClient.swift \
36+
// RUN: -I %t/sdk -module-cache-path %t/cache \
37+
// RUN: -index-system-modules \
38+
// RUN: -index-store-path %t/idx \
39+
// RUN: -index-ignore-stdlib \
40+
// RUN: -sdk %t/sdk -Rmodule-loading 2>&1 \
41+
// RUN: | %FileCheck -check-prefix=CHECK-SWIFTMODULE %s
42+
43+
/// Failing case when building against the cached swiftmodule.
44+
// RUN: %target-swift-frontend -typecheck %t/TestClient.swift \
45+
// RUN: -I %t/sdk -module-cache-path %t/cache \
46+
// RUN: -sdk %t/sdk -Rmodule-loading \
47+
// RUN: -verify -verify-ignore-unknown -show-diagnostics-after-fatal 2>&1
48+
49+
//--- module.modulemap
50+
51+
module LocalSystemModule [system] { }
52+
53+
//--- LocalSystemModule.swift
54+
55+
@_exported import LocalSystemModule
56+
57+
//--- TestClient.swift
58+
59+
@testable import LocalSystemModule // expected-error {{module 'LocalSystemModule' was not compiled for testing}}
60+
// expected-remark @-1 {{LocalSystemModule.swiftinterface}}

0 commit comments

Comments
 (0)