[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Sema / parentheses.c
blobf0090c2ee9d14add04216e3a3f6d38fb1ad65f04
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
3 // RUN: %clang_cc1 -Wparentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
5 // Test the various warnings under -Wparentheses
6 void if_assign(void) {
7 int i;
8 if (i = 4) {} // expected-warning {{assignment as a condition}} \
9 // expected-note{{place parentheses around the assignment to silence this warning}} \
10 // expected-note{{use '==' to turn this assignment into an equality comparison}}
11 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:7-[[@LINE-3]]:7}:"("
12 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:12-[[@LINE-4]]:12}:")"
13 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:9-[[@LINE-5]]:10}:"=="
15 if ((i = 4)) {}
18 void bitwise_rel(unsigned i) {
19 (void)(i & 0x2 == 0); // expected-warning {{& has lower precedence than ==}} \
20 // expected-note{{place parentheses around the '==' expression to silence this warning}} \
21 // expected-note{{place parentheses around the & expression to evaluate it first}}
22 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:14-[[@LINE-3]]:14}:"("
23 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")"
24 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
25 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:17-[[@LINE-6]]:17}:")"
27 (void)(0 == i & 0x2); // expected-warning {{& has lower precedence than ==}} \
28 // expected-note{{place parentheses around the '==' expression to silence this warning}} \
29 // expected-note{{place parentheses around the & expression to evaluate it first}}
30 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
31 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
32 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("
33 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:22-[[@LINE-6]]:22}:")"
35 (void)(i & 0xff < 30); // expected-warning {{& has lower precedence than <}} \
36 // expected-note{{place parentheses around the '<' expression to silence this warning}} \
37 // expected-note{{place parentheses around the & expression to evaluate it first}}
38 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:14-[[@LINE-3]]:14}:"("
39 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:23-[[@LINE-4]]:23}:")"
40 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:10-[[@LINE-5]]:10}:"("
41 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:18-[[@LINE-6]]:18}:")"
43 (void)((i & 0x2) == 0);
44 (void)(i & (0x2 == 0));
45 // Eager logical op
46 (void)(i == 1 | i == 2 | i == 3);
47 (void)(i != 1 & i != 2 & i != 3);
50 _Bool someConditionFunc(void);
52 void conditional_op(int x, int y, _Bool b, void* p) {
53 (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
54 // expected-note {{place parentheses around the '+' expression to silence this warning}} \
55 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
56 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
57 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:33-[[@LINE-4]]:33}:")"
58 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
59 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:41-[[@LINE-6]]:41}:")"
61 (void)((x + someConditionFunc()) ? 1 : 2); // no warning
63 (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
64 // expected-note {{place parentheses around the '-' expression to silence this warning}} \
65 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
66 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
67 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:15-[[@LINE-4]]:15}:")"
68 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
69 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")"
71 (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \
72 // expected-note {{place parentheses around the '*' expression to silence this warning}} \
73 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
74 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
75 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")"
76 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
77 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:30-[[@LINE-6]]:30}:")"
79 (void)(x / !x ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '/'}} \
80 // expected-note {{place parentheses around the '/' expression to silence this warning}} \
81 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
82 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("
83 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"
84 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("
85 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:24-[[@LINE-6]]:24}:")"
87 (void)(x % 2 ? 1 : 2); // no warning
89 (void)(x + p ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}}
90 (void)(p + x ? 1 : 2); // no warning
92 (void)(p + b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}}
94 (void)(x + y > 0 ? 1 : 2); // no warning
95 (void)(x + (y > 0) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}}
97 (void)(b ? 0xf0 : 0x10 | b ? 0x5 : 0x2); // expected-warning {{operator '?:' has lower precedence than '|'}} expected-note 2{{place parentheses}}
99 (void)((b ? 0xf0 : 0x10) | (b ? 0x5 : 0x2)); // no warning, has parentheses
100 (void)(b ? 0xf0 : (0x10 | b) ? 0x5 : 0x2); // no warning, has parentheses
102 (void)(x | b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '|'}} expected-note 2{{place parentheses}}
103 (void)(x & b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '&'}} expected-note 2{{place parentheses}}
105 (void)((x | b) ? 1 : 2); // no warning, has parentheses
106 (void)(x | (b ? 1 : 2)); // no warning, has parentheses
107 (void)((x & b) ? 1 : 2); // no warning, has parentheses
108 (void)(x & (b ? 1 : 2)); // no warning, has parentheses
110 // Only warn on uses of the bitwise operators, and not the logical operators.
111 // The bitwise operators are more likely to be bugs while the logical
112 // operators are more likely to be used correctly. Since there is no
113 // explicit logical-xor operator, the bitwise-xor is commonly used instead.
114 // For this warning, treat the bitwise-xor as if it were a logical operator.
115 (void)(x ^ b ? 1 : 2); // no warning, ^ is often used as logical xor
116 (void)(x || b ? 1 : 2); // no warning, logical operator
117 (void)(x && b ? 1 : 2); // no warning, logical operator
120 // RUN: not %clang_cc1 -fsyntax-only -Wparentheses -Werror %s 2>&1 | FileCheck %s -check-prefix=CHECK-FLAG
121 // CHECK-FLAG: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses]