Skip to content

ClangImporter: don't import clang SPI attributes by default #39488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,9 @@ namespace swift {
/// When set, don't look for or load overlays.
bool DisableOverlayModules = false;

/// When set, import SPI_AVAILABLE symbols with Swift SPI attribtues.
bool EnableClangSPI = false;

/// When set, don't enforce warnings with -Werror.
bool DebuggerSupport = false;

Expand Down Expand Up @@ -767,7 +770,8 @@ namespace swift {
DetailedPreprocessingRecord,
ImportForwardDeclarations,
DisableSwiftBridgeAttr,
DisableOverlayModules);
DisableOverlayModules,
EnableClangSPI);
}
};

Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ def disable_testable_attr_requires_testable_module :
Flag<["-"], "disable-testable-attr-requires-testable-module">,
HelpText<"Disable checking of @testable">;

def enable_clang_spi :
Flag<["-"], "enable-clang-spi">,
HelpText<"Import Clang SPIs as Swift SPIs">;

def enable_target_os_checking :
Flag<["-"], "enable-target-os-checking">,
HelpText<"Enable checking the target OS of serialized modules">;
Expand Down
1 change: 1 addition & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,7 @@ ClangImporter::Implementation::Implementation(
DisableSwiftBridgeAttr(ctx.ClangImporterOpts.DisableSwiftBridgeAttr),
BridgingHeaderExplicitlyRequested(!ctx.ClangImporterOpts.BridgingHeader.empty()),
DisableOverlayModules(ctx.ClangImporterOpts.DisableOverlayModules),
EnableClangSPI(ctx.ClangImporterOpts.EnableClangSPI),
IsReadingBridgingPCH(false),
CurrentVersion(ImportNameVersion::fromOptions(ctx.LangOpts)),
BridgingHeaderLookupTable(new SwiftLookupTable(nullptr)),
Expand Down
20 changes: 11 additions & 9 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8711,15 +8711,17 @@ void ClangImporter::Implementation::importAttributes(
AnyUnavailable = true;
}

if (isUsingMacroName(getClangASTContext().getSourceManager(),
avail->getLoc(), "SPI_AVAILABLE") ||
isUsingMacroName(getClangASTContext().getSourceManager(),
avail->getLoc(), "__SPI_AVAILABLE")) {
// The decl has been marked as SPI in the header by using the SPI macro,
// thus we add the SPI attribute to it with a default group name.
MappedDecl->getAttrs().add(SPIAccessControlAttr::create(SwiftContext,
SourceLoc(), SourceRange(),
SwiftContext.getIdentifier(CLANG_MODULE_DEFUALT_SPI_GROUP_NAME)));
if (EnableClangSPI) {
if (isUsingMacroName(getClangASTContext().getSourceManager(),
avail->getLoc(), "SPI_AVAILABLE") ||
isUsingMacroName(getClangASTContext().getSourceManager(),
avail->getLoc(), "__SPI_AVAILABLE")) {
// The decl has been marked as SPI in the header by using the SPI macro,
// thus we add the SPI attribute to it with a default group name.
MappedDecl->getAttrs().add(SPIAccessControlAttr::create(SwiftContext,
SourceLoc(), SourceRange(),
SwiftContext.getIdentifier(CLANG_MODULE_DEFUALT_SPI_GROUP_NAME)));
}
}

StringRef message = avail->getMessage();
Expand Down
1 change: 1 addition & 0 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
const bool DisableSwiftBridgeAttr;
const bool BridgingHeaderExplicitlyRequested;
const bool DisableOverlayModules;
const bool EnableClangSPI;

bool IsReadingBridgingPCH;
llvm::SmallVector<clang::serialization::SubmoduleID, 2> PCHImportedSubmodules;
Expand Down
2 changes: 2 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,8 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts,

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

Opts.EnableClangSPI |= Args.hasArg(OPT_enable_clang_spi);

Opts.ExtraArgsOnly |= Args.hasArg(OPT_extra_clang_options_only);

if (const Arg *A = Args.getLastArg(OPT_pch_output_dir)) {
Expand Down
4 changes: 2 additions & 2 deletions test/ClangImporter/availability_spi_as_unavailable.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// REQUIRES: OS=macosx
// RUN: %target-swift-frontend -typecheck %s -F %S/Inputs/frameworks -verify -DNOT_UNDERLYING
// RUN: %target-swift-frontend -typecheck %s -F %S/Inputs/frameworks -module-name SPIContainer -import-underlying-module -verify
// RUN: %target-swift-frontend -typecheck %s -F %S/Inputs/frameworks -enable-clang-spi -verify -DNOT_UNDERLYING
// RUN: %target-swift-frontend -typecheck %s -F %S/Inputs/frameworks -module-name SPIContainer -import-underlying-module -enable-clang-spi -verify

#if NOT_UNDERLYING
import SPIContainer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// REQUIRES: OS=macosx
// RUN: %target-swift-frontend -typecheck %s -import-objc-header %S/Inputs/frameworks/SPIContainer.framework/Headers/SPIContainer.h -verify
// RUN: %target-swift-frontend -typecheck %s -import-objc-header %S/Inputs/frameworks/SPIContainer.framework/Headers/SPIContainer.h -enable-clang-spi -verify


@_spi(a) public let a: SPIInterface1
Expand Down