[clang] 32d5221 - [clang] Fix unexpected warnings after a01307a (#75591)

via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 15 06:25:06 PST 2023


Author: Mariya Podchishchaeva
Date: 2023-12-15T15:25:01+01:00
New Revision: 32d5221ec4810dd723ccebaabbda1df5d3b4cfcf

URL: https://github.com/llvm/llvm-project/commit/32d5221ec4810dd723ccebaabbda1df5d3b4cfcf
DIFF: https://github.com/llvm/llvm-project/commit/32d5221ec4810dd723ccebaabbda1df5d3b4cfcf.diff

LOG: [clang] Fix unexpected warnings after a01307a (#75591)

a01307a broke silencing of -Wmissing-field-initializers warnings in C
for nested designators. This fixes the issue.

Added: 
    

Modified: 
    clang/lib/Sema/SemaInit.cpp
    clang/test/Sema/missing-field-initializers.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index de0d92edb550dd..035eaae58965a4 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -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;

diff  --git a/clang/test/Sema/missing-field-initializers.c b/clang/test/Sema/missing-field-initializers.c
index 8653591ff1187a..8dc8288ad92e6c 100644
--- a/clang/test/Sema/missing-field-initializers.c
+++ b/clang/test/Sema/missing-field-initializers.c
@@ -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


        


More information about the cfe-commits mailing list