Main Content

Prevent Creation of Unused Variables for Lenient Variant Choices

This example shows how to prevent Simulink® models from creating unused variables in generated code when the variant condition of the source block is more lenient than the variant condition of the destination block. Preventing the creation of unused variables in generated code increases the likelihood of generating C code that is compliant with Rule 2.2 of the MISRA C:2012 guidelines.

Model Description

In this model, the source of the highlighted signal is the EvlnB block. The destination of the signal is VariantSource3. The variant condition of EvlnB is A > 0, which is more lenient than the variant condition A > 0 && B == 1 of VariantSource3.

Generate C Code Using Embedded Coder

Suppose that the value of A is 1, and the value of B is 0.

When you generate code using Embedded Coder™, the variant condition of EvlnB, A > 0, evaluates to true, and the condition of VariantSource3, A > 0 && B == 1, evaluates to false. However, code is generated for all variant choices. In the generated code, the variable rtb_Switch is created. This variable corresponds to the Switch block that is located inside the EvlnB block. rtb_Switch is used only when A > 0 && B == 1 evaluates to true. In this example, since A > 0 && B == 1 evaluates to false, rtb_Switch remains unused.

To avoid modeling issues that may create unused variables in generated code, select Model Settings > Diagnostics. Expand the Advanced parameters section and scroll to the bottom of the dialog box. In the Modeling issues related to variants section, set the Variant condition mismatch at signal source and destination parameter to warning or error. Setting this parameter warns you about unused variables during code generation.

See Also