分支覆盖率低,尤其是在使用第三方库时.前助推 [英] Low branch coverage especially when using 3rd party libs. ex boost

查看:530
本文介绍了分支覆盖率低,尤其是在使用第三方库时.前助推的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解C ++中的分支覆盖方面遇到问题.即使对于简单的程序,分支覆盖率也为50%.当我使用Boost时,分支覆盖率低于20%.

I have problems understanding branch coverage in c++. Even for a simple program the branch coverage is 50%. When i use boost the branch coverage is below 20%.

有人可以解释一下为什么会这样吗?我正在使用

Could someone explain me why this is happening? I am using

-fno-exceptions -g -O0 -fprofile-arcs -ftest-coverage -fPIC -Wall

编译器标志和gcovr用于生成报告.我也尝试过lcov结果完全相同

compiler flags and gcovr for generating the report. I also tried lcov with exactly the same result

推荐答案

这只是一般的C ++编译器怪异之处. gcovr和lcov都取决于GCC在目标代码中测量的覆盖率数据,并且编译器似乎在其中插入了一些分支语句.

This is just general C++ compiler weirdness. Both gcovr and lcov depend on the coverage data that GCC measures within the object code, and the compiler seems to insert some branch statements there.

我查看了在Godbolt上反汇编生成的代码,编译器的确做到了在__static_initialization_and_destruction_0部分下插入两个jne分支指令.当您使用-O1进行编译时,这些将消失.

I've looked at the disassembly of the generated code on Godbolt, and the compiler does indeed insert two jne branch instructions under a section __static_initialization_and_destruction_0. These disappear when you compile with -O1.

您应该为覆盖率测量选择哪个优化级别有些棘手.启用的优化越多,将覆盖率度量值与特定的源代码行绑定起来就越困难,因为编译器可能会浪费很多代码.但是,对于C ++,可以预期会进行一些优化,并且在编译器生成不必要的代码时会造成混淆.就像这里的情况一样.无论选择哪种级别,通常都将无法实现完整的分支机构覆盖范围.

Which optimization levels you should pick for your coverage measurements is a bit tricky. The more optimizations you enable, the more difficult it is to tie a coverage measurement to a specific source code line because the compiler may optimize much code away. But with C++, some optimizations are expected, and it's confusing when the compiler produces unnecessary code. As is the case here. Whatever level you choose, you won't generally be able to achieve full branch coverage.

gcov文档还讨论了使用gcov使用GCC优化. gcovr使用Gcov来处理原始coverage数据,因此具有相同的局限性.

The gcov documentation also discusses using gcov with GCC optimization. Gcov is used by gcovr to process the raw coverage data, and therefore has the same limitations.

但是,gcovr执行一些后处理,可以排除不包含源代码的行上的分支.在这里,给定--exclude-unreachable-branches标志时,它将忽略}行上的任何分支.

However, gcovr performs some post-processing where it can exclude branches on lines that don't carry source code. Here, this would ignore any branches on the } line when given the --exclude-unreachable-branches flag.

这篇关于分支覆盖率低,尤其是在使用第三方库时.前助推的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