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

4.7k views Asked by At

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?

2

There are 2 answers

0
timakro On BEST ANSWER

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.

0
James from CppDepend Team On

You can try CppDepend and its query language CQLinq, you can create with CQLinq advanced queries to filter the result as you want, for example in your case you can execute this query:

from m in Methods where m.MethodsCallingMe
.Where(a=>!a.SimpleName.Contains(("test"))).Count()>0
select m