Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s |
| 2 | // RUN: %clang_cc1 -Wparentheses -fixit %s -o - | %clang_cc1 -Wparentheses -Werror - |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 3 | |
| 4 | // Test the various warnings under -Wparentheses |
| 5 | void if_assign(void) { |
| 6 | int i; |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 7 | if (i = 4) {} // expected-warning {{assignment as a condition}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8 | // expected-note{{use '==' to turn this assignment into an equality comparison}} \ |
| 9 | // expected-note{{place parentheses around the assignment to silence this warning}} |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 10 | if ((i = 4)) {} |
| 11 | } |
| 12 | |
| 13 | void bitwise_rel(unsigned i) { |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 14 | (void)(i & 0x2 == 0); // expected-warning {{& has lower precedence than ==}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 15 | // expected-note{{place parentheses around the & expression to evaluate it first}} \ |
| 16 | // expected-note{{place parentheses around the == expression to silence this warning}} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 17 | (void)(0 == i & 0x2); // expected-warning {{& has lower precedence than ==}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 18 | // expected-note{{place parentheses around the & expression to evaluate it first}} \ |
| 19 | // expected-note{{place parentheses around the == expression to silence this warning}} |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 20 | (void)(i & 0xff < 30); // expected-warning {{& has lower precedence than <}} \ |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 21 | // expected-note{{place parentheses around the & expression to evaluate it first}} \ |
| 22 | // expected-note{{place parentheses around the < expression to silence this warning}} |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 23 | (void)((i & 0x2) == 0); |
| 24 | (void)(i & (0x2 == 0)); |
| 25 | // Eager logical op |
| 26 | (void)(i == 1 | i == 2 | i == 3); |
| 27 | (void)(i != 1 & i != 2 & i != 3); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 28 | |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 29 | (void)(i || |
| 30 | i && i); // expected-warning {{'&&' within '||'}} \ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 31 | // expected-note {{place parentheses around the '&&' expression to silence this warning}} |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 32 | (void)(i || i && "w00t"); // no warning. |
| 33 | (void)("w00t" && i || i); // no warning. |
| 34 | (void)(i || i && "w00t" || i); // expected-warning {{'&&' within '||'}} \ |
| 35 | // expected-note {{place parentheses around the '&&' expression to silence this warning}} |
| 36 | (void)(i || "w00t" && i || i); // expected-warning {{'&&' within '||'}} \ |
| 37 | // expected-note {{place parentheses around the '&&' expression to silence this warning}} |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 38 | (void)(i && i || 0); // no warning. |
| 39 | (void)(0 || i && i); // no warning. |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 40 | } |
Ted Kremenek | 7decebf | 2011-02-25 01:28:26 +0000 | [diff] [blame] | 41 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame^] | 42 | _Bool someConditionFunc(); |
| 43 | |
| 44 | void conditional_op(int x, int y, _Bool b) { |
| 45 | (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{?: has lower precedence than +}} \ |
| 46 | // expected-note {{place parentheses around the ?: expression to evaluate it first}} \ |
| 47 | // expected-note {{place parentheses around the + expression to silence this warning}} |
| 48 | |
| 49 | (void)(x - b ? 1 : 2); // expected-warning {{?: has lower precedence than -}} \ |
| 50 | // expected-note {{place parentheses around the ?: expression to evaluate it first}} \ |
| 51 | // expected-note {{place parentheses around the - expression to silence this warning}} |
| 52 | |
| 53 | (void)(x * (x == y) ? 1 : 2); // expected-warning {{?: has lower precedence than *}} \ |
| 54 | // expected-note {{place parentheses around the ?: expression to evaluate it first}} \ |
| 55 | // expected-note {{place parentheses around the * expression to silence this warning}} |
| 56 | |
| 57 | (void)(x / !x ? 1 : 2); // expected-warning {{?: has lower precedence than /}} \ |
| 58 | // expected-note {{place parentheses around the ?: expression to evaluate it first}} \ |
| 59 | // expected-note {{place parentheses around the / expression to silence this warning}} |
| 60 | |
| 61 | |
| 62 | (void)(x % 2 ? 1 : 2); // no warning |
| 63 | } |
| 64 | |
Ted Kremenek | 7decebf | 2011-02-25 01:28:26 +0000 | [diff] [blame] | 65 | // RUN: %clang_cc1 -fsyntax-only -Wparentheses -Werror -fdiagnostics-show-option %s 2>&1 | FileCheck %s |
| 66 | // CHECK: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses] |