Bug 17188 - struct Foo { } redefinition
Summary: struct Foo { } redefinition
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.4.0
: P2 minor
Target Milestone: 3.4.5
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2004-08-25 18:27 UTC by M Welinder
Modified: 2005-07-28 23:10 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 2.95.3 3.0.4 3.2.3 3.3.3 3.4.0 4.0.0
Last reconfirmed: 2004-09-17 02:28:42


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description M Welinder 2004-08-25 18:27:18 UTC
When an empty structure or union is defined twice, no error is given.

struct Foo { };
struct Foo { };

> gcc-3.4 -std=c99 -pedantic -Wall ~/foo.c
/home/welinder/foo.c:1: warning: struct has no members
/home/welinder/foo.c:2: warning: struct has no members

If I add members, say "int : 1;", I get 

struct Foo { int : 1; };
struct Foo { int : 1; };

> gcc-3.4 -std=c99 -pedantic -Wall ~/foo.c
/home/welinder/foo.c:1: warning: struct has no named members
/home/welinder/foo.c:2: error: redefinition of `struct Foo'
/home/welinder/foo.c:2: warning: struct has no named members


And the mid-point would be...

struct Foo { };
struct Foo { int : 1; };
> gcc-3.4 -std=c99 -pedantic -Wall ~/foo.c
/home/welinder/foo.c:1: warning: struct has no members
/home/welinder/foo.c:2: warning: struct has no named members


Note, that empty structures is a gcc extension.
Comment 1 Andrew Pinski 2004-08-25 18:41:57 UTC
I don't know about the GCC extension that well but it looks to me that an empty struct is acting as the 
same as forward declaring the struct.
Comment 2 Wolfgang Bangerth 2004-08-25 22:41:40 UTC
That would be completely bogus. I hope gcc doesn't do this. It's clearly 
a bug. 
 
W. 
 
Comment 3 Andrew Pinski 2004-08-25 23:32:40 UTC
Not  if it was not on purpose as an extension of C which is tottally likely because C does not have 
empty structs.
Comment 4 Giovanni Bajo 2004-09-17 02:28:42 UTC
Confirmed, not a regression (assuming the bug report is valid in the first 
place - I don't know C standard that well):
Comment 5 Andrew Pinski 2004-09-17 03:14:59 UTC
Since empty structs are extensions, this would then need to be invalid GNU C which it might mean 
anything then (and it might in fact be right to accept it).
Comment 6 GCC Commits 2004-09-24 17:22:28 UTC
Subject: Bug 17188

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	jsm28@gcc.gnu.org	2004-09-24 17:22:20

Modified files:
	gcc            : ChangeLog c-decl.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/gcc.dg: decl-3.c 
Added files:
	gcc/testsuite/gcc.dg: nested-redef-1.c pr17188-1.c 

Log message:
	PR c/17188
	* c-decl.c (diagnose_mismatched_decls): Check for duplicate
	declarations of enumerators.
	(start_struct): Check TYPE_SIZE rather than TYPE_FIELDS to check
	for redefinition.  Check for nested redefinition.
	(finish_struct): Don't check for nested redefinition.
	(start_enum): Check for nested redefinition.
	
	testsuite:
	* gcc.dg/nested-redef-1.c, gcc.dg/pr17188-1.c: New tests.
	* gcc.dg/decl-3.c: Adjust expected message.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.5603&r2=2.5604
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-decl.c.diff?cvsroot=gcc&r1=1.588&r2=1.589
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.4344&r2=1.4345
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/nested-redef-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/pr17188-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/decl-3.c.diff?cvsroot=gcc&r1=1.2&r2=1.3

Comment 7 Andrew Pinski 2004-09-24 17:34:53 UTC
Fixed.
Comment 8 GCC Commits 2005-07-28 23:02:44 UTC
Subject: Bug 17188

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_4-branch
Changes by:	jsm28@gcc.gnu.org	2005-07-28 23:01:31

Modified files:
	gcc            : ChangeLog c-decl.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/gcc.dg: decl-3.c 
Added files:
	gcc/testsuite/gcc.dg: nested-redef-1.c pr17188-1.c 

Log message:
	PR c/17188
	PR c/21899
	* c-decl.c (diagnose_mismatched_decls): Check for duplicate
	declarations of enumerators.
	(start_struct): Check TYPE_SIZE rather than TYPE_FIELDS to check
	for redefinition.  Check for nested redefinition.
	(finish_struct): Don't check for nested redefinition.
	(start_enum): Check for nested redefinition.
	
	testsuite:
	* gcc.dg/nested-redef-1.c, gcc.dg/pr17188-1.c: New tests.
	* gcc.dg/decl-3.c: Adjust expected message.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=2.2326.2.889&r2=2.2326.2.890
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-decl.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.470.4.20&r2=1.470.4.21
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.3389.2.414&r2=1.3389.2.415
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/nested-redef-1.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=NONE&r2=1.1.60.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/pr17188-1.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=NONE&r2=1.1.60.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/decl-3.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.2&r2=1.2.4.1

Comment 9 Joseph S. Myers 2005-07-28 23:10:15 UTC
Patch backported to 3.4 branch since bug 21899 showed some cases were regressions.