blob: 981ad1c3bc2ae9af80bd56a9193f1e1d8167188a [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
Sebastian Redl9e1d29b2009-10-26 15:24:15 +00002
3// Test the various warnings under -Wparentheses
4void if_assign(void) {
5 int i;
Douglas Gregor827feec2010-01-08 00:20:23 +00006 if (i = 4) {} // expected-warning {{assignment as a condition}} \
Douglas Gregor55b38842010-04-14 16:09:52 +00007 // expected-note{{use '==' to turn this assignment into an equality comparison}} \
8 // expected-note{{place parentheses around the assignment to silence this warning}}
Sebastian Redl9e1d29b2009-10-26 15:24:15 +00009 if ((i = 4)) {}
10}
11
12void bitwise_rel(unsigned i) {
Douglas Gregor827feec2010-01-08 00:20:23 +000013 (void)(i & 0x2 == 0); // expected-warning {{& has lower precedence than ==}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000014 // expected-note{{place parentheses around the & expression to evaluate it first}} \
David Blaikie6b34c172012-10-08 01:19:49 +000015 // expected-note{{place parentheses around the '==' expression to silence this warning}}
Douglas Gregor827feec2010-01-08 00:20:23 +000016 (void)(0 == i & 0x2); // expected-warning {{& has lower precedence than ==}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000017 // expected-note{{place parentheses around the & expression to evaluate it first}} \
David Blaikie6b34c172012-10-08 01:19:49 +000018 // expected-note{{place parentheses around the '==' expression to silence this warning}}
Douglas Gregor827feec2010-01-08 00:20:23 +000019 (void)(i & 0xff < 30); // expected-warning {{& has lower precedence than <}} \
Douglas Gregor55b38842010-04-14 16:09:52 +000020 // expected-note{{place parentheses around the & expression to evaluate it first}} \
David Blaikie6b34c172012-10-08 01:19:49 +000021 // expected-note{{place parentheses around the '<' expression to silence this warning}}
Sebastian Redl9e1d29b2009-10-26 15:24:15 +000022 (void)((i & 0x2) == 0);
23 (void)(i & (0x2 == 0));
24 // Eager logical op
25 (void)(i == 1 | i == 2 | i == 3);
26 (void)(i != 1 & i != 2 & i != 3);
Argyrios Kyrtzidisbee77f72010-11-16 21:00:12 +000027
Argyrios Kyrtzidis33f46e22011-06-20 18:41:26 +000028 (void)(i & i | i); // expected-warning {{'&' within '|'}} \
29 // expected-note {{place parentheses around the '&' expression to silence this warning}}
30
31 (void)(i | i & i); // expected-warning {{'&' within '|'}} \
32 // expected-note {{place parentheses around the '&' expression to silence this warning}}
33
Argyrios Kyrtzidisa61aedc2011-04-22 19:16:27 +000034 (void)(i ||
35 i && i); // expected-warning {{'&&' within '||'}} \
Argyrios Kyrtzidisbee77f72010-11-16 21:00:12 +000036 // expected-note {{place parentheses around the '&&' expression to silence this warning}}
Argyrios Kyrtzidis567bb712010-11-17 18:26:36 +000037 (void)(i || i && "w00t"); // no warning.
38 (void)("w00t" && i || i); // no warning.
39 (void)(i || i && "w00t" || i); // expected-warning {{'&&' within '||'}} \
40 // expected-note {{place parentheses around the '&&' expression to silence this warning}}
41 (void)(i || "w00t" && i || i); // expected-warning {{'&&' within '||'}} \
42 // expected-note {{place parentheses around the '&&' expression to silence this warning}}
Argyrios Kyrtzidis47d512c2010-11-17 19:18:19 +000043 (void)(i && i || 0); // no warning.
44 (void)(0 || i && i); // no warning.
Sebastian Redl9e1d29b2009-10-26 15:24:15 +000045}
Ted Kremenek7decebf2011-02-25 01:28:26 +000046
Hans Wennborg9cfdae32011-06-03 18:00:36 +000047_Bool someConditionFunc();
48
49void conditional_op(int x, int y, _Bool b) {
Chandler Carruth9d456242011-06-16 01:05:12 +000050 (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
51 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
52 // expected-note {{place parentheses around the '+' expression to silence this warning}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +000053
Hans Wennborgcb4d7c22011-09-12 12:07:30 +000054 (void)((x + someConditionFunc()) ? 1 : 2); // no warning
55
Chandler Carruth9d456242011-06-16 01:05:12 +000056 (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
57 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
58 // expected-note {{place parentheses around the '-' expression to silence this warning}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +000059
Chandler Carruth9d456242011-06-16 01:05:12 +000060 (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
61 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
62 // expected-note {{place parentheses around the '*' expression to silence this warning}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +000063
Chandler Carruth9d456242011-06-16 01:05:12 +000064 (void)(x / !x ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '/'}} \
65 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
66 // expected-note {{place parentheses around the '/' expression to silence this warning}}
Hans Wennborg9cfdae32011-06-03 18:00:36 +000067
Hans Wennborg9cfdae32011-06-03 18:00:36 +000068 (void)(x % 2 ? 1 : 2); // no warning
69}
70
Ted Kremenek7decebf2011-02-25 01:28:26 +000071// 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]