Main Content

Suppress Generation of Default Cases for Unreachable Stateflow Switch Statements

This example shows how to specify whether to generate default cases for switch-case statements in the code for Stateflow® charts. Generated code that does not contain default cases conserves ROM consumption and enables better code coverage because every branch in the generated code is falsifiable.

Some coding standards, such as MISRA, require the default case for switch-case statements. If you want to increase your chances of producing MISRA C compliant code, generate default cases for unreachable Stateflow switch statements.

Example

Figures 1, 2, and 3 show relevant portions of the sldemo_fuelsys model, a closed-loop system containing a plant and controller. The Air-fuel rate controller logic is a Stateflow chart that specifies the different operation modes.

Figure 1: Top-level model of the plant and controller

Figure 2: Fuel rate controller subsystem

Figure 3: Fuel rate controller logic

Generate Code with Default Cases for Unreachable Stateflow Switch Statements

  1. In the MATLAB Command Window, to open sldemo_fuelsys enter:

    openExample('sldemo_fuelsys');
  2. Open the Model Configuration parameters dialog box. On the Code Generation > Code Style tab, clear the Suppress generation of default cases for Stateflow switch statements if unreachable parameter.

  3. In the MATLAB Command Window, to build the model, enter:

    slbuild('sldemo_fuelsys/fuel_rate_control');

For the different operation modes, the fuel_rate_control.c file contains default cases for unreachable switch statements. For example, for the Shutdown operation mode, the generated code contains this default statement:

default:
 /* Unreachable state, for coverage only */
 rtDWork.bitsForTID0.is_Fuel_Disabled = IN_NO_ACTIVE_CHILD;
 break;

For the Warmup operation mode, the generated code contains this default statement:

default:
 /* Unreachable state, for coverage only */
 rtDWork.bitsForTID0.is_Low_Emissions = IN_NO_ACTIVE_CHILD;
 break;

Suppress Default Cases for Unreachable Stateflow Switch Statements

  1. Open the Configuration Parameters dialog box. On the Code Generation > Code Style tab, select the Suppress generation of default cases for Stateflow switch statements if unreachable parameter.

  2. Build the model.

Read through the fuel_rate_control.c file. The default cases for unreachable switch statements are not in the generated code.

Related Topics