Skip to content

Add a hidden flag to disable the swift bridge attribute #1932

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
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
3 changes: 3 additions & 0 deletions include/swift/ClangImporter/ClangImporterOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class ClangImporterOptions {
/// member of some type instead. This includes inits, computed properties,
/// and methods.
bool InferImportAsMember = false;

/// If true ignore the swift bridged attribute.
bool DisableSwiftBridgeAttr = false;
};

} // end namespace swift
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ def solver_memory_threshold : Separate<["-"], "solver-memory-threshold">,
Flags<[FrontendOption, HelpHidden, DoesNotAffectIncrementalBuild]>,
HelpText<"Set the upper bound for memory consumption, in bytes, by the constraint solver">;

def disable_swift_bridge_attr : Flag<["-"], "disable-swift-bridge-attr">,
Flags<[FrontendOption, HelpHidden]>,
HelpText<"Disable using the swift bridge attribute">;

// Diagnostic control options
def suppress_warnings : Flag<["-"], "suppress-warnings">,
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
Expand Down
10 changes: 8 additions & 2 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ ClangImporter::Implementation::Implementation(ASTContext &ctx,
: SwiftContext(ctx),
ImportForwardDeclarations(opts.ImportForwardDeclarations),
InferImportAsMember(opts.InferImportAsMember),
DisableSwiftBridgeAttr(opts.DisableSwiftBridgeAttr),
BridgingHeaderLookupTable(nullptr)
{
// Add filters to determine if a Clang availability attribute
Expand Down Expand Up @@ -1957,7 +1958,11 @@ namespace {
/// Determine whether the given Objective-C class, or any of its
/// superclasses, either has or inherits a swift_bridge attribute.
static bool hasOrInheritsSwiftBridgeAttr(
const clang::ObjCInterfaceDecl *objcClass) {
const clang::ObjCInterfaceDecl *objcClass,
bool DisableSwiftBridgeAttr) {
// If we have disabled the attribute just return that there is none.
if (DisableSwiftBridgeAttr)
return false;
do {
// Look at the definition, if there is one.
if (auto def = objcClass->getDefinition())
Expand Down Expand Up @@ -2481,7 +2486,8 @@ auto ClangImporter::Implementation::importFullName(
if (D->getDeclContext()->getRedeclContext()->isFileContext() &&
(isa<clang::TypeDecl>(D) ||
(isa<clang::ObjCInterfaceDecl>(D) &&
!hasOrInheritsSwiftBridgeAttr(cast<clang::ObjCInterfaceDecl>(D))) ||
!hasOrInheritsSwiftBridgeAttr(cast<clang::ObjCInterfaceDecl>(D),
DisableSwiftBridgeAttr)) ||
isa<clang::ObjCProtocolDecl>(D))) {
// Find the original declaration, from which we can determine
// the owning module.
Expand Down
2 changes: 2 additions & 0 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,8 @@ namespace {
/// Map the Clang swift_bridge attribute to a specific type.
Type mapSwiftBridgeAttr(const clang::NamedDecl *clangDecl) {
// Check whether there is a swift_bridge attribute.
if (Impl.DisableSwiftBridgeAttr)
return Type();
auto bridgeAttr = clangDecl->getAttr<clang::SwiftBridgeAttr>();
if (!bridgeAttr) return Type();

Expand Down
1 change: 1 addition & 0 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation

const bool ImportForwardDeclarations;
const bool InferImportAsMember;
const bool DisableSwiftBridgeAttr;

constexpr static const char * const moduleImportBufferName =
"<swift-imported-modules>";
Expand Down
2 changes: 2 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,8 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts,
if (Args.hasArg(OPT_embed_bitcode))
Opts.Mode = ClangImporterOptions::Modes::EmbedBitcode;

Opts.DisableSwiftBridgeAttr |= Args.hasArg(OPT_disable_swift_bridge_attr);

return false;
}

Expand Down
18 changes: 18 additions & 0 deletions test/SILGen/objc_disable_brigding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: rm -rf %t && mkdir -p %t
// RUN: %build-silgen-test-overlays
// RUN: %target-swift-frontend -emit-module -o %t -sdk %S/Inputs -I %S/../Inputs/ObjCBridging %S/../Inputs/ObjCBridging/Appliances.swift -I %t
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/Inputs -I %t -I %S/../Inputs/ObjCBridging) -disable-swift-bridge-attr -Xllvm -sil-full-demangle -emit-silgen %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%target-cpu --check-prefix=CHECK-%target-os-%target-cpu

// REQUIRES: objc_interop

import Foundation
import Appliances

// This tests the -disable-swift-bridge-attr flag. Make sure we don't emit bridging code.

// CHECK-LABEL: sil hidden @{{.*}}objc_disable_brigding16updateFridgeTemp
func updateFridgeTemp(home: APPHouse, delta: Double) {
// CHECK-NOT: function_ref @{{.*}}BridgeFromObjectiveC
home.fridge.temperature += delta
// CHECK: return
}