Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s |
Richard Trieu | cf2a4c0 | 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 | cf9bac4 | 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 | f8e06c9 | 2011-06-16 01:05:12 +0000 | [diff] [blame] | 7 | (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \ |
Richard Trieu | cf2a4c0 | 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 | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 14 | |
Chandler Carruth | f8e06c9 | 2011-06-16 01:05:12 +0000 | [diff] [blame] | 15 | (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \ |
Richard Trieu | cf2a4c0 | 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 | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 22 | |
Chandler Carruth | f8e06c9 | 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 | cf2a4c0 | 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 | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 30 | } |
Hans Wennborg | de2e67e | 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 | 15f17cb | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 37 | Stream &operator>>(int); |
| 38 | Stream &operator>>(const char*); |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | void f(Stream& s, bool b) { |
Chandler Carruth | f8e06c9 | 2011-06-16 01:05:12 +0000 | [diff] [blame] | 42 | (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \ |
Richard Trieu | cf2a4c0 | 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 | 9ad40ac | 2014-10-28 04:37:34 +0000 | [diff] [blame] | 50 | (void)(s << 5 == 1); // expected-warning {{overloaded operator << has higher precedence than comparison operator}} \ |
Richard Trieu | e089497 | 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 | cf2a4c0 | 2013-04-18 00:56:23 +0000 | [diff] [blame] | 57 | |
Richard Trieu | 9ad40ac | 2014-10-28 04:37:34 +0000 | [diff] [blame] | 58 | (void)(s >> 5 == 1); // expected-warning {{overloaded operator >> has higher precedence than comparison operator}} \ |
Richard Trieu | e089497 | 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 | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 65 | } |
Chandler Carruth | 4352b0b | 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 | cf2a4c0 | 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 | 4352b0b | 2011-06-21 17:22:09 +0000 | [diff] [blame] | 80 | |
Hans Wennborg | be207b3 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 81 | (void)((*s + true) ? "foo" : "bar"); // No warning. |
| 82 | |
Chandler Carruth | 4352b0b | 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 | 15f17cb | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 86 | |
| 87 | void test(int a, int b, int c) { |
David Blaikie | 82d3ab9 | 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 | 15f17cb | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 89 | expected-note {{place parentheses around the '+' expression to silence this warning}} |
Richard Trieu | cf2a4c0 | 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 | 82d3ab9 | 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 | 15f17cb | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 94 | expected-note {{place parentheses around the '-' expression to silence this warning}} |
Richard Trieu | cf2a4c0 | 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 | 15f17cb | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 98 | Stream() << b + c; |
David Blaikie | 82d3ab9 | 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 | 15f17cb | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 100 | expected-note {{place parentheses around the '+' expression to silence this warning}} |
Richard Trieu | cf2a4c0 | 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 | 15f17cb | 2012-10-05 00:41:03 +0000 | [diff] [blame] | 103 | } |
Benjamin Kramer | 0345f9f | 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 | } |
Richard Trieu | 7ec1a31 | 2014-08-23 00:30:57 +0000 | [diff] [blame] | 116 | |
| 117 | namespace PR20735 { |
| 118 | struct X { |
| 119 | static int state; |
| 120 | static int get(); |
| 121 | int get_num(); |
| 122 | int num; |
| 123 | }; |
| 124 | namespace ns { |
| 125 | int num = 0; |
| 126 | int get(); |
| 127 | } |
| 128 | void test(X x) { |
| 129 | if (5 & x.get_num() != 0) {} |
| 130 | // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}} |
| 131 | // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}} |
| 132 | // expected-note@-3 {{place parentheses around the & expression to evaluate it first}} |
| 133 | // CHECK: place parentheses around the '!=' expression to silence this warning |
| 134 | // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"(" |
| 135 | // fix-it:"{{.*}}":{[[@LINE-6]]:29-[[@LINE-6]]:29}:")" |
| 136 | // CHECK: place parentheses around the & expression to evaluate it first |
| 137 | // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"(" |
| 138 | // fix-it:"{{.*}}":{[[@LINE-9]]:24-[[@LINE-9]]:24}:")" |
| 139 | |
| 140 | if (5 & x.num != 0) {} |
| 141 | // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}} |
| 142 | // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}} |
| 143 | // expected-note@-3 {{place parentheses around the & expression to evaluate it first}} |
| 144 | // CHECK: place parentheses around the '!=' expression to silence this warning |
| 145 | // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"(" |
| 146 | // fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")" |
| 147 | // CHECK: place parentheses around the & expression to evaluate it first |
| 148 | // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"(" |
| 149 | // fix-it:"{{.*}}":{[[@LINE-9]]:18-[[@LINE-9]]:18}:")" |
| 150 | |
| 151 | if (5 & x.state != 0) {} |
| 152 | // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}} |
| 153 | // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}} |
| 154 | // expected-note@-3 {{place parentheses around the & expression to evaluate it first}} |
| 155 | // CHECK: place parentheses around the '!=' expression to silence this warning |
| 156 | // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"(" |
| 157 | // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")" |
| 158 | // CHECK: place parentheses around the & expression to evaluate it first |
| 159 | // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"(" |
| 160 | // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")" |
| 161 | |
| 162 | if (5 & x.get() != 0) {} |
| 163 | // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}} |
| 164 | // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}} |
| 165 | // expected-note@-3 {{place parentheses around the & expression to evaluate it first}} |
| 166 | // CHECK: place parentheses around the '!=' expression to silence this warning |
| 167 | // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"(" |
| 168 | // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")" |
| 169 | // CHECK: place parentheses around the & expression to evaluate it first |
| 170 | // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"(" |
| 171 | // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")" |
| 172 | |
| 173 | if (5 & X::state != 0) {} |
| 174 | // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}} |
| 175 | // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}} |
| 176 | // expected-note@-3 {{place parentheses around the & expression to evaluate it first}} |
| 177 | // CHECK: place parentheses around the '!=' expression to silence this warning |
| 178 | // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"(" |
| 179 | // fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")" |
| 180 | // CHECK: place parentheses around the & expression to evaluate it first |
| 181 | // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"(" |
| 182 | // fix-it:"{{.*}}":{[[@LINE-9]]:21-[[@LINE-9]]:21}:")" |
| 183 | |
| 184 | if (5 & X::get() != 0) {} |
| 185 | // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}} |
| 186 | // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}} |
| 187 | // expected-note@-3 {{place parentheses around the & expression to evaluate it first}} |
| 188 | // CHECK: place parentheses around the '!=' expression to silence this warning |
| 189 | // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"(" |
| 190 | // fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")" |
| 191 | // CHECK: place parentheses around the & expression to evaluate it first |
| 192 | // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"(" |
| 193 | // fix-it:"{{.*}}":{[[@LINE-9]]:21-[[@LINE-9]]:21}:")" |
| 194 | |
| 195 | if (5 & ns::get() != 0) {} |
| 196 | // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}} |
| 197 | // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}} |
| 198 | // expected-note@-3 {{place parentheses around the & expression to evaluate it first}} |
| 199 | // CHECK: place parentheses around the '!=' expression to silence this warning |
| 200 | // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"(" |
| 201 | // fix-it:"{{.*}}":{[[@LINE-6]]:27-[[@LINE-6]]:27}:")" |
| 202 | // CHECK: place parentheses around the & expression to evaluate it first |
| 203 | // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"(" |
| 204 | // fix-it:"{{.*}}":{[[@LINE-9]]:22-[[@LINE-9]]:22}:")" |
| 205 | |
| 206 | if (5 & ns::num != 0) {} |
| 207 | // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}} |
| 208 | // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}} |
| 209 | // expected-note@-3 {{place parentheses around the & expression to evaluate it first}} |
| 210 | // CHECK: place parentheses around the '!=' expression to silence this warning |
| 211 | // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"(" |
| 212 | // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")" |
| 213 | // CHECK: place parentheses around the & expression to evaluate it first |
| 214 | // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"(" |
| 215 | // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")" |
| 216 | } |
| 217 | } |