compare.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare %s -Wno-unreachable-code
  2. int test(char *C) { // nothing here should warn.
  3. return C != ((void*)0);
  4. return C != (void*)0;
  5. return C != 0;
  6. return C != 1; // expected-warning {{comparison between pointer and integer ('char *' and 'int')}}
  7. }
  8. int ints(long a, unsigned long b) {
  9. enum EnumA {A};
  10. enum EnumB {B};
  11. enum EnumC {C = 0x10000};
  12. return
  13. // (a,b)
  14. (a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
  15. (a == (unsigned int) b) +
  16. (a == (unsigned short) b) +
  17. (a == (unsigned char) b) +
  18. ((long) a == b) + // expected-warning {{comparison of integers of different signs}}
  19. ((int) a == b) + // expected-warning {{comparison of integers of different signs}}
  20. ((short) a == b) + // expected-warning {{comparison of integers of different signs}}
  21. ((signed char) a == b) + // expected-warning {{comparison of integers of different signs}}
  22. ((long) a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
  23. ((int) a == (unsigned int) b) + // expected-warning {{comparison of integers of different signs}}
  24. ((short) a == (unsigned short) b) +
  25. ((signed char) a == (unsigned char) b) +
  26. (a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
  27. (a < (unsigned int) b) +
  28. (a < (unsigned short) b) +
  29. (a < (unsigned char) b) +
  30. ((long) a < b) + // expected-warning {{comparison of integers of different signs}}
  31. ((int) a < b) + // expected-warning {{comparison of integers of different signs}}
  32. ((short) a < b) + // expected-warning {{comparison of integers of different signs}}
  33. ((signed char) a < b) + // expected-warning {{comparison of integers of different signs}}
  34. ((long) a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
  35. ((int) a < (unsigned int) b) + // expected-warning {{comparison of integers of different signs}}
  36. ((short) a < (unsigned short) b) +
  37. ((signed char) a < (unsigned char) b) +
  38. // (A,b)
  39. (A == (unsigned long) b) +
  40. (A == (unsigned int) b) +
  41. (A == (unsigned short) b) +
  42. (A == (unsigned char) b) +
  43. ((long) A == b) +
  44. ((int) A == b) +
  45. ((short) A == b) +
  46. ((signed char) A == b) +
  47. ((long) A == (unsigned long) b) +
  48. ((int) A == (unsigned int) b) +
  49. ((short) A == (unsigned short) b) +
  50. ((signed char) A == (unsigned char) b) +
  51. (A < (unsigned long) b) +
  52. (A < (unsigned int) b) +
  53. (A < (unsigned short) b) +
  54. (A < (unsigned char) b) +
  55. ((long) A < b) +
  56. ((int) A < b) +
  57. ((short) A < b) +
  58. ((signed char) A < b) +
  59. ((long) A < (unsigned long) b) +
  60. ((int) A < (unsigned int) b) +
  61. ((short) A < (unsigned short) b) +
  62. ((signed char) A < (unsigned char) b) +
  63. // (a,B)
  64. (a == (unsigned long) B) +
  65. (a == (unsigned int) B) +
  66. (a == (unsigned short) B) +
  67. (a == (unsigned char) B) +
  68. ((long) a == B) +
  69. ((int) a == B) +
  70. ((short) a == B) +
  71. ((signed char) a == B) +
  72. ((long) a == (unsigned long) B) +
  73. ((int) a == (unsigned int) B) +
  74. ((short) a == (unsigned short) B) +
  75. ((signed char) a == (unsigned char) B) +
  76. (a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}}
  77. (a < (unsigned int) B) +
  78. (a < (unsigned short) B) +
  79. (a < (unsigned char) B) +
  80. ((long) a < B) +
  81. ((int) a < B) +
  82. ((short) a < B) +
  83. ((signed char) a < B) +
  84. ((long) a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}}
  85. ((int) a < (unsigned int) B) + // expected-warning {{comparison of integers of different signs}}
  86. ((short) a < (unsigned short) B) +
  87. ((signed char) a < (unsigned char) B) +
  88. // (C,b)
  89. (C == (unsigned long) b) +
  90. (C == (unsigned int) b) +
  91. (C == (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
  92. (C == (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
  93. ((long) C == b) +
  94. ((int) C == b) +
  95. ((short) C == b) +
  96. ((signed char) C == b) +
  97. ((long) C == (unsigned long) b) +
  98. ((int) C == (unsigned int) b) +
  99. ((short) C == (unsigned short) b) +
  100. ((signed char) C == (unsigned char) b) +
  101. (C < (unsigned long) b) +
  102. (C < (unsigned int) b) +
  103. (C < (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
  104. (C < (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
  105. ((long) C < b) +
  106. ((int) C < b) +
  107. ((short) C < b) +
  108. ((signed char) C < b) +
  109. ((long) C < (unsigned long) b) +
  110. ((int) C < (unsigned int) b) +
  111. ((short) C < (unsigned short) b) +
  112. ((signed char) C < (unsigned char) b) +
  113. // (a,C)
  114. (a == (unsigned long) C) +
  115. (a == (unsigned int) C) +
  116. (a == (unsigned short) C) +
  117. (a == (unsigned char) C) +
  118. ((long) a == C) +
  119. ((int) a == C) +
  120. ((short) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always false}}
  121. ((signed char) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always false}}
  122. ((long) a == (unsigned long) C) +
  123. ((int) a == (unsigned int) C) +
  124. ((short) a == (unsigned short) C) +
  125. ((signed char) a == (unsigned char) C) +
  126. (a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}}
  127. (a < (unsigned int) C) +
  128. (a < (unsigned short) C) +
  129. (a < (unsigned char) C) +
  130. ((long) a < C) +
  131. ((int) a < C) +
  132. ((short) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always true}}
  133. ((signed char) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always true}}
  134. ((long) a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}}
  135. ((int) a < (unsigned int) C) + // expected-warning {{comparison of integers of different signs}}
  136. ((short) a < (unsigned short) C) +
  137. ((signed char) a < (unsigned char) C) +
  138. // (0x80000,b)
  139. (0x80000 == (unsigned long) b) +
  140. (0x80000 == (unsigned int) b) +
  141. (0x80000 == (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}}
  142. (0x80000 == (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}}
  143. ((long) 0x80000 == b) +
  144. ((int) 0x80000 == b) +
  145. ((short) 0x80000 == b) +
  146. ((signed char) 0x80000 == b) +
  147. ((long) 0x80000 == (unsigned long) b) +
  148. ((int) 0x80000 == (unsigned int) b) +
  149. ((short) 0x80000 == (unsigned short) b) +
  150. ((signed char) 0x80000 == (unsigned char) b) +
  151. (0x80000 < (unsigned long) b) +
  152. (0x80000 < (unsigned int) b) +
  153. (0x80000 < (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}}
  154. (0x80000 < (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}}
  155. ((long) 0x80000 < b) +
  156. ((int) 0x80000 < b) +
  157. ((short) 0x80000 < b) +
  158. ((signed char) 0x80000 < b) +
  159. ((long) 0x80000 < (unsigned long) b) +
  160. ((int) 0x80000 < (unsigned int) b) +
  161. ((short) 0x80000 < (unsigned short) b) +
  162. ((signed char) 0x80000 < (unsigned char) b) +
  163. // (a,0x80000)
  164. (a == (unsigned long) 0x80000) +
  165. (a == (unsigned int) 0x80000) +
  166. (a == (unsigned short) 0x80000) +
  167. (a == (unsigned char) 0x80000) +
  168. ((long) a == 0x80000) +
  169. ((int) a == 0x80000) +
  170. ((short) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always false}}
  171. ((signed char) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always false}}
  172. ((long) a == (unsigned long) 0x80000) +
  173. ((int) a == (unsigned int) 0x80000) +
  174. ((short) a == (unsigned short) 0x80000) +
  175. ((signed char) a == (unsigned char) 0x80000) +
  176. (a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}}
  177. (a < (unsigned int) 0x80000) +
  178. (a < (unsigned short) 0x80000) +
  179. (a < (unsigned char) 0x80000) +
  180. ((long) a < 0x80000) +
  181. ((int) a < 0x80000) +
  182. ((short) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always true}}
  183. ((signed char) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always true}}
  184. ((long) a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}}
  185. ((int) a < (unsigned int) 0x80000) + // expected-warning {{comparison of integers of different signs}}
  186. ((short) a < (unsigned short) 0x80000) +
  187. ((signed char) a < (unsigned char) 0x80000) +
  188. // We should be able to avoid warning about this.
  189. (b != (a < 4 ? 1 : 2)) +
  190. 10
  191. ;
  192. }
  193. int equal(char *a, const char *b) {
  194. return a == b;
  195. }
  196. int arrays(char (*a)[5], char(*b)[10], char(*c)[5]) {
  197. int d = (a == c);
  198. return a == b; // expected-warning {{comparison of distinct pointer types}}
  199. }
  200. int pointers(int *a) {
  201. return a > 0; // expected-warning {{ordered comparison between pointer and zero ('int *' and 'int') is an extension}}
  202. return a > 42; // expected-warning {{ordered comparison between pointer and integer ('int *' and 'int')}}
  203. return a > (void *)0; // expected-warning {{comparison of distinct pointer types}}
  204. }
  205. int function_pointers(int (*a)(int), int (*b)(int), void (*c)(int)) {
  206. return a > b; // expected-warning {{ordered comparison of function pointers}}
  207. return function_pointers > function_pointers; // expected-warning {{self-comparison always evaluates to false}} expected-warning{{ordered comparison of function pointers}}
  208. return a > c; // expected-warning {{comparison of distinct pointer types}}
  209. return a == (void *) 0;
  210. return a == (void *) 1; // expected-warning {{equality comparison between function pointer and void pointer}}
  211. }
  212. int void_pointers(void* foo) {
  213. return foo == (void*) 0;
  214. return foo == (void*) 1;
  215. }
  216. int test1(int i) {
  217. enum en { zero };
  218. return i > zero;
  219. }
  220. // PR5937
  221. int test2(int i32) {
  222. struct foo {
  223. unsigned int u8 : 8;
  224. unsigned long long u31 : 31;
  225. unsigned long long u32 : 32;
  226. unsigned long long u63 : 63;
  227. unsigned long long u64 : 64;
  228. } *x;
  229. if (x->u8 == i32) { // comparison in int32, exact
  230. return 0;
  231. } else if (x->u31 == i32) { // comparison in int32, exact
  232. return 1;
  233. } else if (x->u32 == i32) { // expected-warning {{comparison of integers of different signs}}
  234. return 2;
  235. } else if (x->u63 == i32) { // comparison in uint64, exact because ==
  236. return 3;
  237. } else if (x->u64 == i32) { // expected-warning {{comparison of integers of different signs}}
  238. return 4;
  239. } else {
  240. return 5;
  241. }
  242. }
  243. // PR5887
  244. void test3() {
  245. unsigned short x, y;
  246. unsigned int z;
  247. if ((x > y ? x : y) > z)
  248. (void) 0;
  249. }
  250. // PR5961
  251. extern char *ptr4;
  252. void test4() {
  253. long value;
  254. if (value < (unsigned long) &ptr4) // expected-warning {{comparison of integers of different signs}}
  255. return;
  256. }
  257. // PR4807
  258. int test5(unsigned int x) {
  259. return (x < 0) // expected-warning {{comparison of unsigned expression < 0 is always false}}
  260. && (0 > x) // expected-warning {{comparison of 0 > unsigned expression is always false}}
  261. && (x >= 0) // expected-warning {{comparison of unsigned expression >= 0 is always true}}
  262. && (0 <= x); // expected-warning {{comparison of 0 <= unsigned expression is always true}}
  263. }
  264. int test6(unsigned i, unsigned power) {
  265. unsigned x = (i < (1 << power) ? i : 0);
  266. return x != 3 ? 1 << power : i;
  267. }
  268. // <rdar://problem/8414119> enum >= (enum)0 comparison should not generate any warnings
  269. enum rdar8414119_Vals { X, Y, Z };
  270. #define ZERO 0
  271. #define CHECK(x) (x >= X)
  272. void rdar8414119_foo(enum rdar8414119_Vals v) {
  273. if (CHECK(v)) // no-warning
  274. return;
  275. if (v >= X) // no-warning
  276. return;
  277. }
  278. int rdar8414119_bar(unsigned x) {
  279. return x >= ZERO; // no-warning
  280. }
  281. #undef ZERO
  282. #undef CHECK
  283. int rdar8511238() {
  284. enum A { A_foo, A_bar };
  285. enum A a;
  286. if (a < 0) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
  287. return 0;
  288. return 20;
  289. }
  290. // PR10336
  291. int test9(int sv, unsigned uv, long slv) {
  292. return sv == (uv ^= slv); // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}}
  293. }
  294. void test10(void) {
  295. int si;
  296. unsigned int ui;
  297. long sl;
  298. _Bool b;
  299. b = (si == (ui = sl)); // expected-warning {{comparison of integers of different signs: 'int' and 'unsigned int'}}
  300. b = (si == (ui = sl&15));
  301. }
  302. // PR11572
  303. struct test11S { unsigned x : 30; };
  304. int test11(unsigned y, struct test11S *p) {
  305. return y > (p->x >> 24); // no-warning
  306. }
  307. typedef char one_char[1];
  308. typedef char two_chars[2];
  309. void test12(unsigned a) {
  310. if (0 && -1 > a) { }
  311. }