blob: 300e58558867ead319a959c3ee6a86f3f1d84cd0 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
Richard Trieuaea52012013-04-18 00:56:23 +00002// RUN: %clang_cc1 -Wparentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
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}} \
Richard Trieuaea52012013-04-18 00:56:23 +00008 // expected-note{{place parentheses around the assignment to silence this warning}} \
9 // expected-note{{use '==' to turn this assignment into an equality comparison}}
10 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:7-[[@LINE-3]]:7}:"("
11 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:12-[[@LINE-4]]:12}:")"
12 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:9-[[@LINE-5]]:10}:"=="
13
Sebastian Redl9e1d29b2009-10-26 15:24:15 +000014 if ((i = 4)) {}
15}
16
17void bitwise_rel(unsigned i) {
Douglas Gregor827feec2010-01-08 00:20:23 +000018 (void)(i & 0x2 == 0); // expected-warning {{& has lower precedence than ==}} \
Richard Trieuaea52012013-04-18 00:56:23 +000019 // expected-note{{place parentheses around the '==' expression to silence this warning}} \
20 // expected-note{{place parentheses around the & expression to evaluate it first}}
21 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:14-[[@LINE-3]]:14}:"("
22 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")"
23 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
24 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:17-[[@LINE-6]]:17}:")"
25
Douglas Gregor827feec2010-01-08 00:20:23 +000026 (void)(0 == i & 0x2); // expected-warning {{& has lower precedence than ==}} \
Richard Trieuaea52012013-04-18 00:56:23 +000027 // expected-note{{place parentheses around the '==' expression to silence this warning}} \
28 // expected-note{{place parentheses around the & expression to evaluate it first}}
29 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
30 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
31 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
32 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:22-[[@LINE-6]]:22}:")"
33
Douglas Gregor827feec2010-01-08 00:20:23 +000034 (void)(i & 0xff < 30); // expected-warning {{& has lower precedence than <}} \
Richard Trieuaea52012013-04-18 00:56:23 +000035 // expected-note{{place parentheses around the '<' expression to silence this warning}} \
36 // expected-note{{place parentheses around the & expression to evaluate it first}}
37 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:14-[[@LINE-3]]:14}:"("
38 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:23-[[@LINE-4]]:23}:")"
39 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
40 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:18-[[@LINE-6]]:18}:")"
41
Sebastian Redl9e1d29b2009-10-26 15:24:15 +000042 (void)((i & 0x2) == 0);
43 (void)(i & (0x2 == 0));
44 // Eager logical op
45 (void)(i == 1 | i == 2 | i == 3);
46 (void)(i != 1 & i != 2 & i != 3);
Argyrios Kyrtzidisbee77f72010-11-16 21:00:12 +000047
Argyrios Kyrtzidis33f46e22011-06-20 18:41:26 +000048 (void)(i & i | i); // expected-warning {{'&' within '|'}} \
49 // expected-note {{place parentheses around the '&' expression to silence this warning}}
Richard Trieuaea52012013-04-18 00:56:23 +000050 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"("
51 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:")"
Argyrios Kyrtzidis33f46e22011-06-20 18:41:26 +000052
53 (void)(i | i & i); // expected-warning {{'&' within '|'}} \
54 // expected-note {{place parentheses around the '&' expression to silence this warning}}
Richard Trieuaea52012013-04-18 00:56:23 +000055 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:14-[[@LINE-2]]:14}:"("
56 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:19-[[@LINE-3]]:19}:")"
Argyrios Kyrtzidis33f46e22011-06-20 18:41:26 +000057
Argyrios Kyrtzidisa61aedc2011-04-22 19:16:27 +000058 (void)(i ||
59 i && i); // expected-warning {{'&&' within '||'}} \
Richard Trieuaea52012013-04-18 00:56:23 +000060 // expected-note {{place parentheses around the '&&' expression to silence this warning}}
61 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:14-[[@LINE-2]]:14}:"("
62 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"
63
Argyrios Kyrtzidis567bb712010-11-17 18:26:36 +000064 (void)(i || i && "w00t"); // no warning.
65 (void)("w00t" && i || i); // no warning.
Richard Trieuaea52012013-04-18 00:56:23 +000066
Argyrios Kyrtzidis567bb712010-11-17 18:26:36 +000067 (void)(i || i && "w00t" || i); // expected-warning {{'&&' within '||'}} \
68 // expected-note {{place parentheses around the '&&' expression to silence this warning}}
Richard Trieuaea52012013-04-18 00:56:23 +000069 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
70 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")"
71
Argyrios Kyrtzidis567bb712010-11-17 18:26:36 +000072 (void)(i || "w00t" && i || i); // expected-warning {{'&&' within '||'}} \
73 // expected-note {{place parentheses around the '&&' expression to silence this warning}}
Richard Trieuaea52012013-04-18 00:56:23 +000074 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("
75 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")"
76
Argyrios Kyrtzidis47d512c2010-11-17 19:18:19 +000077 (void)(i && i || 0); // no warning.
78 (void)(0 || i && i); // no warning.
Sebastian Redl9e1d29b2009-10-26 15:24:15 +000079}
Ted Kremenek7decebf2011-02-25 01:28:26 +000080
Hans Wennborg9cfdae32011-06-03 18:00:36 +000081_Bool someConditionFunc();
82
83void conditional_op(int x, int y, _Bool b) {
Chandler Carruth9d456242011-06-16 01:05:12 +000084 (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
Richard Trieuaea52012013-04-18 00:56:23 +000085 // expected-note {{place parentheses around the '+' expression to silence this warning}} \
86 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
87 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
88 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:33-[[@LINE-4]]:33}:")"
89 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
90 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:41-[[@LINE-6]]:41}:")"
Hans Wennborg9cfdae32011-06-03 18:00:36 +000091
Hans Wennborgcb4d7c22011-09-12 12:07:30 +000092 (void)((x + someConditionFunc()) ? 1 : 2); // no warning
93
Chandler Carruth9d456242011-06-16 01:05:12 +000094 (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
Richard Trieuaea52012013-04-18 00:56:23 +000095 // expected-note {{place parentheses around the '-' expression to silence this warning}} \
96 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
97 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
98 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:15-[[@LINE-4]]:15}:")"
99 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
100 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")"
Hans Wennborg9cfdae32011-06-03 18:00:36 +0000101
Chandler Carruth9d456242011-06-16 01:05:12 +0000102 (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
Richard Trieuaea52012013-04-18 00:56:23 +0000103 // expected-note {{place parentheses around the '*' expression to silence this warning}} \
104 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
105 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
106 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")"
107 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
108 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:30-[[@LINE-6]]:30}:")"
Hans Wennborg9cfdae32011-06-03 18:00:36 +0000109
Chandler Carruth9d456242011-06-16 01:05:12 +0000110 (void)(x / !x ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '/'}} \
Richard Trieuaea52012013-04-18 00:56:23 +0000111 // expected-note {{place parentheses around the '/' expression to silence this warning}} \
112 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
113 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
114 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
115 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
116 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:24-[[@LINE-6]]:24}:")"
Hans Wennborg9cfdae32011-06-03 18:00:36 +0000117
Hans Wennborg9cfdae32011-06-03 18:00:36 +0000118 (void)(x % 2 ? 1 : 2); // no warning
119}
120
Richard Trieuaea52012013-04-18 00:56:23 +0000121// RUN: %clang_cc1 -fsyntax-only -Wparentheses -Werror -fdiagnostics-show-option %s 2>&1 | FileCheck %s -check-prefix=CHECK-FLAG
122// CHECK-FLAG: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses]