122 lines · c
1// RUN: %clang_cc1 -fsyntax-only -verify %s2// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s3// RUN: %clang_cc1 -Wparentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s4 5// Test the various warnings under -Wparentheses6void 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}:"=="14 15 if ((i = 4)) {}16}17 18void 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}:")"26 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}:")"34 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}:")"42 43 (void)((i & 0x2) == 0);44 (void)(i & (0x2 == 0));45 // Eager logical op46 (void)(i == 1 | i == 2 | i == 3);47 (void)(i != 1 & i != 2 & i != 3);48}49 50_Bool someConditionFunc(void);51 52void 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}:")"60 61 (void)((x + someConditionFunc()) ? 1 : 2); // no warning62 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}:")"70 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}:")"78 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}:")"86 87 (void)(x % 2 ? 1 : 2); // no warning88 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 warning91 92 (void)(p + b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}}93 94 (void)(x + y > 0 ? 1 : 2); // no warning95 (void)(x + (y > 0) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}}96 97 (void)(b ? 0xf0 : 0x10 | b ? 0x5 : 0x2); // expected-warning {{operator '?:' has lower precedence than '|'}} expected-note 2{{place parentheses}}98 99 (void)((b ? 0xf0 : 0x10) | (b ? 0x5 : 0x2)); // no warning, has parentheses100 (void)(b ? 0xf0 : (0x10 | b) ? 0x5 : 0x2); // no warning, has parentheses101 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}}104 105 (void)((x | b) ? 1 : 2); // no warning, has parentheses106 (void)(x | (b ? 1 : 2)); // no warning, has parentheses107 (void)((x & b) ? 1 : 2); // no warning, has parentheses108 (void)(x & (b ? 1 : 2)); // no warning, has parentheses109 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 logical112 // operators are more likely to be used correctly. Since there is no113 // 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 xor116 (void)(x || b ? 1 : 2); // no warning, logical operator117 (void)(x && b ? 1 : 2); // no warning, logical operator118}119 120// RUN: not %clang_cc1 -fsyntax-only -Wparentheses -Werror %s 2>&1 | FileCheck %s -check-prefix=CHECK-FLAG121// CHECK-FLAG: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses]122