[m-dev.] do {} while(0) pattern in macros

Paul Bone paul at bone.id.au
Fri Apr 27 11:35:23 AEST 2018


On Fri, Apr 27, 2018 at 10:22:19AM +1000, Peter Wang wrote:
> On Fri, 27 Apr 2018 09:46:28 +1000, Paul Bone <paul at bone.id.au> wrote:
> > 
> > Can someone remind me why we do this?  My guess it's to make sure that the
> > macro is always a single statement and cannot be used as an expression or
> > expand to multiple statements in a misleading way such as in:
> > 
> >     if (cond)
> >         CALL_MY_MACRO(a, b);
> > 
> > If that's the case, why don't we just put them inside some curly brackets.
> > 
> > 	#define MAYBE_A_SAFE_MACRO(a, b, c)                 \
> > 		{                                               \
> >             a_statement(a, b);                          \
> >             another_statement(c);                       \
> > 		}
> 
> Hi Paul,
> 
> Now you've got two statements.
> 
> https://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html

Ah.

Thanks.

This seems to be a bit belt-and-suspenders (and there's nothing wrong with
that) since the Mercury style guide also says we should also always use
curlies.  But I think we (Mercury) may break that rule sometimes..

Cheers.


-- 
Paul Bone
http://paul.bone.id.au


More information about the developers mailing list