From 7ccb1a11e5a8debe5689d6d149a94175c7fe9ae2 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Tue, 10 Mar 2015 07:38:57 +0100 Subject: [PATCH] re PR c/65120 (Wlogical-not-parentheses should not warn about double exclamation !!) 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. From-SVN: r221299 --- gcc/c-family/ChangeLog | 6 ++++++ gcc/c-family/c-common.c | 6 ++++++ gcc/c/ChangeLog | 6 ++++++ gcc/c/c-typeck.c | 1 + gcc/cp/ChangeLog | 7 ++++++ gcc/cp/parser.c | 4 +++- gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/c-c++-common/pr49706.c | 28 ++++++++++++++++++++++-- gcc/testsuite/c-c++-common/pr65120.c | 32 ++++++++++++++++++++++++++++ 9 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/pr65120.c diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 6bd5a28fa8b..3420c96aff1 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,9 @@ +2015-03-10 Jakub Jelinek + + PR c/65120 + * c-common.c (warn_logical_not_parentheses): Don't warn for + !x == 0 or !x != 0. + 2015-03-07 Marek Polacek PR sanitizer/65280 diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 8c23e091962..7e84eb194b2 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -1800,6 +1800,12 @@ warn_logical_not_parentheses (location_t location, enum tree_code code, || 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"); diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index 7b81080a34f..c15b86f5540 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,9 @@ +2015-03-10 Jakub Jelinek + + PR c/65120 + * c-typeck.c (parser_build_binary_op): Check for tcc_comparison + before preparing arguments to warn_logical_not_parentheses. + 2015-03-09 Jakub Jelinek PR c/65120 diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 98bff32f17e..ebe4c735891 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -3459,6 +3459,7 @@ parser_build_binary_op (location_t location, enum tree_code code, 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. */ diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 67aa184bcb1..d354e07d279 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2015-03-10 Jakub Jelinek + + PR c/65120 + * parser.c (cp_parser_binary_expression): Check for tcc_comparison + before preparing arguments to warn_logical_not_parentheses. + Use maybe_constant_value on rhs. + 2015-03-09 Jason Merrill PR c++/65339 diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 2a3578fee3c..ad132b1850f 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -8270,6 +8270,7 @@ cp_parser_binary_expression (cp_parser* parser, bool cast_p, 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* parser, bool cast_p, && (!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 == diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6c9ae2ca96e..8c087eda1f6 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,11 @@ 2015-03-10 Jakub Jelinek + PR c/65120 + * 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. + PR rtl-optimization/65321 * gcc.dg/pr65321.c: New test. diff --git a/gcc/testsuite/c-c++-common/pr49706.c b/gcc/testsuite/c-c++-common/pr49706.c index f41fa8bb5ff..56272add7da 100644 --- a/gcc/testsuite/c-c++-common/pr49706.c +++ b/gcc/testsuite/c-c++-common/pr49706.c @@ -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 (); +} diff --git a/gcc/testsuite/c-c++-common/pr65120.c b/gcc/testsuite/c-c++-common/pr65120.c new file mode 100644 index 00000000000..c9c1f5f7e65 --- /dev/null +++ b/gcc/testsuite/c-c++-common/pr65120.c @@ -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" } */ +} -- 2.30.2