1*f4a2713aSLionel Sambuc // Force x86-64 because some of our heuristics are actually based 2*f4a2713aSLionel Sambuc // on integer sizes. 3*f4a2713aSLionel Sambuc 4*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare -std=c++11 %s 5*f4a2713aSLionel Sambuc 6*f4a2713aSLionel Sambuc int test0(long a, unsigned long b) { 7*f4a2713aSLionel Sambuc enum EnumA {A}; 8*f4a2713aSLionel Sambuc enum EnumB {B}; 9*f4a2713aSLionel Sambuc enum EnumC {C = 0x10000}; 10*f4a2713aSLionel Sambuc return 11*f4a2713aSLionel Sambuc // (a,b) 12*f4a2713aSLionel Sambuc (a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}} 13*f4a2713aSLionel Sambuc (a == (unsigned int) b) + 14*f4a2713aSLionel Sambuc (a == (unsigned short) b) + 15*f4a2713aSLionel Sambuc (a == (unsigned char) b) + 16*f4a2713aSLionel Sambuc ((long) a == b) + // expected-warning {{comparison of integers of different signs}} 17*f4a2713aSLionel Sambuc ((int) a == b) + // expected-warning {{comparison of integers of different signs}} 18*f4a2713aSLionel Sambuc ((short) a == b) + // expected-warning {{comparison of integers of different signs}} 19*f4a2713aSLionel Sambuc ((signed char) a == b) + // expected-warning {{comparison of integers of different signs}} 20*f4a2713aSLionel Sambuc ((long) a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}} 21*f4a2713aSLionel Sambuc ((int) a == (unsigned int) b) + // expected-warning {{comparison of integers of different signs}} 22*f4a2713aSLionel Sambuc ((short) a == (unsigned short) b) + 23*f4a2713aSLionel Sambuc ((signed char) a == (unsigned char) b) + 24*f4a2713aSLionel Sambuc (a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}} 25*f4a2713aSLionel Sambuc (a < (unsigned int) b) + 26*f4a2713aSLionel Sambuc (a < (unsigned short) b) + 27*f4a2713aSLionel Sambuc (a < (unsigned char) b) + 28*f4a2713aSLionel Sambuc ((long) a < b) + // expected-warning {{comparison of integers of different signs}} 29*f4a2713aSLionel Sambuc ((int) a < b) + // expected-warning {{comparison of integers of different signs}} 30*f4a2713aSLionel Sambuc ((short) a < b) + // expected-warning {{comparison of integers of different signs}} 31*f4a2713aSLionel Sambuc ((signed char) a < b) + // expected-warning {{comparison of integers of different signs}} 32*f4a2713aSLionel Sambuc ((long) a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}} 33*f4a2713aSLionel Sambuc ((int) a < (unsigned int) b) + // expected-warning {{comparison of integers of different signs}} 34*f4a2713aSLionel Sambuc ((short) a < (unsigned short) b) + 35*f4a2713aSLionel Sambuc ((signed char) a < (unsigned char) b) + 36*f4a2713aSLionel Sambuc 37*f4a2713aSLionel Sambuc // (A,b) 38*f4a2713aSLionel Sambuc (A == (unsigned long) b) + 39*f4a2713aSLionel Sambuc (A == (unsigned int) b) + 40*f4a2713aSLionel Sambuc (A == (unsigned short) b) + 41*f4a2713aSLionel Sambuc (A == (unsigned char) b) + 42*f4a2713aSLionel Sambuc ((long) A == b) + 43*f4a2713aSLionel Sambuc ((int) A == b) + 44*f4a2713aSLionel Sambuc ((short) A == b) + 45*f4a2713aSLionel Sambuc ((signed char) A == b) + 46*f4a2713aSLionel Sambuc ((long) A == (unsigned long) b) + 47*f4a2713aSLionel Sambuc ((int) A == (unsigned int) b) + 48*f4a2713aSLionel Sambuc ((short) A == (unsigned short) b) + 49*f4a2713aSLionel Sambuc ((signed char) A == (unsigned char) b) + 50*f4a2713aSLionel Sambuc (A < (unsigned long) b) + 51*f4a2713aSLionel Sambuc (A < (unsigned int) b) + 52*f4a2713aSLionel Sambuc (A < (unsigned short) b) + 53*f4a2713aSLionel Sambuc (A < (unsigned char) b) + 54*f4a2713aSLionel Sambuc ((long) A < b) + 55*f4a2713aSLionel Sambuc ((int) A < b) + 56*f4a2713aSLionel Sambuc ((short) A < b) + 57*f4a2713aSLionel Sambuc ((signed char) A < b) + 58*f4a2713aSLionel Sambuc ((long) A < (unsigned long) b) + 59*f4a2713aSLionel Sambuc ((int) A < (unsigned int) b) + 60*f4a2713aSLionel Sambuc ((short) A < (unsigned short) b) + 61*f4a2713aSLionel Sambuc ((signed char) A < (unsigned char) b) + 62*f4a2713aSLionel Sambuc 63*f4a2713aSLionel Sambuc // (a,B) 64*f4a2713aSLionel Sambuc (a == (unsigned long) B) + 65*f4a2713aSLionel Sambuc (a == (unsigned int) B) + 66*f4a2713aSLionel Sambuc (a == (unsigned short) B) + 67*f4a2713aSLionel Sambuc (a == (unsigned char) B) + 68*f4a2713aSLionel Sambuc ((long) a == B) + 69*f4a2713aSLionel Sambuc ((int) a == B) + 70*f4a2713aSLionel Sambuc ((short) a == B) + 71*f4a2713aSLionel Sambuc ((signed char) a == B) + 72*f4a2713aSLionel Sambuc ((long) a == (unsigned long) B) + 73*f4a2713aSLionel Sambuc ((int) a == (unsigned int) B) + 74*f4a2713aSLionel Sambuc ((short) a == (unsigned short) B) + 75*f4a2713aSLionel Sambuc ((signed char) a == (unsigned char) B) + 76*f4a2713aSLionel Sambuc (a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}} 77*f4a2713aSLionel Sambuc (a < (unsigned int) B) + 78*f4a2713aSLionel Sambuc (a < (unsigned short) B) + 79*f4a2713aSLionel Sambuc (a < (unsigned char) B) + 80*f4a2713aSLionel Sambuc ((long) a < B) + 81*f4a2713aSLionel Sambuc ((int) a < B) + 82*f4a2713aSLionel Sambuc ((short) a < B) + 83*f4a2713aSLionel Sambuc ((signed char) a < B) + 84*f4a2713aSLionel Sambuc ((long) a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}} 85*f4a2713aSLionel Sambuc ((int) a < (unsigned int) B) + // expected-warning {{comparison of integers of different signs}} 86*f4a2713aSLionel Sambuc ((short) a < (unsigned short) B) + 87*f4a2713aSLionel Sambuc ((signed char) a < (unsigned char) B) + 88*f4a2713aSLionel Sambuc 89*f4a2713aSLionel Sambuc // (C,b) 90*f4a2713aSLionel Sambuc (C == (unsigned long) b) + 91*f4a2713aSLionel Sambuc (C == (unsigned int) b) + 92*f4a2713aSLionel Sambuc (C == (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}} 93*f4a2713aSLionel Sambuc (C == (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}} 94*f4a2713aSLionel Sambuc ((long) C == b) + 95*f4a2713aSLionel Sambuc ((int) C == b) + 96*f4a2713aSLionel Sambuc ((short) C == b) + 97*f4a2713aSLionel Sambuc ((signed char) C == b) + 98*f4a2713aSLionel Sambuc ((long) C == (unsigned long) b) + 99*f4a2713aSLionel Sambuc ((int) C == (unsigned int) b) + 100*f4a2713aSLionel Sambuc ((short) C == (unsigned short) b) + 101*f4a2713aSLionel Sambuc ((signed char) C == (unsigned char) b) + 102*f4a2713aSLionel Sambuc (C < (unsigned long) b) + 103*f4a2713aSLionel Sambuc (C < (unsigned int) b) + 104*f4a2713aSLionel Sambuc (C < (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}} 105*f4a2713aSLionel Sambuc (C < (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}} 106*f4a2713aSLionel Sambuc ((long) C < b) + 107*f4a2713aSLionel Sambuc ((int) C < b) + 108*f4a2713aSLionel Sambuc ((short) C < b) + 109*f4a2713aSLionel Sambuc ((signed char) C < b) + 110*f4a2713aSLionel Sambuc ((long) C < (unsigned long) b) + 111*f4a2713aSLionel Sambuc ((int) C < (unsigned int) b) + 112*f4a2713aSLionel Sambuc ((short) C < (unsigned short) b) + 113*f4a2713aSLionel Sambuc ((signed char) C < (unsigned char) b) + 114*f4a2713aSLionel Sambuc 115*f4a2713aSLionel Sambuc // (a,C) 116*f4a2713aSLionel Sambuc (a == (unsigned long) C) + 117*f4a2713aSLionel Sambuc (a == (unsigned int) C) + 118*f4a2713aSLionel Sambuc (a == (unsigned short) C) + 119*f4a2713aSLionel Sambuc (a == (unsigned char) C) + 120*f4a2713aSLionel Sambuc ((long) a == C) + 121*f4a2713aSLionel Sambuc ((int) a == C) + 122*f4a2713aSLionel Sambuc ((short) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always false}} 123*f4a2713aSLionel Sambuc ((signed char) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always false}} 124*f4a2713aSLionel Sambuc ((long) a == (unsigned long) C) + 125*f4a2713aSLionel Sambuc ((int) a == (unsigned int) C) + 126*f4a2713aSLionel Sambuc ((short) a == (unsigned short) C) + 127*f4a2713aSLionel Sambuc ((signed char) a == (unsigned char) C) + 128*f4a2713aSLionel Sambuc (a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}} 129*f4a2713aSLionel Sambuc (a < (unsigned int) C) + 130*f4a2713aSLionel Sambuc (a < (unsigned short) C) + 131*f4a2713aSLionel Sambuc (a < (unsigned char) C) + 132*f4a2713aSLionel Sambuc ((long) a < C) + 133*f4a2713aSLionel Sambuc ((int) a < C) + 134*f4a2713aSLionel Sambuc ((short) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always true}} 135*f4a2713aSLionel Sambuc ((signed char) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always true}} 136*f4a2713aSLionel Sambuc ((long) a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}} 137*f4a2713aSLionel Sambuc ((int) a < (unsigned int) C) + // expected-warning {{comparison of integers of different signs}} 138*f4a2713aSLionel Sambuc ((short) a < (unsigned short) C) + 139*f4a2713aSLionel Sambuc ((signed char) a < (unsigned char) C) + 140*f4a2713aSLionel Sambuc 141*f4a2713aSLionel Sambuc // (0x80000,b) 142*f4a2713aSLionel Sambuc (0x80000 == (unsigned long) b) + 143*f4a2713aSLionel Sambuc (0x80000 == (unsigned int) b) + 144*f4a2713aSLionel Sambuc (0x80000 == (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}} 145*f4a2713aSLionel Sambuc (0x80000 == (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}} 146*f4a2713aSLionel Sambuc ((long) 0x80000 == b) + 147*f4a2713aSLionel Sambuc ((int) 0x80000 == b) + 148*f4a2713aSLionel Sambuc ((short) 0x80000 == b) + 149*f4a2713aSLionel Sambuc ((signed char) 0x80000 == b) + 150*f4a2713aSLionel Sambuc ((long) 0x80000 == (unsigned long) b) + 151*f4a2713aSLionel Sambuc ((int) 0x80000 == (unsigned int) b) + 152*f4a2713aSLionel Sambuc ((short) 0x80000 == (unsigned short) b) + 153*f4a2713aSLionel Sambuc ((signed char) 0x80000 == (unsigned char) b) + 154*f4a2713aSLionel Sambuc (0x80000 < (unsigned long) b) + 155*f4a2713aSLionel Sambuc (0x80000 < (unsigned int) b) + 156*f4a2713aSLionel Sambuc (0x80000 < (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}} 157*f4a2713aSLionel Sambuc (0x80000 < (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}} 158*f4a2713aSLionel Sambuc ((long) 0x80000 < b) + 159*f4a2713aSLionel Sambuc ((int) 0x80000 < b) + 160*f4a2713aSLionel Sambuc ((short) 0x80000 < b) + 161*f4a2713aSLionel Sambuc ((signed char) 0x80000 < b) + 162*f4a2713aSLionel Sambuc ((long) 0x80000 < (unsigned long) b) + 163*f4a2713aSLionel Sambuc ((int) 0x80000 < (unsigned int) b) + 164*f4a2713aSLionel Sambuc ((short) 0x80000 < (unsigned short) b) + 165*f4a2713aSLionel Sambuc ((signed char) 0x80000 < (unsigned char) b) + 166*f4a2713aSLionel Sambuc 167*f4a2713aSLionel Sambuc // (a,0x80000) 168*f4a2713aSLionel Sambuc (a == (unsigned long) 0x80000) + 169*f4a2713aSLionel Sambuc (a == (unsigned int) 0x80000) + 170*f4a2713aSLionel Sambuc (a == (unsigned short) 0x80000) + 171*f4a2713aSLionel Sambuc (a == (unsigned char) 0x80000) + 172*f4a2713aSLionel Sambuc ((long) a == 0x80000) + 173*f4a2713aSLionel Sambuc ((int) a == 0x80000) + 174*f4a2713aSLionel Sambuc ((short) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always false}} 175*f4a2713aSLionel Sambuc ((signed char) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always false}} 176*f4a2713aSLionel Sambuc ((long) a == (unsigned long) 0x80000) + 177*f4a2713aSLionel Sambuc ((int) a == (unsigned int) 0x80000) + 178*f4a2713aSLionel Sambuc ((short) a == (unsigned short) 0x80000) + 179*f4a2713aSLionel Sambuc ((signed char) a == (unsigned char) 0x80000) + 180*f4a2713aSLionel Sambuc (a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}} 181*f4a2713aSLionel Sambuc (a < (unsigned int) 0x80000) + 182*f4a2713aSLionel Sambuc (a < (unsigned short) 0x80000) + 183*f4a2713aSLionel Sambuc (a < (unsigned char) 0x80000) + 184*f4a2713aSLionel Sambuc ((long) a < 0x80000) + 185*f4a2713aSLionel Sambuc ((int) a < 0x80000) + 186*f4a2713aSLionel Sambuc ((short) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always true}} 187*f4a2713aSLionel Sambuc ((signed char) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always true}} 188*f4a2713aSLionel Sambuc ((long) a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}} 189*f4a2713aSLionel Sambuc ((int) a < (unsigned int) 0x80000) + // expected-warning {{comparison of integers of different signs}} 190*f4a2713aSLionel Sambuc ((short) a < (unsigned short) 0x80000) + 191*f4a2713aSLionel Sambuc ((signed char) a < (unsigned char) 0x80000) + 192*f4a2713aSLionel Sambuc 193*f4a2713aSLionel Sambuc 10 194*f4a2713aSLionel Sambuc ; 195*f4a2713aSLionel Sambuc } 196*f4a2713aSLionel Sambuc 197*f4a2713aSLionel Sambuc int test1(int i) { 198*f4a2713aSLionel Sambuc enum en { zero }; 199*f4a2713aSLionel Sambuc return i > zero; 200*f4a2713aSLionel Sambuc } 201*f4a2713aSLionel Sambuc 202*f4a2713aSLionel Sambuc enum E { e }; 203*f4a2713aSLionel Sambuc void test2(int i, void *vp) { 204*f4a2713aSLionel Sambuc if (test1 == vp) { } // expected-warning{{equality comparison between function pointer and void pointer}} 205*f4a2713aSLionel Sambuc if (test1 == e) { } // expected-error{{comparison between pointer and integer}} 206*f4a2713aSLionel Sambuc if (vp < 0) { } 207*f4a2713aSLionel Sambuc if (test1 < e) { } // expected-error{{comparison between pointer and integer}} 208*f4a2713aSLionel Sambuc } 209*f4a2713aSLionel Sambuc 210*f4a2713aSLionel Sambuc // PR7536 211*f4a2713aSLionel Sambuc static const unsigned int kMax = 0; 212*f4a2713aSLionel Sambuc int pr7536() { 213*f4a2713aSLionel Sambuc return (kMax > 0); 214*f4a2713aSLionel Sambuc } 215*f4a2713aSLionel Sambuc 216*f4a2713aSLionel Sambuc // -Wsign-compare should not warn when ?: operands have different signedness. 217*f4a2713aSLionel Sambuc // This will be caught by -Wsign-conversion 218*f4a2713aSLionel Sambuc void test3() { 219*f4a2713aSLionel Sambuc unsigned long a; 220*f4a2713aSLionel Sambuc signed long b; 221*f4a2713aSLionel Sambuc (void) (true ? a : b); 222*f4a2713aSLionel Sambuc (void) (true ? (unsigned int)a : (signed int)b); 223*f4a2713aSLionel Sambuc (void) (true ? b : a); 224*f4a2713aSLionel Sambuc (void) (true ? (unsigned char)b : (signed char)a); 225*f4a2713aSLionel Sambuc } 226*f4a2713aSLionel Sambuc 227*f4a2713aSLionel Sambuc // Test comparison of short to unsigned. If tautological compare does not 228*f4a2713aSLionel Sambuc // trigger, then the signed comparision warning will. 229*f4a2713aSLionel Sambuc void test4(short s) { 230*f4a2713aSLionel Sambuc // A is max short plus 1. All zero and positive shorts are smaller than it. 231*f4a2713aSLionel Sambuc // All negative shorts are cast towards the max unsigned range. Relation 232*f4a2713aSLionel Sambuc // comparisons are possible, but equality comparisons are tautological. 233*f4a2713aSLionel Sambuc const unsigned A = 32768; 234*f4a2713aSLionel Sambuc void (s < A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 235*f4a2713aSLionel Sambuc void (s > A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 236*f4a2713aSLionel Sambuc void (s <= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 237*f4a2713aSLionel Sambuc void (s >= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 238*f4a2713aSLionel Sambuc 239*f4a2713aSLionel Sambuc void (s == A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always false}} 240*f4a2713aSLionel Sambuc void (s != A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always true}} 241*f4a2713aSLionel Sambuc 242*f4a2713aSLionel Sambuc // When negative one is converted to an unsigned value, it becomes the max 243*f4a2713aSLionel Sambuc // unsigned. Likewise, a negative one short can also be converted to max 244*f4a2713aSLionel Sambuc // unsigned. 245*f4a2713aSLionel Sambuc const unsigned B = -1; 246*f4a2713aSLionel Sambuc void (s < B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 247*f4a2713aSLionel Sambuc void (s > B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 248*f4a2713aSLionel Sambuc void (s <= B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 249*f4a2713aSLionel Sambuc void (s >= B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 250*f4a2713aSLionel Sambuc void (s == B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 251*f4a2713aSLionel Sambuc void (s != B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} 252*f4a2713aSLionel Sambuc 253*f4a2713aSLionel Sambuc } 254*f4a2713aSLionel Sambuc 255*f4a2713aSLionel Sambuc void test5(bool b) { 256*f4a2713aSLionel Sambuc (void) (b < -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}} 257*f4a2713aSLionel Sambuc (void) (b > -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}} 258*f4a2713aSLionel Sambuc (void) (b == -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}} 259*f4a2713aSLionel Sambuc (void) (b != -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}} 260*f4a2713aSLionel Sambuc (void) (b <= -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}} 261*f4a2713aSLionel Sambuc (void) (b >= -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}} 262*f4a2713aSLionel Sambuc 263*f4a2713aSLionel Sambuc (void) (b < -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}} 264*f4a2713aSLionel Sambuc (void) (b > -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}} 265*f4a2713aSLionel Sambuc (void) (b == -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}} 266*f4a2713aSLionel Sambuc (void) (b != -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}} 267*f4a2713aSLionel Sambuc (void) (b <= -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}} 268*f4a2713aSLionel Sambuc (void) (b >= -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}} 269*f4a2713aSLionel Sambuc 270*f4a2713aSLionel Sambuc (void) (b < 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}} 271*f4a2713aSLionel Sambuc (void) (b > 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}} 272*f4a2713aSLionel Sambuc (void) (b == 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}} 273*f4a2713aSLionel Sambuc (void) (b != 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}} 274*f4a2713aSLionel Sambuc (void) (b <= 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}} 275*f4a2713aSLionel Sambuc (void) (b >= 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}} 276*f4a2713aSLionel Sambuc 277*f4a2713aSLionel Sambuc (void) (b < 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}} 278*f4a2713aSLionel Sambuc (void) (b > 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}} 279*f4a2713aSLionel Sambuc (void) (b == 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}} 280*f4a2713aSLionel Sambuc (void) (b != 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}} 281*f4a2713aSLionel Sambuc (void) (b <= 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}} 282*f4a2713aSLionel Sambuc (void) (b >= 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}} 283*f4a2713aSLionel Sambuc } 284*f4a2713aSLionel Sambuc 285*f4a2713aSLionel Sambuc void test6(signed char sc) { 286*f4a2713aSLionel Sambuc (void)(sc < 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} 287*f4a2713aSLionel Sambuc (void)(sc > 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} 288*f4a2713aSLionel Sambuc (void)(sc <= 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} 289*f4a2713aSLionel Sambuc (void)(sc >= 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} 290*f4a2713aSLionel Sambuc (void)(sc == 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} 291*f4a2713aSLionel Sambuc (void)(sc != 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} 292*f4a2713aSLionel Sambuc 293*f4a2713aSLionel Sambuc (void)(200 < sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} 294*f4a2713aSLionel Sambuc (void)(200 > sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} 295*f4a2713aSLionel Sambuc (void)(200 <= sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} 296*f4a2713aSLionel Sambuc (void)(200 >= sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} 297*f4a2713aSLionel Sambuc (void)(200 == sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} 298*f4a2713aSLionel Sambuc (void)(200 != sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} 299*f4a2713aSLionel Sambuc } 300*f4a2713aSLionel Sambuc 301*f4a2713aSLionel Sambuc // Test many signedness combinations. 302*f4a2713aSLionel Sambuc void test7(unsigned long other) { 303*f4a2713aSLionel Sambuc // Common unsigned, other unsigned, constant unsigned 304*f4a2713aSLionel Sambuc (void)((unsigned)other != (unsigned long)(0x1ffffffff)); // expected-warning{{true}} 305*f4a2713aSLionel Sambuc (void)((unsigned)other != (unsigned long)(0xffffffff)); 306*f4a2713aSLionel Sambuc (void)((unsigned long)other != (unsigned)(0x1ffffffff)); 307*f4a2713aSLionel Sambuc (void)((unsigned long)other != (unsigned)(0xffffffff)); 308*f4a2713aSLionel Sambuc 309*f4a2713aSLionel Sambuc // Common unsigned, other signed, constant unsigned 310*f4a2713aSLionel Sambuc (void)((int)other != (unsigned long)(0xffffffffffffffff)); // expected-warning{{different signs}} 311*f4a2713aSLionel Sambuc (void)((int)other != (unsigned long)(0x00000000ffffffff)); // expected-warning{{true}} 312*f4a2713aSLionel Sambuc (void)((int)other != (unsigned long)(0x000000000fffffff)); 313*f4a2713aSLionel Sambuc (void)((int)other < (unsigned long)(0x00000000ffffffff)); // expected-warning{{different signs}} 314*f4a2713aSLionel Sambuc (void)((int)other == (unsigned)(0x800000000)); 315*f4a2713aSLionel Sambuc 316*f4a2713aSLionel Sambuc // Common unsigned, other unsigned, constant signed 317*f4a2713aSLionel Sambuc (void)((unsigned long)other != (int)(0xffffffff)); // expected-warning{{different signs}} 318*f4a2713aSLionel Sambuc 319*f4a2713aSLionel Sambuc // Common unsigned, other signed, constant signed 320*f4a2713aSLionel Sambuc // Should not be possible as the common type should also be signed. 321*f4a2713aSLionel Sambuc 322*f4a2713aSLionel Sambuc // Common signed, other signed, constant signed 323*f4a2713aSLionel Sambuc (void)((int)other != (long)(0xffffffff)); // expected-warning{{true}} 324*f4a2713aSLionel Sambuc (void)((int)other != (long)(0xffffffff00000000)); // expected-warning{{true}} 325*f4a2713aSLionel Sambuc (void)((int)other != (long)(0xfffffff)); 326*f4a2713aSLionel Sambuc (void)((int)other != (long)(0xfffffffff0000000)); 327*f4a2713aSLionel Sambuc 328*f4a2713aSLionel Sambuc // Common signed, other signed, constant unsigned 329*f4a2713aSLionel Sambuc (void)((int)other != (unsigned char)(0xffff)); 330*f4a2713aSLionel Sambuc (void)((int)other != (unsigned char)(0xff)); 331*f4a2713aSLionel Sambuc 332*f4a2713aSLionel Sambuc // Common signed, other unsigned, constant signed 333*f4a2713aSLionel Sambuc (void)((unsigned char)other != (int)(0xff)); 334*f4a2713aSLionel Sambuc (void)((unsigned char)other != (int)(0xffff)); // expected-warning{{true}} 335*f4a2713aSLionel Sambuc 336*f4a2713aSLionel Sambuc // Common signed, other unsigned, constant unsigned 337*f4a2713aSLionel Sambuc (void)((unsigned char)other != (unsigned short)(0xff)); 338*f4a2713aSLionel Sambuc (void)((unsigned char)other != (unsigned short)(0x100)); // expected-warning{{true}} 339*f4a2713aSLionel Sambuc (void)((unsigned short)other != (unsigned char)(0xff)); 340*f4a2713aSLionel Sambuc } 341*f4a2713aSLionel Sambuc 342*f4a2713aSLionel Sambuc void test8(int x) { 343*f4a2713aSLionel Sambuc enum E { 344*f4a2713aSLionel Sambuc Negative = -1, 345*f4a2713aSLionel Sambuc Positive = 1 346*f4a2713aSLionel Sambuc }; 347*f4a2713aSLionel Sambuc 348*f4a2713aSLionel Sambuc (void)((E)x == 1); 349*f4a2713aSLionel Sambuc (void)((E)x == -1); 350*f4a2713aSLionel Sambuc } 351*f4a2713aSLionel Sambuc 352*f4a2713aSLionel Sambuc void test9(int x) { 353*f4a2713aSLionel Sambuc enum E : int { 354*f4a2713aSLionel Sambuc Positive = 1 355*f4a2713aSLionel Sambuc }; 356*f4a2713aSLionel Sambuc (void)((E)x == 1); 357*f4a2713aSLionel Sambuc } 358*f4a2713aSLionel Sambuc 359*f4a2713aSLionel Sambuc namespace templates { 360*f4a2713aSLionel Sambuc template<class T> T max(); 361*f4a2713aSLionel Sambuc 362*f4a2713aSLionel Sambuc template<> constexpr int max<int>() { return 2147483647; }; 363*f4a2713aSLionel Sambuc 364*f4a2713aSLionel Sambuc template<typename T> 365*f4a2713aSLionel Sambuc bool less_than_max(short num, T value) { 366*f4a2713aSLionel Sambuc const T vmax = max<T>(); 367*f4a2713aSLionel Sambuc return (vmax >= num); // no warning 368*f4a2713aSLionel Sambuc } 369*f4a2713aSLionel Sambuc 370*f4a2713aSLionel Sambuc template<typename T> 371*f4a2713aSLionel Sambuc bool less_than_max(short num) { 372*f4a2713aSLionel Sambuc // This should trigger one warning on the template pattern, and not a 373*f4a2713aSLionel Sambuc // warning per specialization. 374*f4a2713aSLionel Sambuc return num < max<int>(); // expected-warning{{comparison of constant 2147483647 with expression of type 'short' is always true}} 375*f4a2713aSLionel Sambuc } 376*f4a2713aSLionel Sambuc 377*f4a2713aSLionel Sambuc void test10(short num, int x) { 378*f4a2713aSLionel Sambuc less_than_max(num, x); 379*f4a2713aSLionel Sambuc less_than_max<int>(num); 380*f4a2713aSLionel Sambuc less_than_max<long>(num); 381*f4a2713aSLionel Sambuc less_than_max<short>(num); 382*f4a2713aSLionel Sambuc } 383*f4a2713aSLionel Sambuc 384*f4a2713aSLionel Sambuc template<typename T> 385*f4a2713aSLionel Sambuc inline bool less_than_zero(T num, T value) { 386*f4a2713aSLionel Sambuc return num < 0; // no warning 387*f4a2713aSLionel Sambuc } 388*f4a2713aSLionel Sambuc 389*f4a2713aSLionel Sambuc template<typename T> 390*f4a2713aSLionel Sambuc inline bool less_than_zero(unsigned num) { 391*f4a2713aSLionel Sambuc // This should trigger one warning on the template pattern, and not a 392*f4a2713aSLionel Sambuc // warning per specialization. 393*f4a2713aSLionel Sambuc return num < 0; // expected-warning{{comparison of unsigned expression < 0 is always false}} 394*f4a2713aSLionel Sambuc } 395*f4a2713aSLionel Sambuc 396*f4a2713aSLionel Sambuc void test11(unsigned num) { 397*f4a2713aSLionel Sambuc less_than_zero(num, num); 398*f4a2713aSLionel Sambuc less_than_zero<int>(num); 399*f4a2713aSLionel Sambuc less_than_zero<long>(num); 400*f4a2713aSLionel Sambuc less_than_zero<short>(num); 401*f4a2713aSLionel Sambuc } 402*f4a2713aSLionel Sambuc 403*f4a2713aSLionel Sambuc template<unsigned n> bool compare(unsigned k) { return k >= n; } 404*f4a2713aSLionel Sambuc 405*f4a2713aSLionel Sambuc void test12() { 406*f4a2713aSLionel Sambuc compare<0>(42); 407*f4a2713aSLionel Sambuc } 408*f4a2713aSLionel Sambuc 409*f4a2713aSLionel Sambuc struct A { static int x; }; 410*f4a2713aSLionel Sambuc struct B { static int x; }; 411*f4a2713aSLionel Sambuc typedef A otherA; 412*f4a2713aSLionel Sambuc 413*f4a2713aSLionel Sambuc template <typename T> 414*f4a2713aSLionel Sambuc void testx() { 415*f4a2713aSLionel Sambuc if (A::x == T::x && // no warning 416*f4a2713aSLionel Sambuc A::x == otherA::x) // expected-warning{{self-comparison always evaluates to true}} 417*f4a2713aSLionel Sambuc return; 418*f4a2713aSLionel Sambuc } 419*f4a2713aSLionel Sambuc 420*f4a2713aSLionel Sambuc void test13() { 421*f4a2713aSLionel Sambuc testx<A>(); 422*f4a2713aSLionel Sambuc testx<B>(); 423*f4a2713aSLionel Sambuc } 424*f4a2713aSLionel Sambuc } 425