Automated Dead code detection in native C++ application on Windows?

╄→гoц情女王★ 提交于 2019-12-03 06:57:40

问题


Background

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.

Assumptions/Constraints

The method and/or tools must support:

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

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.

Why using unit tests for code coverage is problematic

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.

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.

Question

How do you go about detecting dead code in an automated or semi-automated fashion in a native C++ application on the Windows platform with the Visual Studio 2005 development environment?

See Also

Dead code detection in legacy C/C++ project I want tell the VC++ Compiler to compile all code. Can it be done?


回答1:


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.




回答2:


We use Bullseye, and I can recommend it. It doesn't need to be run from a unit test environment, although that's what we do.




回答3:


Use a code coverage tool against your unit test suite.



来源:https://stackoverflow.com/questions/307478/automated-dead-code-detection-in-native-c-application-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!