MISRA C: 2004: 应该使用指示大小和符号的类型定义来代替基本类型

MISRA C: 2004: typedefs that indicate size and signedness should be used in place of the basic types

我有这个 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);

MISRA 规则针对的事实是 C 没有定义其标准整数类型的确切大小、范围或表示形式。 stdint.h header 通过提供 several families of typedefs 表达 implementation-supported 整数类型来缓解这个问题,这些整数类型提供符号、大小和表示的特定组合。每个 C 实现都提供适合该实现的 stdint.h header。

您应该遵守 MISRA 规则,使用您的实施 stdint.h header 中定义的类型,从它实际支持的类型(或您期望的类型)中选择满足您需求的类型支持)。例如,如果您想要一个恰好 32 位宽的有符号整数类型,没有填充位,并以二进制补码表示形式表示,那么就是 int32_t —— 如果您的实现完全提供了它(这会令人惊讶,但并非不可能,因为这种类型不可用)。

例如,

#include <stdint.h>

// relies on the 'int32_t' definition from the above header:
static int32_t handlerCalled = 0;

我在评论中提出的观点是,您似乎说您不仅包括了 header,而且还为 [=] 定义了 您自己的 typedef 15=]。 您不得为此类型或 stdint.h 范围内的其他类型定义自己的 typedef。充其量这样做是多余的,但在最坏的情况下它满足 MISRA 检查器但会破坏您的代码。