| Summary: | -Wmissing-field-initializers relaxation request | ||
|---|---|---|---|
| Product: | gcc | Reporter: | Pádraig Brady <P> |
| Component: | c | Assignee: | Not yet assigned to anyone <unassigned> |
| Status: | RESOLVED FIXED | ||
| Severity: | enhancement | CC: | amonakov, gcc-bugs, gdr, jim, manu, nightstrike, otte |
| Priority: | P3 | Keywords: | diagnostic |
| Version: | 4.1.2 | ||
| Target Milestone: | --- | ||
| 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 | ||
A related thread: http://gcc.gnu.org/ml/gcc-bugs/1998-07/msg00031.html 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... 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 The above patch suppresses the warning in C regardless of whether the trailing comma is present. Closing as fixed. *** Bug 260998 has been marked as a duplicate of this bug. *** Seen from the domain http://volichat.com Page where seen: http://volichat.com/adult-chat-rooms Marked for reference. Resolved as fixed @bugzilla. This is still an issue in C++ 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.
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. 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.
So should I open a new PR for not warning in C++? Because even the "= {0}" case warns there.
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,};
^
(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. Apparently, this got fixed for C++ somewhere inbetween GCC 4.9.2 and 5.1.1.
```
int main() {
struct S {
int a;
float b;
};
S s = {}; // identical to S s = {0, 0.0, std::string};
}
```
Compiled with `-Wextra`, the warning isn't present anymore for GCC 5.1.1.
I think PR 61489 changed the behaviour for GCC 5. |
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.