public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C/C++] PR c/4076 -Wunused doesn't warn about static function only called by itself
@ 2007-03-03 11:48 Manuel López-Ibáñez
  2007-03-14 23:15 ` [PING^2] " Manuel López-Ibáñez
  0 siblings, 1 reply; 13+ messages in thread
From: Manuel López-Ibáñez @ 2007-03-03 11:48 UTC (permalink / raw)
  To: gcc-patches; +Cc: Joseph Myers, Nathan Sidwell, Richard Henderson

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

: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.

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

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;

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2007-04-09 10:44 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-03 11:48 [C/C++] PR c/4076 -Wunused doesn't warn about static function only called by itself Manuel López-Ibáñez
2007-03-14 23:15 ` [PING^2] " Manuel López-Ibáñez
2007-03-15 10:17   ` Nathan Sidwell
2007-03-16 11:32     ` Manuel López-Ibáñez
2007-03-16 12:38       ` Nathan Sidwell
2007-03-16 15:21         ` Gabriel Dos Reis
2007-03-17  0:47         ` Manuel López-Ibáñez
2007-03-19 12:16           ` Nathan Sidwell
2007-03-24 17:23             ` Manuel López-Ibáñez
2007-04-03 22:22               ` [PING^3] " Manuel López-Ibáñez
2007-04-09 10:44                 ` Nathan Sidwell
2007-03-16 15:29       ` [PING^2] " Gabriel Dos Reis
2007-03-16 15:42         ` 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).