Skip to content

[LLVM-COV] wrong coverage for std library function call #148623

@8ss-boop

Description

@8ss-boop

llvm version:
clang version 21.0.0git (https://github.com/llvm/llvm-project.git 872eac7)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /root/software/llvm-releases/llvm-0704/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64

#include <stdatomic.h>
#include <stdio.h>
int main() {
    atomic_int ai = 42;  
    int val = atomic_load(&ai);
    int a = val + 10;
    int b = (a * 2) / (a != 0 ? a : 1);
    for (int i = 0; i < b && i < 50; ++i) { 
        printf("i=%d\n",i);
        if (i % 3 == 0)
            atomic_fetch_add(&ai, i);
    }
    printf("Final atomic value: %d\n", atomic_load(&ai));
    return 0;
}

for the program above, we got the following coverage report from llvm-cov

/*
clang -std=c2x -lm -fcoverage-mapping -fprofile-instr-generate test.c
./a.out
llvm-profdata merge default.profraw  -o default.profdata;
llvm-cov show --show-line-counts -instr-profile=default.profdata ./a.out > test.c.lcov
*/

    1|       |#include <stdatomic.h>
    2|       |#include <stdio.h>
    3|      1|int main() {
    4|      1|    atomic_int ai = 42;  
    5|      1|    int val = atomic_load(&ai);
    6|      1|    int a = val + 10;
    7|      1|    int b = (a * 2) / (a != 0 ? a : 1);
    8|      3|    for (int i = 0; i < b && i < 50; ++i) { 
    9|      2|        printf("i=%d\n",i);
   10|      2|        if (i % 3 == 0)
   11|      2|            atomic_fetch_add(&ai, i);
   12|      2|    }
   13|       |    printf("Final atomic value: %d\n", atomic_load(&ai));
   14|      1|    return 0;
   15|      1|}

the output is:

i=0
i=1
Final atomic value: 42

so the line 11 should be executed only once instead of twice
btw, no coverage count was given for line 13 which is rare for llvm

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions