Hi! As mentioned by richi, !x == 0 is actually equivalent to !(x == 0) and x != 0 and !x != 0 is equivalent to !(x != 0) and x == 0, so this patch adjusts the warning not to warn in that case, as adding parens doesn't change anything.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2015-03-09 Jakub Jelinek <ja...@redhat.com> PR c/65120 * c-common.c (warn_logical_not_parentheses): Don't warn for !x == 0 or !x != 0. * c-typeck.c (parser_build_binary_op): Check for tcc_comparison before preparing arguments to warn_logical_not_parentheses. * parser.c (cp_parser_binary_expression): Check for tcc_comparison before preparing arguments to warn_logical_not_parentheses. Use maybe_constant_value on rhs. * c-c++-common/pr49706.c (fn2): Don't expect warning if enumerator on rhs is 0. (fn4): New test. * c-c++-common/pr65120.c: New test. --- gcc/c-family/c-common.c.jj 2015-02-25 07:40:46.000000000 +0100 +++ gcc/c-family/c-common.c 2015-03-09 19:45:27.861896547 +0100 @@ -1800,6 +1800,12 @@ warn_logical_not_parentheses (location_t || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE) return; + /* Don't warn for !x == 0 or !y != 0, those are equivalent to + !(x == 0) or !(y != 0). */ + if ((code == EQ_EXPR || code == NE_EXPR) + && integer_zerop (rhs)) + return; + warning_at (location, OPT_Wlogical_not_parentheses, "logical not is only applied to the left hand side of " "comparison"); --- gcc/c/c-typeck.c.jj 2015-03-09 19:26:00.000000000 +0100 +++ gcc/c/c-typeck.c 2015-03-09 20:03:42.181300577 +0100 @@ -3459,6 +3459,7 @@ parser_build_binary_op (location_t locat code1, arg1.value, code2, arg2.value); if (warn_logical_not_paren + && TREE_CODE_CLASS (code) == tcc_comparison && code1 == TRUTH_NOT_EXPR && code2 != TRUTH_NOT_EXPR /* Avoid warning for !!x == y. */ --- gcc/cp/parser.c.jj 2015-03-09 19:26:00.000000000 +0100 +++ gcc/cp/parser.c 2015-03-09 20:03:10.120815774 +0100 @@ -8270,6 +8270,7 @@ cp_parser_binary_expression (cp_parser* c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node; if (warn_logical_not_paren + && TREE_CODE_CLASS (current.tree_type) == tcc_comparison && current.lhs_type == TRUTH_NOT_EXPR /* Avoid warning for !!x == y. */ && (TREE_CODE (current.lhs) != NE_EXPR @@ -8284,7 +8285,8 @@ cp_parser_binary_expression (cp_parser* && (!DECL_P (current.lhs) || TREE_TYPE (current.lhs) == NULL_TREE || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE)) - warn_logical_not_parentheses (current.loc, current.tree_type, rhs); + warn_logical_not_parentheses (current.loc, current.tree_type, + maybe_constant_value (rhs)); overload = NULL; /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type == --- gcc/testsuite/c-c++-common/pr49706.c.jj 2015-03-09 19:26:00.000000000 +0100 +++ gcc/testsuite/c-c++-common/pr49706.c 2015-03-09 19:52:31.240086911 +0100 @@ -107,9 +107,9 @@ fn2 (enum E e) b = foo_e () == A; b = foo_e () == foo_e (); - b = !e == A; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + b = !e == A; b = !e == foo_e (); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ - b = !foo_e () == A; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + b = !foo_e () == A; b = !foo_e () == foo_e (); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ b = !(e == A); @@ -163,3 +163,27 @@ fn3 (int i1, float f2) b = !!i1 <= f2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */ b = !!i1 >= f2; /* { dg-bogus "logical not is only applied to the left hand side of comparison" } */ } + +void +fn4 (enum E e) +{ + b = e == A; + b = e == foo_e (); + b = foo_e () == B; + b = foo_e () == foo_e (); + + b = !e == B; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + b = !e == foo_e (); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + b = !foo_e () == B; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + b = !foo_e () == foo_e (); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + + b = !(e == B); + b = !(e == foo_e ()); + b = !(foo_e () == B); + b = !(foo_e () == foo_e ()); + + b = (!e) == B; + b = (!e) == foo_e (); + b = (!foo_e ()) == B; + b = (!foo_e ()) == foo_e (); +} --- gcc/testsuite/c-c++-common/pr65120.c.jj 2015-03-09 20:09:01.368171390 +0100 +++ gcc/testsuite/c-c++-common/pr65120.c 2015-03-09 20:17:09.994324151 +0100 @@ -0,0 +1,32 @@ +/* { dg-do compile } */ +/* { dg-options "-Wlogical-not-parentheses" } */ + +/* Test that we don't warn if rhs is 0 and comparison is == or !=. */ + +#ifndef __cplusplus +#define bool _Bool +#endif + +bool r; + +int +f1 (int a) +{ + r = !a == 0; + r = !a != 0; + r = !a == 1; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + r = !a != 1; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ +} + +int +f2 (int a) +{ + r = !a > 0; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + r = !a >= 0; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + r = !a < 0; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + r = !a <= 0; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + r = !a > 1; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + r = !a >= 1; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + r = !a < 1; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + r = !a <= 1; /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ +} Jakub