Skip to content

Commit 32d5221

Browse files
authored
[clang] Fix unexpected warnings after a01307a (#75591)
a01307a broke silencing of -Wmissing-field-initializers warnings in C for nested designators. This fixes the issue.
1 parent 07a6d73 commit 32d5221

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

clang/lib/Sema/SemaInit.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,17 @@ InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity,
864864
WarnIfMissingField &=
865865
SemaRef.getLangOpts().CPlusPlus || !hasAnyDesignatedInits(SForm);
866866

867+
if (OuterILE) {
868+
// When nested designators are present, there might be two nested init
869+
// lists created and only outer will contain designated initializer
870+
// expression, so check outer list as well.
871+
InitListExpr *OuterSForm = OuterILE->isSyntacticForm()
872+
? OuterILE
873+
: OuterILE->getSyntacticForm();
874+
WarnIfMissingField &= SemaRef.getLangOpts().CPlusPlus ||
875+
!hasAnyDesignatedInits(OuterSForm);
876+
}
877+
867878
unsigned NumElems = numStructUnionElements(ILE->getType());
868879
if (!RDecl->isUnion() && RDecl->hasFlexibleArrayMember())
869880
++NumElems;

clang/test/Sema/missing-field-initializers.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,26 @@ struct S {
6161
// f1, now we no longer issue that warning (note, this code is still unsafe
6262
// because of the buffer overrun).
6363
struct S s = {1, {1, 2}};
64+
65+
struct S1 {
66+
long int l;
67+
struct { int a, b; } d1;
68+
};
69+
70+
struct S1 s01 = { 1, {1} }; // expected-warning {{missing field 'b' initializer}}
71+
struct S1 s02 = { .d1.a = 1 }; // designator avoids MFI warning
72+
73+
union U1 {
74+
long int l;
75+
struct { int a, b; } d1;
76+
};
77+
78+
union U1 u01 = { 1 };
79+
union U1 u02 = { .d1.a = 1 }; // designator avoids MFI warning
80+
81+
struct S2 {
82+
long int l;
83+
struct { int a, b; struct {int c; } d2; } d1;
84+
};
85+
86+
struct S2 s22 = { .d1.d2.c = 1 }; // designator avoids MFI warning

0 commit comments

Comments
 (0)