public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* -Wmissing-field-initializers false positive with compound literals
       [not found] <d6e5a2c4-20f6-23d0-1e92-a6b0d3234220.ref@att.net>
@ 2022-02-03 20:03 ` Alexey Neyman
  2022-02-04  8:29   ` Richard Sandiford
  0 siblings, 1 reply; 2+ messages in thread
From: Alexey Neyman @ 2022-02-03 20:03 UTC (permalink / raw)
  To: gcc-help

Hi all,

I've encountered a weird behavior of -Wmissing-field-initializers option 
(which is a part of -Wextra) regarding structure initializers that 
contain compound literals as initializers for some of their fields. 
Reproduced with all GCC versions from 4.8.5 to 11.2.

Consider the following test case:

struct foo {
        const char *a1;
        const char * const *a2;
        void *a3;
        void *a4;
};

const char *aux[] = { "y", 0 };

struct foo a = {
        .a1 = "x",
#if defined(CASE1)
        .a2 = (const char * const []){ "y", 0 },
#elif defined(CASE2)
        .a2 = aux,
#elif defined(CASE3)
        .a2 = 0,
#elif defined(CASE4)
        /* .a2 not initialized */
#elif defined(CASE5)
        .a2 = (const char * const []){ "y", 0 },
        .a3 = 0,
#endif
};

struct foo b = {
        .a2 = (const char * const []){ "y", 0 },
        .a1 = "x",
};

CASE1 gives a warning about 'a3' field being initialized, despite the 
manual stating that named field initializers should prevent this warning 
from being generated. CASE2 initializes the field to point to an 
explicitly defined array, and it works with no warnings. CASE3 uses a 
constant as an initializer and also works without warnings. CASE5 
initializes the field 'a3' and produces no warnings about the next 
field, 'a4'.

Reversing the order of the 'a1' and 'a2' initializers (as in the 'b' 
variable) also does not produce a warning. It seems that the warning is 
only produced if the last initialized field in a structure uses a 
compound literal.

Looks like a bug to me; please confirm that I should file it into GCC's 
bugzilla.

Regards,
Alexey.


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: -Wmissing-field-initializers false positive with compound literals
  2022-02-03 20:03 ` -Wmissing-field-initializers false positive with compound literals Alexey Neyman
@ 2022-02-04  8:29   ` Richard Sandiford
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Sandiford @ 2022-02-04  8:29 UTC (permalink / raw)
  To: Alexey Neyman; +Cc: gcc-help

Alexey Neyman <stilor@att.net> writes:
> Hi all,
>
> I've encountered a weird behavior of -Wmissing-field-initializers option 
> (which is a part of -Wextra) regarding structure initializers that 
> contain compound literals as initializers for some of their fields. 
> Reproduced with all GCC versions from 4.8.5 to 11.2.
>
> Consider the following test case:
>
> struct foo {
>         const char *a1;
>         const char * const *a2;
>         void *a3;
>         void *a4;
> };
>
> const char *aux[] = { "y", 0 };
>
> struct foo a = {
>         .a1 = "x",
> #if defined(CASE1)
>         .a2 = (const char * const []){ "y", 0 },
> #elif defined(CASE2)
>         .a2 = aux,
> #elif defined(CASE3)
>         .a2 = 0,
> #elif defined(CASE4)
>         /* .a2 not initialized */
> #elif defined(CASE5)
>         .a2 = (const char * const []){ "y", 0 },
>         .a3 = 0,
> #endif
> };
>
> struct foo b = {
>         .a2 = (const char * const []){ "y", 0 },
>         .a1 = "x",
> };
>
> CASE1 gives a warning about 'a3' field being initialized, despite the 
> manual stating that named field initializers should prevent this warning 
> from being generated. CASE2 initializes the field to point to an 
> explicitly defined array, and it works with no warnings. CASE3 uses a 
> constant as an initializer and also works without warnings. CASE5 
> initializes the field 'a3' and produces no warnings about the next 
> field, 'a4'.
>
> Reversing the order of the 'a1' and 'a2' initializers (as in the 'b' 
> variable) also does not produce a warning. It seems that the warning is 
> only produced if the last initialized field in a structure uses a 
> compound literal.
>
> Looks like a bug to me; please confirm that I should file it into GCC's 
> bugzilla.

Yeah, I agree it's a bug.  I think it's likely to be the same underlying
issue as PR82283, so I've copied the example there (hope you don't mind).

Thanks,
Richard

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-02-04  8:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <d6e5a2c4-20f6-23d0-1e92-a6b0d3234220.ref@att.net>
2022-02-03 20:03 ` -Wmissing-field-initializers false positive with compound literals Alexey Neyman
2022-02-04  8:29   ` Richard Sandiford

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).