Bug 4784 - Anonymous structs issues
Summary: Anonymous structs issues
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.1
: P3 normal
Target Milestone: 4.6.0
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
: 47376 (view as bug list)
Depends on:
Blocks:
 
Reported: 2001-11-04 09:36 UTC by Joseph S. Myers
Modified: 2011-02-07 02:00 UTC (History)
5 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work: 4.6.0
Known to fail: 3.0.4 3.2.3 3.2.2 3.3.3 3.4.0 4.0.0
Last reconfirmed: 2005-12-10 05:43:02


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joseph S. Myers 2001-11-04 09:36:00 UTC
Where anonymous members of structures and unions are used, GCC fails
to detect whether this might lead to multiple interpretations of a
field name.

Release:
3.1 20011103 (experimental)

Environment:
System: Linux digraph 2.2.19 #1 Sun Oct 21 10:19:10 UTC 2001 i686 unknown
Architecture: i686

	
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../gcc-cvs/configure --prefix=/opt/gcc/mainline --disable-shared --enable-threads=posix --with-system-zlib

How-To-Repeat:
Some examples in
http://gcc.gnu.org/ml/gcc-patches/2001-10/msg00149.html
(see also the rest of that thread).
Comment 1 Joseph S. Myers 2001-11-04 09:36:00 UTC
Fix:
Appropriate recursive checks.  Update the manual to reflect that such
usages are now hard errors.
Comment 2 Wolfgang Bangerth 2003-03-07 02:24:47 UTC
State-Changed-From-To: open->feedback
State-Changed-Why: Hm, with 3.3 and present mainline, we get on this code
    -----------------------
    struct s {
        int x;
        struct { int x; };
    };
    ------------------
    the following message:
    g/x> /home/bangerth/bin/gcc-3.4-pre/bin/gcc -W -Wall -std=c99 -c x.c
    x.c:3: warning: declaration does not declare anything
    
    gcc3.2 is quiet. If compiled in C++ mode, we get this:
    g/x> /home/bangerth/bin/gcc-3.4-pre/bin/gcc -W -Wall -c x.cc
    x.cc:3: error: declaration of `int s::<anonymous struct>::x'
    x.cc:2: error: conflicts with previous declaration `int s::x'
    x.cc:3: error: duplicate member `s::<anonymous struct>::x'
    
    Joseph, is this approximately what you wanted gcc to
    tell us?
    
    W.
Comment 3 Wolfgang Bangerth 2003-03-07 09:09:05 UTC
From: Wolfgang Bangerth <bangerth@ticam.utexas.edu>
To: "Joseph S. Myers" <jsm28@cam.ac.uk>
Cc: gcc-bugs@gcc.gnu.org, <gcc-gnats@gcc.gnu.org>
Subject: Re: c/4784: Anonymous structs issues
Date: Fri, 7 Mar 2003 09:09:05 -0600 (CST)

 > >     -----------------------
 > >     struct s {
 > >         int x;
 > >         struct { int x; };
 > >     };
 > >     ------------------
 > >     the following message:
 > >     g/x> /home/bangerth/bin/gcc-3.4-pre/bin/gcc -W -Wall -std=c99 -c x.c
 > >     x.c:3: warning: declaration does not declare anything
 > 
 > What's relevant is -std=gnu99 and -std=gnu89, as this is a GNU extension.
 > That warning hardly diagnoses what the problem is; the point of the 
 > extension is that certain declarations that don't declare anything do, in 
 > fact, declare nested anonymous struct elements.  The PR asks for a hard 
 > error (with a more sensible error message).
 
 Ah, ok. Thanks for clarifying this (seems I am not familiar enough with 
 the subtleties of gnu extensions). I just checked, and 3.4 with -std=gnu99 
 accepts the code without any warnings, indeed.
 
 > What about more complicated cases, such as:
 > 
 > struct s {
 >   struct {
 >     int a;
 >     struct {
 >       int b;
 >     };
 >   };
 >   struct {
 >     int b;
 >     int c;
 >   };
 > };
 > 
 > (where the duplicate "b" elements should be diagnosed)?
 
 The same, it isn't.
 
 > >     gcc3.2 is quiet. If compiled in C++ mode, we get this:
 > >     g/x> /home/bangerth/bin/gcc-3.4-pre/bin/gcc -W -Wall -c x.cc
 > >     x.cc:3: error: declaration of `int s::<anonymous struct>::x'
 > >     x.cc:2: error: conflicts with previous declaration `int s::x'
 > >     x.cc:3: error: duplicate member `s::<anonymous struct>::x'
 > >     
 > >     Joseph, is this approximately what you wanted gcc to
 > >     tell us?
 > 
 > What the C++ compiler does isn't part of the PR [...]
 
 Sorry, I wasn't clear enough. I had wanted to know whether this is what 
 you would also like the C frontend to issue. You answered this already.
 
 Thanks for clarifying matters
   Wolfgang
 
 
 PS: Joseph, I have an unrelated favor that I'd like to ask you for: you 
 have a lot of reports where you point to some mail or a testcase. Figuring 
 out what exactly is the claim of this report can sometimes be tedious, and 
 leads to situations like the one above where I missed that this is 
 actually a gnu extension. It would be much simpler if you could copy and 
 paste a small example program from the cited sources into the PR (for the 
 present one, there was a one-liner in the message you pointed to, but no 
 command line or mention of gnu99), and a short description (3-5 lines) of 
 what you expect with what command line. -- Thanks!
 
 -------------------------------------------------------------------------
 Wolfgang Bangerth             email:            bangerth@ticam.utexas.edu
                               www: http://www.ticam.utexas.edu/~bangerth/
