Skip to content

[Coverage] wrong line coverage for macros with multiple case labels #158003

@whentojump

Description

@whentojump

The program is reduced from https://sources.debian.org/src/mawk/1.3.4.20200120-3.1/rexp2.c#L359

How to reproduce (Compiler Explorer link):

$ clang --version
Ubuntu clang version 20.1.8 (++20250708082409+6fb913d3e2ec-1~exp1~20250708202428.132)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-20/bin
rm *.profraw *.profdata

cat > test.c << 'EOF'
#define CASE_42_43 case 42: \
    case 43
int g;
int main() {
    switch (42) {
    CASE_42_43:
        g++;
    }
}
EOF

clang -fprofile-instr-generate -fcoverage-mapping -o prog test.c
./prog
llvm-profdata merge *.profraw -o default.profdata
llvm-cov show prog -instr-profile=default.profdata
    1|      1|#define CASE_42_43 case 42: \
    2|      1|    case 43
    3|       |int g;
    4|      1|int main() {
    5|      1|    switch (42) {
    6|      2|    CASE_42_43:
    7|      2|        g++;
    8|      1|    }
    9|      1|}

Line 6 and 7 are wrongly reported to have executed twice.

In contrast, when the macro gets expanded the problem is gone.

    1|       |#define CASE_42_43 case 42: \
    2|       |    case 43
    3|       |int g;
    4|      1|int main() {
    5|      1|    switch (42) {
    6|      1|    case 42: case 43:
    7|      1|        g++;
    8|      1|    }
    9|      1|}

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