229 lines · cpp
1// RUN: %clang_cc1 -Wno-error=parentheses -fsyntax-only -verify %s2// RUN: %clang_cc1 -Wno-error=parentheses -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s3 4bool someConditionFunc();5 6void conditional_op(int x, int y, bool b) {7 (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \8 // expected-note {{place parentheses around the '+' expression to silence this warning}} \9 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}10 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("11 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:33-[[@LINE-4]]:33}:")"12 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("13 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:41-[[@LINE-6]]:41}:")"14 15 (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \16 // expected-note {{place parentheses around the '-' expression to silence this warning}} \17 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}18 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("19 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:15-[[@LINE-4]]:15}:")"20 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("21 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")"22 23 (void)(x * (x == y) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '*'}} \24 // expected-note {{place parentheses around the '*' expression to silence this warning}} \25 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}26 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("27 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:22}:")"28 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:14-[[@LINE-5]]:14}:"("29 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:30-[[@LINE-6]]:30}:")"30}31 32class Stream {33public:34 operator int();35 Stream &operator<<(int);36 Stream &operator<<(const char*);37 Stream &operator>>(int);38 Stream &operator>>(const char*);39};40 41void f(Stream& s, bool b) {42 (void)(s << b ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '<<'}} \43 // expected-note {{place parentheses around the '<<' expression to silence this warning}} \44 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}45 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("46 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"47 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("48 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:32-[[@LINE-6]]:32}:")"49 50 (void)(s << 5 == 1); // expected-warning {{overloaded operator << has higher precedence than comparison operator}} \51 // expected-note {{place parentheses around the '<<' expression to silence this warning}} \52 // expected-note {{place parentheses around comparison expression to evaluate it first}}53 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("54 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"55 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("56 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"57 58 (void)(s >> 5 == 1); // expected-warning {{overloaded operator >> has higher precedence than comparison operator}} \59 // expected-note {{place parentheses around the '>>' expression to silence this warning}} \60 // expected-note {{place parentheses around comparison expression to evaluate it first}}61 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("62 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:16-[[@LINE-4]]:16}:")"63 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("64 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:21-[[@LINE-6]]:21}:")"65}66 67struct S {68 operator int() { return 42; }69 friend S operator+(const S &lhs, bool) { return S(); }70};71 72void test(S *s, bool (S::*m_ptr)()) {73 (void)(*s + true ? "foo" : "bar"); // expected-warning {{operator '?:' has lower precedence than '+'}} \74 // expected-note {{place parentheses around the '+' expression to silence this warning}} \75 // expected-note {{place parentheses around the '?:' expression to evaluate it first}}76 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"("77 // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:19-[[@LINE-4]]:19}:")"78 // CHECK: fix-it:"{{.*}}":{[[@LINE-5]]:15-[[@LINE-5]]:15}:"("79 // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:35-[[@LINE-6]]:35}:")"80 81 (void)((*s + true) ? "foo" : "bar"); // No warning.82 83 // Don't crash on unusual member call expressions.84 (void)((s->*m_ptr)() ? "foo" : "bar");85}86 87void test(int a, int b, int c) {88 (void)(a >> b + c); // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \89 expected-note {{place parentheses around the '+' expression to silence this warning}}90 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("91 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"92 93 (void)(a - b << c); // expected-warning {{operator '<<' has lower precedence than '-'; '-' will be evaluated first}} \94 expected-note {{place parentheses around the '-' expression to silence this warning}}95 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:10-[[@LINE-2]]:10}:"("96 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:15-[[@LINE-3]]:15}:")"97 98 Stream() << b + c;99 Stream() >> b + c; // expected-warning {{operator '>>' has lower precedence than '+'; '+' will be evaluated first}} \100 expected-note {{place parentheses around the '+' expression to silence this warning}}101 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"("102 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:")"103}104 105namespace PR15628 {106 struct BlockInputIter {107 void* operator++(int);108 void* operator--(int);109 };110 111 void test(BlockInputIter i) {112 (void)(i++ ? true : false); // no-warning113 (void)(i-- ? true : false); // no-warning114 }115}116 117namespace PR20735 {118 struct X {119 static int state;120 static int get();121 int get_num();122 int num;123 };124 namespace ns {125 int num = 0;126 int get();127 }128 void test(X x) {129 if (5 & x.get_num() != 0) {}130 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}131 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}132 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}133 // CHECK: place parentheses around the '!=' expression to silence this warning134 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("135 // fix-it:"{{.*}}":{[[@LINE-6]]:29-[[@LINE-6]]:29}:")"136 // CHECK: place parentheses around the & expression to evaluate it first137 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("138 // fix-it:"{{.*}}":{[[@LINE-9]]:24-[[@LINE-9]]:24}:")"139 140 if (5 & x.num != 0) {}141 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}142 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}143 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}144 // CHECK: place parentheses around the '!=' expression to silence this warning145 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("146 // fix-it:"{{.*}}":{[[@LINE-6]]:23-[[@LINE-6]]:23}:")"147 // CHECK: place parentheses around the & expression to evaluate it first148 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("149 // fix-it:"{{.*}}":{[[@LINE-9]]:18-[[@LINE-9]]:18}:")"150 151 if (5 & x.state != 0) {}152 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}153 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}154 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}155 // CHECK: place parentheses around the '!=' expression to silence this warning156 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("157 // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")"158 // CHECK: place parentheses around the & expression to evaluate it first159 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("160 // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")"161 162 if (5 & x.get() != 0) {}163 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}164 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}165 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}166 // CHECK: place parentheses around the '!=' expression to silence this warning167 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("168 // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")"169 // CHECK: place parentheses around the & expression to evaluate it first170 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("171 // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")"172 173 if (5 & X::state != 0) {}174 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}175 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}176 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}177 // CHECK: place parentheses around the '!=' expression to silence this warning178 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("179 // fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"180 // CHECK: place parentheses around the & expression to evaluate it first181 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("182 // fix-it:"{{.*}}":{[[@LINE-9]]:21-[[@LINE-9]]:21}:")"183 184 if (5 & X::get() != 0) {}185 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}186 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}187 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}188 // CHECK: place parentheses around the '!=' expression to silence this warning189 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("190 // fix-it:"{{.*}}":{[[@LINE-6]]:26-[[@LINE-6]]:26}:")"191 // CHECK: place parentheses around the & expression to evaluate it first192 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("193 // fix-it:"{{.*}}":{[[@LINE-9]]:21-[[@LINE-9]]:21}:")"194 195 if (5 & ns::get() != 0) {}196 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}197 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}198 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}199 // CHECK: place parentheses around the '!=' expression to silence this warning200 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("201 // fix-it:"{{.*}}":{[[@LINE-6]]:27-[[@LINE-6]]:27}:")"202 // CHECK: place parentheses around the & expression to evaluate it first203 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("204 // fix-it:"{{.*}}":{[[@LINE-9]]:22-[[@LINE-9]]:22}:")"205 206 if (5 & ns::num != 0) {}207 // expected-warning@-1 {{& has lower precedence than !=; != will be evaluated first}}208 // expected-note@-2 {{place parentheses around the '!=' expression to silence this warning}}209 // expected-note@-3 {{place parentheses around the & expression to evaluate it first}}210 // CHECK: place parentheses around the '!=' expression to silence this warning211 // fix-it:"{{.*}}":{[[@LINE-5]]:13-[[@LINE-5]]:13}:"("212 // fix-it:"{{.*}}":{[[@LINE-6]]:25-[[@LINE-6]]:25}:")"213 // CHECK: place parentheses around the & expression to evaluate it first214 // fix-it:"{{.*}}":{[[@LINE-8]]:9-[[@LINE-8]]:9}:"("215 // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")"216 }217}218 219void consecutive_builtin_compare(int x, int y, int z) {220 (void)(x < y < z); // expected-warning {{chained comparison 'X < Y < Z' does not behave the same as a mathematical expression}}221 (void)(x < y > z); // expected-warning {{chained comparison 'X < Y > Z' does not behave the same as a mathematical expression}}222 (void)(x < y <= z); // expected-warning {{chained comparison 'X < Y <= Z' does not behave the same as a mathematical expression}}223 (void)(x <= y > z); // expected-warning {{chained comparison 'X <= Y > Z' does not behave the same as a mathematical expression}}224 (void)((x < y) < z); // no-warning225 (void)((x < y) >= z); // no-warning226 227 // TODO: Also issue warning for consecutive comparisons that use overloaded comparison operators?228}229