Essential type of enumerated constants

Moderators: misra-c, david ward

Post Reply
psusi
Posts: 4
Joined: Fri Feb 20, 2015 4:03 pm
Company: Iradimed, Inc

Essential type of enumerated constants

Post by psusi » Fri Mar 06, 2015 2:47 pm

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: Select all

enum list {
ONE,
TWO,
THREE,
}

int c;
for( c = 0; c < THREE; c++ )  /* THREE is an integer constant so comparing it with c, an int, is not a violation */
....

michael.metivier
Posts: 10
Joined: Thu Mar 21, 2013 7:30 pm
Company: Gentex, Corp.

Re: Essential type of enumerated constants

Post by michael.metivier » Tue Mar 17, 2015 5:34 pm

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: Select all

enum {
ONE,
TWO,
THREE
};
I believe it would be compliant.

misra-c
Posts: 566
Joined: Thu Jan 05, 2006 1:11 pm

Re: Essential type of enumerated constants

Post by misra-c » Thu Mar 26, 2015 3: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.
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: Select all

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:
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<i>;
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<i>:

Code: Select all

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: Select all

 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: Select all

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

Post Reply

Return to “8.10 The essential type model”