Is __builtin_choose_expr supposed to suppress type errors?

Florian Weimer fweimer@redhat.com
Wed Oct 12 09:24:00 GMT 2016


The documentation only mentions that syntax errors may be reported for 
the non-chosen branch.

Consider this example:

struct dynarray_int { int a; };
struct dynarray_str { char * a; };

int dynarray_int_call (struct dynarray_int *);
int dynarray_str_call (struct dynarray_str *);

#define GENERIC(dyn) __builtin_choose_expr        \
   (__builtin_types_compatible_p                         \
      (__typeof__ (dyn), struct dynarray_int *),         \
    dynarray_int_call (dyn), dynarray_str_call (dyn))

void
f (void)
{
   {
     struct dynarray_int dyn;
     GENERIC (&dyn);
   }
   {
     struct dynarray_str dyn;
     GENERIC (&dyn);
   }
}

It results in this (with a somewhat dated version of GCC 7):

choose.c: In function ‘f’:
choose.c:17:14: warning: passing argument 1 of ‘dynarray_str_call’ from 
incompatible pointer type [-Wincompatible-pointer-types]
      GENERIC (&dyn);
               ^
choose.c:10:48: note: in definition of macro ‘GENERIC’
     dynarray_int_call (dyn), dynarray_str_call (dyn))
                                                 ^~~
choose.c:5:5: note: expected ‘struct dynarray_str *’ but argument is of 
type ‘struct dynarray_int *’
  int dynarray_str_call (struct dynarray_str *);
      ^~~~~~~~~~~~~~~~~
choose.c:21:14: warning: passing argument 1 of ‘dynarray_int_call’ from 
incompatible pointer type [-Wincompatible-pointer-types]
      GENERIC (&dyn);
               ^
choose.c:10:23: note: in definition of macro ‘GENERIC’
     dynarray_int_call (dyn), dynarray_str_call (dyn))
                        ^~~
choose.c:4:5: note: expected ‘struct dynarray_int *’ but argument is of 
type ‘struct dynarray_str *’
  int dynarray_int_call (struct dynarray_int *);
      ^~~~~~~~~~~~~~~~~

I suppose I can add casts to void * to get rid of the warning in this 
case, but it's still a bit annoying.  I would rather not override type 
warnings for the actually chosen branch.  Is there any way to do this?

Thanks,
Florian



More information about the Gcc-help mailing list