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}} |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 32 | } |
Chandler Carruth | 14d251c | 2011-06-21 17:22:09 +0000 | [diff] [blame] | 33 | |
| 34 | struct S { |
| 35 | operator int() { return 42; } |
| 36 | friend S operator+(const S &lhs, bool) { return S(); } |
| 37 | }; |
| 38 | |
| 39 | void test(S *s, bool (S::*m_ptr)()) { |
| 40 | (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \ |
| 41 | // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \ |
| 42 | // expected-note {{place parentheses around the '+' expression to silence this warning}} |
| 43 | |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 44 | (void)((*s + true) ? "foo" : "bar"); // No warning. |
| 45 | |
Chandler Carruth | 14d251c | 2011-06-21 17:22:09 +0000 | [diff] [blame] | 46 | // Don't crash on unusual member call expressions. |
| 47 | (void)((s->*m_ptr)() ? "foo" : "bar"); |
| 48 | } |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 49 | |
| 50 | void test(int a, int b, int c) { |
David Blaikie | 5f531a4 | 2012-10-19 18:26:06 +0000 | [diff] [blame] | 51 | (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] | 52 | expected-note {{place parentheses around the '+' expression to silence this warning}} |
David Blaikie | 5f531a4 | 2012-10-19 18:26:06 +0000 | [diff] [blame] | 53 | (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] | 54 | expected-note {{place parentheses around the '-' expression to silence this warning}} |
| 55 | Stream() << b + c; |
David Blaikie | 5f531a4 | 2012-10-19 18:26:06 +0000 | [diff] [blame] | 56 | 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] | 57 | expected-note {{place parentheses around the '+' expression to silence this warning}} |
| 58 | } |
Benjamin Kramer | 66dca6e | 2013-03-30 11:56:00 +0000 | [diff] [blame] | 59 | |
| 60 | namespace PR15628 { |
| 61 | struct BlockInputIter { |
| 62 | void* operator++(int); |
| 63 | void* operator--(int); |
| 64 | }; |
| 65 | |
| 66 | void test(BlockInputIter i) { |
| 67 | (void)(i++ ? true : false); // no-warning |
| 68 | (void)(i-- ? true : false); // no-warning |
| 69 | } |
| 70 | } |