【问题标题】:MISRA C 2004 and c99MISRA C 2004 和 c99
【发布时间】:2012-12-29 16:27:59
【问题描述】:

MISRA C 2004 的规则 1.1 规定该规范涵盖 c90 而不是 c99。

我想使用 stdint 和 stdbool 库而不是自己编写代码。有没有人在他们的 MISRA 实施中做出过这个例外?

【问题讨论】:

  • Rule 6.3 实际上推荐了stdint.h 的“用户定义”实现——所以如果你的编译器支持stdint.h(和/或stdbool.h)那么我认为这是合理的偏差(根据第 4.3.2 节)来使用它们。

标签: c c99 misra stdint stdbool


【解决方案1】:

您绝对应该使用 stdint.h 中的类型名称。这就是我以符合 MISRA-C:2004 的方式解决它的方法:

#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 */

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 2015-12-02
    相关资源
    最近更新 更多