xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/bool-compare.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc 
f(int x,int y,int z)4*0a6a1f1dSLionel Sambuc void f(int x, int y, int z) {
5*0a6a1f1dSLionel Sambuc 
6*0a6a1f1dSLionel Sambuc   bool a,b;
7*0a6a1f1dSLionel Sambuc 
8*0a6a1f1dSLionel Sambuc   if(b > true)    {} // expected-warning {{comparison of true with expression of type 'bool' is always false}}
9*0a6a1f1dSLionel Sambuc   if(b < true)    {} // no warning
10*0a6a1f1dSLionel Sambuc   if(b >= true)   {} // no warning
11*0a6a1f1dSLionel Sambuc   if(b <= true)   {} // expected-warning {{comparison of true with expression of type 'bool' is always true}}
12*0a6a1f1dSLionel Sambuc   if(b == true)   {} // no warning
13*0a6a1f1dSLionel Sambuc   if(b != true)   {} // no warning
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc   if(b > false)   {} // no warning
16*0a6a1f1dSLionel Sambuc   if(b < false)   {} // expected-warning {{comparison of false with expression of type 'bool' is always false}}
17*0a6a1f1dSLionel Sambuc   if(b >= false)  {} // expected-warning {{comparison of false with expression of type 'bool' is always true}}
18*0a6a1f1dSLionel Sambuc   if(b <= false)  {} // no warning
19*0a6a1f1dSLionel Sambuc   if(b == false)  {} // no warning
20*0a6a1f1dSLionel Sambuc   if(b != false)  {} // no warning
21*0a6a1f1dSLionel Sambuc 
22*0a6a1f1dSLionel Sambuc   if(b > 1U){} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
23*0a6a1f1dSLionel Sambuc 
24*0a6a1f1dSLionel Sambuc   if (a > b)      {} // no warning
25*0a6a1f1dSLionel Sambuc   if (a < b)      {} // no warning
26*0a6a1f1dSLionel Sambuc   if (a >= b)     {} // no warning
27*0a6a1f1dSLionel Sambuc   if (a <= b)     {} // no warning
28*0a6a1f1dSLionel Sambuc   if (a == b)     {} // no warning
29*0a6a1f1dSLionel Sambuc   if (a != b)     {} // no warning
30*0a6a1f1dSLionel Sambuc 
31*0a6a1f1dSLionel Sambuc   if (a > 0) {} // no warning
32*0a6a1f1dSLionel Sambuc   if (a > 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
33*0a6a1f1dSLionel Sambuc   if (a > 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc   if (a >= 0) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always true}}
36*0a6a1f1dSLionel Sambuc   if (a >= 1) {} // no warning
37*0a6a1f1dSLionel Sambuc   if (a >= 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
38*0a6a1f1dSLionel Sambuc   if (a >= -1) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
39*0a6a1f1dSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc   if (a <= 0) {} // no warning
41*0a6a1f1dSLionel Sambuc   if (a <= 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always true}}
42*0a6a1f1dSLionel Sambuc   if (a <= 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
43*0a6a1f1dSLionel Sambuc   if (a <= -1) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
44*0a6a1f1dSLionel Sambuc 
45*0a6a1f1dSLionel Sambuc   if (!a > 0)     {} // no warning
46*0a6a1f1dSLionel Sambuc   if (!a > 1)     {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
47*0a6a1f1dSLionel Sambuc   if (!a > 2)     {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
48*0a6a1f1dSLionel Sambuc   if (!a > y)     {} // no warning
49*0a6a1f1dSLionel Sambuc   if (!a > b)     {} // no warning
50*0a6a1f1dSLionel Sambuc   if (!a > -1)    {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
51*0a6a1f1dSLionel Sambuc 
52*0a6a1f1dSLionel Sambuc   if (!a < 0)     {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
53*0a6a1f1dSLionel Sambuc   if (!a < 1)     {} // no warning
54*0a6a1f1dSLionel Sambuc   if (!a < 2)     {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
55*0a6a1f1dSLionel Sambuc   if (!a < y)     {} // no warning
56*0a6a1f1dSLionel Sambuc   if (!a < b)     {} // no warning
57*0a6a1f1dSLionel Sambuc   if (!a < -1)    {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
58*0a6a1f1dSLionel Sambuc 
59*0a6a1f1dSLionel Sambuc   if (!a >= 0)    {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always true}}
60*0a6a1f1dSLionel Sambuc   if (!a >= 1)    {} // no warning
61*0a6a1f1dSLionel Sambuc   if (!a >= 2)    {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
62*0a6a1f1dSLionel Sambuc   if (!a >= y)    {} // no warning
63*0a6a1f1dSLionel Sambuc   if (!a >= b)    {} // no warning
64*0a6a1f1dSLionel Sambuc   if (!a >= -1)   {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
65*0a6a1f1dSLionel Sambuc 
66*0a6a1f1dSLionel Sambuc   if (!a <= 0)    {} // no warning
67*0a6a1f1dSLionel Sambuc   if (!a <= 1)    {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always true}}
68*0a6a1f1dSLionel Sambuc   if (!a <= 2)    {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
69*0a6a1f1dSLionel Sambuc   if (!a <= y)    {} // no warning
70*0a6a1f1dSLionel Sambuc   if (!a <= b)    {} // no warning
71*0a6a1f1dSLionel Sambuc   if (!a <= -1)   {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
72*0a6a1f1dSLionel Sambuc 
73*0a6a1f1dSLionel Sambuc   if ((a||b) > 0) {} // no warning
74*0a6a1f1dSLionel Sambuc   if ((a||b) > 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
75*0a6a1f1dSLionel Sambuc   if ((a||b) > 4) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always false}}
76*0a6a1f1dSLionel Sambuc   if ((a||b) > -1) {}// expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
77*0a6a1f1dSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc   if ((a&&b) > 0) {} // no warning
79*0a6a1f1dSLionel Sambuc   if ((a&&b) > 1) {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
80*0a6a1f1dSLionel Sambuc   if ((a&&b) > 4) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always false}}
81*0a6a1f1dSLionel Sambuc 
82*0a6a1f1dSLionel Sambuc   if ((a<y) > 0)  {} // no warning
83*0a6a1f1dSLionel Sambuc   if ((a<y) > 1)  {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
84*0a6a1f1dSLionel Sambuc   if ((a<y) > 4)  {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always false}}
85*0a6a1f1dSLionel Sambuc   if ((a<y) > z)  {} // no warning
86*0a6a1f1dSLionel Sambuc   if ((a<y) > -1) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
87*0a6a1f1dSLionel Sambuc 
88*0a6a1f1dSLionel Sambuc   if ((a<y) == 0) {} // no warning
89*0a6a1f1dSLionel Sambuc   if ((a<y) == 1) {} // no warning
90*0a6a1f1dSLionel Sambuc   if ((a<y) == 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
91*0a6a1f1dSLionel Sambuc   if ((a<y) == z) {} // no warning
92*0a6a1f1dSLionel Sambuc   if ((a<y) == -1) {}// expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
93*0a6a1f1dSLionel Sambuc 
94*0a6a1f1dSLionel Sambuc   if ((a<y) != 0) {} // no warning
95*0a6a1f1dSLionel Sambuc   if ((a<y) != 1) {} // no warning
96*0a6a1f1dSLionel Sambuc   if ((a<y) != 2) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
97*0a6a1f1dSLionel Sambuc   if ((a<y) != z) {} // no warning
98*0a6a1f1dSLionel Sambuc   if ((a<y) != -1) {}// expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
99*0a6a1f1dSLionel Sambuc 
100*0a6a1f1dSLionel Sambuc   if ((a<y) == z) {} // no warning
101*0a6a1f1dSLionel Sambuc   if (a>y<z)      {} // no warning
102*0a6a1f1dSLionel Sambuc   if ((a<y) > z)  {} // no warning
103*0a6a1f1dSLionel Sambuc   if((a<y)>(z<y)) {} // no warning
104*0a6a1f1dSLionel Sambuc   if((a<y)==(z<y)){} // no warning
105*0a6a1f1dSLionel Sambuc   if((a<y)!=(z<y)){} // no warning
106*0a6a1f1dSLionel Sambuc   if((z==x)<(y==z)){}  // no warning
107*0a6a1f1dSLionel Sambuc   if((a<y)!=((z==x)<(y==z))){} // no warning
108*0a6a1f1dSLionel Sambuc 
109*0a6a1f1dSLionel Sambuc 
110*0a6a1f1dSLionel Sambuc   if (0 > !a)     {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
111*0a6a1f1dSLionel Sambuc   if (1 > !a)     {} // no warning
112*0a6a1f1dSLionel Sambuc   if (2 > !a)     {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
113*0a6a1f1dSLionel Sambuc   if (y > !a)     {} // no warning
114*0a6a1f1dSLionel Sambuc   if (-1 > !a)    {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
115*0a6a1f1dSLionel Sambuc 
116*0a6a1f1dSLionel Sambuc   if (0 < !a)     {} // no warning
117*0a6a1f1dSLionel Sambuc   if (1 < !a)     {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always false}}
118*0a6a1f1dSLionel Sambuc   if (2 < !a)     {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
119*0a6a1f1dSLionel Sambuc   if (y < !a)     {} // no warning
120*0a6a1f1dSLionel Sambuc   if (-1 < !a)    {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc 
123*0a6a1f1dSLionel Sambuc   if (0 >= !a)    {} // no warning
124*0a6a1f1dSLionel Sambuc   if (1 >= !a)    {} // expected-warning {{comparison of constant 1 with expression of type 'bool' is always true}}
125*0a6a1f1dSLionel Sambuc   if (2 >= !a)    {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
126*0a6a1f1dSLionel Sambuc   if (y >= !a)    {} // no warning
127*0a6a1f1dSLionel Sambuc   if (-1 >= !a)   {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
128*0a6a1f1dSLionel Sambuc 
129*0a6a1f1dSLionel Sambuc   if (0 <= !a)    {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always true}}
130*0a6a1f1dSLionel Sambuc   if (1 <= !a)    {} // no warning
131*0a6a1f1dSLionel Sambuc   if (2 <= !a)    {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
132*0a6a1f1dSLionel Sambuc   if (y <= !a)    {} //
133*0a6a1f1dSLionel Sambuc   if (-1 <= !a)   {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
134*0a6a1f1dSLionel Sambuc 
135*0a6a1f1dSLionel Sambuc   if (0 > (a||b)) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
136*0a6a1f1dSLionel Sambuc   if (1 > (a||b)) {} // no warning
137*0a6a1f1dSLionel Sambuc   if (4 > (a||b)) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always true}}
138*0a6a1f1dSLionel Sambuc 
139*0a6a1f1dSLionel Sambuc   if (0 > (a&&b)) {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
140*0a6a1f1dSLionel Sambuc   if (1 > (a&&b)) {} // no warning
141*0a6a1f1dSLionel Sambuc   if (4 > (a&&b)) {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always true}}
142*0a6a1f1dSLionel Sambuc 
143*0a6a1f1dSLionel Sambuc   if (0 > (a<y))  {} // expected-warning {{comparison of constant 0 with expression of type 'bool' is always false}}
144*0a6a1f1dSLionel Sambuc   if (1 > (a<y))  {} // no warning
145*0a6a1f1dSLionel Sambuc   if (4 > (a<y))  {} // expected-warning {{comparison of constant 4 with expression of type 'bool' is always true}}
146*0a6a1f1dSLionel Sambuc   if (z > (a<y))  {} //
147*0a6a1f1dSLionel Sambuc   if (-1 > (a<y)) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
148*0a6a1f1dSLionel Sambuc 
149*0a6a1f1dSLionel Sambuc   if (0 == (a<y)) {} // no warning
150*0a6a1f1dSLionel Sambuc   if (1 == (a<y)) {} // no warning
151*0a6a1f1dSLionel Sambuc   if (2 == (a<y)) {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always false}}
152*0a6a1f1dSLionel Sambuc   if (z == (a<y)) {} // no warning
153*0a6a1f1dSLionel Sambuc   if (-1 == (a<y)){} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
154*0a6a1f1dSLionel Sambuc 
155*0a6a1f1dSLionel Sambuc   if (0 !=(a<y))  {} // no warning
156*0a6a1f1dSLionel Sambuc   if (1 !=(a<y))  {} // no warning
157*0a6a1f1dSLionel Sambuc   if (2 !=(a<y))  {} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
158*0a6a1f1dSLionel Sambuc   if (z !=(a<y))  {} // no warning
159*0a6a1f1dSLionel Sambuc   if (-1 !=(a<y)) {} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always true}}
160*0a6a1f1dSLionel Sambuc 
161*0a6a1f1dSLionel Sambuc   if (z ==(a<y))  {} // no warning
162*0a6a1f1dSLionel Sambuc   if (z<a>y)      {} // no warning
163*0a6a1f1dSLionel Sambuc   if (z > (a<y))  {} // no warning
164*0a6a1f1dSLionel Sambuc   if((z<y)>(a<y)) {} // no warning
165*0a6a1f1dSLionel Sambuc   if((z<y)==(a<y)){} // no warning
166*0a6a1f1dSLionel Sambuc   if((z<y)!=(a<y)){} // no warning
167*0a6a1f1dSLionel Sambuc   if((y==z)<(z==x)){} // no warning
168*0a6a1f1dSLionel Sambuc   if(((z==x)<(y==z))!=(a<y)){}  // no warning
169*0a6a1f1dSLionel Sambuc 
170*0a6a1f1dSLionel Sambuc   if(((z==x)<(-1==z))!=(a<y)){} // no warning
171*0a6a1f1dSLionel Sambuc   if(((z==x)<(z==-1))!=(a<y)){} // no warning
172*0a6a1f1dSLionel Sambuc   if(((z==x)<-1)!=(a<y)){} // expected-warning {{comparison of constant -1 with expression of type 'bool' is always false}}
173*0a6a1f1dSLionel Sambuc   if(((z==x)< 2)!=(a<y)){} // expected-warning {{comparison of constant 2 with expression of type 'bool' is always true}}
174*0a6a1f1dSLionel Sambuc   if(((z==x)<(z>2))!=(a<y)){} // no warning
175*0a6a1f1dSLionel Sambuc 
176*0a6a1f1dSLionel Sambuc }
177*0a6a1f1dSLionel Sambuc 
178*0a6a1f1dSLionel Sambuc 
179*0a6a1f1dSLionel Sambuc template<typename T, typename U, typename V> struct X6 {
fX6180*0a6a1f1dSLionel Sambuc   U f(T t, U u, V v) {
181*0a6a1f1dSLionel Sambuc     // IfStmt
182*0a6a1f1dSLionel Sambuc     if (t > 0)
183*0a6a1f1dSLionel Sambuc       return u;
184*0a6a1f1dSLionel Sambuc     else {
185*0a6a1f1dSLionel Sambuc       if (t < 0)
186*0a6a1f1dSLionel Sambuc         return v; // expected-error{{cannot initialize return object of type}}
187*0a6a1f1dSLionel Sambuc     }
188*0a6a1f1dSLionel Sambuc     bool r;
189*0a6a1f1dSLionel Sambuc     // FIXME: We should warn here, DiagRuntimeBehavior does currently not detect this.
190*0a6a1f1dSLionel Sambuc     if(r<0){}
191*0a6a1f1dSLionel Sambuc 
192*0a6a1f1dSLionel Sambuc     if (T x = t) {
193*0a6a1f1dSLionel Sambuc       t = x;
194*0a6a1f1dSLionel Sambuc     }
195*0a6a1f1dSLionel Sambuc     return v; // expected-error{{cannot initialize return object of type}}
196*0a6a1f1dSLionel Sambuc   }
197*0a6a1f1dSLionel Sambuc };
198*0a6a1f1dSLionel Sambuc 
199*0a6a1f1dSLionel Sambuc struct ConvertibleToInt {
200*0a6a1f1dSLionel Sambuc   operator int() const;
201*0a6a1f1dSLionel Sambuc };
202*0a6a1f1dSLionel Sambuc 
203*0a6a1f1dSLionel Sambuc template struct X6<ConvertibleToInt, float, char>;
204*0a6a1f1dSLionel Sambuc template struct X6<bool, int, int*>; // expected-note{{instantiation}}
205*0a6a1f1dSLionel Sambuc 
206*0a6a1f1dSLionel Sambuc 
207*0a6a1f1dSLionel Sambuc 
208