_Generic 发出奇怪的 gcc 警告

Weird gcc warning with _Generic

我有一些代码使用 _Generic 根据参数的类型分派函数。我不明白 gcc 生成的警告。

gcc main.c

编译
#include <stdio.h>

#define FOOBAR(x) _Generic((x),  \
  int *: foo(x),                  \
  long *: bar(x)                  \
)

void foo(int* x) {
    printf("foo\n");
}

void bar(long* y) {
    printf("bar\n");
}

int main() {
    int a = 1111;
    long b = 2222;

    FOOBAR(&a);
    FOOBAR(&b);
}

现在,这段代码可以编译并且 _Generic 按预期工作,即先出现“foo”,然后出现“bar”。然而,编译器(gcc 和 clang)生成了一个奇怪的警告,看起来它匹配反对 _Generic 的参数到错误的行:

main.c: In function ‘main’:
main.c:20:12: warning: passing argument 1 of ‘bar’ from incompatible pointer type [-Wincompatible-pointer-types]
   20 |     FOOBAR(&a);
      |            ^~
      |            |
      |            int *
main.c:5:15: note: in definition of macro ‘FOOBAR’
    5 |   long *: bar(x)                \
      |               ^
main.c:12:16: note: expected ‘long int *’ but argument is of type ‘int *’
   12 | void bar(long* y) {
      |          ~~~~~~^
main.c:21:12: warning: passing argument 1 of ‘foo’ from incompatible pointer type [-Wincompatible-pointer-types]
   21 |     FOOBAR(&b);
      |            ^~
      |            |
      |            long int *
main.c:4:14: note: in definition of macro ‘FOOBAR’
    4 |   int *: foo(x),                \
      |              ^
main.c:8:15: note: expected ‘int *’ but argument is of type ‘long int *’
    8 | void foo(int* x) {
      |          ~~~~~^

生成了两个警告,每个 FOOBAR 一个。它似乎将 &a 是 int * 传递给需要 long * 的 bar,反之亦然 &b。 (注释掉其中一个 FOOBAR,只看到一个不兼容的指针错误。)

为什么 gcc 警告我 _Generic 正在将其 arg 分派给错误的函数?


我知道人们通常不是这样使用 _Generic 的,即参数列表通常 _Generic() 之外。但是我有一些用例可以分派到采用不同数量参数的函数。

注意标准§6.5.1.1 Generic selection中的例子是:

5 EXAMPLE The cbrt type-generic macro could be implemented as follows:

 #define cbrt(X) _Generic((X),                             \
                         long double: cbrtl,               \
                         default: cbrt,                    \
                         float: cbrtf                      \
                         )(X)

注意函数调用的括号在哪里 — 在通用选择的 _Generic(…) 部分之外。

根据您的代码进行调整:

#include <stdio.h>

#define FOOBAR(x) _Generic((x),           \
                           int *:  foo,   \
                           long *: bar    \
                           )(x)

static void foo(int *x)
{
    printf("foo (%d)\n", *x);
}

static void bar(long *y)
{
    printf("bar (%ld)\n", *y);
}

int main(void)
{
    int a = 1111;
    long b = 2222;

    FOOBAR(&a);
    FOOBAR(&b);

    return 0;
}

使用 GCC 10.2.0 编译干净(源文件 gs31.c):

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -fno-common -c gs31.c
$

FOOBAR 宏外的代码更改避免了我的标准编译选项要求的编译警告。

您的代码的 C 预处理器的输出是:

int main() {
    int a = 1111;
    long b = 2222;

    _Generic((&a), int *: foo(&a), long *: bar(&a) );
    _Generic((&b), int *: foo(&b), long *: bar(&b) );
}

与:

相比
int main(void)
{
    int a = 1111;
    long b = 2222;

    _Generic((&a), int *: foo, long *: bar )(&a);
    _Generic((&b), int *: foo, long *: bar )(&b);
}

不同之处在于,您的代码调用 foo() 时使用 long *(又名 &b),而 bar() 时使用 int *(又名 &a),这就是(正确)触发警告的原因。