Skip to content

Commit 40676e4

Browse files
committed
ok to init struct with zero members
1 parent a584bd9 commit 40676e4

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4284,6 +4284,8 @@ bool SemaHLSL::transformInitList(const InitializedEntity &Entity,
42844284
}
42854285
size_t ExpectedSize = ILT.DestTypes.size();
42864286
size_t ActualSize = ILT.ArgExprs.size();
4287+
if (ExpectedSize == 0 && ActualSize == 0)
4288+
return true;
42874289
// For incomplete arrays it is completely arbitrary to choose whether we think
42884290
// the user intended fewer or more elements. This implementation assumes that
42894291
// the user intended more, and errors that there are too few initializers to

clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ struct Unnamed {
5050
int : 8;
5151
};
5252

53+
struct Empty {
54+
};
55+
56+
struct UnnamedOnly {
57+
int : 8;
58+
};
59+
60+
// CHECK-DAG: [[ConstE:@.*]] = private unnamed_addr constant %struct.Empty undef, align 1
61+
// CHECK-DAG: [[ConstUO:@.*]] = private unnamed_addr constant %struct.UnnamedOnly undef, align 1
62+
5363
// Case 1: Extraneous braces get ignored in literal instantiation.
5464
// CHECK-LABEL: define hidden void @_Z5case1v(
5565
// CHECK-SAME: ptr dead_on_unwind noalias writable sret([[STRUCT_TWOFLOATS:%.*]]) align 1 [[AGG_RESULT:%.*]]) #[[ATTR0:[0-9]+]] {
@@ -985,3 +995,35 @@ void case18() {
985995
void case19(Unnamed U) {
986996
TwoInts TI = {U, 1};
987997
}
998+
999+
// InitList with Empty Struct on LHS
1000+
// CHECK-LABEL: case20
1001+
// CHECK: [[E:%.*]] = alloca %struct.Empty, align 1
1002+
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 1 [[E]], ptr align 1 [[ConstE]], i32 1, i1 false)
1003+
void case20() {
1004+
Empty E = {};
1005+
}
1006+
1007+
// InitList with Empty Struct on RHS
1008+
// CHECK-LABEL: case21
1009+
// CHECK: [[TI:%.*]] = alloca %struct.TwoInts, align 1
1010+
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 1 %TI, ptr align 1 {{.*}}, i32 8, i1 false)
1011+
void case21(Empty E) {
1012+
TwoInts TI = {E, 1, 2};
1013+
}
1014+
1015+
// InitList with Struct with only unnamed bitfield on LHS
1016+
// CHECK-LABEL: case22
1017+
// CHECK: [[UO:%.*]] = alloca %struct.UnnamedOnly, align 1
1018+
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 1 [[UO]], ptr align 1 [[ConstUO]], i32 1, i1 false)
1019+
void case22() {
1020+
UnnamedOnly UO = {};
1021+
}
1022+
1023+
// InitList with Struct with only unnamed bitfield on RHS
1024+
// CHECK-LABEL: case23
1025+
// CHECK: [[TI:%.*]] = alloca %struct.TwoInts, align 1
1026+
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 1 [[TI]], ptr align 1 {{.*}}, i32 8, i1 false)
1027+
void case23(UnnamedOnly UO) {
1028+
TwoInts TI = {UO, 1, 2};
1029+
}

0 commit comments

Comments
 (0)