OSDN Git Service

2007-01-07 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / g++.dg / warn / overflow-warn-1.C
1 /* Test for diagnostics for constant overflow.  */
2 /* Origin: Joseph Myers <joseph@codesourcery.com> */
3 /* { dg-do compile } */
4 /* { dg-options "" } */
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 *-*-* } 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   /* Again, overflow in evaluated subexpression.  */
23   E6 = 0 * (INT_MAX + 1), /* { dg-warning "warning: integer overflow in expression" } */
24   /* A cast does not constitute overflow in conversion.  */
25   E7 = (char) INT_MAX
26 };
27
28 struct s {
29   int a;
30   int : 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
31   int : 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
32 };
33
34 void
35 f (void)
36 {
37   /* This expression is not required to be a constant expression, so
38      it should just involve undefined behavior at runtime.  */
39   int c = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
40 }
41
42 /* This expression is neither required to be constant.  */
43 static int sc = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
44
45
46 // Test for overflow in null pointer constant.  
47 void *n = 0;
48 /* The first two of these involve overflow, so are not null pointer
49    constants.  The third has the overflow in an unevaluated
50    subexpression, so is a null pointer constant.  */
51 void *p = 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
52 /* { dg-error "invalid conversion from 'int' to 'void" "null" { target *-*-* } 51 } */
53 void *q = 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
54 /* { dg-error "invalid conversion from 'int' to 'void*'" "null" { xfail *-*-* } 53 } */
55 void *r = (1 ? 0 : INT_MAX+1); /* { dg-bogus "integer overflow in expression" "" { xfail *-*-* } 55 } */
56
57 void
58 g (int i)
59 {
60   switch (i)
61     {
62     case 0 * (1/0): /* { dg-warning "warning: division by zero" } */
63       ;
64     case 1 + 0 * (INT_MAX + 1): /* { dg-warning "warning: integer overflow in expression" } */
65       ;
66     }
67 }
68
69 int
70 h (void)
71 {
72   return INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
73 }
74
75 int
76 h1 (void)
77 {
78   return INT_MAX + 1 - INT_MAX; /* { dg-warning "warning: integer overflow in expression" } */
79 }
80
81 void fuc (unsigned char);
82 void fsc (signed char);
83
84 void
85 h2 (void)
86 {
87   fsc (SCHAR_MAX + 1);
88   fsc (SCHAR_MIN - 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
89   fsc (UCHAR_MAX);
90   fsc (UCHAR_MAX + 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
91   fuc (-1);
92   fuc (UCHAR_MAX + 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
93   fuc (SCHAR_MIN);
94   fuc (SCHAR_MIN - 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
95   fuc (-UCHAR_MAX); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
96 }
97
98 void fui (unsigned int);
99 void fsi (signed int);
100
101 int si;
102 unsigned ui;
103
104 void
105 h2i (int x)
106 {
107   /* For some reason, we only give certain warnings for implicit
108      conversions among values of the same precision with -Wconversion,
109      while we don't give others at all.  */
110   fsi ((unsigned)INT_MAX + 1);
111   si = (unsigned)INT_MAX + 1;
112   si = x ? (unsigned)INT_MAX + 1 : 1;
113   fsi ((unsigned)INT_MAX + 2);
114   si = (unsigned)INT_MAX + 2;
115   si = x ? (unsigned)INT_MAX + 2 : 1;
116   fsi (UINT_MAX);
117   si = UINT_MAX;
118   fui (-1);
119   ui = -1;
120   ui = x ? -1 : 1U;
121   fui (INT_MIN);
122   ui = INT_MIN;
123   ui = x ? INT_MIN : 1U;
124 }
125 /* Test for diagnostics for constant overflow.  */
126 /* Origin: Joseph Myers <joseph@codesourcery.com> */
127 /* { dg-do compile } */
128 /* { dg-options "" } */
129
130 #include <limits.h>
131
132 enum e {
133   E0 = INT_MAX,
134   /* Unsigned overflow wraps around.  */
135   E1 = UINT_MAX + 1,
136   /* Overflow in an unevaluated part of an expression is OK (example
137      in the standard).  */
138   E2 = 2 || 1 / 0, /* { dg-bogus "warning: division by zero" "" { xfail *-*-* } 14 } */
139   E3 = 1 / 0, /* { dg-warning "warning: division by zero" } */
140   /* { dg-error "error: enumerator value for 'E3' is not an integer constant" "enum error" { target *-*-* } 15 } */
141   /* But as in DR#031, the 1/0 in an evaluated subexpression means the
142      whole expression violates the constraints.  */
143   E4 = 0 * (1 / 0), /* { dg-warning "warning: division by zero" } */
144   /* { dg-error "error: enumerator value for 'E4' is not an integer constant" "enum error" { xfail *-*-* } 19 } */
145   E5 = INT_MAX + 1, /* { dg-warning "warning: integer overflow in expression" } */
146   /* Again, overflow in evaluated subexpression.  */
147   E6 = 0 * (INT_MAX + 1), /* { dg-warning "warning: integer overflow in expression" } */
148   /* A cast does not constitute overflow in conversion.  */
149   E7 = (char) INT_MAX
150 };
151
152 struct s {
153   int a;
154   int : 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
155   int : 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
156 };
157
158 void
159 f (void)
160 {
161   /* This expression is not required to be a constant expression, so
162      it should just involve undefined behavior at runtime.  */
163   int c = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
164 }
165
166 /* But this expression does need to be constant.  */
167 static int sc = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
168
169 /* The first two of these involve overflow, so are not null pointer
170    constants.  The third has the overflow in an unevaluated
171    subexpression, so is a null pointer constant.  */
172 void *p = 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
173 /* { dg-error "invalid conversion from 'int' to 'void" "null" { target *-*-* } 48 } */
174 void *q = 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
175 /* { dg-error "invalid conversion from 'int' to 'void*'" "null" { xfail *-*-* } 50 } */
176 void *r = (1 ? 0 : INT_MAX+1); /* { dg-bogus "integer overflow in expression" "" { xfail *-*-* } 52 } */
177
178 void
179 g (int i)
180 {
181   switch (i)
182     {
183     case 0 * (1/0): /* { dg-warning "warning: division by zero" } */
184       ;
185     case 1 + 0 * (INT_MAX + 1): /* { dg-warning "warning: integer overflow in expression" } */
186       ;
187     }
188 }
189
190 int
191 h (void)
192 {
193   return INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
194 }
195
196 int
197 h1 (void)
198 {
199   return INT_MAX + 1 - INT_MAX; /* { dg-warning "warning: integer overflow in expression" } */
200 }
201
202 void fuc (unsigned char);
203 void fsc (signed char);
204
205 void
206 h2 (void)
207 {
208   fsc (SCHAR_MAX + 1);
209   fsc (SCHAR_MIN - 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
210   fsc (UCHAR_MAX);
211   fsc (UCHAR_MAX + 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
212   fuc (-1);
213   fuc (UCHAR_MAX + 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
214   fuc (SCHAR_MIN);
215   fuc (SCHAR_MIN - 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
216   fuc (-UCHAR_MAX); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
217 }
218
219 void fui (unsigned int);
220 void fsi (signed int);
221
222 int si;
223 unsigned ui;
224
225 void
226 h2i (int x)
227 {
228   /* For some reason, we only give certain warnings for implicit
229      conversions among values of the same precision with -Wconversion,
230      while we don't give others at all.  */
231   fsi ((unsigned)INT_MAX + 1);
232   si = (unsigned)INT_MAX + 1;
233   si = x ? (unsigned)INT_MAX + 1 : 1;
234   fsi ((unsigned)INT_MAX + 2);
235   si = (unsigned)INT_MAX + 2;
236   si = x ? (unsigned)INT_MAX + 2 : 1;
237   fsi (UINT_MAX);
238   si = UINT_MAX;
239   fui (-1);
240   ui = -1;
241   ui = x ? -1 : 1U;
242   fui (INT_MIN);
243   ui = INT_MIN;
244   ui = x ? INT_MIN : 1U;
245 }
246 /* Test for diagnostics for constant overflow.  */
247 /* Origin: Joseph Myers <joseph@codesourcery.com> */
248 /* { dg-do compile } */
249 /* { dg-options "" } */
250
251 #include <limits.h>
252
253 enum e {
254   E0 = INT_MAX,
255   /* Unsigned overflow wraps around.  */
256   E1 = UINT_MAX + 1,
257   /* Overflow in an unevaluated part of an expression is OK (example
258      in the standard).  */
259   E2 = 2 || 1 / 0, /* { dg-bogus "warning: division by zero" "" { xfail *-*-* } 14 } */
260   E3 = 1 / 0, /* { dg-warning "warning: division by zero" } */
261   /* { dg-error "error: enumerator value for 'E3' is not an integer constant" "enum error" { target *-*-* } 15 } */
262   /* But as in DR#031, the 1/0 in an evaluated subexpression means the
263      whole expression violates the constraints.  */
264   E4 = 0 * (1 / 0), /* { dg-warning "warning: division by zero" } */
265   /* { dg-error "error: enumerator value for 'E4' is not an integer constant" "enum error" { xfail *-*-* } 19 } */
266   E5 = INT_MAX + 1, /* { dg-warning "warning: integer overflow in expression" } */
267   /* Again, overflow in evaluated subexpression.  */
268   E6 = 0 * (INT_MAX + 1), /* { dg-warning "warning: integer overflow in expression" } */
269   /* A cast does not constitute overflow in conversion.  */
270   E7 = (char) INT_MAX
271 };
272
273 struct s {
274   int a;
275   int : 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
276   int : 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
277 };
278
279 void
280 f (void)
281 {
282   /* This expression is not required to be a constant expression, so
283      it should just involve undefined behavior at runtime.  */
284   int c = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
285 }
286
287 /* This expression is neither required to be constant.  */
288 static int sc = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
289
290
291 // Test for overflow in null pointer constant.  
292 void *n = 0;
293 /* The first two of these involve overflow, so are not null pointer
294    constants.  The third has the overflow in an unevaluated
295    subexpression, so is a null pointer constant.  */
296 void *p = 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
297 /* { dg-error "invalid conversion from 'int' to 'void" "null" { target *-*-* } 51 } */
298 void *q = 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
299 /* { dg-error "invalid conversion from 'int' to 'void*'" "null" { xfail *-*-* } 53 } */
300 void *r = (1 ? 0 : INT_MAX+1); /* { dg-bogus "integer overflow in expression" "" { xfail *-*-* } 55 } */
301
302 void
303 g (int i)
304 {
305   switch (i)
306     {
307     case 0 * (1/0): /* { dg-warning "warning: division by zero" } */
308       ;
309     case 1 + 0 * (INT_MAX + 1): /* { dg-warning "warning: integer overflow in expression" } */
310       ;
311     }
312 }
313
314 int
315 h (void)
316 {
317   return INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
318 }
319
320 int
321 h1 (void)
322 {
323   return INT_MAX + 1 - INT_MAX; /* { dg-warning "warning: integer overflow in expression" } */
324 }
325
326 void fuc (unsigned char);
327 void fsc (signed char);
328
329 void
330 h2 (void)
331 {
332   fsc (SCHAR_MAX + 1);
333   fsc (SCHAR_MIN - 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
334   fsc (UCHAR_MAX);
335   fsc (UCHAR_MAX + 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
336   fuc (-1);
337   fuc (UCHAR_MAX + 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
338   fuc (SCHAR_MIN);
339   fuc (SCHAR_MIN - 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
340   fuc (-UCHAR_MAX); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
341 }
342
343 void fui (unsigned int);
344 void fsi (signed int);
345
346 int si;
347 unsigned ui;
348
349 void
350 h2i (int x)
351 {
352   /* For some reason, we only give certain warnings for implicit
353      conversions among values of the same precision with -Wconversion,
354      while we don't give others at all.  */
355   fsi ((unsigned)INT_MAX + 1);
356   si = (unsigned)INT_MAX + 1;
357   si = x ? (unsigned)INT_MAX + 1 : 1;
358   fsi ((unsigned)INT_MAX + 2);
359   si = (unsigned)INT_MAX + 2;
360   si = x ? (unsigned)INT_MAX + 2 : 1;
361   fsi (UINT_MAX);
362   si = UINT_MAX;
363   fui (-1);
364   ui = -1;
365   ui = x ? -1 : 1U;
366   fui (INT_MIN);
367   ui = INT_MIN;
368   ui = x ? INT_MIN : 1U;
369 }