From cbb4feb38e39bac728d1a0d053f14d9868d99e8d Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Mon, 3 May 2010 17:16:40 -0400 Subject: re PR c++/43680 ([DR 1022] G++ is too aggressive in optimizing away bounds checking with enums) PR c++/43680 gcc: * c.opt (-fstrict-enums): New. * doc/invoke.texi (C++ Dialect Options): Document -fstrict-enums. gcc/cp: * decl.c (finish_enum): Use the TYPE_MIN_VALUE and TYPE_MAX_VALUE from the selected underlying type unless -fstrict-enums. Set ENUM_UNDERLYING_TYPE to have the restricted range. * cvt.c (type_promotes_to): Use ENUM_UNDERLYING_TYPE. * class.c (check_bitfield_decl): Likewise. From-SVN: r159006 --- gcc/cp/cvt.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'gcc/cp/cvt.c') diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c index 1f87c5f..5be0b8d 100644 --- a/gcc/cp/cvt.c +++ b/gcc/cp/cvt.c @@ -682,7 +682,8 @@ ocp_convert (tree type, tree expr, int convtype, int flags) the original value is within the range of the enumeration values. Otherwise, the resulting enumeration value is unspecified. */ - if (TREE_CODE (expr) == INTEGER_CST && !int_fits_type_p (expr, type)) + if (TREE_CODE (expr) == INTEGER_CST + && !int_fits_type_p (expr, ENUM_UNDERLYING_TYPE (type))) warning (OPT_Wconversion, "the result of the conversion is unspecified because " "%qE is outside the range of type %qT", @@ -1311,6 +1312,8 @@ type_promotes_to (tree type) int precision = MAX (TYPE_PRECISION (type), TYPE_PRECISION (integer_type_node)); tree totype = c_common_type_for_size (precision, 0); + if (TREE_CODE (type) == ENUMERAL_TYPE) + type = ENUM_UNDERLYING_TYPE (type); if (TYPE_UNSIGNED (type) && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype)) type = c_common_type_for_size (precision, 1); -- cgit v1.1