Message ID | 20160824174314.GJ11131@redhat.com |
---|---|
State | New |
Headers | show |
On Wed, Aug 24, 2016 at 7:43 PM, Marek Polacek <polacek@redhat.com> wrote: > The -Wlogical-not-parentheses deliberately doesn't warn when the RHS has > boolean type. But since in C the type of a comparison is int, we need > to check for tcc_comparison, too. > > Bootstrapped/regtested on x86_64-linux and ppc64le-redhat-linux, ok for trunk? What about using truth_value_p ()? That also includes && and || (just in case those do not have boolean type). Richard. > 2016-08-24 Marek Polacek <polacek@redhat.com> > > PR c/77292 > * c-common.c (warn_logical_not_parentheses): Don't warn if RHS is > a comparison. > > * c-c++-common/Wlogical-not-parentheses-1.c: New test. > > diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c > index 3feb910..47255c3 100644 > --- gcc/c-family/c-common.c > +++ gcc/c-family/c-common.c > @@ -1481,7 +1481,7 @@ warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs) > > /* Warn about logical not used on the left hand side operand of a comparison. > This function assumes that the LHS is inside of TRUTH_NOT_EXPR. > - Do not warn if RHS is of a boolean type. */ > + Do not warn if RHS is of a boolean type, or a comparison. */ > > void > warn_logical_not_parentheses (location_t location, enum tree_code code, > @@ -1489,7 +1489,8 @@ warn_logical_not_parentheses (location_t location, enum tree_code code, > { > if (TREE_CODE_CLASS (code) != tcc_comparison > || TREE_TYPE (rhs) == NULL_TREE > - || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE) > + || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE > + || TREE_CODE_CLASS (TREE_CODE (rhs)) == tcc_comparison) > return; > > /* Don't warn for !x == 0 or !y != 0, those are equivalent to > diff --git gcc/testsuite/c-c++-common/Wlogical-not-parentheses-1.c gcc/testsuite/c-c++-common/Wlogical-not-parentheses-1.c > index e69de29..4ae9ec2 100644 > --- gcc/testsuite/c-c++-common/Wlogical-not-parentheses-1.c > +++ gcc/testsuite/c-c++-common/Wlogical-not-parentheses-1.c > @@ -0,0 +1,18 @@ > +/* PR c/77292 */ > +/* { dg-do compile } */ > +/* { dg-options "-Wlogical-not-parentheses" } */ > + > + /* Test that we don't warn if rhs is a comparison. */ > + > +int > +foo (int a, int b) > +{ > + int r = 0; > + r += !a == (a < b); > + r += !a == (a > b); > + r += !a == (a >= b); > + r += !a == (a <= b); > + r += !a == (a != b); > + r += !a == (a == b); > + return r; > +} > > Marek
diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c index 3feb910..47255c3 100644 --- gcc/c-family/c-common.c +++ gcc/c-family/c-common.c @@ -1481,7 +1481,7 @@ warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs) /* Warn about logical not used on the left hand side operand of a comparison. This function assumes that the LHS is inside of TRUTH_NOT_EXPR. - Do not warn if RHS is of a boolean type. */ + Do not warn if RHS is of a boolean type, or a comparison. */ void warn_logical_not_parentheses (location_t location, enum tree_code code, @@ -1489,7 +1489,8 @@ warn_logical_not_parentheses (location_t location, enum tree_code code, { if (TREE_CODE_CLASS (code) != tcc_comparison || TREE_TYPE (rhs) == NULL_TREE - || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE) + || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE + || TREE_CODE_CLASS (TREE_CODE (rhs)) == tcc_comparison) return; /* Don't warn for !x == 0 or !y != 0, those are equivalent to diff --git gcc/testsuite/c-c++-common/Wlogical-not-parentheses-1.c gcc/testsuite/c-c++-common/Wlogical-not-parentheses-1.c index e69de29..4ae9ec2 100644 --- gcc/testsuite/c-c++-common/Wlogical-not-parentheses-1.c +++ gcc/testsuite/c-c++-common/Wlogical-not-parentheses-1.c @@ -0,0 +1,18 @@ +/* PR c/77292 */ +/* { dg-do compile } */ +/* { dg-options "-Wlogical-not-parentheses" } */ + + /* Test that we don't warn if rhs is a comparison. */ + +int +foo (int a, int b) +{ + int r = 0; + r += !a == (a < b); + r += !a == (a > b); + r += !a == (a >= b); + r += !a == (a <= b); + r += !a == (a != b); + r += !a == (a == b); + return r; +}