代码之家  ›  专栏  ›  技术社区  ›  Gianni Spear

MISRA C:2004:表示大小和符号的typedef应该用来代替基本类型

  •  0
  • Gianni Spear  · 技术社区  · 7 年前

    我有这个MISRA C:2004违规行为 typedefs that indicate size and signedness should be used in place of the basic types

    static int handlerCalled = 0;
    
    int llvm_test_diagnostic_handler(void) {
      LLVMContextRef C = LLVMGetGlobalContext();
      LLVMContextSetDiagnosticHandler(C, &diagnosticHandler, &handlerCalled);
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   John Bollinger    7 年前

    MISRA规则的目标是C不定义其标准整数类型的确切大小、范围或表示形式。这个 stdint.h header通过提供 several families of typedefs 标准力 适用于该实现的标题。

    您应该通过使用您的实现中定义的类型来遵守MISRA规则 标准力 标题,从它实际支持的类型(或您期望它支持的类型)中选择满足您需求的类型。例如,如果您想要一个32位宽的有符号整数类型,没有填充位,并且以2的补码表示形式表示,那么这就是 int32_t

    #include <stdint.h>
    
    // relies on the 'int32_t' definition from the above header:
    static int32_t handlerCalled = 0;
    

    我在评论中提到的一点是,您似乎说您不仅包含了标题,还定义了 类型定义 uint32_t . 您不能定义自己的typedef 对于此类型或范围内的其他类型 标准力 . 在最好的情况下,这样做是多余的,但在最坏的情况下,它满足了MISRA检查程序的要求,但却破坏了您的代码。

    推荐文章