26-03-2015, 03:15 PM
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.
The essential type of ONE, TWO and THREE will now be essentially signed char which will not violate any MISRA rule.
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:
1. A named enum type is an enumeration which has a tag or which is used in the definition of any object, function or type;
2. An anonymous enum type is an enumeration which does not have a tag and which is not used in the definition of any object, function or type.
The following all have named enum type and have distinct essential types:Code:enum ETAG { A, B, C };
typedef enum { A, B, C } ETYPE;
typedef enum ETAG { A, B, C } ETYPE;
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.
1. If an enumeration defines a named enum type then the essential type of its enumeration constants is enum;
1.1 If a named enum type is used to define an essentially Boolean type then the essential type of its enumeration constants is essentially Boolean.
2. If an enumeration defines an anonymous enum type then the essential type of each enumeration constant is the STLR of its value.
In the following, each of the enumeration constants, and the object x, have essential type of enum:
The following anonymous enum type defines a set of constants. The essential type of each constant is the STLR. Therefore on a machine with 8-bit char and 16-bit short types, A and B have an essential type of signed char but C has an essential type of signed short.Code:enum ETAG { A = 8, B = 64, C = 128 } x;
Code:enum { A = 8, B = 64, C = 128 };
Code:
enum { ONE, TWO, THREE};
Posted by and on behalf of the MISRA C Working Group