blob: 9751336018b7b184a15ade20f9ef405f9feb6978 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
2// RUN: %clang_cc1 -Wparentheses -fixit %s -o - | %clang_cc1 -Wparentheses -Werror -
Sebastian Redl9e1d29b2009-10-26 15:24:15 +00003
4// Test the various warnings under -Wparentheses
5void if_assign(void) {
6 int i;
Douglas Gregor827feec2010-01-08 00:20:23 +00007 if (i = 4) {} // expected-warning {{assignment as a condition}} \
Douglas Gregor55b38842010-04-14 16:09:52 +00008 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
9 // expected-note{{place parentheses around the assignment to silence this warning}}
Sebastian Redl9e1d29b2009-10-26 15:24:15 +000010 if ((i = 4)) {}
11}
12
13void bitwise_rel(unsigned i) {
Douglas Gregor827feec2010-01-08 00:20:23 +000014 (void)(i & 0x2 == 0); // expected-warning {{& has lower precedence than ==}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000015 // expected-note{{place parentheses around the & expression to evaluate it first}} \
16 // expected-note{{place parentheses around the == expression to silence this warning}}
Douglas Gregor827feec2010-01-08 00:20:23 +000017 (void)(0 == i & 0x2); // expected-warning {{& has lower precedence than ==}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000018 // expected-note{{place parentheses around the & expression to evaluate it first}} \
19 // expected-note{{place parentheses around the == expression to silence this warning}}
Douglas Gregor827feec2010-01-08 00:20:23 +000020 (void)(i & 0xff < 30); // expected-warning {{& has lower precedence than <}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000021 // expected-note{{place parentheses around the & expression to evaluate it first}} \
22 // expected-note{{place parentheses around the < expression to silence this warning}}
Sebastian Redl9e1d29b2009-10-26 15:24:15 +000023 (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 Kyrtzidisbee77f72010-11-16 21:00:12 +000028
Argyrios Kyrtzidis33f46e22011-06-20 18:41:26 +000029 (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
Argyrios Kyrtzidisa61aedc2011-04-22 19:16:27 +000035 (void)(i ||
36 i && i); // expected-warning {{'&&' within '||'}} \
Argyrios Kyrtzidisbee77f72010-11-16 21:00:12 +000037 // expected-note {{place parentheses around the '&&' expression to silence this warning}}
Argyrios Kyrtzidis567bb712010-11-17 18:26:36 +000038 (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}}
Argyrios Kyrtzidis47d512c2010-11-17 19:18:19 +000044 (void)(i && i || 0); // no warning.
45 (void)(0 || i && i); // no warning.
Sebastian Redl9e1d29b2009-10-26 15:24:15 +000046}
Ted Kremenek7decebf2011-02-25 01:28:26 +000047
Hans Wennborg9cfdae32011-06-03 18:00:36 +000048_Bool someConditionFunc();
49
50void conditional_op(int x, int y, _Bool b) {
Chandler Carruth9d456242011-06-16 01:05:12 +000051 (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}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +000054
Hans Wennborgcb4d7c22011-09-12 12:07:30 +000055 (void)((x + someConditionFunc()) ? 1 : 2); // no warning
56
Chandler Carruth9d456242011-06-16 01:05:12 +000057 (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' 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}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +000060
Chandler Carruth9d456242011-06-16 01:05:12 +000061 (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
62 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
63 // expected-note {{place parentheses around the '*' expression to silence this warning}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +000064
Chandler Carruth9d456242011-06-16 01:05:12 +000065 (void)(x / !x ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '/'}} \
66 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
67 // expected-note {{place parentheses around the '/' expression to silence this warning}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +000068
Hans Wennborg9cfdae32011-06-03 18:00:36 +000069 (void)(x % 2 ? 1 : 2); // no warning
70}
71
Ted Kremenek7decebf2011-02-25 01:28:26 +000072// RUN: %clang_cc1 -fsyntax-only -Wparentheses -Werror -fdiagnostics-show-option %s 2>&1 | FileCheck %s
73// CHECK: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses]