getCodeCoverageData
Extract coverage data for generated C/C++ code and custom C/C++ code (MATLAB code generation)
Since R2023a
Description
generates C/C++ code coverage object by using data created by latest execution of
coverageObject
= getCodeCoverageData(myMEXFunction
)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.
Examples
Add Instrumentation That Performs Coverage Analysis
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 instumentation 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
ans = 14
coverageAnalysisExample_sil(10,4,-2)
ans = 6
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');
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
myMEXFunction
— Name of SIL or PIL MEX function
character vector | string scalar
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
See Also
Beispiel öffnen
Sie haben eine geänderte Version dieses Beispiels. Möchten Sie dieses Beispiel mit Ihren Änderungen öffnen?
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)