https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121241

            Bug ID: 121241
           Summary: [GCOV] macro function leads to incorrect cov
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: gcov-profile
          Assignee: unassigned at gcc dot gnu.org
          Reporter: njuwy at smail dot nju.edu.cn
  Target Milestone: ---

gcc version:
gcc version 16.0.0 20250704 (experimental) (GCC) 

cmd:
gcc --coverage -std=c2x -lm -O0 test.c -o test
./test
gcov ./*.gcda -t > test.c.gcov

$ cat test.c
#include <stdlib.h>
typedef struct {
    int (*func)();
} Op;
int add() { return 42 + 17; }
int mul() { return 42 * 17; }
#define OP(...) ({ \
    _Static_assert(sizeof((Op[]){__VA_ARGS__}) > 0); \
    (Op[]){__VA_ARGS__}; \
})
#define DO_OPS(op_arr) ({ \
    int total = 0; \
    for (size_t i = 0; i < sizeof(op_arr)/sizeof(*op_arr); ++i) \
        total += op_arr[i].func(); \
    total; \
})
int main() {
    Op ops[] = {
        [0] = { .func = add },
        [1] = { .func = mul },
        [2] = { .func = add },
    };
    int result = DO_OPS(ops);
    return result;
}

$ cat test.c.gcov
        -:    1:#include <stdlib.h>
        -:    2:typedef struct {
        -:    3:    int (*func)();
        -:    4:} Op;
        2:    5:int add() { return 42 + 17; }
        1:    6:int mul() { return 42 * 17; }
        -:    7:#define OP(...) ({ \
        -:    8:    _Static_assert(sizeof((Op[]){__VA_ARGS__}) > 0); \
        -:    9:    (Op[]){__VA_ARGS__}; \
        -:   10:})
        -:   11:#define DO_OPS(op_arr) ({ \
        -:   12:    int total = 0; \
        -:   13:    for (size_t i = 0; i < sizeof(op_arr)/sizeof(*op_arr); ++i)
\
        -:   14:        total += op_arr[i].func(); \
        -:   15:    total; \
        -:   16:})
        1:   17:int main() {
        1:   18:    Op ops[] = {
        -:   19:        [0] = { .func = add },
        -:   20:        [1] = { .func = mul },
        -:   21:        [2] = { .func = add },
        -:   22:    };
        4:   23:    int result = DO_OPS(ops);
        1:   24:    return result;
        -:   25:}


I'm not sure if the cov of line 23 should be 1 or 4.

Reply via email to