compare.cpp revision 7adf3a9f84688f334a1cd977317bb42f9e06c9f4
15dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall// Force x86-64 because some of our heuristics are actually based 25dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall// on integer sizes. 35dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall 4d87de7bdbc25dd84ae3dbefffda4539c443000d2Eli Friedman// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare -std=c++11 %s 5b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall 6b13c87f0c9705d91d5a3e134be9934c9ad531071John McCallint test0(long a, unsigned long b) { 75dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall enum EnumA {A}; 85dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall enum EnumB {B}; 95dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall enum EnumC {C = 0x10000}; 105dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall return 115dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall // (a,b) 125dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}} 135dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned int) b) + 145dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned short) b) + 155dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned char) b) + 165dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a == b) + // expected-warning {{comparison of integers of different signs}} 175dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a == b) + // expected-warning {{comparison of integers of different signs}} 185dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) a == b) + // expected-warning {{comparison of integers of different signs}} 195dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) a == b) + // expected-warning {{comparison of integers of different signs}} 205dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}} 215dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a == (unsigned int) b) + // expected-warning {{comparison of integers of different signs}} 22323ed74658bc8375278eabf074b4777458376540John McCall ((short) a == (unsigned short) b) + 23323ed74658bc8375278eabf074b4777458376540John McCall ((signed char) a == (unsigned char) b) + 245dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}} 255dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned int) b) + 265dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned short) b) + 275dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned char) b) + 285dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a < b) + // expected-warning {{comparison of integers of different signs}} 295dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a < b) + // expected-warning {{comparison of integers of different signs}} 305dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) a < b) + // expected-warning {{comparison of integers of different signs}} 315dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) a < b) + // expected-warning {{comparison of integers of different signs}} 325dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}} 335dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a < (unsigned int) b) + // expected-warning {{comparison of integers of different signs}} 34323ed74658bc8375278eabf074b4777458376540John McCall ((short) a < (unsigned short) b) + 35323ed74658bc8375278eabf074b4777458376540John McCall ((signed char) a < (unsigned char) b) + 365dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall 375dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall // (A,b) 385dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (A == (unsigned long) b) + 395dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (A == (unsigned int) b) + 405dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (A == (unsigned short) b) + 415dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (A == (unsigned char) b) + 425dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) A == b) + 435dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) A == b) + 445dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) A == b) + 455dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) A == b) + 465dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) A == (unsigned long) b) + 475dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) A == (unsigned int) b) + 485dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) A == (unsigned short) b) + 495dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) A == (unsigned char) b) + 505dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (A < (unsigned long) b) + 515dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (A < (unsigned int) b) + 52842aef8d942a880eeb9535d40de31a86838264cbJohn McCall (A < (unsigned short) b) + 53842aef8d942a880eeb9535d40de31a86838264cbJohn McCall (A < (unsigned char) b) + 545dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) A < b) + 555dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) A < b) + 565dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) A < b) + 575dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) A < b) + 585dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) A < (unsigned long) b) + 595dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) A < (unsigned int) b) + 605dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) A < (unsigned short) b) + 615dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) A < (unsigned char) b) + 625dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall 635dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall // (a,B) 645dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned long) B) + 655dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned int) B) + 665dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned short) B) + 675dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned char) B) + 685dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a == B) + 695dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a == B) + 705dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) a == B) + 715dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) a == B) + 725dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a == (unsigned long) B) + 735dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a == (unsigned int) B) + 745dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) a == (unsigned short) B) + 755dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) a == (unsigned char) B) + 765dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}} 775dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned int) B) + 785dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned short) B) + 795dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned char) B) + 805dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a < B) + 81842aef8d942a880eeb9535d40de31a86838264cbJohn McCall ((int) a < B) + 82842aef8d942a880eeb9535d40de31a86838264cbJohn McCall ((short) a < B) + 83842aef8d942a880eeb9535d40de31a86838264cbJohn McCall ((signed char) a < B) + 845dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}} 855dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a < (unsigned int) B) + // expected-warning {{comparison of integers of different signs}} 86323ed74658bc8375278eabf074b4777458376540John McCall ((short) a < (unsigned short) B) + 87323ed74658bc8375278eabf074b4777458376540John McCall ((signed char) a < (unsigned char) B) + 885dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall 895dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall // (C,b) 905dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (C == (unsigned long) b) + 915dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (C == (unsigned int) b) + 927adf3a9f84688f334a1cd977317bb42f9e06c9f4Ted Kremenek (C == (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}} 937adf3a9f84688f334a1cd977317bb42f9e06c9f4Ted Kremenek (C == (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}} 945dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) C == b) + 955dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) C == b) + 965dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) C == b) + 975dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) C == b) + 985dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) C == (unsigned long) b) + 995dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) C == (unsigned int) b) + 1005dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) C == (unsigned short) b) + 1015dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) C == (unsigned char) b) + 1025dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (C < (unsigned long) b) + 1035dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (C < (unsigned int) b) + 1047adf3a9f84688f334a1cd977317bb42f9e06c9f4Ted Kremenek (C < (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}} 1057adf3a9f84688f334a1cd977317bb42f9e06c9f4Ted Kremenek (C < (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}} 1065dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) C < b) + 1075dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) C < b) + 1085dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) C < b) + 1095dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) C < b) + 1105dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) C < (unsigned long) b) + 1115dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) C < (unsigned int) b) + 1125dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) C < (unsigned short) b) + 1135dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) C < (unsigned char) b) + 1145dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall 1155dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall // (a,C) 1165dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned long) C) + 1175dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned int) C) + 1185dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned short) C) + 1195dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned char) C) + 1205dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a == C) + 1215dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a == C) + 1227adf3a9f84688f334a1cd977317bb42f9e06c9f4Ted Kremenek ((short) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always false}} 1237adf3a9f84688f334a1cd977317bb42f9e06c9f4Ted Kremenek ((signed char) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always false}} 1245dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a == (unsigned long) C) + 1255dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a == (unsigned int) C) + 1265dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) a == (unsigned short) C) + 1275dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) a == (unsigned char) C) + 1285dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}} 1295dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned int) C) + 1305dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned short) C) + 1315dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned char) C) + 1325dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a < C) + 133842aef8d942a880eeb9535d40de31a86838264cbJohn McCall ((int) a < C) + 1347adf3a9f84688f334a1cd977317bb42f9e06c9f4Ted Kremenek ((short) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always true}} 1357adf3a9f84688f334a1cd977317bb42f9e06c9f4Ted Kremenek ((signed char) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always true}} 1365dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}} 1375dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a < (unsigned int) C) + // expected-warning {{comparison of integers of different signs}} 138323ed74658bc8375278eabf074b4777458376540John McCall ((short) a < (unsigned short) C) + 139323ed74658bc8375278eabf074b4777458376540John McCall ((signed char) a < (unsigned char) C) + 1405dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall 1415dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall // (0x80000,b) 1425dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (0x80000 == (unsigned long) b) + 1435dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (0x80000 == (unsigned int) b) + 144a193f20916f0e0e5a3b0f76ca69e2b3870c1a325Fariborz Jahanian (0x80000 == (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}} 145a193f20916f0e0e5a3b0f76ca69e2b3870c1a325Fariborz Jahanian (0x80000 == (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}} 1465dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) 0x80000 == b) + 1475dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) 0x80000 == b) + 1485dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) 0x80000 == b) + 1495dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) 0x80000 == b) + 1505dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) 0x80000 == (unsigned long) b) + 1515dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) 0x80000 == (unsigned int) b) + 1525dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) 0x80000 == (unsigned short) b) + 1535dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) 0x80000 == (unsigned char) b) + 1545dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (0x80000 < (unsigned long) b) + 1555dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (0x80000 < (unsigned int) b) + 156a193f20916f0e0e5a3b0f76ca69e2b3870c1a325Fariborz Jahanian (0x80000 < (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}} 157a193f20916f0e0e5a3b0f76ca69e2b3870c1a325Fariborz Jahanian (0x80000 < (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}} 1585dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) 0x80000 < b) + 1595dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) 0x80000 < b) + 1605dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) 0x80000 < b) + 1615dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) 0x80000 < b) + 1625dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) 0x80000 < (unsigned long) b) + 1635dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) 0x80000 < (unsigned int) b) + 1645dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) 0x80000 < (unsigned short) b) + 1655dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) 0x80000 < (unsigned char) b) + 1665dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall 1675dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall // (a,0x80000) 1685dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned long) 0x80000) + 1695dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned int) 0x80000) + 1705dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned short) 0x80000) + 1715dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a == (unsigned char) 0x80000) + 1725dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a == 0x80000) + 1735dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a == 0x80000) + 174a193f20916f0e0e5a3b0f76ca69e2b3870c1a325Fariborz Jahanian ((short) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always false}} 175a193f20916f0e0e5a3b0f76ca69e2b3870c1a325Fariborz Jahanian ((signed char) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always false}} 1765dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a == (unsigned long) 0x80000) + 1775dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a == (unsigned int) 0x80000) + 1785dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((short) a == (unsigned short) 0x80000) + 1795dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((signed char) a == (unsigned char) 0x80000) + 1805dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}} 1815dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned int) 0x80000) + 1825dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned short) 0x80000) + 1835dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall (a < (unsigned char) 0x80000) + 1845dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a < 0x80000) + 1855dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a < 0x80000) + 186a193f20916f0e0e5a3b0f76ca69e2b3870c1a325Fariborz Jahanian ((short) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always true}} 187a193f20916f0e0e5a3b0f76ca69e2b3870c1a325Fariborz Jahanian ((signed char) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always true}} 1885dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((long) a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}} 1895dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ((int) a < (unsigned int) 0x80000) + // expected-warning {{comparison of integers of different signs}} 190323ed74658bc8375278eabf074b4777458376540John McCall ((short) a < (unsigned short) 0x80000) + 191323ed74658bc8375278eabf074b4777458376540John McCall ((signed char) a < (unsigned char) 0x80000) + 1925dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall 1935dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall 10 1945dbad3d46c43e8051dac0c3775bcbaf8f1a6b8feJohn McCall ; 195b13c87f0c9705d91d5a3e134be9934c9ad531071John McCall} 196842aef8d942a880eeb9535d40de31a86838264cbJohn McCall 197842aef8d942a880eeb9535d40de31a86838264cbJohn McCallint test1(int i) { 198842aef8d942a880eeb9535d40de31a86838264cbJohn McCall enum en { zero }; 199842aef8d942a880eeb9535d40de31a86838264cbJohn McCall return i > zero; 200842aef8d942a880eeb9535d40de31a86838264cbJohn McCall} 2016e5122c8ce152e19355b707d952ab53fe58bd7adDouglas Gregor 2026e5122c8ce152e19355b707d952ab53fe58bd7adDouglas Gregorenum E { e }; 2036e5122c8ce152e19355b707d952ab53fe58bd7adDouglas Gregorvoid test2(int i, void *vp) { 2046e5122c8ce152e19355b707d952ab53fe58bd7adDouglas Gregor if (test1 == vp) { } // expected-warning{{equality comparison between function pointer and void pointer}} 2056e5122c8ce152e19355b707d952ab53fe58bd7adDouglas Gregor if (test1 == e) { } // expected-error{{comparison between pointer and integer}} 2066e5122c8ce152e19355b707d952ab53fe58bd7adDouglas Gregor if (vp < 0) { } 2076e5122c8ce152e19355b707d952ab53fe58bd7adDouglas Gregor if (test1 < e) { } // expected-error{{comparison between pointer and integer}} 2086e5122c8ce152e19355b707d952ab53fe58bd7adDouglas Gregor} 2093e026e323f1bd820fb9c880b1db951c87df94bb4Douglas Gregor 2103e026e323f1bd820fb9c880b1db951c87df94bb4Douglas Gregor// PR7536 2113e026e323f1bd820fb9c880b1db951c87df94bb4Douglas Gregorstatic const unsigned int kMax = 0; 2123e026e323f1bd820fb9c880b1db951c87df94bb4Douglas Gregorint pr7536() { 2133e026e323f1bd820fb9c880b1db951c87df94bb4Douglas Gregor return (kMax > 0); 2143e026e323f1bd820fb9c880b1db951c87df94bb4Douglas Gregor} 2155254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu 2165254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu// -Wsign-compare should not warn when ?: operands have different signedness. 2175254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu// This will be caught by -Wsign-conversion 2185254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieuvoid test3() { 2195254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu unsigned long a; 2205254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu signed long b; 2215254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu (void) (true ? a : b); 2225254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu (void) (true ? (unsigned int)a : (signed int)b); 2235254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu (void) (true ? b : a); 2245254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu (void) (true ? (unsigned char)b : (signed char)a); 2255254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu} 226526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 227526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu// Test comparison of short to unsigned. If tautological compare does not 228526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu// trigger, then the signed comparision warning will. 229526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieuvoid test4(short s) { 230526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // A is max short plus 1. All zero and positive shorts are smaller than it. 231526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // All negative shorts are cast towards the max unsigned range. Relation 232526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // comparisons are possible, but equality comparisons are tautological. 233526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu const unsigned A = 32768; 234526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu void (s < A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 235526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu void (s > A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 236526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu void (s <= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 237526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu void (s >= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 238526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 239526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu void (s == A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always false}} 240526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu void (s != A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always true}} 241526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 242526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // When negative one is converted to an unsigned value, it becomes the max 243526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // unsigned. Likewise, a negative one short can also be converted to max 244526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // unsigned. 245526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu const unsigned B = -1; 246526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu void (s < B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 247526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu void (s > B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 248526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu void (s <= B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 249526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu void (s >= B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 250526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu void (s == B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 251526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu void (s != B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 252526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 253526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu} 254526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 255526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieuvoid test5(bool b) { 256526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b < -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}} 257526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b > -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}} 258526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b == -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}} 259526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b != -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}} 260526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b <= -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}} 261526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b >= -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}} 262526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 263526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b < -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}} 264526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b > -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}} 265526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b == -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}} 266526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b != -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}} 267526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b <= -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}} 268526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b >= -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}} 269526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 270526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b < 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}} 271526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b > 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}} 272526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b == 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}} 273526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b != 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}} 274526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b <= 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}} 275526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b >= 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}} 276526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 277526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b < 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}} 278526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b > 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}} 279526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b == 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}} 280526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b != 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}} 281526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b <= 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}} 282526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void) (b >= 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}} 283526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu} 284526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 285526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieuvoid test6(signed char sc) { 286526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)(sc < 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} 287526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)(sc > 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} 288526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)(sc <= 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} 289526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)(sc >= 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} 290526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)(sc == 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} 291526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)(sc != 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} 292526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 293526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)(200 < sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} 294526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)(200 > sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} 295526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)(200 <= sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} 296526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)(200 >= sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} 297526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)(200 == sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} 298526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)(200 != sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} 299526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu} 300526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 301526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu// Test many signedness combinations. 302526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieuvoid test7(unsigned long other) { 303526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // Common unsigned, other unsigned, constant unsigned 304526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((unsigned)other != (unsigned long)(0x1ffffffff)); // expected-warning{{true}} 305526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((unsigned)other != (unsigned long)(0xffffffff)); 306526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((unsigned long)other != (unsigned)(0x1ffffffff)); 307526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((unsigned long)other != (unsigned)(0xffffffff)); 308526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 309526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // Common unsigned, other signed, constant unsigned 310526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((int)other != (unsigned long)(0xffffffffffffffff)); // expected-warning{{different signs}} 311526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((int)other != (unsigned long)(0x00000000ffffffff)); // expected-warning{{true}} 312526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((int)other != (unsigned long)(0x000000000fffffff)); 313526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((int)other < (unsigned long)(0x00000000ffffffff)); // expected-warning{{different signs}} 3145d1cf4f292cb060b1973eb197607fc6d5716bd12Richard Trieu (void)((int)other == (unsigned)(0x800000000)); 315526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 316526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // Common unsigned, other unsigned, constant signed 317526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((unsigned long)other != (int)(0xffffffff)); // expected-warning{{different signs}} 318526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 319526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // Common unsigned, other signed, constant signed 320526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // Should not be possible as the common type should also be signed. 321526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 322526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // Common signed, other signed, constant signed 323526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((int)other != (long)(0xffffffff)); // expected-warning{{true}} 324526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((int)other != (long)(0xffffffff00000000)); // expected-warning{{true}} 325526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((int)other != (long)(0xfffffff)); 326526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((int)other != (long)(0xfffffffff0000000)); 327526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 328526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // Common signed, other signed, constant unsigned 329526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((int)other != (unsigned char)(0xffff)); 330526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((int)other != (unsigned char)(0xff)); 331526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 332526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // Common signed, other unsigned, constant signed 333526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((unsigned char)other != (int)(0xff)); 334526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((unsigned char)other != (int)(0xffff)); // expected-warning{{true}} 335526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu 336526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu // Common signed, other unsigned, constant unsigned 337526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((unsigned char)other != (unsigned short)(0xff)); 338526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((unsigned char)other != (unsigned short)(0x100)); // expected-warning{{true}} 339526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu (void)((unsigned short)other != (unsigned char)(0xff)); 340526e627d2bd7e8cbf630526d315c90864898d9ffRichard Trieu} 3418f50b24c8b17368f68c2e71240d487dde53f6da8Richard Trieu 3428f50b24c8b17368f68c2e71240d487dde53f6da8Richard Trieuvoid test8(int x) { 3438f50b24c8b17368f68c2e71240d487dde53f6da8Richard Trieu enum E { 3448f50b24c8b17368f68c2e71240d487dde53f6da8Richard Trieu Negative = -1, 3458f50b24c8b17368f68c2e71240d487dde53f6da8Richard Trieu Positive = 1 3468f50b24c8b17368f68c2e71240d487dde53f6da8Richard Trieu }; 3478f50b24c8b17368f68c2e71240d487dde53f6da8Richard Trieu 3488f50b24c8b17368f68c2e71240d487dde53f6da8Richard Trieu (void)((E)x == 1); 3498f50b24c8b17368f68c2e71240d487dde53f6da8Richard Trieu (void)((E)x == -1); 3508f50b24c8b17368f68c2e71240d487dde53f6da8Richard Trieu} 351d87de7bdbc25dd84ae3dbefffda4539c443000d2Eli Friedman 352d87de7bdbc25dd84ae3dbefffda4539c443000d2Eli Friedmanvoid test9(int x) { 353d87de7bdbc25dd84ae3dbefffda4539c443000d2Eli Friedman enum E : int { 354d87de7bdbc25dd84ae3dbefffda4539c443000d2Eli Friedman Positive = 1 355d87de7bdbc25dd84ae3dbefffda4539c443000d2Eli Friedman }; 356d87de7bdbc25dd84ae3dbefffda4539c443000d2Eli Friedman (void)((E)x == 1); 357d87de7bdbc25dd84ae3dbefffda4539c443000d2Eli Friedman} 358