问题描述
在几年的时间里,我有一个使用本地C ++编写的应用程序,大约60 KLOC。有许多函数和类已经死了(可能10-15%类似于下面提到的类似的基于Unix的问题)。我们最近开始对所有新代码进行单元测试,并尽可能将其应用于修改的代码。
I have an application written in native C++ over the course of several years that is around 60 KLOC. There are many many functions and classes that are dead (probably 10-15% like the similar Unix based question below asked). We recently began doing unit testing on all new code and applying it to modified code whenever possible. However, I would make a SWAG that we have less than 5% test coverage at the present moment.
方法和/或工具必须支持:
The method and/or tools must support:
- 本机(即非托管)C ++
- Windows XP
- Visual Studio 2005
- 不能要求用户提供测试用例。 (例如,不能依靠单元测试来生成代码覆盖)
- Native (i.e. unmanaged) C++
- Windows XP
- Visual Studio 2005
- Must not require user supplied test cases for coverage. (e.g. can't depend on unit tests to generate code coverage)
如果方法支持超过这些要求,那么很好。
If the methods support more than these requirements, then great.
注意:我们目前使用的是专业版的Visual Studio 2005,而不是团队系统。因此,使用Team System可能是一个有效的建议(我不知道,我从来没有使用过),但我希望它不是只有解决方案。
NOTE: We currently use the Professional edition of Visual Studio 2005, not the Team System. Therefore, using Team System might be a valid suggestion (I don't know, I've never used it) however I'm hoping it is not the only solution.
我相信一个通用工具是不可能的在任何任意应用程序中找到所有死(例如无法到达的代码),零假误报(我认为这将等同于暂停问题)。然而,我也相信一个通用工具可能找到很多类型的死代码,很可能事实上已经死了,像类或函数从来没有在代码中引用任何其他。
I believe that it is impossible for a generic tool to find all the dead (e.g. unreachable code) in any arbitrary application with zero false positives (I think this would be equivalent to the Halting problem). However, I also believe it is possible for a generic tool to find many types of dead code that are highly probable to in fact be dead, like classes or functions which are never reference in the code by anything else.
通过使用单元测试来提供此覆盖,您不再使用通用算法,因此增加了可以检测的死代码的百分比和任何命中不是假阳性的概率。相反,使用单元测试可能会导致错误的否定,因为单元测试本身可能是执行给定代码段的唯一方法。理想情况下,我将进行回归测试,执行所有外部可用的方法,API,用户控件等,这将作为代码覆盖率分析的基线测量,以排除某些方法从假阳性。很遗憾,我目前没有这种自动化测试。
By using unit tests to provide this coverage, you no longer using a generic algorithm and are thus increasing both the percentage of dead code you can detect and the probability that any hits are not false positives. Conversely, using unit tests could result in false negatives since the unit tests themselves might be the only thing exercising a given piece of code. Ideally, I would have regression testing that exercises all externally available methods, APIs, user controls, etc. which would serve as a baseline measurement of code coverage analysis to rule out certain methods from being false positives. Sadly however, I do not have this automated testing at the present time.
由于我有这么大的代码库,测试用例覆盖率很低,
Since I have such a large code base with such a low test case coverage percentage however, I'm looking for something that could help without requiring huge amounts of time invested in writing test cases.
如何在Windows平台上使用Visual Studio 2005开发环境以自动或半自动方式在本机C ++应用程序中检测死代码?
Dead code detection in legacy C/C++ projectI want tell the VC++ Compiler to compile all code. Can it be done?
推荐答案
要求链接器删除未引用的对象(/ OPT:REF)。如果使用函数级链接和详细链接器输出,链接器输出将列出可以证明未使用的每个函数。此列表可能远未完成,但您已拥有所需的工具。
Ask the linker to remove unreferenced objects (/OPT:REF). If you use function-level linking, and verbose linker output, the linker output will list every function it can prove is unused. This list may be far from complete, but you already have the tools needed.
这篇关于Windows上的本机C ++应用程序中的自动死代码检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!