From ff8e21599213bbea6ccacecf2522ad19836c8fd1 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Fri, 18 Mar 2011 21:16:31 +0000 Subject: [PATCH] c-decl.c (grokfield): Don't allow typedefs for structures or unions with no tag by default. * c-decl.c (grokfield): Don't allow typedefs for structures or unions with no tag by default. * doc/extend.texi (Unnamed Fields): Update. testsuite: * gcc.dg/c1x-anon-struct-1.c: Don't test use of typedefs. * gcc.dg/c1x-anon-struct-3.c: New test. * gcc.dg/anon-struct-11.c: Update. From-SVN: r171170 --- gcc/ChangeLog | 6 +++++ gcc/c-decl.c | 7 +++-- gcc/doc/extend.texi | 5 ++-- gcc/testsuite/ChangeLog | 6 +++++ gcc/testsuite/gcc.dg/anon-struct-11.c | 4 +-- gcc/testsuite/gcc.dg/c1x-anon-struct-1.c | 25 +++++++++-------- gcc/testsuite/gcc.dg/c1x-anon-struct-3.c | 34 ++++++++++++++++++++++++ 7 files changed, 67 insertions(+), 20 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/c1x-anon-struct-3.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 762dbb4a45c..b6680a99ab5 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2011-03-18 Joseph Myers + + * c-decl.c (grokfield): Don't allow typedefs for structures or + unions with no tag by default. + * doc/extend.texi (Unnamed Fields): Update. + 2011-03-18 Uros Bizjak * config/i386/i386.md (float2): diff --git a/gcc/c-decl.c b/gcc/c-decl.c index b438b066c83..8f9c4440048 100644 --- a/gcc/c-decl.c +++ b/gcc/c-decl.c @@ -6674,11 +6674,14 @@ grokfield (location_t loc, || TREE_CODE (type) == UNION_TYPE); bool ok = false; - if (type_ok) + if (type_ok + && (flag_ms_extensions + || flag_plan9_extensions + || !declspecs->typedef_p)) { if (flag_ms_extensions || flag_plan9_extensions) ok = true; - else if (TYPE_NAME (TYPE_MAIN_VARIANT (type)) == NULL) + else if (TYPE_NAME (type) == NULL) ok = true; else ok = false; diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index eaad0892d9c..c8971012b4e 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -13352,12 +13352,11 @@ The compiler gives errors for such constructs. @opindex fms-extensions Unless @option{-fms-extensions} is used, the unnamed field must be a structure or union definition without a tag (for example, @samp{struct -@{ int a; @};}), or a @code{typedef} name for such a structure or -union. If @option{-fms-extensions} is used, the field may +@{ int a; @};}). If @option{-fms-extensions} is used, the field may also be a definition with a tag such as @samp{struct foo @{ int a; @};}, a reference to a previously defined structure or union such as @samp{struct foo;}, or a reference to a @code{typedef} name for a -previously defined structure or union type with a tag. +previously defined structure or union type. @opindex fplan9-extensions The option @option{-fplan9-extensions} enables diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 5c39684eaec..3cda22b33a2 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2011-03-18 Joseph Myers + + * gcc.dg/c1x-anon-struct-1.c: Don't test use of typedefs. + * gcc.dg/c1x-anon-struct-3.c: New test. + * gcc.dg/anon-struct-11.c: Update. + 2011-03-18 Rainer Orth * gcc.dg/vect/slp-multitypes-2.c: Replace dg-do run with diff --git a/gcc/testsuite/gcc.dg/anon-struct-11.c b/gcc/testsuite/gcc.dg/anon-struct-11.c index 1084e5bded6..c2f85fc24e4 100644 --- a/gcc/testsuite/gcc.dg/anon-struct-11.c +++ b/gcc/testsuite/gcc.dg/anon-struct-11.c @@ -50,7 +50,7 @@ struct E { struct F { char f; }; /* { dg-warning "does not declare anything" } */ char c; union { - D; + D; /* { dg-warning "does not declare anything" } */ }; char e; }; @@ -85,7 +85,7 @@ test2 (void) e.e = 5; f2 (&e); /* { dg-warning "incompatible pointer type" } */ f3 (&e); /* { dg-warning "incompatible pointer type" } */ - if (e.d != 4) + if (e.d != 4) /* { dg-error "no member" } */ abort (); if (e.f != 6) /* { dg-error "no member" } */ abort (); diff --git a/gcc/testsuite/gcc.dg/c1x-anon-struct-1.c b/gcc/testsuite/gcc.dg/c1x-anon-struct-1.c index 711fe65df3d..6d4b433d733 100644 --- a/gcc/testsuite/gcc.dg/c1x-anon-struct-1.c +++ b/gcc/testsuite/gcc.dg/c1x-anon-struct-1.c @@ -4,20 +4,13 @@ #include -typedef struct -{ - int i; -} s0; - -typedef union -{ - int i; -} u0; - struct s1 { int a; - u0; + union + { + int i; + }; struct { int b; @@ -27,7 +20,10 @@ struct s1 union u1 { int b; - s0; + struct + { + int i; + }; union { int c; @@ -44,7 +40,10 @@ struct s2 struct s3 { - u0; + union + { + int i; + }; }; struct s4 diff --git a/gcc/testsuite/gcc.dg/c1x-anon-struct-3.c b/gcc/testsuite/gcc.dg/c1x-anon-struct-3.c new file mode 100644 index 00000000000..1841eddd0a1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c1x-anon-struct-3.c @@ -0,0 +1,34 @@ +/* Test for anonymous structures and unions in C1X. Test for invalid + cases: typedefs disallowed by N1549. */ +/* { dg-do compile } */ +/* { dg-options "-std=c1x -pedantic-errors" } */ + +typedef struct +{ + int i; +} s0; + +typedef union +{ + int i; +} u0; + +struct s1 +{ + int a; + u0; /* { dg-error "declaration does not declare anything" } */ + struct + { + int b; + }; +}; + +union u1 +{ + int b; + s0; /* { dg-error "declaration does not declare anything" } */ + union + { + int c; + }; +}; -- 2.30.2