1 |
298 |
jeremybenn |
/* Test for diagnostics for constant overflow. Test with -Wtraditional-conversion. */
|
2 |
|
|
/* Origin: Joseph Myers <joseph@codesourcery.com> */
|
3 |
|
|
/* { dg-do compile } */
|
4 |
|
|
/* { dg-options "-std=c99 -Wtraditional-conversion" } */
|
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,
|
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" { target *-*-* } 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 |
|
|
/* { dg-error "not an integer constant" "integer constant" { target *-*-* } 30 } */
|
32 |
|
|
int : 0 * (INT_MAX + 1); /* { dg-warning "integer overflow in expression" } */
|
33 |
|
|
};
|
34 |
|
|
|
35 |
|
|
void
|
36 |
|
|
f (void)
|
37 |
|
|
{
|
38 |
|
|
/* This expression is not required to be a constant expression, so
|
39 |
|
|
it should just involve undefined behavior at runtime. */
|
40 |
|
|
int c = INT_MAX + 1; /* { dg-warning "integer overflow in expression" } */
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
/* But this expression does need to be constant. */
|
44 |
|
|
static int sc = INT_MAX + 1; /* { dg-warning "integer overflow in expression" } */
|
45 |
|
|
|
46 |
|
|
/* The first two of these involve overflow, so are not null pointer
|
47 |
|
|
constants. The third has the overflow in an unevaluated
|
48 |
|
|
subexpression, so is a null pointer constant. */
|
49 |
|
|
void *p = 0 * (INT_MAX + 1); /* { dg-warning "integer overflow in expression" } */
|
50 |
|
|
/* { dg-warning "initialization makes pointer from integer without a cast" "null" { target *-*-* } 49 } */
|
51 |
|
|
void *q = 0 * (1 / 0); /* { dg-warning "division by zero" } */
|
52 |
|
|
/* { dg-error "initializer element is not computable at load time" "constant" { target *-*-* } 51 } */
|
53 |
|
|
/* { dg-warning "initialization makes pointer from integer without a cast" "null" { target *-*-* } 51 } */
|
54 |
|
|
void *r = (1 ? 0 : INT_MAX+1);
|
55 |
|
|
|
56 |
|
|
void
|
57 |
|
|
g (int i)
|
58 |
|
|
{
|
59 |
|
|
switch (i)
|
60 |
|
|
{
|
61 |
|
|
case 0 * (1/0): /* { dg-warning "division by zero" } */
|
62 |
|
|
/* { dg-error "case label does not reduce to an integer constant" "constant" { target *-*-* } 61 } */
|
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 |
|
|
/* { dg-warning "passing argument 1 of 'fsc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 87 } */
|
89 |
|
|
fsc (SCHAR_MIN - 1); /* { dg-warning "overflow in implicit constant conversion" } */
|
90 |
|
|
/* { dg-warning "passing argument 1 of 'fsc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 89 } */
|
91 |
|
|
fsc (UCHAR_MAX);
|
92 |
|
|
/* { dg-warning "passing argument 1 of 'fsc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 91 } */
|
93 |
|
|
fsc (UCHAR_MAX + 1); /* { dg-warning "overflow in implicit constant conversion" } */
|
94 |
|
|
/* { dg-warning "passing argument 1 of 'fsc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 93 } */
|
95 |
|
|
fuc (-1);
|
96 |
|
|
/* { dg-warning "passing argument 1 of 'fuc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 95 } */
|
97 |
|
|
fuc (UCHAR_MAX + 1); /* { dg-warning "large integer implicitly truncated to unsigned type" } */
|
98 |
|
|
/* { dg-warning "passing argument 1 of 'fuc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 97 } */
|
99 |
|
|
fuc (SCHAR_MIN);
|
100 |
|
|
/* { dg-warning "passing argument 1 of 'fuc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 99 } */
|
101 |
|
|
fuc (SCHAR_MIN - 1); /* { dg-warning "large integer implicitly truncated to unsigned type" } */
|
102 |
|
|
/* { dg-warning "passing argument 1 of 'fuc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 101 } */
|
103 |
|
|
fuc (-UCHAR_MAX); /* { dg-warning "large integer implicitly truncated to unsigned type" } */
|
104 |
|
|
/* { dg-warning "passing argument 1 of 'fuc' with different width due to prototype" "-Wtraditional-conversion" { target *-*-* } 103 } */
|
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 -Wtraditional-conversion,
|
118 |
|
|
while we don't give others at all. */
|
119 |
|
|
fsi ((unsigned)INT_MAX + 1); /* { dg-warning "passing argument 1 of 'fsi' as signed due to prototype" } */
|
120 |
|
|
si = (unsigned)INT_MAX + 1;
|
121 |
|
|
si = x ? (unsigned)INT_MAX + 1 : 1;
|
122 |
|
|
fsi ((unsigned)INT_MAX + 2); /* { dg-warning "passing argument 1 of 'fsi' as signed due to prototype" } */
|
123 |
|
|
si = (unsigned)INT_MAX + 2;
|
124 |
|
|
si = x ? (unsigned)INT_MAX + 2 : 1;
|
125 |
|
|
fsi (UINT_MAX); /* { dg-warning "passing argument 1 of 'fsi' as signed due to prototype" } */
|
126 |
|
|
si = UINT_MAX;
|
127 |
|
|
fui (-1);
|
128 |
|
|
/* { dg-warning "passing argument 1 of 'fui' as unsigned due to prototype" "-Wtraditional-conversion" { target *-*-* } 127 } */
|
129 |
|
|
ui = -1;
|
130 |
|
|
ui = x ? -1 : 1U;
|
131 |
|
|
fui (INT_MIN);
|
132 |
|
|
/* { dg-warning "passing argument 1 of 'fui' as unsigned due to prototype" "-Wtraditional-conversion" { target *-*-* } 131 } */
|
133 |
|
|
ui = INT_MIN;
|
134 |
|
|
ui = x ? INT_MIN : 1U;
|
135 |
|
|
}
|