Search code examples
cmisra

MISRA 19.4 clarification


I want to use the following MACRO in my file

#define DO_WAKEUP_SYNCHRONISIEREN proc.id = process_WAKEUP;proc.priority.level = PRIO_AKTIV_1;proc.priority.param=0u;(void)prio_mapF_put_in_matrix(proc)

I know that it is a violation of MISRA-C:2004 Rule 19.4 - but I need to use this and to solve the violation.

Is there any way I can use the MACRO and also solve the MISRA-C violation?

MISRA-C:2004 Rule 19.4 is

C macros shall only expand to a braced initialiser, a constant, a string literal, a parenthesised expression, a type qualifier, a storage class specifier, or a do-while-zero construct.


Solution

  • it looks like the rule is:

    #define B A
    

    The above is not a permitted form of macro definition under this rule because A is not a constant, braced initializer, parenthesized expression, type qualifier, storage class specifier or do-while-zero construct

    #define B (A)
    

    The above is permitted. The replacement list is a parenthesised expression.

    an extension of this would be (even without MISRA constraints I'd do this, but I just noticed that the rule states it: "do-while-zero construct", IMHO it's not obvious at first glance what "do-while-zero construct" means when you're not aware of that...):

    #define DO_WAKEUP_SYNCHRONISIEREN do {proc.id = process_WAKEUP;proc.priority.level = PRIO_AKTIV_1;proc.priority.param=0u;(void)prio_mapF_put_in_matrix(proc);} while(0)
    

    so the instruction block cannot be "partially called" by constructs like this:

    if (flag) DO_WAKEUP_SYNCHRONISIEREN;
    

    which would give headaches to anyone trying to understand the bug.

    And at the same time it forces to add a ; as opposed to a simple {} block. So the macro looks very much like a function call (also read Why use do { } while (0) in macro definition?).