Skip to content

Commit

Permalink
[clang] Fix unexpected warnings after a01307a (#75591)
Browse files Browse the repository at this point in the history
a01307a broke silencing of -Wmissing-field-initializers warnings in C
for nested designators. This fixes the issue.
  • Loading branch information
Fznamznon committed Dec 15, 2023
1 parent 07a6d73 commit 32d5221
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions clang/lib/Sema/SemaInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,17 @@ InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity,
WarnIfMissingField &=
SemaRef.getLangOpts().CPlusPlus || !hasAnyDesignatedInits(SForm);

if (OuterILE) {
// When nested designators are present, there might be two nested init
// lists created and only outer will contain designated initializer
// expression, so check outer list as well.
InitListExpr *OuterSForm = OuterILE->isSyntacticForm()
? OuterILE
: OuterILE->getSyntacticForm();
WarnIfMissingField &= SemaRef.getLangOpts().CPlusPlus ||
!hasAnyDesignatedInits(OuterSForm);
}

unsigned NumElems = numStructUnionElements(ILE->getType());
if (!RDecl->isUnion() && RDecl->hasFlexibleArrayMember())
++NumElems;
Expand Down
23 changes: 23 additions & 0 deletions clang/test/Sema/missing-field-initializers.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,26 @@ struct S {
// f1, now we no longer issue that warning (note, this code is still unsafe
// because of the buffer overrun).
struct S s = {1, {1, 2}};

struct S1 {
long int l;
struct { int a, b; } d1;
};

struct S1 s01 = { 1, {1} }; // expected-warning {{missing field 'b' initializer}}
struct S1 s02 = { .d1.a = 1 }; // designator avoids MFI warning

union U1 {
long int l;
struct { int a, b; } d1;
};

union U1 u01 = { 1 };
union U1 u02 = { .d1.a = 1 }; // designator avoids MFI warning

struct S2 {
long int l;
struct { int a, b; struct {int c; } d2; } d1;
};

struct S2 s22 = { .d1.d2.c = 1 }; // designator avoids MFI warning

0 comments on commit 32d5221

Please sign in to comment.