# Swift | xccov-to-sonarqube-generic.sh does not handle branch coverage

**URL:** <https://community.sonarsource.com/t/swift-xccov-to-sonarqube-generic-sh-does-not-handle-branch-coverage/143795>\
**Category:** SonarQube Cloud\
**Tags:** swift, coverage\
**Created:** [July 2, 2025, 11:16am UTC](https://community.sonarsource.com/t/swift-xccov-to-sonarqube-generic-sh-does-not-handle-branch-coverage/143795 "2025-07-02T11:16:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlos-santos](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/carlos-santos/32/70470_2.png) [@carlos-santos](https://community.sonarsource.com/u/carlos-santos)\
**Post date:** [July 2, 2025, 11:16am UTC](https://community.sonarsource.com/t/swift-xccov-to-sonarqube-generic-sh-does-not-handle-branch-coverage/143795/1 "2025-07-02T11:16:16Z")

</div>

ℹ I’m not sure who [owns this repo](https://github.com/SonarSource/sonar-scanning-examples), but it contains the only way I have found to convert **Xcode** ’s code coverage into a generic Sonarqube format (please advise if there’s a better way).

* * *

I would like to discuss something that can be improved in [this specific script](https://github.com/SonarSource/sonar-scanning-examples/blob/master/swift-coverage/swift-coverage-example/xccov-to-sonarqube-generic.sh).

I opened an issue explaining what I found, while converting an `.xcresult`’s generated from from Xcode.

> <https://github.com/SonarSource/sonar-scanning-examples/issues/206>
>
> Hi,
> 
> AFAIK, the \[xccov-to-sonarqube-generic.sh\](https://github.com/SonarSource/s…onar-scanning-examples/blob/master/swift-coverage/swift-coverage-example/xccov-to-sonarqube-generic.sh) is not accounting with \*\*multiple code branches in the same line\*\*, like ternary operators of \`nil\` coalescing fallbacks.
> 
> e.g.:
> 
> Using this snippet we can see that we have 2 different code paths:
> 
> \`\`\`swift
> testBool(optionalBool ?? false)
> \`\`\`
> 
> 1. \`optionalBool\` exists and its value is used
> 1. \`optionalBool\` is \`nil\` and we are coalescing to \`false\`
> 
> From a random export of \`xcrun xccov view --archive App.xcresult\`:
> (this is not a match from the snippet above, only an example)
> 
> \`\`\`
> /Users/Random/work/app-ios/Sources/Model.swift:
> ...
> 59: 0
> 60: 0
> 61: 2 \[
> (1, 9, 0)
> \]
> 62: 2
> 63: 2
> 64: 8 \[
> (1, 67, 2)
> (91, 0, 2)
> \]
> 65: 2
> 66: 2
> 67: 4 \[
> (1, 28, 2)
> (97, 0, 2)
> \]
> 68: 2 \[
> (87, 1, 0)
> \]
> 69: 2
> 70: 2 \[
> (77, 2, 1)
> \]
> 71: 2
> 72: 2
> 73: \*
> ...
> \`\`\`
> 
> Looking at the conversion script, it only cares about:
> 
> \`\`\`
> 69: 2
> 70: 2
> \`\`\`
> 
> But there's extra info on the next line that is important to confirm if we have covered all the possible code branches:
> 
> \`\`\`
> 70: 2 \[
> (77, 2, 1)
> \]
> \`\`\`
> 
> Questions:
> 
> 1. As of today, is that script the official way of reporting Swift code coverage from an \`xcresult\`?
> 1. If so, shouldn't it support different code branches?
> 
> Thanks.

This script is analyzing if the line had a hit or not, but completely ignores branch coverage, for other code branches in the same line, like ternary operators, or nil coalescing fallbacks.

I’m not sure if this was on purpose, but I guess that Sonarqube supports this, I can see on the [official docs](https://www.sonarsource.com/blog/sonarqube-code-coverage/) an image with the red/white striped pattern:

 ![sonarqube branch coverage](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/e/a/ea0bac6eb1fc24037be8b65225a0576570953409.png)

Also, I can see that this is supported by the [xsd definition](https://docs.sonarsource.com/sonarqube-server/9.6/analyzing-source-code/test-coverage/generic-test-data/#generic-test-coverage).

My question is:

- Shouldn’t this script support branch coverage?
- If so, and if Sonar owns the repo, can we expect the script to be updated?

It should require a refactor, because it needs to analyze multiple lines of the output, but it’s not very complex.

Thanks in advance.

### Details

- ALM used - GitHub
- CI system used: GitHub actions
- Scanner command used when applicable: [GitHub - SonarSource/sonarqube-scan-action](https://github.com/SonarSource/sonarqube-scan-action)
- Languages of the repository: Swift

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [July 3, 2025, 8:31am UTC](https://community.sonarsource.com/t/swift-xccov-to-sonarqube-generic-sh-does-not-handle-branch-coverage/143795/2 "2025-07-03T08:31:17Z")

</div>

Hey @carlos-santos

- Yes, we (Sonar) own this repo
- It’s an example that lots of customers are using
- SonarQube’s [generic coverage format](https://docs.sonarsource.com/sonarqube-cloud/enriching/test-coverage/generic-test-data/) supports branch coverage, so if the source data (`xccov`) has that data, like you said it’s simply a matter of updating the script

It’s not really on our radar. But it’s open source, and we accept PRs. 🙂

---

<div class="post-metadata">

**Author:** ![Alexandre\_Gigleux](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/alexandre_gigleux/32/26_2.png) [@Alexandre\_Gigleux](https://community.sonarsource.com/u/Alexandre_Gigleux)\
**Post date:** [August 19, 2025, 8:21am UTC](https://community.sonarsource.com/t/swift-xccov-to-sonarqube-generic-sh-does-not-handle-branch-coverage/143795/6 "2025-08-19T08:21:43Z")

</div>

Hello,

Thank you for your interest in loading your coverage data into SonarQube.

We are currently in the process of rewriting our Swift analyzer to provide improved support for the latest versions of Swift. As part of this effort, we will also be revisiting our approach to test and coverage data integration. While I cannot guarantee that these enhancements will be completed in the coming weeks, we would greatly appreciate any contributions from the community. If you are interested in enhancing the xccov-to-sonarqube-generic.sh script, we warmly welcome your pull requests.
