View | Details | Raw Unified | Return to bug 43680
Collapse All | Expand All | Context: (Patch / File /
)

(-)a/gcc/cp/class.c (-8 / +2 lines)
 Lines 2781-2794   check_bitfield_decl (tree field) Link Here 
2781
	       && TREE_CODE (type) != BOOLEAN_TYPE)
2781
	       && TREE_CODE (type) != BOOLEAN_TYPE)
2782
	warning (0, "width of %q+D exceeds its type", field);
2782
	warning (0, "width of %q+D exceeds its type", field);
2783
      else if (TREE_CODE (type) == ENUMERAL_TYPE
2783
      else if (TREE_CODE (type) == ENUMERAL_TYPE
2784
	       && (0 > compare_tree_int (w,
2784
	       && (0 > (compare_tree_int
2785
					 tree_int_cst_min_precision
2785
			(w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
2786
					 (TYPE_MIN_VALUE (type),
2787
					  TYPE_UNSIGNED (type)))
2788
		   ||  0 > compare_tree_int (w,
2789
					     tree_int_cst_min_precision
2790
					     (TYPE_MAX_VALUE (type),
2791
					      TYPE_UNSIGNED (type)))))
2792
	warning (0, "%q+D is too small to hold all values of %q#T", field, type);
2786
	warning (0, "%q+D is too small to hold all values of %q#T", field, type);
2793
    }
2787
    }
2794
2788
(-)a/gcc/cp/cvt.c (-1 / +4 lines)
 Lines 680-686   ocp_convert (tree type, tree expr, int convtype, int flags) Link Here 
680
	     the original value is within the range of the enumeration
680
	     the original value is within the range of the enumeration
681
	     values. Otherwise, the resulting enumeration value is
681
	     values. Otherwise, the resulting enumeration value is
682
	     unspecified.  */
682
	     unspecified.  */
683
	  if (TREE_CODE (expr) == INTEGER_CST && !int_fits_type_p (expr, type))
683
	  if (TREE_CODE (expr) == INTEGER_CST
684
	      && !int_fits_type_p (expr, ENUM_UNDERLYING_TYPE (type)))
684
	    warning (OPT_Wconversion, 
685
	    warning (OPT_Wconversion, 
685
		     "the result of the conversion is unspecified because "
686
		     "the result of the conversion is unspecified because "
686
		     "%qE is outside the range of type %qT",
687
		     "%qE is outside the range of type %qT",
 Lines 1309-1314   type_promotes_to (tree type) Link Here 
1309
      int precision = MAX (TYPE_PRECISION (type),
1310
      int precision = MAX (TYPE_PRECISION (type),
1310
			   TYPE_PRECISION (integer_type_node));
1311
			   TYPE_PRECISION (integer_type_node));
1311
      tree totype = c_common_type_for_size (precision, 0);
1312
      tree totype = c_common_type_for_size (precision, 0);
1313
      if (TREE_CODE (type) == ENUMERAL_TYPE)
1314
	type = ENUM_UNDERLYING_TYPE (type);
1312
      if (TYPE_UNSIGNED (type)
1315
      if (TYPE_UNSIGNED (type)
1313
	  && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1316
	  && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1314
	type = c_common_type_for_size (precision, 1);
1317
	type = c_common_type_for_size (precision, 1);
(-)a/gcc/cp/decl.c (-37 / +36 lines)
 Lines 11233-11244   finish_enum (tree enumtype) Link Here 
11233
  tree maxnode;
11233
  tree maxnode;
11234
  tree value;
11234
  tree value;
11235
  tree t;
11235
  tree t;
11236
  bool unsignedp;
11237
  bool use_short_enum;
11238
  int lowprec;
11239
  int highprec;
11240
  int precision;
11241
  unsigned int itk;
11242
  tree underlying_type = NULL_TREE;
11236
  tree underlying_type = NULL_TREE;
11243
  bool fixed_underlying_type_p 
11237
  bool fixed_underlying_type_p 
11244
    = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
11238
    = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
 Lines 11301-11317   finish_enum (tree enumtype) Link Here 
11301
       the enumeration had a single enumerator with value 0.  */
11295
       the enumeration had a single enumerator with value 0.  */
11302
    minnode = maxnode = integer_zero_node;
11296
    minnode = maxnode = integer_zero_node;
11303
11297
11304
  /* Compute the number of bits require to represent all values of the
11305
     enumeration.  We must do this before the type of MINNODE and
11306
     MAXNODE are transformed, since tree_int_cst_min_precision relies
11307
     on the TREE_TYPE of the value it is passed.  */
11308
  unsignedp = tree_int_cst_sgn (minnode) >= 0;
11309
  lowprec = tree_int_cst_min_precision (minnode, unsignedp);
11310
  highprec = tree_int_cst_min_precision (maxnode, unsignedp);
11311
  precision = MAX (lowprec, highprec);
11312
11313
  if (!fixed_underlying_type_p)
11298
  if (!fixed_underlying_type_p)
11314
    {
11299
    {
11300
      /* Compute the number of bits require to represent all values of the
11301
	 enumeration.  We must do this before the type of MINNODE and
11302
	 MAXNODE are transformed, since tree_int_cst_min_precision relies
11303
	 on the TREE_TYPE of the value it is passed.  */
11304
      bool unsignedp = tree_int_cst_sgn (minnode) >= 0;
11305
      int lowprec = tree_int_cst_min_precision (minnode, unsignedp);
11306
      int highprec = tree_int_cst_min_precision (maxnode, unsignedp);
11307
      int precision = MAX (lowprec, highprec);
11308
      unsigned int itk;
11309
      bool use_short_enum;
11310
11315
      /* Determine the underlying type of the enumeration.
11311
      /* Determine the underlying type of the enumeration.
11316
11312
11317
         [dcl.enum]
11313
         [dcl.enum]
 Lines 11358-11400   finish_enum (tree enumtype) Link Here 
11358
         The value of sizeof() applied to an enumeration type, an object
11354
         The value of sizeof() applied to an enumeration type, an object
11359
         of an enumeration type, or an enumerator, is the value of sizeof()
11355
         of an enumeration type, or an enumerator, is the value of sizeof()
11360
         applied to the underlying type.  */
11356
         applied to the underlying type.  */
11357
      TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (underlying_type);
11358
      TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (underlying_type);
11361
      TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
11359
      TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
11362
      TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
11360
      TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
11363
      SET_TYPE_MODE (enumtype, TYPE_MODE (underlying_type));
11361
      SET_TYPE_MODE (enumtype, TYPE_MODE (underlying_type));
11362
      TYPE_PRECISION (enumtype) = TYPE_PRECISION (underlying_type);
11364
      TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
11363
      TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
11365
      TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
11364
      TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
11366
      TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (underlying_type);
11365
      TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (underlying_type);
11367
11366
11368
      /* Set the underlying type of the enumeration type to the
11367
      /* Compute the minimum and maximum values for the type.
11369
         computed enumeration type, restricted to the enumerator
11368
11370
         values. */
11369
	 [dcl.enum]
11370
11371
	 For an enumeration where emin is the smallest enumerator and emax
11372
	 is the largest, the values of the enumeration are the values of the
11373
	 underlying type in the range bmin to bmax, where bmin and bmax are,
11374
	 respectively, the smallest and largest values of the smallest bit-
11375
	 field that can store emin and emax.  */
11376
11377
      /* The middle-end currently assumes that types with TYPE_PRECISION
11378
	 narrower than their underlying type are suitably zero or sign
11379
	 extended to fill their mode.  Similarly, it assumes that the front
11380
	 end assures that a value of a particular type must be within
11381
	 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
11382
11383
	 We used to set these fields based on bmin and bmax, but that led
11384
	 to invalid assumptions like optimizing away bounds checking.  So
11385
	 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
11386
	 TYPE_MAX_VALUE to the values for the mode above and only restrict
11387
	 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics.  */
11371
      ENUM_UNDERLYING_TYPE (enumtype)
11388
      ENUM_UNDERLYING_TYPE (enumtype)
11372
	= build_distinct_type_copy (underlying_type);
11389
	= build_distinct_type_copy (underlying_type);
11390
      TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
11373
      set_min_and_max_values_for_integral_type 
11391
      set_min_and_max_values_for_integral_type 
11374
        (ENUM_UNDERLYING_TYPE (enumtype), precision, unsignedp);
11392
        (ENUM_UNDERLYING_TYPE (enumtype), precision, unsignedp);
11375
    }
11393
    }
11376
  else
11394
  else
11377
    underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
11395
    underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
11378
11396
11379
  /* Compute the minimum and maximum values for the type.
11380
11381
     [dcl.enum]
11382
11383
     For an enumeration where emin is the smallest enumerator and emax
11384
     is the largest, the values of the enumeration are the values of the
11385
     underlying type in the range bmin to bmax, where bmin and bmax are,
11386
     respectively, the smallest and largest values of the smallest bit-
11387
     field that can store emin and emax.  */
11388
  
11389
  /* The middle-end currently assumes that types with TYPE_PRECISION
11390
     narrower than their underlying type are suitably zero or sign
11391
     extended to fill their mode.  g++ doesn't make these guarantees.
11392
     Until the middle-end can represent such paradoxical types, we
11393
     set the TYPE_PRECISION to the width of the underlying type.  */
11394
  TYPE_PRECISION (enumtype) = TYPE_PRECISION (underlying_type);
11395
  
11396
  set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp);
11397
  
11398
  /* Convert each of the enumerators to the type of the underlying
11397
  /* Convert each of the enumerators to the type of the underlying
11399
     type of the enumeration.  */
11398
     type of the enumeration.  */
11400
  for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
11399
  for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
(-)a/gcc/testsuite/g++.dg/opt/enum2.C (+21 lines)
Line 0    Link Here 
1
// PR c++/43680
2
// Test that we don't make excessively aggressive assumptions about what
3
// values an enum variable can have.
4
// { dg-options "-O2 -fPIC" }
5
// { dg-do run }
6
7
extern "C" void abort ();
8
9
enum E { A, B, C, D };
10
11
void
12
CheckE(const E value)
13
{
14
  long v = value;
15
  if (v <= D)
16
    abort ();
17
}
18
19
int main() {
20
  CheckE(static_cast<E>(5));
21
}
(-)a/gcc/testsuite/g++.dg/warn/Wswitch-1.C (-2 / +2 lines)
 Lines 50-62   foo (int i, int j, enum e ei, enum e ej, enum e ek, enum e el, Link Here 
50
    {
50
    {
51
    case e1: return 1;
51
    case e1: return 1;
52
    case e2: return 2;
52
    case e2: return 2;
53
    case 3: return 3; /* { dg-warning "exceeds maximum value" } */
53
    case 3: return 3; /* { dg-warning "case value" } */
54
    }
54
    }
55
  switch (ep)
55
  switch (ep)
56
    {
56
    {
57
    case e1: return 1;
57
    case e1: return 1;
58
    case e2: return 2;
58
    case e2: return 2;
59
    case 3: return 3; /* { dg-warning "exceeds maximum value" } */
59
    case 3: return 3; /* { dg-warning "case value" } */
60
    default: break;
60
    default: break;
61
    }
61
    }
62
  return 0;
62
  return 0;
(-)a/gcc/testsuite/g++.dg/warn/pr33738.C (-26 lines)
 Lines 1-26    Link Here 
1
// { dg-do run }
2
// { dg-options "-O2 -Wtype-limits" }
3
extern void link_error (void);
4
5
enum Alpha {
6
 ZERO = 0, ONE, TWO, THREE
7
};
8
9
Alpha a2;
10
11
int m1 = -1;
12
int GetM1() {
13
 return m1;
14
}
15
16
int main() {
17
 a2 = static_cast<Alpha>(GetM1());
18
 if (a2 == -1) {	// { dg-warning "always false due" }
19
    link_error ();
20
 }
21
 if (-1 == a2) {	// { dg-warning "always false due" }
22
    link_error ();
23
 }
24
 return 0;
25
}
26

Return to bug 43680