Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

c++ - gcov is not generating coverage information for header files

I'm using gcov for the first time and I'm having a problem which is similar to the one reported in this thread. However, I wasn't able to solve my problem following the comments in that thread.

I'm trying to measure the coverage of a concrete test case in KMyMoney-4.6.4, namely "testConstructor" in the test file "mymoneyaccounttest.cpp". This test case is using the following method, which is in the header file "mymoneyaccount.h" and has executable code:

 const QString& institutionId(void) const {
    return m_institution;
  }

So, I build the program and the tests, and then execute: gcov mymoneyaccount.cpp

Among the coverage information displayed, I obtain:

... File 'mymoneyaccount.cpp' Lines executed:8.91% of 393 Creating 'mymoneyaccount.cpp.gcov'

File 'mymoneyaccount.h' Lines executed:25.00% of 4 Creating 'mymoneyaccount.h.gcov' ...

The coverage information in "mymoneyaccount.cpp.gcov" is ok. In contrast, "mymoneyaccount.h.gcov" shows:

    6:   81:class KMM_MYMONEY_EXPORT MyMoneyAccount : public MyMoneyObject, public MyMoneyKeyValueContainer
    ...
    -:  192:  const QString& institutionId(void) const {
    -:  193:    return m_institution;
    -:  194:  }
    ...
    -:  272:  int accountCount(void) const {
#####:  273:    return m_accountList.count();
    -:  274:  };

Maybe I'm wrong, but this result means that gcov is not considering "return m_institution;" as executable code while does it considering "return m_accountList.count()" as executable code. Moreover, it shows "6" in the line of the class declaration, so this file has coverage information but not what I was expecting.

I have to say that maybe the problem has to do with the names of the files and the directories:

1- The object files are created in "CMakefiles/kmm_mymoney.dir" ending with ".cpp.o", for instance, mymoneyaccount.cpp.o. Thus, the "gcno" and "gcda" files are created with the name "mymoneyaccount.cpp.gcno" and "mymoneyaccount.cpp.gcda" (of course, in the directory "CMakefiles/kmm_mymoney.dir").

2- When I execute: gcov -o CMakeFiles/kmm_mymoney.dir mymoneyaccount.cpp gcov gives the following error:

mymoneyaccount.gcno:cannot open notes file

So I have to rename those files: mv mymoneyaccount.cpp.gcno mymoneyaccount.gcno mv mymoneyaccount.cpp.gcda mymoneyaccount.gcda

Finally, I have another question. When I execute gcov in the file containing the test cases, I mean, "gcov mymoneyaccounttest.cpp" instead of "gcov mymoneyaccount.cpp", I also obtain a "mymoneyaccount.h.gcov" file, but the coverage information is even worse:

#####:   81:class KMM_MYMONEY_EXPORT MyMoneyAccount : public MyMoneyObject, public MyMoneyKeyValueContainer

Anyway, the question is: should I execute "gcov" on the implementation file "mymoneyaccount.cpp" or in the test file "mymoneyaccounttest.cpp"?

Sorry for the length. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thank you very much Tony!! That link had the solution. I just changed the optimization from -O2 to -O0 and now the result is:

1: 192: const QString& institutionId(void) const { 
1: 193:     return m_institution; 
-: 194: } 

I have to note that to obtain "mymoneyaccount.cpp.gcov" I execute "gcov mymoneyaccount.cpp", but to obtain "mymoneyaccount.h.gcov" I have to execute "gcov mymoneyaccounttest.cpp". Do you know why? Both files include "mymoneyaccount.h".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...