llvm / llvm/llvm-project

[llvm-cov] wrong coverage with destructor attribute

Open
#173,233 0 comments 0 reactions 0 assignees View on GitHub
coverage
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Target compiler:

```
$ clang-trunk -v
clang version 22.0.0git (https://github.com/llvm/llvm-project.git a8dc57f0d7253df172b554dc3e3f4790913d578d)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /compiler/llvm-trunk/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
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64
```

Case:

```
$ cat small.c
int a;
__attribute__((destructor)) int b() {
return a;
}
int main() {}
```

LLVM-cov:

```$ clang-trunk -w -O0 -fcoverage-mapping -fprofile-instr-generate=small.c.profraw small.c -o small && ./small && /compiler/llvm-trunk/bin/llvm-profdata merge small.c.profraw -o small.c.profdata && /compiler/llvm-trunk/bin/llvm-cov show small -instr-profile=small.c.profdata small.c
1| |int a;
2| 0|__attribute__((destructor)) int b() {
3| 0| return a;
4| 0|}
5| 1|int main() {}
```

Gcov:
```
gcc-trunk -w -O0 -fprofile-arcs -ftest-coverage small.c -o small && ./small && /compiler/gcc-trunk/bin/gcov small.c && cat small.c.gcov
File 'small.c'
Lines executed:100.00% of 3
Creating 'small.c.gcov'

Lines executed:100.00% of 3
-: 0:Source:small.c
-: 0:Graph:small.gcno
-: 0:Data:small.gcda
-: 0:Runs:1
-: 1:int a;
1: 2:__attribute__((destructor)) int b() {
1: 3: return a;
-: 4:}
1: 5:int main() {}

$ gcc-trunk -v
Using built-in specs.
COLLECT_GCC=gcc-trunk
COLLECT_LTO_WRAPPER=/compiler/gcc-trunk/libexec/gcc/x86_64-pc-linux-gnu/16.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --disable-multilib --disable-bootstrap --enable-languages=c,c++ --prefix=/compiler/gcc-trunk --enable-coverage
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 16.0.0 20251204 (experimental) (GCC)
```

There is a difference between gcov and llvm-cov when using `destructor` attribute.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.