Search code examples
cmisracppcheck

How to provide #defines to misra checking in cppcheck?


I'm trying to utilize misra C:2012 checking in from cppcheck (v2.9). However, when executing on a particular file I get the following violations:

vehicle_controller.c:1494:2: style: All identifiers used in the controlling expression of #if or #elif preprocessing directives shall be #define’d before evaluation. [misra-c2012-20.9] #if OS == LINUX_OS ^ src/emb/bwk/

I'm using the following command to execute:

$ cppcheck -DOS=LINUX_OS --addon=misra.json vehicle_controller.c

Is there a different way to pass #define to be properly picked up by the misra checking?


Solution

  • As per the examples within Rule 20.9, an undefined identifier results in a zero value:

    #if OS == 0    /* Non-compliant - OS may be zero or undefined */
    ...
    #endif
    

    The MISRA compliant approach is to ensure that the identifier is defined before use. Thus:

    #ifndef OS
    ...                   /* OS is undefined           */
    #else
      #if OS == 0
      ...                 /* OS is defined as zero     */
      #else
      ...                 /* OS is defined as non-zero */
      #endif
    #endif