title: Automated Dead code detection in native C++ application on Windows?
body: <h2>Background</h2>
  
  <p>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.</p>
  
  <h2>Assumptions/Constraints</h2>
  
  <p>The method and/or tools must support:</p>
  
  <ul>
  <li>Native (i.e. unmanaged) C++</li>
  <li>Windows XP</li>
  <li>Visual Studio 2005</li>
  <li>Must not require user supplied test cases for coverage. (e.g. can't depend on unit tests to generate code coverage)</li>
  </ul>
  
  <p>If the methods support more than these requirements, then great. </p>
  
  <p><strong>NOTE:</strong> We currently use the Professional edition of Visual Studio 2005, not the Team System. Therefore, using Team System might be a <em>valid</em> suggestion (I don't know, I've never used it) however I'm hoping it is not the <em>only</em> solution.</p>
  
  <h2>Why using unit tests for code coverage is problematic</h2>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <h2>Question</h2>
  
  <p><strong>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?</strong></p>
  
  <h2>See Also</h2>
  
  <p><a href="https://stackoverflow.com/questions/229069/dead-code-detection-in-legacy-cc-project">Dead code detection in legacy C/C++ project</a>
  <a href="https://stackoverflow.com/questions/104952/i-want-tell-the-vc-compiler-to-compile-all-code-can-it-be-done">I want tell the VC++ Compiler to compile all code. Can it be done?</a></p>