Skip to content
Snippets Groups Projects
Commit 2139b1ed authored by Hans Wennborg's avatar Hans Wennborg
Browse files

Make the ?: precedence warning handle pointers to the left of ?

Previously, Clang would fail to warn on:

  int n = x + foo ? 1 : 2;

when foo is a pointer.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@226870 91177308-0d34-0410-b5e6-96231b3b80d8
parent 8f3dd24e
No related branches found
No related tags found
No related merge requests found
...@@ -6129,6 +6129,8 @@ static bool ExprLooksBoolean(Expr *E) { ...@@ -6129,6 +6129,8 @@ static bool ExprLooksBoolean(Expr *E) {
return IsLogicOp(OP->getOpcode()); return IsLogicOp(OP->getOpcode());
if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E))
return OP->getOpcode() == UO_LNot; return OP->getOpcode() == UO_LNot;
if (E->getType()->isPointerType())
return true;
   
return false; return false;
} }
......
...@@ -80,7 +80,7 @@ void bitwise_rel(unsigned i) { ...@@ -80,7 +80,7 @@ void bitwise_rel(unsigned i) {
_Bool someConditionFunc(); _Bool someConditionFunc();
void conditional_op(int x, int y, _Bool b) { void conditional_op(int x, int y, _Bool b, void* p) {
(void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \ (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} \
// expected-note {{place parentheses around the '+' expression to silence this warning}} \ // expected-note {{place parentheses around the '+' expression to silence this warning}} \
// expected-note {{place parentheses around the '?:' expression to evaluate it first}} // expected-note {{place parentheses around the '?:' expression to evaluate it first}}
...@@ -116,6 +116,14 @@ void conditional_op(int x, int y, _Bool b) { ...@@ -116,6 +116,14 @@ void conditional_op(int x, int y, _Bool b) {
// CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:24-[[@LINE-6]]:24}:")" // CHECK: fix-it:"{{.*}}":{[[@LINE-6]]:24-[[@LINE-6]]:24}:")"
(void)(x % 2 ? 1 : 2); // no warning (void)(x % 2 ? 1 : 2); // no warning
(void)(x + p ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}}
(void)(p + x ? 1 : 2); // no warning
(void)(p + b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}}
(void)(x + y > 0 ? 1 : 2); // no warning
(void)(x + (y > 0) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}}
} }
// RUN: not %clang_cc1 -fsyntax-only -Wparentheses -Werror -fdiagnostics-show-option %s 2>&1 | FileCheck %s -check-prefix=CHECK-FLAG // RUN: not %clang_cc1 -fsyntax-only -Wparentheses -Werror -fdiagnostics-show-option %s 2>&1 | FileCheck %s -check-prefix=CHECK-FLAG
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment