Main Content

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

When you record coverage for models containing supported C/C++ S-Functions, MATLAB Function blocks that call external C/C++ code, C Caller blocks with C/C++ code , or Stateflow® charts that integrate custom C/C++ code for simulation, coverage is recorded for the C/C++ code within the C/C++ S-Functions, MATLAB Function blocks, or Stateflow charts. The coverage results for the custom code can be viewed in the same report as the rest of the model. For each S-Function block, MATLAB Function block, or Stateflow chart, the report links to a detailed coverage report for the C/C++ code in the block.

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. On the Simulation Target pane of the Configuration Parameters, select Import custom code.

  2. On the Simulation Target pane of the Configuration Parameters, 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.

Code Coverage for S-Functions

Make S-Function Compatible with Model Coverage

If you use the legacy_code function, S-Function Builder block or mex function to create your S-Functions, adapt your method appropriately to make the S-Function compatible with model coverage.

For more information on the three approaches, see Implement C/C++ S-Functions.

S-Function Using 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, turn on the option def.Options.supportCoverage.

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

S-Function Using S-Function Builder

  1. Copy an instance of the S-Function Builder block from the User-Defined Functions library in the Library Browser into the your 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.

S-Function Using mex 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 Coverage Report for S-Function

  1. In the Simulink Editor, select Model Settings on the Modeling tab.

  2. On the Coverage pane of the Configuration Parameters dialog box, under Include in analysis, select C/C++ S-functions.

When you run a simulation, the coverage report contains coverage metrics for 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.

Related Examples

More About