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