覆盖数据中的意外分支计数

时间:2016-07-20 10:35:58

标签: c++ code-coverage gcov

我生成覆盖数据并使用在线服务进行可视化和CI。但是我有些奇怪的"偏见" (if-branches未完全覆盖)。查看gcov数据,似乎检测到了过多的分支。

一个例子:

function _ZNK5World8AdjustBQE5PointItEh15BuildingQuality called 415 returned 100% blocks executed 76%
      415:  377:BuildingQuality World::AdjustBQ(const MapPoint pt, unsigned char player, BuildingQuality nodeBQ) const
        -:  378:{
      415:  379:    if(nodeBQ == BQ_NOTHING || GetNode(pt).owner != player + 1 || !IsPlayerTerritory(pt))
branch  0 taken 95% (fallthrough)
branch  1 taken 5%
call    2 returned 100%
call    3 returned 100%
branch  4 taken 100% (fallthrough)
branch  5 taken 0% (throw)
branch  6 taken 85% (fallthrough)
branch  7 taken 15%
call    8 returned 100%
call    9 returned 100%
branch 10 taken 100% (fallthrough)
branch 11 taken 0% (throw)
branch 12 taken 17% (fallthrough)
branch 13 taken 83%
branch 14 taken 80% (fallthrough)
branch 15 taken 20%
branch 16 taken 95% (fallthrough)
branch 17 taken 5%
branch 18 taken 33% (fallthrough)
branch 19 taken 67%
branch 20 never executed
branch 21 never executed
branch 22 never executed
branch 23 never executed
call   24 never executed

虽然我已经使用-g -O0 --coverage -fno-default-inline -fno-inline进行编译,但似乎函数仍然被内联和计算。我能做些什么,所以我只能得到有意义的"数据和内联函数在其定义中是否正确归因?

我使用g ++ - 4.8和gcov-4.8

编辑:如果我将if拆分为其调用,我会得到:

      415:  379:    unsigned char owner = GetNode(pt).owner;
call    0 returned 100%
call    1 returned 100%
branch  2 taken 100% (fallthrough)
branch  3 taken 0% (throw)
call    4 never executed
      415:  380:    bool isPlayerTer = IsPlayerTerritory(pt);
call    0 returned 100%
call    1 returned 100%
branch  2 taken 100% (fallthrough)
branch  3 taken 0% (throw)
call    4 never executed
      415:  381:    if(nodeBQ == BQ_NOTHING || owner != player + 1 || !isPlayerTer)
branch  0 taken 95% (fallthrough)
branch  1 taken 5%
branch  2 taken 85% (fallthrough)
branch  3 taken 15%
branch  4 taken 17% (fallthrough)
branch  5 taken 83%

所以问题实际上就是电话。我该怎么做才能将这些电话(及其分支机构(???))添加到覆盖数据中?

刚刚在asm代码中发现了一个人工分支,只是检查寄存器并执行nop(除了gcov计数器增量)WTF?

   0x0000000000d967de <World::AdjustBQ(Point<unsigned short>, unsigned char, BuildingQuality) const+340>:   test   %bl,%bl
   0x0000000000d967e0 <World::AdjustBQ(Point<unsigned short>, unsigned char, BuildingQuality) const+342>:   je     0xd967f5 <World::AdjustBQ(Point<unsigned short>, unsigned char, BuildingQuality) const+363>
   0x0000000000d967e2 <World::AdjustBQ(Point<unsigned short>, unsigned char, BuildingQuality) const+344>:   nop
   0x0000000000d967e3 <World::AdjustBQ(Point<unsigned short>, unsigned char, BuildingQuality) const+345>:   mov    0x1206c9e(%rip),%rax        # 0x1f9d488 <__gcov0._ZNK5World8AdjustBQE5PointItEh15BuildingQuality+72>
   0x0000000000d967ea <World::AdjustBQ(Point<unsigned short>, unsigned char, BuildingQuality) const+352>:   add    $0x1,%rax
   0x0000000000d967ee <World::AdjustBQ(Point<unsigned short>, unsigned char, BuildingQuality) const+356>:   mov    %rax,0x1206c93(%rip)        # 0x1f9d488 <__gcov0._ZNK5World8AdjustBQE5PointItEh15BuildingQuality+72>
   0x0000000000d967f5 <World::AdjustBQ(Point<unsigned short>, unsigned char, BuildingQuality) const+363>:   test   %dl,%dl

我切换到clang ++和llvm-cov gcov并获得了这种情况的正确结果(只有6个分支)。不过我在其他地方也有一些误报。其中一个最令人不安的似乎是&#34;函数__cxx_global_array_dtor call&#34;在一条线上的函数中间,类似的线(只更改整数参数)没有这个。所以我目前的解决方案是使用没有分支数据的gcov-4.8,因为它完全不可靠。

1 个答案:

答案 0 :(得分:0)

有可能在点处有额外的分支,可能会发生异常,例如在功能调用点。

例如,我们在我们的(不是noexcept)空虚拟析构函数中观察到了一个带有gcov的分支,我们的测试无法覆盖它。