2009-12-16 04:14:24 +08:00
|
|
|
// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
|
|
|
|
// RUN: %clang_cc1 -Wparentheses -fixit %s -o - | %clang_cc1 -Wparentheses -Werror -
|
2009-10-26 23:24:15 +08:00
|
|
|
|
|
|
|
// Test the various warnings under -Wparentheses
|
|
|
|
void if_assign(void) {
|
|
|
|
int i;
|
2010-01-08 08:20:23 +08:00
|
|
|
if (i = 4) {} // expected-warning {{assignment as a condition}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{use '==' to turn this assignment into an equality comparison}} \
|
|
|
|
// expected-note{{place parentheses around the assignment to silence this warning}}
|
2009-10-26 23:24:15 +08:00
|
|
|
if ((i = 4)) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bitwise_rel(unsigned i) {
|
2010-01-08 08:20:23 +08:00
|
|
|
(void)(i & 0x2 == 0); // expected-warning {{& has lower precedence than ==}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{place parentheses around the & expression to evaluate it first}} \
|
2012-10-08 09:19:49 +08:00
|
|
|
// expected-note{{place parentheses around the '==' expression to silence this warning}}
|
2010-01-08 08:20:23 +08:00
|
|
|
(void)(0 == i & 0x2); // expected-warning {{& has lower precedence than ==}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{place parentheses around the & expression to evaluate it first}} \
|
2012-10-08 09:19:49 +08:00
|
|
|
// expected-note{{place parentheses around the '==' expression to silence this warning}}
|
2010-01-08 08:20:23 +08:00
|
|
|
(void)(i & 0xff < 30); // expected-warning {{& has lower precedence than <}} \
|
2010-04-15 00:09:52 +08:00
|
|
|
// expected-note{{place parentheses around the & expression to evaluate it first}} \
|
2012-10-08 09:19:49 +08:00
|
|
|
// expected-note{{place parentheses around the '<' expression to silence this warning}}
|
2009-10-26 23:24:15 +08:00
|
|
|
(void)((i & 0x2) == 0);
|
|
|
|
(void)(i & (0x2 == 0));
|
|
|
|
// Eager logical op
|
|
|
|
(void)(i == 1 | i == 2 | i == 3);
|
|
|
|
(void)(i != 1 & i != 2 & i != 3);
|
2010-11-17 05:00:12 +08:00
|
|
|
|
2011-06-21 02:41:26 +08:00
|
|
|
(void)(i & i | i); // expected-warning {{'&' within '|'}} \
|
|
|
|
// expected-note {{place parentheses around the '&' expression to silence this warning}}
|
|
|
|
|
|
|
|
(void)(i | i & i); // expected-warning {{'&' within '|'}} \
|
|
|
|
// expected-note {{place parentheses around the '&' expression to silence this warning}}
|
|
|
|
|
2011-04-23 03:16:27 +08:00
|
|
|
(void)(i ||
|
|
|
|
i && i); // expected-warning {{'&&' within '||'}} \
|
2010-11-17 05:00:12 +08:00
|
|
|
// expected-note {{place parentheses around the '&&' expression to silence this warning}}
|
2010-11-18 02:26:36 +08:00
|
|
|
(void)(i || i && "w00t"); // no warning.
|
|
|
|
(void)("w00t" && i || i); // no warning.
|
|
|
|
(void)(i || i && "w00t" || i); // expected-warning {{'&&' within '||'}} \
|
|
|
|
// expected-note {{place parentheses around the '&&' expression to silence this warning}}
|
|
|
|
(void)(i || "w00t" && i || i); // expected-warning {{'&&' within '||'}} \
|
|
|
|
// expected-note {{place parentheses around the '&&' expression to silence this warning}}
|
2010-11-18 03:18:19 +08:00
|
|
|
(void)(i && i || 0); // no warning.
|
|
|
|
(void)(0 || i && i); // no warning.
|
2009-10-26 23:24:15 +08:00
|
|
|
}
|
2011-02-25 09:28:26 +08:00
|
|
|
|
2011-06-04 02:00:36 +08:00
|
|
|
_Bool someConditionFunc();
|
|
|
|
|
|
|
|
void conditional_op(int x, int y, _Bool b) {
|
2011-06-16 09:05:12 +08:00
|
|
|
(void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
|
|
|
|
// expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
|
|
|
|
// expected-note {{place parentheses around the '+' expression to silence this warning}}
|
2011-06-04 02:00:36 +08:00
|
|
|
|
2011-09-12 20:07:30 +08:00
|
|
|
(void)((x + someConditionFunc()) ? 1 : 2); // no warning
|
|
|
|
|
2011-06-16 09:05:12 +08:00
|
|
|
(void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
|
|
|
|
// expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
|
|
|
|
// expected-note {{place parentheses around the '-' expression to silence this warning}}
|
2011-06-04 02:00:36 +08:00
|
|
|
|
2011-06-16 09:05:12 +08:00
|
|
|
(void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
|
|
|
|
// expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
|
|
|
|
// expected-note {{place parentheses around the '*' expression to silence this warning}}
|
2011-06-04 02:00:36 +08:00
|
|
|
|
2011-06-16 09:05:12 +08:00
|
|
|
(void)(x / !x ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '/'}} \
|
|
|
|
// expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
|
|
|
|
// expected-note {{place parentheses around the '/' expression to silence this warning}}
|
2011-06-04 02:00:36 +08:00
|
|
|
|
|
|
|
(void)(x % 2 ? 1 : 2); // no warning
|
|
|
|
}
|
|
|
|
|
2011-02-25 09:28:26 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Wparentheses -Werror -fdiagnostics-show-option %s 2>&1 | FileCheck %s
|
|
|
|
// CHECK: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses]
|