This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[C++ PATCH] Fix -Wunused-but-set-* with fields with reference type (PR c++/44444)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Jason Merrill <jason at redhat dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Mon, 7 Jun 2010 17:39:37 +0200
- Subject: [C++ PATCH] Fix -Wunused-but-set-* with fields with reference type (PR c++/44444)
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
Hi!
convert_from_reference creates an INDIRECT_REF, but mark_exp_read
doesn't see through it, so we never mark z in the testcase as read.
Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux.
Ok for trunk?
Alternatively, convert_from_reference perhaps could mark_exp_read instead.
2010-06-07 Jakub Jelinek <jakub@redhat.com>
PR c++/44444
* expr.c (mark_exp_read): Handle INDIRECT_REF.
* cvt.c (convert_to_void): Handle INDIRECT_REF like
handled_component_p.
* g++.dg/warn/Wunused-var-12.C: New test.
--- gcc/cp/expr.c.jj 2010-05-28 14:35:53.000000000 +0200
+++ gcc/cp/expr.c 2010-06-07 14:35:59.000000000 +0200
@@ -1,7 +1,7 @@
/* Convert language-specific tree expression to rtl instructions,
for GNU compiler.
Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
- 2000, 2001, 2002, 2003, 2004, 2007 Free Software Foundation, Inc.
+ 2000, 2001, 2002, 2003, 2004, 2007, 2010 Free Software Foundation, Inc.
This file is part of GCC.
@@ -132,6 +132,7 @@ mark_exp_read (tree exp)
case IMAGPART_EXPR:
CASE_CONVERT:
case ADDR_EXPR:
+ case INDIRECT_REF:
mark_exp_read (TREE_OPERAND (exp, 0));
break;
case COMPOUND_EXPR:
--- gcc/cp/cvt.c.jj 2010-06-07 11:25:02.000000000 +0200
+++ gcc/cp/cvt.c 2010-06-07 14:40:33.000000000 +0200
@@ -834,7 +834,9 @@ convert_to_void (tree expr, const char *
while (TREE_CODE (exprv) == COMPOUND_EXPR)
exprv = TREE_OPERAND (exprv, 1);
- if (DECL_P (exprv) || handled_component_p (exprv))
+ if (DECL_P (exprv)
+ || handled_component_p (exprv)
+ || TREE_CODE (exprv) == INDIRECT_REF)
/* Expr is not being 'used' here, otherwise we whould have
called mark_{rl}value_use use here, which would have in turn
called mark_exp_read. Rather, we call mark_exp_read directly
--- gcc/testsuite/g++.dg/warn/Wunused-var-12.C.jj 2010-06-07 14:49:03.000000000 +0200
+++ gcc/testsuite/g++.dg/warn/Wunused-var-12.C 2010-06-07 14:48:15.000000000 +0200
@@ -0,0 +1,36 @@
+// PR c++/44444
+// { dg-do compile }
+// { dg-options "-Wunused" }
+
+struct S
+{
+ const int &u;
+ const int &v;
+ S (const int &a, const int &b) : u(a), v(b) { }
+};
+
+bool
+f1 ()
+{
+ bool t = false;
+ S z = S (1, 2);
+ t |= z.u == 1;
+ t |= z.v == 2;
+ return t;
+}
+
+void
+f2 ()
+{
+ S z = S (1, 2);
+ z.u; // { dg-warning "no effect" }
+}
+
+int i;
+
+void
+f3 ()
+{
+ S z = S (1, 2);
+ i++, z.u; // { dg-warning "no effect" }
+}
Jakub