bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Ben Pfaff <blp@cs.stanford.edu>
To: Bruno Haible <bruno@clisp.org>
Cc: "Marc Nieper-Wißkirchen" <marc.nieper+gnu@gmail.com>,
	"Paul Eggert" <eggert@cs.ucla.edu>,
	bug-gnulib <bug-gnulib@gnu.org>
Subject: Re: cast macros
Date: Sun, 18 Apr 2021 12:08:36 -0700	[thread overview]
Message-ID: <CAA_G9+iC6YdiQU2_nf_QH0UojPAqrDP1B_xTJUgJjVCn3Y9WDw@mail.gmail.com> (raw)
In-Reply-To: <1791223.bzdyQdYaU1@omega>

On Tue, Apr 6, 2021 at 4:04 PM Bruno Haible <bruno@clisp.org> 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


  reply	other threads:[~2021-04-18 19:09 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-06  7:18 Type-safe typecasts Marc Nieper-Wißkirchen
2021-04-06 18:59 ` Paul Eggert
2021-04-06 19:13   ` Marc Nieper-Wißkirchen
2021-04-06 19:20     ` Paul Eggert
2021-04-06 19:34       ` Marc Nieper-Wißkirchen
2021-04-06 20:28       ` gl_list API Bruno Haible
2021-04-06 20:39         ` Paul Eggert
2021-04-06 21:04           ` Bruno Haible
2021-04-06 21:09             ` Marc Nieper-Wißkirchen
2021-04-06 23:03         ` cast macros Bruno Haible
2021-04-18 19:08           ` Ben Pfaff [this message]
2021-04-18 19:27         ` gl_list API Ben Pfaff
2021-04-06 20:13     ` Type-safe typecasts Bruno Haible
2021-04-06 19:25 ` Ben Pfaff
2021-04-06 19:54   ` Bruno Haible
2021-04-06 19:56   ` Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAA_G9+iC6YdiQU2_nf_QH0UojPAqrDP1B_xTJUgJjVCn3Y9WDw@mail.gmail.com \
    --to=blp@cs.stanford.edu \
    --cc=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    --cc=marc.nieper+gnu@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).