[clang] [clang] Fix unexpected warnings after a01307a (PR #75591)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 15 04:17:18 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Mariya Podchishchaeva (Fznamznon)
<details>
<summary>Changes</summary>
a01307a broke silencing of -Wmissing-field-initializers warnings in C for nested designators. This fixes the issue.
---
Full diff: https://github.com/llvm/llvm-project/pull/75591.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaInit.cpp (+8)
- (modified) clang/test/Sema/missing-field-initializers.c (+23)
``````````diff
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index de0d92edb550dd..1cd2198503abaf 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -864,6 +864,14 @@ InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity,
WarnIfMissingField &=
SemaRef.getLangOpts().CPlusPlus || !hasAnyDesignatedInits(SForm);
+ if (OuterILE) {
+ 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
``````````
</details>
https://github.com/llvm/llvm-project/pull/75591
More information about the cfe-commits
mailing list