From b1d63d0938a401ad83d8121c03ab9cba3598ac1a Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Mon, 30 Oct 2023 14:17:56 -0700 Subject: [PATCH] [interop][SwiftToCxx] Use 'SWIFT_INLINE_PRIVATE_HELPER' for getTypeMetadata helper to avoid emitting a reference to it when 'DEBUG' macro is set This is needed as the metadata for C++ types might not be actually emitted and thus we can't always reference it Fixes https://github.com/apple/swift/issues/69234 rdar://117089662 --- lib/PrintAsClang/PrintClangValueType.cpp | 2 +- .../bridge-cxx-struct-back-to-cxx.swift | 4 +- .../link-cxx-type-metadata-accessor.swift | 42 +++++++++++++++++++ .../SwiftToCxx/class/swift-class-in-cxx.swift | 2 +- .../generics/generic-struct-in-cxx.swift | 2 +- .../structs/resilient-struct-in-cxx.swift | 2 +- .../structs/swift-struct-in-cxx.swift | 2 +- 7 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 test/Interop/CxxToSwiftToCxx/link-cxx-type-metadata-accessor.swift diff --git a/lib/PrintAsClang/PrintClangValueType.cpp b/lib/PrintAsClang/PrintClangValueType.cpp index 37a1433ba4b3c..9eef4577123b3 100644 --- a/lib/PrintAsClang/PrintClangValueType.cpp +++ b/lib/PrintAsClang/PrintClangValueType.cpp @@ -651,7 +651,7 @@ void ClangValueTypePrinter::printTypeGenericTraits( } os << "> {\n"; os << " static "; - ClangSyntaxPrinter(os).printInlineForThunk(); + ClangSyntaxPrinter(os).printInlineForHelperFunction(); os << "void * _Nonnull getTypeMetadata() {\n"; os << " return "; if (!typeDecl->hasClangNode()) { diff --git a/test/Interop/CxxToSwiftToCxx/bridge-cxx-struct-back-to-cxx.swift b/test/Interop/CxxToSwiftToCxx/bridge-cxx-struct-back-to-cxx.swift index 5e51447369848..7aa284a9aed77 100644 --- a/test/Interop/CxxToSwiftToCxx/bridge-cxx-struct-back-to-cxx.swift +++ b/test/Interop/CxxToSwiftToCxx/bridge-cxx-struct-back-to-cxx.swift @@ -204,7 +204,7 @@ public struct Strct { // CHECK-NEXT: static inline const constexpr bool isUsableInGenericContext = true; // CHECK-NEXT: template<> // CHECK-NEXT: struct TypeMetadataTrait { -// CHECK-NEXT: static SWIFT_INLINE_THUNK void * _Nonnull getTypeMetadata() { +// CHECK-NEXT: static SWIFT_INLINE_PRIVATE_HELPER void * _Nonnull getTypeMetadata() { // CHECK-NEXT: return _impl::$sSo2nsO0030NonTrivialTemplateCInt_hHAFhrbVMa(0)._0; // CHECK-NEXT: } // CHECK-NEXT: }; @@ -243,7 +243,7 @@ public struct Strct { // CHECK-NEXT: static inline const constexpr bool isUsableInGenericContext = true; // CHECK-NEXT: template<> // CHECK-NEXT: struct TypeMetadataTrait { -// CHECK-NEXT: static SWIFT_INLINE_THUNK void * _Nonnull getTypeMetadata() { +// CHECK-NEXT: static SWIFT_INLINE_PRIVATE_HELPER void * _Nonnull getTypeMetadata() { // CHECK-NEXT: return _impl::$sSo2nsO0037NonTrivialTemplateTrivialinNS_CsGGkdcVMa(0)._0; // CHECK-NEXT: } // CHECK-NEXT: }; diff --git a/test/Interop/CxxToSwiftToCxx/link-cxx-type-metadata-accessor.swift b/test/Interop/CxxToSwiftToCxx/link-cxx-type-metadata-accessor.swift new file mode 100644 index 0000000000000..65add3ee14171 --- /dev/null +++ b/test/Interop/CxxToSwiftToCxx/link-cxx-type-metadata-accessor.swift @@ -0,0 +1,42 @@ +// RUN: %empty-directory(%t) +// RUN: split-file %s %t + +// RUN: %target-swift-frontend -typecheck %t/use-cxx-types.swift -typecheck -module-name UseCxx -emit-clang-header-path %t/UseCxx.h -I %t -enable-experimental-cxx-interop -clang-header-expose-decls=all-public + +// Use the 'DEBUG' flug to force llvm used +// RUN: %target-interop-build-clangxx -std=c++20 -c %t/use-swift-cxx-types.cpp -I %t -o %t/swift-cxx-execution.o -DDEBUG +// RUN: %target-interop-build-swift %t/use-cxx-types.swift -o %t/swift-cxx-execution -Xlinker %t/swift-cxx-execution.o -module-name UseCxx -Xfrontend -entry-point-function-name -Xfrontend swiftMain -I %t + +//--- header.h + +struct CxxStruct { + inline CxxStruct(int x) : x(x) {} + inline CxxStruct(const CxxStruct &other) : x(other.x) {} + inline ~CxxStruct() {} + + int x; +}; + +//--- module.modulemap +module CxxTest { + header "header.h" + requires cplusplus +} + +//--- use-cxx-types.swift +import CxxTest + +public func retCxxStruct() -> CxxStruct { + return CxxStruct(2) +} + +//--- use-swift-cxx-types.cpp + +#include "header.h" +#include "UseCxx.h" +#include + +int main() { + auto x = UseCxx::retCxxStruct(); + return 0; +} diff --git a/test/Interop/SwiftToCxx/class/swift-class-in-cxx.swift b/test/Interop/SwiftToCxx/class/swift-class-in-cxx.swift index fce9fb9f314d6..f124903c1b0fd 100644 --- a/test/Interop/SwiftToCxx/class/swift-class-in-cxx.swift +++ b/test/Interop/SwiftToCxx/class/swift-class-in-cxx.swift @@ -89,7 +89,7 @@ public final class ClassWithIntField { // CHECK-NEXT: #pragma clang diagnostic ignored "-Wc++17-extensions" // CHECK-NEXT: template<> // CHECK-NEXT: struct TypeMetadataTrait { -// CHECK-NEXT: SWIFT_INLINE_THUNK void * _Nonnull getTypeMetadata() { +// CHECK-NEXT: SWIFT_INLINE_PRIVATE_HELPER void * _Nonnull getTypeMetadata() { // CHECK-NEXT: return Class::_impl::$s5Class0A12WithIntFieldCMa(0)._0; // CHECK-NEXT: } // CHECK-NEXT: }; diff --git a/test/Interop/SwiftToCxx/generics/generic-struct-in-cxx.swift b/test/Interop/SwiftToCxx/generics/generic-struct-in-cxx.swift index 0bf941623af99..c1800dbf5706b 100644 --- a/test/Interop/SwiftToCxx/generics/generic-struct-in-cxx.swift +++ b/test/Interop/SwiftToCxx/generics/generic-struct-in-cxx.swift @@ -275,7 +275,7 @@ public func inoutConcretePair(_ x: UInt16, _ y: inout GenericPair && swift::isUsableInGenericContext // CHECK-NEXT: #endif // CHECK-NEXT: struct TypeMetadataTrait> { -// CHECK-NEXT: static SWIFT_INLINE_THUNK void * _Nonnull getTypeMetadata() { +// CHECK-NEXT: static SWIFT_INLINE_PRIVATE_HELPER void * _Nonnull getTypeMetadata() { // CHECK-NEXT: return Generics::_impl::$s8Generics11GenericPairVMa(0, swift::TypeMetadataTrait::getTypeMetadata(), swift::TypeMetadataTrait::getTypeMetadata())._0; // CHECK-NEXT: } // CHECK-NEXT: }; diff --git a/test/Interop/SwiftToCxx/structs/resilient-struct-in-cxx.swift b/test/Interop/SwiftToCxx/structs/resilient-struct-in-cxx.swift index 8e1bc8aed4aa6..7b9ee340652e6 100644 --- a/test/Interop/SwiftToCxx/structs/resilient-struct-in-cxx.swift +++ b/test/Interop/SwiftToCxx/structs/resilient-struct-in-cxx.swift @@ -85,7 +85,7 @@ public struct FirstSmallStruct { // CHECK-NEXT: #pragma clang diagnostic ignored "-Wc++17-extensions" // CHECK-NEXT: template<> // CHECK-NEXT: struct TypeMetadataTrait { -// CHECK-NEXT: SWIFT_INLINE_THUNK void * _Nonnull getTypeMetadata() { +// CHECK-NEXT: SWIFT_INLINE_PRIVATE_HELPER void * _Nonnull getTypeMetadata() { // CHECK-NEXT: return Structs::_impl::$s7Structs16FirstSmallStructVMa(0)._0; // CHECK-NEXT: } // CHECK-NEXT: }; diff --git a/test/Interop/SwiftToCxx/structs/swift-struct-in-cxx.swift b/test/Interop/SwiftToCxx/structs/swift-struct-in-cxx.swift index 5bb67a1b4e06e..1081a75c970ef 100644 --- a/test/Interop/SwiftToCxx/structs/swift-struct-in-cxx.swift +++ b/test/Interop/SwiftToCxx/structs/swift-struct-in-cxx.swift @@ -106,7 +106,7 @@ // CHECK-NEXT: #pragma clang diagnostic ignored "-Wc++17-extensions" // CHECK-NEXT: template<> // CHECK-NEXT: struct TypeMetadataTrait -// CHECK-NEXT: SWIFT_INLINE_THUNK void * _Nonnull getTypeMetadata() { +// CHECK-NEXT: SWIFT_INLINE_PRIVATE_HELPER void * _Nonnull getTypeMetadata() { // CHECK-NEXT: return Structs::_impl::$s7Structs18StructWithIntFieldVMa(0)._0; // CHECK-NEXT: } // CHECK-NEXT: };