1 // RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s 2 // RUN: %clang_cc1 -Wparentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s 3 4 // Test the various warnings under -Wparentheses 5 void if_assign(void) { 6 int i; 7 if (i = 4) {} // expected-warning {{assignment as a condition}} \ 8 // 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 14 if ((i = 4)) {} 15 } 16 17 void bitwise_rel(unsigned i) { 18 (void)(i & 0x2 == 0); // expected-warning {{& has lower precedence than ==}} \ 19 // 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 26 (void)(0 == i & 0x2); // expected-warning {{& has lower precedence than ==}} \ 27 // 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 34 (void)(i & 0xff < 30); // expected-warning {{& has lower precedence than <}} \ 35 // 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 42 (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); 47 48 (void)(i & i | i); // expected-warning {{'&' within '|'}} \ 49 // expected-note {{place parentheses around the '&' expression to silence this warning}} 50 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"(" 51 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:")" 52 53 (void)(i | i & i); // expected-warning {{'&' within '|'}} \ 54 // expected-note {{place parentheses around the '&' expression to silence this warning}} 55 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:14-[[@LINE-2]]:14}:"(" 56 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:19-[[@LINE-3]]:19}:")" 57 58 (void)(i || 59 i && i); // expected-warning {{'&&' within '||'}} \ 60 // 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 64 (void)(i || i && "w00t"); // no warning. 65 (void)("w00t" && i || i); // no warning. 66 67 (void)(i || i && "w00t" || i); // expected-warning {{'&&' within '||'}} \ 68 // expected-note {{place parentheses around the '&&' expression to silence this warning}} 69 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"(" 70 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")" 71 72 (void)(i || "w00t" && i || i); // expected-warning {{'&&' within '||'}} \ 73 // expected-note {{place parentheses around the '&&' expression to silence this warning}} 74 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"(" 75 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")" 76 77 (void)(i && i || 0); // no warning. 78 (void)(0 || i && i); // no warning. 79 } 80 81 _Bool someConditionFunc(); 82 83 void conditional_op(int x, int y, _Bool b) { 84 (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \ 85 // 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}:")" 91 92 (void)((x + someConditionFunc()) ? 1 : 2); // no warning 93 94 (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \ 95 // 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}:")" 101 102 (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \ 103 // 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}:")" 109 110 (void)(x / !x ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '/'}} \ 111 // 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}:")" 117 118 (void)(x % 2 ? 1 : 2); // no warning 119 } 120 121 // RUN: not %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] 123