gcov 忽略源文件中的代码行

7

我正在使用gcov来计算我贡献的C++库的测试覆盖率。但是出现了一个问题,gcov没有识别出很多文件中的可执行行。在给定文件的大约160行中,它只会把40行标记为可执行。例如:

           -:    0:Source:../evo/NK.h
    -:    0:Graph:test_driver.gcno
    -:    0:Data:test_driver.gcda
    -:    0:Runs:1
    -:    0:Programs:1
    -:    1://  This file is part of Empirical, https://github.com/devosoft/Empirical
    -:    2://  Copyright (C) Michigan State University, 2016.
    -:    3://  Released under the MIT Software license; see doc/LICENSE
    -:    4://
    -:    5://
    -:    6://  This file provides code to build NK-based algorithms.
    -:    7:
    -:    8:#ifndef EMP_EVO_NK_H
    -:    9:#define EMP_EVO_NK_H
    -:   10:
    -:   11:#include <array>
    -:   12:
    -:   13:#include "../tools/BitVector.h"
    -:   14:#include "../tools/const_utils.h"
    -:   15:#include "../tools/Random.h"
    -:   16:#include "../tools/vector.h"
    -:   17:
    -:   18:namespace emp {
    -:   19:namespace evo {
    -:   20:
    -:   21:  class NKLandscape {
    -:   22:  private:
    -:   23:    const uint32_t N;
    -:   24:    const uint32_t K;
    -:   25:    const uint32_t state_count;
    -:   26:    const uint32_t total_count;
    -:   27:    emp::vector< emp::vector<double> > landscape;
    -:   28:
    -:   29:  public:
    -:   30:    NKLandscape() = delete;
    -:   31:    NKLandscape(const NKLandscape &) = delete;
    -:   32:    NKLandscape(int _N, int _K, emp::Random & random)
    -:   33:     : N(_N), K(_K)
    -:   34:     , state_count(emp::constant::IntPow<uint32_t>(2,K+1))
    -:   35:     , total_count(N * state_count)
    -:   36:     , landscape(N)
    -:   37:    {
    -:   38:      for ( auto & ltable : landscape) {
    -:   39:        ltable.resize(state_count);
    -:   40:        for (double & pos : ltable) {
    -:   41:          pos = random.GetDouble();
    -:   42:        }
    -:   43:      }
    -:   44:    }
    -:   45:    ~NKLandscape() { ; }
    -:   46:    NKLandscape & operator=(const NKLandscape &) = delete;
    -:   47:
    -:   48:    int GetN() const { return N; }
    -:   49:    int GetK() const { return K; }
    -:   50:    int GetStateCount() const { return state_count; }
    -:   51:    int GetTotalCount() const { return total_count; }
    -:   52:
    -:   53:    double GetFitness(int n, uint32_t state) const {
    -:   54:      emp_assert(state < state_count, state, state_count);
    -:   55:      return landscape[n][state];
    -:   56:    }
    -:   57:    double GetFitness( std::vector<uint32_t> states ) const {
    -:   58:      emp_assert(states.size() == N);
    -:   59:      double total = landscape[0][states[0]];
    -:   60:      for (int i = 1; i < N; i++) total += GetFitness(i,states[i]);
    -:   61:      return total;
    -:   62:    }
    -:   63:    double GetFitness(BitVector genome) const {
    -:   64:      emp_assert(genome.GetSize() == N);
    -:   65:
    -:   66:      // Use a double-length genome to easily handle wrap-around.
    -:   67:      genome.Resize(N*2);
    -:   68:      genome |= (genome << N);
    -:   69:
    -:   70:      double total = 0.0;
    -:   71:      uint32_t mask = emp::constant::MaskLow<uint32_t>(K+1);
    -:   72:      for (int i = 0; i < N; i++) {
    -:   73:        const uint32_t cur_val = (genome >> i).GetUInt(0) & mask;
    -:   74:          const double cur_fit = GetFitness(i, cur_val);
    -:   75:        total += cur_fit;
    -:   76:      }
    -:   77:      return total;
    -:   78:    }
    -:   79:  };
    -:   80:
    -:   81:}
    3:   82:}
    -:   83:
    -:   84:#endif

在这里,gcov将文件中几乎所有的行标记为非可执行代码,但是追踪到第82行代码执行了3次: 单个闭合括号。

对我来说这没有任何意义,我在网上也找不到任何关于这个问题的信息。如有帮助,将不胜感激。

1个回答

8
这是一个关于 gcov(以及相关软件如 gcovr 和 lcov)行为的草图流程图:
当编译器(GCC)生成目标代码并被要求插入覆盖率/分析工具时,它会做两件额外的事情:
1. 目标代码被插装以在执行时将覆盖度量写入 .gcda 文件。 2. 生成一个 .gcno 文件,描述了目标代码的结构。
然后,gcov 实用程序解析 .gcda 和 .gcno 文件以计算覆盖度量。对于带注释的源报告,它还会读取源文件。
因为是编译器确定目标代码的哪部分对应于特定的行,所以您展示的报告是正确的:那一行不存在。更准确地说:没有为这些源代码行生成目标代码。这通常是预期的行为,因为许多源代码行只是编译时声明。
在您的情况下,您有一个带有内联函数(类定义中的任何函数定义都是隐式内联的)的C++类。编译器不需要为未使用的内联函数生成代码。如果使用非内联函数,即在头文件中声明函数并在.cpp文件中提供实现,则情况将有所不同。
那么三个闭合大括号的执行情况如何呢?编译器通常需要发出一些与静态对象的初始化和清理相关的代码。这些代码与特定行没有真正关联,因此出现在您编译单元的最后一行的一部分。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接