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
This commit is contained in:
Joseph Myers 2004-09-24 18:22:20 +01:00 committed by Joseph Myers
parent a2159c4c35
commit 085e33aa0a
6 changed files with 114 additions and 7 deletions

View File

@ -1,3 +1,13 @@
2004-09-24 Joseph S. Myers <jsm@polyomino.org.uk>
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 <dpatel@apple.com>
* tree-if-conv.c (tree_if_convert_cond_expr0: Create temp. variable

View File

@ -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 %<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
{
@ -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 %<enum %s%>", IDENTIFIER_POINTER (name));
C_TYPE_BEING_DEFINED (enumtype) = 1;
if (TYPE_VALUES (enumtype) != 0)

View File

@ -1,3 +1,9 @@
2004-09-24 Joseph S. Myers <jsm@polyomino.org.uk>
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 <tobias.schlueter@physik.uni-muenchen.de>
PR fortran/17615

View File

@ -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" } */

View File

@ -0,0 +1,44 @@
/* Test diagnosis of nested tag redefinitions. */
/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
/* { 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 };

View File

@ -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 <jsm@polyomino.org.uk> */
/* { 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 { };