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