liekamg
liekamg

Reputation: 23

C how to show all unused functions (including ones defined in h files)

Somewhat related to this, but it still doesn't quite answer my question

I have a C project, and i would like to enumerate all unreferenced functions (including non-static, so setting compiler option for werror=unused-function only partially works...) in order to identify and clean up the codebase.

One way I thought is to enumerate all the functions in the project, and then make a script to go through each and see if it is called via cscope... but I'm not sure how to get a list-form of all the functions in the first place. That link above has a solution that is failing for me.

Any other ideas are welcome.

Upvotes: 2

Views: 1527

Answers (4)

Luis Colorado
Luis Colorado

Reputation: 12668

Knowing if a function will be called or not is an undecidable problem (analogous to the stop issue for Turing machines). As you can call a function through a pointer (as it is done by callbacks) you cannot actually determine if a function will be called until you actually take it out from the code and run the code.

Linker only links functions that are referenced in the code, and doesn't know if they belong to dead code that will not be called.

If you want to know all the referenced functions in your code, just grep the output of nm(1) command on all your .o files, to get all the U undefined references (this is the list of functions that must be externally linked to your code). This will list all the external references to functions that must be resolved by the linker. If your function is not there, then it is not used by that module. You can match this list with the list of external functions (the ones marked as T in nm(1) output) of the .o files that you want to check (or shared objects .so) and you'll see (as the linker does) which ones are published to the linker but not referenced in your code. Think twice, as this only represents a direct reference, you have to manage also for indirect references (your module asks for a function in another module, that finally asks for the function you are trying to check).

In case your functions are static (only file visibility) just surround the function definition by a #if 0 directive, and you'll get if the function is being referenced somewhere.

I repeat, you cannot easily know if a function will be called in your code, you can know if it is referenced somewhere.

I don't know what are you trying to identify with this question, but you can run into the XY problem instance (what you ask is not what you try to solve)

By the way, defined functions in .h header files are commonly declared inline by developers to optimice function call/return execution. For this reason, they will be inlined where they are used and so, no reference appears to them on linking, so you have to search for them in the code (with the added problem of being macro expanded, so you need to run the preprocessor first to find the references to those functions)

Upvotes: 1

Thomas Dickey
Thomas Dickey

Reputation: 54515

I do this in ncurses, to see which library symbols are used by a (custom) script list-used.sh which makes a list of the symbols exported from its libraries and which of those are used (or not) from programs linked against the libraries.

The output of the script is a report which makes up most of the test/README file.

In other answers, there were tools suggested which would be either (a) unavailable or (b) require extensive rework of the build structure (to put a few hundred files into a single command-line as required by those tools).

Upvotes: 0

Gem Taylor
Gem Taylor

Reputation: 5613

There are g++ compiler options to remove unused code or not, and you can use the difference in symbol tables to point to unused non-virtual methods. Virtual methods are "used" by virtue of being linked into the vtable even if they are never called, so the only way to find those is via coverage tools or perhaps lint. Dynamically linked library methods are a different issue, as you should only have a limited number of published entrypoints that you need to check through. And anyway you have to decide if some other client would want those entrypoints.

Upvotes: 0

Andrew Cottrell
Andrew Cottrell

Reputation: 3413

I would use a static code analysis tool such as lint. It is a good tool for discovering potential problems in your code as well as for keeping it tidy, since it can be configured to point out unused functions/variables/etc.

Others have suggested Cppcheck which is a free alternative, but I have not used it so I cannot personally vouch for it.

Upvotes: 0

Related Questions