如何检测仅使用 cppcheck 从未使用的函数调用的函数?

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

我正在尝试检测 C++ 中未使用的函数。目前我正在尝试使用 cppcheck,但我不知道是否有可能或如何检测函数,这些函数被使用但仅由它们自己未使用的函数使用。

这是我的小测试代码:

int bla() {
    return 0;
}

int test() {
    return bla();
}

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

这就是 cppcheck 使用我当前设置检测到的内容:

$ 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.

问题是它没有将函数 bla 检测为未使用,因为它是在测试中调用的。但是从来没有调用过测试,所以 bla 也没有。我希望除 main 使用的函数之外的所有函数都标记为未使用。

您是否知道 cppcheck 的选项或什至其他静态代码分析工具可以将 bla 检测为未使用?

您可以尝试 CppDepend 及其查询语言 CQLinq,您可以使用 CQLinq 高级查询创建以根据需要过滤结果,例如在您的情况下您可以执行此查询:

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

我使用 callcatcher http://www.skynet.ie/~caolan/Packages/callcatcher.html 找到了自己的解决方案。这不是静态代码分析,但它完全按照我希望的方式工作。