Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

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.

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,

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top