我运行 C++ 代码以获得代码覆盖率结果 this post .
#include <iostream>
using namespace std;
int testfunction(int input)
{
if (input > 0) {
return 1;
}
else {
return 0;
}
}
int main()
{
testfunction(-1);
testfunction(1);
}
代码覆盖率结果表明 main() 中有三个块,testfunction() 中有四个块。块是什么意思? main/testfunction 中的 3/4 块如何?
添加
当我修改代码如下时,
int main()
{
testfunction(1);
testfunction(1);
}
或如下
int main()
{
testfunction(-1);
testfunction(-1);
}
我有这个结果。
而且似乎
testfunction()
有四个块。我收到了来自 this post 的提示.
最佳答案
代码覆盖中块的技术术语是基本块。直接从 the Wikipedia entry :
The code in a basic block has one entry point, meaning no code within it is the destination of a jump instruction anywhere in the program, and it has one exit point, meaning only the last instruction can cause the program to begin executing code in a different basic block. Under these circumstances, whenever the first instruction in a basic block is executed, the rest of the instructions are necessarily executed exactly once, in order.
基本块在代码覆盖率中很重要,因为我们可以在基本块的开头插入一个探针。当这个探测器被击中时,我们知道该基本块中的所有以下指令都将被执行(由于基本块的属性)。
不幸的是,对于编译器(尤其是优化),源代码如何映射到基本块并不总是很明显。最简单的判断方法是查看生成的程序集。例如,让我们看看您原来的
main
& testfunction
:对于
main
,我看到下面的程序集(与原始来源交错)。类似于 Peter does here ,我已经注意到基本块的开始位置。int main()
{
013B2D20 push ebp <--- Block 0 (initial)
013B2D21 mov ebp,esp
013B2D23 sub esp,40h
013B2D26 push ebx
013B2D27 push esi
013B2D28 push edi
testfunction(-1);
013B2D29 push 0FFFFFFFFh
013B2D2B call testfunction (013B10CDh)
013B2D30 add esp,4 <--- Block 1 (due to call)
testfunction(1);
013B2D33 push 1
013B2D35 call testfunction (013B10CDh)
013B2D3A add esp,4 <--- Block 2 (due to call)
}
013B2D3D xor eax,eax
013B2D3F pop edi
013B2D40 pop esi
013B2D41 pop ebx
013B2D42 mov esp,ebp
013B2D44 pop ebp
013B2D45 ret
我们看到
main
具有三个基本块:一个是初始块,另外两个是因为函数调用。查看代码,这似乎是合理的。 testfunction
有点硬。单看源码,似乎分为三个块:input > 0
)return 1
) return 0
) 但是,由于实际生成的程序集,有四个块。我假设您在禁用优化的情况下构建了代码。当我在调试配置(禁用优化)中使用 VS2010 进行构建时,我看到以下对
testfunction
的反汇编:int testfunction(int input)
{
013B2CF0 push ebp <--- Block 0 (initial)
013B2CF1 mov ebp,esp
013B2CF3 sub esp,40h
013B2CF6 push ebx
013B2CF7 push esi
013B2CF8 push edi
if (input > 0) {
013B2CF9 cmp dword ptr [input],0
013B2CFD jle testfunction+18h (013B2D08h)
return 1;
013B2CFF mov eax,1 <--- Block 1 (due to jle branch)
013B2D04 jmp testfunction+1Ah (013B2D0Ah)
}
else {
013B2D06 jmp testfunction+1Ah (013B2D0Ah) <--- Not a block (unreachable code)
return 0;
013B2D08 xor eax,eax <--- Block 2 (due to jmp branch @ 013B2D04)
}
}
013B2D0A pop edi <--- Block 3 (due to being jump target from 013B2D04)
013B2D0B pop esi
013B2D0C pop ebx
013B2D0D mov esp,ebp
013B2D0F pop ebp
013B2D10 ret
在这里,我们有四个块:
如果编译器在条件 true 和条件 false 分支中复制了函数结语,您将只会看到三个块。此外,有趣的是,编译器插入了一个虚假的
jmp
说明在 013B2D06
.因为它是无法访问的代码,所以它不被视为基本块。一般来说,所有这些分析都是多余的,因为整体代码覆盖率指标会告诉您需要了解的内容。这个答案只是为了强调为什么块的数量并不总是显而易见的或预期的。
关于visual-studio-2010 - VS2010 的代码覆盖 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4958209/