From 50db8ec9b321cbb3b5efeb73511744c6a32beb82 Mon Sep 17 00:00:00 2001 From: miod Date: Wed, 1 Feb 2023 20:34:10 +0000 Subject: [PATCH] Backport gnu/gcc/gcc/c-typeck.c r1.3 to the gcc 3 code base: Adjust how gcc3 handles the "missing braces around initializer" warning. In c99 any value can be initalised using a { 0 } constructor independent of the type. Now if a struct's first member is another struct then gcc4 issues the above warning but it should not do that. Move the warning check from push_init_level() to pop_init_level() and check if either { 0 } or { } was used. If additional implicit braces were added surpress the warning. Inspired by gcc PR#64709 light testing by me, serious testing by aoyama@ --- gnu/usr.bin/gcc/gcc/c-typeck.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/gnu/usr.bin/gcc/gcc/c-typeck.c b/gnu/usr.bin/gcc/gcc/c-typeck.c index e574845e4d5..78342f51f89 100644 --- a/gnu/usr.bin/gcc/gcc/c-typeck.c +++ b/gnu/usr.bin/gcc/gcc/c-typeck.c @@ -4945,6 +4945,9 @@ static int constructor_erroneous; /* 1 if have called defer_addressed_constants. */ static int constructor_subconstants_deferred; +/* 1 if this constructor is a zero init. */ +static int constructor_zeroinit; + /* Structure for managing pending initializer elements, organized as an AVL tree. */ @@ -5403,12 +5406,6 @@ push_init_level (implicit) set_nonincremental_init (); } - if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned) - { - missing_braces_mentioned = 1; - warning_init ("missing braces around initializer"); - } - if (TREE_CODE (constructor_type) == RECORD_TYPE || TREE_CODE (constructor_type) == UNION_TYPE) { @@ -5534,6 +5531,24 @@ pop_init_level (implicit) abort (); } + if (constructor_elements == 0) + constructor_zeroinit = 1; + else if (TREE_CHAIN (constructor_elements) == 0 && + initializer_zerop (TREE_VALUE (constructor_elements))) + { + constructor_zeroinit = 1; + } + else + constructor_zeroinit = 0; + + /* only warn for missing braces unless it is { 0 } */ + if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned && + !constructor_zeroinit) + { + missing_braces_mentioned = 1; + warning_init ("missing braces around initializer"); + } + /* Warn when some struct elements are implicitly initialized to zero. */ if (extra_warnings && constructor_type -- 2.20.1