getCodeCoverageData - Extract coverage data for generated C/C++ code and custom C/C++ code (MATLAB code generation) - MATLAB

Main Content

Extract coverage data for generated C/C++ code and custom C/C++ code (MATLAB code generation)

Since R2023a

Syntax

Description

coverageObject = getCodeCoverageData(myMEXFunction) generates C/C++ code coverage object by using data created by latest execution of myMEXFunction in software-in-the-loop (SIL) or processor-in-the-loop (PIL) simulation. Running this function also opens the C/C++ code coverage HTML report.

example

Examples

collapse all

This example shows how to use the instrumentCode function to add instrumentation for C/C++ coverage analysis to code that you already generated by using the codegen command. To perform coverage analysis, you need a MATLAB® Test™ license.

Define Entry-Point Function and Generate Code

Define a MATLAB entry-point function coverageAnalysisExample that performs a binary operation on its first two inputs. The binary operation depends on whether the third input is positive, negative, or zero.

type coverageAnalysisExample
function out = coverageAnalysisExample(A,B,flag) %#codegen
if flag > 0
    out = A + B;
elseif flag < 0
    out = A - B;
else
    out = A * B;
end
end

Generate static library code for software-in-the-loop (SIL) execution. Specify all inputs to be of scalar double type. Specify the name of the code generation folder to be my_codegen_folder.

cfg = coder.config('lib');
cfg.VerificationMode = 'SIL';
codegen -config cfg coverageAnalysisExample -args {0,0,0} -d my_codegen_folder
Code generation successful.

Add Instrumentation to Generated Code

Add C/C++ code coverage instrumentation to the generated code and produce an instrumented MEX.

instrumentCode('my_codegen_folder', 'CodeCoverage', true)
Completed instrumentation of generated code.

Execute the MEX and View Coverage Data

Execute coverageAnalysisExample_sil with two sets of sample inputs. Make sure the third input is positive for one set and negative for the other.

coverageAnalysisExample_sil(10,4,3)
### Starting SIL execution for 'coverageAnalysisExample'
    To terminate execution: clear coverageAnalysisExample_sil
coverageAnalysisExample_sil(10,4,-2)

Terminate SIL execution and view the code coverage report.

clear coverageAnalysisExample_sil
### Application stopped
### Stopping SIL execution for 'coverageAnalysisExample'
    Code coverage report: getCodeCoverageData('coverageAnalysisExample_sil')
getCodeCoverageData('coverageAnalysisExample_sil');

code_coverage.png

Because neither of your test runs had the third input argument equal to zero, the last branch of the if-else block was never executed in the generated C entry-point file coverageAnalysisExample.c. This caused the coverageAnalysisExample.c file to have less than 100% statement coverage.

Input Arguments

collapse all

Name of the software-in-the-loop (SIL) or processor-in-the-loop (PIL) MEX function for which you intend to generate C/C++ code coverage report.

Data Types: char | string

Version History

Introduced in R2023a