xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/parentheses.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
2f4a2713aSLionel Sambuc // RUN: %clang_cc1 -Wparentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
3f4a2713aSLionel Sambuc 
4f4a2713aSLionel Sambuc bool someConditionFunc();
5f4a2713aSLionel Sambuc 
conditional_op(int x,int y,bool b)6f4a2713aSLionel Sambuc void conditional_op(int x, int y, bool b) {
7f4a2713aSLionel Sambuc   (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
8f4a2713aSLionel Sambuc                                            // expected-note {{place parentheses around the '+' expression to silence this warning}} \
9f4a2713aSLionel Sambuc                                            // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
10f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
11f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:33-[[@LINE-4]]:33}:")"
12f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
13f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:41-[[@LINE-6]]:41}:")"
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc   (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
16f4a2713aSLionel Sambuc                          // expected-note {{place parentheses around the '-' expression to silence this warning}} \
17f4a2713aSLionel Sambuc                          // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
18f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
19f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:15-[[@LINE-4]]:15}:")"
20f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
21f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")"
22f4a2713aSLionel Sambuc 
23f4a2713aSLionel Sambuc   (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
24f4a2713aSLionel Sambuc                                 // expected-note {{place parentheses around the '*' expression to silence this warning}} \
25f4a2713aSLionel Sambuc                                 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
26f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
27f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")"
28f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
29f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:30-[[@LINE-6]]:30}:")"
30f4a2713aSLionel Sambuc }
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc class Stream {
33f4a2713aSLionel Sambuc public:
34f4a2713aSLionel Sambuc   operator int();
35f4a2713aSLionel Sambuc   Stream &operator<<(int);
36f4a2713aSLionel Sambuc   Stream &operator<<(const char*);
37f4a2713aSLionel Sambuc   Stream &operator>>(int);
38f4a2713aSLionel Sambuc   Stream &operator>>(const char*);
39f4a2713aSLionel Sambuc };
40f4a2713aSLionel Sambuc 
f(Stream & s,bool b)41f4a2713aSLionel Sambuc void f(Stream& s, bool b) {
42f4a2713aSLionel Sambuc   (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \
43f4a2713aSLionel Sambuc                                   // expected-note {{place parentheses around the '<<' expression to silence this warning}} \
44f4a2713aSLionel Sambuc                                   // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
45f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
46f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
47f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
48f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:32-[[@LINE-6]]:32}:")"
49f4a2713aSLionel Sambuc 
50*0a6a1f1dSLionel Sambuc   (void)(s << 5 == 1); // expected-warning {{overloaded operator << has higher precedence than comparison operator}} \
51f4a2713aSLionel Sambuc                        // expected-note {{place parentheses around the '<<' expression to silence this warning}} \
52f4a2713aSLionel Sambuc                        // expected-note {{place parentheses around comparison expression to evaluate it first}}
53f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
54f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
55f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
56f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
57f4a2713aSLionel Sambuc 
58*0a6a1f1dSLionel Sambuc   (void)(s >> 5 == 1); // expected-warning {{overloaded operator >> has higher precedence than comparison operator}} \
59f4a2713aSLionel Sambuc                        // expected-note {{place parentheses around the '>>' expression to silence this warning}} \
60f4a2713aSLionel Sambuc                        // expected-note {{place parentheses around comparison expression to evaluate it first}}
61f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
62f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
63f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
64f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
65f4a2713aSLionel Sambuc }
66f4a2713aSLionel Sambuc 
67f4a2713aSLionel Sambuc struct S {
operator intS68f4a2713aSLionel Sambuc   operator int() { return 42; }
operator +(const S & lhs,bool)69f4a2713aSLionel Sambuc   friend S operator+(const S &lhs, bool) { return S(); }
70f4a2713aSLionel Sambuc };
71f4a2713aSLionel Sambuc 
test(S * s,bool (S::* m_ptr)())72f4a2713aSLionel Sambuc void test(S *s, bool (S::*m_ptr)()) {
73f4a2713aSLionel Sambuc   (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \
74f4a2713aSLionel Sambuc                                      // expected-note {{place parentheses around the '+' expression to silence this warning}} \
75f4a2713aSLionel Sambuc                                      // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
76f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
77f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:19-[[@LINE-4]]:19}:")"
78f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
79f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:35-[[@LINE-6]]:35}:")"
80f4a2713aSLionel Sambuc 
81f4a2713aSLionel Sambuc   (void)((*s + true) ? "foo" : "bar"); // No warning.
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc   // Don't crash on unusual member call expressions.
84f4a2713aSLionel Sambuc   (void)((s->*m_ptr)() ? "foo" : "bar");
85f4a2713aSLionel Sambuc }
86f4a2713aSLionel Sambuc 
test(int a,int b,int c)87f4a2713aSLionel Sambuc void test(int a, int b, int c) {
88f4a2713aSLionel Sambuc   (void)(a >> b + c); // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
89f4a2713aSLionel Sambuc                          expected-note {{place parentheses around the '+' expression to silence this warning}}
90f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
91f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc   (void)(a - b << c); // expected-warning {{operator '<<' has lower precedence than '-'; '-' will be evaluated first}} \
94f4a2713aSLionel Sambuc                          expected-note {{place parentheses around the '-' expression to silence this warning}}
95f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"("
96f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:")"
97f4a2713aSLionel Sambuc 
98f4a2713aSLionel Sambuc   Stream() << b + c;
99f4a2713aSLionel Sambuc   Stream() >> b + c; // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
100f4a2713aSLionel Sambuc                         expected-note {{place parentheses around the '+' expression to silence this warning}}
101f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
102f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
103f4a2713aSLionel Sambuc }
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc namespace PR15628 {
106f4a2713aSLionel Sambuc   struct BlockInputIter {
107f4a2713aSLionel Sambuc     void* operator++(int);
108f4a2713aSLionel Sambuc     void* operator--(int);
109f4a2713aSLionel Sambuc   };
110f4a2713aSLionel Sambuc 
test(BlockInputIter i)111f4a2713aSLionel Sambuc   void test(BlockInputIter i) {
112f4a2713aSLionel Sambuc     (void)(i++ ? true : false); // no-warning
113f4a2713aSLionel Sambuc     (void)(i-- ? true : false); // no-warning
114f4a2713aSLionel Sambuc   }
115f4a2713aSLionel Sambuc }
116*0a6a1f1dSLionel Sambuc 
117*0a6a1f1dSLionel Sambuc namespace PR20735 {
118*0a6a1f1dSLionel Sambuc   struct X {
119*0a6a1f1dSLionel Sambuc     static int state;
120*0a6a1f1dSLionel Sambuc     static int get();
121*0a6a1f1dSLionel Sambuc     int get_num();
122*0a6a1f1dSLionel Sambuc     int num;
123*0a6a1f1dSLionel Sambuc   };
124*0a6a1f1dSLionel Sambuc   namespace ns {
125*0a6a1f1dSLionel Sambuc     int num = 0;
126*0a6a1f1dSLionel Sambuc     int get();
127*0a6a1f1dSLionel Sambuc   }
test(X x)128*0a6a1f1dSLionel Sambuc   void test(X x) {
129*0a6a1f1dSLionel Sambuc     if (5 & x.get_num() != 0) {}
130*0a6a1f1dSLionel Sambuc     // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
131*0a6a1f1dSLionel Sambuc     // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
132*0a6a1f1dSLionel Sambuc     // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
133*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the '!=' expression to silence this warning
134*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
135*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-6]]:29-[[@LINE-6]]:29}:")"
136*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the & expression to evaluate it first
137*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
138*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-9]]:24-[[@LINE-9]]:24}:")"
139*0a6a1f1dSLionel Sambuc 
140*0a6a1f1dSLionel Sambuc     if (5 & x.num != 0) {}
141*0a6a1f1dSLionel Sambuc     // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
142*0a6a1f1dSLionel Sambuc     // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
143*0a6a1f1dSLionel Sambuc     // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
144*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the '!=' expression to silence this warning
145*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
146*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")"
147*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the & expression to evaluate it first
148*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
149*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-9]]:18-[[@LINE-9]]:18}:")"
150*0a6a1f1dSLionel Sambuc 
151*0a6a1f1dSLionel Sambuc     if (5 & x.state != 0) {}
152*0a6a1f1dSLionel Sambuc     // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
153*0a6a1f1dSLionel Sambuc     // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
154*0a6a1f1dSLionel Sambuc     // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
155*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the '!=' expression to silence this warning
156*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
157*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")"
158*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the & expression to evaluate it first
159*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
160*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")"
161*0a6a1f1dSLionel Sambuc 
162*0a6a1f1dSLionel Sambuc     if (5 & x.get() != 0) {}
163*0a6a1f1dSLionel Sambuc     // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
164*0a6a1f1dSLionel Sambuc     // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
165*0a6a1f1dSLionel Sambuc     // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
166*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the '!=' expression to silence this warning
167*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
168*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")"
169*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the & expression to evaluate it first
170*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
171*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")"
172*0a6a1f1dSLionel Sambuc 
173*0a6a1f1dSLionel Sambuc     if (5 & X::state != 0) {}
174*0a6a1f1dSLionel Sambuc     // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
175*0a6a1f1dSLionel Sambuc     // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
176*0a6a1f1dSLionel Sambuc     // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
177*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the '!=' expression to silence this warning
178*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
179*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"
180*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the & expression to evaluate it first
181*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
182*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-9]]:21-[[@LINE-9]]:21}:")"
183*0a6a1f1dSLionel Sambuc 
184*0a6a1f1dSLionel Sambuc     if (5 & X::get() != 0) {}
185*0a6a1f1dSLionel Sambuc     // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
186*0a6a1f1dSLionel Sambuc     // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
187*0a6a1f1dSLionel Sambuc     // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
188*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the '!=' expression to silence this warning
189*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
190*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"
191*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the & expression to evaluate it first
192*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
193*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-9]]:21-[[@LINE-9]]:21}:")"
194*0a6a1f1dSLionel Sambuc 
195*0a6a1f1dSLionel Sambuc     if (5 & ns::get() != 0) {}
196*0a6a1f1dSLionel Sambuc     // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
197*0a6a1f1dSLionel Sambuc     // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
198*0a6a1f1dSLionel Sambuc     // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
199*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the '!=' expression to silence this warning
200*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
201*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-6]]:27-[[@LINE-6]]:27}:")"
202*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the & expression to evaluate it first
203*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
204*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-9]]:22-[[@LINE-9]]:22}:")"
205*0a6a1f1dSLionel Sambuc 
206*0a6a1f1dSLionel Sambuc     if (5 & ns::num != 0) {}
207*0a6a1f1dSLionel Sambuc     // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}
208*0a6a1f1dSLionel Sambuc     // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}
209*0a6a1f1dSLionel Sambuc     // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}
210*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the '!=' expression to silence this warning
211*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("
212*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")"
213*0a6a1f1dSLionel Sambuc     // CHECK: place parentheses around the & expression to evaluate it first
214*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("
215*0a6a1f1dSLionel Sambuc     // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")"
216*0a6a1f1dSLionel Sambuc   }
217*0a6a1f1dSLionel Sambuc }
218