1 /* Test for diagnostics for constant overflow. Test with -pedantic. */
2 /* Origin: Joseph Myers <joseph@codesourcery.com> */
3 /* { dg-do compile } */
4 /* { dg-options "-std=c99 -pedantic" } */
10 /* Unsigned overflow wraps around. */
12 /* Overflow in an unevaluated part of an expression is OK (example
15 E3
= 1 / 0, /* { dg-warning "division by zero" } */
16 /* { dg-error "enumerator value for 'E3' is not an integer constant" "enum error" { target *-*-* } .-1 } */
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 "division by zero" } */
20 /* { dg-error "enumerator value for 'E4' is not an integer constant" "enum error" { target *-*-* } .-1 } */
21 E5
= INT_MAX
+ 1, /* { dg-warning "integer overflow in expression" } */
22 /* { dg-warning "overflow in constant expression" "constant" { target *-*-* } .-1 } */
23 /* Again, overflow in evaluated subexpression. */
24 E6
= 0 * (INT_MAX
+ 1), /* { dg-warning "integer overflow in expression" } */
25 /* { dg-warning "overflow in constant expression" "constant" { target *-*-* } .-1 } */
26 /* A cast does not constitute overflow in conversion. */
32 int : 0 * (1 / 0); /* { dg-warning "division by zero" } */
33 /* { dg-error "not an integer constant" "integer constant" { target *-*-* } .-1 } */
34 int : 0 * (INT_MAX
+ 1); /* { dg-warning "integer overflow in expression" } */
35 /* { dg-warning "overflow in constant expression" "constant" { target *-*-* } .-1 } */
41 /* This expression is not required to be a constant expression, so
42 it should just involve undefined behavior at runtime. */
43 int c
= INT_MAX
+ 1; /* { dg-warning "integer overflow in expression" } */
47 /* But this expression does need to be constant. */
48 static int sc
= INT_MAX
+ 1; /* { dg-warning "integer overflow in expression" } */
49 /* { dg-warning "overflow in constant expression" "constant" { target *-*-* } .-1 } */
51 /* The first two of these involve overflow, so are not null pointer
52 constants. The third has the overflow in an unevaluated
53 subexpression, so is a null pointer constant. */
54 void *p
= 0 * (INT_MAX
+ 1); /* { dg-warning "integer overflow in expression" } */
55 /* { dg-warning "overflow in constant expression" "constant" { target *-*-* } .-1 } */
56 /* { dg-warning "initialization of 'void \\*' from 'int' makes pointer from integer without a cast" "null" { target *-*-* } .-2 } */
57 void *q
= 0 * (1 / 0); /* { dg-warning "division by zero" } */
58 /* { dg-error "initializer element is not constant" "constant" { target *-*-* } .-1 } */
59 /* { dg-warning "initialization of 'void \\*' from 'int' makes pointer from integer without a cast" "null" { target *-*-* } .-2 } */
60 void *r
= (1 ? 0 : INT_MAX
+1);
67 case 0 * (1/0): /* { dg-warning "division by zero" } */
68 /* { dg-error "case label does not reduce to an integer constant" "constant" { target *-*-* } .-1 } */
70 case 1 + 0 * (INT_MAX
+ 1): /* { dg-warning "integer overflow in expression" } */
71 /* { dg-warning "overflow in constant expression" "constant" { target *-*-* } .-1 } */
79 return INT_MAX
+ 1; /* { dg-warning "integer overflow in expression" } */
85 return INT_MAX
+ 1 - INT_MAX
; /* { dg-warning "integer overflow in expression" } */
88 void fuc (unsigned char);
89 void fsc (signed char);
94 fsc (SCHAR_MAX
+ 1); /* { dg-warning "overflow in conversion from .int. to .signed char. changes value" } */
95 fsc (SCHAR_MIN
- 1); /* { dg-warning "overflow in conversion from .int. to .signed char. changes value" } */
96 fsc (UCHAR_MAX
); /* { dg-warning "overflow in conversion from .int. to .signed char. changes value" } */
97 fsc (UCHAR_MAX
+ 1); /* { dg-warning "overflow in conversion from .int. to .signed char. changes value " } */
99 fuc (UCHAR_MAX
+ 1); /* { dg-warning "unsigned conversion from .int. to .unsigned char. changes value" } */
101 fuc (SCHAR_MIN
- 1); /* { dg-warning "unsigned conversion from .int. to .unsigned char. changes value" } */
102 fuc (-UCHAR_MAX
); /* { dg-warning "unsigned conversion from .int. to .unsigned char. changes value" } */
105 void fui (unsigned int);
106 void fsi (signed int);
114 /* For some reason, we only give certain warnings for implicit
115 conversions among values of the same precision with -Wconversion,
116 while we don't give others at all. */
117 fsi ((unsigned)INT_MAX
+ 1);
118 si
= (unsigned)INT_MAX
+ 1;
119 si
= x
? (unsigned)INT_MAX
+ 1 : 1;
120 fsi ((unsigned)INT_MAX
+ 2);
121 si
= (unsigned)INT_MAX
+ 2;
122 si
= x
? (unsigned)INT_MAX
+ 2 : 1;
130 ui
= x
? INT_MIN
: 1U;