1 /* Test for diagnostics for constant overflow. Test with -pedantic-errors. */
2 /* Origin: Joseph Myers <joseph@codesourcery.com> */
3 /* { dg-do compile } */
4 /* { dg-options "-pedantic-errors" } */
10 /* Unsigned overflow wraps around. */
12 /* Overflow in an unevaluated part of an expression is OK (example
14 E2 = 2 || 1 / 0, /* { dg-bogus "warning: division by zero" "" { xfail *-*-* } 14 } */
15 E3 = 1 / 0, /* { dg-warning "warning: division by zero" } */
16 /* { dg-error "error: enumerator value for 'E3' is not an integer constant" "enum error" { target *-*-* } 15 } */
17 /* But as in DR#031, the 1/0 in an evaluated subexpression means the
18 whole expression violates the constraints. */
19 E4 = 0 * (1 / 0), /* { dg-warning "warning: division by zero" } */
20 /* { dg-error "error: enumerator value for 'E4' is not an integer constant" "enum error" { xfail *-*-* } 19 } */
21 E5 = INT_MAX + 1, /* { dg-warning "warning: integer overflow in expression" } */
22 /* { dg-error "error: overflow in constant expression" "constant" { target *-*-* } 21 } */
23 /* Again, overflow in evaluated subexpression. */
24 E6 = 0 * (INT_MAX + 1), /* { dg-warning "warning: integer overflow in expression" } */
25 /* { dg-error "error: overflow in constant expression" "constant" { target *-*-* } 24 } */
26 /* A cast does not constitute overflow in conversion. */
32 int : 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
33 int : 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
34 /* { dg-error "error: overflow in constant expression" "constant" { target *-*-* } 33 } */
40 /* This expression is not required to be a constant expression, so
41 it should just involve undefined behavior at runtime. */
42 int c = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
46 /* This expression is neither required to be constant. */
47 static int sc = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
50 // Test for overflow in null pointer constant.
52 /* The first two of these involve overflow, so are not null pointer
53 constants. The third has the overflow in an unevaluated
54 subexpression, so is a null pointer constant. */
55 void *p = 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
56 /* { dg-error "invalid conversion from 'int' to 'void" "null" { target *-*-* } 55 } */
58 void *q = 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
59 /* { dg-error "invalid conversion from 'int' to 'void*'" "null" { xfail *-*-* } 58 } */
60 void *r = (1 ? 0 : INT_MAX+1); /* { dg-bogus "integer overflow in expression" "" { xfail *-*-* } 60 } */
67 case 0 * (1/0): /* { dg-warning "warning: division by zero" } */
69 case 1 + 0 * (INT_MAX + 1): /* { dg-warning "warning: integer overflow in expression" } */
70 /* { dg-error "error: overflow in constant expression" "constant" { target *-*-* } 69 } */
78 return INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
84 return INT_MAX + 1 - INT_MAX; /* { dg-warning "warning: integer overflow in expression" } */
87 void fuc (unsigned char);
88 void fsc (signed char);
93 fsc (SCHAR_MAX + 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
94 fsc (SCHAR_MIN - 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
95 fsc (UCHAR_MAX); /* { dg-warning "warning: overflow in implicit constant conversion" } */
96 fsc (UCHAR_MAX + 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
98 fuc (UCHAR_MAX + 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
100 fuc (SCHAR_MIN - 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
101 fuc (-UCHAR_MAX); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
104 void fui (unsigned int);
105 void fsi (signed int);
113 /* For some reason, we only give certain warnings for implicit
114 conversions among values of the same precision with -Wconversion,
115 while we don't give others at all. */
116 fsi ((unsigned)INT_MAX + 1);
117 si = (unsigned)INT_MAX + 1;
118 si = x ? (unsigned)INT_MAX + 1 : 1;
119 fsi ((unsigned)INT_MAX + 2);
120 si = (unsigned)INT_MAX + 2;
121 si = x ? (unsigned)INT_MAX + 2 : 1;
129 ui = x ? INT_MIN : 1U;