LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Jamie Iles <jamie@jamieiles.com>
To: Sri Ram Vemulpali <sri.ram.gmu06@gmail.com>
Cc: Kernel-newbies <kernelnewbies@nl.linux.org>,
	linux-kernel-mail <linux-kernel@vger.kernel.org>
Subject: Re: typecheck code
Date: Mon, 31 Jan 2011 17:20:38 +0000	[thread overview]
Message-ID: <20110131172038.GD2819@pulham.picochip.com> (raw)
In-Reply-To: <AANLkTimjz6t9ziPajW1wJdUE8ytWy=FbCr-Zg71PiBgf@mail.gmail.com>

On Mon, Jan 31, 2011 at 12:03:37PM -0500, Sri Ram Vemulpali wrote:
> Hi all,
> 
> /*
>  * Check at compile time that something is of a particular type.
>  * Always evaluates to 1 so you may use it easily in comparisons.
>  */
>  #define typecheck(type,x) \
>  ({      type __dummy; \
>         typeof(x) __dummy2; \

So here we're creating __dummy of the macro specified type and __dummy2 
by using the GCC extensions to work out the type.

>         (void)(&__dummy == &__dummy2); \

This does a comparison and casts the result to void so we ignore the 
result at runtime (and the compiler can optimise it away), but at 
compile time, if the types don't match then we'll get:

	warning: comparison of distinct pointer types lacks a cast

>         1; \

and here we always return 1.

>  })
> 
> #define typecheck_fn(type,function) \
> ({      typeof(type) __tmp = function; \
>        (void)__tmp; \
> })
> 
> Can anyone help me, explain the above code typecheck. How does
> (void)(&__dummy == &__dummy2) evaluates to 1

It's not that comparison that is evaluating to 1, it's the line 
immediately after.  The GCC statement expression extensions [1] mean 
that the "1; \" at the end will always be the value that the macro is 
evaluated to.

Jamie

1. http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html

  reply	other threads:[~2011-01-31 17:20 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-31 17:03 Sri Ram Vemulpali
2011-01-31 17:20 ` Jamie Iles [this message]
2011-01-31 17:22 ` Geert Uytterhoeven
2011-01-31 17:44 ` Rajat Sharma
2011-01-31 18:03 ` Manish Katiyar
2011-01-31 20:23   ` Sri Ram Vemulpali
2011-01-31 20:26 ` Dave Hylands
2011-01-31 21:42   ` julie Sullivan

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

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

  git send-email \
    --in-reply-to=20110131172038.GD2819@pulham.picochip.com \
    --to=jamie@jamieiles.com \
    --cc=kernelnewbies@nl.linux.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sri.ram.gmu06@gmail.com \
    --subject='Re: typecheck code' \
    /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

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 NNTP newsgroup(s).