Main Content

Coverage for Custom C/C++ Code in Simulink Models

When you record coverage for models that contain supported C/C++ S-Functions, MATLAB Function blocks, C Caller blocks with C/C++ code, or Stateflow® charts that integrate custom C/C++ code for simulation, Simulink® Coverage™ analyzes code coverage for the C/C++ code. For each block or chart, the coverage report links to the details for the C/C++ code coverage.

Enable Code Coverage for Custom C/C++ code in MATLAB Function Blocks, C Caller Blocks, and Stateflow Charts

To enable code coverage for custom C/C++ code in your Simulink model:

  1. In the Configuration Parameters dialog box, in the left pane, click Simulation Target.

  2. Select Import custom code.

  3. Select Enable custom code analysis.

Simulink Coverage records code coverage for custom C/C++ code in MATLAB Function blocks, C Caller blocks, and Stateflow charts. For C Function blocks, the code coverage report includes custom C/C++ code files included in the Block Parameters dialog box under Simulation Custom Code. The code coverage report does not include code written in the Block Parameters in the Output, Start, Initialize Conditions, or Terminate fields in the Simulation tab.

Code Coverage for S-Functions

You can collect custom code coverage and generate coverage reports for models that contain S-Functions. The way you enable code coverage depends on whether you build the S-Function by using the legacy_code function, the S-Function Builder block, or the mex function. For more information on creating S-Functions, see Author Blocks Using C MEX S-Functions.

Use the legacy_code Function

  1. Initialize a MATLAB® structure with fields that represent Legacy Code Tool properties.

    def = legacy_code("initialize")

  2. To enable model coverage, enable the option def.Options.supportCoverage.

    def.Options.supportCoverageAndDesignVerifier = true;
  3. Use def to generate an S-function. For an example, see Coverage for S-Functions.

Use the S-Function Builder Block

  1. Copy an instance of the S-Function Builder block into the model.

  2. Double-click the block to open the S-Function Builder dialog box.

  3. On the Build Info tab, select Enable support for coverage.

Use the slcovmex Function

If you use the mex function to compile and link your source files, use the slcovmex function instead. The slcovmex function compiles your source code and also makes it compatible with coverage.

This function has the same syntax and takes the same options as the mex function. In addition, you can provide some options relevant for model coverage. For more information, see slcovmex.

Generate Code Coverage Report for S-Functions

  1. In the Configuration Parameters dialog box, in the left pane, click Coverage.

  2. Under Include in analysis, select C/C++ S-functions.

When you run a simulation, the coverage report contains coverage metrics for the C/C++ S-Function blocks in your model. For each S-Function block, the report links to a detailed coverage report for the C/C++ code in the block.

See Also

Topics