2016-09-21 68 views
7

我正在使用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

運行testgcovr -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主頁或其他地方找到關於該主題的任何指南。

任何幫助,非常感謝。

+2

gcov輸出文件顯示什麼?即使沒有內聯,所有std庫代碼也會歸入總結中的'test.cpp',但詳細輸出應顯示分支出現的實際功能。 – Useless

回答

4

那麼,你應該總是仔細檢查你的期望。非常感謝@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 

很抱歉的噪音。