Bug 64918 - invalid (?) warning when initializing structure
Summary: invalid (?) warning when initializing structure
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.9.2
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2015-02-03 13:43 UTC by Øystein Schønning-Johansen
Modified: 2021-01-05 16:57 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-04-23 00:00:00


Attachments
Code that trigger the invalid warning. (335 bytes, text/x-csrc)
2015-02-03 13:43 UTC, Øystein Schønning-Johansen
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Øystein Schønning-Johansen 2015-02-03 13:43:36 UTC
Created attachment 34651 [details]
Code that trigger the invalid warning.

Hi!

I'm initializing a complex structure with designated initialisers and overriding an array element (intentionally), and I then get the error message:

sideffects.c:26:13: warning: initialized field with side-effects overwritten
             [3] = { .func = thefunction, .args = &((char*[]){"foo4", "bar4", "baz4"})},
             ^
sideffects.c:26:13: warning: (near initialization for 'myconfig.config[3]')

However I do believe my code is correct. It complains about initializing with side-effects, but I think my code is legal c99. (Please tell me otherwise.)

I compile the attached code with:
gcc -std=gnu99 -Wall -Wextra -Wno-override-init sideeffects.c -o sideeffects

I have tried several different versions of GCC, and here is a short summary:

GCC 4.9.2 (Arch Linux x86_64)  -> Warning generated.
GCC 4.9.2 (Mingw32-w64 x86_64) -> Warning generated.
GCC 4.8.0 (Linux x86_64)       -> Warning generated. 
GCC 4.4.7 (Red Hat 4.4.7-4)    -> No warning.
GCC 4.1.2 (Red Hat 4.1.2-52)   -> No warning.
clang 3.5.1 (Arch Linux)       -> No warning.

Best regards,
-Øystein



Full info on compilers used (Those that generates warning):

[oystein@jupiter ~]$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /build/gcc/src/gcc-4.9-20141224/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-cloog-backend=isl --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --disable-multilib --disable-werror --enable-checking=release
Thread model: posix
gcc version 4.9.2 20141224 (prerelease) (GCC)


[14:29:03,90 c:\APPL]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=C:/Program\ Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev1/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/4.9.2/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-4.9.2/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw492/x86_64-492-posix-seh-rt_v3-rev1/mingw64 --with-gxx-include-dir=/mingw64/x86_64-w64-mingw32/include/c++ --enable-shared --enable-static --disable-multilib --enable-languages=ada,c,c++,fortran,objc,obj-c++,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-isl-version-check --disable-cloog-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw492/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw492/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw492/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw492/prerequisites/x86_64-w64-mingw32-static --with-cloog=/c/mingw492/prerequisites/x86_64-w64-mingw32-static --enable-cloog-backend=isl --with-pkgversion='x86_64-posix-seh-rev1, Built by MinGW-W64 project' --with-bugurl=http://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -I/c/mingw492/x86_64-492-posix-seh-rt_v3-rev1/mingw64/opt/include -I/c/mingw492/prerequisites/x86_64-zlib-static/include -I/c/mingw492/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -I/c/mingw492/x86_64-492-posix-seh-rt_v3-rev1/mingw64/opt/include -I/c/mingw492/prerequisites/x86_64-zlib-static/include -I/c/mingw492/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS= LDFLAGS='-pipe -L/c/mingw492/x86_64-492-posix-seh-rt_v3-rev1/mingw64/opt/lib -L/c/mingw492/prerequisites/x86_64-zlib-static/lib -L/c/mingw492/prerequisites/x86_64-w64-mingw32-static/lib '
Thread model: posix
gcc version 4.9.2 (x86_64-posix-seh-rev1, Built by MinGW-W64 project)

