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