Essential type of enumerated constants - 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.10 The essential type model (https://forum.misra.org.uk/forumdisplay.php?fid=165) +---- Thread: Essential type of enumerated constants (/showthread.php?tid=1163) |
Essential type of enumerated constants - psusi - 06-03-2015 What is the essential type of an enumerated constant? I believe it should be integer constant, and rule 10.3 seems to agree since it says this expression is compliant: s8a = K1; /* constant value fits */ However, my static analyzer insists that this is a violation: Code: enum list { Re: Essential type of enumerated constants - michael.metivier - 17-03-2015 The example you cited is showing usage of anonymous enums, which are treated as "essentially signed" type and, thus, can be mixed with int. However, your example "enum list" is a named enum, causing it to be "essentially enum" type, which severely reduces its allowable operations. If your example were instead Code: enum { Re: Essential type of enumerated constants - misra-c - 26-03-2015 To answer your question, you must first understand the difference between MISRA’s named enum type and anonymous enum type. These are conventions that MISRA has added on top of the C definitions, which do not affect the C behaviour, but allow a set of consistent MISRA guidelines to be produced. Appendix D.5 gives the definition of named enum type and anonymous enum type. Quote:Two distinct types of enumeration need to be considered:Appendix D.6 gives the information on the essential type for enumeration constants: Quote: The essential type of an enumeration constant is determined as follows:Your example has an enum tag, which makes it a named enum type. Therefore the enumerations constants have an enum essential type. If you wish to use your enumeration constants as essentially signed integers, you may rewrite your definition as an anonymous enum type. Code: enum { ONE, TWO, THREE}; |