Comment 4 Joseph S. Myers 2003-03-07 09:51:25 UTC
From: "Joseph S. Myers" <jsm28@cam.ac.uk>
To: <bangerth@dealii.org>,  <gcc-bugs@gcc.gnu.org>, 
    Joseph Myers <jsm28@cam.ac.uk>,  <gcc-gnats@gcc.gnu.org>
Cc:  
Subject: Re: c/4784: Anonymous structs issues
Date: Fri, 7 Mar 2003 09:51:25 +0000 (GMT)

 On 7 Mar 2003 bangerth@dealii.org wrote:
 
 >     Hm, with 3.3 and present mainline, we get on this code
 >     -----------------------
 >     struct s {
 >         int x;
 >         struct { int x; };
 >     };
 >     ------------------
 >     the following message:
 >     g/x> /home/bangerth/bin/gcc-3.4-pre/bin/gcc -W -Wall -std=c99 -c x.c
 >     x.c:3: warning: declaration does not declare anything
 
 What's relevant is -std=gnu99 and -std=gnu89, as this is a GNU extension.
 That warning hardly diagnoses what the problem is; the point of the 
 extension is that certain declarations that don't declare anything do, in 
 fact, declare nested anonymous struct elements.  The PR asks for a hard 
 error (with a more sensible error message).  What about more complicated 
 cases, such as:
 
 struct s {
   struct {
     int a;
     struct {
       int b;
     };
   };
   struct {
     int b;
     int c;
   };
 };
 
 (where the duplicate "b" elements should be diagnosed)?
 
 >     gcc3.2 is quiet. If compiled in C++ mode, we get this:
 >     g/x> /home/bangerth/bin/gcc-3.4-pre/bin/gcc -W -Wall -c x.cc
 >     x.cc:3: error: declaration of `int s::<anonymous struct>::x'
 >     x.cc:2: error: conflicts with previous declaration `int s::x'
 >     x.cc:3: error: duplicate member `s::<anonymous struct>::x'
 >     
 >     Joseph, is this approximately what you wanted gcc to
 >     tell us?
 
 What the C++ compiler does isn't part of the PR (C++ has its own rules;  
 this is a C extension), but those error messages are the sort of error I
 think the C front end ought to be giving: they accurately diagnose what
 the problem in the example code is.
 
 -- 
 Joseph S. Myers
 jsm28@cam.ac.uk
 

Comment 5 Andrew Pinski 2003-06-07 20:59:21 UTC
This is a bug because gcc should error that x is already declared.
Comment 6 Joseph S. Myers 2010-01-26 16:33:00 UTC
C1x anonymous structures and unions will likely require a fix for this.
Comment 7 Joseph S. Myers 2010-05-09 16:20:05 UTC
Subject: Bug 4784

Author: jsm28
Date: Sun May  9 16:19:28 2010
New Revision: 159204

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=159204
Log:
	PR c/4784
	* c-decl.c (detect_field_duplicates_hash): New.  Handle anonymous
	structures and unions recursively.
	(detect_field_duplicates): Move duplicate detection with a hash to
	detect_field_duplicates_hash.  Always use a hash if anonymous
	structures or unions are present.
	* doc/extend.texi (Unnamed Fields): Document that duplicate fields
	give errors.

testsuite:
	* gcc.dg/anon-struct-9.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/anon-struct-9.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-decl.c
    trunk/gcc/doc/extend.texi
    trunk/gcc/testsuite/ChangeLog

Comment 8 Joseph S. Myers 2010-05-09 16:21:58 UTC
Fixed for 4.6.
Comment 9 Joseph S. Myers 2011-02-07 02:00:15 UTC
*** Bug 47376 has been marked as a duplicate of this bug. ***