Disallow redefinition of variably modified typedefs
Richard Guenther
richard.guenther@gmail.com
Sat Mar 19 12:20:00 GMT 2011
On Sat, Mar 19, 2011 at 12:29 AM, Joseph S. Myers
<joseph@codesourcery.com> wrote:
> This week's London WG14 meeting decided that typedef redefinition
> should not be allowed in the case of variably modified types. This
> patch implements this, giving an error for such redefinitions.
>
> Bootstrapped with no regressions on x86_64-unknown-linux-gnu. Applied
> to mainline. Any comments from other RMs about whether this should go
> in 4.6 as well, to avoid 4.6.0 being more lenient in this area than
> both 4.5 and 4.7? This is much the same issue as for anonymous
> structs and unions, but for a much more obscure case.
>
> (These two patches cover the only two cases of C1X features that were
> fully implemented for 4.6 according to the pre-London draft, and then
> changed in London. The post-London document will be going to
> enquiry-stage ITTF/JTC1 ballot so if that passes there should be no
> more technical changes before publication.)
I think it should go into 4.6.0 as well.
Thanks,
Richard.
> 2011-03-18 Joseph Myers <joseph@codesourcery.com>
>
> * c-decl.c (diagnose_mismatched_decls): Give an error for
> redefining a typedef with variably modified type.
>
> testsuite:
> 2011-03-18 Joseph Myers <joseph@codesourcery.com>
>
> * gcc.dg/c1x-typedef-1.c: Expect errors for redefinitions of
> variably modified typedefs.
> * gcc.dg/c1x-typedef-2.c: Remove.
>
> Index: testsuite/gcc.dg/c1x-typedef-1.c
> ===================================================================
> --- testsuite/gcc.dg/c1x-typedef-1.c (revision 171110)
> +++ testsuite/gcc.dg/c1x-typedef-1.c (working copy)
> @@ -3,7 +3,8 @@
> /* { dg-options "-std=c1x -pedantic-errors" } */
>
> /* C1X permits typedefs to be redeclared to the same type, but not to
> - different-but-compatible types. */
> + different-but-compatible types, and not when the type is variably
> + modified. */
>
> #include <limits.h>
>
> @@ -60,9 +61,10 @@ f (void)
> typedef void FN2(int (*p)[*]); /* { dg-message "previous declaration" } */
> typedef void FN2(int (*p)[]); /* { dg-error "with different type" } */
> typedef int AV[a]; /* { dg-message "previous declaration" } */
> - typedef int AV[b-1]; /* { dg-warning "may be a constraint violation at runtime" } */
> - typedef int AAa[a];
> + typedef int AV[b-1]; /* { dg-error "redefinition" } */
> + typedef int AAa[a]; /* { dg-message "previous declaration" } */
> typedef int AAb[b-1];
> typedef AAa *VF(void); /* { dg-message "previous declaration" } */
> - typedef AAb *VF(void); /* { dg-warning "may be a constraint violation at runtime" } */
> + typedef AAb *VF(void); /* { dg-error "redefinition" } */
> + typedef AAa AAa; /* { dg-error "redefinition" } */
> }
> Index: testsuite/gcc.dg/c1x-typedef-2.c
> ===================================================================
> --- testsuite/gcc.dg/c1x-typedef-2.c (revision 171110)
> +++ testsuite/gcc.dg/c1x-typedef-2.c (working copy)
> @@ -1,18 +0,0 @@
> -/* Test typedef redeclaration in C1X. Side effects from duplicate
> - declarations still apply. */
> -/* { dg-do run } */
> -/* { dg-options "-std=c1x -pedantic-errors" } */
> -
> -extern void exit (int);
> -extern void abort (void);
> -
> -int
> -main (void)
> -{
> - int a = 1, b = 1;
> - typedef int T[++a]; /* { dg-message "previous declaration" } */
> - typedef int T[++b]; /* { dg-warning "may be a constraint violation at runtime" } */
> - if (a != 2 || b != 2)
> - abort ();
> - exit (0);
> -}
> Index: c-decl.c
> ===================================================================
> --- c-decl.c (revision 171110)
> +++ c-decl.c (working copy)
> @@ -1813,20 +1813,16 @@ diagnose_mismatched_decls (tree newdecl,
> || TREE_NO_WARNING (olddecl))
> return true; /* Allow OLDDECL to continue in use. */
>
> - if (pedantic && !flag_isoc1x)
> + if (variably_modified_type_p (newtype, NULL))
> {
> - pedwarn (input_location, OPT_pedantic,
> - "redefinition of typedef %q+D", newdecl);
> + error ("redefinition of typedef %q+D with variably modified type",
> + newdecl);
> locate_old_decl (olddecl);
> }
> - else if (variably_modified_type_p (newtype, NULL))
> + else if (pedantic && !flag_isoc1x)
> {
> - /* Whether there is a constraint violation for the types not
> - being the same cannot be determined at compile time; a
> - warning that there may be one at runtime is considered
> - appropriate (WG14 reflector message 11743, 8 May 2009). */
> - warning (0, "redefinition of typedef %q+D may be a constraint "
> - "violation at runtime", newdecl);
> + pedwarn (input_location, OPT_pedantic,
> + "redefinition of typedef %q+D", newdecl);
> locate_old_decl (olddecl);
> }
>
>
> --
> Joseph S. Myers
> joseph@codesourcery.com
>
More information about the Gcc-patches
mailing list