[C++ PATCH] Fix error handling in build_enumerator (PR c++/37389)

Jakub Jelinek jakub@redhat.com
Tue Sep 9 20:14:00 GMT 2008


On Tue, Sep 09, 2008 at 08:39:22AM -0700, Mark Mitchell wrote:
> > later in build_enumerator.  Should I drop that
> > "if (value == error_mark_node) value = NULL_TREE;" ?
> 
> Yes, please.  Conceptually, the value should not be NULL once we've seen
> the initializer; it's erroneous, but it exists.

Ok, bootstrapped & regtested on x86_64-linux.

2008-09-09  Jakub Jelinek  <jakub@redhat.com>

	PR c++/37389
	* decl.c (build_enumerator): Handle previous value's DECL_INITIAL
	being error_operand_p.  Don't clear value if it was error_mark_node.

	* g++.dg/parse/enum4.C: New test.

--- gcc/cp/decl.c.jj	2008-09-09 09:31:02.000000000 +0200
+++ gcc/cp/decl.c	2008-09-09 09:34:29.000000000 +0200
@@ -11143,21 +11143,26 @@ build_enumerator (tree name, tree value,
 	      tree prev_value;
 	      bool overflowed;
 
-	      /* The next value is the previous value plus one.  We can
-		 safely assume that the previous value is an INTEGER_CST.
+	      /* The next value is the previous value plus one.
 		 add_double doesn't know the type of the target expression,
 		 so we must check with int_fits_type_p as well.  */
 	      prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
-	      overflowed = add_double (TREE_INT_CST_LOW (prev_value),
-				       TREE_INT_CST_HIGH (prev_value),
-				       1, 0, &lo, &hi);
-	      value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi);
-	      overflowed |= !int_fits_type_p (value, TREE_TYPE (prev_value));
-
-	      if (overflowed)
+	      if (error_operand_p (prev_value))
+		value = error_mark_node;
+	      else
 		{
-		  error ("overflow in enumeration values at %qD", name);
-		  value = error_mark_node;
+		  overflowed = add_double (TREE_INT_CST_LOW (prev_value),
+					   TREE_INT_CST_HIGH (prev_value),
+					   1, 0, &lo, &hi);
+		  value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi);
+		  overflowed
+		    |= !int_fits_type_p (value, TREE_TYPE (prev_value));
+
+		  if (overflowed)
+		    {
+		      error ("overflow in enumeration values at %qD", name);
+		      value = error_mark_node;
+		    }
 		}
 	    }
 	  else
@@ -11181,8 +11186,6 @@ build_enumerator (tree name, tree value,
           /* Silently convert the value so that we can continue.  */
           value = perform_implicit_conversion (ENUM_UNDERLYING_TYPE (enumtype),
                                                value, tf_none);
-          if (value == error_mark_node)
-            value = NULL_TREE;
         }
     }
 
--- gcc/testsuite/g++.dg/parse/enum4.C.jj	2008-09-09 09:37:33.000000000 +0200
+++ gcc/testsuite/g++.dg/parse/enum4.C	2008-09-09 09:37:10.000000000 +0200
@@ -0,0 +1,10 @@
+// PR c++/37389
+// { dg-do compile }
+// { dg-options "-std=gnu++98" }
+
+enum
+{
+  A = 9223372036854775807ULL * 2 + 1,
+  B = B0,	// { dg-error "was not declared|overflow" }
+  C = C0	// { dg-error "was not declared" }
+};


	Jakub



More information about the Gcc-patches mailing list