xref: /aosp_15_r20/external/clang/test/SemaCXX/warn-logical-not-compare.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -Wlogical-not-parentheses -verify %s
2*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -Wlogical-not-parentheses -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
3*67e74705SXin Li 
4*67e74705SXin Li bool getBool();
5*67e74705SXin Li int getInt();
6*67e74705SXin Li 
test1(int i1,int i2,bool b1,bool b2)7*67e74705SXin Li bool test1(int i1, int i2, bool b1, bool b2) {
8*67e74705SXin Li   bool ret;
9*67e74705SXin Li 
10*67e74705SXin Li   ret = !i1 == i2;
11*67e74705SXin Li   // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}}
12*67e74705SXin Li   // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}}
13*67e74705SXin Li   // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}}
14*67e74705SXin Li   // CHECK: warn-logical-not-compare.cpp:[[line:[0-9]*]]:9: warning
15*67e74705SXin Li   // CHECK: to evaluate the comparison first
16*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:10-[[line]]:10}:"("
17*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:18-[[line]]:18}:")"
18*67e74705SXin Li   // CHECK: to silence this warning
19*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:9-[[line]]:9}:"("
20*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:12-[[line]]:12}:")"
21*67e74705SXin Li 
22*67e74705SXin Li   ret = !i1 != i2;
23*67e74705SXin Li   //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}}
24*67e74705SXin Li   // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}}
25*67e74705SXin Li   // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}}
26*67e74705SXin Li   // CHECK: warn-logical-not-compare.cpp:[[line:[0-9]*]]:9: warning
27*67e74705SXin Li   // CHECK: to evaluate the comparison first
28*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:10-[[line]]:10}:"("
29*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:18-[[line]]:18}:")"
30*67e74705SXin Li   // CHECK: to silence this warning
31*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:9-[[line]]:9}:"("
32*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:12-[[line]]:12}:")"
33*67e74705SXin Li 
34*67e74705SXin Li   ret = !i1 < i2;
35*67e74705SXin Li   //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}}
36*67e74705SXin Li   // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}}
37*67e74705SXin Li   // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}}
38*67e74705SXin Li   // CHECK: warn-logical-not-compare.cpp:[[line:[0-9]*]]:9: warning
39*67e74705SXin Li   // CHECK: to evaluate the comparison first
40*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:10-[[line]]:10}:"("
41*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:17-[[line]]:17}:")"
42*67e74705SXin Li   // CHECK: to silence this warning
43*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:9-[[line]]:9}:"("
44*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:12-[[line]]:12}:")"
45*67e74705SXin Li 
46*67e74705SXin Li   ret = !i1 > i2;
47*67e74705SXin Li   //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}}
48*67e74705SXin Li   // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}}
49*67e74705SXin Li   // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}}
50*67e74705SXin Li   // CHECK: warn-logical-not-compare.cpp:[[line:[0-9]*]]:9: warning
51*67e74705SXin Li   // CHECK: to evaluate the comparison first
52*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:10-[[line]]:10}:"("
53*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:17-[[line]]:17}:")"
54*67e74705SXin Li   // CHECK: to silence this warning
55*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:9-[[line]]:9}:"("
56*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:12-[[line]]:12}:")"
57*67e74705SXin Li 
58*67e74705SXin Li   ret = !i1 <= i2;
59*67e74705SXin Li   //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}}
60*67e74705SXin Li   // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}}
61*67e74705SXin Li   // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}}
62*67e74705SXin Li   // CHECK: warn-logical-not-compare.cpp:[[line:[0-9]*]]:9: warning
63*67e74705SXin Li   // CHECK: to evaluate the comparison first
64*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:10-[[line]]:10}:"("
65*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:18-[[line]]:18}:")"
66*67e74705SXin Li   // CHECK: to silence this warning
67*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:9-[[line]]:9}:"("
68*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:12-[[line]]:12}:")"
69*67e74705SXin Li 
70*67e74705SXin Li   ret = !i1 >= i2;
71*67e74705SXin Li   //expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}}
72*67e74705SXin Li   // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}}
73*67e74705SXin Li   // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}}
74*67e74705SXin Li   // CHECK: warn-logical-not-compare.cpp:[[line:[0-9]*]]:9: warning
75*67e74705SXin Li   // CHECK: to evaluate the comparison first
76*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:10-[[line]]:10}:"("
77*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:18-[[line]]:18}:")"
78*67e74705SXin Li   // CHECK: to silence this warning
79*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:9-[[line]]:9}:"("
80*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:12-[[line]]:12}:")"
81*67e74705SXin Li 
82*67e74705SXin Li   ret = i1 == i2;
83*67e74705SXin Li   ret = i1 != i2;
84*67e74705SXin Li   ret = i1 < i2;
85*67e74705SXin Li   ret = i1 > i2;
86*67e74705SXin Li   ret = i1 <= i2;
87*67e74705SXin Li   ret = i1 >= i2;
88*67e74705SXin Li 
89*67e74705SXin Li   // Warning silenced by parens.
90*67e74705SXin Li   ret = (!i1) == i2;
91*67e74705SXin Li   ret = (!i1) != i2;
92*67e74705SXin Li   ret = (!i1) < i2;
93*67e74705SXin Li   ret = (!i1) > i2;
94*67e74705SXin Li   ret = (!i1) <= i2;
95*67e74705SXin Li   ret = (!i1) >= i2;
96*67e74705SXin Li 
97*67e74705SXin Li   ret = !b1 == b2;
98*67e74705SXin Li   ret = !b1 != b2;
99*67e74705SXin Li   ret = !b1 < b2;
100*67e74705SXin Li   ret = !b1 > b2;
101*67e74705SXin Li   ret = !b1 <= b2;
102*67e74705SXin Li   ret = !b1 >= b2;
103*67e74705SXin Li 
104*67e74705SXin Li   ret = !getInt() == i1;
105*67e74705SXin Li   // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}}
106*67e74705SXin Li   // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}}
107*67e74705SXin Li   // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}}
108*67e74705SXin Li   // CHECK: warn-logical-not-compare.cpp:[[line:[0-9]*]]:9: warning
109*67e74705SXin Li   // CHECK: to evaluate the comparison first
110*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:10-[[line]]:10}:"("
111*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:24-[[line]]:24}:")"
112*67e74705SXin Li   // CHECK: to silence this warning
113*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:9-[[line]]:9}:"("
114*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:18-[[line]]:18}:")"
115*67e74705SXin Li 
116*67e74705SXin Li   ret = (!getInt()) == i1;
117*67e74705SXin Li   ret = !getBool() == b1;
118*67e74705SXin Li   return ret;
119*67e74705SXin Li }
120*67e74705SXin Li 
121*67e74705SXin Li enum E {e1, e2};
122*67e74705SXin Li E getE();
123*67e74705SXin Li 
test2(E e)124*67e74705SXin Li bool test2 (E e) {
125*67e74705SXin Li   bool ret;
126*67e74705SXin Li   ret = e == e1;
127*67e74705SXin Li   ret = e == getE();
128*67e74705SXin Li   ret = getE() == e1;
129*67e74705SXin Li   ret = getE() == getE();
130*67e74705SXin Li 
131*67e74705SXin Li   ret = !e == e1;
132*67e74705SXin Li   // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}}
133*67e74705SXin Li   // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}}
134*67e74705SXin Li   // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}}
135*67e74705SXin Li   // CHECK: warn-logical-not-compare.cpp:[[line:[0-9]*]]:9: warning
136*67e74705SXin Li   // CHECK: to evaluate the comparison first
137*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:10-[[line]]:10}:"("
138*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:17-[[line]]:17}:")"
139*67e74705SXin Li   // CHECK: to silence this warning
140*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:9-[[line]]:9}:"("
141*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:11-[[line]]:11}:")"
142*67e74705SXin Li 
143*67e74705SXin Li   ret = !e == getE();
144*67e74705SXin Li   // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}}
145*67e74705SXin Li   // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}}
146*67e74705SXin Li   // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}}
147*67e74705SXin Li   // CHECK: warn-logical-not-compare.cpp:[[line:[0-9]*]]:9: warning
148*67e74705SXin Li   // CHECK: to evaluate the comparison first
149*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:10-[[line]]:10}:"("
150*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:21-[[line]]:21}:")"
151*67e74705SXin Li   // CHECK: to silence this warning
152*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:9-[[line]]:9}:"("
153*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:11-[[line]]:11}:")"
154*67e74705SXin Li 
155*67e74705SXin Li   ret = !getE() == e1;
156*67e74705SXin Li   // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}}
157*67e74705SXin Li   // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}}
158*67e74705SXin Li   // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}}
159*67e74705SXin Li   // CHECK: warn-logical-not-compare.cpp:[[line:[0-9]*]]:9: warning
160*67e74705SXin Li   // CHECK: to evaluate the comparison first
161*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:10-[[line]]:10}:"("
162*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:22-[[line]]:22}:")"
163*67e74705SXin Li   // CHECK: to silence this warning
164*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:9-[[line]]:9}:"("
165*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:16-[[line]]:16}:")"
166*67e74705SXin Li 
167*67e74705SXin Li   ret = !getE() == getE();
168*67e74705SXin Li   // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}}
169*67e74705SXin Li   // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}}
170*67e74705SXin Li   // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}}
171*67e74705SXin Li   // CHECK: warn-logical-not-compare.cpp:[[line:[0-9]*]]:9: warning
172*67e74705SXin Li   // CHECK: to evaluate the comparison first
173*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:10-[[line]]:10}:"("
174*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:26-[[line]]:26}:")"
175*67e74705SXin Li   // CHECK: to silence this warning
176*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:9-[[line]]:9}:"("
177*67e74705SXin Li   // CHECK: fix-it:"{{.*}}":{[[line]]:16-[[line]]:16}:")"
178*67e74705SXin Li 
179*67e74705SXin Li   ret = !(e == e1);
180*67e74705SXin Li   ret = !(e == getE());
181*67e74705SXin Li   ret = !(getE() == e1);
182*67e74705SXin Li   ret = !(getE() == getE());
183*67e74705SXin Li 
184*67e74705SXin Li   ret = (!e) == e1;
185*67e74705SXin Li   ret = (!e) == getE();
186*67e74705SXin Li   ret = (!getE()) == e1;
187*67e74705SXin Li   ret = (!getE()) == getE();
188*67e74705SXin Li 
189*67e74705SXin Li   return ret;
190*67e74705SXin Li }
191*67e74705SXin Li 
PR16673(int x)192*67e74705SXin Li bool PR16673(int x) {
193*67e74705SXin Li   bool ret;
194*67e74705SXin Li   // Make sure we don't emit a fixit for the left paren, but not the right paren.
195*67e74705SXin Li #define X(x) x
196*67e74705SXin Li   ret = X(!x == 1 && 1);
197*67e74705SXin Li   // expected-warning@-1 {{logical not is only applied to the left hand side of this comparison}}
198*67e74705SXin Li   // expected-note@-2 {{add parentheses after the '!' to evaluate the comparison first}}
199*67e74705SXin Li   // expected-note@-3 {{add parentheses around left hand side expression to silence this warning}}
200*67e74705SXin Li   // CHECK: warn-logical-not-compare.cpp:[[line:[0-9]*]]:11: warning
201*67e74705SXin Li   // CHECK: to evaluate the comparison first
202*67e74705SXin Li   // CHECK-NOT: fix-it
203*67e74705SXin Li   // CHECK: to silence this warning
204*67e74705SXin Li   // CHECK-NOT: fix-it
205*67e74705SXin Li   return ret;
206*67e74705SXin Li }
207