Constant Size and Rule 12.2 - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4) +--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21) +---- Forum: 8.7 Literals and constants (https://forum.misra.org.uk/forumdisplay.php?fid=162) +---- Thread: Constant Size and Rule 12.2 (/showthread.php?tid=1019) |
Constant Size and Rule 12.2 - mjlinden - 20-01-2014 Hi Folks, I'm trying to come to an understanding of an issue associated with MISRA 2012 Rule 12.2 and the interpretation of constant size. Here is the situation I'm dealing with. ===================================================================== int32_t result; result = (1 Re: Constant Size and Rule 12.2 - misra-c - 10-02-2014 In replying to this question it is necessary to understand the distinction between the standard types as described in the C standard and essential types as described in the MISRA C:2012 guidelines. The rationale and description of the MISRA essential types can be found in section 8.10 and Appendices C and D of the MISRA C:2012 guidelines. You are correct in saying that the smallest C standard type for an integer constant with no suffix is "signed int". However, the essential type is the type with lowest rank that can represent the value. This is defined in Appendix D.3. In the above example this means that "1" has an essential type of "essentially signed char". And hence 15 would exceed the maximum value of 7 that is permitted for shifting an "essentially signed char". The MISRA essential types model produces a internally consistent view of C types, but a side-effect of keeping the model simple has been that a few integer constant expressions may violate some MISRA guidelines. Your example is one of those occasions and is similar to that given in the example section of rule 12.2 where "1u Re: Constant Size and Rule 12.2 - mjlinden - 12-02-2014 Thanks for your response... -Michael Re: Constant Size and Rule 12.2 - hhallen - 26-05-2016 I have recently had a dialogue with the support at Programming Research, since we use their QA-C static analysis tool with the MISRA-C:2012 add-on. My example: extern unsigned int getShift(void); const unsigned int shift = getShift(); const unsigned int multiplier = 1u Re: Constant Size and Rule 12.2 - misra-c - 23-09-2016 Thank you for your feedback. The essential type system is designed to be stricter than the standard C type system. The messages are consistent with the MISRA-C guidelines. It is inappropriate for us to comment on the output of individual tools. |