re PR c++/25466 (typeid expression fails to throw bad_typeid according to 5.2.8p2)
authorJason Merrill <jason@redhat.com>
Tue, 9 Apr 2013 18:11:38 +0000 (14:11 -0400)
committerJason Merrill <jason@gcc.gnu.org>
Tue, 9 Apr 2013 18:11:38 +0000 (14:11 -0400)
PR c++/25466
* rtti.c (build_typeid): Check the address of the argument
rather than looking for an INDIRECT_REF.

From-SVN: r197644

gcc/cp/ChangeLog
gcc/cp/rtti.c
gcc/testsuite/g++.dg/rtti/typeid10.C [new file with mode: 0644]

index 0604b55009291c3fa86019664138ea2f03086dc3..c01fb3cb43e563cbc65e06b67abdb6f2f48a5ac9 100644 (file)
@@ -1,3 +1,9 @@
+2013-04-09  Jason Merrill  <jason@redhat.com>
+
+       PR c++/25466
+       * rtti.c (build_typeid): Check the address of the argument
+       rather than looking for an INDIRECT_REF.
+
 2013-04-04  Jason Merrill  <jason@redhat.com>
 
        PR c++/56838
index e83d6666d9b14c373fe1634d2a3534da5b9646aa..b3c6687a75d95e65a95056a59ace6afd9bfd3b81 100644 (file)
@@ -326,18 +326,16 @@ build_typeid (tree exp, tsubst_flags_t complain)
 
   /* FIXME when integrating with c_fully_fold, mark
      resolves_to_fixed_type_p case as a non-constant expression.  */
-  if (INDIRECT_REF_P (exp)
-      && TYPE_PTR_P (TREE_TYPE (TREE_OPERAND (exp, 0)))
-      && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
+  if (TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
       && ! resolves_to_fixed_type_p (exp, &nonnull)
       && ! nonnull)
     {
       /* So we need to look into the vtable of the type of exp.
-         This is an lvalue use of expr then.  */
-      exp = mark_lvalue_use (exp);
+         Make sure it isn't a null lvalue.  */
+      exp = cp_build_addr_expr (exp, complain);
       exp = stabilize_reference (exp);
-      cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0),
-                        complain);
+      cond = cp_convert (boolean_type_node, exp, complain);
+      exp = cp_build_indirect_ref (exp, RO_NULL, complain);
     }
 
   exp = get_tinfo_decl_dynamic (exp, complain);
diff --git a/gcc/testsuite/g++.dg/rtti/typeid10.C b/gcc/testsuite/g++.dg/rtti/typeid10.C
new file mode 100644 (file)
index 0000000..47b45b1
--- /dev/null
@@ -0,0 +1,36 @@
+// PR c++/25466
+// { dg-do run }
+
+#include <typeinfo>
+
+const std::type_info *a;
+
+template <class T>
+bool is_polymorphic() {
+   bool result(false);
+   const std::type_info &a1 = typeid( (result=true), *(T*)0);
+   a = &a1;
+   return result;
+}
+
+struct non_polymorphic {};
+struct polymorphic { virtual ~polymorphic() {} };
+
+
+int main() {
+  if (is_polymorphic<int>()) __builtin_abort();
+  if (is_polymorphic<non_polymorphic>()) __builtin_abort();
+  try
+    {
+      is_polymorphic<polymorphic>();
+      __builtin_abort(); // should have thrown bad_typeid
+    }
+  catch (std::bad_typeid&)
+    {
+      // OK
+    }
+  catch (...)
+    {
+      __builtin_abort();
+    }
+}