xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/parentheses.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -Wparentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc bool someConditionFunc();
5*f4a2713aSLionel Sambuc 
6*f4a2713aSLionel Sambuc void conditional_op(int x, int y, bool b) {
7*f4a2713aSLionel Sambuc   (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
8*f4a2713aSLionel Sambuc                                            // expected-note {{place parentheses around the '+' expression to silence this warning}} \
9*f4a2713aSLionel Sambuc                                            // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
10*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
11*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:33-[[@LINE-4]]:33}:")"
12*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
13*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:41-[[@LINE-6]]:41}:")"
14*f4a2713aSLionel Sambuc 
15*f4a2713aSLionel Sambuc   (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
16*f4a2713aSLionel Sambuc                          // expected-note {{place parentheses around the '-' expression to silence this warning}} \
17*f4a2713aSLionel Sambuc                          // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
18*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
19*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:15-[[@LINE-4]]:15}:")"
20*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
21*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")"
22*f4a2713aSLionel Sambuc 
23*f4a2713aSLionel Sambuc   (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
24*f4a2713aSLionel Sambuc                                 // expected-note {{place parentheses around the '*' expression to silence this warning}} \
25*f4a2713aSLionel Sambuc                                 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
26*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
27*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")"
28*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
29*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:30-[[@LINE-6]]:30}:")"
30*f4a2713aSLionel Sambuc }
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc class Stream {
33*f4a2713aSLionel Sambuc public:
34*f4a2713aSLionel Sambuc   operator int();
35*f4a2713aSLionel Sambuc   Stream &operator<<(int);
36*f4a2713aSLionel Sambuc   Stream &operator<<(const char*);
37*f4a2713aSLionel Sambuc   Stream &operator>>(int);
38*f4a2713aSLionel Sambuc   Stream &operator>>(const char*);
39*f4a2713aSLionel Sambuc };
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc void f(Stream& s, bool b) {
42*f4a2713aSLionel Sambuc   (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \
43*f4a2713aSLionel Sambuc                                   // expected-note {{place parentheses around the '<<' expression to silence this warning}} \
44*f4a2713aSLionel Sambuc                                   // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
45*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
46*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
47*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
48*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:32-[[@LINE-6]]:32}:")"
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc   (void)(s << 5 == 1); // expected-warning {{overloaded operator << has lower precedence than comparison operator}} \
51*f4a2713aSLionel Sambuc                        // expected-note {{place parentheses around the '<<' expression to silence this warning}} \
52*f4a2713aSLionel Sambuc                        // expected-note {{place parentheses around comparison expression to evaluate it first}}
53*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
54*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
55*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
56*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc   (void)(s >> 5 == 1); // expected-warning {{overloaded operator >> has lower precedence than comparison operator}} \
59*f4a2713aSLionel Sambuc                        // expected-note {{place parentheses around the '>>' expression to silence this warning}} \
60*f4a2713aSLionel Sambuc                        // expected-note {{place parentheses around comparison expression to evaluate it first}}
61*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
62*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
63*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
64*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"
65*f4a2713aSLionel Sambuc }
66*f4a2713aSLionel Sambuc 
67*f4a2713aSLionel Sambuc struct S {
68*f4a2713aSLionel Sambuc   operator int() { return 42; }
69*f4a2713aSLionel Sambuc   friend S operator+(const S &lhs, bool) { return S(); }
70*f4a2713aSLionel Sambuc };
71*f4a2713aSLionel Sambuc 
72*f4a2713aSLionel Sambuc void test(S *s, bool (S::*m_ptr)()) {
73*f4a2713aSLionel Sambuc   (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \
74*f4a2713aSLionel Sambuc                                      // expected-note {{place parentheses around the '+' expression to silence this warning}} \
75*f4a2713aSLionel Sambuc                                      // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
76*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
77*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:19-[[@LINE-4]]:19}:")"
78*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
79*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:35-[[@LINE-6]]:35}:")"
80*f4a2713aSLionel Sambuc 
81*f4a2713aSLionel Sambuc   (void)((*s + true) ? "foo" : "bar"); // No warning.
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc   // Don't crash on unusual member call expressions.
84*f4a2713aSLionel Sambuc   (void)((s->*m_ptr)() ? "foo" : "bar");
85*f4a2713aSLionel Sambuc }
86*f4a2713aSLionel Sambuc 
87*f4a2713aSLionel Sambuc void test(int a, int b, int c) {
88*f4a2713aSLionel Sambuc   (void)(a >> b + c); // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
89*f4a2713aSLionel Sambuc                          expected-note {{place parentheses around the '+' expression to silence this warning}}
90*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
91*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
92*f4a2713aSLionel Sambuc 
93*f4a2713aSLionel Sambuc   (void)(a - b << c); // expected-warning {{operator '<<' has lower precedence than '-'; '-' will be evaluated first}} \
94*f4a2713aSLionel Sambuc                          expected-note {{place parentheses around the '-' expression to silence this warning}}
95*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"("
96*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:")"
97*f4a2713aSLionel Sambuc 
98*f4a2713aSLionel Sambuc   Stream() << b + c;
99*f4a2713aSLionel Sambuc   Stream() >> b + c; // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \
100*f4a2713aSLionel Sambuc                         expected-note {{place parentheses around the '+' expression to silence this warning}}
101*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
102*f4a2713aSLionel Sambuc   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
103*f4a2713aSLionel Sambuc }
104*f4a2713aSLionel Sambuc 
105*f4a2713aSLionel Sambuc namespace PR15628 {
106*f4a2713aSLionel Sambuc   struct BlockInputIter {
107*f4a2713aSLionel Sambuc     void* operator++(int);
108*f4a2713aSLionel Sambuc     void* operator--(int);
109*f4a2713aSLionel Sambuc   };
110*f4a2713aSLionel Sambuc 
111*f4a2713aSLionel Sambuc   void test(BlockInputIter i) {
112*f4a2713aSLionel Sambuc     (void)(i++ ? true : false); // no-warning
113*f4a2713aSLionel Sambuc     (void)(i-- ? true : false); // no-warning
114*f4a2713aSLionel Sambuc   }
115*f4a2713aSLionel Sambuc }
116