1 // RUN: %clang_cc1 -fsyntax-only -verify=expected,expected-cxx11 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify=expected,expected-cxx11 -std=c++11 %s
4
5 namespace BooleanFalse {
6 int* j = false;
7 #if __cplusplus <= 199711L
8 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
9 #else
10 // expected-error@-4 {{cannot initialize a variable of type 'int *' with an rvalue of type 'bool'}}
11 #endif
12
13 #if __cplusplus <= 199711L
14 // expected-warning@+5 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
15 #else
16 // expected-error@+3 {{cannot initialize a parameter of type 'int *' with an rvalue of type 'bool'}}
17 // expected-note@+2 {{passing argument to parameter 'j' here}}
18 #endif
19 void bar(int *j = false);
20
21 #if __cplusplus > 199711L
22 // expected-note@+2 4{{candidate function not viable: no known conversion}}
23 #endif
foo(int * i)24 void foo(int *i)
25 {
26 foo(false);
27 #if __cplusplus <= 199711L
28 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
29 #else
30 // expected-error@-4 {{no matching function for call to 'foo'}}
31 #endif
32
33 foo((int*)false); // OK: explicit cast
34 foo(0); // OK: not a bool, even though it's convertible to bool
35
36 foo(false == true);
37 #if __cplusplus <= 199711L
38 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
39 #else
40 // expected-error@-4 {{no matching function for call to 'foo'}}
41 #endif
42
43 foo((42 + 24) < 32);
44 #if __cplusplus <= 199711L
45 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
46 #else
47 // expected-error@-4 {{no matching function for call to 'foo'}}
48 #endif
49
50 const bool kFlag = false;
51 foo(kFlag);
52 #if __cplusplus <= 199711L
53 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
54 #else
55 // expected-error@-4 {{no matching function for call to 'foo'}}
56 #endif
57 }
58
59 char f(struct Undefined*);
60 double f(...);
61
62 // Ensure that when using false in metaprogramming machinery its conversion
63 // isn't flagged.
64 template <int N> struct S {};
65 S<sizeof(f(false))> s;
66
67 }
68
69 namespace Function {
70 void f1();
71
72 struct S {
73 static void f2();
74 };
75
76 extern void f3() __attribute__((weak_import));
77
78 struct S2 {
79 static void f4() __attribute__((weak_import));
80 };
81
82 bool f5();
83 bool f6(int);
84 #if __cplusplus >= 201103L
__anon37b3cc020102null85 auto f7 = []{};
__anon37b3cc020202()86 auto f8 = [](){};
87
foo()88 void foo() {
89 bool b;
90 b = f7; // expected-warning {{address of lambda function pointer conversion operator will always evaluate to 'true'}}
91 b = f8; // expected-warning {{address of lambda function pointer conversion operator will always evaluate to 'true'}}
92 bool is_true = [](){ return true; };
93 // expected-warning@-1{{address of lambda function pointer conversion operator will always evaluate to 'true'}}
94 }
95
96 template <typename... Ts>
IsFalse(const Ts &...)97 static bool IsFalse(const Ts&...) { return false; }
98 template <typename T>
IsFalse(const T & p)99 static bool IsFalse(const T& p) {
100 bool b;
101 b = f7; // expected-warning {{address of lambda function pointer conversion operator will always evaluate to 'true'}}
102 // Intentionally not warned on because p could be a lambda type in one
103 // instantiation, but a pointer type in another.
104 return p ? false : true;
105 }
106
use_instantiation()107 bool use_instantiation() {
108 return IsFalse([]() { return 0; });
109 }
110 #endif
111
bar()112 void bar() {
113 bool b;
114
115 b = f1; // expected-warning {{address of function 'f1' will always evaluate to 'true'}} \
116 expected-note {{prefix with the address-of operator to silence this warning}}
117 if (f1) {} // expected-warning {{address of function 'f1' will always evaluate to 'true'}} \
118 expected-note {{prefix with the address-of operator to silence this warning}}
119 b = S::f2; // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}} \
120 expected-note {{prefix with the address-of operator to silence this warning}}
121 if (S::f2) {} // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}} \
122 expected-note {{prefix with the address-of operator to silence this warning}}
123 b = f5; // expected-warning {{address of function 'f5' will always evaluate to 'true'}} \
124 expected-note {{prefix with the address-of operator to silence this warning}} \
125 expected-note {{suffix with parentheses to turn this into a function call}}
126 b = f6; // expected-warning {{address of function 'f6' will always evaluate to 'true'}} \
127 expected-note {{prefix with the address-of operator to silence this warning}}
128
129 // implicit casts of weakly imported symbols are ok:
130 b = f3;
131 if (f3) {}
132 b = S2::f4;
133 if (S2::f4) {}
134 }
135 }
136
137 namespace Array {
138 #define GetValue(ptr) ((ptr) ? ptr[0] : 0)
139 extern int a[] __attribute__((weak));
140 int b[] = {8,13,21};
141 struct {
142 int x[10];
143 } c;
144 const char str[] = "text";
ignore()145 void ignore() {
146 if (a) {}
147 if (a) {}
148 (void)GetValue(b);
149 }
test()150 void test() {
151 if (b) {}
152 // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}}
153 if (b) {}
154 // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}}
155 if (c.x) {}
156 // expected-warning@-1{{address of array 'c.x' will always evaluate to 'true'}}
157 if (str) {}
158 // expected-warning@-1{{address of array 'str' will always evaluate to 'true'}}
159 }
160 }
161
162 namespace Pointer {
163 extern int a __attribute__((weak));
164 int b;
165 static int c;
166 class S {
167 public:
168 static int a;
169 int b;
170 };
ignored()171 void ignored() {
172 if (&a) {}
173 }
test()174 void test() {
175 S s;
176 if (&b) {}
177 // expected-warning@-1{{address of 'b' will always evaluate to 'true'}}
178 if (&c) {}
179 // expected-warning@-1{{address of 'c' will always evaluate to 'true'}}
180 if (&s.a) {}
181 // expected-warning@-1{{address of 's.a' will always evaluate to 'true'}}
182 if (&s.b) {}
183 // expected-warning@-1{{address of 's.b' will always evaluate to 'true'}}
184 if (&S::a) {}
185 // expected-warning@-1{{address of 'S::a' will always evaluate to 'true'}}
186 }
187 }
188
189 namespace macros {
190 #define assert(x) if (x) {}
191 #define zero_on_null(x) ((x) ? *(x) : 0)
192
193 int array[5];
194 void fun();
195 int x;
196
test()197 void test() {
198 assert(array);
199 assert(array && "expecting null pointer");
200 // expected-warning@-1{{address of array 'array' will always evaluate to 'true'}}
201
202 assert(fun);
203 assert(fun && "expecting null pointer");
204 // expected-warning@-1{{address of function 'fun' will always evaluate to 'true'}}
205 // expected-note@-2 {{prefix with the address-of operator to silence this warning}}
206
207 // TODO: warn on assert(&x) while not warning on zero_on_null(&x)
208 zero_on_null(&x);
209 assert(zero_on_null(&x));
210 assert(&x);
211 assert(&x && "expecting null pointer");
212 // expected-warning@-1{{address of 'x' will always evaluate to 'true'}}
213 }
214 }
215
216 #if __cplusplus < 201703L
217 namespace Template {
218 // FIXME: These cases should not warn.
f()219 template<int *p> void f() { if (p) {} } // expected-warning 2{{will always evaluate to 'true'}} expected-cxx11-warning {{implicit conversion of nullptr}}
g()220 template<int (*p)[3]> void g() { if (p) {} } // expected-warning 2{{will always evaluate to 'true'}} expected-cxx11-warning {{implicit conversion of nullptr}}
h()221 template<int (*p)()> void h() { if (p) {} }
222
223 int a, b[3], c[3][3], d();
224 template void f<&a>(); // expected-note {{instantiation of}}
225 template void f<b>(); // expected-note {{instantiation of}}
226 #if __cplusplus >= 201103L
227 template void f<(int*)nullptr>(); // expected-note {{instantiation of}}
228 #endif
229 template void g<&b>(); // expected-note {{instantiation of}}
230 template void g<c>(); // expected-note {{instantiation of}}
231 #if __cplusplus >= 201103L
232 template void g<(int(*)[3])nullptr>(); // expected-note {{instantiation of}}
233 #endif
234 template void h<d>();
235 }
236 #endif // __cplusplus < 201703L
237