Skip to content

Commit 74d7a15

Browse files
authored
Merge pull request #75996 from tshortli/module-interface-skip-spi-platform-attrs
ModuleInterface: Skip attributes referencing SPI platforms
2 parents d1125e9 + f042ba8 commit 74d7a15

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

include/swift/AST/PlatformKind.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ bool inheritsAvailabilityFromPlatform(PlatformKind Child, PlatformKind Parent);
9191
llvm::VersionTuple canonicalizePlatformVersion(
9292
PlatformKind platform, const llvm::VersionTuple &version);
9393

94+
/// Returns true if \p Platform should be considered to be SPI and therefore not
95+
/// printed in public `.swiftinterface` files, for example.
96+
bool isPlatformSPI(PlatformKind Platform);
97+
9498
} // end namespace swift
9599

96100
#endif

lib/AST/Attr.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,30 @@ static void printDifferentiableAttrArguments(
973973
printer << '(' << stream.str() << ')';
974974
}
975975

976+
/// Returns the `PlatformKind` referenced by \p attr if applicable, or
977+
/// `std::nullopt` otherwise.
978+
static std::optional<PlatformKind>
979+
referencedPlatform(const DeclAttribute *attr) {
980+
switch (attr->getKind()) {
981+
case DeclAttrKind::Available:
982+
return static_cast<const AvailableAttr *>(attr)->Platform;
983+
case DeclAttrKind::BackDeployed:
984+
return static_cast<const BackDeployedAttr *>(attr)->Platform;
985+
case DeclAttrKind::OriginallyDefinedIn:
986+
return static_cast<const OriginallyDefinedInAttr *>(attr)->Platform;
987+
default:
988+
return std::nullopt;
989+
}
990+
}
991+
992+
/// Returns true if \p attr contains a reference to a `PlatformKind` that should
993+
/// be considered SPI.
994+
static bool referencesSPIPlatform(const DeclAttribute *attr) {
995+
if (auto platform = referencedPlatform(attr))
996+
return isPlatformSPI(*platform);
997+
return false;
998+
}
999+
9761000
void DeclAttributes::print(ASTPrinter &Printer, const PrintOptions &Options,
9771001
const Decl *D) const {
9781002
if (!DeclAttrs)
@@ -995,6 +1019,8 @@ void DeclAttributes::print(ASTPrinter &Printer, const PrintOptions &Options,
9951019
AttributeVector longAttributes;
9961020
AttributeVector attributes;
9971021
AttributeVector modifiers;
1022+
bool libraryLevelAPI =
1023+
D->getASTContext().LangOpts.LibraryLevel == LibraryLevel::API;
9981024

9991025
for (auto DA : llvm::reverse(FlattenedAttrs)) {
10001026
// Don't skip implicit custom attributes. Custom attributes like global
@@ -1010,6 +1036,12 @@ void DeclAttributes::print(ASTPrinter &Printer, const PrintOptions &Options,
10101036
if (Options.excludeAttrKind(DA->getKind()))
10111037
continue;
10121038

1039+
// In the public interfaces of -library-level=api modules, skip attributes
1040+
// that reference SPI platforms.
1041+
if (Options.printPublicInterface() && libraryLevelAPI &&
1042+
referencesSPIPlatform(DA))
1043+
continue;
1044+
10131045
// If we're supposed to suppress expanded macros, check whether this is
10141046
// a macro.
10151047
if (Options.SuppressExpandedMacros) {

lib/AST/PlatformKind.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,25 @@ llvm::VersionTuple swift::canonicalizePlatformVersion(
262262

263263
return version;
264264
}
265+
266+
bool swift::isPlatformSPI(PlatformKind Platform) {
267+
switch (Platform) {
268+
case PlatformKind::macOS:
269+
case PlatformKind::macOSApplicationExtension:
270+
case PlatformKind::iOS:
271+
case PlatformKind::iOSApplicationExtension:
272+
case PlatformKind::macCatalyst:
273+
case PlatformKind::macCatalystApplicationExtension:
274+
case PlatformKind::tvOS:
275+
case PlatformKind::tvOSApplicationExtension:
276+
case PlatformKind::watchOS:
277+
case PlatformKind::watchOSApplicationExtension:
278+
case PlatformKind::visionOS:
279+
case PlatformKind::visionOSApplicationExtension:
280+
case PlatformKind::OpenBSD:
281+
case PlatformKind::Windows:
282+
case PlatformKind::none:
283+
return false;
284+
}
285+
llvm_unreachable("bad PlatformKind");
286+
}

0 commit comments

Comments
 (0)