Zig NEWS

LI Yu
LI Yu

Posted on

Tiny change to Kcov for better covering zig

Recently just engaged with Zig, and I really like it. Strongly believe in that "Favor reading code over writing code." So I start to do something with it, and then I start to wonder how to use coverage tools.

Then I have read this wonderful article here
https://zig.news/squeek502/code-coverage-for-zig-1dk1, and learned kcov as such as amazing tool: fast and easy to use, and it works without any headache.

There is only one thing letting me down, which is that I do not find any instructions in kcov on how to ignore some lines in source code for not covering.

And as Zig has unreachable and @panic which should never be covered, so I spend some time wondering in the cpp code base of kcov.

Turns out, it is easier than I image, so I forked kcov and create my own version.

  • It can recognise zig's unreachable and @panic, auto ignore it.
  • I also added the ability to add /* no-cover */ in c/cpp source code so that can make kcov ignore some lines too.

The result is quite satisfying to myself, like

this one auto ignoring unreachable

kcov auto ignoring zig unreachable

or this one auto ignoring @panic

kcov auto ignoring zig @panic

or this one manual ignoring line with /* no-cover */

kcov ignoring line with comment no-cover

The repo is here:
https://github.com/liyu1981/kcov/tree/kcov-zig

Hope it is useful for not just me :)

Top comments (4)

Collapse
 
kristoff profile image
Loris Cro

Awesome writeup, thank you for sharing. I guess the next step would be to add a build.zig file to kcov :^)

Collapse
 
liyu1981 profile image
LI Yu

did my try, and now with a build.zig works on macos M1 at here: github.com/liyu1981/kcov/tree/buil.... Zig is awesome.

meanwhile found an tricky part of Zig: zig debug build will make kcov stop working while CMake debug will keep it working. Write down my findings as an issue here: github.com/ziglang/zig/issues/18521

Collapse
 
liyu1981 profile image
LI Yu

The issue: github.com/ziglang/zig/issues/18521 is resolved by adding "-fno-sanitize=undefined" to cflags. Now this build.zig works smoothly to compile kcov with zig!

Collapse
 
liyu1981 profile image
LI Yu

This sounds like a more challenge practice to me on getting familiar with build.zig. I will try :)