OSDN Git Service

2009-09-01 Diego Novillo <dnovillo@google.com>
[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 "division by zero" } */
16   /* { dg-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 "division by zero" } */
20   /* { dg-error "enumerator value for 'E4' is not an integer constant" "enum error" { xfail *-*-* } 19 } */
21   E5 = INT_MAX + 1, /* { dg-warning "integer overflow in expression" } */
22   /* Again, overflow in evaluated subexpression.  */
23   E6 = 0 * (INT_MAX + 1), /* { dg-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 "division by zero" } */
31   int : 0 * (INT_MAX + 1); /* { dg-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 "integer overflow in expression" } */
40 }
41
42 /* This expression is neither required to be constant.  */
43 static int sc = INT_MAX + 1; /* { dg-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 "integer overflow in expression" } */
52 /* { dg-error "invalid conversion from 'int' to 'void" "null" { target *-*-* } 51 } */
53 void *q = 0 * (1 / 0); /* { dg-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 "division by zero" } */
63       ;
64     case 1 + 0 * (INT_MAX + 1): /* { dg-warning "integer overflow in expression" } */
65       ;
66     }
67 }
68
69 int
70 h (void)
71 {
72   return INT_MAX + 1; /* { dg-warning "integer overflow in expression" } */
73 }
74
75 int
76 h1 (void)
77 {
78   return INT_MAX + 1 - INT_MAX; /* { dg-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 "overflow in implicit constant conversion" } */
89   fsc (UCHAR_MAX);
90   fsc (UCHAR_MAX + 1); /* { dg-warning "overflow in implicit constant conversion" } */
91   fuc (-1);
92   fuc (UCHAR_MAX + 1); /* { dg-warning "large integer implicitly truncated to unsigned type" } */
93   fuc (SCHAR_MIN);
94   fuc (SCHAR_MIN - 1); /* { dg-warning "large integer implicitly truncated to unsigned type" } */
95   fuc (-UCHAR_MAX); /* { dg-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 }