Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get warned about unused functions?

Tags:

c

gcc

gcc-warning

I'd like to find unused functions in a codebase - including across compilations units. I'm using gcc as my compiler.

Here's an example:

foo.c (assume appropriate foo.h):

void foo() {    .... }  void bar() {    .... } 

main.c:

#include <stdio.h> #include "foo.h"    int main(void)  {     bar();     return 0; } 

In this example, I'd like to get warned about foo() not being used.

There is the -Wunused-function gcc option:

-Wunused-function

Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall.

but it's only for static functions - it won't produce a warning on the example above.

I'll also accept suggestions of tools/scripts/other compilers that can do this for me - though I'd prefer to stick with gcc if possible.

like image 509
Timothy Jones Avatar asked Feb 01 '12 05:02

Timothy Jones


People also ask

Does compiler remove unused functions?

If either the compiler or the linker can see that there are no references to C functions or C variables, they can (and usually do) remove those unused things.

How do you find unused functions in Python?

In Python you can find unused code by using dynamic or static code analyzers. Two examples for dynamic analyzers are coverage and figleaf . They have the drawback that you have to run all possible branches of your code in order to find unused parts, but they also have the advantage that you get very reliable results.


2 Answers

Caolan Mc Namara, a LibreOffice developer, has made a small tool to detect this type of thing in LibreOffice source code. They had around thousands functions & methods unused in LibreOffice. His tool is a key element for removing them.

It's called callcatcher. It can

collect functions/methods defined and subtract called/referenced

It works directly on assembler output and so, it works only for x86 and x86_64 architecture. It can produce output like this. You can integrate it with your traditional compiling and linking call to gcc.

Caolan agrees that it should become a gcc plugin.

like image 75
Coren Avatar answered Oct 09 '22 05:10

Coren


I know you asked for warnings and prefers not to use gcc option but it is really easy.

You can use linker optimization (--gc-sections) in order to remove the dead code from your application.

From gcc's man page:

--gc-sections --no-gc-sections Enable garbage collection of unused input sections. It is ignored on targets that do not support this option. The default behaviour (of not performing this garbage collection) can be restored by specifying --no-gc-sections on the command line.

--gc-sections decides which input sections are used by examining symbols and relocations. The section containing the entry symbol and all sections containing symbols undefined on the command-line will be kept, as will sections containing symbols referenced by dynamic objects. Note that when building shared libraries, the linker must assume that any visible symbol is referenced. Once this initial set of sections has been determined, the linker recursively marks as used any section referenced by their relocations. See --entry and --undefined.

This option can be set when doing a partial link (enabled with option -r). In this case the root of symbols kept must be explicitly specified either by an --entry or --undefined option or by a "ENTRY" command in the linker script.

like image 33
eyalm Avatar answered Oct 09 '22 06:10

eyalm