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
This commit is contained in:
Jakub Jelinek 2015-03-10 07:38:57 +01:00 committed by Jakub Jelinek
parent 26d83bccd1
commit 7ccb1a11e5
9 changed files with 93 additions and 3 deletions

View File

@ -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

View File

@ -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");

View File

@ -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

View File

@ -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. */

View File

@ -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

View File

@ -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 ==

View File

@ -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.

View File

@ -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 ();
}

View File

@ -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" } */
}