我违反了 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 来缓解此问题。表达实现支持的整数类型,这些整数类型提供符号、大小和表示的特定组合。每个 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 ,而且还为 uint32_t
定义了您自己的 typedef。 您不得为此类型或 stdint.h
范围内的其他类型定义自己的 typedef。这样做充其量是多余的,但在最坏的情况下它满足 MISRA 检查器但会破坏您的代码。
关于c - 米斯拉 C : 2004: typedefs that indicate size and signedness should be used in place of the basic types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54522287/