Codejie's C++ Space

Using C++

Why the inline function can not be covered?

Please have a look the following codes, they include two source files (source.h, source.c), one test file (test_source.c) and one makefile file.

 1 #ifndef __SOURCE_H__
 2 #define __SOURCE_H__
 4 #include <stdio.h>
 6 inline void inline_test()
 7 {
 8     printf("\nthis is the inline function.");
 9 }
11 void normal_test();
13 #endif

1 #include "source.h"
3 void normal_test()
4 {
5     printf("\nThis is a normal function.");
6 }

 1 #include "source.h"
 3 void test_inline() 
 4 {
 5     inline_test();
 6 }
 8 void test_normal()
 9 {
10     normal_test();
11 }
13 int main()
14 {
15     test_inline();
16     test_normal();
18     return 0;
19 }

 1 TEST = test_source.o
 3 TARGET = a.exe
 5 all: $(TARGET)
 8     gcc -Xlinker --allow-multiple-definition $^ -fprofile-arcs -ftest-coverage -o $(TARGET)    
10 $(SOURCE): %.o:%.c
11     gcc -MM -MF $(subst .o,.d,$@) -MT "$@ $(subst .o,.d,$@)" $<
12     gcc -c $< -o $@ -fprofile-arcs -ftest-coverage
14 $(TEST): %.o:%.c
15     gcc -MM -MF $(subst .o,.d,$@) -MT "$@ $(subst .o,.d,$@)" $<
16     gcc -c $< -o $@   
18 clean:
19     -rm -rf $(DIR_COVERAGE)
22     -rm -rf *.d *.o *.gcda *.gcno $(TARGET)


My purpose is that get the UT line coverage report of source files by running the test file. I guess I should get the below report of line coverage, because I have called every functions of source file in test file, include the inline function and the normal function.

But after run test file, I got the report is just 50%, the inline function has not been covered. Why? Why it is not be covered? I am sure it is called in test file.
OK, Calming down. If it is not covered, there are two reason, one is that the inline function is not be called, of course it is impossible, because we did it. So the root cause must be the second one that gcov/lcov tools do not process those lines of inline function. Have any idea? gcov/lcov tools process all files with compilation parameters '-fprofile-arcs -ftest-coverage', so let's check makefile file again.
Look here, source files have the compilation paraments, but why normal function has been coverage, but inline function not?

$(SOURCE): %.o:%.c
    gcc -MM -MF $(subst .o,.d,$@) -MT "$@ $(subst .o,.d,$@)" $<
    gcc -c $< -o $@ -fprofile-arcs -ftest-coverage

Thinking again, as we knew, the difference between inline function and normal function is just that inline function will be inserted source code while compiling period. Yes, the answer is here! While compiling, the inline function is put into test file, but the test file has not '-fprofile-arcs -ftest-coverage' paramenter. So, so it was not covered.

$(TEST): %.o:%.c
    gcc -MM -MF $(subst .o,.d,$@) -MT "$@ $(subst .o,.d,$@)" $<
    gcc -c $< -o $@

To fix it, just add the parameter to test file while compiling. OK, let's update the makefile file like the below line.

$(TEST): %.o:%.c
    gcc -MM -MF $(subst .o,.d,$@) -MT "$@ $(subst .o,.d,$@)" $<
    gcc -c $< -o $@ -fprofile-arcs -ftest-coverage    

Done, enjoy it!

posted on 2012-09-21 17:45 codejie 阅读(2723) 评论(2)  编辑 收藏 引用 所属分类: C++随笔而已


# re: Why the inline function can not be coveraged? 2012-09-26 19:35 smagle

gcov原理分析详细可参考:  回复  更多评论   

# re: Why the inline function can not be coveraged?[未登录] 2012-09-26 22:16 codejie

very great article. thanks!  回复  更多评论   

【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


Using C++