compare.c revision 35de813674503b87ec5117b6492cc0a4ef7d8728
1// RUN: clang-cc -fsyntax-only -pedantic -verify -Wsign-compare %s
2
3int test(char *C) { // nothing here should warn.
4  return C != ((void*)0);
5  return C != (void*)0;
6  return C != 0;
7  return C != 1;  // expected-warning {{comparison between pointer and integer ('char *' and 'int')}}
8}
9
10int ints(long a, unsigned long b) {
11  return (a == b) +        // expected-warning {{comparison of integers of different signs}}
12         ((int)a == b) +   // expected-warning {{comparison of integers of different signs}}
13         ((short)a == b) + // expected-warning {{comparison of integers of different signs}}
14         (a == (unsigned int) b) +  // expected-warning {{comparison of integers of different signs}}
15         (a == (unsigned short) b); // expected-warning {{comparison of integers of different signs}}
16
17  enum Enum {B};
18  return (a == B) +
19         ((int)a == B) +
20         ((short)a == B) +
21         (a == (unsigned int) B) +  // expected-warning {{comparison of integers of different signs}}
22         (a == (unsigned short) B); // expected-warning {{comparison of integers of different signs}}
23
24  // Should be able to prove all of these are non-negative.
25  return (b == (long) B) +
26         (b == (int) B) +
27         (b == (short) B);
28}
29
30int equal(char *a, const char *b) {
31    return a == b;
32}
33
34int arrays(char (*a)[5], char(*b)[10], char(*c)[5]) {
35  int d = (a == c);
36  return a == b; // expected-warning {{comparison of distinct pointer types}}
37}
38
39int pointers(int *a) {
40  return a > 0; // expected-warning {{ordered comparison between pointer and zero ('int *' and 'int') is an extension}}
41  return a > 42; // expected-warning {{ordered comparison between pointer and integer ('int *' and 'int')}}
42  return a > (void *)0; // expected-warning {{comparison of distinct pointer types}}
43}
44
45int function_pointers(int (*a)(int), int (*b)(int), void (*c)(int)) {
46  return a > b; // expected-warning {{ordered comparison of function pointers}}
47  return function_pointers > function_pointers; // expected-warning {{ordered comparison of function pointers}}
48  return a > c; // expected-warning {{comparison of distinct pointer types}}
49  return a == (void *) 0;
50  return a == (void *) 1; // expected-warning {{equality comparison between function pointer and void pointer}}
51}
52
53int void_pointers(void* foo) {
54  return foo == (void*) 0;
55  return foo == (void*) 1;
56}
57