如何使用gcov抑制内联模板
我正在使用GCC 4.9和GCOV来获取代码和分支机构覆盖范围。然而,分支覆盖的结果对我的C++代码来说完全没有用处。尽管使用了我所知道的所有-fno-*-inline
标志,但似乎GCC内联模板。如何使用gcov抑制内联模板
这里是一个小例子应用程序,说明了这个问题:
#include <string>
#include <iostream>
using namespace std;
int main() {
string foo;
foo = "abc";
cout << foo << endl;
}
我编译程序与g++ -O0 -fno-inline -fno-inline-small-functions -fno-default-inline --coverage -fprofile-arcs test.cpp -o test
运行test
,gcovr -r . -b
打印后:
------------------------------------------------------------------------------
GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File Branches Taken Cover Missing
------------------------------------------------------------------------------
test.cpp 14 7 50% 7,8,9,10
------------------------------------------------------------------------------
TOTAL 14 7 50%
------------------------------------------------------------------------------
没有一个在我们的main
函数中有单分支。例如,第7行包含string foo;
。看起来std::basic_string<...>
的构造函数中有一些if语句,但在查看main
的覆盖范围时,这不是有用的信息。
问题是,所有这些内联分支总结,并为我的实际单元测试计算分支覆盖率约为40%。我对我的代码的分支覆盖感兴趣,而不是我在C++标准库中打的多少分支。
是否有任何方法可以在编译器中完全关闭内联或者告诉GCOV不考虑内联分支?我无法在GCOV主页或其他地方找到关于该主题的任何指南。
任何帮助,非常感谢。
那么,你应该总是仔细检查你的期望。非常感谢@Useless为我指出gcov
输出本身。但是,您并不完全正确:分支不归属于test.cpp
文件。运行gcovr
与-k
并查看所有中间文件显示gcov
正确生成文件,如#usr#include#c++#4.9#bits#basic_string.h.gcov
,显示覆盖C++标准库一侧的事物。
但是,test.cpp
中所有分支的原因不是内联。这是例外。由于潜在的例外情况,每次调用标准库都是分支(例如std::bad_alloc
)。通过cat foo.cpp.gcov
打印
------------------------------------------------------------------------------
GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File Branches Taken Cover Missing
------------------------------------------------------------------------------
test.cpp 4 2 50% 10
------------------------------------------------------------------------------
TOTAL 4 2 50%
------------------------------------------------------------------------------
挖掘更深的gcov
输出:添加-fno-exceptions
到编译器标志给出了下面的输出
-: 0:Source:test.cpp
-: 0:Graph:/home/neverlord/gcov/test.gcno
-: 0:Data:/home/neverlord/gcov/test.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include <string>
-: 2:#include <iostream>
-: 3:
-: 4:using namespace std;
-: 5:
function main called 1 returned 100% blocks executed 100%
1: 6:int main() {
1: 7: string foo;
call 0 returned 1
1: 8: foo = "abc";
call 0 returned 1
1: 9: cout << foo << endl;
call 0 returned 1
call 1 returned 1
call 2 returned 1
function _GLOBAL__sub_I_main called 1 returned 100% blocks executed 100%
function _Z41__static_initialization_and_destruction_0ii called 1 returned 100% blocks executed 100%
4: 10:}
call 0 returned 1
branch 1 taken 1 (fallthrough)
branch 2 taken 0
branch 3 taken 1 (fallthrough)
branch 4 taken 0
很抱歉的噪音。
gcov输出文件显示什么?即使没有内联,所有std库代码也会归入总结中的'test.cpp',但详细输出应显示分支出现的实际功能。 – Useless