views:

873

answers:

7

Whilst refactoring some old code I realised that a particular header file was full of function declarations for functions long since removed from the .cpp file. Does anyone know of a tool that could find (and strip) these automatically?

A: 

If you index to code with Doxygen you can see from where is each function referenced. However, you would have to browse through each class (1 HTML page per class) and scan for those that don't have anything pointing to them.

Alternatively, you could use ctags to generate list of all functions in the code, and then use objdump or some similar tool to get list of all function in .o files - and then compare those lists. However, this can be problematic due to name mangling.

Milan Babuškov
A: 

I don't think there is such thing because some functions not having a body in the actual source tree might be defined in some external library. This can only be done by creating a script which makes a list of declared functions in a header and verifies if they are sometimes called.

Iulian Şerbănoiu
+2  A: 

You could if possible make a test.cpp file to call them all, the linker will flag the ones that have no code as unresolved, this way your test code only need compile and not worry about actually running.

titanae
That will end up being one more set of additions for each function that is written. Additionally, if a function call is missed in this test file that doesn't do anything, at some point, it will be near-impossible to know about that.
Kris Kumler
Agreed, I was thinking more as a one off to test the area being refactored. It's not a long term solution, another approach might be to use VERBOSE output on the linker.
titanae
A: 

I have a C++ ftplugin for vim that is able is check and report unmatched functions -- vimmers, the ftplugin suite is not yet straightforward to install. The ftplugin is based on ctags results (hence its heuristic could be easily adapted to other environments), sometimes there are false positives in the case of inline functions.

HTH,

Luc Hermitte
A: 

In addition Doxygen (@Milan Babuskov), you can see if there are warnings for this in your compiler. E.g. gcc has -Wunused-function for static functions; -fdump-ipa-cgraph.

Kris Kumler
A: 

I've heard good things about PC-Lint, but I imagine it's probably overkill for your needs.

tfinniga
Not so. PC-Lint is good for a lot of things as well as this. Just remember to turn off all warnings and slowly turn them on as you need them. Really, you only need a select core of about 2 dozen to improve your code base. Being pedantic and linting to the max makes your code butt-ugly.
graham.reeds
+2  A: 

PC-lint can be tunned for dedicated purpose:

I tested the following code against for your question:

void foo(int );

int main()
{
    return 0;
}
lint.bat test_unused.cpp

and got the following result:

============================================================

--- Module:   test_unused.cpp (C++)

    --- Wrap-up for Module: test_unused.cpp

Info 752: local declarator 'foo(int)' (line 2, file test_unused.cpp) not referenced
test_unused.cpp(2) : Info 830: Location cited in prior message

============================================================

So you can pass the warning number 752 for your puropse:

lint.bat  -"e*"  +e752  test_unused.cpp

-e"*" will remove all the warnings and +e752 will turn on this specific one

赵如飞