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