Analyze Code and Test Software-in-the-Loop

Products Used

Polyspace®
Bug Finder™, Embedded
Coder®

Code Analysis and Testing Software-in-the-Loop Overview

Analyze code to detect errors, check standards compliance, and
evaluate key metrics such as length and cyclomatic complexity. Typically
for handwritten code, you check for run-time errors with static code
analysis and run test cases that evaluate the code against requirements
and evaluate code coverage. Based on the results, refine the code
and add tests. For generated code, demonstrate that code execution
produces equivalent results to the model by using the same test cases
and baseline results. Compare the code coverage to the model coverage.
Based on test results, add tests and modify the model to regenerate
code.

Analyze Code for Defects, Metrics, and MISRA C:2012

This workflow describes how to check if your model produces MISRA® C:2012
compliant code and how to check your generated code for code metrics,
code defects, and MISRA compliance. To produce more MISRA compliant
code from your model, you use the code generation and model advisors.
To check whether the code is MISRA compliant, you use the Polyspace MISRA C:2012
checker and report generation capabilities. For this example, you
use the model simulinkCruiseErrorAndStandardsExample.
To open the model:

Open the Simulink® project:

slVerificationCruiseStart

From the Simulink project, open the model simulinkCruiseErrorAndStandardsExample.

Run Code Generator Checks

Before you generate code from your model, there are steps that
you can take to generate code more compliant with MISRA C and
more compatible with Polyspace. This example shows how to use
the Code Generation Advisor to check your model before generating
code.

The Code Generation Advisor checks whether there are any blocks
or configuration settings that are not recommended for MISRA C:2012
compliance and Polyspace code analysis. For this mode, the check
for incompatible blocks passes, but there are some configuration settings
that are incompatible with MISRA compliance and Polyspace checking.

Click on check that was not passed. Accept the parameter
changes by selecting Modify Parameters.

Rerun the check by selecting Run This Check.

For your own model, you might not want to use all the recommended
configuration settings. Using nonrecommended settings can generate
less MISRA compliant code.

Run Model Advisor Checks

Before you generate code from your model, there are steps you
can take to generate code more compliant with MISRA C and more
compatible with Polyspace. This example shows you how to use
the Model Advisor to check your model further before generating code.

For more checking before generating code, you can also run the
Modeling Guidelines for MISRA C:2012.

At the bottom of the Code Generation Advisor window,
select Model Advisor.

Under the By Task folder, select
the Modeling Guidelines for MISRA C:2012 advisor
checks.

Click Run Selected Checks and
review the results.

If any of the tasks fail, make the suggested modifications
and rerun the checks until the MISRA modeling guidelines pass.

For your own model, you might not want to use all the recommendations.
Using nonrecommended settings or blocks can generate less MISRA compliant
code.

Generate and Analyze Code

After you have done the model compliance checking, you can now
generate code. With Polyspace, you can check your code for compliance
with MISRA C:2012 and generate reports to demonstrate compliance
with MISRA C:2012.

Polyspace
Bug Finder analyzes the generated code for a subset
of MISRA checks and defect checks. You can see the progress of
the analysis in the MATLAB Command Window. Once the analysis is finished,
the Polyspace environment opens.

Review Results

After you run a Polyspace analysis of your generated code,
the Polyspace environment shows you the results of the static
code analysis. There are 50 MISRA C:2012 coding rule violations
in your generated code.

Expand the tree for rule 8.7 and click through the
different results.

Rule 8.7 states that functions and objects should not be global
if the function or object is local. As you click through the 8.7 violations,
you can see that these results refer to variables that other components
also use, such as CruiseOnOff. You can annotate
your code or your model to justify every result. But, because this
model is a unit in a larger program, you can also change the configuration
of the analysis to check only a subset of MISRA rules.

Set the Settings from (Polyspace Bug Finder) option
to Project configuration. This option allow
you to choose a subset of MISRA rules in the Polyspace configuration.

Click the Configure button.

In the Polyspace Configuration window, on the Coding
Rules & Code Metrics pane, select the check box Check
MISRA C:2012 and from the drop-down list, select single-unit-rules.
Now, Polyspace checks only the MISRA C:2012 rules that are
applicable to a single unit.

Save and close the Polyspace configuration window.

Rerun the analysis with the new configuration.

When the Polyspace environment reopens, there are no MISRA results,
only code metric results. The rules Polyspace showed previously
were found because the model was analyzed by itself. When you limited
the rules Polyspace checked to the single-unit subset, no violations
were found.

When this model is integrated with its parent model, you can
add the rest of the MISRA C:2012 rules.

Generate Report

To demonstrate compliance with MISRA C:2012 and report
on your generated code metrics, you must export your results. This
section shows you how to generate a report after the analysis. If
you want to generate a report every time you run an analysis, see Generate report.

If they are not open already, open your results in
the Polyspace environment.