Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 2 | |
| 3 | bool someConditionFunc(); |
| 4 | |
| 5 | void conditional_op(int x, int y, bool b) { |
Chandler Carruth | 9d45624 | 2011-06-16 01:05:12 +0000 | [diff] [blame] | 6 | (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \ |
| 7 | // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \ |
| 8 | // expected-note {{place parentheses around the '+' expression to silence this warning}} |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 9 | |
Chandler Carruth | 9d45624 | 2011-06-16 01:05:12 +0000 | [diff] [blame] | 10 | (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \ |
| 11 | // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \ |
| 12 | // expected-note {{place parentheses around the '-' expression to silence this warning}} |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 13 | |
Chandler Carruth | 9d45624 | 2011-06-16 01:05:12 +0000 | [diff] [blame] | 14 | (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \ |
| 15 | // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \ |
| 16 | // expected-note {{place parentheses around the '*' expression to silence this warning}} |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 17 | } |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 18 | |
| 19 | class Stream { |
| 20 | public: |
| 21 | operator int(); |
| 22 | Stream &operator<<(int); |
| 23 | Stream &operator<<(const char*); |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 24 | Stream &operator>>(int); |
| 25 | Stream &operator>>(const char*); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | void f(Stream& s, bool b) { |
Chandler Carruth | 9d45624 | 2011-06-16 01:05:12 +0000 | [diff] [blame] | 29 | (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \ |
| 30 | // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \ |
| 31 | // expected-note {{place parentheses around the '<<' expression to silence this warning}} |
Richard Trieu | 2a6e528 | 2013-04-17 02:12:45 +0000 | [diff] [blame] | 32 | (void)(s << 5 == 1); // expected-warning {{overloaded operator << has lower precedence than comparison operator}} \ |
| 33 | // expected-note {{place parentheses around the '<<' expression to silence this warning}} \ |
| 34 | // expected-note {{place parentheses around comparison expression to evaluate it first}} |
| 35 | (void)(s >> 5 == 1); // expected-warning {{overloaded operator >> has lower precedence than comparison operator}} \ |
| 36 | // expected-note {{place parentheses around the '>>' expression to silence this warning}} \ |
| 37 | // expected-note {{place parentheses around comparison expression to evaluate it first}} |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 38 | } |
Chandler Carruth | 14d251c | 2011-06-21 17:22:09 +0000 | [diff] [blame] | 39 | |
| 40 | struct S { |
| 41 | operator int() { return 42; } |
| 42 | friend S operator+(const S &lhs, bool) { return S(); } |
| 43 | }; |
| 44 | |
| 45 | void test(S *s, bool (S::*m_ptr)()) { |
| 46 | (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \ |
| 47 | // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \ |
| 48 | // expected-note {{place parentheses around the '+' expression to silence this warning}} |
| 49 | |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 50 | (void)((*s + true) ? "foo" : "bar"); // No warning. |
| 51 | |
Chandler Carruth | 14d251c | 2011-06-21 17:22:09 +0000 | [diff] [blame] | 52 | // Don't crash on unusual member call expressions. |
| 53 | (void)((s->*m_ptr)() ? "foo" : "bar"); |
| 54 | } |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 55 | |
| 56 | void test(int a, int b, int c) { |
David Blaikie | 5f531a4 | 2012-10-19 18:26:06 +0000 | [diff] [blame] | 57 | (void)(a >> b + c); // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \ |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 58 | expected-note {{place parentheses around the '+' expression to silence this warning}} |
David Blaikie | 5f531a4 | 2012-10-19 18:26:06 +0000 | [diff] [blame] | 59 | (void)(a - b << c); // expected-warning {{operator '<<' has lower precedence than '-'; '-' will be evaluated first}} \ |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 60 | expected-note {{place parentheses around the '-' expression to silence this warning}} |
| 61 | Stream() << b + c; |
David Blaikie | 5f531a4 | 2012-10-19 18:26:06 +0000 | [diff] [blame] | 62 | Stream() >> b + c; // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \ |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 63 | expected-note {{place parentheses around the '+' expression to silence this warning}} |
| 64 | } |
Benjamin Kramer | 66dca6e | 2013-03-30 11:56:00 +0000 | [diff] [blame] | 65 | |
| 66 | namespace PR15628 { |
| 67 | struct BlockInputIter { |
| 68 | void* operator++(int); |
| 69 | void* operator--(int); |
| 70 | }; |
| 71 | |
| 72 | void test(BlockInputIter i) { |
| 73 | (void)(i++ ? true : false); // no-warning |
| 74 | (void)(i-- ? true : false); // no-warning |
| 75 | } |
| 76 | } |