Main Content

Prevent Creation of Unused Variables for Unconditional and Conditional 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 unconditional and the variant condition of the destination block is conditional. 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 Variant Sink block has the variant condition expressions V == 1 and V == 2.

Generate C Code Using Embedded Coder

Suppose that the value of V is set to 3.

When you generate code using Embedded Coder™, the variant condition expressions V == 1 and V == 2 evaluate to false. Simulink disables all the blocks connected to the input and output stream of the Variant Sink block. However, code is generated for all the variant choices in the model.

A variable Add is created in the generated code. During code compilation, this variable remains unused since both choices evaluate to false.

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

Related Topics