如何在同时运行lcov程序的情况下生成代码覆盖率

时间:2019-07-03 06:56:19

标签: lcov

我有一个大型项目,在其他计算机上运行单元测试二进制文件。因此,gcda文件是在其他计算机上生成的。然后,我将它们下载到本地计算机,但目录不同。每个目录都有源代码。

例如:dir gcda1/src/{*.gcda, *.gcno, *.h, *.cpp}...,dir gcda2/src/{*.gcda, *.gcno, *.h, *.cpp}...

因为项目很大,所以我必须同时运行多个lcov进程来生成信息文件以节省时间。然后合并这些信息文件。

问题是,当我合并这些信息文件时,它将带有目录信息,例如:

gcda1/src/unittest1.cpp
gcda2/src/unittest1.cpp

我想要这个:

src/unittest1.cpp
#src/unittest1.cpp # this is expected to merge with above

我使用的命令:

$ cd gcda1
$ lcov --rc lcov_branch_coverage=1 -c -d ./ -b ./ --no-external -o gcda1.info
$ cd ../gcda2
$ lcov --rc lcov_branch_coverage=1 -c -d ./ -b ./ --no-external -o gcda2.info
$ cd ..
$ lcov -a gcda1/gcda1.info -a gcda1/gcda2.info -o gcda.info
$ genhtml gcda.info -o output

根目录包含源代码。

1 个答案:

答案 0 :(得分:0)

说明

好吧,我终于找到了解决这个问题的方法。

生成的信息文件lcov是纯文本文件。这样我们就可以直接对其进行编辑。

打开这些文件后,您将看到每个文件行都以SF开头。如下所示:

SF:/path/to/your/source/code.h
SF:/path/to/your/source/code.cpp
...

问题

在我的问题中,这些将是:

// file gcda1.info
SF:/path/to/root_dir/gcda1/src/unittest1.cpp
// file gcda2.info
SF:/path/to/root_dir/gcda2/src/unittest1.cpp

lcov合并后,它将是:

// file gcda.info
SF:/path/to/root_dir/gcda1/src/unittest1.cpp
SF:/path/to/root_dir/gcda2/src/unittest1.cpp

但是,我希望这样:

// file gcda.info
SF:/path/to/root_dir/src/unittest1.cpp

方法

我解决问题的方法是直接编辑信息文件。

首先,编辑gcda1.infogcda2.info,将/path/to/root_dir/gcda1/src/unittest1.cpp更改为/path/to/root_dir/src/unittest1.cpp,将/path/to/root_dir/gcda2/src/unittest1.cpp更改为/path/to/root_dir/src/unittest1.cpp

然后按如下所示合并它们并生成html报告:

$ lcov -a gcda1.info -a gcda2.info -o gcda.info
$ genhtml gcda.info -o output

在大型项目中,我们无法手动编辑每个信息文件,否则您将崩溃。

我们可以使用sed来帮助我们。如下所示:

$ sed "s/\(^SF.*\/\)gcda[0-9]+\/\(.*\)/\1\2/g" gcda_tmp.info > gcda.info
相关问题