6

I'm looking for a way to check if my C project, that compiles to an ELF, has unused functions, and locate them. That is functions that are declared but are not called anywhere in my code.

The solution can be one of:

  • A utility that goes through my .c files, analysing them
  • A utility that goes through my compiled ELF file, that has symbols, analysing it statically
  • A way to warn about unused functions in gcc (and -Wunused-functions doesn't do that for global functions)

The solution cannot be one of:

  • Deleting unused functions in compile time, without knowing what functions were deleted
  • Analysing the ELF file in run-time, since not every function will be called in every run such as gprof (some functions take days until they are called, but in the code flow you can see that they are eventually called)
  • A utility that discovers dead-code inside functions (i.e. code after a return from function), rather than unused functions

Thank you

speller
  • 1,641
  • 2
  • 20
  • 27
  • An ugly idea: concatenate all your code files temporarily into one file, make all your functions static in that file. The compiler will tell you the unused ones. But probably you would like something nicer... – Gábor Buella Mar 24 '14 at 22:14
  • Suggestion: the preprocessed sources can be maybe a better subject of the analyse. – peterh Mar 24 '14 at 22:14
  • Use a compiler/linker that removes unreferenced functions and outputs a list of those. – undur_gongor Mar 24 '14 at 22:16
  • Do you have system or integration tests? – detly Mar 24 '14 at 22:19
  • 6
    Have you looked ***[HERE](http://stackoverflow.com/a/229121/645128)***? Or ***[HERE](http://stackoverflow.com/a/10732950/645128)***? Or ***[HERE](http://stackoverflow.com/a/9319528/645128)***? – ryyker Mar 24 '14 at 22:19
  • Compiler specific: But, if the functions you are trying to detect are not using extern linkage, perhaps using `-Wunused-function or -Wunused` (or sililar depending on your environment) in the make will help. – ryyker Mar 24 '14 at 22:25
  • As I recall, the VMS linker indicates "unused" symbols on its maps. These are global symbols which are not referenced. It does have the weakness that a reference from inside a module which defines the symbol does not usually prevent the "unused" flag. – wallyk Mar 24 '14 at 22:48

2 Answers2

1

If you need something exact, automated or polished, you need your compiler and build-system to team up and do it for you, somehow.

If you don't need exact results or something particularly automated or polished, then here's a very rough approximation: It'll find every word that occurs only once in all of your .c files.

find . -name \*.c -exec cat {} \; \
   | tr -s '[[:space:];:,?!.|()-"<>=]' '\n' \
   | sort \
   | uniq -u

This could of course fail in a million ways: preprocessor tricks, comments repeating function names, functions named the same as common words used in comments etc.

Søren Debois
  • 5,598
  • 26
  • 48
1

The easiest way is to process the object files in the project, rather like a linker would. It doesn't have to do anything other than notice unreferenced symbols, so this is much easier than writing a linker.

The Unix/Linux/Cygwin utility called lorder does what you want.

wallyk
  • 56,922
  • 16
  • 83
  • 148