From 3d06b6f2fd3258e2d9ab18e034f0bacb06a9d062 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Mon, 29 Aug 2016 18:13:13 +0000 Subject: [PATCH] re PR c/77292 (Spurious warning: logical not is only applied to the left hand side of comparison) PR c/77292 * c-common.c (warn_logical_not_parentheses): Don't warn for a comparison or a logical operator. * c-c++-common/Wlogical-not-parentheses-1.c: New test. From-SVN: r239833 --- gcc/c-family/ChangeLog | 6 +++++ gcc/c-family/c-common.c | 6 +++-- gcc/testsuite/ChangeLog | 5 ++++ .../c-c++-common/Wlogical-not-parentheses-1.c | 26 +++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Wlogical-not-parentheses-1.c diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 07be3bbd5b0..2a38deb1666 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,9 @@ +2016-08-29 Marek Polacek + + PR c/77292 + * c-common.c (warn_logical_not_parentheses): Don't warn for + a comparison or a logical operator. + 2016-08-29 Tom de Vries * c-common.c (build_va_arg): Fix type comparison assert. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 00f19ff24ef..b29334ad9af 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -1481,7 +1481,8 @@ 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, a logical operator, or + a comparison. */ void warn_logical_not_parentheses (location_t location, enum tree_code code, @@ -1489,7 +1490,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 + || truth_value_p (TREE_CODE (rhs))) return; /* Don't warn for !x == 0 or !y != 0, those are equivalent to diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 577df5111b1..3aed281761d 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2016-08-29 Marek Polacek + + PR c/77292 + * c-c++-common/Wlogical-not-parentheses-1.c: New test. + 2016-08-29 Tom de Vries PR c/77398 diff --git a/gcc/testsuite/c-c++-common/Wlogical-not-parentheses-1.c b/gcc/testsuite/c-c++-common/Wlogical-not-parentheses-1.c new file mode 100644 index 00000000000..b6b4e9d37c7 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wlogical-not-parentheses-1.c @@ -0,0 +1,26 @@ +/* PR c/77292 */ +/* { dg-do compile } */ +/* { dg-options "-Wlogical-not-parentheses" } */ + + /* Test that we don't warn if rhs is a comparison or a logical op. */ + +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); + r += !a == (a || b); + r += !a == (a && b); + r += !a == (!b); + + r += !a == (a ^ b); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + r += !a == (a | b); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + r += !a == (a & b); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */ + + return r; +} -- 2.30.2