Re: cast macros

2021-04-18 Thread Ben Pfaff
On Tue, Apr 6, 2021 at 4:04 PM Bruno Haible  wrote:
>
> I wrote:
> > So far we have been lacking type-casts that warn for invalid use;
> > it is well possible that with the PSPP macros (or with Marc's approach
> > of _Generic), we can design a type-safe wrapper around gl_list.h
>
> Here is a simplified test case: The simplest container type is a box type,
> that contain just one value. Can one of you complete this code, so that
> it produces the warnings / no warnings as indicated?
>
> 
> typedef const void ** gl_box_t;
>
> extern gl_box_t create_box (const void *);
> extern const void *box_get_value (gl_box_t);
>
> #include "cast.h"
>
> /* CAST_TO_FROM (TO, FROM, EXPR)
>gives a warning if EXPR is not of type FROM,
>and returns EXPR, cast to type TO.  */
>
> struct foo;
> #define ELEMENT_TYPE struct foo *
> #define CREATE_BOX(V) \
>   create_box (CAST_TO_FROM (const void *, ELEMENT_TYPE, V))
> #define BOX_GET_VALUE(BOX) \
>   CAST_TO_FROM (ELEMENT_TYPE, const void *, box_get_value (BOX))
>
> struct foo *a;
> struct foo *b;
> const void *c;
> void *d;
> int *i;
>
> int
> main ()
> {
>   (void) CAST_TO_FROM (int *, const void *, c); // no warning
>   (void) CAST_TO_FROM (int *, void *, d);   // no warning
>   (void) CAST_TO_FROM (int *, const void *, d); // no warning
>   (void) CAST_TO_FROM (int *, void *, c);   // warning
>   (void) CAST_TO_FROM (int *, void *, i);   // warning
>   (void) CAST_TO_FROM (int *, char *, i);   // warning
>
>   gl_box_t box = CREATE_BOX (a);// no warning
>   return BOX_GET_VALUE (box) == b;  // no warning
> }
> 

I spent some time experimenting and I don't know a way to do that.

One way to come close, using GCC extensions:
  #define CAST_TO_FROM(to, from, expr) ({ \
_Static_assert (__builtin_types_compatible_p (typeof(expr), from)); \
(to) (expr);  \
  })
but this will give an unwanted warning for
  (void) CAST_TO_FROM (int *, const void *, d); // no warning
because the 'const' is not outermost.

Another way, without GCC extensions, is:
  #define CAST_TO_FROM(to, from, expr) ({ \
(void) sizeof ((from) (expr) == (expr));  \
(to) (expr);  \
  })
but this fails to warn for either of the following because void mixes with
other types so freely in C:
  (void) CAST_TO_FROM (int *, void *, c);   // warning
  (void) CAST_TO_FROM (int *, void *, i);   // warning



Re: cast macros

2021-04-06 Thread Bruno Haible
I wrote:
> So far we have been lacking type-casts that warn for invalid use;
> it is well possible that with the PSPP macros (or with Marc's approach
> of _Generic), we can design a type-safe wrapper around gl_list.h

Here is a simplified test case: The simplest container type is a box type,
that contain just one value. Can one of you complete this code, so that
it produces the warnings / no warnings as indicated?


typedef const void ** gl_box_t;

extern gl_box_t create_box (const void *);
extern const void *box_get_value (gl_box_t);

#include "cast.h"

/* CAST_TO_FROM (TO, FROM, EXPR)
   gives a warning if EXPR is not of type FROM,
   and returns EXPR, cast to type TO.  */

struct foo;
#define ELEMENT_TYPE struct foo *
#define CREATE_BOX(V) \
  create_box (CAST_TO_FROM (const void *, ELEMENT_TYPE, V))
#define BOX_GET_VALUE(BOX) \
  CAST_TO_FROM (ELEMENT_TYPE, const void *, box_get_value (BOX))

struct foo *a;
struct foo *b;
const void *c;
void *d;
int *i;

int
main ()
{
  (void) CAST_TO_FROM (int *, const void *, c); // no warning
  (void) CAST_TO_FROM (int *, void *, d);   // no warning
  (void) CAST_TO_FROM (int *, const void *, d); // no warning
  (void) CAST_TO_FROM (int *, void *, c);   // warning
  (void) CAST_TO_FROM (int *, void *, i);   // warning
  (void) CAST_TO_FROM (int *, char *, i);   // warning

  gl_box_t box = CREATE_BOX (a);// no warning
  return BOX_GET_VALUE (box) == b;  // no warning
}