parentheses.cpp revision 14d251cd62942bf7d56bb87a267ba2ca2f7fae3e
1// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
2// RUN: %clang_cc1 -Wparentheses -fixit %s -o - | %clang_cc1 -Wparentheses -Werror -
3
4bool someConditionFunc();
5
6void conditional_op(int x, int y, bool b) {
7  (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
8                                           // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
9                                           // expected-note {{place parentheses around the '+' expression to silence this warning}}
10
11  (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
12                         // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
13                         // expected-note {{place parentheses around the '-' expression to silence this warning}}
14
15  (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
16                                // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
17                                // expected-note {{place parentheses around the '*' expression to silence this warning}}
18}
19
20class Stream {
21public:
22  operator int();
23  Stream &operator<<(int);
24  Stream &operator<<(const char*);
25};
26
27void f(Stream& s, bool b) {
28  (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \
29                                  // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
30                                  // expected-note {{place parentheses around the '<<' expression to silence this warning}}
31}
32
33struct S {
34  operator int() { return 42; }
35  friend S operator+(const S &lhs, bool) { return S(); }
36};
37
38void test(S *s, bool (S::*m_ptr)()) {
39  (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \
40                                     // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
41                                     // expected-note {{place parentheses around the '+' expression to silence this warning}}
42
43  // Don't crash on unusual member call expressions.
44  (void)((s->*m_ptr)() ? "foo" : "bar");
45}
46