public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/30006]  New: Compound literal in structure initializer causes irrelevant warning
@ 2006-11-28  8:47 yar at bsd dot chem dot msu dot ru
  2006-12-02  8:10 ` [Bug c/30006] Compound literal in structure initializer causes missing initializer warning to happen pinskia at gcc dot gnu dot org
  2008-06-06  9:51 ` he at uninett dot no
  0 siblings, 2 replies; 3+ messages in thread
From: yar at bsd dot chem dot msu dot ru @ 2006-11-28  8:47 UTC (permalink / raw)
  To: gcc-bugs

Note: This report doesn't use the FreeBSD system GCC compiler; it uses an
original GCC 4.3.0 snapshot built and installed separately. However, the bug
also persists in GCC 3.4.6 found in FreeBSD, and in original GCC 4.2.0.

The complete test.c program:

struct foo {
        char *p;
        int i;
} bar = {
#ifdef BUG
        .p = (char[]){"abc"}
#else
        .p = "abc"
#endif
};

I.e., it will use a plain string to initialize bar.p if BUG is undefined,
but it will use a compound literal if BUG is defined.

The attempts to compile it (blank lines added for clarity):

$gcc43 -v
Using built-in specs.
Target: i386-portbld-freebsd7.0
Configured with: ./..//gcc-4.3-20061125/configure --disable-nls
--with-system-zlib --with-libiconv-prefix=/usr/local --with-gmp=/usr/local
--program-suffix=43 --libdir=/usr/local/lib/gcc-4.3.0
--with-gxx-include-dir=/usr/local/lib/gcc-4.3.0/include/c++/
--infodir=/usr/local/info/gcc43 --disable-rpath --prefix=/usr/local
i386-portbld-freebsd7.0
Thread model: posix
gcc version 4.3.0 20061125 (experimental)

$gcc43 -Wall -W -Werror -c test.c

$gcc43 -DBUG -Wall -W -Werror -c test.c
cc1: warnings being treated as errors
test.c:10: warning: missing initializer
test.c:10: warning: (near initialization for 'bar.i')

That is, using a compound literal instead of a plain string
to initialize bar.p leads to a warning on a different structure
member, bar.i.

AFAIK GCC would issue such a warning if an old-style structure
initializer were used and not all its members were specified.
However, the example program uses a C99 structure initializer,
and GCC is happy about it unless the compound literal is there
(see the successful compilation when BUG wasn't defined.)

Although it's just a warning, it can be painful for projects
that enforce -Werror policy for their code.

Thanks a lot!


-- 
           Summary: Compound literal in structure initializer causes
                    irrelevant warning
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: yar at bsd dot chem dot msu dot ru
 GCC build triplet: i386-portbld-freebsd7.0
  GCC host triplet: i386-portbld-freebsd7.0
GCC target triplet: i386-portbld-freebsd7.0


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30006


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

* [Bug c/30006] Compound literal in structure initializer causes missing initializer warning to happen
  2006-11-28  8:47 [Bug c/30006] New: Compound literal in structure initializer causes irrelevant warning yar at bsd dot chem dot msu dot ru
@ 2006-12-02  8:10 ` pinskia at gcc dot gnu dot org
  2008-06-06  9:51 ` he at uninett dot no
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-12-02  8:10 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2425 bytes --]



------- Comment #1 from pinskia at gcc dot gnu dot org  2006-12-02 08:10 -------
Confirmed.
        /* Do not warn if this level of the initializer uses member
           designators; it is likely to be deliberate.  */
        if (constructor_unfilled_fields && !constructor_designated)

I think we forget to push/pop the context for compound literals.
Here is another testcase where we warn:
struct foo {
        char *p;
        int i;
        int j;
} bar = {
        .j = 1,
        .p = (char[]){"abc"}
};

And one where we don't as the order of the compound literal has changed:
struct foo {
        char *p;
        int i;
        int j;
} bar = {
        .p = (char[]){"abc"},
        .j = 1
};
-----
Here is an even more complex testcase where we warn too much:
struct h
{
  int x, y;
};
struct g
{
  struct h *i;
  int j;
};
struct foo {
        struct g *v;
        int i1;
        int j1;
} bar = {
  &(struct g) {
    .i = &(struct h){1}
  }
};
------------------------------------
t.c:16: warning: missing initializer
t.c:16: warning: (near initialization for ‘(anonymous).y’)
t.c:17: warning: missing initializer
t.c:17: warning: (near initialization for ‘(anonymous).j’)
t.c:18: warning: missing initializer
t.c:18: warning: (near initialization for ‘bar.i1’)

We should not warn about (anonymous).j.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
  GCC build triplet|i386-portbld-freebsd7.0     |
   GCC host triplet|i386-portbld-freebsd7.0     |
 GCC target triplet|i386-portbld-freebsd7.0     |
           Keywords|                            |diagnostic
      Known to fail|                            |2.95 3.2.3 3.4.0 4.0.0 4.2.0
                   |                            |4.0.4 4.1.2 4.2.0 4.3.0
   Last reconfirmed|0000-00-00 00:00:00         |2006-12-02 08:10:07
               date|                            |
            Summary|Compound literal in         |Compound literal in
                   |structure initializer causes|structure initializer causes
                   |irrelevant warning          |missing initializer warning
                   |                            |to happen


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30006


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

* [Bug c/30006] Compound literal in structure initializer causes missing initializer warning to happen
  2006-11-28  8:47 [Bug c/30006] New: Compound literal in structure initializer causes irrelevant warning yar at bsd dot chem dot msu dot ru
  2006-12-02  8:10 ` [Bug c/30006] Compound literal in structure initializer causes missing initializer warning to happen pinskia at gcc dot gnu dot org
@ 2008-06-06  9:51 ` he at uninett dot no
  1 sibling, 0 replies; 3+ messages in thread
From: he at uninett dot no @ 2008-06-06  9:51 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from he at uninett dot no  2008-06-06 09:50 -------
Hi,

here is another testcase which I think might have the
same or at least a similar root cause:

struct o {
        struct n {
                int b;
                int a;
        } n;
};

const struct o o = {
#ifdef BUG
        .n.b = 1
#else
        .n = { .b = 1 }
#endif
};

When compiled with "-c -Wextra" with gcc 4.1.3 as found in NetBSD-current,
this will complain about missing initializers if BUG is defined, even though
a C99-style initializer is used, something which I thought should have obviated 
the insistence on initializing all the fields of the struct.


-- 

he at uninett dot no changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |he at uninett dot no


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30006


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

end of thread, other threads:[~2008-06-06  9:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-11-28  8:47 [Bug c/30006] New: Compound literal in structure initializer causes irrelevant warning yar at bsd dot chem dot msu dot ru
2006-12-02  8:10 ` [Bug c/30006] Compound literal in structure initializer causes missing initializer warning to happen pinskia at gcc dot gnu dot org
2008-06-06  9:51 ` he at uninett dot no

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