[PING^2] [C/C++] PR c/4076 -Wunused doesn't warn about static function only called by itself

Manuel López-Ibáñez lopezibanez@gmail.com
Wed Mar 14 23:15:00 GMT 2007


This is the last ping of tonight, I don't want to annoy people.

http://gcc.gnu.org/ml/gcc-patches/2007-03/msg00171.html

Cheers,

Manuel.

On 03/03/07, Manuel López-Ibáñez <lopezibanez@gmail.com> wrote:
> :ADDPATCH C / C++:
>
> -Wunused currently does not warn about static functions that are
> called only by themselves. This patch prevents marking a function as
> used if the call is done within the same function.
>
> Bootstrapped and regression tested with --enable-languages=all.
> This patch has found a few unused in GCC codebase.
>
> OK for mainline?
>
>
> 2007-03-03  Manuel Lopez-Ibanez  <manu@gcc.gnu.org>
>
>   PR c/4076
>   * c-typeck.c (build_external_ref): Don't mark as used if called from itself.
>   * calls.c (rtx_for_function_call): Likewise.
>
> cp/
>   * call.c (build_call_a): Don't mark as used if called from itself.
>   * semantics.c (finish_id_expression): Likewise.
>
> testsuite/
>   * gcc.dg/Wunused-function.c: New.
>   * g++.dg/warn/Wunused-function.C: New.
>
>
-------------- next part --------------
Index: gcc/testsuite/gcc.dg/Wunused-function.c
===================================================================
--- gcc/testsuite/gcc.dg/Wunused-function.c	(revision 0)
+++ gcc/testsuite/gcc.dg/Wunused-function.c	(revision 0)
@@ -0,0 +1,6 @@
+/* PR c/4076  -Wunused doesn't warn about static function only called by itself.  */
+/* { dg-do compile } */
+/* { dg-options "-Wunused-function" } */
+
+static void foo (void) {} /* { dg-warning "'foo' defined but not used" } */
+static void bar (void) { bar (); } /* { dg-warning "'bar' defined but not used" } */
Index: gcc/testsuite/g++.dg/warn/Wunused-function.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Wunused-function.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/Wunused-function.C	(revision 0)
@@ -0,0 +1,9 @@
+// PR c/4076  -Wunused doesn't warn about static function only called by itself.  
+// { dg-do compile } 
+// { dg-options "-Wunused-function" }
+
+static void foo (void);
+static void bar (void);
+
+static void foo (void) {} // { dg-warning "foo.?.?' defined but not used" } 
+static void bar (void) { bar (); } // { dg-warning "bar.?.?' defined but not used" } 
Index: gcc/cp/call.c
===================================================================
--- gcc/cp/call.c	(revision 122472)
+++ gcc/cp/call.c	(working copy)
@@ -314,7 +314,9 @@ build_call_a (tree function, int n, tree
       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
     {
       decl = TREE_OPERAND (function, 0);
-      if (!TREE_USED (decl))
+
+      /* Recursive call does not count as usage.  */
+      if (!TREE_USED (decl) && decl != current_function_decl)
 	{
 	  /* We invoke build_call directly for several library
 	     functions.  These may have been declared normally if
@@ -5028,7 +5030,9 @@ build_over_call (struct z_candidate *can
       return val;
     }
 
-  mark_used (fn);
+  /* Recursive call does not count as usage.  */
+  if (fn != current_function_decl)
+    mark_used (fn);
 
   if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
     {
Index: gcc/cp/semantics.c
===================================================================
--- gcc/cp/semantics.c	(revision 122472)
+++ gcc/cp/semantics.c	(working copy)
@@ -2852,7 +2852,8 @@ finish_id_expression (tree id_expression
 	  if (TREE_CODE (first_fn) == TEMPLATE_DECL)
 	    first_fn = DECL_TEMPLATE_RESULT (first_fn);
 
-	  if (!really_overloaded_fn (decl))
+	  /* Recursive call does not count as usage.  */
+	  if (!really_overloaded_fn (decl) && decl != current_function_decl)
 	    mark_used (first_fn);
 
 	  if (!template_arg_p
Index: gcc/c-typeck.c
===================================================================
--- gcc/c-typeck.c	(revision 122472)
+++ gcc/c-typeck.c	(working copy)
@@ -2077,9 +2077,13 @@ build_external_ref (tree id, int fun, lo
   if (TREE_DEPRECATED (ref))
     warn_deprecated_use (ref);
 
-  if (!skip_evaluation)
-    assemble_external (ref);
-  TREE_USED (ref) = 1;
+  /* Recursive call does not count as usage.  */
+  if (ref != current_function_decl) 
+    {
+      if (!skip_evaluation)
+	assemble_external (ref);
+      TREE_USED (ref) = 1;
+    }
 
   if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
     {
Index: gcc/calls.c
===================================================================
--- gcc/calls.c	(revision 122472)
+++ gcc/calls.c	(working copy)
@@ -1482,7 +1482,7 @@ rtx_for_function_call (tree fndecl, tree
     {
       /* If this is the first use of the function, see if we need to
 	 make an external definition for it.  */
-      if (! TREE_USED (fndecl))
+      if (!TREE_USED (fndecl) && fndecl != current_function_decl)
 	{
 	  assemble_external (fndecl);
 	  TREE_USED (fndecl) = 1;


More information about the Gcc-patches mailing list