找出在 Visual Studio 2012 年的 C++ 项目中从未调用过哪些代码

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

我正在使用 VS2012,我想知道我的项目中的哪些代码从未被调用过。我该怎么做?

这是我试过的用于死代码分析的菜单,但在这里没有找到。

简答:Visual Studio不支持这个1.

可以找到未调用函数的代码分析检查器仅适用于托管(即 .NET)代码,例如CA1811: Avoid uncalled private code.

C++代码的静态分析要难很多,和unused/redundant/unreachable代码相关的Code Analysis for C/C++ Warnings屈指可数:

  • C6235: "( || <表达式>) 始终是 non-zero 常量"
  • C6236: "(<表达式> || ) 始终是 non-zero 常量"
  • C6237: "( && ) 始终为零。 永远不会被计算并且可能有副作用"
  • C6239: "( && ) 始终计算为 的结果。您是否打算使用 bitwise-and运算符?"
  • C6240: "( && ) 的计算结果总是 的结果。您是否打算使用 bitwise-and运算符?"
  • 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 常量。您是否打算使用 bitwise-and 运算符?"
  • C6286: "( || ) 始终是 non-zero 常量。 永远不会被计算,并且可能有副作用
  • 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"

所有这些规则要么表明存在错误,要么指向永远不会执行的冗余代码。该列表适用于 Visual Studio 2017 年实施的代码分析规则。Visual Studio 之前的版本可能不会为所有规则提供检查器。


1 直到并包括 Visual Studio 2017,这是撰写本文时的最新版本。

您可以尝试 CppDepend 加载项,您可以在其中使用 CQLinq 查询代码库并获取静态分析可以检测为死代码的内容。