xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-bool-conversion.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2f4a2713aSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc namespace BooleanFalse {
4f4a2713aSLionel Sambuc int* j = false; // expected-warning{{initialization of pointer of type 'int *' to null from a constant boolean expression}}
5f4a2713aSLionel Sambuc 
foo(int * i,int * j=(false))6f4a2713aSLionel Sambuc void foo(int* i, int *j=(false)) // expected-warning{{initialization of pointer of type 'int *' to null from a constant boolean expression}}
7f4a2713aSLionel Sambuc {
8f4a2713aSLionel Sambuc   foo(false); // expected-warning{{initialization of pointer of type 'int *' to null from a constant boolean expression}}
9f4a2713aSLionel Sambuc   foo((int*)false); // no-warning: explicit cast
10f4a2713aSLionel Sambuc   foo(0); // no-warning: not a bool, even though its convertible to bool
11f4a2713aSLionel Sambuc 
12f4a2713aSLionel Sambuc   foo(false == true); // expected-warning{{initialization of pointer of type 'int *' to null from a constant boolean expression}}
13f4a2713aSLionel Sambuc   foo((42 + 24) < 32); // expected-warning{{initialization of pointer of type 'int *' to null from a constant boolean expression}}
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc   const bool kFlag = false;
16f4a2713aSLionel Sambuc   foo(kFlag); // expected-warning{{initialization of pointer of type 'int *' to null from a constant boolean expression}}
17f4a2713aSLionel Sambuc }
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc char f(struct Undefined*);
20f4a2713aSLionel Sambuc double f(...);
21f4a2713aSLionel Sambuc 
22f4a2713aSLionel Sambuc // Ensure that when using false in metaprogramming machinery its conversion
23f4a2713aSLionel Sambuc // isn't flagged.
24f4a2713aSLionel Sambuc template <int N> struct S {};
25f4a2713aSLionel Sambuc S<sizeof(f(false))> s;
26*0a6a1f1dSLionel Sambuc 
27*0a6a1f1dSLionel Sambuc }
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc namespace Function {
30*0a6a1f1dSLionel Sambuc void f1();
31*0a6a1f1dSLionel Sambuc 
32*0a6a1f1dSLionel Sambuc struct S {
33*0a6a1f1dSLionel Sambuc   static void f2();
34*0a6a1f1dSLionel Sambuc };
35*0a6a1f1dSLionel Sambuc 
36*0a6a1f1dSLionel Sambuc extern void f3() __attribute__((weak_import));
37*0a6a1f1dSLionel Sambuc 
38*0a6a1f1dSLionel Sambuc struct S2 {
39*0a6a1f1dSLionel Sambuc   static void f4() __attribute__((weak_import));
40*0a6a1f1dSLionel Sambuc };
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc bool f5();
43*0a6a1f1dSLionel Sambuc bool f6(int);
44*0a6a1f1dSLionel Sambuc 
bar()45*0a6a1f1dSLionel Sambuc void bar() {
46*0a6a1f1dSLionel Sambuc   bool b;
47*0a6a1f1dSLionel Sambuc 
48*0a6a1f1dSLionel Sambuc   b = f1; // expected-warning {{address of function 'f1' will always evaluate to 'true'}} \
49*0a6a1f1dSLionel Sambuc              expected-note {{prefix with the address-of operator to silence this warning}}
50*0a6a1f1dSLionel Sambuc   if (f1) {} // expected-warning {{address of function 'f1' will always evaluate to 'true'}} \
51*0a6a1f1dSLionel Sambuc                 expected-note {{prefix with the address-of operator to silence this warning}}
52*0a6a1f1dSLionel Sambuc   b = S::f2; // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}} \
53*0a6a1f1dSLionel Sambuc                 expected-note {{prefix with the address-of operator to silence this warning}}
54*0a6a1f1dSLionel Sambuc   if (S::f2) {} // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}} \
55*0a6a1f1dSLionel Sambuc                    expected-note {{prefix with the address-of operator to silence this warning}}
56*0a6a1f1dSLionel Sambuc   b = f5; // expected-warning {{address of function 'f5' will always evaluate to 'true'}} \
57*0a6a1f1dSLionel Sambuc              expected-note {{prefix with the address-of operator to silence this warning}} \
58*0a6a1f1dSLionel Sambuc              expected-note {{suffix with parentheses to turn this into a function call}}
59*0a6a1f1dSLionel Sambuc   b = f6; // expected-warning {{address of function 'f6' will always evaluate to 'true'}} \
60*0a6a1f1dSLionel Sambuc              expected-note {{prefix with the address-of operator to silence this warning}}
61*0a6a1f1dSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc   // implicit casts of weakly imported symbols are ok:
63*0a6a1f1dSLionel Sambuc   b = f3;
64*0a6a1f1dSLionel Sambuc   if (f3) {}
65*0a6a1f1dSLionel Sambuc   b = S2::f4;
66*0a6a1f1dSLionel Sambuc   if (S2::f4) {}
67*0a6a1f1dSLionel Sambuc }
68*0a6a1f1dSLionel Sambuc }
69*0a6a1f1dSLionel Sambuc 
70*0a6a1f1dSLionel Sambuc namespace Array {
71*0a6a1f1dSLionel Sambuc   #define GetValue(ptr)  ((ptr) ? ptr[0] : 0)
72*0a6a1f1dSLionel Sambuc   extern int a[] __attribute__((weak));
73*0a6a1f1dSLionel Sambuc   int b[] = {8,13,21};
74*0a6a1f1dSLionel Sambuc   struct {
75*0a6a1f1dSLionel Sambuc     int x[10];
76*0a6a1f1dSLionel Sambuc   } c;
77*0a6a1f1dSLionel Sambuc   const char str[] = "text";
ignore()78*0a6a1f1dSLionel Sambuc   void ignore() {
79*0a6a1f1dSLionel Sambuc     if (a) {}
80*0a6a1f1dSLionel Sambuc     if (a) {}
81*0a6a1f1dSLionel Sambuc     (void)GetValue(b);
82*0a6a1f1dSLionel Sambuc   }
test()83*0a6a1f1dSLionel Sambuc   void test() {
84*0a6a1f1dSLionel Sambuc     if (b) {}
85*0a6a1f1dSLionel Sambuc     // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}}
86*0a6a1f1dSLionel Sambuc     if (b) {}
87*0a6a1f1dSLionel Sambuc     // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}}
88*0a6a1f1dSLionel Sambuc     if (c.x) {}
89*0a6a1f1dSLionel Sambuc     // expected-warning@-1{{address of array 'c.x' will always evaluate to 'true'}}
90*0a6a1f1dSLionel Sambuc     if (str) {}
91*0a6a1f1dSLionel Sambuc     // expected-warning@-1{{address of array 'str' will always evaluate to 'true'}}
92*0a6a1f1dSLionel Sambuc   }
93*0a6a1f1dSLionel Sambuc }
94*0a6a1f1dSLionel Sambuc 
95*0a6a1f1dSLionel Sambuc namespace Pointer {
96*0a6a1f1dSLionel Sambuc   extern int a __attribute__((weak));
97*0a6a1f1dSLionel Sambuc   int b;
98*0a6a1f1dSLionel Sambuc   static int c;
99*0a6a1f1dSLionel Sambuc   class S {
100*0a6a1f1dSLionel Sambuc   public:
101*0a6a1f1dSLionel Sambuc     static int a;
102*0a6a1f1dSLionel Sambuc     int b;
103*0a6a1f1dSLionel Sambuc   };
ignored()104*0a6a1f1dSLionel Sambuc   void ignored() {
105*0a6a1f1dSLionel Sambuc     if (&a) {}
106*0a6a1f1dSLionel Sambuc   }
test()107*0a6a1f1dSLionel Sambuc   void test() {
108*0a6a1f1dSLionel Sambuc     S s;
109*0a6a1f1dSLionel Sambuc     if (&b) {}
110*0a6a1f1dSLionel Sambuc     // expected-warning@-1{{address of 'b' will always evaluate to 'true'}}
111*0a6a1f1dSLionel Sambuc     if (&c) {}
112*0a6a1f1dSLionel Sambuc     // expected-warning@-1{{address of 'c' will always evaluate to 'true'}}
113*0a6a1f1dSLionel Sambuc     if (&s.a) {}
114*0a6a1f1dSLionel Sambuc     // expected-warning@-1{{address of 's.a' will always evaluate to 'true'}}
115*0a6a1f1dSLionel Sambuc     if (&s.b) {}
116*0a6a1f1dSLionel Sambuc     // expected-warning@-1{{address of 's.b' will always evaluate to 'true'}}
117*0a6a1f1dSLionel Sambuc     if (&S::a) {}
118*0a6a1f1dSLionel Sambuc     // expected-warning@-1{{address of 'S::a' will always evaluate to 'true'}}
119*0a6a1f1dSLionel Sambuc   }
120*0a6a1f1dSLionel Sambuc }
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc namespace macros {
123*0a6a1f1dSLionel Sambuc   #define assert(x) if (x) {}
124*0a6a1f1dSLionel Sambuc   #define zero_on_null(x) ((x) ? *(x) : 0)
125*0a6a1f1dSLionel Sambuc 
126*0a6a1f1dSLionel Sambuc   int array[5];
127*0a6a1f1dSLionel Sambuc   void fun();
128*0a6a1f1dSLionel Sambuc   int x;
129*0a6a1f1dSLionel Sambuc 
test()130*0a6a1f1dSLionel Sambuc   void test() {
131*0a6a1f1dSLionel Sambuc     assert(array);
132*0a6a1f1dSLionel Sambuc     assert(array && "expecting null pointer");
133*0a6a1f1dSLionel Sambuc     // expected-warning@-1{{address of array 'array' will always evaluate to 'true'}}
134*0a6a1f1dSLionel Sambuc 
135*0a6a1f1dSLionel Sambuc     assert(fun);
136*0a6a1f1dSLionel Sambuc     assert(fun && "expecting null pointer");
137*0a6a1f1dSLionel Sambuc     // expected-warning@-1{{address of function 'fun' will always evaluate to 'true'}}
138*0a6a1f1dSLionel Sambuc     // expected-note@-2 {{prefix with the address-of operator to silence this warning}}
139*0a6a1f1dSLionel Sambuc 
140*0a6a1f1dSLionel Sambuc     // TODO: warn on assert(&x) while not warning on zero_on_null(&x)
141*0a6a1f1dSLionel Sambuc     zero_on_null(&x);
142*0a6a1f1dSLionel Sambuc     assert(zero_on_null(&x));
143*0a6a1f1dSLionel Sambuc     assert(&x);
144*0a6a1f1dSLionel Sambuc     assert(&x && "expecting null pointer");
145*0a6a1f1dSLionel Sambuc     // expected-warning@-1{{address of 'x' will always evaluate to 'true'}}
146*0a6a1f1dSLionel Sambuc   }
147*0a6a1f1dSLionel Sambuc }
148