1 // RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -Wparentheses -fixit %s -o - | %clang_cc1 -Wparentheses -Werror -
3
4 // Test the various warnings under -Wparentheses
if_assign(void)5 void if_assign(void) {
6 int i;
7 if (i = 4) {} // expected-warning {{assignment as a condition}} \
8 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
9 // expected-note{{place parentheses around the assignment to silence this warning}}
10 if ((i = 4)) {}
11 }
12
bitwise_rel(unsigned i)13 void bitwise_rel(unsigned i) {
14 (void)(i & 0x2 == 0); // expected-warning {{& 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}}
17 (void)(0 == i & 0x2); // expected-warning {{& has lower precedence than ==}} \
18 // expected-note{{place parentheses around the & expression to evaluate it first}} \
19 // expected-note{{place parentheses around the == expression to silence this warning}}
20 (void)(i & 0xff < 30); // expected-warning {{& has lower precedence than <}} \
21 // expected-note{{place parentheses around the & expression to evaluate it first}} \
22 // expected-note{{place parentheses around the < expression to silence this warning}}
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);
28
29 (void)(i & i | i); // expected-warning {{'&' within '|'}} \
30 // expected-note {{place parentheses around the '&' expression to silence this warning}}
31
32 (void)(i | i & i); // expected-warning {{'&' within '|'}} \
33 // expected-note {{place parentheses around the '&' expression to silence this warning}}
34
35 (void)(i ||
36 i && i); // expected-warning {{'&&' within '||'}} \
37 // expected-note {{place parentheses around the '&&' expression to silence this warning}}
38 (void)(i || i && "w00t"); // no warning.
39 (void)("w00t" && i || i); // no warning.
40 (void)(i || i && "w00t" || i); // expected-warning {{'&&' within '||'}} \
41 // expected-note {{place parentheses around the '&&' expression to silence this warning}}
42 (void)(i || "w00t" && i || i); // expected-warning {{'&&' within '||'}} \
43 // expected-note {{place parentheses around the '&&' expression to silence this warning}}
44 (void)(i && i || 0); // no warning.
45 (void)(0 || i && i); // no warning.
46 }
47
48 _Bool someConditionFunc();
49
conditional_op(int x,int y,_Bool b)50 void conditional_op(int x, int y, _Bool b) {
51 (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
52 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
53 // expected-note {{place parentheses around the '+' expression to silence this warning}}
54
55 (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
56 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
57 // expected-note {{place parentheses around the '-' expression to silence this warning}}
58
59 (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
60 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
61 // expected-note {{place parentheses around the '*' expression to silence this warning}}
62
63 (void)(x / !x ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '/'}} \
64 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
65 // expected-note {{place parentheses around the '/' expression to silence this warning}}
66
67
68 (void)(x % 2 ? 1 : 2); // no warning
69 }
70
71 // RUN: %clang_cc1 -fsyntax-only -Wparentheses -Werror -fdiagnostics-show-option %s 2>&1 | FileCheck %s
72 // CHECK: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses]
73