This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C++ PATCH] Fix static_cast overflow handling (PR c++/23056) (take 2)


On Mon, Sep 05, 2005 at 02:30:23PM +0200, Giovanni Bajo wrote:
> > Ok to commit for HEAD/4.0 (4.0/4.1 regression) if bootstrap/regtesting
> > succeeds?
> 
> Surely you can factor out the new code into a function, to avoid
> duplication?

Sure:

2005-09-05  Jakub Jelinek  <jakub@redhat.com>

	PR c++/23056
	* typeck.c (ignore_static_cast_overflows): New helper function.
	(build_static_cast_1): Use it.

	* g++.dg/opt/pr23056.C: New test.

--- gcc/cp/typeck.c.jj	2005-09-05 11:44:02.000000000 +0200
+++ gcc/cp/typeck.c	2005-09-05 14:56:40.000000000 +0200
@@ -4526,6 +4526,36 @@ convert_ptrmem (tree type, tree expr, bo
 			     allow_inverse_p, c_cast_p);
 }
 
+/* Ignore any integer overflow caused by the static cast of ORIG
+   to EXPR.  */
+
+static tree
+ignore_static_cast_overflows (tree expr, tree orig)
+{
+  if (TREE_CODE (expr) == INTEGER_CST
+      && CONSTANT_CLASS_P (orig)
+      && TREE_CODE (orig) != STRING_CST
+      && (TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig)
+	  || TREE_CONSTANT_OVERFLOW (expr)
+	     != TREE_CONSTANT_OVERFLOW (orig)))
+    {
+      if (!TREE_OVERFLOW (orig) && !TREE_CONSTANT_OVERFLOW (orig))
+	/* Ensure constant sharing.  */
+	expr = build_int_cst_wide (TREE_TYPE (expr),
+				   TREE_INT_CST_LOW (expr),
+				   TREE_INT_CST_HIGH (expr));
+      else
+	{
+	  /* Avoid clobbering a shared constant.  */
+	  expr = copy_node (expr);
+	  TREE_OVERFLOW (expr) = TREE_OVERFLOW (orig);
+	  TREE_CONSTANT_OVERFLOW (expr)
+	    = TREE_CONSTANT_OVERFLOW (orig);
+	}
+    }
+  return expr;
+}
+
 /* Perform a static_cast from EXPR to TYPE.  When C_CAST_P is true,
    this static_cast is being attempted as one of the possible casts
    allowed by a C-style cast.  (In that case, accessibility of base
@@ -4629,13 +4659,8 @@ build_static_cast_1 (tree type, tree exp
       result = convert_from_reference (result);
 
       /* Ignore any integer overflow caused by the cast.  */
-      if (TREE_CODE (result) == INTEGER_CST
-	  && CONSTANT_CLASS_P (orig))
-	{
-	  TREE_OVERFLOW (result) = TREE_OVERFLOW (orig);
-	  TREE_CONSTANT_OVERFLOW (result)
-	    = TREE_CONSTANT_OVERFLOW (orig);
-	}
+      result = ignore_static_cast_overflows (result, orig);
+
       /* [expr.static.cast]
 
 	 If T is a reference type, the result is an lvalue; otherwise,
@@ -4678,12 +4703,7 @@ build_static_cast_1 (tree type, tree exp
       expr = ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL);
 
       /* Ignore any integer overflow caused by the cast.  */
-      if (TREE_CODE (expr) == INTEGER_CST
-	  && CONSTANT_CLASS_P (orig))
-	{
-	  TREE_OVERFLOW (expr) = TREE_OVERFLOW (orig);
-	  TREE_CONSTANT_OVERFLOW (expr) = TREE_CONSTANT_OVERFLOW (orig);
-	}
+      expr = ignore_static_cast_overflows (expr, orig);
       return expr;
     }
 
--- gcc/testsuite/g++.dg/opt/pr23056.C.jj	2005-09-05 12:57:04.000000000 +0200
+++ gcc/testsuite/g++.dg/opt/pr23056.C	2005-09-05 12:56:21.000000000 +0200
@@ -0,0 +1,9 @@
+// PR c++/23056
+// { dg-do compile }
+
+template <bool T> struct S { virtual ~S(); };
+void foo ()
+{
+  static_cast<bool>("Foo");
+}
+S<true> a;


	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]