Rule 14.3 and null macros

6.14 Control Flow

Moderators: misra-c, david ward

Post Reply
JohnC
Posts: 3
Joined: Tue Sep 13, 2011 8:11 am
Company: Secure Meters (UK)

Rule 14.3 and null macros

Post by JohnC » Wed Sep 14, 2011 8:20 am

If a function is sometimes to be included and sometimes omitted, the classic solution is to define it like this:

Code: Select all

#if FnIsWanted
  extern void Fn( void );
#else
  #define Fn( ) 
#endif
As already noted, this falls foul of rule 14.3, though primarily it is a breach of rule 19.4 (what a macro can be defined as).

I can get the the effect I want on my compiler without apparently breaching MISRA rules by using the following alternative:

Code: Select all

#if FnIsWanted
  extern void Fn( void );
#else
  #define Fn( ) do{}while(0)
#endif
However, the body of this do-while is a null (compound) statement, so has my compiler missed a violation?

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

Re: Rule 14.3 and null macros

Post by misra-c » Wed Sep 21, 2011 10:06 am

MISRA C uses the term "null statement" in the same way as the standard, namely a statement consisting of nothing but a semicolon character. The body of the while loop is an empty compound statement but it's not syntactically a null statement. Therefore the checker is correct not to report a violation of Rule 14.3.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.14 Control Flow”