gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:38:34 26 4
gpt4 key购买 nike

我正在尝试检测 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

关于c++ - 如何检测仅使用 cppcheck 从未使用的函数调用的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30711286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com