CTFE in coverage reports - I finally fixed it

Stefan Koch uplink.coder at googlemail.com
Mon Jun 15 15:02:04 UTC 2020


Hi Guys,

CTFE not being in coverage reports has made 100% coverage 
unattainable
for code which has if (__ctfe) branches.

Well fret no more.
A few hours ago I've gotten this result.

---
       4|int f(int n) { int acc;
      42|    foreach(i;0 .. n)
        |    {
      10|        acc += i;
        |    }
       4|    return acc;
        |}
        |
        |
        |pragma(msg, f2(4));
        |pragma(msg, f(4));
        |pragma(msg, f2(4));
        |
        |void main()
        |{
        |    import core.stdc.stdio;
       1|    printf("%d %d %d\n", f(1), f(2), f(3));
        |}
        |
        |int f2(int n)
        |{
       2|    int acc;
      30|    foreach(i;0 .. n)
        |    {
       8|        acc += i;
        |    }
       2|    return acc;}
test_ctfe.d is 100% covered
---

the function f2() is never called at runtime at yet we are 100% 
covered.

The way it works is quite simple.
During CTFE we keep an array which counts the lines we hit.
When it comes time to do codegen we simply initialize the hidden 
__coverge symbol.
Which is a uint[]  representing the coverage counts for line 
numbers with that information rather than filling it with zeros.

Therefore no modification to the runtime is necessary at all.

It's open as DMD pr: https://github.com/dlang/dmd/pull/11279

and hopefully passing on all platforms.

Cheers,

Stefan


More information about the Digitalmars-d mailing list