31-03-2020, 08:17 AM
First some background on the C standard conversions in switch statements.
The actual check that should be performed is between the essential type of the case label expression and the essential type of controlling expression.
The MISRA C working group agree that the wording is not clear and it will be clarified in the next Technical Corigendum.
Concerning your examples:
Quote:C99 6.8.4.2(5) "The integer promotions are performed on the controlling expression. The constant expression in each case label is converted to the promoted type of the controlling expression."The Amplification to rule 10.3 is written in a way which describes the relevent conversion in the C standard.
The actual check that should be performed is between the essential type of the case label expression and the essential type of controlling expression.
The MISRA C working group agree that the wording is not clear and it will be clarified in the next Technical Corigendum.
Quote:From:
2. The conversion of the constant expression in a switch statement's case label to the promoted type of the controlling expression.
To:
2. The conversion of the constant expression in a switch statement's case label to the essential type of the controlling expression.
Concerning your examples:
Code:
uint8_t a = 1;
switch (a) // essential type of unsigned 8 bit
{
case 1u: // compliant
case -200: // not compliant with 10.3
break;
}
uint16_t num = ...;
switch (num >> 1u) // essential type always unsigned 16 bit
(
case 1u: // compliant
case (uint16_t)2u: // compliant
case 2: // Compliant due to exception 3.
break;
}
Posted by and on behalf of the MISRA C Working Group