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
5 namespace BooleanFalse
{
7 #if __cplusplus <= 199711L
8 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
10 // expected-error@-4 {{cannot initialize a variable of type 'int *' with an rvalue of type 'bool'}}
13 #if __cplusplus <= 199711L
14 // expected-warning@+5 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
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}}
19 void bar(int *j
= false);
21 #if __cplusplus > 199711L
22 // expected-note@+2 4{{candidate function not viable: no known conversion}}
27 #if __cplusplus <= 199711L
28 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
30 // expected-error@-4 {{no matching function for call to 'foo'}}
33 foo((int*)false); // OK: explicit cast
34 foo(0); // OK: not a bool, even though it's convertible to bool
37 #if __cplusplus <= 199711L
38 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
40 // expected-error@-4 {{no matching function for call to 'foo'}}
44 #if __cplusplus <= 199711L
45 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
47 // expected-error@-4 {{no matching function for call to 'foo'}}
50 const bool kFlag
= false;
52 #if __cplusplus <= 199711L
53 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
55 // expected-error@-4 {{no matching function for call to 'foo'}}
59 char f(struct Undefined
*);
62 // Ensure that when using false in metaprogramming machinery its conversion
64 template <int N
> struct S
{};
65 S
<sizeof(f(false))> s
;
76 extern void f3() __attribute__((weak_import
));
79 static void f4() __attribute__((weak_import
));
84 #if __cplusplus >= 201103L
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'}}
96 template <typename
... Ts
>
97 static bool IsFalse(const Ts
&...) { return false; }
99 static bool IsFalse(const T
& p
) {
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;
107 bool use_instantiation() {
108 return IsFalse([]() { return 0; });
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
}}
129 // implicit casts of weakly imported symbols are ok:
138 #define GetValue(ptr) ((ptr) ? ptr[0] : 0)
139 extern int a
[] __attribute__((weak
));
144 const char str
[] = "text";
152 // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}}
154 // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}}
156 // expected-warning@-1{{address of array 'c.x' will always evaluate to 'true'}}
158 // expected-warning@-1{{address of array 'str' will always evaluate to 'true'}}
163 extern int a
__attribute__((weak
));
177 // expected-warning@-1{{address of 'b' will always evaluate to 'true'}}
179 // expected-warning@-1{{address of 'c' will always evaluate to 'true'}}
181 // expected-warning@-1{{address of 's.a' will always evaluate to 'true'}}
183 // expected-warning@-1{{address of 's.b' will always evaluate to 'true'}}
185 // expected-warning@-1{{address of 'S::a' will always evaluate to 'true'}}
190 #define assert(x) if (x) {}
191 #define zero_on_null(x) ((x) ? *(x) : 0)
199 assert(array
&& "expecting null pointer");
200 // expected-warning@-1{{address of array 'array' will always evaluate to 'true'}}
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}}
207 // TODO: warn on assert(&x) while not warning on zero_on_null(&x)
209 assert(zero_on_null(&x
));
211 assert(&x
&& "expecting null pointer");
212 // expected-warning@-1{{address of 'x' will always evaluate to 'true'}}
216 #if __cplusplus < 201703L
218 // FIXME: These cases should not warn.
219 template<int *p
> void f() { if (p
) {} } // expected-warning 2{{will always evaluate to 'true'}} expected-cxx11-warning {{implicit conversion of nullptr}}
220 template<int (*p
)[3]> void g() { if (p
) {} } // expected-warning 2{{will always evaluate to 'true'}} expected-cxx11-warning {{implicit conversion of nullptr}}
221 template<int (*p
)()> void h() { if (p
) {} }
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}}
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}}
234 template void h
<d
>();
236 #endif // __cplusplus < 201703L