diff mbox

[C++] Fix -Wunused-but-set-* on RHS of x op= rhs if rhs has side-effects (PR c++/50158)

Message ID 20110823154510.GK2687@tyan-ft48-01.lab.bos.redhat.com
State New
Headers show

Commit Message

Jakub Jelinek Aug. 23, 2011, 3:45 p.m. UTC
Hi!

The recent cp_build_modify_expr change broke the following testcase,
we now complain that b is set but not used.  The problem is that
when rhs has side-effects, stabilize_expr returns a temporary (+ creates
a TARGET_EXPR), which means that mark_exp_read isn't actually called on the
original RHS expression.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk?

2011-08-23  Jakub Jelinek  <jakub@redhat.com>

	PR c++/50158
	* typeck.c (cp_build_modify_expr): Call mark_rvalue_use on rhs
	if it has side-effects and needs to be preevaluated.

	* g++.dg/warn/Wunused-var-16.C: New test.


	Jakub

Comments

Jason Merrill Aug. 23, 2011, 3:48 p.m. UTC | #1
OK.

Jason
diff mbox

Patch

--- gcc/cp/typeck.c.jj	2011-08-18 08:35:38.000000000 +0200
+++ gcc/cp/typeck.c	2011-08-23 14:36:42.000000000 +0200
@@ -6692,6 +6692,8 @@  cp_build_modify_expr (tree lhs, enum tre
 	     side effect associated with any single compound assignment
 	     operator. -- end note ]  */
 	  lhs = stabilize_reference (lhs);
+	  if (TREE_SIDE_EFFECTS (rhs))
+	    rhs = mark_rvalue_use (rhs);
 	  rhs = stabilize_expr (rhs, &init);
 	  newrhs = cp_build_binary_op (input_location,
 				       modifycode, lhs, rhs,
--- gcc/testsuite/g++.dg/warn/Wunused-var-16.C.jj	2011-08-23 14:37:31.000000000 +0200
+++ gcc/testsuite/g++.dg/warn/Wunused-var-16.C	2011-08-23 14:37:19.000000000 +0200
@@ -0,0 +1,13 @@ 
+// PR c++/50158
+// { dg-do compile }
+// { dg-options "-Wunused" }
+
+int bar (int);
+
+int
+foo (int a)
+{
+  int b[] = { a, -a };
+  a += b[bar (a) < a];
+  return a;
+}