hotcolddog
hotcolddog

Reputation: 3

Find out which code is never called in C++ project in Visual Studio 2012

I am using VS2012 and I would like to know which code in my project is never called. How can I do it?

Here is the menu I tried out for the dead code analysis, but did not find it here.

enter image description here

Upvotes: 0

Views: 2448

Answers (2)

Krishty
Krishty

Reputation: 380

On Visual Studio 2019 or later:

  1. enable /OPT:REF on the linker (removal of unused functions)
  2. enable /VERBOSE:REF on the linker (enable verbose output for above pass)

This will result in such a list:

…
Discarded "public: int * __cdecl OS::Region::tryToReserve<int>(unsigned __int64,struct Supervisor const *)" from allocator.cpp.o
Discarded "public: float * __cdecl OS::Region::tryToReserve<float>(unsigned __int64,struct Supervisor const *)" from allocator.cpp.o
…

Note that the list will be quite long because it lists template instantiations instead of declarations, contains unwind information, imports from 3rd-party libraries, etc.

But it is a starting point.

Upvotes: 2

IInspectable
IInspectable

Reputation: 51364

Short answer: Visual Studio does not support this1.

The code analysis checkers that can find uncalled functions are available for managed (i.e. .NET) code only, e.g. CA1811: Avoid uncalled private code.

Static analysis of C++ code is a lot more difficult, and there are only a handful of Code Analysis for C/C++ Warnings related to unused/redundant/unreachable code:

  • C6235: "(<non-zero constant> || <expression>) is always a non-zero constant"
  • C6236: "(<expression> || <non-zero constant>) is always a non-zero constant"
  • C6237: "(<zero> && <expression>) is always zero. <expression> is never evaluated and may have side effects"
  • C6239: "(<non-zero constant> && <expression>) always evaluates to the result of <expression>. Did you intend to use the bitwise-and operator?"
  • C6240: "(<expression> && <non-zero constant>) always evaluates to the result of <expression>. Did you intend to use the bitwise-and operator?"
  • C6259: "labeled code is unreachable: (<expression> & <constant>) in switch-expr cannot evaluate to <case-label>"
  • C6269: "possible incorrect order of operations: dereference ignored"
  • C6285: "(<non-zero constant> || <non-zero constant>) is always a non-zero constant. Did you intend to use the bitwise-and operator?"
  • C6286: "(<non-zero constant> || <expression>) is always a non-zero constant. <expression> is never evaluated and may have side effects"
  • C6287: "redundant code: the left and right sub-expressions are identical"
  • C6288: "Incorrect operator: mutual inclusion over && is always zero. Did you intent to use || instead?"
  • C6289: "Incorrect operator: mutual exclusion over || is always a non-zero constant. Did you intend to use && instead?"
  • C6294: "Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed"
  • C6313: "Incorrect operator: Zero-valued flag cannot be tested with bitwise-and. Use an equality test to check for zero-valued flags"
  • C6316: "Incorrect operator: tested expression is constant and non-zero. Use bitwise-and to determine whether bits are set"
  • C6319: "use of the comma-operator in a tested expression causes the left argument to be ignored when it has no side-effects"

All of those rules indicate either a bug, or point to redundant code, that is never executed. The list applies to code analysis rules implemented in Visual Studio 2017. Previous versions of Visual Studio may not provide checkers for all of them.


1 This is true up to and including Visual Studio 2017, the most current release at the time of writing.

Upvotes: 3

Related Questions