Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Essential type of enumerated constants
#1
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 {
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 */
....
<t></t>
#2
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 {
ONE,
TWO,
THREE
};
I believe it would be compliant.
<t></t>
#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


Forum Jump:


Users browsing this thread: 2 Guest(s)