From: Joseph Myers Date: Fri, 24 Sep 2004 17:22:20 +0000 (+0100) Subject: re PR c/17188 (struct Foo { } redefinition) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=085e33aa0ad674c2680e6216b7d84737b935351c;p=gcc.git re PR c/17188 (struct Foo { } redefinition) 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. From-SVN: r88063 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index af1d69f58ed..e522714f97b 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,13 @@ +2004-09-24 Joseph S. Myers + + 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. + 2004-09-24 Devang Patel * tree-if-conv.c (tree_if_convert_cond_expr0: Create temp. variable diff --git a/gcc/c-decl.c b/gcc/c-decl.c index 98f1580ed5f..3a2f5763b13 100644 --- a/gcc/c-decl.c +++ b/gcc/c-decl.c @@ -1145,6 +1145,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 %qD", newdecl, newdecl); + locate_old_decl (olddecl, error); + return false; + } + if (!comptypes (oldtype, newtype)) { if (TREE_CODE (olddecl) == FUNCTION_DECL @@ -4910,13 +4919,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 %", IDENTIFIER_POINTER (name)); else error ("redefinition of %", IDENTIFIER_POINTER (name)); } + else if (C_TYPE_BEING_DEFINED (ref)) + { + if (code == UNION_TYPE) + error ("nested redefinition of %", + IDENTIFIER_POINTER (name)); + else + error ("nested redefinition of %", + IDENTIFIER_POINTER (name)); + } } else { @@ -5123,11 +5141,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 %qs", - IDENTIFIER_POINTER (TYPE_NAME (t))); - if (DECL_INITIAL (x)) { unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1); @@ -5330,6 +5343,9 @@ start_enum (tree name) pushtag (name, enumtype); } + if (C_TYPE_BEING_DEFINED (enumtype)) + error ("nested redefinition of %", 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 2dcc996052e..9787c4d2c61 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2004-09-24 Joseph S. Myers + + PR c/17188 + * gcc.dg/nested-redef-1.c, gcc.dg/pr17188-1.c: New tests. + * gcc.dg/decl-3.c: Adjust expected message. + 2004-09-24 Tobias Schlueter PR fortran/17615 diff --git a/gcc/testsuite/gcc.dg/decl-3.c b/gcc/testsuite/gcc.dg/decl-3.c index 2bfac89c136..5bbe19cd224 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 new file mode 100644 index 00000000000..34b92d8f571 --- /dev/null +++ b/gcc/testsuite/gcc.dg/nested-redef-1.c @@ -0,0 +1,44 @@ +/* Test diagnosis of nested tag redefinitions. */ +/* Origin: Joseph Myers */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +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'" } */ +}; + +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'" } */ + } y; + } z; +}; + +struct s6; +struct s6 { struct s6 *p; }; + +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'" } */ +}; + +enum e1 { + E2 = sizeof(enum e2 { E2 }), /* { dg-error "error: redeclaration of enumerator 'E2'" } */ + /* { dg-error "previous definition" "previous E2" { target *-*-* } 38 } */ + E3 +}; + +enum e3; +enum e3 { E4 = 0 }; diff --git a/gcc/testsuite/gcc.dg/pr17188-1.c b/gcc/testsuite/gcc.dg/pr17188-1.c new file mode 100644 index 00000000000..634e60c0363 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr17188-1.c @@ -0,0 +1,31 @@ +/* A redefinition of an empty struct should be diagnosed the same as a + redefinition of any other tag, but formerly only s2 and s4 were + diagnosed. Bug 17188. */ +/* Origin: Joseph Myers */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +struct s0 { }; +struct s0; +struct s0 { }; /* { dg-error "error: redefinition of 'struct s0'" } */ + +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 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 s5 { int a : 1; }; +struct s5; + +struct s6; +struct s6 { int a : 1; }; + +struct s7; +struct s7 { };