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 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 85 void bar() { 86 bool b; 87 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}} 101 102 // implicit casts of weakly imported symbols are ok: 103 b = f3; 104 if (f3) {} 105 b = S2::f4; 106 if (S2::f4) {} 107 } 108 } 109 110 namespace Array { 111 #define GetValue(ptr) ((ptr) ? ptr[0] : 0) 112 extern int a[] __attribute__((weak)); 113 int b[] = {8,13,21}; 114 struct { 115 int x[10]; 116 } c; 117 const char str[] = "text"; 118 void ignore() { 119 if (a) {} 120 if (a) {} 121 (void)GetValue(b); 122 } 123 void test() { 124 if (b) {} 125 // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}} 126 if (b) {} 127 // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}} 128 if (c.x) {} 129 // expected-warning@-1{{address of array 'c.x' will always evaluate to 'true'}} 130 if (str) {} 131 // expected-warning@-1{{address of array 'str' will always evaluate to 'true'}} 132 } 133 } 134 135 namespace Pointer { 136 extern int a __attribute__((weak)); 137 int b; 138 static int c; 139 class S { 140 public: 141 static int a; 142 int b; 143 }; 144 void ignored() { 145 if (&a) {} 146 } 147 void test() { 148 S s; 149 if (&b) {} 150 // expected-warning@-1{{address of 'b' will always evaluate to 'true'}} 151 if (&c) {} 152 // expected-warning@-1{{address of 'c' will always evaluate to 'true'}} 153 if (&s.a) {} 154 // expected-warning@-1{{address of 's.a' will always evaluate to 'true'}} 155 if (&s.b) {} 156 // expected-warning@-1{{address of 's.b' will always evaluate to 'true'}} 157 if (&S::a) {} 158 // expected-warning@-1{{address of 'S::a' will always evaluate to 'true'}} 159 } 160 } 161 162 namespace macros { 163 #define assert(x) if (x) {} 164 #define zero_on_null(x) ((x) ? *(x) : 0) 165 166 int array[5]; 167 void fun(); 168 int x; 169 170 void test() { 171 assert(array); 172 assert(array && "expecting null pointer"); 173 // expected-warning@-1{{address of array 'array' will always evaluate to 'true'}} 174 175 assert(fun); 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}} 179 180 // TODO: warn on assert(&x) while not warning on zero_on_null(&x) 181 zero_on_null(&x); 182 assert(zero_on_null(&x)); 183 assert(&x); 184 assert(&x && "expecting null pointer"); 185 // expected-warning@-1{{address of 'x' will always evaluate to 'true'}} 186 } 187 } 188 189 namespace Template { 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) {} } 194 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}} 200 #endif 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}} 205 #endif 206 template void h<d>(); 207 } 208