1

Rule 1.1 of the MISRA C 2004 specifies that the spec covers c90 and not c99.

I would like to use the stdint and stdbool libraries instead of coding my own. Has anyone made this exception in their MISRA implementation?

Community
  • 1
  • 1
JeffV
  • 52,985
  • 32
  • 103
  • 124
  • Rule 6.3 in effect recommends a "user defined" implementation of `stdint.h` - so if your compiler supports `stdint.h` (and/or `stdbool.h` for that matter) then I think this is a justified deviation (as per para 4.3.2) to use them. – Andrew Jan 22 '13 at 08:51

1 Answers1

3

You should definitely use the type names from stdint.h. This is how I have solved it, in a MISRA-C:2004 compliant way:

#ifdef __STDC_VERSION__ 
  #if (__STDC_VERSION__ >= 199901L)  /* C99 or later? */
    #include <stdint.h>
    #include <stdbool.h>
  #else
    #define C90_COMPILER
  #endif /* #if (__STDC_VERSION__ >= 199901L) */
#else
  #define C90_COMPILER
#endif /* __STDC_VERSION__  */


#ifdef C90_COMPILER
  typedef unsigned char uint8_t;
  typedef unsigned int  uint16_t;
  typedef unsigned long uint32_t;
  typedef signed char   int8_t;
  typedef signed int    int16_t;
  typedef signed long   int32_t;

  #ifndef BOOL
    #ifndef FALSE
      #define FALSE 0u
      #define false 0u
      #define TRUE  1u
      #define true  1u
    #endif

    typedef uint8_t BOOL;
    typedef uint8_t bool;
  #endif
#endif /* C90_COMPILER */
Lundin
  • 195,001
  • 40
  • 254
  • 396