]> granicus.if.org Git - clang/commitdiff
Silence ?: precendence warning when parenthesis are present.
authorHans Wennborg <hans@hanshq.net>
Mon, 12 Sep 2011 12:07:30 +0000 (12:07 +0000)
committerHans Wennborg <hans@hanshq.net>
Mon, 12 Sep 2011 12:07:30 +0000 (12:07 +0000)
Fixes PR10898. The warning should be silent when there are parenthesis
around the condition expression.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@139492 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExpr.cpp
test/Sema/parentheses.c
test/Sema/parentheses.cpp

index 1828841b30f4cefb0c0b3b8434e4b87b8821f3e1..99c881fe5ce53042851310fd9299b56ec15c71b4 100644 (file)
@@ -4926,9 +4926,10 @@ static bool IsArithmeticOp(BinaryOperatorKind Opc) {
 /// expression.
 static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode,
                                    Expr **RHSExprs) {
-  E = E->IgnoreParenImpCasts();
+  // Don't strip parenthesis: we should not warn if E is in parenthesis.
+  E = E->IgnoreImpCasts();
   E = E->IgnoreConversionOperator();
-  E = E->IgnoreParenImpCasts();
+  E = E->IgnoreImpCasts();
 
   // Built-in binary operator.
   if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) {
index 13ea3ecbe16a27e6cc95c12fa99380882de920d9..9751336018b7b184a15ade20f9ef405f9feb6978 100644 (file)
@@ -52,6 +52,8 @@ void conditional_op(int x, int y, _Bool b) {
                                            // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
                                            // expected-note {{place parentheses around the '+' expression to silence this warning}}
 
+  (void)((x + someConditionFunc()) ? 1 : 2); // no warning
+
   (void)(x - b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '-'}} \
                          // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
                          // expected-note {{place parentheses around the '-' expression to silence this warning}}
@@ -64,7 +66,6 @@ void conditional_op(int x, int y, _Bool b) {
                           // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
                           // expected-note {{place parentheses around the '/' expression to silence this warning}}
 
-
   (void)(x % 2 ? 1 : 2); // no warning
 }
 
index 252455dcad491031e33ab1260aceeed3c47a0ea9..767416677e00f4d5d4c2fd5315f569ddf361031e 100644 (file)
@@ -40,6 +40,8 @@ void test(S *s, bool (S::*m_ptr)()) {
                                      // expected-note {{place parentheses around the '?:' expression to evaluate it first}} \
                                      // expected-note {{place parentheses around the '+' expression to silence this warning}}
 
+  (void)((*s + true) ? "foo" : "bar"); // No warning.
+
   // Don't crash on unusual member call expressions.
   (void)((s->*m_ptr)() ? "foo" : "bar");
 }