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

(-)a/gcc/cp/class.c (-8 / +2 lines)
 Lines 1-8    Link Here 
1
	       && (0 > compare_tree_int (w,
1
	       && (0 > (compare_tree_int
2
					 tree_int_cst_min_precision
2
			(w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3
					 (TYPE_MIN_VALUE (type),
4
					  TYPE_UNSIGNED (type)))
5
		   ||  0 > compare_tree_int (w,
6
					     tree_int_cst_min_precision
7
					     (TYPE_MAX_VALUE (type),
8
					      TYPE_UNSIGNED (type)))))
(-)a/gcc/cp/cvt.c (-1 / +4 lines)
Line 1    Link Here 
1
	  if (TREE_CODE (expr) == INTEGER_CST && !int_fits_type_p (expr, type))
1
	  if (TREE_CODE (expr) == INTEGER_CST
2
	      && !int_fits_type_p (expr, ENUM_UNDERLYING_TYPE (type)))
3
      if (TREE_CODE (type) == ENUMERAL_TYPE)
4
	type = ENUM_UNDERLYING_TYPE (type);
(-)a/gcc/cp/decl.c (-37 / +36 lines)
 Lines 1-37    Link Here 
1
  bool unsignedp;
1
      /* Compute the number of bits require to represent all values of the
2
  bool use_short_enum;
2
	 enumeration.  We must do this before the type of MINNODE and
3
  int lowprec;
3
	 MAXNODE are transformed, since tree_int_cst_min_precision relies
4
  int highprec;
4
	 on the TREE_TYPE of the value it is passed.  */
5
  int precision;
5
      bool unsignedp = tree_int_cst_sgn (minnode) >= 0;
6
  unsigned int itk;
6
      int lowprec = tree_int_cst_min_precision (minnode, unsignedp);
7
  /* Compute the number of bits require to represent all values of the
7
      int highprec = tree_int_cst_min_precision (maxnode, unsignedp);
8
     enumeration.  We must do this before the type of MINNODE and
8
      int precision = MAX (lowprec, highprec);
9
     MAXNODE are transformed, since tree_int_cst_min_precision relies
9
      unsigned int itk;
10
     on the TREE_TYPE of the value it is passed.  */
10
      bool use_short_enum;
11
  unsignedp = tree_int_cst_sgn (minnode) >= 0;
11
12
  lowprec = tree_int_cst_min_precision (minnode, unsignedp);
12
      TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (underlying_type);
13
  highprec = tree_int_cst_min_precision (maxnode, unsignedp);
13
      TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (underlying_type);
14
  precision = MAX (lowprec, highprec);
14
      TYPE_PRECISION (enumtype) = TYPE_PRECISION (underlying_type);
15
16
      /* Set the underlying type of the enumeration type to the
15
      /* Compute the minimum and maximum values for the type.
17
         computed enumeration type, restricted to the enumerator
16
18
         values. */
17
	 [dcl.enum]
18
19
	 For an enumeration where emin is the smallest enumerator and emax
20
	 is the largest, the values of the enumeration are the values of the
21
	 underlying type in the range bmin to bmax, where bmin and bmax are,
22
	 respectively, the smallest and largest values of the smallest bit-
23
	 field that can store emin and emax.  */
24
25
      /* The middle-end currently assumes that types with TYPE_PRECISION
26
	 narrower than their underlying type are suitably zero or sign
27
	 extended to fill their mode.  Similarly, it assumes that the front
28
	 end assures that a value of a particular type must be within
29
	 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
30
31
	 We used to set these fields based on bmin and bmax, but that led
32
	 to invalid assumptions like optimizing away bounds checking.  So
33
	 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
34
	 TYPE_MAX_VALUE to the values for the mode above and only restrict
35
	 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics.  */
36
      TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
19
  /* Compute the minimum and maximum values for the type.
20
21
     [dcl.enum]
22
23
     For an enumeration where emin is the smallest enumerator and emax
24
     is the largest, the values of the enumeration are the values of the
25
     underlying type in the range bmin to bmax, where bmin and bmax are,
26
     respectively, the smallest and largest values of the smallest bit-
27
     field that can store emin and emax.  */
28
  
29
  /* The middle-end currently assumes that types with TYPE_PRECISION
30
     narrower than their underlying type are suitably zero or sign
31
     extended to fill their mode.  g++ doesn't make these guarantees.
32
     Until the middle-end can represent such paradoxical types, we
33
     set the TYPE_PRECISION to the width of the underlying type.  */
34
  TYPE_PRECISION (enumtype) = TYPE_PRECISION (underlying_type);
35
  
36
  set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp);
37
  
(-)a/gcc/testsuite/g++.dg/opt/enum2.C (+21 lines)
Line 1    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 1-2    Link Here 
1
    case 3: return 3; /* { dg-warning "exceeds maximum value" } */
1
    case 3: return 3; /* { dg-warning "case value" } */
2
    case 3: return 3; /* { dg-warning "exceeds maximum value" } */
2
    case 3: return 3; /* { dg-warning "case value" } */
(-)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