Use of DUTs
Topics:
Assign enumeration variables to associated enumerators only
Only the enumerators of the associated enumeration should be assigned to the instance of an enumeration. To force this rule, it is recommended to set the attribute {attribute 'strict'} when defining the type of the enumeration. See also: Use attributes 'qualified_only' and 'strict' for enumeration
Static Analysis:
Verify using the following Static Analysis rules:
- SA0034: Enumeration variables with incorrect assignment
- SA0171: Enumerations should have the 'strict' attribute
Negative sample:
{attribute 'qualified_only'}
TYPE E_ColorTrafficLight :
(
Red := 0,
Yellow := 1,
Green := 2
);
END_TYPE
PROGRAM Sample_neg
VAR
eColorTrafficLight : E_ColorTrafficLight; // Used to handle the state of the traffic light
END_VAR
CASE eColorTrafficLight OF
E_ColorTrafficLight.Green:
eColorTrafficLight := E_ColorTrafficLight.Yellow;
E_ColorTrafficLight.Yellow:
eColorTrafficLight := E_ColorTrafficLight.Red;
E_ColorTrafficLight.Red:
eColorTrafficLight := 2; // NON COMPLIANT: Only values defines in E_StandardColor should be assigned to enum instance eColorTrafficLight
ELSE
eColorTrafficLight := E_ColorTrafficLight.Red;
END_CASE
Positive sample:
{attribute 'strict'}
{attribute 'qualified_only'}
TYPE E_ColorTrafficLight :
(
Red := 0,
Yellow := 1,
Green := 2
);
END_TYPE
PROGRAM Sample_pos
VAR
eColorTrafficLight : E_ColorTrafficLight; // Used to handle the state of the traffic light
END_VAR
CASE eColorTrafficLight OF
E_ColorTrafficLight.Green:
eColorTrafficLight := E_ColorTrafficLight.Yellow;
E_ColorTrafficLight.Yellow:
eColorTrafficLight := E_ColorTrafficLight.Red;
E_ColorTrafficLight.Red:
eColorTrafficLight := E_ColorTrafficLight.Green; // COMPLIANT
ELSE
eColorTrafficLight := E_ColorTrafficLight.Red;
END_CASE
See also: