This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/84685] Designated initializers warning


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84685

Jonathan Leffler <jonathan.leffler at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jonathan.leffler at gmail dot com

--- Comment #1 from Jonathan Leffler <jonathan.leffler at gmail dot com> ---
The problem was first noted and discussed in a question on Stack Overflow:
https://stackoverflow.com/questions/49081541/

There are some comments there that may help with the diagnosis.  The problem
occurs with GCC 7.3.0 compiled on a Mac too.  A major component of the bug is
the inconsistency; it requires a compound array literal as an initializer, and
it does not report for all fields that are not specifically initialized. 

There are two ways the bug fix could go.

(1) It reports on each uninitialized member, not just the one after the member
initialized with compound array literal.
(2) It doesn't report on any uninitialized member because they're all
zero-initialized when a designated initializer is used.

Of the two, fix version (2) is what I'd prefer to see.  Note that Clang does
not report an error for this, which is in line with fix version (2).

The compound literal must be an array for the problem to reproduce.  Using

    struct T t = {.b = &t.a};

in the initialization doesn't trigger any warning, for example.

The warning appears for the first field after the last member initialized with
a compound array literal.

Consider this code (file diw83.c):

struct T
{
    int a;
    int *b;
    int c;
    int d;
    int *e;
    int f;
    int g;
    int h;
};

struct T foo(int bar);

struct T foo(int bar)
{
    struct T t = { .b = (int[]){ 1 }, .e = (int[]){ 2 } };
    t.c = bar;
    return t;
}

Compilation:

$ gcc -v -save-temps -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes
-Wstrict-prototypes -c diw83.c
Using built-in specs.
COLLECT_GCC=gcc
Target: x86_64-apple-darwin17.4.0
Configured with: ../gcc-7.3.0/configure --prefix=/opt/gcc/v7.3.0
CC=/usr/bin/clang CXX=/usr/bin/clang++
Thread model: posix
gcc version 7.3.0 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-g' '-std=c11' '-Wall' '-Wextra'
'-Werror' '-Wmissing-prototypes' '-Wstrict-prototypes' '-c'
'-mmacosx-version-min=10.13.4' '-asm_macosx_version_min=10.13' '-mtune=core2'
 /opt/gcc/v7.3.0/libexec/gcc/x86_64-apple-darwin17.4.0/7.3.0/cc1 -E -quiet -v
-D__DYNAMIC__ diw83.c -fPIC -feliminate-unused-debug-symbols
-mmacosx-version-min=10.13.4 -mtune=core2 -std=c11 -Wall -Wextra -Werror
-Wmissing-prototypes -Wstrict-prototypes -g -fworking-directory -O3
-fpch-preprocess -o diw83.i
ignoring nonexistent directory
"/opt/gcc/v7.3.0/lib/gcc/x86_64-apple-darwin17.4.0/7.3.0/../../../../x86_64-apple-darwin17.4.0/include"
#include "..." search starts here:
#include <...> search starts here:
 /opt/gcc/v7.3.0/lib/gcc/x86_64-apple-darwin17.4.0/7.3.0/include
 /usr/local/include
 /opt/gcc/v7.3.0/include
 /opt/gcc/v7.3.0/lib/gcc/x86_64-apple-darwin17.4.0/7.3.0/include-fixed
 /usr/include
 /System/Library/Frameworks
 /Library/Frameworks
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O3' '-g' '-std=c11' '-Wall' '-Wextra'
'-Werror' '-Wmissing-prototypes' '-Wstrict-prototypes' '-c'
'-mmacosx-version-min=10.13.4' '-asm_macosx_version_min=10.13' '-mtune=core2'
 /opt/gcc/v7.3.0/libexec/gcc/x86_64-apple-darwin17.4.0/7.3.0/cc1 -fpreprocessed
diw83.i -fPIC -feliminate-unused-debug-symbols -quiet -dumpbase diw83.c
-mmacosx-version-min=10.13.4 -mtune=core2 -auxbase diw83 -g -O3 -Wall -Wextra
-Werror -Wmissing-prototypes -Wstrict-prototypes -std=c11 -version -o diw83.s
GNU C11 (GCC) version 7.3.0 (x86_64-apple-darwin17.4.0)
        compiled by GNU C version 7.3.0, GMP version 6.1.2, MPFR version 3.1.5,
MPC version 1.0.3, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C11 (GCC) version 7.3.0 (x86_64-apple-darwin17.4.0)
        compiled by GNU C version 7.3.0, GMP version 6.1.2, MPFR version 3.1.5,
MPC version 1.0.3, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 43361e81476fe13fcb47d99cd4904fd3
diw83.c: In function ‘foo’:
diw83.c:17:12: error: missing initializer for field ‘f’ of ‘struct T’
[-Werror=missing-field-initializers]
     struct T t = { .b = (int[]){ 1 }, .e = (int[]){ 2 } };
            ^
diw83.c:8:9: note: ‘f’ declared here
     int f;
         ^
cc1: all warnings being treated as errors
$

Apart from five #line directives at the start, the preprocessed file (diw83.i)
is the same as the input file.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]