Re: Type-generic macros with C

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jörg Leis <joerg@xxxxxxxxxxxxx> writes:

> The code
>
> void f_int(int *x);
> void f_float(float *x);
>
> #define is_int_ptr(x) __builtin_types_compatible_p((x),int *)
> #define f(x) __builtin_choose_expr(is_int_ptr(x), f_int(x),  f_float(x))
>
> will warn about incompatible pointer types when called with a pointer to
> an int and with a pointer to a float, although the chosen expression is
> valid in either case. Moreover, it will warn about "dereferencing
> type-punned pointer" when compiling with -O2.

Thanks for the example.  That code compiles fine for me, but this code
does get a warning (note that I had to add "typeof"):

void f_int(int *x);
void f_float(float *x);

#define is_int_ptr(x) __builtin_types_compatible_p(typeof(x),int *)
#define f(x) __builtin_choose_expr(is_int_ptr(x), f_int(x),  f_float(x))

void foo(int *p) { f(p); }


With this I get a warning:

foo.c: In function ‘foo’:
foo.c:7:1: warning: passing argument 1 of ‘f_float’ from incompatible pointer type
foo.c:2:6: note: expected ‘float *’ but argument is of type ‘int *’

This is a class of warning will gcc will emit even for code that is
never executed.  It would be reasonable to disable that type of
warning in code in a __builtin_choose_expr.  I would encourage you to
file a bug report as described at http://gcc.gnu.org/bugs/ .  Thanks.

Ian


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux