PATCH RFA: C++: Omit incorrect "no effect" warnings

Ian Lance Taylor iant@google.com
Wed Jun 17 17:45:00 GMT 2009


In the C++ frontend, code like this:

  b ? f1(), 0 : 0;

gets a warning "right-hand operand of comma has no effect".  As
described in

http://gcc.gnu.org/ml/gcc-patches/2009-03/msg00217.html

this warning triggers on some generally reasonable C preprocessor
macros.  Although the warning is technically correct, in this context it
is not useful.  I think the right way to handle this is to avoid warning
about a ?: construct if either side has an effect.  This patch does
that.

Bootstrapped and tested on x86_64-unknown-linux-gnu.  OK for mainline?

Ian


gcc/cp/ChangeLog:

2009-06-17  Ian Lance Taylor  <iant@google.com>

	* cvt.c (convert_to_void): Only warn about COND_EXPR if neither
	the second nor third operand has side effects.

gcc/testsuite/ChangeLog:

2009-06-17  Ian Lance Taylor  <iant@google.com>

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


Index: cp/cvt.c
===================================================================
--- cp/cvt.c	(revision 148566)
+++ cp/cvt.c	(working copy)
@@ -828,11 +828,12 @@ convert_to_void (tree expr, const char *
 	/* The two parts of a cond expr might be separate lvalues.  */
 	tree op1 = TREE_OPERAND (expr,1);
 	tree op2 = TREE_OPERAND (expr,2);
+	bool side_effects = TREE_SIDE_EFFECTS (op1) || TREE_SIDE_EFFECTS (op2);
 	tree new_op1 = convert_to_void
-	  (op1, (implicit && !TREE_SIDE_EFFECTS (op2)
+	  (op1, (implicit && !side_effects
 		 ? "second operand of conditional" : NULL), complain);
 	tree new_op2 = convert_to_void
-	  (op2, (implicit && !TREE_SIDE_EFFECTS (op1)
+	  (op2, (implicit && !side_effects
 		 ? "third operand of conditional" : NULL), complain);
 
 	expr = build3 (COND_EXPR, TREE_TYPE (new_op1),
Index: testsuite/g++.dg/warn/Wunused-16.C
===================================================================
--- testsuite/g++.dg/warn/Wunused-16.C	(revision 0)
+++ testsuite/g++.dg/warn/Wunused-16.C	(revision 0)
@@ -0,0 +1,9 @@
+// { dg-do compile }
+// { dg-options "-Wunused-value" }
+
+extern void f1();
+void
+f(bool b)
+{
+  b ? f1(), 0 : 0;	// { dg-bogus "has no effect" }
+}



More information about the Gcc-patches mailing list