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