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]

[tree-ssa] Revised patch for PR 14730


Having giving it some more thought, I've added warnings.
Bootstrapped and GCC testsuite completes without regressions.

Okay to commit?

Ben


2004-04-19  Ben Elliston  <bje@au.ibm.com>

	PR middle-end/14730
	* expr.c (expand_expr_real_1) <SWITCH_EXPR>: Discard out of bounds
	case label values and ranges.  Saturate case range values that
	exceed the minimum or maximum permitted value for the controlling
	expression type to TYPE_MIN_VALUE or TYPE_MAX_VALUE.

[testsuite ChangeLog]
2004-04-19  Ben Elliston  <bje@au.ibm.com>

	PR middle-end/14730
	* gcc.c-torture/compile/pr14730.c: New test.

Index: gcc/expr.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/expr.c,v
retrieving revision 1.467.2.86
diff -u -p -r1.467.2.86 expr.c
--- gcc/expr.c	13 Apr 2004 22:33:25 -0000	1.467.2.86
+++ gcc/expr.c	19 Apr 2004 03:41:58 -0000
@@ -9294,8 +9294,52 @@ expand_expr_real_1 (tree exp, rtx target
 	  for (i = 0; i < n; ++i)
 	    {
 	      tree elt = TREE_VEC_ELT (vec, i);
-	      add_case_node (CASE_LOW (elt), CASE_HIGH (elt),
-			     CASE_LABEL (elt), &duplicate, true);
+	      tree controlling_expr_type = TREE_TYPE (SWITCH_COND (exp));
+	      tree min_value = TYPE_MIN_VALUE (controlling_expr_type);
+	      tree max_value = TYPE_MAX_VALUE (controlling_expr_type);
+	      
+	      tree case_low = CASE_LOW (elt);
+	      tree case_high = CASE_HIGH (elt) ? CASE_HIGH (elt) : case_low;
+	      if (case_low && case_high)
+		{
+		  /* Case label is less than minimum for type.  */
+		  if ((tree_int_cst_compare (case_low, min_value) < 0)
+		      && (tree_int_cst_compare (case_high, min_value) < 0))
+		    {
+		      warning ("case label value %d is less than minimum value for type",
+			       TREE_INT_CST (case_low));
+		      continue;
+		    }
+		  
+		  /* Case value is greater than maximum for type.  */
+		  if ((tree_int_cst_compare (case_low, max_value) > 0)
+		      && (tree_int_cst_compare (case_high, max_value) > 0))
+		    {
+		      warning ("case label value %d exceeds maximum value for type",
+			       TREE_INT_CST (case_high));
+		      continue;
+		    }
+		  
+		  /* Saturate lower case label value to minimum.  */
+		  if ((tree_int_cst_compare (case_high, min_value) >= 0)
+		      && (tree_int_cst_compare (case_low, min_value) < 0))
+		    {
+		      warning ("lower value %d in case label range less than minimum value for type",
+			       TREE_INT_CST (case_low));
+		      case_low = min_value;
+		    }
+		  
+		  /* Saturate upper case label value to maximum.  */
+		  if ((tree_int_cst_compare (case_low, max_value) <= 0)
+		      && (tree_int_cst_compare (case_high, max_value) > 0))
+		    {
+		      warning ("upper value %d in case label range exceeds maximum value for type",
+			       TREE_INT_CST (case_high));
+		      case_high = max_value;
+		    }
+		}
+	      
+	      add_case_node (case_low, case_high, CASE_LABEL (elt), &duplicate, true);
 	      if (duplicate)
 		abort ();
 	    }

Index: gcc/testsuite/gcc.c-torture/compile/pr14730.c
===================================================================
RCS file: gcc/testsuite/gcc.c-torture/compile/pr14730.c
diff -N gcc/testsuite/gcc.c-torture/compile/pr14730.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gcc/testsuite/gcc.c-torture/compile/pr14730.c	19 Apr 2004 03:42:07 -0000
@@ -0,0 +1,16 @@
+/* PR middle-end/14730 */
+
+int t (char i)
+{
+  switch (i)
+    {
+    case 1:
+    case 7:
+    case 10:
+    case 14:
+    case 9:
+    case 256:
+      return 0;
+    }
+  return 0;
+}


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