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
));
88 b
= f1
; // expected-warning {{address of function 'f1' will always evaluate to 'true'}} \
89 expected
-note
{{prefix with the address
-of
operator to silence
this warning
}}
90 if (f1
) {} // expected-warning {{address of function 'f1' will always evaluate to 'true'}} \
91 expected
-note
{{prefix with the address
-of
operator to silence
this warning
}}
92 b
= S::f2
; // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}} \
93 expected
-note
{{prefix with the address
-of
operator to silence
this warning
}}
94 if (S::f2
) {} // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}} \
95 expected
-note
{{prefix with the address
-of
operator to silence
this warning
}}
96 b
= f5
; // expected-warning {{address of function 'f5' will always evaluate to 'true'}} \
97 expected
-note
{{prefix with the address
-of
operator to silence
this warning
}} \
98 expected
-note
{{suffix with parentheses to turn
this into a function call
}}
99 b
= f6
; // expected-warning {{address of function 'f6' will always evaluate to 'true'}} \
100 expected
-note
{{prefix with the address
-of
operator to silence
this warning
}}
102 // implicit casts of weakly imported symbols are ok:
111 #define GetValue(ptr) ((ptr) ? ptr[0] : 0)
112 extern int a
[] __attribute__((weak
));
117 const char str
[] = "text";
125 // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}}
127 // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}}
129 // expected-warning@-1{{address of array 'c.x' will always evaluate to 'true'}}
131 // expected-warning@-1{{address of array 'str' will always evaluate to 'true'}}
136 extern int a
__attribute__((weak
));
150 // expected-warning@-1{{address of 'b' will always evaluate to 'true'}}
152 // expected-warning@-1{{address of 'c' will always evaluate to 'true'}}
154 // expected-warning@-1{{address of 's.a' will always evaluate to 'true'}}
156 // expected-warning@-1{{address of 's.b' will always evaluate to 'true'}}
158 // expected-warning@-1{{address of 'S::a' will always evaluate to 'true'}}
163 #define assert(x) if (x) {}
164 #define zero_on_null(x) ((x) ? *(x) : 0)
172 assert(array
&& "expecting null pointer");
173 // expected-warning@-1{{address of array 'array' will always evaluate to 'true'}}
176 assert(fun
&& "expecting null pointer");
177 // expected-warning@-1{{address of function 'fun' will always evaluate to 'true'}}
178 // expected-note@-2 {{prefix with the address-of operator to silence this warning}}
180 // TODO: warn on assert(&x) while not warning on zero_on_null(&x)
182 assert(zero_on_null(&x
));
184 assert(&x
&& "expecting null pointer");
185 // expected-warning@-1{{address of 'x' will always evaluate to 'true'}}
190 // FIXME: These cases should not warn.
191 template<int *p
> void f() { if (p
) {} } // expected-warning 2{{will always evaluate to 'true'}} expected-cxx11-warning {{implicit conversion of nullptr}}
192 template<int (*p
)[3]> void g() { if (p
) {} } // expected-warning 2{{will always evaluate to 'true'}} expected-cxx11-warning {{implicit conversion of nullptr}}
193 template<int (*p
)()> void h() { if (p
) {} }
195 int a
, b
[3], c
[3][3], d();
196 template void f
<&a
>(); // expected-note {{instantiation of}}
197 template void f
<b
>(); // expected-note {{instantiation of}}
198 #if __cplusplus >= 201103L
199 template void f
<(int*)nullptr>(); // expected-note {{instantiation of}}
201 template void g
<&b
>(); // expected-note {{instantiation of}}
202 template void g
<c
>(); // expected-note {{instantiation of}}
203 #if __cplusplus >= 201103L
204 template void g
<(int(*)[3])nullptr>(); // expected-note {{instantiation of}}
206 template void h
<d
>();