Files
swift-mirror/test/Interop/Cxx/class/constructors-irgen-windows.swift
Egor Zhdan bac5d0e9a1 [cxx-interop] Zero-initialize C++ structs when calling their default constructors
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
2024-04-16 13:42:02 +01:00

53 lines
2.9 KiB
Swift

// Target-specific tests for C++ constructor call code generation.
// RUN: %swift -module-name MySwift -target x86_64-unknown-windows-msvc %target-swift-flags -dump-clang-diagnostics -I %S/Inputs -enable-experimental-cxx-interop -emit-ir %s -parse-stdlib -parse-as-library -disable-legacy-type-info | %FileCheck %s -check-prefix=MICROSOFT_X64
// REQUIRES: OS=windows-msvc
// REQUIRES: CPU=x86_64
import Constructors
import TypeClassification
public func createHasVirtualBase() -> HasVirtualBase {
// MICROSOFT_X64: define dllexport swiftcc void @"$s7MySwift20createHasVirtualBaseSo0{{bcD0VyF|deF0VyF}}"(ptr noalias sret({{.*}}) [[V0:%.*]])
// MICROSOFT_X64-NOT: define
// Note `this` return type and implicit "most derived" argument.
// MICROSOFT_X64: [[V1:%.*]] = alloca %{{.*}}, align 8
// MICROSOFT_X64: call ptr @"??0HasVirtualBase@@QEAA@UArgType@@@Z"(ptr [[V1]], i32 %{{[0-9]+}}, i32 1)
// MICROSOFT_X64: call ptr @"??0HasVirtualBase@@QEAA@UArgType@@@Z"(ptr [[V0]], i32 %{{[0-9]+}}, i32 1)
let _ : HasVirtualBase = HasVirtualBase(ArgType())
return HasVirtualBase(ArgType())
}
public func createImplicitDefaultConstructor() -> ImplicitDefaultConstructor {
// MICROSOFT_X64: define dllexport swiftcc i32 @"$s7MySwift32createImplicitDefaultConstructorSo0{{bcD0VyF|deF0VyF}}"()
// MICROSOFT_X64-NOT: define
// Note `this` return type but no implicit "most derived" argument.
// MICROSOFT_X64: call void @llvm.memset.p0.i64
// MICROSOFT_X64: call ptr @"??0ImplicitDefaultConstructor@@QEAA@XZ"(ptr %{{[0-9]+}})
return ImplicitDefaultConstructor()
}
public func createStructWithSubobjectCopyConstructorAndValue() {
// MICROSOFT_X64-LABEL: define dllexport swiftcc void @"$s7MySwift48createStructWithSubobjectCopyConstructorAndValueyyF"()
// MICROSOFT_X64: [[MEMBER:%.*]] = alloca %TSo33StructWithCopyConstructorAndValueV
// MICROSOFT_X64: [[OBJ:%.*]] = alloca %TSo42StructWithSubobjectCopyConstructorAndValueV
// MICROSOFT_X64: [[TMP:%.*]] = alloca %TSo33StructWithCopyConstructorAndValueV
// MICROSOFT_X64: call ptr @"??0StructWithCopyConstructorAndValue@@QEAA@XZ"(ptr [[MEMBER]])
// MICROSOFT_X64: call ptr @"??0StructWithCopyConstructorAndValue@@QEAA@AEBU0@@Z"(ptr [[TMP]], ptr [[MEMBER]])
// MICROSOFT_X64: ret void
let member = StructWithCopyConstructorAndValue()
let obj = StructWithSubobjectCopyConstructorAndValue(member: member)
}
public func createTemplatedConstructor() {
// MICROSOFT_X64-LABEL: define dllexport swiftcc void @"$s7MySwift26createTemplatedConstructoryyF"()
// MICROSOFT_X64: [[OBJ:%.*]] = alloca %TSo20TemplatedConstructorV
// MICROSOFT_X64: [[IVAL:%.*]] = load i32, ptr
// MICROSOFT_X64: call ptr @"??$?0UArgType@@@TemplatedConstructor@@QEAA@UArgType@@@Z"(ptr [[OBJ]], i32 [[IVAL]])
// MICROSOFT_X64: ret void
// MICROSOFT_X64-LABEL: define {{.*}}ptr @"??$?0UArgType@@@TemplatedConstructor@@QEAA@UArgType@@@Z"(ptr {{.*}}, i32 {{.*}})
let templated = TemplatedConstructor(ArgType())
}