public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/82283] Wrong warning with -Wmissing-field-initializers
       [not found] <bug-82283-4@http.gcc.gnu.org/bugzilla/>
@ 2020-05-12 19:53 ` marxin at gcc dot gnu.org
  2021-04-14  9:39 ` robert.dumitru at cyberthorstudios dot com
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: marxin at gcc dot gnu.org @ 2020-05-12 19:53 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |marxin at gcc dot gnu.org,
                   |                            |mpolacek at gcc dot gnu.org

--- Comment #9 from Martin Liška <marxin at gcc dot gnu.org> ---
@Marek: Would it be possible to fix these issues in C FE?

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

* [Bug c/82283] Wrong warning with -Wmissing-field-initializers
       [not found] <bug-82283-4@http.gcc.gnu.org/bugzilla/>
  2020-05-12 19:53 ` [Bug c/82283] Wrong warning with -Wmissing-field-initializers marxin at gcc dot gnu.org
@ 2021-04-14  9:39 ` robert.dumitru at cyberthorstudios dot com
  2021-04-14  9:56 ` redi at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: robert.dumitru at cyberthorstudios dot com @ 2021-04-14  9:39 UTC (permalink / raw)
  To: gcc-bugs

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

Robert Dumitru <robert.dumitru at cyberthorstudios dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |robert.dumitru@cyberthorstu
                   |                            |dios.com

--- Comment #10 from Robert Dumitru <robert.dumitru at cyberthorstudios dot com> ---
We are experiencing the same issue:

The warning: missing initializer for field ... [-Wmissing-field-initializers]
is being thrown incorrectly. 

The following code is correct, however [-Wmissing-field-initializers] are
shown. 

struct test_t{
  int value1;
  int value2;
};

struct test_t test[] = {
  [0].value1 = 1,
  [0].value2 = 2,
  [1].value1 = 10,
  [1].value2 = 20
};

int main(){
  return 0;
}

warning: missing initializer for field 'value2' of 'struct test_t'
[-Wmissing-field-initializers]
   [0].value2 = 2,
warning: missing initializer for field 'value2' of 'struct test_t'
[-Wmissing-field-initializers]
   [1].value2 = 20

The initialization is correct:
_test:
.long 1
.long 2
.long 10
.long 20

This bug was discovered first on version 8.3 but it can be reproduced on
version 10.2 as well. Please note you need the -Wextra flag in order to
reproduce this. 

I think https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99081 is also relating to
this.

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

* [Bug c/82283] Wrong warning with -Wmissing-field-initializers
       [not found] <bug-82283-4@http.gcc.gnu.org/bugzilla/>
  2020-05-12 19:53 ` [Bug c/82283] Wrong warning with -Wmissing-field-initializers marxin at gcc dot gnu.org
  2021-04-14  9:39 ` robert.dumitru at cyberthorstudios dot com
@ 2021-04-14  9:56 ` redi at gcc dot gnu.org
  2022-02-04  8:28 ` rsandifo at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2021-04-14  9:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Robert Dumitru from comment #10)
> I think https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99081 is also relating
> to this.

A similar issue, but I think the code for parsing these initializes in C and
C++ is completely separate. I've added it to the "See Also" field anyway.

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

* [Bug c/82283] Wrong warning with -Wmissing-field-initializers
       [not found] <bug-82283-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2021-04-14  9:56 ` redi at gcc dot gnu.org
@ 2022-02-04  8:28 ` rsandifo at gcc dot gnu.org
  2022-03-22 18:36 ` mpolacek at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: rsandifo at gcc dot gnu.org @ 2022-02-04  8:28 UTC (permalink / raw)
  To: gcc-bugs

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

rsandifo at gcc dot gnu.org <rsandifo at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rsandifo at gcc dot gnu.org

--- Comment #12 from rsandifo at gcc dot gnu.org <rsandifo at gcc dot gnu.org> ---
Here's another example, from Alexey Neyman
[https://gcc.gnu.org/pipermail/gcc-help/2022-February/141197.html]:

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",
};

Only CASE1 of a warns; the others are (correctly) accepted
without warnings.

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

* [Bug c/82283] Wrong warning with -Wmissing-field-initializers
       [not found] <bug-82283-4@http.gcc.gnu.org/bugzilla/>
                   ` (3 preceding siblings ...)
  2022-02-04  8:28 ` rsandifo at gcc dot gnu.org
