This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C/C++ PATCH] -Wunused-but-set-variable fixes (PR c++/44361)


Hi!

The C++ hunk fixes a problem Richard reported in --enable-build-with-cxx
- explicit (void) cast of an assignment expression didn't make LHS of it
marked as read.

The C hunks is just something I've discovered while writing the testcase,
statement expressions with an assignment or compound expression in its last
statement weren't properly marked.

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

2010-06-01  Jakub Jelinek  <jakub@redhat.com>

	PR c++/44361
	* c-typeck.c (mark_exp_read): Handle C_MAYBE_CONST_EXPR.
	* c-parser.c (c_parser_postfix_expression): Call mark_exp_read on
	statement expression.

	* cvt.c (convert_to_void): If implicit is NULL, call mark_rvalue_use
	instead of calling mark_exp_read only when not an assignment.

	* c-c++-common/Wunused-var-9.c: New test.

--- gcc/c-parser.c.jj	2010-05-28 14:36:05.000000000 +0200
+++ gcc/c-parser.c	2010-06-01 16:55:51.000000000 +0200
@@ -5601,6 +5601,7 @@ c_parser_postfix_expression (c_parser *p
 	  pedwarn (loc, OPT_pedantic,
 		   "ISO C forbids braced-groups within expressions");
 	  expr.value = c_finish_stmt_expr (brace_loc, stmt);
+	  mark_exp_read (expr.value);
 	}
       else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
 	{
--- gcc/c-typeck.c.jj	2010-05-28 14:36:04.000000000 +0200
+++ gcc/c-typeck.c	2010-06-01 16:59:37.000000000 +0200
@@ -1841,6 +1841,7 @@ mark_exp_read (tree exp)
       mark_exp_read (TREE_OPERAND (exp, 0));
       break;
     case COMPOUND_EXPR:
+    case C_MAYBE_CONST_EXPR:
       mark_exp_read (TREE_OPERAND (exp, 1));
       break;
     default:
--- gcc/cp/cvt.c.jj	2010-05-13 12:21:20.000000000 +0200
+++ gcc/cp/cvt.c	2010-06-01 13:05:10.000000000 +0200
@@ -822,22 +822,26 @@ ocp_convert (tree type, tree expr, int c
 tree
 convert_to_void (tree expr, const char *implicit, tsubst_flags_t complain)
 {
-  tree exprv;
-
   if (expr == error_mark_node
       || TREE_TYPE (expr) == error_mark_node)
     return error_mark_node;
 
-  exprv = expr;
-  while (TREE_CODE (exprv) == COMPOUND_EXPR)
-    exprv = TREE_OPERAND (exprv, 1);
-  if (DECL_P (exprv) || handled_component_p (exprv))
-    /* 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
-       to avoid some warnings when
-       -Wunused-but-set-{variable,parameter} is in effect.  */
-    mark_exp_read (exprv);
+  if (implicit == NULL)
+    mark_rvalue_use (expr);
+  else
+    {
+      tree exprv = expr;
+
+      while (TREE_CODE (exprv) == COMPOUND_EXPR)
+	exprv = TREE_OPERAND (exprv, 1);
+      if (DECL_P (exprv) || handled_component_p (exprv))
+	/* 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
+	   to avoid some warnings when
+	   -Wunused-but-set-{variable,parameter} is in effect.  */
+	mark_exp_read (exprv);
+    }
 
   if (!TREE_TYPE (expr))
     return expr;
--- gcc/testsuite/c-c++-common/Wunused-var-9.c.jj	2010-06-01 13:00:13.000000000 +0200
+++ gcc/testsuite/c-c++-common/Wunused-var-9.c	2010-06-01 17:01:18.000000000 +0200
@@ -0,0 +1,80 @@
+/* { dg-options "-Wunused" } */
+/* { dg-do compile } */
+
+int f1 (int *, int);
+int f2 (int *);
+int f3 (int *);
+
+int
+f4 (int x)
+{
+  int a, n = 0;
+  int b;
+  for (a = f1 (&b, x); f2 (&b); (void) (a = f3 (&b)))
+    n++;
+  return n;
+}
+
+void
+f5 (int x)
+{
+  int a;
+  a = x;
+  (void) (a = x);
+}
+
+void
+f6 (int x)
+{
+  int a;	/* { dg-warning "set but not used" } */
+  a = x;
+}
+
+void
+f7 (int x)
+{
+  int a;
+  ({ a = x; });
+}
+
+int
+f8 (int x)
+{
+  int a;
+  int b = ({ a = x; });
+  return b;
+}
+
+int v;
+
+void
+f9 (int x)
+{
+  int a;
+  ({ v++, a = x; });
+}
+
+int
+f10 (int x)
+{
+  int a;
+  int b = ({ v++, a = x; });
+  return b;
+}
+
+void
+f11 (int x)
+{
+  int a;
+  a = x;
+  ({ v++, a; });
+}
+
+int
+f12 (int x)
+{
+  int a;
+  a = x;
+  int b = ({ v++, a; });
+  return b;
+}

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]