Rule 19.4 do-while(0) for single statements

6.19 Preprocessing Directives

Moderators: misra-c, david ward

Post Reply
wwinzer
Posts: 1
Joined: Fri May 24, 2013 11:54 am
Company: Pierburg GmbH

Rule 19.4 do-while(0) for single statements

Post by wwinzer » Tue May 28, 2013 7:24 am

While I fully understand the need to weld multiple statements in a function-like macro into a compound statement using the do-while(0) construct, I wonder why rule 19.4 explicitely includes macros with only one statement.
The do-while-zero construct is used to wrap a series of one or more statements and ensure correct behaviour.
I've searched the net high and low and could not find any reason for requiring single statements to be encapsulated in do-while(0). Could you give an example here of code that breaks when disregarding this rule?

ChipWeller
Posts: 2
Joined: Mon Apr 25, 2005 5:09 pm
Location: Columbus, Ohio USA

Re: Rule 19.4 do-while(0) for single statements

Post by ChipWeller » Mon Jun 03, 2013 3:42 pm

I believe what they are trying to do is to force your macro statement to require a ";" at the end, making it look like the termination of a C expression-statement. There are only a couple of statements (see C90 section 6.6) that require termination with a ";", the expression-statement, the do iteration-statement and the jump-statement. Since without the ";" the expression-statement is simply an expression, which is allowed by Rule 19.4, I think this makes good sense.

Note that I cannot find this rule in the MISRA C:2012 version, so it may be that the MISRA committee now agrees with your view.

misra-c
Posts: 572
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rule 19.4 do-while(0) for single statements

Post by misra-c » Wed Sep 11, 2013 12:04 pm

The 2004 rule was trying to ensure encapsulation of macros, to prevent unintended consequences.

For 2012 we have tightened the focus of the guidelines to address definite problems, rather than be generic. This was one Rule that has become more focussed.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.19 Preprocessing Directives”