@ 2022-03-22 18:36 ` mpolacek at gcc dot gnu.org
  2022-03-22 20:41 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-03-22 18:36 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #13 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I have a patch which fixes all the testcases here.

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

* [Bug c/82283] Wrong warning with -Wmissing-field-initializers
       [not found] <bug-82283-4@http.gcc.gnu.org/bugzilla/>
                   ` (4 preceding siblings ...)
  2022-03-22 18:36 ` mpolacek at gcc dot gnu.org
@ 2022-03-22 20:41 ` cvs-commit at gcc dot gnu.org
  2022-03-22 20:43 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-03-22 20:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #14 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:4b7d9f8f51bd96d290aac230c71e501fcb6b21a6

commit r12-7772-g4b7d9f8f51bd96d290aac230c71e501fcb6b21a6
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Mar 22 14:37:02 2022 -0400

    c: -Wmissing-field-initializers and designated inits [PR82283, PR84685]

    This patch fixes two kinds of wrong -Wmissing-field-initializers
    warnings.  Our docs say that this warning "does not warn about designated
    initializers", but we give a warning for

    1) the array case:

      struct S {
        struct N {
          int a;
          int b;
        } c[1];
      } d = {
        .c[0].a = 1,
        .c[0].b = 1, // missing initializer for field 'b' of 'struct N'
      };

    we warn because push_init_level, when constructing an array, clears
    constructor_designated (which the warning relies on), and we forget
    that we were in a designated initializer context.  Fixed by the
    push_init_level hunk; and

    2) the compound literal case:

      struct T {
        int a;
        int *b;
        int c;
      };

      struct T t = { .b = (int[]){1} }; // missing initializer for field 'c' of
