I have this MISRA C:2004 violation typedefs that indicate size and signedness should be used in place of the basic types
for example I have this piece of code, where I did not understand the right solution to avoid this violation
static int handlerCalled = 0;
int llvm_test_diagnostic_handler(void) {
LLVMContextRef C = LLVMGetGlobalContext();
LLVMContextSetDiagnosticHandler(C, &diagnosticHandler, &handlerCalled);
The MISRA rule is aimed at the fact that C does not define the exact size, range, or representation of its standard integer types. The stdint.h
header mitigates this issue by providing several families of typedefs expressing the implementation-supported integer types that provide specific combinations of signedness, size, and representation. Each C implementation provides a stdint.h
header appropriate for that implementation.
You should comply with the MISRA rule by using the types defined in your implementation's stdint.h
header, choosing the types that meet your needs from among those it actually supports (or those you expect it to support). For example, if you want a signed integer type exactly 32 bits wide, with no padding bits, and expressed in two's complement representation, then that is int32_t
-- if your implementation provides that at all (it would be surprising, but not impossible, for such a type not to be available).
For example,
#include <stdint.h>
// relies on the 'int32_t' definition from the above header:
static int32_t handlerCalled = 0;
The point I was raising in my comment was that you seemed to say that you not only included the header, but also defined your own typedef for uint32_t
. You must not define your own typedef for this or other types in the scope of stdint.h
. At best it is redundant to do so, but at worst it satisfies the MISRA checker yet breaks your code.