diff mbox

[C/C++] Fix a -Waddress regression (PR c/69768)

Message ID 20160211162526.GD3017@tucnak.redhat.com
State New
Headers show

Commit Message

Jakub Jelinek Feb. 11, 2016, 4:25 p.m. UTC
Hi!

Until recently, integer_zerop would STRIP_NOPS, so that change regressed
some cases in the -Waddress warning that affect some real-world code.
The following patch re-adds stripping of nops for that case (but doesn't try
to fold it further).
With this patch, for C and C++98 we get the same behavior as in 5.x,
for C++11 and C++14 we warn even about the weirdo cases where (10 - 10)
or (&e - &e) is cast to a pointer (but, e.g. for C we have been and are
warning about the latter only).  I'd say warning for such weird cases is
fine.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2016-02-11  Jakub Jelinek  <jakub@redhat.com>

	PR c/69768
	* c-typeck.c (parser_build_binary_op): Strip nops from integer_zerop
	arguments for -Waddress warning.

	* typeck.c (cp_build_binary_op): Strip nops from integer_zerop
	arguments for -Waddress warning.  Fix up formatting.

	* c-c++-common/Waddress-1.c: New test.


	Jakub

Comments

Jason Merrill Feb. 11, 2016, 6:56 p.m. UTC | #1
On 02/11/2016 11:25 AM, Jakub Jelinek wrote:
> +	       && !integer_zerop (tree_strip_nop_conversions (op1)))

Maybe cp_fold rather than tree_strip_nop_conversions?

Jason
diff mbox

Patch

--- gcc/c/c-typeck.c.jj	2016-01-27 20:30:18.000000000 +0100
+++ gcc/c/c-typeck.c	2016-02-11 17:17:07.470222811 +0100
@@ -3597,8 +3597,10 @@  parser_build_binary_op (location_t locat
      of testing for equality or inequality of a string literal with NULL.  */
   if (code == EQ_EXPR || code == NE_EXPR)
     {
-      if ((code1 == STRING_CST && !integer_zerop (arg2.value))
-	  || (code2 == STRING_CST && !integer_zerop (arg1.value)))
+      if ((code1 == STRING_CST
+	   && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
+	  || (code2 == STRING_CST
+	      && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
 	warning_at (location, OPT_Waddress,
 		    "comparison with string literal results in unspecified behavior");
     }
--- gcc/cp/typeck.c.jj	2016-02-11 10:50:58.000000000 +0100
+++ gcc/cp/typeck.c	2016-02-11 17:17:07.473222771 +0100
@@ -4487,9 +4487,12 @@  cp_build_binary_op (location_t location,
 	warning (OPT_Wfloat_equal,
 		 "comparing floating point with == or != is unsafe");
       if ((complain & tf_warning)
-	  && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
-	      || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
-	warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
+	  && ((TREE_CODE (orig_op0) == STRING_CST
+	       && !integer_zerop (tree_strip_nop_conversions (op1)))
+	      || (TREE_CODE (orig_op1) == STRING_CST
+		  && !integer_zerop (tree_strip_nop_conversions (op0)))))
+	warning (OPT_Waddress, "comparison with string literal results "
+			       "in unspecified behaviour");
 
       build_type = boolean_type_node;
       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
--- gcc/testsuite/c-c++-common/Waddress-1.c.jj	2016-02-11 14:30:30.677090429 +0100
+++ gcc/testsuite/c-c++-common/Waddress-1.c	2016-02-11 17:18:13.970329341 +0100
@@ -0,0 +1,15 @@ 
+/* PR c/69768 */
+/* { dg-do compile } */
+/* { dg-options "-Waddress" } */
+
+static int e;
+
+int
+foo ()
+{
+  return "foo1" != (void *) 0	/* { dg-bogus "comparison with string literal results in unspecified behaviou?r" } */
+	 && "foo2" != (const char *) ((void *) 0)	/* { dg-bogus "comparison with string literal results in unspecified behaviou?r" } */
+	 && "foo3" != (const char *) ((void *) (10 - 10))	/* { dg-warning "comparison with string literal results in unspecified behaviou?r" "" { target c++11 } } */
+	 && "foo4" != (const char *) ((void *) (&e - &e))	/* { dg-warning "comparison with string literal results in unspecified behaviou?r" "" { target { c || c++11 } } } */
+	 && "foo5" != "foo6";	/* { dg-warning "comparison with string literal results in unspecified behaviou?r" } */
+}