mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
When Swift imports C structs, it synthesizes an initializer that takes no arguments and zero-initializes the C struct. When C++ interop is enabled, Clang treats all C structs as if they were C++ structs. This means that some of the C structs will get a default constructor implicitly generated by Clang. This implicit default constructor will not zero-initialize trivial fields of the struct. This is a common source of confusion and subtle bugs for developers who try to enable C++ interop in existing projects that use C interop and rely on zero-initialization of C structs. rdar://115909532
53 lines
2.7 KiB
Swift
53 lines
2.7 KiB
Swift
// Target-specific tests for C++ constructor call code generation.
|
|
|
|
// RUN: %swift -module-name MySwift -target aarch64-unknown-linux-android -dump-clang-diagnostics -I %S/Inputs -enable-experimental-cxx-interop -emit-ir %s -parse-stdlib -parse-as-library -disable-legacy-type-info -Xcc -fignore-exceptions | %FileCheck %s -check-prefix=ITANIUM_ARM
|
|
|
|
// REQUIRES: OS=linux-android
|
|
// REQUIRES: CPU=aarch64
|
|
|
|
import Constructors
|
|
import TypeClassification
|
|
|
|
public func createHasVirtualBase() -> HasVirtualBase {
|
|
// ITANIUM_ARM: define protected swiftcc void @"$s7MySwift20createHasVirtualBaseSo0deF0VyF"(ptr noalias sret({{.*}}) %0)
|
|
// To verify that the thunk is inlined, make sure there's no intervening
|
|
// `define`, i.e. the call to the C++ constructor happens in
|
|
// createHasVirtualBase(), not some later function.
|
|
// ITANIUM_ARM-NOT: define
|
|
// Note `this` return type.
|
|
// ITANIUM_ARM: call void @_ZN14HasVirtualBaseC1E7ArgType(ptr %{{[0-9]+}}, i64 %{{[0-9]+}})
|
|
return HasVirtualBase(ArgType())
|
|
}
|
|
|
|
public func createImplicitDefaultConstructor() -> ImplicitDefaultConstructor {
|
|
// ITANIUM_ARM: define protected swiftcc i32 @"$s7MySwift32createImplicitDefaultConstructorSo0deF0VyF"()
|
|
// ITANIUM_ARM-NOT: define
|
|
// Note `this` return type.
|
|
// ITANIUM_ARM: call void @llvm.memset.p0.i64
|
|
// ITANIUM_ARM: call void @_ZN26ImplicitDefaultConstructorC2Ev(ptr %{{[0-9]+}})
|
|
return ImplicitDefaultConstructor()
|
|
}
|
|
|
|
public func createStructWithSubobjectCopyConstructorAndValue() {
|
|
// ITANIUM_ARM-LABEL: define protected swiftcc void @"$s7MySwift48createStructWithSubobjectCopyConstructorAndValueyyF"()
|
|
// ITANIUM_ARM: [[MEMBER:%.*]] = alloca %TSo33StructWithCopyConstructorAndValueV
|
|
// ITANIUM_ARM: [[OBJ:%.*]] = alloca %TSo42StructWithSubobjectCopyConstructorAndValueV
|
|
// ITANIUM_ARM: [[TMP:%.*]] = alloca %TSo33StructWithCopyConstructorAndValueV
|
|
// ITANIUM_ARM: call void @_ZN33StructWithCopyConstructorAndValueC2Ev(ptr [[MEMBER]])
|
|
// ITANIUM_ARM: call void @_ZN33StructWithCopyConstructorAndValueC2ERKS_(ptr [[TMP]], ptr [[MEMBER]])
|
|
// ITANIUM_ARM: ret void
|
|
let member = StructWithCopyConstructorAndValue()
|
|
let obj = StructWithSubobjectCopyConstructorAndValue(member: member)
|
|
}
|
|
|
|
public func createTemplatedConstructor() {
|
|
// ITANIUM_ARM-LABEL: define protected swiftcc void @"$s7MySwift26createTemplatedConstructoryyF"()
|
|
// ITANIUM_ARM: [[OBJ:%.*]] = alloca %TSo20TemplatedConstructorV
|
|
// ITANIUM_ARM: [[IVAL:%.*]] = load i64, ptr
|
|
// ITANIUM_ARM: call void @_ZN20TemplatedConstructorC2I7ArgTypeEET_(ptr [[OBJ]], i64 [[IVAL]])
|
|
// ITANIUM_ARM: ret void
|
|
|
|
// ITANIUM_ARM-LABEL: define {{.*}}void @_ZN20TemplatedConstructorC2I7ArgTypeEET_(ptr {{.*}}, i64 {{.*}})
|
|
let templated = TemplatedConstructor(ArgType())
|
|
}
|