diff options
author | Jakub Jelinek <jakub@redhat.com> | 2015-03-10 07:38:57 +0100 |
---|---|---|
committer | Jakub Jelinek <jakub@gcc.gnu.org> | 2015-03-10 07:38:57 +0100 |
commit | 7ccb1a11e5a8debe5689d6d149a94175c7fe9ae2 (patch) | |
tree | af3e28b78c0d8a3c890506ce8948602e57219233 /gcc | |
parent | 26d83bccd1ed42a08b5940ba73742537b81efa2b (diff) | |
download | gcc-7ccb1a11e5a8debe5689d6d149a94175c7fe9ae2.zip gcc-7ccb1a11e5a8debe5689d6d149a94175c7fe9ae2.tar.gz gcc-7ccb1a11e5a8debe5689d6d149a94175c7fe9ae2.tar.bz2 |
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
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/c-family/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/c-family/c-common.c | 6 | ||||
-rw-r--r-- | gcc/c/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/c/c-typeck.c | 1 | ||||
-rw-r--r-- | gcc/cp/ChangeLog | 7 | ||||
-rw-r--r-- | gcc/cp/parser.c | 4 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/testsuite/c-c++-common/pr49706.c | 28 | ||||
-rw-r--r-- | gcc/testsuite/c-c++-common/pr65120.c | 32 |
9 files changed, 93 insertions, 3 deletions
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 6bd5a28..3420c96 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,9 @@ +2015-03-10 Jakub Jelinek <jakub@redhat.com> + + PR c/65120 + * c-common.c (warn_logical_not_parentheses): Don't warn for + !x == 0 or !x != 0. + 2015-03-07 Marek Polacek <polacek@redhat.com> PR sanitizer/65280 diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 8c23e09..7e84eb1 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 7b81080..c15b86f 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,9 @@ +2015-03-10 Jakub Jelinek <jakub@redhat.com> + + 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 <jakub@redhat.com> PR c/65120 diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 98bff32..ebe4c73 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 67aa184..d354e07 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2015-03-10 Jakub Jelinek <jakub@redhat.com> + + 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 <jason@redhat.com> PR c++/65339 diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 2a3578f..ad132b1 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 6c9ae2c..8c087ed 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,11 @@ 2015-03-10 Jakub Jelinek <jakub@redhat.com> + 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 f41fa8b..56272ad 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 0000000..c9c1f5f --- /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" } */ +} |