From: Joseph Myers Date: Thu, 28 Jul 2005 23:01:29 +0000 (+0100) Subject: re PR c/17188 (struct Foo { } redefinition) X-Git-Tag: releases/gcc-3.4.5~297 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=58cbc1909f38378029c6eb27276598add066e479;p=thirdparty%2Fgcc.git re PR c/17188 (struct Foo { } redefinition) 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. From-SVN: r102526 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index c0b6b05577fd..dbae8f32256f 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,14 @@ +2005-07-28 Joseph S. Myers + + 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. + 2005-07-28 Joseph S. Myers PR c/21873 diff --git a/gcc/c-decl.c b/gcc/c-decl.c index 8d717733ddc3..4eca4f26dc7e 100644 --- a/gcc/c-decl.c +++ b/gcc/c-decl.c @@ -955,6 +955,15 @@ diagnose_mismatched_decls (tree newdecl, tree olddecl, return false; } + /* Enumerators have no linkage, so may only be declared once in a + given scope. */ + if (TREE_CODE (olddecl) == CONST_DECL) + { + error ("%Jredeclaration of enumerator `%D'", newdecl, newdecl); + locate_old_decl (olddecl, error); + return false; + } + if (!comptypes (oldtype, newtype, COMPARE_STRICT)) { if (TREE_CODE (olddecl) == FUNCTION_DECL @@ -4779,13 +4788,22 @@ start_struct (enum tree_code code, tree name) ref = lookup_tag (code, name, 1); if (ref && TREE_CODE (ref) == code) { - if (TYPE_FIELDS (ref)) + if (TYPE_SIZE (ref)) { if (code == UNION_TYPE) error ("redefinition of `union %s'", IDENTIFIER_POINTER (name)); else error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name)); } + else if (C_TYPE_BEING_DEFINED (ref)) + { + if (code == UNION_TYPE) + error ("nested redefinition of `union %s'", + IDENTIFIER_POINTER (name)); + else + error ("nested redefinition of `struct %s'", + IDENTIFIER_POINTER (name)); + } } else { @@ -5000,11 +5018,6 @@ finish_struct (tree t, tree fieldlist, tree attributes) if (C_DECL_VARIABLE_SIZE (x)) C_TYPE_VARIABLE_SIZE (t) = 1; - /* Detect invalid nested redefinition. */ - if (TREE_TYPE (x) == t) - error ("nested redefinition of `%s'", - IDENTIFIER_POINTER (TYPE_NAME (t))); - if (DECL_INITIAL (x)) { unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1); @@ -5202,6 +5215,9 @@ start_enum (tree name) pushtag (name, enumtype); } + if (C_TYPE_BEING_DEFINED (enumtype)) + error ("nested redefinition of `enum %s'", IDENTIFIER_POINTER (name)); + C_TYPE_BEING_DEFINED (enumtype) = 1; if (TYPE_VALUES (enumtype) != 0) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index b1df1c2a6410..6612dedf20c1 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2005-07-28 Joseph S. Myers + + PR c/17188 + PR c/21899 + * gcc.dg/nested-redef-1.c, gcc.dg/pr17188-1.c: New tests. + * gcc.dg/decl-3.c: Adjust expected message. + 2005-07-28 Joseph S. Myers PR c/21873 diff --git a/gcc/testsuite/gcc.dg/decl-3.c b/gcc/testsuite/gcc.dg/decl-3.c index 2bfac89c1360..5bbe19cd2248 100644 --- a/gcc/testsuite/gcc.dg/decl-3.c +++ b/gcc/testsuite/gcc.dg/decl-3.c @@ -2,4 +2,4 @@ /* { dg-do compile } */ enum { CODES }; /* { dg-error "previous definition" } */ -enum { CODES }; /* { dg-error "conflicting types" } */ +enum { CODES }; /* { dg-error "conflicting types|redeclaration of enumerator" } */ diff --git a/gcc/testsuite/gcc.dg/nested-redef-1.c b/gcc/testsuite/gcc.dg/nested-redef-1.c index 34b92d8f571d..0aeda61451f8 100644 --- a/gcc/testsuite/gcc.dg/nested-redef-1.c +++ b/gcc/testsuite/gcc.dg/nested-redef-1.c @@ -4,21 +4,21 @@ /* { dg-options "" } */ struct s0 { - struct s0 { int a; } x; /* { dg-error "error: nested redefinition of 'struct s0'" } */ + struct s0 { int a; } x; /* { dg-error "error: nested redefinition of `struct s0'" } */ }; struct s1 { - const struct s1 { int b; } x; /* { dg-error "error: nested redefinition of 'struct s1'" } */ + const struct s1 { int b; } x; /* { dg-error "error: nested redefinition of `struct s1'" } */ }; struct s2 { - struct s2 { int c; } *x; /* { dg-error "error: nested redefinition of 'struct s2'" } */ + struct s2 { int c; } *x; /* { dg-error "error: nested redefinition of `struct s2'" } */ }; struct s3 { struct s4 { struct s5 { - struct s3 { int a; } **x; /* { dg-error "error: nested redefinition of 'struct s3'" } */ + struct s3 { int a; } **x; /* { dg-error "error: nested redefinition of `struct s3'" } */ } y; } z; }; @@ -27,15 +27,15 @@ struct s6; struct s6 { struct s6 *p; }; union u0 { - union u0 { int c; } *x; /* { dg-error "error: nested redefinition of 'union u0'" } */ + union u0 { int c; } *x; /* { dg-error "error: nested redefinition of `union u0'" } */ }; enum e0 { - E0 = sizeof(enum e0 { E1 }) /* { dg-error "error: nested redefinition of 'enum e0'" } */ + E0 = sizeof(enum e0 { E1 }) /* { dg-error "error: nested redefinition of `enum e0'" } */ }; enum e1 { - E2 = sizeof(enum e2 { E2 }), /* { dg-error "error: redeclaration of enumerator 'E2'" } */ + E2 = sizeof(enum e2 { E2 }), /* { dg-error "error: redeclaration of enumerator `E2'" } */ /* { dg-error "previous definition" "previous E2" { target *-*-* } 38 } */ E3 }; diff --git a/gcc/testsuite/gcc.dg/pr17188-1.c b/gcc/testsuite/gcc.dg/pr17188-1.c index 634e60c03630..86b7ff3b43c7 100644 --- a/gcc/testsuite/gcc.dg/pr17188-1.c +++ b/gcc/testsuite/gcc.dg/pr17188-1.c @@ -7,19 +7,19 @@ struct s0 { }; struct s0; -struct s0 { }; /* { dg-error "error: redefinition of 'struct s0'" } */ +struct s0 { }; /* { dg-error "error: redefinition of `struct s0'" } */ struct s1 { }; -struct s1 { }; /* { dg-error "error: redefinition of 'struct s1'" } */ +struct s1 { }; /* { dg-error "error: redefinition of `struct s1'" } */ struct s2 { int a : 1; }; -struct s2 { int a : 1; }; /* { dg-error "error: redefinition of 'struct s2'" } */ +struct s2 { int a : 1; }; /* { dg-error "error: redefinition of `struct s2'" } */ struct s3 { }; -struct s3 { int a : 1; }; /* { dg-error "error: redefinition of 'struct s3'" } */ +struct s3 { int a : 1; }; /* { dg-error "error: redefinition of `struct s3'" } */ struct s4 { int a : 1; }; -struct s4 { }; /* { dg-error "error: redefinition of 'struct s4'" } */ +struct s4 { }; /* { dg-error "error: redefinition of `struct s4'" } */ struct s5 { int a : 1; }; struct s5;