Search code examples
c++static-analysiscppcheckdead-code

How to detect functions, that are only called from unused functions using cppcheck?


I'm trying to detect unused functions in C++. At the moment I'm trying to use cppcheck but i don't know if it is possible or how it is possible to detect functions, that are used but only by functions that are not used themselves.

Here's my little test code:

int bla() {
    return 0;
}

int test() {
    return bla();
}

int main() {
    int a = 0;
    int b = 0;
    return b;
}

That's what cppcheck detects with my current settings:

$ cppcheck --enable=style,unusedFunction test.cpp 
Checking test.cpp...
[test.cpp:10]: (style) Variable 'a' is assigned a value that is never used.
Checking usage of global functions..
[test.cpp:5]: (style) The function 'test' is never used.

The problem is that it doesn't detect the function bla as unused, because its called in test. But test is never called so neither is bla. I want that all functions except for functions that are used by main are marked as unused.

Do you know a option for cppcheck?


Solution

  • I found my own solution using callcatcher http://www.skynet.ie/~caolan/Packages/callcatcher.html. It's not a static code analysis but it works exaclty how i want it to work.