[previous] 3459 [next] Macro defines a 'do-while-zero' construct.
Macro Definition

A do-while-zero macro is being defined.

This type of macro is sometimes used as an elegant way of encapsulating a sequence of statements within the body of a macro. If the macro call is followed by a semicolon, the construct may be treated as if it were a single statement. just like a function call. This means that, if desired, it can be used without surrounding parentheses as the statement following an if, else, while, do or for.

For example:


/*PRQA S 506,553,1006,2017,2212,3220,3227,3361,3408,3410,3412,3429,3435,3456 ++*/
/************************************
 * OPTIONS: -ex asm
 ***********************************/

#define DISABLE_INTERRUPTS asm {...}
#define ENABLE_INTERRUPTS  asm {...}
#define MASK 0x38u

#define SET_PORT(PX)          \
    do {                      \
       unsigned int A;        \
       DISABLE_INTERRUPTS;    \
        A = *PX;              \
        A = A & MASK;         \
        *PX = A;              \
       ENABLE_INTERRUPTS;     \
    } while (0)                                             /* Message 3459 */

extern void foo(int n, unsigned int *p)
{
    if (n > 0)
        SET_PORT(p);
}

See also:

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents