public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Wunused-value and Wextra interaction (PR7651 Define -Wextra strictly in terms of other warning flags)
@ 2007-01-05 23:36 Manuel López-Ibáñez
  0 siblings, 0 replies; only message in thread
From: Manuel López-Ibáñez @ 2007-01-05 23:36 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1850 bytes --]

:ADDPATCH c++:

This patch continues the effort to fix PR7651 [1].

Currently, Wextra warns when an expression-statement or the left-hand
side of a comma expression contains no side effects. However, as it is
currently implemented, the same warning is enabled by -Wunused-value,
which in turn is enabled by -Wall.

The following patch removes the warning from Wextra and expands the
description of Wunused-value to mention this particular case. It also
unifies the code that handles the warning for C and C++ into a single
function in c-common.c.

A noteworthy difference between C and C++ implementations is that C++
front-end does not emit the warning for empty statements
(IS_EMPTY_STMT). I have favoured the C implementation that does not
check for this (since there are instances in the testsuite of such
cases while there is no test checking the behaviour of the C++
front-end). Is there any rationale for not emitting the warning for
empty statements in C++?

Bootstrapped and regression tested with --enable-languages=all on
i686-pc-linux-gnu.

OK for mainline?

[1] http://gcc.gnu.org/ml/gcc-patches/2006-12/msg01103.html

2007-01-05  Manuel Lopez-Ibanez  <manu@gcc.gnu.org>

  PR middle-end/7651
  * doc/invoke.texi (Wunused-value): Update description.
     (Wextra): Delete item.
  * opts.c (set_Wextra): Don't use the value of Wextra to set the
value of Wunused-value.
  * c-typeck.c (c_process_expr_stmt): Don't check extra_warnings.
  (c_finish_stmt_expr): Don't check extra_warnings.
  (emit_side_effect_warnings): Move function to...
  * c-common.c (emit_side_effect_warnings): ... here.
  * c-common.h (emit_side_effect_warnings): Declare.

cp/
2007-01-05  Manuel Lopez-Ibanez  <manu@gcc.gnu.org>

  PR middle-end/7651
  * cp-gimplify.c (gimplify_expr_stmt): Call emit_side_effect_warnings
in c-common.c. Don't check extra_warnings.

[-- Attachment #2: wunused-wextra.diff --]
[-- Type: text/plain, Size: 5668 bytes --]

Index: gcc/doc/invoke.texi
===================================================================
--- gcc/doc/invoke.texi	(revision 120347)
+++ gcc/doc/invoke.texi	(working copy)
@@ -2704,10 +2704,14 @@ To suppress this warning use the @samp{u
 
 @item -Wunused-value
 @opindex Wunused-value
-Warn whenever a statement computes a result that is explicitly not used.
-This warning is enabled by @option{-Wall}.
+Warn whenever a statement computes a result that is explicitly not
+used. To suppress this warning cast the unused expression to
+@samp{void}. This includes an expression-statement or the left-hand
+side of a comma expression that contains no side effects. For example,
+an expression such as @samp{x[i,j]} will cause a warning, while
+@samp{x[(void)i,j]} will not.
 
-To suppress this warning cast the expression to @samp{void}.
+This warning is enabled by @option{-Wall}.
 
 @item -Wunused
 @opindex Wunused
@@ -2876,13 +2880,6 @@ foo (a)
 @end smallexample
 
 @item
-An expression-statement or the left-hand side of a comma expression
-contains no side effects.
-To suppress the warning, cast the unused expression to void.
-For example, an expression such as @samp{x[i,j]} will cause a warning,
-but @samp{x[(void)i,j]} will not.
-
-@item
 An unsigned value is compared against zero with @samp{<} or @samp{>=}.
 
 @item @r{(C only)}
Index: gcc/cp/cp-gimplify.c
===================================================================
--- gcc/cp/cp-gimplify.c	(revision 120347)
+++ gcc/cp/cp-gimplify.c	(working copy)
@@ -363,18 +363,9 @@ gimplify_expr_stmt (tree *stmt_p)
      In this case we will not want to emit the gimplified statement.
      However, we may still want to emit a warning, so we do that before
      gimplification.  */
-  if (stmt && (extra_warnings || warn_unused_value))
-    {
-      if (!TREE_SIDE_EFFECTS (stmt))
-	{
-	  if (!IS_EMPTY_STMT (stmt)
-	      && !VOID_TYPE_P (TREE_TYPE (stmt))
-	      && !TREE_NO_WARNING (stmt))
-	    warning (OPT_Wextra, "statement with no effect");
-	}
-      else if (warn_unused_value)
-	warn_if_unused_value (stmt, input_location);
-    }
+
+  if (stmt && warn_unused_value)
+    emit_side_effect_warnings (stmt);
 
   if (stmt == NULL_TREE)
     stmt = alloc_stmt_list ();
Index: gcc/opts.c
===================================================================
--- gcc/opts.c	(revision 120347)
+++ gcc/opts.c	(working copy)
@@ -1039,7 +1039,6 @@ static void
 set_Wextra (int setting)
 {
   extra_warnings = setting;
-  warn_unused_value = setting;
   warn_unused_parameter = (setting && maybe_warn_unused_parameter);
 
   /* We save the value of warn_uninitialized, since if they put
Index: gcc/c-typeck.c
===================================================================
--- gcc/c-typeck.c	(revision 120347)
+++ gcc/c-typeck.c	(working copy)
@@ -7338,23 +7338,6 @@ c_finish_bc_stmt (tree *label_p, bool is
   return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
 }
 
-/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr.  */
-
-static void
-emit_side_effect_warnings (tree expr)
-{
-  if (expr == error_mark_node)
-    ;
-  else if (!TREE_SIDE_EFFECTS (expr))
-    {
-      if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
-	warning (0, "%Hstatement with no effect",
-		 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
-    }
-  else if (warn_unused_value)
-    warn_if_unused_value (expr, input_location);
-}
-
 /* Process an expression as if it were a complete statement.  Emit
    diagnostics, but do not call ADD_STMT.  */
 
@@ -7376,7 +7359,7 @@ c_process_expr_stmt (tree expr)
      Warnings for statement expressions will be emitted later, once we figure
      out which is the result.  */
   if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
-      && (extra_warnings || warn_unused_value))
+      && warn_unused_value)
     emit_side_effect_warnings (expr);
 
   /* If the expression is not of a type to which we cannot assign a line
@@ -7492,7 +7475,7 @@ c_finish_stmt_expr (tree body)
 
       /* If we're supposed to generate side effects warnings, process
 	 all of the statements except the last.  */
-      if (extra_warnings || warn_unused_value)
+      if (warn_unused_value)
 	{
 	  for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
 	    emit_side_effect_warnings (tsi_stmt (i));
Index: gcc/c-common.c
===================================================================
--- gcc/c-common.c	(revision 120347)
+++ gcc/c-common.c	(working copy)
@@ -6715,5 +6715,23 @@ warn_about_parentheses (enum tree_code c
 	     "have their mathematical meaning");
 }
 
+/* Invoke this function before gimplification for every statement that
+   is processed and only if -Wunused-value was given.  */
+
+void
+emit_side_effect_warnings (tree expr)
+{
+  if (expr == error_mark_node)
+    ;
+  else if (!TREE_SIDE_EFFECTS (expr))
+    {
+      if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
+        warning (OPT_Wunused_value, "%Hstatement with no effect",
+		 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
+    }
+  else
+    warn_if_unused_value (expr, input_location);
+}
+
 
 #include "gt-c-common.h"
Index: gcc/c-common.h
===================================================================
--- gcc/c-common.h	(revision 120347)
+++ gcc/c-common.h	(working copy)
@@ -861,7 +861,7 @@ extern tree builtin_type_for_size (int, 
 extern void warn_array_subscript_with_type_char (tree);
 extern void warn_about_parentheses (enum tree_code, enum tree_code,
 				    enum tree_code);
-
+extern void emit_side_effect_warnings (tree);
 
 /* In c-gimplify.c  */
 extern void c_genericize (tree);

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2007-01-05 23:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-05 23:36 Wunused-value and Wextra interaction (PR7651 Define -Wextra strictly in terms of other warning flags) Manuel López-Ibáñez

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).