[C++ PATCH] Fix aliasing warnings for typeid (PR c++/32260)

Richard Guenther richard.guenther@gmail.com
Thu Nov 1 12:53:00 GMT 2007


On 11/1/07, Jakub Jelinek <jakub@redhat.com> wrote:
> Hi!
>
> -O2 -Wstrict-aliasing currently warns about many uses of typeid ().
> THe problem is that the various artifical abi::__*info types have
> abi::__type_info_pseudo rather than std::type_info as their first
> field, although they really have the same layout.  So typeid casts
> the address of say abi::__fundamental_type_info _ZTI* var to std::type_info *
> and dereferences it, but the two have different alias sets and so
> -Wstrict-aliasing warns.  We can't use std::type_info as the first
> field of the derived types, as it is not a builtin type and we rely
> on <typeinfo> to define it, but we surely can use the same alias
> set for std::type_info and abi::__type_info_pseudo (and therefore
> aliasing will be ok even for all the "derived" types, as they have
> abi::__type_info_pseudo as the first field).  Nothing ever needs
> to create alias sets for the abi::__* types before
> {build,get}_typeid is called or emit_tinfo_decl is called
> (they are touched only during the initialization otherwise).
> {build,get}_typeid in typeid_ok_p ensure <typeinfo> has been included
> (std::type_info is complete) and we can there copy the alias set
> of std::type_info to abi:__fundamental_type_info when we call it the first
> time.  emit_tinfo_decl is only called at the end and if there were no
> typeids parsed, the alias set really doesn't matter.
>
> Ok for trunk?
>
> 2007-11-01  Jakub Jelinek  <jakub@redhat.com>
>
>         PR c++/32260
>         * rtti.c (enum_tinfo_kind): Fix TK_TYPE_INFO_TYPE comment.
>         (typeid_ok_p): Use the same alias set for abi::__type_info_pseudo
>         as for std::type_info.
>
>         * g++.dg/rtti/typeid7.C: New test.
>
> --- gcc/cp/rtti.c.jj    2007-09-07 10:29:32.000000000 +0200
> +++ gcc/cp/rtti.c       2007-11-01 12:55:34.000000000 +0100
> @@ -79,7 +79,7 @@ DEF_VEC_ALLOC_O(tinfo_s,gc);
>
>  typedef enum tinfo_kind
>  {
> -  TK_TYPE_INFO_TYPE,    /* std::type_info */
> +  TK_TYPE_INFO_TYPE,    /* abi::__type_info_pseudo */
>    TK_BASE_TYPE,                /* abi::__base_class_type_info */
>    TK_BUILTIN_TYPE,     /* abi::__fundamental_type_info */
>    TK_ARRAY_TYPE,       /* abi::__array_type_info */
> @@ -264,6 +264,8 @@ get_tinfo_decl_dynamic (tree exp)
>  static bool
>  typeid_ok_p (void)
>  {
> +  tree pseudo_type_info;
> +
>    if (! flag_rtti)
>      {
>        error ("cannot use typeid with -fno-rtti");
> @@ -276,6 +278,17 @@ typeid_ok_p (void)
>        return false;
>      }
>
> +  pseudo_type_info
> +    = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE)->type;
> +
> +  /* Make sure abi::__type_info_pseudo has the same alias set
> +     as std::type_info.  */
> +  if (! TYPE_ALIAS_SET_KNOWN_P (pseudo_type_info))
> +    {

The comment hints at that this should be

       gcc_assert (! TYPE_ALIAS_SET_KNOWN_P (pseudo_type_info));

or does this break at any time?

Richard.



More information about the Gcc-patches mailing list