Bug 36750 - -Wmissing-field-initializers relaxation request
Summary: -Wmissing-field-initializers relaxation request
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.1.2
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2008-07-07 15:34 UTC by Pádraig Brady
Modified: 2015-05-12 02:20 UTC (History)
9 users (show)

See Also:
Host: i386-redhat-linux
Target: i386-redhat-linux
Build: i386-redhat-linux
Known to work:
Known to fail:
Last reconfirmed: 2010-11-24 14:18:07


Attachments
Test case showing overly strict warning (85 bytes, text/plain)
2014-09-30 19:17 UTC, Daniel Sommermann
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Pádraig Brady 2008-07-07 15:34:24 UTC
While trying to compile coreutils with -Wextra,
I noticed many warnings due to automatic variables
initialized with { 0, }.

As I understand it, since C90 the above will initialize
[all members of] the type to that used in static scope,
including any unused padding space in the structure.

I.E. the following is valid:

mbstate_t m = { 0, };
int i = { 0, };
struct { int a; int b; } s = { 0, };

It would be great if gcc would relax this warning
when a trailing ',' is specified as that would be clear indication
that one wants to init all [other] elements to 0, and that
we haven't just forgotten some members.

At least the warning should be suppressed for the specific
most common case is where { 0, } is specified.

thanks.
Comment 1 Pádraig Brady 2010-11-24 12:09:33 UTC
A related thread: http://gcc.gnu.org/ml/gcc-bugs/1998-07/msg00031.html
Comment 2 Manuel López-Ibáñez 2010-11-24 14:18:07 UTC
I think this is reasonable. Have you tried what clang does in these cases?

But the mail that you link talks about C++ not C...
Comment 3 Alexander Monakov 2011-04-22 11:53:05 UTC
Author: amonakov
Date: Fri Apr 22 11:53:01 2011
New Revision: 172857

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172857
Log:
	PR c/36750
	* c-typeck.c (pop_init_level): Do not warn about initializing
	with ` = {0}'.

testsuite:
	* gcc.dg/missing-field-init-2.c: Update testcase.


Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-typeck.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/missing-field-init-2.c
Comment 4 Alexander Monakov 2011-04-22 11:56:39 UTC
The above patch suppresses the warning in C regardless of whether the trailing comma is present.  Closing as fixed.
Comment 5 Jackie Rosen 2014-02-16 13:17:29 UTC Comment hidden (spam)
Comment 6 nightstrike 2014-04-14 14:08:59 UTC
This is still an issue in C++
Comment 7 Alexander Monakov 2014-04-14 14:43:14 UTC
Nightstrike, is there a particular reason you want C++ warning behavior be adjusted?  Note that unlike C, in C++ you get zero-initialization by default, so you don't need to write ' = {0};' to zero-initialize a structure in the first place.  Thus I don't see if adjusting the warning is desirable for C++.  I might be wrong; in that case I'd recommend you to open a new bug and reference the new bug from here.
Comment 8 nightstrike 2014-04-15 05:54:30 UTC
Are you sure C++ works like that?  I thought that member variables in a struct would get default initialized to indeterminate values, as seen here:

http://en.cppreference.com/w/cpp/language/default_initialization

Surely, the C++ syntax of saying MyType x = {}; should be supported, as seen here:

http://en.cppreference.com/w/cpp/language/aggregate_initialization

where for instance:

struct S {
    int a;
    float b;
    std::string str;
};

S s = {}; // identical to S s = {0, 0.0, std::string};

That shouldn't warn for the reasons stated in earlier comments.
Comment 9 Alexander Monakov 2014-04-15 06:37:16 UTC
My statement about zero-initialization was inaccurate (thanks), but the general point still stands: in C you have to write ' = {0}' since empty-braces initializer is not supported by the language (you get a warning with -pedantic); in C++, you can write ' = {}' or 'T foo = T();', but you don't need to write ' = {0}' specifically.
Comment 10 nightstrike 2014-04-16 03:34:44 UTC
So should I open a new PR for not warning in C++?  Because even the "= {0}" case warns there.
Comment 11 Daniel Sommermann 2014-09-30 19:17:32 UTC
Created attachment 33627 [details]
Test case showing overly strict warning

This still produces false positives in C++11.

I attached a test case with several false positives. The compilation should be clean as there are no uninitialized members. Repros with g++ 4.9.1

Compile with "g++ test.cpp -std=c++11 -Wmissing-field-initializers`"

Produces:

dcsommer@dcsommer-ThinkPad-T440s:~/src/proxygen-oss-test/proxygen$ g++ test.cpp -Wmissing-field-initializers -std=c++11
test.cpp: In function ‘int main()’:
test.cpp:7:10: warning: missing initializer for member ‘Foo::bar’ [-Wmissing-field-initializers]
   Foo f1{};
          ^
test.cpp:7:10: warning: missing initializer for member ‘Foo::baz’ [-Wmissing-field-initializers]
test.cpp:8:11: warning: missing initializer for member ‘Foo::baz’ [-Wmissing-field-initializers]
   Foo f2{0};
           ^
test.cpp:9:14: warning: missing initializer for member ‘Foo::baz’ [-Wmissing-field-initializers]
   Foo f3 = {0};
              ^
test.cpp:10:15: warning: missing initializer for member ‘Foo::baz’ [-Wmissing-field-initializers]
   Foo f4 = {0,};
               ^
Comment 12 nightstrike 2015-05-12 02:20:39 UTC
(In reply to Daniel Sommermann from comment #11)
> Created attachment 33627 [details]
> Test case showing overly strict warning
> 
> This still produces false positives in C++11.
> 
> I attached a test case with several false positives. The compilation should
> be clean as there are no uninitialized members. Repros with g++ 4.9.1
> 
> Compile with "g++ test.cpp -std=c++11 -Wmissing-field-initializers`"
> 
> Produces:
> 
> dcsommer@dcsommer-ThinkPad-T440s:~/src/proxygen-oss-test/proxygen$ g++
> test.cpp -Wmissing-field-initializers -std=c++11
> test.cpp: In function ‘int main()’:
> test.cpp:7:10: warning: missing initializer for member ‘Foo::bar’
> [-Wmissing-field-initializers]
>    Foo f1{};
>           ^
> test.cpp:7:10: warning: missing initializer for member ‘Foo::baz’
> [-Wmissing-field-initializers]
> test.cpp:8:11: warning: missing initializer for member ‘Foo::baz’
> [-Wmissing-field-initializers]
>    Foo f2{0};
>            ^
> test.cpp:9:14: warning: missing initializer for member ‘Foo::baz’
> [-Wmissing-field-initializers]
>    Foo f3 = {0};
>               ^
> test.cpp:10:15: warning: missing initializer for member ‘Foo::baz’
> [-Wmissing-field-initializers]
>    Foo f4 = {0,};
>                ^


This reproduces with 4.9.2 as well.  Request reopen.