【问题标题】:Weird gcc warning with _Generic_Generic 的奇怪 gcc 警告
【发布时间】:2021-07-08 22:17:39
【问题描述】:

我有一些代码使用 _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 一个。它似乎正在将 &amp;a(它是 int *)传递给需要长 * 的 bar,反之亦然 &amp;b。 (注释掉其中一个 FOOBAR,只看到一个不兼容的指针错误。)

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


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

【问题讨论】:

  • 你为什么不直接用-E 预处理文件,看看你的宏扩展成什么?
  • @Useless:你试过了吗?这不是很有帮助,因为 _Generic 是在编译时处理的,而不是预处理时处理的,预处理器只会通过它。
  • 请注意,_Generic 不是宏——它是一个主要的表达式。见§6.5.1.1 Generic selection
  • 哦,这绝对有道理——我只是没有真正使用过 C11,问题使用了“宏”这个词。

标签: c macros


【解决方案1】:

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

5 示例 cbrt 类型泛型宏可以实现如下:

 #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 set fussy 编译干净(源文件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);
}

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

【讨论】:

  • 啊,我没有考虑宏扩展的输出。您的编辑回答了我的问题,谢谢。
  • 第一部分 - 我确实考虑过。但正如我在问题接近尾声时提到的,我可能有一个需要 1 个参数的 foo 和一个需要 2 个参数的 bar - 因此我试图“内联”这些参数。
  • 您可以在宏中使用__VA_ARGS__... 表示法,尽管处理可选性很痛苦。 GCC 将允许#define FOOBAR(x, ...) _Generic((x), …)(x, ##__VA_ARGS__)。 C++ 和 C2x 中有一些建议可以干净地处理“宏的零变量参数”——我忘记了它们的确切状态,但您可以查看 open-std.org/jtc1/sc22/wg14(C 标准)和 open-std.org/jtc1/sc22/wg21(C++ 标准)页面以找到细节。 (参见n4849.pdf 中的__VA_OPT__)。
  • 请注意,_Generic(…); 中的变量参数列表会出现问题。调用必须在通用选择之外。有关##__VA_ARGS__ 的信息,另请参阅#define macro for debug printing in C? 末尾的讨论。
猜你喜欢
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
相关资源
最近更新 更多