blob: 324d9b5f1e414523bd3e635ec260ca4235226165 [file] [log] [blame]
Hans Wennborgcf9bac42011-06-03 18:00:36 +00001// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
Richard Trieucf2a4c02013-04-18 00:56:23 +00002// RUN: %clang_cc1 -Wparentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
Hans Wennborgcf9bac42011-06-03 18:00:36 +00003
4bool someConditionFunc();
5
6void conditional_op(int x, int y, bool b) {
Chandler Carruthf8e06c92011-06-16 01:05:12 +00007 (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
Richard Trieucf2a4c02013-04-18 00:56:23 +00008 // expected-note {{place parentheses around the '+' expression to silence this warning}} \
9 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
10 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
11 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:33-[[@LINE-4]]:33}:")"
12 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
13 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:41-[[@LINE-6]]:41}:")"
Hans Wennborgcf9bac42011-06-03 18:00:36 +000014
Chandler Carruthf8e06c92011-06-16 01:05:12 +000015 (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
Richard Trieucf2a4c02013-04-18 00:56:23 +000016 // expected-note {{place parentheses around the '-' expression to silence this warning}} \
17 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
18 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
19 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:15-[[@LINE-4]]:15}:")"
20 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
21 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")"
Hans Wennborgcf9bac42011-06-03 18:00:36 +000022
Chandler Carruthf8e06c92011-06-16 01:05:12 +000023 (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
Richard Trieucf2a4c02013-04-18 00:56:23 +000024 // expected-note {{place parentheses around the '*' expression to silence this warning}} \
25 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
26 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
27 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")"
28 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
29 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:30-[[@LINE-6]]:30}:")"
Hans Wennborgcf9bac42011-06-03 18:00:36 +000030}
Hans Wennborgde2e67e2011-06-09 17:06:51 +000031
32class Stream {
33public:
34 operator int();
35 Stream &operator<<(int);
36 Stream &operator<<(const char*);
David Blaikie15f17cb2012-10-05 00:41:03 +000037 Stream &operator>>(int);
38 Stream &operator>>(const char*);
Hans Wennborgde2e67e2011-06-09 17:06:51 +000039};
40
41void f(Stream& s, bool b) {
Chandler Carruthf8e06c92011-06-16 01:05:12 +000042 (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \
Richard Trieucf2a4c02013-04-18 00:56:23 +000043 // expected-note {{place parentheses around the '<<' expression to silence this warning}} \
44 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
45 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
46 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
47 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
48 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:32-[[@LINE-6]]:32}:")"
49
Richard Trieu9ad40ac2014-10-28 04:37:34 +000050 (void)(s << 5 == 1); // expected-warning {{overloaded operator << has higher precedence than comparison operator}} \
Richard Trieue0894972013-04-18 01:04:37 +000051 // expected-note {{place parentheses around the '<<' expression to silence this warning}} \
52 // expected-note {{place parentheses around comparison expression to evaluate it first}}
53 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
54 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
55 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
56 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
Richard Trieucf2a4c02013-04-18 00:56:23 +000057
Richard Trieu9ad40ac2014-10-28 04:37:34 +000058 (void)(s >> 5 == 1); // expected-warning {{overloaded operator >> has higher precedence than comparison operator}} \
Richard Trieue0894972013-04-18 01:04:37 +000059 // expected-note {{place parentheses around the '>>' expression to silence this warning}} \
60 // expected-note {{place parentheses around comparison expression to evaluate it first}}
61 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
62 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
63 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
64 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
Hans Wennborgde2e67e2011-06-09 17:06:51 +000065}
Chandler Carruth4352b0b2011-06-21 17:22:09 +000066
67struct S {
68 operator int() { return 42; }
69 friend S operator+(const S &lhs, bool) { return S(); }
70};
71
72void test(S *s, bool (S::*m_ptr)()) {
73 (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \
Richard Trieucf2a4c02013-04-18 00:56:23 +000074 // expected-note {{place parentheses around the '+' expression to silence this warning}} \
75 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
76 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
77 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:19-[[@LINE-4]]:19}:")"
78 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
79 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:35-[[@LINE-6]]:35}:")"
Chandler Carruth4352b0b2011-06-21 17:22:09 +000080
Hans Wennborgbe207b32011-09-12 12:07:30 +000081 (void)((*s + true) ? "foo" : "bar"); // No warning.
82
Chandler Carruth4352b0b2011-06-21 17:22:09 +000083 // Don't crash on unusual member call expressions.
84 (void)((s->*m_ptr)() ? "foo" : "bar");
85}
David Blaikie15f17cb2012-10-05 00:41:03 +000086
87void test(int a, int b, int c) {
David Blaikie82d3ab92012-10-19 18:26:06 +000088 (void)(a >> b + c); // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
David Blaikie15f17cb2012-10-05 00:41:03 +000089 expected-note {{place parentheses around the '+' expression to silence this warning}}
Richard Trieucf2a4c02013-04-18 00:56:23 +000090 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
91 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
92
David Blaikie82d3ab92012-10-19 18:26:06 +000093 (void)(a - b << c); // expected-warning {{operator '<<' has lower precedence than '-'; '-' will be evaluated first}} \
David Blaikie15f17cb2012-10-05 00:41:03 +000094 expected-note {{place parentheses around the '-' expression to silence this warning}}
Richard Trieucf2a4c02013-04-18 00:56:23 +000095 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"("
96 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:")"
97
David Blaikie15f17cb2012-10-05 00:41:03 +000098 Stream() << b + c;
David Blaikie82d3ab92012-10-19 18:26:06 +000099 Stream() >> b + c; // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
David Blaikie15f17cb2012-10-05 00:41:03 +0000100 expected-note {{place parentheses around the '+' expression to silence this warning}}
Richard Trieucf2a4c02013-04-18 00:56:23 +0000101 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
102 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
David Blaikie15f17cb2012-10-05 00:41:03 +0000103}
Benjamin Kramer0345f9f2013-03-30 11:56:00 +0000104
105namespace PR15628 {
106 struct BlockInputIter {
107 void* operator++(int);
108 void* operator--(int);
109 };
110
111 void test(BlockInputIter i) {
112 (void)(i++ ? true : false); // no-warning
113 (void)(i-- ? true : false); // no-warning
114 }
115}
Richard Trieu7ec1a312014-08-23 00:30:57 +0000116
117namespace PR20735 {
118 struct X {
119 static int state;
120 static int get();
121 int get_num();
122 int num;
123 };
124 namespace ns {
125 int num = 0;
126 int get();
127 }
128 void test(X x) {
129 if (5 & x.get_num() != 0) {}
130 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
131 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
132 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
133 // CHECK: place parentheses around the '!=' expression to silence this warning
134 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
135 // fix-it:"{{.*}}":{[[@LINE-6]]:29-[[@LINE-6]]:29}:")"
136 // CHECK: place parentheses around the & expression to evaluate it first
137 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
138 // fix-it:"{{.*}}":{[[@LINE-9]]:24-[[@LINE-9]]:24}:")"
139
140 if (5 & x.num != 0) {}
141 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
142 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
143 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
144 // CHECK: place parentheses around the '!=' expression to silence this warning
145 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
146 // fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")"
147 // CHECK: place parentheses around the & expression to evaluate it first
148 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
149 // fix-it:"{{.*}}":{[[@LINE-9]]:18-[[@LINE-9]]:18}:")"
150
151 if (5 & x.state != 0) {}
152 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
153 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
154 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
155 // CHECK: place parentheses around the '!=' expression to silence this warning
156 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
157 // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")"
158 // CHECK: place parentheses around the & expression to evaluate it first
159 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
160 // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")"
161
162 if (5 & x.get() != 0) {}
163 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
164 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
165 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
166 // CHECK: place parentheses around the '!=' expression to silence this warning
167 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
168 // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")"
169 // CHECK: place parentheses around the & expression to evaluate it first
170 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
171 // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")"
172
173 if (5 & X::state != 0) {}
174 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
175 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
176 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
177 // CHECK: place parentheses around the '!=' expression to silence this warning
178 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
179 // fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"
180 // CHECK: place parentheses around the & expression to evaluate it first
181 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
182 // fix-it:"{{.*}}":{[[@LINE-9]]:21-[[@LINE-9]]:21}:")"
183
184 if (5 & X::get() != 0) {}
185 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
186 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
187 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
188 // CHECK: place parentheses around the '!=' expression to silence this warning
189 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
190 // fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"
191 // CHECK: place parentheses around the & expression to evaluate it first
192 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
193 // fix-it:"{{.*}}":{[[@LINE-9]]:21-[[@LINE-9]]:21}:")"
194
195 if (5 & ns::get() != 0) {}
196 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
197 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
198 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
199 // CHECK: place parentheses around the '!=' expression to silence this warning
200 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
201 // fix-it:"{{.*}}":{[[@LINE-6]]:27-[[@LINE-6]]:27}:")"
202 // CHECK: place parentheses around the & expression to evaluate it first
203 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
204 // fix-it:"{{.*}}":{[[@LINE-9]]:22-[[@LINE-9]]:22}:")"
205
206 if (5 & ns::num != 0) {}
207 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
208 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
209 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
210 // CHECK: place parentheses around the '!=' expression to silence this warning
211 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
212 // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")"
213 // CHECK: place parentheses around the & expression to evaluate it first
214 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
215 // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")"
216 }
217}