How are integer types converted implicitly?

前端 未结 3 1740
自闭症患者
自闭症患者 2020-12-19 12:21

The following code fails on a MISRA check. The concrete error message is:

(MISRA-C:2004 10.1/R) The value of an expression of integer type shall not

相关标签:
3条回答
  • 2020-12-19 12:24

    There is no bug in the MISRA checker, it behaves correctly. You get this error because the C standard is flawed and illogical.

    There are two items:

    • One is an enumeration constant. The standard §6.7.2.2/2 states that this shall be compatible with int, no exceptions.

    • MyVariable is an enumerated type. The standard §6.7.7.2/4 states that this should be compatible with char, a signed integer type or an unsigned integer type. Which type that applies is implementation-defined behavior.

    In your case, the implementation-defined enumerated type appears to be equal to unsigned int.

    So the code attempts to implictly convert a variable of signed int to unsigned int, which is a violation of MISRA 2004 10.1.

    MISRA-compliant code should be if (One == (MyEnum)MyVariable).

    0 讨论(0)
  • 2020-12-19 12:30

    I would suspect a compiler bug. What compiler are you using? This post mentions a compiler bug causing Misra 10.1/R failures when using TI's compiler.

    0 讨论(0)
  • 2020-12-19 12:31

    I would suspect the compiler internally handles enums as unsigned integer, as long there is no negative value within the enum.

    0 讨论(0)
提交回复
热议问题