compare.c revision 48f5e63aec3f2fda7f1e75565bcbba08a9d6a14f
1// RUN: clang-cc -triple x86_64-apple-darwin -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 enum EnumA {A}; 12 enum EnumB {B}; 13 enum EnumC {C = 0x10000}; 14 return 15 // (a,b) 16 (a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}} 17 (a == (unsigned int) b) + 18 (a == (unsigned short) b) + 19 (a == (unsigned char) b) + 20 ((long) a == b) + // expected-warning {{comparison of integers of different signs}} 21 ((int) a == b) + // expected-warning {{comparison of integers of different signs}} 22 ((short) a == b) + // expected-warning {{comparison of integers of different signs}} 23 ((signed char) a == b) + // expected-warning {{comparison of integers of different signs}} 24 ((long) a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}} 25 ((int) a == (unsigned int) b) + // expected-warning {{comparison of integers of different signs}} 26 ((short) a == (unsigned short) b) + // expected-warning {{comparison of integers of different signs}} 27 ((signed char) a == (unsigned char) b) + // expected-warning {{comparison of integers of different signs}} 28 (a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}} 29 (a < (unsigned int) b) + 30 (a < (unsigned short) b) + 31 (a < (unsigned char) b) + 32 ((long) a < b) + // expected-warning {{comparison of integers of different signs}} 33 ((int) a < b) + // expected-warning {{comparison of integers of different signs}} 34 ((short) a < b) + // expected-warning {{comparison of integers of different signs}} 35 ((signed char) a < b) + // expected-warning {{comparison of integers of different signs}} 36 ((long) a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}} 37 ((int) a < (unsigned int) b) + // expected-warning {{comparison of integers of different signs}} 38 ((short) a < (unsigned short) b) + // expected-warning {{comparison of integers of different signs}} 39 ((signed char) a < (unsigned char) b) + // expected-warning {{comparison of integers of different signs}} 40 41 // (A,b) 42 (A == (unsigned long) b) + 43 (A == (unsigned int) b) + 44 (A == (unsigned short) b) + 45 (A == (unsigned char) b) + 46 ((long) A == b) + 47 ((int) A == b) + 48 ((short) A == b) + 49 ((signed char) A == b) + 50 ((long) A == (unsigned long) b) + 51 ((int) A == (unsigned int) b) + 52 ((short) A == (unsigned short) b) + 53 ((signed char) A == (unsigned char) b) + 54 (A < (unsigned long) b) + 55 (A < (unsigned int) b) + 56 (A < (unsigned short) b) + 57 (A < (unsigned char) b) + 58 ((long) A < b) + 59 ((int) A < b) + 60 ((short) A < b) + 61 ((signed char) A < b) + 62 ((long) A < (unsigned long) b) + 63 ((int) A < (unsigned int) b) + 64 ((short) A < (unsigned short) b) + 65 ((signed char) A < (unsigned char) b) + 66 67 // (a,B) 68 (a == (unsigned long) B) + 69 (a == (unsigned int) B) + 70 (a == (unsigned short) B) + 71 (a == (unsigned char) B) + 72 ((long) a == B) + 73 ((int) a == B) + 74 ((short) a == B) + 75 ((signed char) a == B) + 76 ((long) a == (unsigned long) B) + 77 ((int) a == (unsigned int) B) + 78 ((short) a == (unsigned short) B) + 79 ((signed char) a == (unsigned char) B) + 80 (a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}} 81 (a < (unsigned int) B) + 82 (a < (unsigned short) B) + 83 (a < (unsigned char) B) + 84 ((long) a < B) + 85 ((int) a < B) + 86 ((short) a < B) + 87 ((signed char) a < B) + 88 ((long) a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}} 89 ((int) a < (unsigned int) B) + // expected-warning {{comparison of integers of different signs}} 90 ((short) a < (unsigned short) B) + // expected-warning {{comparison of integers of different signs}} 91 ((signed char) a < (unsigned char) B) + // expected-warning {{comparison of integers of different signs}} 92 93 // (C,b) 94 (C == (unsigned long) b) + 95 (C == (unsigned int) b) + 96 (C == (unsigned short) b) + 97 (C == (unsigned char) b) + 98 ((long) C == b) + 99 ((int) C == b) + 100 ((short) C == b) + 101 ((signed char) C == b) + 102 ((long) C == (unsigned long) b) + 103 ((int) C == (unsigned int) b) + 104 ((short) C == (unsigned short) b) + 105 ((signed char) C == (unsigned char) b) + 106 (C < (unsigned long) b) + 107 (C < (unsigned int) b) + 108 (C < (unsigned short) b) + 109 (C < (unsigned char) b) + 110 ((long) C < b) + 111 ((int) C < b) + 112 ((short) C < b) + 113 ((signed char) C < b) + 114 ((long) C < (unsigned long) b) + 115 ((int) C < (unsigned int) b) + 116 ((short) C < (unsigned short) b) + 117 ((signed char) C < (unsigned char) b) + 118 119 // (a,C) 120 (a == (unsigned long) C) + 121 (a == (unsigned int) C) + 122 (a == (unsigned short) C) + 123 (a == (unsigned char) C) + 124 ((long) a == C) + 125 ((int) a == C) + 126 ((short) a == C) + 127 ((signed char) a == C) + 128 ((long) a == (unsigned long) C) + 129 ((int) a == (unsigned int) C) + 130 ((short) a == (unsigned short) C) + 131 ((signed char) a == (unsigned char) C) + 132 (a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}} 133 (a < (unsigned int) C) + 134 (a < (unsigned short) C) + 135 (a < (unsigned char) C) + 136 ((long) a < C) + 137 ((int) a < C) + 138 ((short) a < C) + 139 ((signed char) a < C) + 140 ((long) a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}} 141 ((int) a < (unsigned int) C) + // expected-warning {{comparison of integers of different signs}} 142 ((short) a < (unsigned short) C) + // expected-warning {{comparison of integers of different signs}} 143 ((signed char) a < (unsigned char) C) + // expected-warning {{comparison of integers of different signs}} 144 145 // (0x80000,b) 146 (0x80000 == (unsigned long) b) + 147 (0x80000 == (unsigned int) b) + 148 (0x80000 == (unsigned short) b) + 149 (0x80000 == (unsigned char) b) + 150 ((long) 0x80000 == b) + 151 ((int) 0x80000 == b) + 152 ((short) 0x80000 == b) + 153 ((signed char) 0x80000 == b) + 154 ((long) 0x80000 == (unsigned long) b) + 155 ((int) 0x80000 == (unsigned int) b) + 156 ((short) 0x80000 == (unsigned short) b) + 157 ((signed char) 0x80000 == (unsigned char) b) + 158 (0x80000 < (unsigned long) b) + 159 (0x80000 < (unsigned int) b) + 160 (0x80000 < (unsigned short) b) + 161 (0x80000 < (unsigned char) b) + 162 ((long) 0x80000 < b) + 163 ((int) 0x80000 < b) + 164 ((short) 0x80000 < b) + 165 ((signed char) 0x80000 < b) + 166 ((long) 0x80000 < (unsigned long) b) + 167 ((int) 0x80000 < (unsigned int) b) + 168 ((short) 0x80000 < (unsigned short) b) + 169 ((signed char) 0x80000 < (unsigned char) b) + 170 171 // (a,0x80000) 172 (a == (unsigned long) 0x80000) + 173 (a == (unsigned int) 0x80000) + 174 (a == (unsigned short) 0x80000) + 175 (a == (unsigned char) 0x80000) + 176 ((long) a == 0x80000) + 177 ((int) a == 0x80000) + 178 ((short) a == 0x80000) + 179 ((signed char) a == 0x80000) + 180 ((long) a == (unsigned long) 0x80000) + 181 ((int) a == (unsigned int) 0x80000) + 182 ((short) a == (unsigned short) 0x80000) + 183 ((signed char) a == (unsigned char) 0x80000) + 184 (a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}} 185 (a < (unsigned int) 0x80000) + 186 (a < (unsigned short) 0x80000) + 187 (a < (unsigned char) 0x80000) + 188 ((long) a < 0x80000) + 189 ((int) a < 0x80000) + 190 ((short) a < 0x80000) + 191 ((signed char) a < 0x80000) + 192 ((long) a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}} 193 ((int) a < (unsigned int) 0x80000) + // expected-warning {{comparison of integers of different signs}} 194 ((short) a < (unsigned short) 0x80000) + // expected-warning {{comparison of integers of different signs}} 195 ((signed char) a < (unsigned char) 0x80000) + // expected-warning {{comparison of integers of different signs}} 196 197 10 198 ; 199} 200 201int equal(char *a, const char *b) { 202 return a == b; 203} 204 205int arrays(char (*a)[5], char(*b)[10], char(*c)[5]) { 206 int d = (a == c); 207 return a == b; // expected-warning {{comparison of distinct pointer types}} 208} 209 210int pointers(int *a) { 211 return a > 0; // expected-warning {{ordered comparison between pointer and zero ('int *' and 'int') is an extension}} 212 return a > 42; // expected-warning {{ordered comparison between pointer and integer ('int *' and 'int')}} 213 return a > (void *)0; // expected-warning {{comparison of distinct pointer types}} 214} 215 216int function_pointers(int (*a)(int), int (*b)(int), void (*c)(int)) { 217 return a > b; // expected-warning {{ordered comparison of function pointers}} 218 return function_pointers > function_pointers; // expected-warning {{ordered comparison of function pointers}} 219 return a > c; // expected-warning {{comparison of distinct pointer types}} 220 return a == (void *) 0; 221 return a == (void *) 1; // expected-warning {{equality comparison between function pointer and void pointer}} 222} 223 224int void_pointers(void* foo) { 225 return foo == (void*) 0; 226 return foo == (void*) 1; 227} 228