[ojohans@st-linapp30 ~/tmp]$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/private/ojohans/personal/bin/../libexec/gcc/x86_64-unknown-linux-gnu/4.8.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-trunk-source/gcc/configure --enable-languages=c,c++,fortran --enable-checking=release --disable-bootstrap --disable-libmudflap --disable-libstdcxx-pch --enable-libgomp --enable-lto --enable-gold --with-plugin-ld=/usr/bin/gold --with-cloog-include=/usr/include-cloog --with-cloog-lib=/usr/lib64 --prefix=/usr/local/gcc-trunk
Thread model: posix
gcc version 4.8.0 20121206 (experimental) [trunk revision 194238] (GCC)
Comment 1 jsm-csl@polyomino.org.uk 2015-02-03 15:47:37 UTC
I think the logical side-effect in C standard terms of the initializer 
being overridden is that it contains a compound literal, and so executing 
that initializer has the side-effect of initializing that compound literal 
(compound literal initializers being executed each time the compound 
literal is reached in the order of execution).  The warning is because 
it's unspecified whether overridden initializers are executed at all, and 
so whether their side-effects occur - but in this case it's maybe not so 
helpful, because the compound literal in the overridden initializer can't 
actually be accessed so it doesn't matter if the side-effect of 
initializing it gets lost (unless any of its initializers contained other 
side-effects, which they don't in this case).
Comment 2 Øystein Schønning-Johansen 2015-02-03 16:37:58 UTC
Really insightful, Joseph. I do understand the warning a bit better now. I have not looked into the GCC parsing code, but based on your description and my (limited) understanding of the problem, I guess a fix is not trivial? Can I suppress this warning in any way?

-Ø
Comment 3 jsm-csl@polyomino.org.uk 2015-02-03 17:33:24 UTC
There isn't currently an option to disable this warning.
Comment 4 Marek Polacek 2015-04-20 15:14:34 UTC
Could be fixed with adding -Woverride-init-side-effects, but not sure if it's worth it.
Comment 5 Marek Polacek 2015-04-23 22:44:16 UTC
Patch posted.
Comment 6 Manuel López-Ibáñez 2015-04-23 23:33:16 UTC
(In reply to Øystein Schønning-Johansen from comment #2)
> Really insightful, Joseph. I do understand the warning a bit better now. I
> have not looked into the GCC parsing code, but based on your description and
> my (limited) understanding of the problem, I guess a fix is not trivial? Can
> I suppress this warning in any way?

I haven't looked at the code, but my guess is that at the time of the warning, it should be possible to check the overriden initializer, if it is a compound literal, recur on its initializers and check for side-effects again.
Comment 7 Marek Polacek 2015-05-07 22:14:58 UTC
Author: mpolacek
Date: Thu May  7 22:14:27 2015
New Revision: 222894

URL: https://gcc.gnu.org/viewcvs?rev=222894&root=gcc&view=rev
Log:
	PR c/64918
	* c.opt (Woverride-init-side-effects): New option.

	* c-typeck.c (add_pending_init): Use OPT_Woverride_init_side_effects.
	(output_init_element): Likewise.

	* doc/invoke.texi: Document -Woverride-init-side-effects.

	* gcc.dg/Woverride-init-side-effects-1.c: New test.
	* gcc.dg/Woverride-init-side-effects-2.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/Woverride-init-side-effects-1.c
    trunk/gcc/testsuite/gcc.dg/Woverride-init-side-effects-2.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c.opt
    trunk/gcc/c/ChangeLog
    trunk/gcc/c/c-typeck.c
    trunk/gcc/doc/invoke.texi
    trunk/gcc/testsuite/ChangeLog
Comment 8 Jakub Jelinek 2016-04-27 10:56:16 UTC
GCC 6.1 has been released.
Comment 9 Manuel López-Ibáñez 2016-08-22 11:42:07 UTC
Only regressions have target milestone.
Comment 10 Martin Liška 2018-11-19 13:24:59 UTC
Can the bug be marked as resolved?
Comment 11 Marek Polacek 2021-01-05 16:57:30 UTC
Fixed.