Skip to content

Commit a4b4b1f

Browse files
committed
ClangImporter: don't import clang SPI attributes by default
1 parent d301f66 commit a4b4b1f

File tree

8 files changed

+27
-13
lines changed

8 files changed

+27
-13
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,9 @@ namespace swift {
740740
/// When set, don't look for or load overlays.
741741
bool DisableOverlayModules = false;
742742

743+
/// When set, import SPI_AVAILABLE symbols with Swift SPI attribtues.
744+
bool EnableClangSPI = false;
745+
743746
/// When set, don't enforce warnings with -Werror.
744747
bool DebuggerSupport = false;
745748

@@ -767,7 +770,8 @@ namespace swift {
767770
DetailedPreprocessingRecord,
768771
ImportForwardDeclarations,
769772
DisableSwiftBridgeAttr,
770-
DisableOverlayModules);
773+
DisableOverlayModules,
774+
EnableClangSPI);
771775
}
772776
};
773777

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ def disable_testable_attr_requires_testable_module :
138138
Flag<["-"], "disable-testable-attr-requires-testable-module">,
139139
HelpText<"Disable checking of @testable">;
140140

141+
def enable_clang_spi :
142+
Flag<["-"], "enable-clang-spi">,
143+
HelpText<"Import Clang SPIs as Swift SPIs">;
144+
141145
def enable_target_os_checking :
142146
Flag<["-"], "enable-target-os-checking">,
143147
HelpText<"Enable checking the target OS of serialized modules">;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,7 @@ ClangImporter::Implementation::Implementation(
21972197
DisableSwiftBridgeAttr(ctx.ClangImporterOpts.DisableSwiftBridgeAttr),
21982198
BridgingHeaderExplicitlyRequested(!ctx.ClangImporterOpts.BridgingHeader.empty()),
21992199
DisableOverlayModules(ctx.ClangImporterOpts.DisableOverlayModules),
2200+
EnableClangSPI(ctx.ClangImporterOpts.EnableClangSPI),
22002201
IsReadingBridgingPCH(false),
22012202
CurrentVersion(ImportNameVersion::fromOptions(ctx.LangOpts)),
22022203
BridgingHeaderLookupTable(new SwiftLookupTable(nullptr)),

lib/ClangImporter/ImportDecl.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8711,15 +8711,17 @@ void ClangImporter::Implementation::importAttributes(
87118711
AnyUnavailable = true;
87128712
}
87138713

8714-
if (isUsingMacroName(getClangASTContext().getSourceManager(),
8715-
avail->getLoc(), "SPI_AVAILABLE") ||
8716-
isUsingMacroName(getClangASTContext().getSourceManager(),
8717-
avail->getLoc(), "__SPI_AVAILABLE")) {
8718-
// The decl has been marked as SPI in the header by using the SPI macro,
8719-
// thus we add the SPI attribute to it with a default group name.
8720-
MappedDecl->getAttrs().add(SPIAccessControlAttr::create(SwiftContext,
8721-
SourceLoc(), SourceRange(),
8722-
SwiftContext.getIdentifier(CLANG_MODULE_DEFUALT_SPI_GROUP_NAME)));
8714+
if (EnableClangSPI) {
8715+
if (isUsingMacroName(getClangASTContext().getSourceManager(),
8716+
avail->getLoc(), "SPI_AVAILABLE") ||
8717+
isUsingMacroName(getClangASTContext().getSourceManager(),
8718+
avail->getLoc(), "__SPI_AVAILABLE")) {
8719+
// The decl has been marked as SPI in the header by using the SPI macro,
8720+
// thus we add the SPI attribute to it with a default group name.
8721+
MappedDecl->getAttrs().add(SPIAccessControlAttr::create(SwiftContext,
8722+
SourceLoc(), SourceRange(),
8723+
SwiftContext.getIdentifier(CLANG_MODULE_DEFUALT_SPI_GROUP_NAME)));
8724+
}
87238725
}
87248726

87258727
StringRef message = avail->getMessage();

lib/ClangImporter/ImporterImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
337337
const bool DisableSwiftBridgeAttr;
338338
const bool BridgingHeaderExplicitlyRequested;
339339
const bool DisableOverlayModules;
340+
const bool EnableClangSPI;
340341

341342
bool IsReadingBridgingPCH;
342343
llvm::SmallVector<clang::serialization::SubmoduleID, 2> PCHImportedSubmodules;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,8 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts,
10521052

10531053
Opts.DisableOverlayModules |= Args.hasArg(OPT_emit_imported_modules);
10541054

1055+
Opts.EnableClangSPI |= Args.hasArg(OPT_enable_clang_spi);
1056+
10551057
Opts.ExtraArgsOnly |= Args.hasArg(OPT_extra_clang_options_only);
10561058

10571059
if (const Arg *A = Args.getLastArg(OPT_pch_output_dir)) {

test/ClangImporter/availability_spi_as_unavailable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// REQUIRES: OS=macosx
2-
// RUN: %target-swift-frontend -typecheck %s -F %S/Inputs/frameworks -verify -DNOT_UNDERLYING
3-
// RUN: %target-swift-frontend -typecheck %s -F %S/Inputs/frameworks -module-name SPIContainer -import-underlying-module -verify
2+
// RUN: %target-swift-frontend -typecheck %s -F %S/Inputs/frameworks -enable-clang-spi -verify -DNOT_UNDERLYING
3+
// RUN: %target-swift-frontend -typecheck %s -F %S/Inputs/frameworks -module-name SPIContainer -import-underlying-module -enable-clang-spi -verify
44

55
#if NOT_UNDERLYING
66
import SPIContainer

test/ClangImporter/availability_spi_as_unavailable_bridging_header.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: OS=macosx
2-
// RUN: %target-swift-frontend -typecheck %s -import-objc-header %S/Inputs/frameworks/SPIContainer.framework/Headers/SPIContainer.h -verify
2+
// RUN: %target-swift-frontend -typecheck %s -import-objc-header %S/Inputs/frameworks/SPIContainer.framework/Headers/SPIContainer.h -enable-clang-spi -verify
33

44

55
@_spi(a) public let a: SPIInterface1

0 commit comments

Comments
 (0)