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