Can't get llvm-cov reports to show up in SonarQube project

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)

I am using SonarQube Developer edition v7.7 with sonar-scanner v4.2

  • what are you trying to achieve

I have a simple project consisting of ~200 lines of code with google tests. I have an html page successfully generated from llvm-cov showing 87% code coverage. I am trying to upload this coverage report to SonarQube but all attempts have lead to 0% coverage on the SonarQube project page.

  • what have you tried so far to achieve this

I have tried the following:

  • llvm-cov show ./MyTest -instr-profile=coverage.profdata -format=html -output-dir=coverage
  • llvm-cov export ./MyTest-instr-profile=coverage.profdata > coverage.json
  • llvm-cov show ./MyTest -instr-profile=coverage.profdata -show-expansions -show-regions -show-line-counts -use-color-false > coverage.txt

The SonarQube documentation does not specify what format the report needs to be in order to be parsed by SonarQube. The actual security and vulnerability scanning works fine. I just can’t seem to get the coverage report to show greater than 0%.

I run the following command with the following relevant output I receive from the scanner:

> build-wrapper-Linux-x86-64 --out-dir sonarqube make -j8
> sonar-scanner -Dsonar.projectKey=MyProject -Dsonar.sources=src -Dsonar.tests=test -Dsonar.cfamily.build-wrapper-output=sonarqube -Dsonar.cfamily.llvm-cov.reportPath=build/bin/coverage.txt -Dsonar.host.url=https://my.url.net -Dsonar.login=my-hash-login
…
INFO: Sensor llvm-cov [cpp]
INFO: Parsing /builds/myproject/build/bin/coverage.txt
INFO: Sensor llvm-cov [cpp] (done) | time=169ms
...

I’m assuiming I’m not feeding the scanner correctly formatted llvm-cov code coverage. Is there a specific format I need to be using or am I doing something obviously incorrect?

Thank you

Hi @amshelley86,

could you please share coverage.txt file? If you prefer to do it privately I can send you a private message.

I don’t have access to the files on my internet machine but I can give a short snippet of the coverage.

I ran the following commands to produce the coverage:

> LLVM_PROFILE_FILE="coverage.profraw" ./MyTest
> llvm-profdata merge -sparse coverage.profraw -o coverage.profdata
> llvm-cov show ./MyTest -instr-profile=coverage.profdata > coverage.txt

The above generated the following:

/path/to/source/file.cpp:
    1|        |#include <gtest/gtest.h>
    2|        |
    3|        |#include <chrono>
    4|        |#include <iostream>
    5|        |#include <thread>
    6|        |
    7|        |void foo()
    8|       2|{
    9|       2|std::this_thread::sleep_for(std::chrono::seconds(1));
   10|       2|}
...

The rest of this file continues with additional source files followed by additional lines of codes marked with the number of times they were hit.

Hi @amshelley86,

the format is ok.

Are you building and analyzing on the same machine?
Do the absolute paths in the report match the files in the machine?

So I am actually running all of this through a GitLab/kubernetes/docker pipeline. Specifically, this coverage test runs in a docker container for a specific stage. All of this occurs in a container within a working directory. The working directory is /builds/mygroup/myproject where binaries are built (configured with CMake) in /builds/mygroup/myproject/build/bin and each file path listed within the coverage.txt file is /builds/mygroup/myproject/test/library/file.cpp

Massimo,
Thank you for the help. After my last post, I think I realized what was happening. As I am working with a demo/learning project for SonarQube, the tests were not correctly written. Therefore, the files listed in my coverage.txt file did not reference the actual source files that I pointed the sonar-scanner to. Once I changed the scanner to point to my test files that the coverage.txt references, I was able to see coverage percentage show up in SonarQube.

1 Like

Hi @amshelley86,

thank you for the update and glad you found the issue.

A post was split to a new topic: How to import llvm-cov data