cmisrax-macros

Is Misra-C compatible with X-macros?


It could be argued that in many cases X-macros increase safety, because it makes easier to make sure that generated arrays are the same length for example.


However, Misra C (from 2004 reference) rules seem to have a lot of preprocessor rules that restrict the usage:

Rule 19.1 (advisory) #include statements in a file should only be preceded by other preprocessor directives or comments.

Troublesome if source table is in other file which is included to generate array for example. But this is advisory rule so it can be worked around.

Rule 19.4 (required) C macros shall only expand to a braced initializer, a constant, a string literal, a parenthesized expression, a type qualifier, a storage class specifier, or a do-while-zero construct.

Should not be issue since most X-macros are used to generate array initializers or constants.

Rule 19.6 (required) #undef shall not be used.

Makes some X-macro usage patterns impossible. Unfortunate, but doesn't outright prevent X-macros.

Rule 19.7 (advisory) A function should be used in preference to a function-like macro.

Advisory rule only.

Rule 19.12 (required) There shall be at most one occurrence of the # or ## preprocessor operators in a single macro definition.

Can be worked around using nested macros.

Rule 19.13 (advisory) The # and ## preprocessor operators should not be used.

Troublesome when generating enums for example, but again this is only advisory rule.

Rule 19.15 (required) Precautions shall be taken in order to prevent the contents of a header file being included twice.

Troublesome in some scenarios, but can be worked around.


Looking at the above, it seems that it is possible to use X-macros with Misra C code, if you are careful.

Is my conclusion correct, or is there some rule I am missing?


Solution

  • Using X-macros to increase type safety is in my opinion a rather weak argument. The main thing they do is to ease maintenance and eliminate problems with code repetition. But like any function-like macros, they are the last resort when everything else fails.

    The proper way to ensure that arrays are of the correct lengths etc is to use static_assert (or some hack:ish pre-C11 mechanism) that yields a compile-time error.

    Addressing your concerns one at a time:

    Conclusion:
    You cannot use X macros in the previous MISRA-C:2004, but you can use them in the current MISRA-C:2012. I would strongly recommend to upgrade, since MISRA-C:2012 is a major change for the better. Various pedantic rules have been dropped, relaxed or re-written. Complicated rules like the ones regarding implicit type promotions have been significantly improved. There is now C99 support. And so on.

    I have used X-macros myself with MISRA-C:2012, and as far as I remember, without the need to raise deviations from any required rules.