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