aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2016-09-05 12:17:09 +0000
committerMarek Polacek <mpolacek@gcc.gnu.org>2016-09-05 12:17:09 +0000
commit25ff5dd35471b2b20f2b2681c8ff24c0d086ec00 (patch)
treecbe9638e9cb6159d1a15ccd556a01d2ef8a445c8 /gcc
parentc5cb79681df13acc02894b4df8314c797895e2bb (diff)
downloadgcc-25ff5dd35471b2b20f2b2681c8ff24c0d086ec00.zip
gcc-25ff5dd35471b2b20f2b2681c8ff24c0d086ec00.tar.gz
gcc-25ff5dd35471b2b20f2b2681c8ff24c0d086ec00.tar.bz2
re PR c/77423 (-Wlogical-not-parentheses false positive for bitwise expression with _Bool operands)
PR c/77423 * doc/invoke.texi: Update -Wlogical-not-parentheses documentation. * c-common.c (bool_promoted_to_int_p): New function. (expr_has_boolean_operands_p): New function. (warn_logical_not_parentheses): Return if expr_has_boolean_operands_p. (maybe_warn_bool_compare): Use bool_promoted_to_int_p. * c-c++-common/Wlogical-not-parentheses-3.c: New test. From-SVN: r239988
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog5
-rw-r--r--gcc/c-family/ChangeLog8
-rw-r--r--gcc/c-family/c-common.c38
-rw-r--r--gcc/doc/invoke.texi4
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/c-c++-common/Wlogical-not-parentheses-3.c31
6 files changed, 86 insertions, 5 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 2eb8986..00a552d 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@
+2016-09-05 Marek Polacek <polacek@redhat.com>
+
+ PR c/77423
+ * doc/invoke.texi: Update -Wlogical-not-parentheses documentation.
+
2016-09-05 Jakub Jelinek <jakub@redhat.com>
PR other/77421
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog
index 7a64140..7d60445 100644
--- a/gcc/c-family/ChangeLog
+++ b/gcc/c-family/ChangeLog
@@ -1,3 +1,11 @@
+2016-09-05 Marek Polacek <polacek@redhat.com>
+
+ PR c/77423
+ * c-common.c (bool_promoted_to_int_p): New function.
+ (expr_has_boolean_operands_p): New function.
+ (warn_logical_not_parentheses): Return if expr_has_boolean_operands_p.
+ (maybe_warn_bool_compare): Use bool_promoted_to_int_p.
+
2016-09-04 Tom de Vries <tom@codesourcery.com>
revert:
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 1a834a3..63b1e28 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -1479,6 +1479,36 @@ warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
}
}
+/* Return true iff T is a boolean promoted to int. */
+
+static bool
+bool_promoted_to_int_p (tree t)
+{
+ return (CONVERT_EXPR_P (t)
+ && TREE_TYPE (t) == integer_type_node
+ && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
+}
+
+/* Return true iff EXPR only contains boolean operands, or comparisons. */
+
+static bool
+expr_has_boolean_operands_p (tree expr)
+{
+ STRIP_NOPS (expr);
+
+ if (CONVERT_EXPR_P (expr))
+ return bool_promoted_to_int_p (expr);
+ else if (UNARY_CLASS_P (expr))
+ return expr_has_boolean_operands_p (TREE_OPERAND (expr, 0));
+ else if (BINARY_CLASS_P (expr))
+ return (expr_has_boolean_operands_p (TREE_OPERAND (expr, 0))
+ && expr_has_boolean_operands_p (TREE_OPERAND (expr, 1)));
+ else if (COMPARISON_CLASS_P (expr))
+ return true;
+ else
+ return false;
+}
+
/* 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, a logical operator, or
@@ -1494,6 +1524,10 @@ warn_logical_not_parentheses (location_t location, enum tree_code code,
|| truth_value_p (TREE_CODE (rhs)))
return;
+ /* Don't warn for expression like !x == ~(bool1 | bool2). */
+ if (expr_has_boolean_operands_p (rhs))
+ 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)
@@ -12415,9 +12449,7 @@ maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
don't want to warn here. */
tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
/* Handle booleans promoted to integers. */
- if (CONVERT_EXPR_P (noncst)
- && TREE_TYPE (noncst) == integer_type_node
- && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
+ if (bool_promoted_to_int_p (noncst))
/* Warn. */;
else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
&& !truth_value_p (TREE_CODE (noncst)))
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 73aab7c..9ab031e 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -5476,8 +5476,8 @@ if (a < 0 && a < 0) @{ @dots{} @}
@opindex Wlogical-not-parentheses
@opindex Wno-logical-not-parentheses
Warn about logical not used on the left hand side operand of a comparison.
-This option does not warn if the RHS operand is of a boolean type. Its
-purpose is to detect suspicious code like the following:
+This option does not warn if the right operand is considered to be a Boolean
+expression. Its purpose is to detect suspicious code like the following:
@smallexample
int a;
@dots{}
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 79ef275..e54fc2f 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2016-09-05 Marek Polacek <polacek@redhat.com>
+
+ PR c/77423
+ * c-c++-common/Wlogical-not-parentheses-3.c: New test.
+
2016-09-04 Steven G. Kargl <kargl@gcc.gnu.org>
PR fortran/77391
diff --git a/gcc/testsuite/c-c++-common/Wlogical-not-parentheses-3.c b/gcc/testsuite/c-c++-common/Wlogical-not-parentheses-3.c
new file mode 100644
index 0000000..00aa747
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wlogical-not-parentheses-3.c
@@ -0,0 +1,31 @@
+/* PR c/77423 */
+/* { dg-do compile } */
+/* { dg-options "-Wlogical-not-parentheses" } */
+
+#ifndef __cplusplus
+# define bool _Bool
+#endif
+
+int
+f (int a, bool b, bool c)
+{
+ int r = 0;
+
+ r += !a == (b | c);
+ r += !a == (b ^ c);
+ r += !a == (b & c);
+ r += !a == ~b;
+ r += !a == ~(int) b;
+ r += !a == ((b & c) | c);
+ r += !a == ((b & c) | (b ^ c));
+ r += !a == (int) (b ^ c);
+ r += !a == (int) ~b;
+ r += !a == ~~b;
+ r += !a == ~(b | c);
+ r += !a == ~(b | (a == 1));
+ r += !a == ~(a == 1);
+
+ r += !a == ((b & c) | (b ^ a)); /* { dg-warning "logical not is only applied to the left hand side of comparison" } */
+
+ return r;
+}