Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s |
Richard Trieu | aea5201 | 2013-04-18 00:56:23 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -Wparentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 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 '+'}} \ |
Richard Trieu | aea5201 | 2013-04-18 00:56:23 +0000 | [diff] [blame] | 8 | // expected-note {{place parentheses around the '+' expression to silence this warning}} \ |
| 9 | // expected-note {{place parentheses around the '?:' expression to evaluate it first}} |
| 10 | // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"(" |
| 11 | // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:33-[[@LINE-4]]:33}:")" |
| 12 | // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"(" |
| 13 | // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:41-[[@LINE-6]]:41}:")" |
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 - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \ |
Richard Trieu | aea5201 | 2013-04-18 00:56:23 +0000 | [diff] [blame] | 16 | // expected-note {{place parentheses around the '-' expression to silence this warning}} \ |
| 17 | // expected-note {{place parentheses around the '?:' expression to evaluate it first}} |
| 18 | // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"(" |
| 19 | // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:15-[[@LINE-4]]:15}:")" |
| 20 | // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"(" |
| 21 | // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")" |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 22 | |
Chandler Carruth | 9d45624 | 2011-06-16 01:05:12 +0000 | [diff] [blame] | 23 | (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \ |
Richard Trieu | aea5201 | 2013-04-18 00:56:23 +0000 | [diff] [blame] | 24 | // expected-note {{place parentheses around the '*' expression to silence this warning}} \ |
| 25 | // expected-note {{place parentheses around the '?:' expression to evaluate it first}} |
| 26 | // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"(" |
| 27 | // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")" |
| 28 | // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"(" |
| 29 | // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:30-[[@LINE-6]]:30}:")" |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 30 | } |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 31 | |
| 32 | class Stream { |
| 33 | public: |
| 34 | operator int(); |
| 35 | Stream &operator<<(int); |
| 36 | Stream &operator<<(const char*); |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 37 | Stream &operator>>(int); |
| 38 | Stream &operator>>(const char*); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | void f(Stream& s, bool b) { |
Chandler Carruth | 9d45624 | 2011-06-16 01:05:12 +0000 | [diff] [blame] | 42 | (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \ |
Richard Trieu | aea5201 | 2013-04-18 00:56:23 +0000 | [diff] [blame] | 43 | // expected-note {{place parentheses around the '<<' expression to silence this warning}} \ |
| 44 | // expected-note {{place parentheses around the '?:' expression to evaluate it first}} |
| 45 | // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"(" |
| 46 | // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")" |
| 47 | // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"(" |
| 48 | // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:32-[[@LINE-6]]:32}:")" |
| 49 | |
Richard Trieu | 2a6e528 | 2013-04-17 02:12:45 +0000 | [diff] [blame] | 50 | (void)(s << 5 == 1); // expected-warning {{overloaded operator << has lower precedence than comparison operator}} \ |
Richard Trieu | 1a7df99 | 2013-04-18 01:04:37 +0000 | [diff] [blame] | 51 | // expected-note {{place parentheses around the '<<' expression to silence this warning}} \ |
| 52 | // expected-note {{place parentheses around comparison expression to evaluate it first}} |
| 53 | // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"(" |
| 54 | // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")" |
| 55 | // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"(" |
| 56 | // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")" |
Richard Trieu | aea5201 | 2013-04-18 00:56:23 +0000 | [diff] [blame] | 57 | |
Richard Trieu | 2a6e528 | 2013-04-17 02:12:45 +0000 | [diff] [blame] | 58 | (void)(s >> 5 == 1); // expected-warning {{overloaded operator >> has lower precedence than comparison operator}} \ |
Richard Trieu | 1a7df99 | 2013-04-18 01:04:37 +0000 | [diff] [blame] | 59 | // expected-note {{place parentheses around the '>>' expression to silence this warning}} \ |
| 60 | // expected-note {{place parentheses around comparison expression to evaluate it first}} |
| 61 | // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"(" |
| 62 | // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")" |
| 63 | // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"(" |
| 64 | // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")" |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 65 | } |
Chandler Carruth | 14d251c | 2011-06-21 17:22:09 +0000 | [diff] [blame] | 66 | |
| 67 | struct S { |
| 68 | operator int() { return 42; } |
| 69 | friend S operator+(const S &lhs, bool) { return S(); } |
| 70 | }; |
| 71 | |
| 72 | void test(S *s, bool (S::*m_ptr)()) { |
| 73 | (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \ |
Richard Trieu | aea5201 | 2013-04-18 00:56:23 +0000 | [diff] [blame] | 74 | // expected-note {{place parentheses around the '+' expression to silence this warning}} \ |
| 75 | // expected-note {{place parentheses around the '?:' expression to evaluate it first}} |
| 76 | // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"(" |
| 77 | // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:19-[[@LINE-4]]:19}:")" |
| 78 | // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"(" |
| 79 | // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:35-[[@LINE-6]]:35}:")" |
Chandler Carruth | 14d251c | 2011-06-21 17:22:09 +0000 | [diff] [blame] | 80 | |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 81 | (void)((*s + true) ? "foo" : "bar"); // No warning. |
| 82 | |
Chandler Carruth | 14d251c | 2011-06-21 17:22:09 +0000 | [diff] [blame] | 83 | // Don't crash on unusual member call expressions. |
| 84 | (void)((s->*m_ptr)() ? "foo" : "bar"); |
| 85 | } |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 86 | |
| 87 | void test(int a, int b, int c) { |
David Blaikie | 5f531a4 | 2012-10-19 18:26:06 +0000 | [diff] [blame] | 88 | (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] | 89 | expected-note {{place parentheses around the '+' expression to silence this warning}} |
Richard Trieu | aea5201 | 2013-04-18 00:56:23 +0000 | [diff] [blame] | 90 | // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"(" |
| 91 | // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")" |
| 92 | |
David Blaikie | 5f531a4 | 2012-10-19 18:26:06 +0000 | [diff] [blame] | 93 | (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] | 94 | expected-note {{place parentheses around the '-' expression to silence this warning}} |
Richard Trieu | aea5201 | 2013-04-18 00:56:23 +0000 | [diff] [blame] | 95 | // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"(" |
| 96 | // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:")" |
| 97 | |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 98 | Stream() << b + c; |
David Blaikie | 5f531a4 | 2012-10-19 18:26:06 +0000 | [diff] [blame] | 99 | 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] | 100 | expected-note {{place parentheses around the '+' expression to silence this warning}} |
Richard Trieu | aea5201 | 2013-04-18 00:56:23 +0000 | [diff] [blame] | 101 | // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"(" |
| 102 | // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")" |
David Blaikie | b3f55c5 | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 103 | } |
Benjamin Kramer | 66dca6e | 2013-03-30 11:56:00 +0000 | [diff] [blame] | 104 | |
| 105 | namespace PR15628 { |
| 106 | struct BlockInputIter { |
| 107 | void* operator++(int); |
| 108 | void* operator--(int); |
| 109 | }; |
| 110 | |
| 111 | void test(BlockInputIter i) { |
| 112 | (void)(i++ ? true : false); // no-warning |
| 113 | (void)(i-- ? true : false); // no-warning |
| 114 | } |
| 115 | } |