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