Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Essential type of enumerated constants
#3
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:
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;
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:
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:
Code:
enum ETAG { A = 8, B = 64, C = 128 } x;
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 { A = 8, B = 64, C = 128 };
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};
The essential type of ONE, TWO and THREE will now be essentially signed char which will not violate any MISRA rule.
Posted by and on behalf of the MISRA C Working Group
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)