1 |
149 |
jeremybenn |
/* Test for diagnostics for constant overflow. Test with -Wconversion. */
|
2 |
|
|
/* Origin: Joseph Myers <joseph@codesourcery.com> */
|
3 |
|
|
/* { dg-do compile } */
|
4 |
|
|
/* { dg-options "-std=c99 -Wconversion" } */
|
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 "warning: division by zero" } */
|
16 |
|
|
/* { dg-error "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 "warning: division by zero" } */
|
20 |
|
|
/* { dg-error "error: enumerator value for 'E4' is not an integer constant" "enum error" { xfail *-*-* } 19 } */
|
21 |
|
|
E5 = INT_MAX + 1, /* { dg-warning "warning: integer overflow in expression" } */
|
22 |
|
|
/* Again, overflow in evaluated subexpression. */
|
23 |
|
|
E6 = 0 * (INT_MAX + 1), /* { dg-warning "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 "warning: division by zero" } */
|
31 |
|
|
int : 0 * (INT_MAX + 1); /* { dg-warning "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 "warning: integer overflow in expression" } */
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
/* But this expression does need to be constant. */
|
43 |
|
|
static int sc = INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
|
44 |
|
|
|
45 |
|
|
/* The first two of these involve overflow, so are not null pointer
|
46 |
|
|
constants. The third has the overflow in an unevaluated
|
47 |
|
|
subexpression, so is a null pointer constant. */
|
48 |
|
|
void *p = 0 * (INT_MAX + 1); /* { dg-warning "warning: integer overflow in expression" } */
|
49 |
|
|
/* { dg-warning "warning: initialization makes pointer from integer without a cast" "null" { target *-*-* } 48 } */
|
50 |
|
|
void *q = 0 * (1 / 0); /* { dg-warning "warning: division by zero" } */
|
51 |
|
|
/* { dg-warning "warning: initialization makes pointer from integer without a cast" "null" { xfail *-*-* } 50 } */
|
52 |
|
|
void *r = (1 ? 0 : INT_MAX+1);
|
53 |
|
|
|
54 |
|
|
void
|
55 |
|
|
g (int i)
|
56 |
|
|
{
|
57 |
|
|
switch (i)
|
58 |
|
|
{
|
59 |
|
|
case 0 * (1/0): /* { dg-warning "warning: division by zero" } */
|
60 |
|
|
;
|
61 |
|
|
case 1 + 0 * (INT_MAX + 1): /* { dg-warning "warning: integer overflow in expression" } */
|
62 |
|
|
;
|
63 |
|
|
}
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
int
|
67 |
|
|
h (void)
|
68 |
|
|
{
|
69 |
|
|
return INT_MAX + 1; /* { dg-warning "warning: integer overflow in expression" } */
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
int
|
73 |
|
|
h1 (void)
|
74 |
|
|
{
|
75 |
|
|
return INT_MAX + 1 - INT_MAX; /* { dg-warning "warning: integer overflow in expression" } */
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
void fuc (unsigned char);
|
79 |
|
|
void fsc (signed char);
|
80 |
|
|
|
81 |
|
|
void
|
82 |
|
|
h2 (void)
|
83 |
|
|
{
|
84 |
|
|
fsc (SCHAR_MAX + 1);
|
85 |
|
|
/* { dg-warning "warning: passing argument 1 of 'fsc' with different width due to prototype" "-Wconversion" { target *-*-* } 84 } */
|
86 |
|
|
fsc (SCHAR_MIN - 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
|
87 |
|
|
/* { dg-warning "warning: passing argument 1 of 'fsc' with different width due to prototype" "-Wconversion" { target *-*-* } 86 } */
|
88 |
|
|
fsc (UCHAR_MAX);
|
89 |
|
|
/* { dg-warning "warning: passing argument 1 of 'fsc' with different width due to prototype" "-Wconversion" { target *-*-* } 88 } */
|
90 |
|
|
fsc (UCHAR_MAX + 1); /* { dg-warning "warning: overflow in implicit constant conversion" } */
|
91 |
|
|
/* { dg-warning "warning: passing argument 1 of 'fsc' with different width due to prototype" "-Wconversion" { target *-*-* } 90 } */
|
92 |
|
|
fuc (-1); /* { dg-warning "warning: negative integer implicitly converted to unsigned type" } */
|
93 |
|
|
/* { dg-warning "warning: passing argument 1 of 'fuc' with different width due to prototype" "-Wconversion" { target *-*-* } 92 } */
|
94 |
|
|
fuc (UCHAR_MAX + 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
|
95 |
|
|
/* { dg-warning "warning: passing argument 1 of 'fuc' with different width due to prototype" "-Wconversion" { target *-*-* } 94 } */
|
96 |
|
|
fuc (SCHAR_MIN); /* { dg-warning "warning: negative integer implicitly converted to unsigned type" } */
|
97 |
|
|
/* { dg-warning "warning: passing argument 1 of 'fuc' with different width due to prototype" "-Wconversion" { target *-*-* } 96 } */
|
98 |
|
|
fuc (SCHAR_MIN - 1); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
|
99 |
|
|
/* { dg-warning "warning: passing argument 1 of 'fuc' with different width due to prototype" "-Wconversion" { target *-*-* } 98 } */
|
100 |
|
|
fuc (-UCHAR_MAX); /* { dg-warning "warning: large integer implicitly truncated to unsigned type" } */
|
101 |
|
|
/* { dg-warning "warning: passing argument 1 of 'fuc' with different width due to prototype" "-Wconversion" { target *-*-* } 100 } */
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
void fui (unsigned int);
|
105 |
|
|
void fsi (signed int);
|
106 |
|
|
|
107 |
|
|
int si;
|
108 |
|
|
unsigned ui;
|
109 |
|
|
|
110 |
|
|
void
|
111 |
|
|
h2i (int x)
|
112 |
|
|
{
|
113 |
|
|
/* For some reason, we only give certain warnings for implicit
|
114 |
|
|
conversions among values of the same precision with -Wconversion,
|
115 |
|
|
while we don't give others at all. */
|
116 |
|
|
fsi ((unsigned)INT_MAX + 1); /* { dg-warning "warning: passing argument 1 of 'fsi' as signed due to prototype" } */
|
117 |
|
|
si = (unsigned)INT_MAX + 1;
|
118 |
|
|
si = x ? (unsigned)INT_MAX + 1 : 1;
|
119 |
|
|
fsi ((unsigned)INT_MAX + 2); /* { dg-warning "warning: passing argument 1 of 'fsi' as signed due to prototype" } */
|
120 |
|
|
si = (unsigned)INT_MAX + 2;
|
121 |
|
|
si = x ? (unsigned)INT_MAX + 2 : 1;
|
122 |
|
|
fsi (UINT_MAX); /* { dg-warning "warning: passing argument 1 of 'fsi' as signed due to prototype" } */
|
123 |
|
|
si = UINT_MAX;
|
124 |
|
|
fui (-1); /* { dg-warning "warning: negative integer implicitly converted to unsigned type" } */
|
125 |
|
|
/* { dg-warning "warning: passing argument 1 of 'fui' as unsigned due to prototype" "-Wconversion" { target *-*-* } 124 } */
|
126 |
|
|
ui = -1; /* { dg-warning "warning: negative integer implicitly converted to unsigned type" } */
|
127 |
|
|
ui = x ? -1 : 1U; /* { dg-warning "warning: negative integer implicitly converted to unsigned type" } */
|
128 |
|
|
fui (INT_MIN); /* { dg-warning "warning: negative integer implicitly converted to unsigned type" } */
|
129 |
|
|
/* { dg-warning "warning: passing argument 1 of 'fui' as unsigned due to prototype" "-Wconversion" { target *-*-* } 128 } */
|
130 |
|
|
ui = INT_MIN; /* { dg-warning "warning: negative integer implicitly converted to unsigned type" } */
|
131 |
|
|
ui = x ? INT_MIN : 1U; /* { dg-warning "warning: negative integer implicitly converted to unsigned type" } */
|
132 |
|
|
}
|