1*67e74705SXin Li // Force x86-64 because some of our heuristics are actually based 2*67e74705SXin Li // on integer sizes. 3*67e74705SXin Li 4*67e74705SXin Li // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -verify -std=c++11 %s 5*67e74705SXin Li 6*67e74705SXin Li namespace RuntimeBehavior { 7*67e74705SXin Li // Avoid emitting tautological compare warnings when the code already has 8*67e74705SXin Li // compile time checks on variable sizes. 9*67e74705SXin Li 10*67e74705SXin Li const int kintmax = 2147483647; test0(short x)11*67e74705SXin Li void test0(short x) { 12*67e74705SXin Li if (sizeof(x) < sizeof(int) || x < kintmax) {} 13*67e74705SXin Li 14*67e74705SXin Li if (x < kintmax) {} 15*67e74705SXin Li // expected-warning@-1{{comparison of constant 2147483647 with expression of type 'short' is always true}} 16*67e74705SXin Li } 17*67e74705SXin Li test1(short x)18*67e74705SXin Li void test1(short x) { 19*67e74705SXin Li if (x < kintmax) {} 20*67e74705SXin Li // expected-warning@-1{{comparison of constant 2147483647 with expression of type 'short' is always true}} 21*67e74705SXin Li 22*67e74705SXin Li if (sizeof(x) < sizeof(int)) 23*67e74705SXin Li return; 24*67e74705SXin Li 25*67e74705SXin Li if (x < kintmax) {} 26*67e74705SXin Li } 27*67e74705SXin Li } 28*67e74705SXin Li 29*67e74705SXin Li namespace ArrayCompare { 30*67e74705SXin Li #define GetValue(ptr) ((ptr != 0) ? ptr[0] : 0) 31*67e74705SXin Li extern int a[] __attribute__((weak)); 32*67e74705SXin Li int b[] = {8,13,21}; 33*67e74705SXin Li struct { 34*67e74705SXin Li int x[10]; 35*67e74705SXin Li } c; 36*67e74705SXin Li const char str[] = "text"; ignore()37*67e74705SXin Li void ignore() { 38*67e74705SXin Li if (a == 0) {} 39*67e74705SXin Li if (a != 0) {} 40*67e74705SXin Li (void)GetValue(b); 41*67e74705SXin Li } test()42*67e74705SXin Li void test() { 43*67e74705SXin Li if (b == 0) {} 44*67e74705SXin Li // expected-warning@-1{{comparison of array 'b' equal to a null pointer is always false}} 45*67e74705SXin Li if (b != 0) {} 46*67e74705SXin Li // expected-warning@-1{{comparison of array 'b' not equal to a null pointer is always true}} 47*67e74705SXin Li if (0 == b) {} 48*67e74705SXin Li // expected-warning@-1{{comparison of array 'b' equal to a null pointer is always false}} 49*67e74705SXin Li if (0 != b) {} 50*67e74705SXin Li // expected-warning@-1{{comparison of array 'b' not equal to a null pointer is always true}} 51*67e74705SXin Li if (c.x == 0) {} 52*67e74705SXin Li // expected-warning@-1{{comparison of array 'c.x' equal to a null pointer is always false}} 53*67e74705SXin Li if (c.x != 0) {} 54*67e74705SXin Li // expected-warning@-1{{comparison of array 'c.x' not equal to a null pointer is always true}} 55*67e74705SXin Li if (str == 0) {} 56*67e74705SXin Li // expected-warning@-1{{comparison of array 'str' equal to a null pointer is always false}} 57*67e74705SXin Li if (str != 0) {} 58*67e74705SXin Li // expected-warning@-1{{comparison of array 'str' not equal to a null pointer is always true}} 59*67e74705SXin Li } 60*67e74705SXin Li } 61*67e74705SXin Li 62*67e74705SXin Li namespace FunctionCompare { 63*67e74705SXin Li #define CallFunction(f) ((f != 0) ? f() : 0) 64*67e74705SXin Li extern void a() __attribute__((weak)); 65*67e74705SXin Li void fun1(); 66*67e74705SXin Li int fun2(); 67*67e74705SXin Li int* fun3(); 68*67e74705SXin Li int* fun4(int); 69*67e74705SXin Li class S { 70*67e74705SXin Li public: 71*67e74705SXin Li static int foo(); 72*67e74705SXin Li }; ignore()73*67e74705SXin Li void ignore() { 74*67e74705SXin Li if (a == 0) {} 75*67e74705SXin Li if (0 != a) {} 76*67e74705SXin Li (void)CallFunction(fun2); 77*67e74705SXin Li } test()78*67e74705SXin Li void test() { 79*67e74705SXin Li if (fun1 == 0) {} 80*67e74705SXin Li // expected-warning@-1{{comparison of function 'fun1' equal to a null pointer is always false}} 81*67e74705SXin Li // expected-note@-2{{prefix with the address-of operator to silence this warning}} 82*67e74705SXin Li if (fun2 == 0) {} 83*67e74705SXin Li // expected-warning@-1{{comparison of function 'fun2' equal to a null pointer is always false}} 84*67e74705SXin Li // expected-note@-2{{prefix with the address-of operator to silence this warning}} 85*67e74705SXin Li // expected-note@-3{{suffix with parentheses to turn this into a function call}} 86*67e74705SXin Li if (fun3 == 0) {} 87*67e74705SXin Li // expected-warning@-1{{comparison of function 'fun3' equal to a null pointer is always false}} 88*67e74705SXin Li // expected-note@-2{{prefix with the address-of operator to silence this warning}} 89*67e74705SXin Li // expected-note@-3{{suffix with parentheses to turn this into a function call}} 90*67e74705SXin Li if (fun4 == 0) {} 91*67e74705SXin Li // expected-warning@-1{{comparison of function 'fun4' equal to a null pointer is always false}} 92*67e74705SXin Li // expected-note@-2{{prefix with the address-of operator to silence this warning}} 93*67e74705SXin Li if (nullptr != fun1) {} 94*67e74705SXin Li // expected-warning@-1{{comparison of function 'fun1' not equal to a null pointer is always true}} 95*67e74705SXin Li // expected-note@-2{{prefix with the address-of operator to silence this warning}} 96*67e74705SXin Li if (nullptr != fun2) {} 97*67e74705SXin Li // expected-warning@-1{{comparison of function 'fun2' not equal to a null pointer is always true}} 98*67e74705SXin Li // expected-note@-2{{prefix with the address-of operator to silence this warning}} 99*67e74705SXin Li if (nullptr != fun3) {} 100*67e74705SXin Li // expected-warning@-1{{comparison of function 'fun3' not equal to a null pointer is always true}} 101*67e74705SXin Li // expected-note@-2{{prefix with the address-of operator to silence this warning}} 102*67e74705SXin Li // expected-note@-3{{suffix with parentheses to turn this into a function call}} 103*67e74705SXin Li if (nullptr != fun4) {} 104*67e74705SXin Li // expected-warning@-1{{comparison of function 'fun4' not equal to a null pointer is always true}} 105*67e74705SXin Li // expected-note@-2{{prefix with the address-of operator to silence this warning}} 106*67e74705SXin Li if (S::foo == 0) {} 107*67e74705SXin Li // expected-warning@-1{{comparison of function 'S::foo' equal to a null pointer is always false}} 108*67e74705SXin Li // expected-note@-2{{prefix with the address-of operator to silence this warning}} 109*67e74705SXin Li // expected-note@-3{{suffix with parentheses to turn this into a function call}} 110*67e74705SXin Li } 111*67e74705SXin Li } 112*67e74705SXin Li 113*67e74705SXin Li namespace PointerCompare { 114*67e74705SXin Li extern int a __attribute__((weak)); 115*67e74705SXin Li int b; 116*67e74705SXin Li static int c; 117*67e74705SXin Li class S { 118*67e74705SXin Li public: 119*67e74705SXin Li static int a; 120*67e74705SXin Li int b; 121*67e74705SXin Li }; ignored()122*67e74705SXin Li void ignored() { 123*67e74705SXin Li if (&a == 0) {} 124*67e74705SXin Li } test()125*67e74705SXin Li void test() { 126*67e74705SXin Li S s; 127*67e74705SXin Li if (&b == 0) {} 128*67e74705SXin Li // expected-warning@-1{{comparison of address of 'b' equal to a null pointer is always false}} 129*67e74705SXin Li if (&c == 0) {} 130*67e74705SXin Li // expected-warning@-1{{comparison of address of 'c' equal to a null pointer is always false}} 131*67e74705SXin Li if (&s.a == 0) {} 132*67e74705SXin Li // expected-warning@-1{{comparison of address of 's.a' equal to a null pointer is always false}} 133*67e74705SXin Li if (&s.b == 0) {} 134*67e74705SXin Li // expected-warning@-1{{comparison of address of 's.b' equal to a null pointer is always false}} 135*67e74705SXin Li if (&S::a == 0) {} 136*67e74705SXin Li // expected-warning@-1{{comparison of address of 'S::a' equal to a null pointer is always false}} 137*67e74705SXin Li } 138*67e74705SXin Li } 139*67e74705SXin Li 140*67e74705SXin Li namespace macros { 141*67e74705SXin Li #define assert(x) if (x) {} 142*67e74705SXin Li int array[5]; 143*67e74705SXin Li void fun(); 144*67e74705SXin Li int x; 145*67e74705SXin Li test()146*67e74705SXin Li void test() { 147*67e74705SXin Li assert(array == 0); 148*67e74705SXin Li // expected-warning@-1{{comparison of array 'array' equal to a null pointer is always false}} 149*67e74705SXin Li assert(array != 0); 150*67e74705SXin Li // expected-warning@-1{{comparison of array 'array' not equal to a null pointer is always true}} 151*67e74705SXin Li assert(array == 0 && "expecting null pointer"); 152*67e74705SXin Li // expected-warning@-1{{comparison of array 'array' equal to a null pointer is always false}} 153*67e74705SXin Li assert(array != 0 && "expecting non-null pointer"); 154*67e74705SXin Li // expected-warning@-1{{comparison of array 'array' not equal to a null pointer is always true}} 155*67e74705SXin Li 156*67e74705SXin Li assert(fun == 0); 157*67e74705SXin Li // expected-warning@-1{{comparison of function 'fun' equal to a null pointer is always false}} 158*67e74705SXin Li // expected-note@-2{{prefix with the address-of operator to silence this warning}} 159*67e74705SXin Li assert(fun != 0); 160*67e74705SXin Li // expected-warning@-1{{comparison of function 'fun' not equal to a null pointer is always true}} 161*67e74705SXin Li // expected-note@-2{{prefix with the address-of operator to silence this warning}} 162*67e74705SXin Li assert(fun == 0 && "expecting null pointer"); 163*67e74705SXin Li // expected-warning@-1{{comparison of function 'fun' equal to a null pointer is always false}} 164*67e74705SXin Li // expected-note@-2{{prefix with the address-of operator to silence this warning}} 165*67e74705SXin Li assert(fun != 0 && "expecting non-null pointer"); 166*67e74705SXin Li // expected-warning@-1{{comparison of function 'fun' not equal to a null pointer is always true}} 167*67e74705SXin Li // expected-note@-2{{prefix with the address-of operator to silence this warning}} 168*67e74705SXin Li 169*67e74705SXin Li assert(&x == 0); 170*67e74705SXin Li // expected-warning@-1{{comparison of address of 'x' equal to a null pointer is always false}} 171*67e74705SXin Li assert(&x != 0); 172*67e74705SXin Li // expected-warning@-1{{comparison of address of 'x' not equal to a null pointer is always true}} 173*67e74705SXin Li assert(&x == 0 && "expecting null pointer"); 174*67e74705SXin Li // expected-warning@-1{{comparison of address of 'x' equal to a null pointer is always false}} 175*67e74705SXin Li assert(&x != 0 && "expecting non-null pointer"); 176*67e74705SXin Li // expected-warning@-1{{comparison of address of 'x' not equal to a null pointer is always true}} 177*67e74705SXin Li } 178*67e74705SXin Li } 179