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 ^ i | i); // expected-warning {{'^' within '|'}} \ 59 // expected-note {{place parentheses around the '^' expression to silence this warning}} 60 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"(" 61 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:")" 62 63 (void)(i | i ^ i); // expected-warning {{'^' within '|'}} \ 64 // expected-note {{place parentheses around the '^' expression to silence this warning}} 65 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:14-[[@LINE-2]]:14}:"(" 66 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:19-[[@LINE-3]]:19}:")" 67 68 (void)(i & i ^ i); // expected-warning {{'&' within '^'}} \ 69 // expected-note {{place parentheses around the '&' expression to silence this warning}} 70 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"(" 71 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:")" 72 73 (void)(i ^ i & i); // expected-warning {{'&' within '^'}} \ 74 // expected-note {{place parentheses around the '&' expression to silence this warning}} 75 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:14-[[@LINE-2]]:14}:"(" 76 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:19-[[@LINE-3]]:19}:")" 77 78 (void)(i || 79 i && i); // expected-warning {{'&&' within '||'}} \ 80 // expected-note {{place parentheses around the '&&' expression to silence this warning}} 81 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:14-[[@LINE-2]]:14}:"(" 82 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")" 83 84 (void)(i || i && "w00t"); // no warning. 85 (void)("w00t" && i || i); // no warning. 86 87 (void)(i || i && "w00t" || i); // expected-warning {{'&&' within '||'}} \ 88 // expected-note {{place parentheses around the '&&' expression to silence this warning}} 89 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"(" 90 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")" 91 92 (void)(i || "w00t" && i || i); // expected-warning {{'&&' within '||'}} \ 93 // expected-note {{place parentheses around the '&&' expression to silence this warning}} 94 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"(" 95 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")" 96 97 (void)(i && i || 0); // no warning. 98 (void)(0 || i && i); // no warning. 99 } 100 101 _Bool someConditionFunc(); 102 103 void conditional_op(int x, int y, _Bool b, void* p) { 104 (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \ 105 // expected-note {{place parentheses around the '+' expression to silence this warning}} \ 106 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} 107 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"(" 108 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:33-[[@LINE-4]]:33}:")" 109 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"(" 110 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:41-[[@LINE-6]]:41}:")" 111 112 (void)((x + someConditionFunc()) ? 1 : 2); // no warning 113 114 (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \ 115 // expected-note {{place parentheses around the '-' expression to silence this warning}} \ 116 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} 117 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"(" 118 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:15-[[@LINE-4]]:15}:")" 119 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"(" 120 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")" 121 122 (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \ 123 // expected-note {{place parentheses around the '*' expression to silence this warning}} \ 124 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} 125 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"(" 126 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")" 127 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"(" 128 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:30-[[@LINE-6]]:30}:")" 129 130 (void)(x / !x ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '/'}} \ 131 // expected-note {{place parentheses around the '/' expression to silence this warning}} \ 132 // expected-note {{place parentheses around the '?:' expression to evaluate it first}} 133 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"(" 134 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")" 135 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"(" 136 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:24-[[@LINE-6]]:24}:")" 137 138 (void)(x % 2 ? 1 : 2); // no warning 139 140 (void)(x + p ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}} 141 (void)(p + x ? 1 : 2); // no warning 142 143 (void)(p + b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}} 144 145 (void)(x + y > 0 ? 1 : 2); // no warning 146 (void)(x + (y > 0) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}} 147 } 148 149 // RUN: not %clang_cc1 -fsyntax-only -Wparentheses -Werror -fdiagnostics-show-option %s 2>&1 | FileCheck %s -check-prefix=CHECK-FLAG 150 // CHECK-FLAG: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses] 151