'struct T'

    where set_designator properly sets constructor_designated to 1, but the
    compound literal causes us to create a whole new initializer_stack in
    start_init, which clears constructor_designated.  Then, after we've parsed
    the compound literal, finish_init flushes the initializer_stack entry,
    but doesn't restore constructor_designated, so we forget we were in
    a designated initializer context, which causes the bogus warning.  (The
    designated flag is also tracked in constructor_stack, but in this case,
    we didn't perform push_init_level between set_designator and start_init
    so it wasn't saved anywhere.)

            PR c/82283
            PR c/84685

    gcc/c/ChangeLog:

            * c-typeck.cc (struct initializer_stack): Add 'designated' member.
            (start_init): Set it.
            (finish_init): Restore constructor_designated.
            (push_init_level): Set constructor_designated to the value of
            constructor_designated in the upper constructor_stack.

    gcc/testsuite/ChangeLog:

            * gcc.dg/Wmissing-field-initializers-1.c: New test.
            * gcc.dg/Wmissing-field-initializers-2.c: New test.
            * gcc.dg/Wmissing-field-initializers-3.c: New test.
            * gcc.dg/Wmissing-field-initializers-4.c: New test.
            * gcc.dg/Wmissing-field-initializers-5.c: New test.

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

* [Bug c/82283] Wrong warning with -Wmissing-field-initializers
       [not found] <bug-82283-4@http.gcc.gnu.org/bugzilla/>
                   ` (5 preceding siblings ...)
  2022-03-22 20:41 ` cvs-commit at gcc dot gnu.org
@ 2022-03-22 20:43 ` mpolacek at gcc dot gnu.org
  2022-03-24  9:26 ` yann at droneaud dot fr
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-03-22 20:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #15 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Patch seems safe to backport to 11.

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

* [Bug c/82283] Wrong warning with -Wmissing-field-initializers
       [not found] <bug-82283-4@http.gcc.gnu.org/bugzilla/>
                   ` (6 preceding siblings ...)
  2022-03-22 20:43 ` mpolacek at gcc dot gnu.org
@ 2022-03-24  9:26 ` yann at droneaud dot fr
  2022-03-29  1:46 ` cvs-commit at gcc dot gnu.org
  2022-03-29  1:49 ` mpolacek at gcc dot gnu.org
  9 siblings, 0 replies; 10+ messages in thread
From: yann at droneaud dot fr @ 2022-03-24  9:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #16 from Yann Droneaud <yann at droneaud dot fr> ---
(In reply to Marek Polacek from comment #13)
> I have a patch which fixes all the testcases here.

I've checked my test cases using godbolt gcc trunk, and, yeah, thanks a lot !

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

* [Bug c/82283] Wrong warning with -Wmissing-field-initializers
       [not found] <bug-82283-4@http.gcc.gnu.org/bugzilla/>
                   ` (7 preceding siblings ...)
  2022-03-24  9:26 ` yann at droneaud dot fr
@ 2022-03-29  1:46 ` cvs-commit at gcc dot gnu.org
  2022-03-29  1:49 ` mpolacek at gcc dot gnu.org
  9 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-03-29  1:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #17 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Marek Polacek
<mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:0fa9022aa30b9c4dde965a0406943c8c0af5eb54

commit r11-9715-g0fa9022aa30b9c4dde965a0406943c8c0af5eb54
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Mar 22 14:37:02 2022 -0400

    c: -Wmissing-field-initializers and designated inits [PR82283, PR84685]

    This patch fixes two kinds of wrong -Wmissing-field-initializers
    warnings.  Our docs say that this warning "does not warn about designated
    initializers", but we give a warning for

    1) the array case:

      struct S {
        struct N {
          int a;
          int b;
        } c[1];
      } d = {
        .c[0].a = 1,
        .c[0].b = 1, // missing initializer for field 'b' of 'struct N'
      };

    we warn because push_init_level, when constructing an array, clears
    constructor_designated (which the warning relies on), and we forget
    that we were in a designated initializer context.  Fixed by the
    push_init_level hunk; and

    2) the compound literal case:

      struct T {
        int a;
        int *b;
        int c;
      };

      struct T t = { .b = (int[]){1} }; // missing initializer for field 'c' of
'struct T'

    where set_designator properly sets constructor_designated to 1, but the
    compound literal causes us to create a whole new initializer_stack in
    start_init, which clears constructor_designated.  Then, after we've parsed
    the compound literal, finish_init flushes the initializer_stack entry,
    but doesn't restore constructor_designated, so we forget we were in
    a designated initializer context, which causes the bogus warning.  (The
    designated flag is also tracked in constructor_stack, but in this case,
    we didn't perform push_init_level between set_designator and start_init
    so it wasn't saved anywhere.)

            PR c/82283
            PR c/84685

    gcc/c/ChangeLog:

            * c-typeck.c (struct initializer_stack): Add 'designated' member.
            (start_init): Set it.
            (finish_init): Restore constructor_designated.
            (push_init_level): Set constructor_designated to the value of
            constructor_designated in the upper constructor_stack.

    gcc/testsuite/ChangeLog:

            * gcc.dg/Wmissing-field-initializers-1.c: New test.
            * gcc.dg/Wmissing-field-initializers-2.c: New test.
            * gcc.dg/Wmissing-field-initializers-3.c: New test.
            * gcc.dg/Wmissing-field-initializers-4.c: New test.
            * gcc.dg/Wmissing-field-initializers-5.c: New test.

    (cherry picked from commit 4b7d9f8f51bd96d290aac230c71e501fcb6b21a6)

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

* [Bug c/82283] Wrong warning with -Wmissing-field-initializers
       [not found] <bug-82283-4@http.gcc.gnu.org/bugzilla/>
                   ` (8 preceding siblings ...)
  2022-03-29  1:46 ` cvs-commit at gcc dot gnu.org
@ 2022-03-29  1:49 ` mpolacek at gcc dot gnu.org
  9 siblings, 0 replies; 10+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-03-29  1:49 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #18 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2022-03-29  1:49 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-82283-4@http.gcc.gnu.org/bugzilla/>
2020-05-12 19:53 ` [Bug c/82283] Wrong warning with -Wmissing-field-initializers marxin at gcc dot gnu.org
2021-04-14  9:39 ` robert.dumitru at cyberthorstudios dot com
2021-04-14  9:56 ` redi at gcc dot gnu.org
2022-02-04  8:28 ` rsandifo at gcc dot gnu.org
2022-03-22 18:36 ` mpolacek at gcc dot gnu.org
2022-03-22 20:41 ` cvs-commit at gcc dot gnu.org
2022-03-22 20:43 ` mpolacek at gcc dot gnu.org
2022-03-24  9:26 ` yann at droneaud dot fr
2022-03-29  1:46 ` cvs-commit at gcc dot gnu.org
2022-03-29  1:49 ` mpolacek at gcc dot gnu.org

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).