Setting Up Test Coverage With gcov For C++ Code

1,453 views
Skip to first unread message

taft...@googlemail.com

unread,
May 14, 2018, 10:20:14 AM5/14/18
to SonarQube
I am using the gcov sensor and the build-wrapper to analyse the code.  

The code is laid out in several "modules" that each has a directory in the top level.  Inside each directory is a further directory with source code and a directory with test code. 

i.e. 
./module1
./module1/source
./module1/test
./module2
./module2/source
./module2/test

Both the source and test code are C++.  GNU Make is used to run the compilation.  Each module can be compiled separately or the whole thing can be built from the top level.  Sometimes there is an Application directory if the module generates a shared library and an executable.

For the first module I can see the following warnings while the analyser runs:
10:52:12.612 DEBUG: 'Swift Squid Sensor' skipped because there is no related file in current project
10:52:12.614 DEBUG: Sensors : gcov -> SonarJavaXmlFileSensor
10:52:12.614 INFO: Sensor gcov [cpp]
10:52:12.617 INFO: Sensor gcov [cpp] (done) | time=3ms
10:52:12.618 INFO: Sensor SonarJavaXmlFileSensor [java]
10:52:12.618 INFO: Sensor SonarJavaXmlFileSensor [java] (done) | time=0ms
10:52:12.631 DEBUG: 'module1/sources/ACompiledFile.cpp' generated metadata with charset 'US-ASCII'

For the second module I see
10:52:12.799 DEBUG: 'Swift Squid Sensor' skipped because there is no related file in current project
10:52:12.800 DEBUG: Sensors : gcov -> SonarJavaXmlFileSensor
10:52:12.800 INFO: Sensor gcov [cpp]
10:52:12.801 INFO: Sensor gcov [cpp] (done) | time=1ms
10:52:12.801 INFO: Sensor SonarJavaXmlFileSensor [java]
10:52:12.801 INFO: Sensor SonarJavaXmlFileSensor [java] (done) | time=0ms
10:52:12.804 DEBUG: 'module2/source/file1.cpp' generated metadata with charset 'US-ASCII'
10:52:12.807 DEBUG: 'module2/source/file2.cpp' generated metadata with charset 'US-ASCII'
10:52:12.809 DEBUG: 'module2/source/file2.h' generated metadata with charset 'US-ASCII'
10:52:12.815 WARN: Invalid character encountered in file module2/source/file3.cpp at line 361 for encoding US-ASCII. Please fix file content or configure the encoding to be used using property 'sonar.sourceEncoding'.
10:52:12.819 DEBUG: 'module2/source/file3.cpp' generated metadata with charset 'US-ASCII'
10:52:12.820 DEBUG: 'HeliosEmulator/Tests/MyOwnNewMacros.h' generated metadata with charset 'US-ASCII'

When I check the SonarQube dashboard for the project there is no coverage data reported.  

My current sonar-project.properties file reads as:

# must be unique in a given SonarQube instance
sonar.projectKey=MYPROJ
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=My Project
sonar.projectVersion=0.0.0

# Project Link Data
sonar.projectDescription=Code that will run
sonar.links.homepage=http://intra_page

# List of the module identifiers
sonar.modules=module1,module2

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=./Sources,./Tests
module2.sonar.sources=./Application,./Sources,./Tests
sonar.cfamily.build-wrapper-output=build_output

# Exclude 3rd Party Code

# path to test source directories (optional)
#sonar.test.inclusions=Tests/

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

# Existing reports
sonar.cfamily.build-wrapper-output=build_output
#sonar.cfamily.cppunit.reportsPath=junit
sonar.cfamily.gcov.reportsPath=.
#sonar.cxx.cppcheck.reportPath=cppcheck-result-1.xml
#sonar.cxx.xunit.reportPath=cpputest_*.xml
sonar.junit.reportPaths=junit


As the above suggests; I did initially try setting things up with cppcheck and the community wrapper.  The company I work for has purchased SonarQube and has stated they would like me to use the official wrapper instead.  

The gcov files are listed in each module's root directory.  Before this I tried generating them from the top level directory and haveing all the code analysed as a single module.  I got error messages about the system being unable to find the related source files though.  I beleive this was becase gcov was being run from the top level directory, when gcc was being called from inside each module directory.  The gcov documentation states that it must be run from the same directory as GCC.


After I get the test coverage working, it would be good to get test results recorded as well.  That is a separate issue and as the code uses the CppUTest framework I do not have a xunit style report to feed in.  


Any help or advise that people can probvide will be very greatfully recieveied.  I have not managed to find much about using SonarQube with C++ at all on my websearches.  

taft...@googlemail.com

unread,
May 14, 2018, 10:31:24 AM5/14/18
to SonarQube

taft...@googlemail.com

unread,
May 14, 2018, 11:52:53 AM5/14/18
to SonarQube
I just found the community guidelines.  My apologies for not saying hello in my first post, I am rather too used to writing bug reports.  

If you would like to know more about me and my background please feel free to ask.  

Regards, Tom Thorne


On Monday, 14 May 2018 15:20:14 UTC+1, taft...@googlemail.com wrote:

taft...@googlemail.com

unread,
May 17, 2018, 11:27:07 AM5/17/18
to SonarQube
When I set the GitLab CI system I am using to keep the .gcov files as artefacts the coverage reporting suddenly started working.  

That makes some sense as the .gcov files were generated by a test job and need to be transferred to the sonar-scanner job via the artefact store.  This is because GitLab CI does not share a work area between dependent jobs and you have to explicitly say what files must be copied.  No files in the scanner job mean it cannot make a coverage report.  

Regards, Thomas
Reply all
Reply to author
Forward
0 new messages