public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Alexey Neyman <stilor@att.net>
To: gcc-help@gcc.gnu.org
Subject: -Wmissing-field-initializers false positive with compound literals
Date: Thu, 3 Feb 2022 12:03:46 -0800	[thread overview]
Message-ID: <d6e5a2c4-20f6-23d0-1e92-a6b0d3234220@att.net> (raw)
In-Reply-To: <d6e5a2c4-20f6-23d0-1e92-a6b0d3234220.ref@att.net>

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.


       reply	other threads:[~2022-02-03 20:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <d6e5a2c4-20f6-23d0-1e92-a6b0d3234220.ref@att.net>
2022-02-03 20:03 ` Alexey Neyman [this message]
2022-02-04  8:29   ` Richard Sandiford

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d6e5a2c4-20f6-23d0-1e92-a6b0d3234220@att.net \
    --to=stilor@att.net \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).