Skip to content

Commit 7afb475

Browse files
authored
Merge pull request #38682 from bdash/cdecl-extern-C
Prefix @_cdecl functions with `extern "C"` in -Swift.h headers
2 parents 2c993d0 + 7a64bf7 commit 7afb475

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

lib/PrintAsObjC/DeclAndTypePrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,8 @@ class DeclAndTypePrinter::Implementation
728728
auto resultTy = getForeignResultType(
729729
FD, funcTy, asyncConvention, errorConvention);
730730

731+
os << "SWIFT_EXTERN ";
732+
731733
// The result type may be a partial function type we need to close
732734
// up later.
733735
PrintMultiPartType multiPart(*this);

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,13 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
243243
"#if !defined(IBSegueAction)\n"
244244
"# define IBSegueAction\n"
245245
"#endif\n"
246+
"#if !defined(SWIFT_EXTERN)\n"
247+
"# if defined(__cplusplus)\n"
248+
"# define SWIFT_EXTERN extern \"C\"\n"
249+
"# else\n"
250+
"# define SWIFT_EXTERN extern\n"
251+
"# endif\n"
252+
"#endif\n"
246253
;
247254
static_assert(SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4,
248255
"need to add SIMD typedefs here if max elements is increased");

test/PrintAsObjC/cdecl.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,51 @@
88
// REQUIRES: objc_interop
99

1010
// CHECK: /// What a nightmare!
11-
// CHECK-LABEL: double (^ _Nonnull block_nightmare(SWIFT_NOESCAPE float (^ _Nonnull x)(NSInteger)))(char) SWIFT_WARN_UNUSED_RESULT;
11+
// CHECK-LABEL: SWIFT_EXTERN double (^ _Nonnull block_nightmare(SWIFT_NOESCAPE float (^ _Nonnull x)(NSInteger)))(char) SWIFT_WARN_UNUSED_RESULT;
1212

1313
/// What a nightmare!
1414
@_cdecl("block_nightmare")
1515
public func block_nightmare(x: @convention(block) (Int) -> Float)
1616
-> @convention(block) (CChar) -> Double { return { _ in 0 } }
1717

18-
// CHECK-LABEL: double (^ _Nonnull block_recurring_nightmare(float (^ _Nonnull x)(SWIFT_NOESCAPE NSInteger (^ _Nonnull)(double))))(SWIFT_NOESCAPE char (^ _Nonnull)(unsigned char)) SWIFT_WARN_UNUSED_RESULT;
18+
// CHECK-LABEL: SWIFT_EXTERN double (^ _Nonnull block_recurring_nightmare(float (^ _Nonnull x)(SWIFT_NOESCAPE NSInteger (^ _Nonnull)(double))))(SWIFT_NOESCAPE char (^ _Nonnull)(unsigned char)) SWIFT_WARN_UNUSED_RESULT;
1919
@_cdecl("block_recurring_nightmare")
2020
public func block_recurring_nightmare(x: @escaping @convention(block) (@convention(block) (Double) -> Int) -> Float)
2121
-> @convention(block) (_ asdfasdf: @convention(block) (CUnsignedChar) -> CChar) -> Double {
2222
fatalError()
2323
}
2424

25-
// CHECK-LABEL: void foo_bar(NSInteger x, NSInteger y);
25+
// CHECK-LABEL: SWIFT_EXTERN void foo_bar(NSInteger x, NSInteger y);
2626
@_cdecl("foo_bar")
2727
func foo(x: Int, bar y: Int) {}
2828

29-
// CHECK-LABEL: double (* _Nonnull function_pointer_nightmare(SWIFT_NOESCAPE float (* _Nonnull x)(NSInteger)))(char) SWIFT_WARN_UNUSED_RESULT;
29+
// CHECK-LABEL: SWIFT_EXTERN double (* _Nonnull function_pointer_nightmare(SWIFT_NOESCAPE float (* _Nonnull x)(NSInteger)))(char) SWIFT_WARN_UNUSED_RESULT;
3030
@_cdecl("function_pointer_nightmare")
3131
func function_pointer_nightmare(x: @convention(c) (Int) -> Float)
3232
-> @convention(c) (CChar) -> Double { return { _ in 0 } }
3333

34-
// CHECK-LABEL: double (* _Nonnull function_pointer_recurring_nightmare(float (* _Nonnull x)(SWIFT_NOESCAPE NSInteger (* _Nonnull)(double))))(SWIFT_NOESCAPE char (* _Nonnull)(unsigned char)) SWIFT_WARN_UNUSED_RESULT;
34+
// CHECK-LABEL: SWIFT_EXTERN double (* _Nonnull function_pointer_recurring_nightmare(float (* _Nonnull x)(SWIFT_NOESCAPE NSInteger (* _Nonnull)(double))))(SWIFT_NOESCAPE char (* _Nonnull)(unsigned char)) SWIFT_WARN_UNUSED_RESULT;
3535
@_cdecl("function_pointer_recurring_nightmare")
3636
public func function_pointer_recurring_nightmare(x: @escaping @convention(c) (@convention(c) (Double) -> Int) -> Float)
3737
-> @convention(c) (@convention(c) (CUnsignedChar) -> CChar) -> Double {
3838
fatalError()
3939
}
4040

41-
// CHECK-LABEL: void has_keyword_arg_names(NSInteger auto_, NSInteger union_);
41+
// CHECK-LABEL: SWIFT_EXTERN void has_keyword_arg_names(NSInteger auto_, NSInteger union_);
4242
@_cdecl("has_keyword_arg_names")
4343
func keywordArgNames(auto: Int, union: Int) {}
4444

4545
@objc
4646
class C {}
4747

48-
// CHECK-LABEL: C * _Null_unspecified return_iuo(void) SWIFT_WARN_UNUSED_RESULT;
48+
// CHECK-LABEL: SWIFT_EXTERN C * _Null_unspecified return_iuo(void) SWIFT_WARN_UNUSED_RESULT;
4949
@_cdecl("return_iuo")
5050
func returnIUO() -> C! { return C() }
5151

52-
// CHECK-LABEL: void return_never(void) SWIFT_NORETURN;
52+
// CHECK-LABEL: SWIFT_EXTERN void return_never(void) SWIFT_NORETURN;
5353
@_cdecl("return_never")
5454
func returnNever() -> Never { fatalError() }
5555

56-
// CHECK-LABEL: void takes_iuo(C * _Null_unspecified c);
56+
// CHECK-LABEL: SWIFT_EXTERN void takes_iuo(C * _Null_unspecified c);
5757
@_cdecl("takes_iuo")
5858
func takesIUO(c: C!) {}

0 commit comments

Comments
 (0)