The standard is somewhat unclear here, but I think what makes sense is to always check whether the address is null rather than confine the check to when the immediate operand of typeid is an INDIRECT_REF.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit 43d5a64843e3e18a74e59b1b368cd129d5f1de88
Author: Jason Merrill <ja...@redhat.com>
Date:   Tue Apr 9 14:11:01 2013 -0400

    	PR c++/25466
    	* rtti.c (build_typeid): Check the address of the argument
    	rather than looking for an INDIRECT_REF.

diff --git a/gcc/cp/rtti.c b/gcc/cp/rtti.c
index e83d666..b3c6687 100644
--- a/gcc/cp/rtti.c
+++ b/gcc/cp/rtti.c
@@ -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
index 0000000..47b45b1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/rtti/typeid10.C
@@ -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();
+    }
+}

Reply via email to