Simulink® Code Inspector™ User's Guide
Simulink® Code Inspector™ User's Guide
User’s Guide
R2014a
How to Contact MathWorks
www.mathworks.com Web
comp.soft-sys.matlab Newsgroup
www.mathworks.com/contact_TS.html Technical Support
508-647-7000 (Phone)
508-647-7001 (Fax)
Trademarks
MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand
names may be trademarks or registered trademarks of their respective holders.
Patents
MathWorks products are protected by one or more U.S. patents. Please see
www.mathworks.com/patents for more information.
Revision History
September 2011 Online only New for Version 1.0 (Release 2011b)
March 2012 Online only Revised for Version 1.1 (Release 2012a)
September 2012 Online only Revised for Version 1.2 (Release 2012b)
March 2013 Online only Revised for Version 1.3 (Release 2013a)
September 2013 Online only Revised for Version 2.0 (Release 2013b)
March 2014 Online only Revised for Version 2.1 (Release 2014a)
Contents
Getting Started
1
Simulink Code Inspector Product Description . . . . . . . 1-2
Key Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
v
Code Inspection
3
Code Inspection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2
vi Contents
Retain Comments When Regenerating a Traceability
Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-33
vii
viii Contents
1
Getting Started
Key Features
• Structural equivalence analysis and reports
• Bidirectional traceability analysis and reports
• Compatibility checker to restrict model, block, state diagrams, and coder
usage to operations typically used in high-integrity applications
• Tool independence from Simulink code generators
• Tool qualification support (with DO Qualification Kit)
1-2
Why Use This Product?
While developing a model intended for generating code, you can incrementally
and iteratively check the model for compatibility with Simulink Code
Inspector rules. This process significantly reduces the amount of time to
achieve satisfactory inspection results.
For companies and organizations that must certify software under DO-178C,
the Simulink Code Inspector significantly reduces the time and cost associated
with verifying code against requirements. Instead of completing manual
line-by-line code reviews with a project checklist, which is time intensive and
error prone, you can run the Simulink Code Inspector and review a detailed
inspection report.
1-3
1 Getting Started
1-4
Code Inspection Limitations
{
<topModelName>_TimingBridge.nTasks = 1;
<topModelName>_TimingBridge.clockTick = (NULL);
<topModelName>_TimingBridge.clockTickH = (NULL);
}
• If you modify a model by replacing blocks and inspect the code without first
regenerating it, code inspection can fail. For example:
- If you replace inports or outports, and inspect the code without first
regenerating it, your model might contain a root-level inport or outport
with a “Simulink Identifier” (SID) that does not match the SID of the
inport or outport block in the model. As a result, code inspection fails.
• Some modeling patterns can result in code inspection failures. For
examples, see “Model Patterns That Can Result in Code Verification
Failures” on page 3-19.
1-5
1 Getting Started
1-6
Approach to Code Inspection
With the top model window selected, issue the following command:
b From the top model window, select Code > Simulink Code Inspector.
This opens the Simulink Code Inspector dialog box.
c Examine the dialog box parameters that apply to model compatibility
checking. If you are checking a model that references other models, you
can choose to check only the top model or the entire model reference
hierarchy. Selecting the option Inspect all referenced models
includes referenced models in model compatibility checking as well as
code inspection.
1-7
1 Getting Started
3 Run model compatibility checks. Click Check this model or Check all
models. The compatibility checker displays a progress bar.
a If you opted to check only the top model, results are displayed directly in
the Model Advisor dialog box. You can use the dialog box to explore and
rerun individual checks and save the results.
b If you opted to check all models, results are displayed in the command
window and in an HTML summary report window. You can click links in
the HTML summary report to view the detailed Model Advisor Report
for each model and referenced model that was checked.
5 Verify the run-time functionality of the model. If you modified the model
to address incompatibilities, reverify model functionality using simulation
test cases. Before proceeding with code inspection, your model should
simulate with no errors.
6 Generate C code for the model, if it has not already been generated. You can
generate code implicitly as part of code inspection (using the Simulink Code
Inspector dialog box option Generate code before code inspection), or
use Embedded Coder separately to generate the model code. If code was
generated previously and placed in a configuration management system,
make sure the code is available and ready for inspection.
a Open the Simulink Code Inspector dialog box, if it is not already open.
b Examine and configure the dialog box parameters that apply to code
inspection.
• If you are inspecting a model that references other models, you can
choose to inspect only the top model or the entire model reference
hierarchy. Selecting the option Inspect all referenced models
includes referenced models in model compatibility checking as well
as code inspection.
1-8
Approach to Code Inspection
• If your generated code does not use the default Embedded Coder folder
structure created by code generation, update the Code placement
parameter appropriately.
• Optionally, you can change the location to which code inspection
writes the code inspection report, using the dialog box parameter
Report folder.
8 Inspect the generated code. Click Inspect Code or Generate and inspect
code. The Simulink Code Inspector displays a progress bar.
a If you opted to inspect only the top model, results are displayed directly
in the detailed code inspection report for the top model.
b If you opted to inspect all models, results are displayed in an HTML
summary report window. You can click links in the HTML summary
report to view the detailed code inspection report for each model and
referenced model that was inspected.
If all models get the overall inspection result Passed, the code inspection is
complete. If Warning or Failed status is returned for a model, fix or work
around the reported conditions and reinspect the model.
1-9
1 Getting Started
>> slcidemo_roll_orig
1-10
Inspect Generated Code for a Sample Model
This model represents a basic roll axis autopilot with two operating modes:
roll attitude hold and heading hold. The mode logic for these modes is
external to this model. The model architecture represents the heading hold
mode and basic roll attitude function as referenced models. The model
includes:
• Virtual subsystem RollAngleReference, which implements the basic
roll angle reference calculation. Embedded Coder code generation inlines
this calculation directly into the main function for slcidemo_roll.
• Model block HeadingMode, referencing a separate model that computes
the roll command to track the heading.
• Model block BasicRollMode, referencing a separate model that computes
the roll attitude control function.
1-11
1 Getting Started
Note If you try this example with a model other than slcidemo_roll, set
the model parameter AdvancedOptControl to the value '-SLCI'. This
setting constrains the code optimizations that Embedded Coder uses
to a subset that is compatible with code inspection. When you use this
setting, the generated code can differ from code generated without the
'-SLCI' setting. However, the differences do not affect the behavior of
the generated code.
With the top model window selected, issue the following command:
a From the top model window, select Code > Simulink Code Inspector.
The Simulink Code Inspector dialog box opens.
b Configure model compatibility checks. For this example, select Inspect
all referenced models and click Apply. This setting includes
referenced models in model compatibility checking as well as code
inspection. The dialog box should appear as follows:
c Run the model compatibility checks by clicking Check all models. The
compatibility checker displays a progress bar.
1-12
Inspect Generated Code for a Sample Model
Note This HTML report also is linked from the command window
results. It is saved as file summaryReport.html in subfolder
/slprj/modeladvisor under the current working folder.
1-13
1 Getting Started
3 Verify the run-time functionality of the model. For this example, you can
verify run-time functionality by simulating the model. Before proceeding
with code inspection, your model should simulate with no errors.
4 Generate code for the model. You can generate code implicitly as part
of code inspection (using the Simulink Code Inspector dialog box option
Generate code before code inspection), or perform code generation
and code inspection as separate steps. This example separates the code
generation step from the code inspection step.
a In the top model window, select Simulation > Model Configuration
Parameters to open the Configuration Parameters dialog box. In the
Code Generation > Report pane, select the option Open report
automatically. (If you try this example with a model other than
slcidemo_roll, also select Code-to-model, Model-to-code, and the
four Traceability Report Contents options in the Report pane.) Click
Apply and save the model changes.
b Go to the Code Generation main pane and click Generate code. (If
the Generate code button does not appear for your model, select the
Generate code only option to enable the button.) Progress is displayed
in the MATLAB Command Window.
c Embedded Coder code generation displays results in an HTML report
window.
1-14
Inspect Generated Code for a Sample Model
1-15
1 Getting Started
c To inspect the generated code, click Inspect Code. The Simulink Code
Inspector displays a progress bar.
d The Simulink Code Inspector displays a summary in an HTML report
window.
The summary report links to detailed code inspection reports for the top
model and each referenced model. For example, here is the topmost
portion of the code inspection report for the top model, slcidemo_roll.
1-16
Inspect Generated Code for a Sample Model
The summary report and the detailed code inspection reports are saved
as HTML files in the Report folder location that you specified.
6 Insert an error into the generated code and inspect for failure.
1-17
1 Getting Started
>> hilite_system('slcidemo_roll/RollAngleReference/Or');
The code inspection report for the top model contains several indications
of a failed comparison between the Logical Operator block and the
corresponding code. The top of the report shows the following.
1-18
Inspect Generated Code for a Sample Model
1-19
1 Getting Started
7 Optionally, try modifying the model or other aspects of the generated code
to see how code inspection results are affected.
1-20
2
Model Compatibility
Checking
During a model compatibility check, the software checks for model and block
configuration settings that help produce an in-memory representation of the
model that is compatible with Simulink Code Inspector rules. You can set
model and block configuration parameters many different ways and produce
a compatible in-memory representation. Compatibility checks scan for a
subset of those ways. Although a model can fail a compatibility check and
still pass inspection, passing compatibility checks increases the likelihood of
satisfactory code inspection.
The compatibility checks look for conditions that violate Simulink Code
Inspector constraints on model configuration parameters, other model-wide
attributes, and block usage. Items affected by Simulink Code Inspector
constraints include:
2-2
Model Compatibility Checking
- Function specifications
- Model arguments
- Unsupported blocks
- Tunable workspace variables
- Sample times
- Global data stores
- Fixed-point instrumentation
- Root outport usage
- Bus usage
• Block usage
- Data types and ports
- Block parameters
• Stateflow object usage
- Charts
- Data and Events
- Junctions
- States
- Transitions
2-3
2 Model Compatibility Checking
To initiate compatibility checking for your model, you can do any of the
following:
• From the model window, select Code > Simulink Code Inspector, and
use the Simulink Code Inspector dialog box to control model compatibility
checking. For more information, see “Check Model Compatibility Using the
Graphical User Interface” on page 2-5.
• Use the slci.Configuration interface to programmatically control model
compatibility checking. For more information, see “Check Model
Compatibility Using the Command-Line Interface” on page 2-9.
• Use the slciadvisor interface to open an SLCI Advisor session (equivalent to
Model Advisor preloaded with Simulink Code Inspector checks) for the open
model. This function provides direct access to SLCI model compatibility
checking that can streamline iterative checking of a model.
The Simulink Code Inspector does not run custom Model Advisor checks that
you add to the compatibility checks.
In the detailed results, the result of each check is explained, and if you need
to fix your model, recommended actions are provided. The available model
compatibility checks are listed in report order and described in the “Simulink
Code Inspector Checks” reference.
2-4
Check Model Compatibility Using the Graphical User Interface
>> slcidemo_roll_orig
2 Open the Simulink Code Inspector dialog box and configure model
compatibility checks.
a From the top model window, select Code > Simulink Code Inspector.
2-5
2 Model Compatibility Checking
3 To run model compatibility checks, click Check this model or Check all
models. The compatibility checker displays a progress bar.
4 If you opted to check only the top model, results are displayed directly in
the Model Advisor dialog box. You can use the dialog box to explore and
rerun individual checks and save the results.
If you opted to check all models, results are displayed in the command
window and in an HTML summary report window.
• The MATLAB Command Window displays results similar to the
following:
2-6
Check Model Compatibility Using the Graphical User Interface
Note This HTML report also is linked from the command window
results. It is saved as file summaryReport.html in subfolder
/slprj/modeladvisor under the current working folder.
To view the detailed Model Advisor Report for a model listed in the
HTML summary report, go to the Systems Run table, and click the
corresponding link in the Model Advisor Report column.
2-7
2 Model Compatibility Checking
1 From the Model Editor, select Analysis > Model Advisor > Model
Advisor.
3 Click OK.
You can use the Model Advisor window to select and run the Simulink Code
Inspector compatibility checks on your subsystem. See “Consult the Model
Advisor”.
2-8
Check Model Compatibility Using the Command-Line Interface
config = slci.Configuration('slcidemo_roll');
result = config.checkCompatibility('DisplayResults','None');
for i = 1:length(result)
fprintf('\nModel ''%s'' passed %d checks with %d issues.',...
result{i}.system,...
result{i}.numPass, result{i}.numWarn + result{i}.numFail)
end
If the checks pass, the model is ready for inspection. If incompatibilities are
reported, fix the issues and recheck the model for compatibility.
2-9
2 Model Compatibility Checking
• If possible, avoid use of externally defined signal objects that are referenced
as global data stores by Data Store Read or Data Store Write blocks. This
usage causes Simulink software to create hidden Data Store memory blocks
at root level, which is incompatible with code inspection.
• Move the affected Data Store Read or Data Store Write blocks into Model
blocks.
2-10
3
Code Inspection
Code Inspection
Code inspection automatically compares generated code with its source
model to satisfy code-review objectives in DO-178C and other high-integrity
standards. The code inspection process builds an in-memory representation of
the model that is independent of the code generation process. The Simulink
Code Inspector systematically examines blocks, parameters, and settings in a
model to determine whether they are structurally equivalent to operations,
operators, and data in the generated code, and generates reports that can be
used to support software certification.
The aspects of a Simulink model that are analyzed by code inspection include
the following:
• Model interface
• Block behavior
• Stateflow behavior
• Block connectivity and execution order
• Data and file packaging
• Local variables
When developing a model from which you intend to generate code that will be
verified using Simulink Code Inspector, you can incrementally and iteratively
check the model for compatibility with Simulink Code Inspector rules. Model
compatibility checking can significantly reduce the amount of time to achieve
satisfactory code inspection results by exposing issues early in the model
development process. Before inspecting the code for a model, you should
check that the model passes Simulink Code Inspector compatibility checks.
For more information, see “Model Compatibility”.
You can generate the model code to be inspected as part of code inspection, or
perform code generation and code inspection as separate steps.
To initiate code inspection for a model that has passed Simulink Code
Inspector compatibility checks, you can do either of the following:
3-2
Code Inspection
• From the model window, select Code > Simulink Code Inspector, and
use the Simulink Code Inspector dialog box to control code inspection. For
more information, see “Inspect Code Using the Graphical User Interface”
on page 3-4.
• Use the slci.Configuration interface to programmatically control
code inspection. For more information, see “Inspect Code Using the
Command-Line Interface” on page 3-8.
Code inspection generates a detailed HTML report for each model inspected.
If you inspected all models in a model reference hierarchy, the software
displays a summary HTML report. You can click links in the HTML
summary report to view the detailed code inspection report for each model
and referenced model that was inspected. If you inspected only one model, the
model results are displayed directly in a detailed code inspection report.
The detailed report for a model documents code verification and traceability
results. The code inspection report contains the following major sections:
For companies and organizations that must certify software under DO-178C,
the Simulink Code Inspector significantly reduces the time and cost associated
with verifying code against requirements. Instead of completing manual
line-by-line code reviews with a project checklist, which is time intensive and
error prone, you can run the Simulink Code Inspector and review a detailed
inspection report. For more information about the code inspection report, see
“Code Inspection Reports” on page 3-9.
3-3
3 Code Inspection
>> slcidemo_roll_orig
3 Verify the run-time functionality of the model. If you modified the model
to address incompatibilities, reverify model functionality using simulation
test cases. For example model slcidemo_roll_orig, you can verify
run-time functionality by simulating the model. Before proceeding with
code inspection, your model should simulate with no errors.
4 Generate code for the model. You can generate code implicitly as part
of code inspection (using the Simulink Code Inspector dialog box option
Generate code before code inspection), or perform code generation
and code inspection as separate steps. This example separates the code
generation step from the code inspection step.
a In the top model window, select Simulation > Model Configuration
Parameters to open the Configuration Parameters dialog box. If you
want to generate an HTML code generation report for later reference
(recommended), go to the Code Generation > Report pane, and select
the option Open report automatically. (If you try this example with a
model other than slcidemo_roll, it is recommended to select all options
in the Report pane.) Click OK and save the model changes.
b Go to the Code Generation main pane and click Generate code. (If
the Generate code button does not appear for your model, select the
3-4
Inspect Code Using the Graphical User Interface
c To inspect the generated code, click Inspect Code. The Simulink Code
Inspector displays a progress bar.
d The Simulink Code Inspector displays a summary in an HTML report
window:
3-5
3 Code Inspection
The summary report links to detailed code inspection reports for the top
model and each referenced model. For example, here is the topmost
portion of the code inspection report for the top model, slcidemo_roll:
3-6
Inspect Code Using the Graphical User Interface
The summary report and the detailed code inspection reports are
saved as HTML files in the Report folder location you specified. If
you reinspect the generated code, the report in the Report folder is
automatically updated.
3-7
3 Code Inspection
config = slci.Configuration('slcidemo_roll');
config.setTopModel(true);
config.setReportFolder(fullfile('.','report'));
result = config.inspect('DisplayResults','None');
fprintf('Model %s status: %s\n',result.ModelName, result.Status);
web(fullfile('.', 'report','slcidemo_roll_report.html'));
3-8
Code Inspection Reports
3-9
3 Code Inspection
3-10
Code Inspection Reports
The detailed code inspection reports provide the following information for
each model:
3-11
3 Code Inspection
The following table shows how code verification and traceability results are
aggregated into the Overall Inspection Result.
3-12
Interpret the Overall Inspection Result
3-13
3 Code Inspection
The following sample report excerpt shows summary code verification results
for generated code that is structurally equivalent to its corresponding model.
Model elements that are outside of the supported language subset and
corresponding code fragments are indicated as “Unable to process” in the
code verification report.
3-14
Analyze Code Verification Results
• The overall code verification result value aggregated from the code
verification report subsections. Possible values for Code Verification
Results are Verified, Partially verified, or Failed to verify.
• Subsection-level verification results:
- Function Interface Verification
3-15
3 Code Inspection
3-16
Analyze Code Verification Results
The following sample report excerpt illustrates how code verification status is
aggregated when one or more objects fails to verify.
3-17
3 Code Inspection
3-18
Model Patterns That Can Result in Code Verification Failures
• Switch blocks with inputs from the same local signals. The switch 3 block
is eliminated in the generated code. Switch blocks switch 1 and switch
2 are not eliminated because they connect to root inports. Simulink Code
Inspector reports it as Failed to verify.
3-19
3 Code Inspection
• Data store read block connected to data store write block. Both the
Data Store Read and Data Store Write1 blocks are eliminated in the
generated code. Simulink Code Inspector reports a Warning status.
• Minmax blocks with inputs from the same local signals. The block compares
two equivalent values, resulting in an if statement with an empty body in
the generated code. Simulink Code Inspector reports it as Failed to verify.
In this example, the Math Function block is fixed at infinity. Although the
Simulink Coder generates code for the model, code verification fails.
3-20
Model Patterns That Can Result in Code Verification Failures
In this example, there are two action subsystems, each with output connected
to a gain block. Code verification fails.
3-21
3 Code Inspection
The generated code might have switch case statements with fall through case
statements. The result is Failed to verify.
In this example, Multiport Switch block input ports 1 and 3 are connected
to the same local signal. Code verification fails.
3-22
Model Patterns That Can Result in Code Verification Failures
During code generation, optimization moves the Gain5 to avoid data copies
in the generated code:
3-23
3 Code Inspection
The following report excerpt shows summary traceability results for generated
code that is structurally equivalent to its corresponding model.
3-24
Analyze Traceability Results
- Not processed — C code lines that were not processed for code-to-model
traceability. For example, code that is located outside the scope of
verified functions. An #include statement that is located outside of
the scope of a model step or initialize function is not processed for
code-to-model traceability.
- Nonfunctional — C code lines that are:
• Empty
• Contain only comments
• Opening or closing brackets ({ and })
3-25
3 Code Inspection
3-26
Analyze Traceability Results
3-27
3 Code Inspection
Traceability Matrices
When you use Model-Based Design and production code generation to develop
application software components, you can generate a traceability matrix. The
traceability matrix provides traceability among model objects, generated code,
and model requirements. You can add comments to the generated traceability
matrix. If you change the model and regenerate the traceability matrix, the
software retains your comments.
For a given model, the generated traceability matrix can provide information
about:
• Model objects that are traceable between the model and generated code,
such as Simulink blocks, Stateflow objects, and MATLAB functions.
• Model objects that are untraceable between the model and generated code,
such as eliminated and virtual blocks.
• Requirements documents that you link to model objects using the Simulink
Verification and Validation™ Requirements Management Interface (RMI).
3-28
Traceability Matrices
3-29
3 Code Inspection
4 Generate and inspect the model code using MATLAB commands. For
example:
• To generate code, enter rtwbuild('slcidemo_roll').
• To inspect the code, enter cfgObj.inspect.
3-30
Generate a Traceability Matrix
slci.ExportTraceReport(cfgObj,'file_name')
For example:
slci.ExportTraceReport(cfgObj,'slcidemo_roll_tracereport')
4 Open the traceability matrix file, review the traceability matrix, and add
comments in new columns. For more information, see “Add Comments to a
Traceability Matrix” on page 3-32.
3-31
3 Code Inspection
3-32
Retain Comments When Regenerating a Traceability Matrix
3-33
3 Code Inspection
3-34
4
DO-178C Objectives
Compliance
The DO-178C software life cycle consists of objectives that must be met for
each of the life cycle stages. In Annex A of the DO-178C standard, these
objectives are summarized in tables. The DO Qualification Kit document
Model-Based Design Workflow for DO-178C summarizes those tables and
provides recommendations on meeting the objectives using a Model-Based
Design process.
4-2
Model-Based Design Workflow in DO-178C
The following table lists MathWorks products and capabilities that can be
used to support the core software development activities in the workflow.
4-3
4 DO-178C Objectives Compliance
The following table lists MathWorks products and capabilities that can be
used to support the core verification and tracing activities of the workflow.
4-4
Model-Based Design Workflow in DO-178C
4-5
4 DO-178C Objectives Compliance
4-6