public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Russell King <rmk+lkml@arm.linux.org.uk>
To: Jiri Slaby <jirislaby@gmail.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Hua Zhong <hzhong@gmail.com>,
	"'Heiko Carstens'" <heiko.carstens@de.ibm.com>,
	"'Andrew Morton'" <akpm@osdl.org>,
	linux-kernel@vger.kernel.org,
	"'Martin Schwidefsky'" <schwidefsky@de.ibm.com>
Subject: Re: do { } while (0) question
Date: Tue, 1 Aug 2006 10:57:51 +0100	[thread overview]
Message-ID: <20060801095751.GC9556@flint.arm.linux.org.uk> (raw)
In-Reply-To: <44CF22E8.9020307@gmail.com>

On Tue, Aug 01, 2006 at 11:45:53AM +0159, Jiri Slaby wrote:
> Peter Zijlstra wrote:
> >On Tue, 2006-08-01 at 02:03 -0700, Hua Zhong wrote:
> >>>#if KILLER == 1
> >>>#define MACRO
> >>>#else
> >>>#define MACRO do { } while (0)
> >>>#endif
> >>>
> >>>{
> >>>	if (some_condition)
> >>>		MACRO
> >>>
> >>>	if_this_is_not_called_you_loose_your_data();
> >>>}
> >>>
> >>>How do you want to define KILLER, 0 or 1? I personally choose 0.
> >>Really? Does it compile?
> >
> >No, and that is the whole point.
> >
> >The empty 'do {} while (0)' makes the missing semicolon a syntax error.
> 
> Bulls^WNope, it was a bad example (we don't want to break the compilation, 
> just not want to emit a warn or an err).

Your sentence does not make sense, but I'm going to take it as saying
that you disagree that the above will cause a syntax error.  Try it:

$ cat t.c
#if KILLER == 1
#define MACRO
#else
#define MACRO do { } while (0)
#endif

void foo(int some_condition)
{
     if (some_condition)
             MACRO

     if_this_is_not_called_you_loose_your_data();
}
$ gcc -O2 -o - -E t.c
# 1 "t.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "t.c"






void foo(int some_condition)
{
     if (some_condition)
             do { } while (0)

     if_this_is_not_called_you_loose_your_data();
}
$ gcc -O2 -o - -S t.c >/dev/null
t.c: In function `foo':
t.c:12: error: parse error before "if_this_is_not_called_you_loose_your_data"
$ gcc -O2 -o - -E t.c -DKILLER
# 1 "t.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "t.c"






void foo(int some_condition)
{
     if (some_condition)


     if_this_is_not_called_you_loose_your_data();
}
$ gcc -O2 -o - -S t.c -DKILLER >/dev/null
$

Hence, using do { } while (0) has had the desired effect - the missing
semicolon causes a compile error, while the empty macro results in
unintentional successful compilation without warning or error.

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 Serial core

  reply	other threads:[~2006-08-01  9:58 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-01  8:21 do { } while (0) question Heiko Carstens
2006-08-01  8:45 ` Jonathan Matthews-Levine
2006-08-01  8:53   ` Heiko Carstens
2006-08-01 16:26     ` Andrew James Wade
2006-08-01  8:49 ` Andrew Morton
2006-08-01  8:52 ` Jiri Slaby
2006-08-01  9:03   ` Hua Zhong
2006-08-01  9:39     ` Peter Zijlstra
2006-08-01  9:46       ` Jiri Slaby
2006-08-01  9:57         ` Russell King [this message]
2006-08-01 10:04           ` Jiri Slaby
2006-08-01  9:59         ` Peter Zijlstra
2006-08-01 10:03           ` Jiri Slaby
2006-08-01 14:49   ` Horst H. von Brand

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=20060801095751.GC9556@flint.arm.linux.org.uk \
    --to=rmk+lkml@arm.linux.org.uk \
    --cc=a.p.zijlstra@chello.nl \
    --cc=akpm@osdl.org \
    --cc=heiko.carstens@de.ibm.com \
    --cc=hzhong@gmail.com \
    --cc=jirislaby@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=schwidefsky@de.ibm.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