This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[C++ PATCH] Fix aliasing warnings for typeid (PR c++/32260)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Mark Mitchell <mark at codesourcery dot com>, Jason Merrill <jason at redhat dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Thu, 1 Nov 2007 08:25:53 -0400
- Subject: [C++ PATCH] Fix aliasing warnings for typeid (PR c++/32260)
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
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))
+ {
+ tree type_info_type = TYPE_MAIN_VARIANT (const_type_info_type_node);
+ TYPE_ALIAS_SET (pseudo_type_info) = get_alias_set (type_info_type);
+ }
+
return true;
}
--- gcc/testsuite/g++.dg/rtti/typeid7.C.jj 2007-11-01 13:07:13.000000000 +0100
+++ gcc/testsuite/g++.dg/rtti/typeid7.C 2007-11-01 13:05:16.000000000 +0100
@@ -0,0 +1,61 @@
+// PR c++/32260
+// { dg-do compile }
+// { dg-options "-O2 -W -Wall" }
+
+#include <typeinfo>
+
+const std::type_info &
+f1 (int i)
+{
+ return typeid (i + 1);
+}
+
+const std::type_info &
+f2 ()
+{
+ return typeid (int);
+}
+
+struct A
+{
+ A ();
+ virtual ~A ();
+ void foo ();
+};
+
+const std::type_info &
+f3 ()
+{
+ return typeid (A);
+}
+
+const std::type_info &
+f4 (A *p)
+{
+ return typeid (*p);
+}
+
+const std::type_info &
+f5 ()
+{
+ return typeid (int *);
+}
+
+const std::type_info &
+f6 ()
+{
+ return typeid (int [26][12]);
+}
+
+const std::type_info &
+f7 ()
+{
+ return typeid (int [26][12]);
+}
+
+void (A::*pmr) ();
+const std::type_info &
+f8 ()
+{
+ return typeid (pmr);
+}
Jakub