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" } */
5
6 #include <limits.h>
7
8 enum e {
9 E0 = INT_MAX,
10 /* Unsigned overflow wraps around. */
11 E1 = UINT_MAX + 1,
12 /* Overflow in an unevaluated part of an expression is OK (example
13 in the standard). */
14 E2 = 2 || 1 / 0, /* { dg-bogus "warning: division by zero" "" { xfail *-*-* } } */
15 E3 = 1 / 0, /* { dg-warning "division by zero" } */
16 /* { dg-error "enumerator value for 'E3' is not an integer constant|not a constant.expression" "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 c++ } .-1 } */
21 /* { dg-error "division by zero is not a constant.expression" "division" { target c++11 } .-2 } */
22 E5 = INT_MAX + 1, /* { dg-warning "integer overflow in expression" } */
23 /* { dg-error "overflow in constant expression" "constant" { target *-*-* } .-1 } */
24 /* { dg-error "enumerator value for 'E5' is not an integer constant" "enum error" { target *-*-* } .-2 } */
25 /* Again, overflow in evaluated subexpression. */
26 E6 = 0 * (INT_MAX + 1), /* { dg-warning "integer overflow in expression" } */
27 /* { dg-error "overflow in constant expression" "constant" { target *-*-* } .-1 } */
28 /* { dg-error "enumerator value for 'E6' is not an integer constant" "enum error" { target *-*-* } .-2 } */
29 /* A cast does not constitute overflow in conversion. */
30 E7 = (char) INT_MAX
31 };
32
33 struct s {
34 int a;
35 int : 0 * (1 / 0); /* { dg-warning "division by zero" } */
36 /* { dg-error "division by zero is not a constant.expression" "division" { target c++11 } .-1 } */
37 /* { dg-error "width not an integer constant" "bit.field" { target c++ } .-2 } */
38 /* { dg-error "is not a constant expression" "division" { target c++ } .-3 } */
39 int : 0 * (INT_MAX + 1); /* { dg-warning "integer overflow in expression" } */
40 /* { dg-error "overflow in constant expression" "constant" { target *-*-* } .-1 } */
41 /* { dg-error "bit-field .* width not an integer constant" "" { target *-*-* } .-2 } */
42 };
43
44 void
f(void)45 f (void)
46 {
47 /* This expression is not required to be a constant expression, so
48 it should just involve undefined behavior at runtime. */
49 int c = INT_MAX + 1; /* { dg-warning "integer overflow in expression" } */
50
51 }
52
53 /* This expression is neither required to be constant. */
54 static int sc = INT_MAX + 1; /* { dg-warning "integer overflow in expression" } */
55
56
57 // Test for overflow in null pointer constant.
58 void *n = 0;
59 /* The first two of these involve overflow, so are not null pointer
60 constants. The third has the overflow in an unevaluated
61 subexpression, so is a null pointer constant. */
62 void *p = 0 * (INT_MAX + 1); /* { dg-warning "integer overflow in expression" } */
63 /* { dg-error "invalid conversion from 'int' to 'void" "null" { target *-*-* } .-1 } */
64
65 void *q = 0 * (1 / 0); /* { dg-warning "division by zero" } */
66 /* { dg-error "invalid conversion from 'int' to 'void" "null" { target *-*-* } .-1 } */
67
68 void *r = (1 ? 0 : INT_MAX+1);
69 /* { dg-bogus "integer overflow in expression" "" { xfail *-*-* } .-1 } */
70 /* { dg-error "invalid conversion from" "convert" { target c++11 } .-2 } */
71
72 void
g(int i)73 g (int i)
74 {
75 switch (i)
76 {
77 case 0 * (1/0):
78 /* { dg-warning "division by zero" "" { target *-*-* } .-1 } */
79 /* { dg-error "division by zero is not a constant expression" "division" { target c++11 } .-2 } */
80 /* { dg-error "is not a constant expression" "const" { target *-*-* } .-3 } */
81 ;
82 case 1 + 0 * (INT_MAX + 1): /* { dg-warning "integer overflow in expression" } */
83 /* { dg-error "overflow in constant expression" "constant" { target *-*-* } .-1 } */
84 ;
85 }
86 }
87
88 int
h(void)89 h (void)
90 {
91 return INT_MAX + 1; /* { dg-warning "integer overflow in expression" } */
92 }
93
94 int
h1(void)95 h1 (void)
96 {
97 return INT_MAX + 1 - INT_MAX; /* { dg-warning "integer overflow in expression" } */
98 }
99
100 void fuc (unsigned char);
101 void fsc (signed char);
102
103 void
h2(void)104 h2 (void)
105 {
106 fsc (SCHAR_MAX + 1); /* { dg-warning "overflow in conversion" } */
107 fsc (SCHAR_MIN - 1); /* { dg-warning "overflow in conversion" } */
108 fsc (UCHAR_MAX); /* { dg-warning "overflow in conversion" } */
109 fsc (UCHAR_MAX + 1); /* { dg-warning "overflow in conversion" } */
110 fuc (-1);
111 fuc (UCHAR_MAX + 1); /* { dg-warning "unsigned conversion from .int. to .unsigned char. changes value" } */
112 fuc (SCHAR_MIN);
113 fuc (SCHAR_MIN - 1); /* { dg-warning "unsigned conversion" } */
114 fuc (-UCHAR_MAX); /* { dg-warning "unsigned conversion" } */
115 }
116
117 void fui (unsigned int);
118 void fsi (signed int);
119
120 int si;
121 unsigned ui;
122
123 void
h2i(int x)124 h2i (int x)
125 {
126 /* For some reason, we only give certain warnings for implicit
127 conversions among values of the same precision with -Wconversion,
128 while we don't give others at all. */
129 fsi ((unsigned)INT_MAX + 1);
130 si = (unsigned)INT_MAX + 1;
131 si = x ? (unsigned)INT_MAX + 1 : 1;
132 fsi ((unsigned)INT_MAX + 2);
133 si = (unsigned)INT_MAX + 2;
134 si = x ? (unsigned)INT_MAX + 2 : 1;
135 fsi (UINT_MAX);
136 si = UINT_MAX;
137 fui (-1);
138 ui = -1;
139 ui = x ? -1 : 1U;
140 fui (INT_MIN);
141 ui = INT_MIN;
142 ui = x ? INT_MIN : 1U;
143 }
144