0

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

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
  • [Probably related](https://stackoverflow.com/questions/38658555/find-dead-code). – user202729 Mar 11 '18 at 13:16
  • @user202729, I saw the post, but I was not able to find `Enable Code Analysis on Build` checkbox. – hotcolddog Mar 11 '18 at 13:19
  • 3
    Finding dead code is a hard problem. When the code is part of a library, for example, it may be dead inside the library but could still be used by users of the library. Also, why are you still using a 6 year old version of VS? Move out of the dark ages already. – Jesper Juhl Mar 11 '18 at 13:22
  • @JesperJuhl, I have a project without any libraries. And it is pretty small. I just want some auto checking for the usage. – hotcolddog Mar 11 '18 at 13:23
  • @hotcolddog Look into tools like [clang-tidy](http://clang.llvm.org/extra/clang-tidy/) to get some help finding dead code. Also, turn your compiler warnings to the max. – Jesper Juhl Mar 11 '18 at 13:25
  • @JesperJuhl, how can I turn my compiler warnings to the max? – hotcolddog Mar 11 '18 at 13:26
  • By reading your compilers documentation and then enable appropriate flags. – Jesper Juhl Mar 11 '18 at 14:06
  • @user202729: Finding dead code in C# is a hell of a lot easier than it is in C++. That other Q&A is asking about C# code, this one about C++. – IInspectable Mar 11 '18 at 20:44
  • @IInspectable I think the OP is asking "how to turn on dead code debugging feature of Visual Studio" instead. – user202729 Mar 12 '18 at 01:31
  • @user202729: I understand that. My comment, that static analysis of code is way easier for C# than it is for C++ is meant to highlight the fact, that not all features available to C# developers are available to C++ developers. *"Calculate Code Metrics"*, for example, is simply not available for C++ projects. I don't know if that applies to dead code analysis, too, but I'd be surprised if that were available for C++ projects. – IInspectable Mar 12 '18 at 09:27
  • For example [CA1811: Avoid uncalled private code](https://learn.microsoft.com/en-us/visualstudio/code-quality/ca1811-avoid-uncalled-private-code) is only available for managed code. Glancing over the [Code Analysis for C/C++ Warnings](https://learn.microsoft.com/en-us/visualstudio/code-quality/code-analysis-for-c-cpp-warnings), I was unable to find anything related at the function level. The best you get is warnings for redundant expressions, or unreachable code inside functions. – IInspectable Mar 12 '18 at 10:08

3 Answers3

3

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.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
2

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.

Krishty
  • 164
  • 12
1

You can try the CppDepend Addin where you can use CQLinq to query the code base and get what's can be detected by the static analysis as dead code.