Commit 577cd070 by Marek Polacek Committed by Marek Polacek

c-common.c (maybe_warn_bool_compare): When comparing with 0/1, require that the…

c-common.c (maybe_warn_bool_compare): When comparing with 0/1, require that the non-constant be of a boolean type.

	* c-common.c (maybe_warn_bool_compare): When comparing with 0/1,
	require that the non-constant be of a boolean type.

	* c-c++-common/Wbool-compare-3.c: New test.

From-SVN: r222622
parent bb83a43d
2015-04-30 Marek Polacek <polacek@redhat.com>
* c-common.c (maybe_warn_bool_compare): When comparing with 0/1,
require that the non-constant be of a boolean type.
2015-04-29 Josh Triplett <josh@joshtriplett.org>
* c-common.c (handle_section_attribute): Refactor to reduce
......
......@@ -11924,6 +11924,17 @@ maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
}
else if (integer_zerop (cst) || integer_onep (cst))
{
/* If the non-constant operand isn't of a boolean type, we
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)
/* Warn. */;
else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
&& !truth_value_p (TREE_CODE (noncst)))
return;
/* Do some magic to get the right diagnostics. */
bool flag = TREE_CODE (op0) == INTEGER_CST;
flag = integer_zerop (cst) ? flag : !flag;
......
2015-04-30 Marek Polacek <polacek@redhat.com>
* c-c++-common/Wbool-compare-3.c: New test.
2015-04-30 Paolo Carlini <paolo.carlini@oracle.com>
PR c++/57610
......
/* { dg-do compile } */
/* { dg-options "-Wbool-compare" } */
#ifndef __cplusplus
# define bool _Bool
#endif
#define A 0
#define B 1
int
foo (int i, bool b)
{
int r = 0;
r += i <= (A || B);
r += i <= b;
r += i <= A;
r += i < (A || B);
r += i < b;
r += i < A;
r += i > (A || B);
r += i > b;
r += i > A;
r += i >= (A || B);
r += i >= b;
r += i >= A;
return r;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment