我如何知道代码中的哪些部分从未使用过? [英] How can I know which parts in the code are never used?

查看:128
本文介绍了我如何知道代码中的哪些部分从未使用过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有遗留的C ++代码,我应该从中删除未使用的代码。问题是代码库很大。

I have legacy C++ code that I'm supposed to remove unused code from. The problem is that the code base is large.

如何知道哪些代码从未被调用/从未使用过?

How can I find out which code is never called/never used?

推荐答案

有两种类型的未使用的代码:

There are two varieties of unused code:


  • ,在某些函数中一些路径或变量未使用(或使用但没有有意义的方式,比如书写但从未读过)。

  • 全局变量:从未访问过

对于第一类,良好的编译器可以帮助:

For the first kind, a good compiler can help:


  • -Wunused (GCC, Clang )应警告未使用的变量,Clang未使用的分析器甚至已经增加,以警告从未读过(即使使用)的变量。

  • - Wunreachable- (旧版GCC,在2010年移除)应警告关于从未被访问的本地块(它发生在早期返回或条件总是求值为真)

  • 没有选项我知道警告未使用 catch 阻塞,因为编译器通常不能证明没有异常被抛出。

  • -Wunused (GCC, Clang) should warn about unused variables, Clang unused analyzer has even been incremented to warn about variables that are never read (even though used).
  • -Wunreachable-code (older GCC, removed in 2010) should warn about local blocks that are never accessed (it happens with early returns or conditions that always evaluate to true)
  • there is no option I know of to warn about unused catch blocks, because the compiler generally cannot prove that no exception will be thrown.

,这要困难得多。静态地,它需要整个程序分析,并且即使链接时间优化实际上可以去除死代码,在实践中,程序在执行时已经被如此多地变换,以至于几乎不可能向用户传达有意义的信息。

For the second kind, it's much more difficult. Statically it requires whole program analysis, and even though link time optimization may actually remove dead code, in practice the program has been so much transformed at the time it is performed that it is near impossible to convey meaningful information to the user.

因此有两种方法:


  • 理论上是使用静态分析器。一个软件,将一次检查整个代码非常详细,找到所有的流路径。

  • 实用的方法是使用启发式方法:使用代码覆盖工具(在GNU链中它 gcov 。请注意,特定标志应在编译期间传递,以使其正常工作)。您运行的代码覆盖工具与一组很好的输入(您的单元测试或非回归测试),死代码必须在未到达的代码...,所以你可以从这里开始。

  • The theoretic one is to use a static analyzer. A piece of software that will examine the whole code at once in great detail and find all the flow paths. In practice I don't know any that would work here.
  • The pragmatic one is to use an heuristic: use a code coverage tool (in the GNU chain it's gcov. Note that specific flags should be passed during compilation for it to work properly). You run the code coverage tool with a good set of varied inputs (your unit-tests or non-regression tests), the dead code is necessarily within the unreached code... and so you can start from here.

如果你对这个主题非常感兴趣,并且有时间和倾向自己实际工作一个工具,我建议使用Clang库构建此类工具。

If you are extremely interested in the subject, and have the time and inclination to actually work out a tool by yourself, I would suggest using the Clang libraries to build such a tool.


  1. 使用Clang库获取AST(抽象语法树)

  2. 从入口点开始执行标记和扫描分析

因为Clang会为您解析代码,并执行重载分辨率,你不必处理C ++语言规则,你将能够专注于手头的问题。

Because Clang will parse the code for you, and perform overload resolution, you won't have to deal with the C++ languages rules, and you'll be able to concentrate on the problem at hand.

但是这种技术不能识别未使用的虚拟覆盖,因为它们可以由您无法进行推理的第三方代码调用。

However this kind of technique cannot identify the virtual overrides that are unused, since they could be called by third-party code you cannot reason about.

这篇关于我如何知道代码中的哪些部分从未使用过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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