Skip to content

Commit 29cd551

Browse files
authored
Merge pull request #42432 from xymus/consider-more-paths-as-spi
[Sema] Consider more paths in the SDK as defining private modules
2 parents 651af99 + 91001f0 commit 29cd551

File tree

5 files changed

+28
-41
lines changed

5 files changed

+28
-41
lines changed

lib/AST/Module.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,26 +2628,35 @@ LibraryLevel
26282628
ModuleLibraryLevelRequest::evaluate(Evaluator &evaluator,
26292629
const ModuleDecl *module) const {
26302630
auto &ctx = module->getASTContext();
2631+
namespace path = llvm::sys::path;
2632+
SmallString<128> scratch;
2633+
2634+
/// Is \p path under the folder SDK/a/b/c/d/e?
2635+
auto hasSDKPrefix =
2636+
[&](StringRef path, const Twine &a, const Twine &b = "",
2637+
const Twine &c = "", const Twine &d = "", const Twine &e = "") {
2638+
scratch = ctx.SearchPathOpts.getSDKPath();
2639+
path::append(scratch, a, b, c, d);
2640+
path::append(scratch, e);
2641+
return path.startswith(scratch);
2642+
};
26312643

26322644
/// Is \p modulePath from System/Library/PrivateFrameworks/?
26332645
auto fromPrivateFrameworks = [&](StringRef modulePath) -> bool {
26342646
if (!ctx.LangOpts.Target.isOSDarwin()) return false;
26352647

2636-
namespace path = llvm::sys::path;
2637-
SmallString<128> scratch;
2638-
scratch = ctx.SearchPathOpts.getSDKPath();
2639-
path::append(scratch, "System", "Library", "PrivateFrameworks");
2640-
return hasPrefix(path::begin(modulePath), path::end(modulePath),
2641-
path::begin(scratch), path::end(scratch));
2648+
return hasSDKPrefix(modulePath, "AppleInternal", "Library", "Frameworks") ||
2649+
hasSDKPrefix(modulePath, "System", "Library", "PrivateFrameworks") ||
2650+
hasSDKPrefix(modulePath, "System", "iOSSupport", "System", "Library", "PrivateFrameworks") ||
2651+
hasSDKPrefix(modulePath, "usr", "local", "include");
26422652
};
26432653

26442654
if (module->isNonSwiftModule()) {
26452655
if (auto *underlying = module->findUnderlyingClangModule()) {
26462656
// Imported clangmodules are SPI if they are defined by a private
26472657
// modulemap or from the PrivateFrameworks folder in the SDK.
26482658
bool moduleIsSPI = underlying->ModuleMapIsPrivate ||
2649-
(underlying->isPartOfFramework() &&
2650-
fromPrivateFrameworks(underlying->PresumedModuleMapFile));
2659+
fromPrivateFrameworks(underlying->PresumedModuleMapFile);
26512660
return moduleIsSPI ? LibraryLevel::SPI : LibraryLevel::API;
26522661
}
26532662
return LibraryLevel::Other;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Empty
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module LocalClang [system] {
2+
header "LocalClang.h"
3+
export *
4+
}

test/Sema/implementation-only-import-suggestion-as-error.swift

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/Sema/implementation-only-import-suggestion.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
// RUN: %t/sdk/System/Library/PrivateFrameworks/PrivateSwift.framework/Modules/PrivateSwift.swiftmodule/source.swift \
1212
// RUN: -o %t/sdk/System/Library/PrivateFrameworks/PrivateSwift.framework/Modules/PrivateSwift.swiftmodule/%target-swiftmodule-name
1313

14-
/// Expect warnings when building a public client.
14+
/// Expect errors when building a public client.
1515
// RUN: %target-swift-frontend -typecheck -sdk %t/sdk -module-cache-path %t %s \
1616
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
1717
// RUN: -library-level api -verify -D PUBLIC_IMPORTS -module-name MainLib
1818

19-
/// Expect no warnings when building an SPI client.
19+
/// Expect no errors when building an SPI client.
2020
// RUN: %target-swift-frontend -typecheck -sdk %t/sdk -module-cache-path %t %s \
2121
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
2222
// RUN: -library-level spi -D PUBLIC_IMPORTS -module-name MainLib
@@ -26,7 +26,7 @@
2626
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
2727
// RUN: -library-level spi -D PUBLIC_IMPORTS -module-name MainLib
2828

29-
/// Expect no warnings when building a client with some other library level.
29+
/// Expect no errors when building a client with some other library level.
3030
// RUN: %target-swift-frontend -typecheck -sdk %t/sdk -module-cache-path %t %s \
3131
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
3232
// RUN: -D PUBLIC_IMPORTS -module-name MainLib
@@ -40,9 +40,10 @@ import PrivateSwift // expected-error{{private module 'PrivateSwift' is imported
4040
import PublicClang
4141
import PublicClang_Private // expected-error{{private module 'PublicClang_Private' is imported publicly from the public module 'MainLib'}}
4242
import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' is imported publicly from the public module 'MainLib'}}
43+
import LocalClang // expected-error{{private module 'LocalClang' is imported publicly from the public module 'MainLib'}}
4344
@_exported import MainLib // expected-warning{{private module 'MainLib' is imported publicly from the public module 'MainLib'}}
4445

45-
/// Expect no warnings with implementation-only imports.
46+
/// Expect no errors with implementation-only imports.
4647
// RUN: %target-swift-frontend -typecheck -sdk %t/sdk -module-cache-path %t %s \
4748
// RUN: -F %t/sdk/System/Library/PrivateFrameworks/ \
4849
// RUN: -library-level api -D IMPL_ONLY_IMPORTS
@@ -51,6 +52,7 @@ import FullyPrivateClang // expected-error{{private module 'FullyPrivateClang' i
5152
@_implementationOnly import PrivateSwift
5253
@_implementationOnly import PublicClang_Private
5354
@_implementationOnly import FullyPrivateClang
55+
@_implementationOnly import LocalClang
5456

5557
#endif
5658

0 commit comments

Comments
 (0)