【发布时间】:2011-11-04 04:32:24
【问题描述】:
当我使用 gcov 测量 C++ 代码的测试覆盖率时,它会报告析构函数中的分支。
struct Foo
{
virtual ~Foo()
{
}
};
int main (int argc, char* argv[])
{
Foo f;
}
当我在启用分支概率 (-b) 的情况下运行 gcov 时,我得到以下输出。
$ gcov /home/epronk/src/lcov-1.9/example/example.gcda -o /home/epronk/src/lcov-1.9/example -b
File 'example.cpp'
Lines executed:100.00% of 6
Branches executed:100.00% of 2
Taken at least once:50.00% of 2
Calls executed:40.00% of 5
example.cpp:creating 'example.cpp.gcov'
困扰我的部分是“至少服用一次:2 的 50.00%”。
生成的 .gcov 文件提供了更多详细信息。
$ cat example.cpp.gcov | c++filt
-: 0:Source:example.cpp
-: 0:Graph:/home/epronk/src/lcov-1.9/example/example.gcno
-: 0:Data:/home/epronk/src/lcov-1.9/example/example.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:struct Foo
function Foo::Foo() called 1 returned 100% blocks executed 100%
1: 2:{
function Foo::~Foo() called 1 returned 100% blocks executed 75%
function Foo::~Foo() called 0 returned 0% blocks executed 0%
1: 3: virtual ~Foo()
1: 4: {
1: 5: }
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
call 2 never executed
call 3 never executed
call 4 never executed
-: 6:};
-: 7:
function main called 1 returned 100% blocks executed 100%
1: 8:int main (int argc, char* argv[])
-: 9:{
1: 10: Foo f;
call 0 returned 100%
call 1 returned 100%
-: 11:}
注意“分支 0 采用 0% (fallthrough)”这一行。
是什么导致了这个分支,我需要在代码中做什么才能在此处获得 100%?
- g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
- gcov (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
【问题讨论】:
-
我如何在这里获得 100% 仍然没有答案。
-
查看我的更新答案,详细了解这里发生的情况。
-
这是检测低级代码(根据语言语义插入分支)而不是直接检测源代码的结果。 GCov这样做是因为它方便GCov,而不是因为它对你有帮助;您了解支持可能经过良好测试的编译器的编译器生成的分支的测试覆盖率没有任何价值。如果你得到一个检测源的测试覆盖率工具,你就不会得到这种虚假的覆盖率数据。 (查看我的简历中的一个选项)。
标签: c++ gcc code-coverage gcov