Skip to content

[coverage] return in macros leads to incorrect coverage mapping after its invocation #140427

Open
@whentojump

Description

@whentojump

Compiler explorer link

Problematic input:

// (1) "return" in macro
#define FOO \
    if (b) \
        return;

int g, cnt;

void foo(int a, int b) {
    do { // (2) inside an "if", "while" etc
        if (a)
            return; // (3) "return", "goto" etc
        // g++; // inserting a statement here eliminates the abnormal behavior
        FOO;
        cnt++; // <= wrong line coverage 
    } while (0);
}

Report:

   10|  43.2k|void foo(int a, int b) {
   11|  43.2k|    do { // (2) inside an "if", "while" etc
   12|  43.2k|        if (a)
   13|  40.2k|            return; // (3) "return", "goto" etc
   14|       |        // g++; // inserting a statement here eliminates the abnormal behavior
   15|  3.02k|        FOO;
   16|  3.02k|        cnt++; // <= wrong line coverage 
   17|  3.02k|    } while (0);
   18|  43.2k|}

Line coverage for line 16 is "3.02k" but printing the value of cnt at the end of the program it instead says "10".

Real-world example: https://sources.debian.org/src/lzma/9.22-2.2/C/LzmaDec.c/#L628

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:codegenIR generation bugs: mangling, exceptions, etc.coverage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions