死code检测传统的C / C ++项目 [英] Dead code detection in legacy C/C++ project

查看:254
本文介绍了死code检测传统的C / C ++项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您会如何去用C / C ++ code死code检测?我有一个pretty大code的基础工作,并和至少10%-15%是死code。是否有任何基于Unix工具来识别这个领域? code的一些作品仍然使用大量的preprocessor,可自动流程处理这个问题?

How would you go about dead code detection in C/C++ code? I have a pretty large code base to work with and at least 10-15% is dead code. Is there any Unix based tool to identify this areas? Some pieces of code still use a lot of preprocessor, can automated process handle that?

推荐答案

您可以用code覆盖分析工具,这一点,看看在你的code未使用的斑点。

You could use a code coverage analysis tool for this and look for unused spots in your code.

在gcc工具一个流行的工具是gcov的,再加上图形前端LCOV(<一个href=\"http://ltp.sourceforge.net/coverage/lcov.php\">http://ltp.sourceforge.net/coverage/lcov.php).

A popular tool for the gcc toolchain is gcov, together with the graphical frontend lcov (http://ltp.sourceforge.net/coverage/lcov.php).

如果你使用gcc,你可以用gcov的支持,这是由--coverage'标志启用编译。接下来,运行应用程序或与此gcov的启用构建运行测试套件。

If you use gcc, you can compile with gcov support, which is enabled by the '--coverage' flag. Next, run your application or run your test suite with this gcov enabled build.

基本上将GCC编译过程中散发出一些额外的文件和应用程序也将发出一些覆盖数据运行时。你必须收集所有这些(.gcdo和.gcda文件)。我不是在所有细节将在这里,但你可能需要设置两个环境变量来收集覆盖数据以正常的方式:GCOV_ preFIX和GCOV_ preFIX_STRIP ...

Basically gcc will emit some extra files during compilation and the application will also emit some coverage data while running. You have to collect all of these (.gcdo and .gcda files). I'm not going in full detail here, but you probably need to set two environment variables to collect the coverage data in a sane way: GCOV_PREFIX and GCOV_PREFIX_STRIP...

运行后,可以把所有的覆盖率数据一起,并通过工具包LCOV运行它。来自不同测试运行中的所有文件覆盖的合并也是可能的,虽然有点麻烦。

After the run, you can put all the coverage data together and run it through the lcov toolsuite. Merging of all the coverage files from different test runs is also possible, albeit a bit involved.

总之,你最终用一个漂亮的一套网页显示一些报道的信息,指出没有覆盖,因此,没有使用code的部分。

Anyhow, you end up with a nice set of webpages showing some coverage information, pointing out the pieces of code that have no coverage and hence, were not used.

当然,你需要仔细检查,如果在任何情况下不使用code的部分,并在很大程度上取决于你的测试有多好锻炼codeBase的。但至少,这将给有关可能的死区code候选人的想法......

Off course, you need to double check if the portions of code are not used in any situation and a lot depends on how good your tests exercise the codebase. But at least, this will give an idea about possible dead-code candidates...

这篇关于死code检测传统的C / C ++项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