gcov 函数未执行,但行已执行

gcov function is not executed but lines are

我一直在使用 gcov 检查代码覆盖率。我现在得到一个奇怪的结果,一些函数被列为未执行但函数中的行被列为已执行。

lcov 输出:

  13           0 :     f<double>& operator*=(f<double>& lhs, const double& rhs)
  14             :     {
  15           9 :         // Some code...
  16           0 :         return lhs;
  17             :     }

gcov 输出:

#####:   13:    f<double>& operator*=(f<double>& lhs, const double& rhs)
    -:   14:    {
    9:   15:        // Some code...
#####:   16:        return lhs;
    -:   17:    }

当行是时,如何将函数列为未执行?这可能是 GCC 优化的产物吗?

编辑 1:

G++/gcov 版本:4.9.2

lcov 版本:1.11

编辑 2:

-O2 比较会产生奇怪的行为。但是,使用 -O1 进行编译会将 #### 更改为 9

零仅表示 gcov 没有代码行的数据。这可能发生只是因为 gcc 没有方便的方法来 instrument — 修改您的程序以添加计数器(如在函数的入口中)。例如,如果优化器删除或组合附近函数中的类似语句,则没有什么可计算的。

根据 manual:

The format is

 execution_count:line_number:source line text

Additional block information may succeed each line, when requested by command line option. The execution_count is ‘-’ for lines containing no code. Unexecuted lines are marked ‘#####’ or ‘====’, depending on whether they are reachable by non-exceptional paths or only exceptional paths such as C++ exception handlers, respectively.

延伸阅读:

  • What are your tips for interpreting gcov output in order to improve coverage?
  • interpret gcov output to identify basic blocks

您应该 运行 gcov 未优化的代码。这将为您提供输出,可能由 lcov 解释,这将向您保证所有行都有 运行.

然后您可以针对发布版本进行优化编译。