1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare -Wtautological-constant-in-range-compare %s -Wno-unreachable-code
2 // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare -Wtype-limits %s -Wno-unreachable-code
4 int test(char *C
) { // nothing here should warn.
5 return C
!= ((void*)0);
8 return C
!= 1; // expected-warning {{comparison between pointer and integer ('char *' and 'int')}}
11 int ints(long a
, unsigned long b
) {
14 enum EnumC
{C
= 0x10000};
17 (a
== (unsigned long) b
) + // expected-warning {{comparison of integers of different signs}}
18 (a
== (unsigned int) b
) +
19 (a
== (unsigned short) b
) +
20 (a
== (unsigned char) b
) +
21 ((long) a
== b
) + // expected-warning {{comparison of integers of different signs}}
22 ((int) a
== b
) + // expected-warning {{comparison of integers of different signs}}
23 ((short) a
== b
) + // expected-warning {{comparison of integers of different signs}}
24 ((signed char) a
== b
) + // expected-warning {{comparison of integers of different signs}}
25 ((long) a
== (unsigned long) b
) + // expected-warning {{comparison of integers of different signs}}
26 ((int) a
== (unsigned int) b
) + // expected-warning {{comparison of integers of different signs}}
27 ((short) a
== (unsigned short) b
) +
28 ((signed char) a
== (unsigned char) b
) +
29 (a
< (unsigned long) b
) + // expected-warning {{comparison of integers of different signs}}
30 (a
< (unsigned int) b
) +
31 (a
< (unsigned short) b
) +
32 (a
< (unsigned char) b
) +
33 ((long) a
< b
) + // expected-warning {{comparison of integers of different signs}}
34 ((int) a
< b
) + // expected-warning {{comparison of integers of different signs}}
35 ((short) a
< b
) + // expected-warning {{comparison of integers of different signs}}
36 ((signed char) a
< b
) + // expected-warning {{comparison of integers of different signs}}
37 ((long) a
< (unsigned long) b
) + // expected-warning {{comparison of integers of different signs}}
38 ((int) a
< (unsigned int) b
) + // expected-warning {{comparison of integers of different signs}}
39 ((short) a
< (unsigned short) b
) +
40 ((signed char) a
< (unsigned char) b
) +
43 (A
== (unsigned long) b
) +
44 (A
== (unsigned int) b
) +
45 (A
== (unsigned short) b
) +
46 (A
== (unsigned char) b
) +
50 ((signed char) A
== b
) +
51 ((long) A
== (unsigned long) b
) +
52 ((int) A
== (unsigned int) b
) +
53 ((short) A
== (unsigned short) b
) +
54 ((signed char) A
== (unsigned char) b
) +
55 (A
< (unsigned long) b
) +
56 (A
< (unsigned int) b
) +
57 (A
< (unsigned short) b
) +
58 (A
< (unsigned char) b
) +
62 ((signed char) A
< b
) +
63 ((long) A
< (unsigned long) b
) +
64 ((int) A
< (unsigned int) b
) +
65 ((short) A
< (unsigned short) b
) +
66 ((signed char) A
< (unsigned char) b
) +
69 (a
== (unsigned long) B
) +
70 (a
== (unsigned int) B
) +
71 (a
== (unsigned short) B
) +
72 (a
== (unsigned char) B
) +
76 ((signed char) a
== B
) +
77 ((long) a
== (unsigned long) B
) +
78 ((int) a
== (unsigned int) B
) +
79 ((short) a
== (unsigned short) B
) +
80 ((signed char) a
== (unsigned char) B
) +
81 (a
< (unsigned long) B
) + // expected-warning {{comparison of unsigned expression < 0 is always false}}
82 (a
< (unsigned int) B
) +
83 (a
< (unsigned short) B
) +
84 (a
< (unsigned char) B
) +
88 ((signed char) a
< B
) +
89 ((long) a
< (unsigned long) B
) + // expected-warning {{comparison of unsigned expression < 0 is always false}}
90 ((int) a
< (unsigned int) B
) + // expected-warning {{comparison of unsigned expression < 0 is always false}}
91 ((short) a
< (unsigned short) B
) +
92 ((signed char) a
< (unsigned char) B
) +
95 (C
== (unsigned long) b
) +
96 (C
== (unsigned int) b
) +
97 (C
== (unsigned short) b
) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
98 (C
== (unsigned char) b
) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
102 ((signed char) C
== b
) +
103 ((long) C
== (unsigned long) b
) +
104 ((int) C
== (unsigned int) b
) +
105 ((short) C
== (unsigned short) b
) +
106 ((signed char) C
== (unsigned char) b
) +
107 (C
< (unsigned long) b
) +
108 (C
< (unsigned int) b
) +
109 (C
< (unsigned short) b
) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
110 (C
< (unsigned char) b
) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
114 ((signed char) C
< b
) +
115 ((long) C
< (unsigned long) b
) +
116 ((int) C
< (unsigned int) b
) +
117 ((short) C
< (unsigned short) b
) +
118 ((signed char) C
< (unsigned char) b
) +
121 (a
== (unsigned long) C
) +
122 (a
== (unsigned int) C
) +
123 (a
== (unsigned short) C
) +
124 (a
== (unsigned char) C
) +
127 ((short) a
== C
) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always false}}
128 ((signed char) a
== C
) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always false}}
129 ((long) a
== (unsigned long) C
) +
130 ((int) a
== (unsigned int) C
) +
131 ((short) a
== (unsigned short) C
) +
132 ((signed char) a
== (unsigned char) C
) +
133 (a
< (unsigned long) C
) + // expected-warning {{comparison of integers of different signs}}
134 (a
< (unsigned int) C
) +
135 (a
< (unsigned short) C
) +
136 (a
< (unsigned char) C
) +
139 ((short) a
< C
) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always true}}
140 ((signed char) a
< C
) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always true}}
141 ((long) a
< (unsigned long) C
) + // expected-warning {{comparison of integers of different signs}}
142 ((int) a
< (unsigned int) C
) + // expected-warning {{comparison of integers of different signs}}
143 ((short) a
< (unsigned short) C
) +
144 ((signed char) a
< (unsigned char) C
) +
147 (0x80000 == (unsigned long) b
) +
148 (0x80000 == (unsigned int) b
) +
149 (0x80000 == (unsigned short) b
) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}}
150 (0x80000 == (unsigned char) b
) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}}
151 ((long) 0x80000 == b
) +
152 ((int) 0x80000 == b
) +
153 ((short) 0x80000 == b
) +
154 ((signed char) 0x80000 == b
) +
155 ((long) 0x80000 == (unsigned long) b
) +
156 ((int) 0x80000 == (unsigned int) b
) +
157 ((short) 0x80000 == (unsigned short) b
) +
158 ((signed char) 0x80000 == (unsigned char) b
) +
159 (0x80000 < (unsigned long) b
) +
160 (0x80000 < (unsigned int) b
) +
161 (0x80000 < (unsigned short) b
) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}}
162 (0x80000 < (unsigned char) b
) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}}
163 ((long) 0x80000 < b
) +
164 ((int) 0x80000 < b
) +
165 ((short) 0x80000 < b
) +
166 ((signed char) 0x80000 < b
) +
167 ((long) 0x80000 < (unsigned long) b
) +
168 ((int) 0x80000 < (unsigned int) b
) +
169 ((short) 0x80000 < (unsigned short) b
) +
170 ((signed char) 0x80000 < (unsigned char) b
) +
173 (a
== (unsigned long) 0x80000) +
174 (a
== (unsigned int) 0x80000) +
175 (a
== (unsigned short) 0x80000) +
176 (a
== (unsigned char) 0x80000) +
177 ((long) a
== 0x80000) +
178 ((int) a
== 0x80000) +
179 ((short) a
== 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always false}}
180 ((signed char) a
== 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always false}}
181 ((long) a
== (unsigned long) 0x80000) +
182 ((int) a
== (unsigned int) 0x80000) +
183 ((short) a
== (unsigned short) 0x80000) +
184 ((signed char) a
== (unsigned char) 0x80000) +
185 (a
< (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}}
186 (a
< (unsigned int) 0x80000) +
187 (a
< (unsigned short) 0x80000) +
188 (a
< (unsigned char) 0x80000) +
189 ((long) a
< 0x80000) +
190 ((int) a
< 0x80000) +
191 ((short) a
< 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always true}}
192 ((signed char) a
< 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always true}}
193 ((long) a
< (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}}
194 ((int) a
< (unsigned int) 0x80000) + // expected-warning {{comparison of integers of different signs}}
195 ((short) a
< (unsigned short) 0x80000) +
196 ((signed char) a
< (unsigned char) 0x80000) +
198 // We should be able to avoid warning about this.
199 (b
!= (a
< 4 ? 1 : 2)) +
205 int equal(char *a
, const char *b
) {
209 int arrays(char (*a
)[5], char(*b
)[10], char(*c
)[5]) {
211 return a
== b
; // expected-warning {{comparison of distinct pointer types}}
214 int pointers(int *a
) {
215 return a
> 0; // expected-warning {{ordered comparison between pointer and zero ('int *' and 'int') is an extension}}
216 return a
> 42; // expected-warning {{ordered comparison between pointer and integer ('int *' and 'int')}}
217 return a
> (void *)0; // expected-warning {{comparison of distinct pointer types}}
220 int function_pointers(int (*a
)(int), int (*b
)(int), void (*c
)(int)) {
221 return a
> b
; // expected-warning {{ordered comparison of function pointers}}
222 return function_pointers
> function_pointers
; // expected-warning {{self-comparison always evaluates to false}} expected-warning{{ordered comparison of function pointers}}
223 return a
> c
; // expected-warning {{comparison of distinct pointer types}} expected-warning {{ordered comparison of function pointers}}
224 return a
== (void *) 0;
225 return a
== (void *) 1; // expected-warning {{equality comparison between function pointer and void pointer}}
228 int void_pointers(void* foo
) {
229 return foo
== (void*) 0;
230 return foo
== (void*) 1;
243 unsigned long long u31
: 31;
244 unsigned long long u32
: 32;
245 unsigned long long u63
: 63;
246 unsigned long long u64
: 64;
249 if (x
->u8
== i32
) { // comparison in int32, exact
251 } else if (x
->u31
== i32
) { // comparison in int32, exact
253 } else if (x
->u32
== i32
) { // expected-warning {{comparison of integers of different signs}}
255 } else if (x
->u63
== i32
) { // comparison in uint64, exact because ==
257 } else if (x
->u64
== i32
) { // expected-warning {{comparison of integers of different signs}}
268 if ((x
> y
? x
: y
) > z
)
276 if (value
< (unsigned long) &ptr4
) // expected-warning {{comparison of integers of different signs}}
281 int test5(unsigned int x
) {
282 return (x
< 0) // expected-warning {{comparison of unsigned expression < 0 is always false}}
283 && (0 > x
) // expected-warning {{comparison of 0 > unsigned expression is always false}}
284 && (x
>= 0) // expected-warning {{comparison of unsigned expression >= 0 is always true}}
285 && (0 <= x
); // expected-warning {{comparison of 0 <= unsigned expression is always true}}
292 unsigned long d
: 40;
295 void test5a(struct bitfield a
) {
297 if (a
.b
< 0) {} // expected-warning {{comparison of unsigned expression < 0 is always false}}
299 if (a
.d
< 0) {} // expected-warning {{comparison of unsigned expression < 0 is always false}}
302 int test6(unsigned i
, unsigned power
) {
303 unsigned x
= (i
< (1 << power
) ? i
: 0);
304 return x
!= 3 ? 1 << power
: i
;
307 // <rdar://problem/8414119> enum >= (enum)0 comparison should not generate any warnings
308 enum rdar8414119_Vals
{ X
, Y
, Z
};
310 #define CHECK(x) (x >= X)
311 void rdar8414119_foo(enum rdar8414119_Vals v
) {
312 if (CHECK(v
)) // no-warning
314 if (v
>= X
) // no-warning
317 int rdar8414119_bar(unsigned x
) {
318 return x
>= ZERO
; // no-warning
323 int rdar8511238(void) {
324 enum A
{ A_foo
, A_bar
};
331 if (a
< 0) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
337 if (a
>= 0) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
346 if (0 <= a
) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
348 if (0 > a
) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
357 if (a
< 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
363 if (a
>= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
372 if (0U <= a
) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
374 if (0U > a
) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
383 int test9(int sv
, unsigned uv
, long slv
) {
384 return sv
== (uv
^= slv
); // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}}
393 b
= (si
== (ui
= sl
)); // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}}
394 b
= (si
== (ui
= sl
&15));
398 struct test11S
{ unsigned x
: 30; };
399 int test11(unsigned y
, struct test11S
*p
) {
400 return y
> (p
->x
>> 24); // no-warning
403 typedef char one_char
[1];
404 typedef char two_chars
[2];
406 void test12(unsigned a
) {
412 enum PR36008EnumTest
{
416 void pr36008(enum PR36008EnumTest lhs
) {
417 __typeof__(lhs
) x
= lhs
;
418 __typeof__(kPR36008Value
) y
= (kPR36008Value
);
419 if (x
== y
) x
= y
; // no warning
420 if (y
== x
) y
= x
; // no warning