3

I have some code dispatching a function based on the type of its argument, using the _Generic. I do not understand the warning gcc generates.

Compile with 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);
}

Now, this code does compile and _Generic works as expected, i.e. "foo" appears then "bar". However, the compiler (gcc and clang) generate a weird warning that looks like its matching the argument against _Generic to the wrong line:

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) {
      |          ~~~~~^

Two warnings are generated, one for each FOOBAR. It appears to be passing &a which is int * to bar which takes a long *, and vice versa for &b. (Comment out the one of the FOOBARs to see just one incompatible pointer error.)

Why is gcc warning me that _Generic is dispatching its arg to the wrong function?


I know this isn't normally how people use _Generic, i.e. the argument list would usually be outside the _Generic(). But I have some use cases for dispatching to functions that take different number of arguments.

Useless
  • 64,155
  • 6
  • 88
  • 132
Donald
  • 693
  • 1
  • 7
  • 12
  • 1
    Why don't you just preprocess the file with `-E` and see exactly what your macro expands to? – Useless Jul 08 '21 at 22:22
  • @Useless: Did you try it? It's not very helpful as `_Generic` is handled at compilation, not preprocessing, and the preprocessor will just pass it through. – Nate Eldredge Jul 08 '21 at 22:24
  • Note that `_Generic` is not a macro — it is a primary expression. See [§6.5.1.1 Generic selection](http://port70.net/~nsz/c/c11/n1570.html#6.5.1.1). – Jonathan Leffler Jul 08 '21 at 22:25
  • Well, I learned something today. – Useless Jul 08 '21 at 22:27
  • I think the issue is that all the selections are parsed and type-checked, even though only one is evaluated. So in `FOOBAR(&a)`, both `foo(&a)` and `bar(&a)` are type checked, and `bar(&a)` generates a warning due to the pointer type mismatch. I think it's harmless, but you could put in casts to silence the warning: `int * : foo((int *)(x))` etc. – Nate Eldredge Jul 08 '21 at 22:28
  • @Useless: The thing to remember is that the preprocessor doesn't know anything about types. Like how it can't evaluate `sizeof`, and you can't do `#if sizeof(int) == 4`. – Nate Eldredge Jul 08 '21 at 22:30
  • 1
    Oh it absolutely makes sense - I just haven't really used C11 and the question used the word "macro". – Useless Jul 08 '21 at 22:32
  • 1
    Does this answer your question? [Incompatible pointer types passing in \_Generic macro](https://stackoverflow.com/questions/24743520/incompatible-pointer-types-passing-in-generic-macro) – Nate Eldredge Jul 08 '21 at 22:35
  • @NateEldredge: The suggested question ([Incompatible pointer types passing in `_generic` macro](https://stackoverflow.com/q/24743520/15168)) does answer this question — but doesn't give the 'worked example' given [here](https://stackoverflow.com/a/68309379/15168). – Jonathan Leffler Jul 08 '21 at 22:58
  • For a strictly conforming method to call functions with different prototypes using `_Generic`, see Question 1 in [this answer](https://stackoverflow.com/questions/63419320/applying-c-requirements-to-unselected-generic-cases) and the Update in the accepted answer—to make it fully strictly conforming you need to define the `NeverCalled` function, but it is not necessary in practice. – Eric Postpischil Jul 08 '21 at 23:10

1 Answers1

7

Note that the example in the standard §6.5.1.1 Generic selection is:

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)

Note where the parentheses for the function invocation are — outside the _Generic(…) part of the generic selection.

Adapting that to your code:

#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;
}

This compiles cleanly with GCC 10.2.0 set fussy (source file gs31.c):

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

The code changes outside the FOOBAR macro avoid compilation warnings requested by my standard compiling options.

The output from the C pre-processor for your code is:

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

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

compared with:

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

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

The difference is that your code calls foo() with a long * (aka &b) and bar() with an int * (aka &a), and this is what (correctly) triggers the warnings.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Ah, I didn't consider the output from macro expansion. Your edit answers my question, thanks. – Donald Jul 08 '21 at 22:56
  • 1
    The first part - I did consider that. But as I mentioned near the end of my question, I might have a foo that takes 1 arg and a bar that takes 2 args - hence I'm trying to "inline" the arguments. – Donald Jul 08 '21 at 22:58
  • 1
    You can probably use the `__VA_ARGS__` and `...` notation in the macro, though dealing with the optionality is a pain. GCC would allow `#define FOOBAR(x, ...) _Generic((x), …)(x, ##__VA_ARGS__)`. There are proposals in C++ and C2x to handle the 'zero variable arguments to a macro' cleanly — I forget their exact status, but you can check out http://www.open-std.org/jtc1/sc22/wg14/ (C standard) and http://www.open-std.org/jtc1/sc22/wg21/ (C++ standard) pages to find the details. (See `__VA_OPT__` in [n4849.pdf](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4849.pdf)). – Jonathan Leffler Jul 08 '21 at 23:17
  • 1
    Note that you'd have problems with variable argument lists inside the `_Generic(…)`; the call must be outside the generic selection. See also discussion towards the end of [`#define` macro for debug printing in C?](https://stackoverflow.com/q/1644868/15168) for information about `##__VA_ARGS__`. – Jonathan Leffler Jul 08 '21 at 23:22