Main Content

RunPolyspaceAnalysis on Code Generated withEmbedded Coder

If you generate code from a Simulink®model by using Embedded Coder®or TargetLink®, you can analyze the generated code for bugs or run-time errors with Polyspace®from within the Simulink environment. You do not have to manually set up a Polyspace project.

This topic uses Embedded Coder for code generation. For analysis of TargetLink-generated code, see运行Polyspace分析G代码enerated with TargetLink

For a tutorial with a specific model, see运行Polyspace分析G代码enerated from Simulink Model

Prerequisites

Before you run Polyspace from Simulink, you must link your Polyspace and MATLAB®installations. SeeIntegrate Polyspace with MATLAB and Simulink

Generate and Analyze Code

Configure Code Generation and Generate Code

To configure code generation and generate code from a model, doone of the following:

  • On theAppstab, selectEmbedded Coder。Then, on theC Codetab, selectQuick Start。Follow the on-screen instructions.

  • On theC Codetab, clickSettingsand configure code generation through Simulink configuration parameters. The chief parameters to set are:

    • Type(Simulink): SelectFixed-step

    • Solver(Simulink): Selectauto (Automatic solver selection)orDiscrete (no continuous states)

    • System target file(Simulink Coder): Enterert.tlcorautosar.tlc。If you derive target files fromert.tlc, you can also specify them.

    • Code-to-model(Embedded Coder): Select this option to enable links from code to model.

    For the full list of parameters to set, seeRecommended Model Configuration Parameters for Polyspace Analysis

    Alternatively, run the Code Generation Advisor with the objectivePolyspaceand see if the required parameters are already set. SeeConfigure Model for Code Generation Objectives by Using Code Generation Advisor(Embedded Coder)

    To generate code from the model, on theC Codetab, selectGenerate Code。You can follow the progress of code generation in the Diagnostic Viewer.

Configure Code Analysis

On theAppstab, selectPolyspace Code Verifier。On thePolyspacetab:

  1. Select the product to run:Bug FinderorCode Prover

  2. SelectSettings。If needed, change default values of these options.

    • Settings from: Enable checking of MISRA®coding rules in addition to the default checks specified in the project configuration. The default Bug Finder checks look for bugs. The default Code Prover checks look for run-time errors.

    • Input,Tunable parametersandOutput: Constrain inputs, tunable parameters, or outputs for a more precise Code Prover analysis.

    • Output folder: Specify a dedicated folder for results. The default analysis saves the results in a folderresults_modelNamein the current working folder.

    • Open results automatically after verification

    For the full list of options to set, seePolyspace Analysis in Simulink

Analyze Code

To analyze the code generated from the model, click anywhere on the canvas. TheAnalyze Code fromfield shows the model name. SelectRun Analysis

When using Embedded Coder, Polyspace checks for generated code when you clickRun Analysis。如果没有generated code is present, Polyspace first launches the code generation process and then starts the analysis.

If the current model is referenced in another model and you want to verify the generated code in the context where the model is referenced, instead ofCode Generated as Top Model, useCode Generated as Model Reference。In the latter case, Polyspace does not launch code generation automatically if there's no generated code. When analyzingCode Generated as Model Reference, generate code before running the Polyspace analysis.

You can follow the progress of the analysis in the MATLAB Command Window.

The results open automatically unless explicitly disabled. By default, the results are saved in a folderresults_ModelNamein the current folder. Each new run overwrites previous results. You can change the default folders or save the results to a Simulink project. To make these changes, on thePolyspacetab, selectSettings

If you have closed the results and want to open them later, on thePolyspacetab, selectAnalysis Results。To open a result prior to the last run, selectOpen Earlier Resultsand navigate to the folder containing the previous results.

Review Analysis Results

Review Results in Code

The results appear in the Polyspace user interface on theResults Listpane. Click each result to see the source code on theSourcepane and details on theResult Detailspane. See also:

Navigate from Code to Model

Links in code comments show blocks that generate the subsequent lines of code. To see the blocks in the model, click the block names in the links. If you encounter issues, seeTroubleshoot Navigation from Code to Model

Alternatively, you can right-click a variable name and selectGo to Model。This option is not available for all variables. Only a subset of source code variables can be directly traced to a Simulink block. TheGo to Modeloptions is available for such a variable. For more details on which variables in generated code can be traced to Simulink blocks, seeTrace Simulink Model Elements in Generated Code(Embedded Coder)

Fix Issue

Investigate whether the issues in your code are related to design flaws in the model.

Design flaws in the model can lead to issues in the generated code. For instance:

  • The generated code might be free of specific run-time errors only for a certain range of a block parameter. To fix this issue, you can change the storage class of that block parameter or use calibration data for the analysis by using the configuration parameterTunable parameters

  • The generated code might be free of specific run-time errors only for a certain range of inputs. To determine this error-free range, you can specify a minimum and maximum value for the Inport block signals. The Polyspace analysis uses this constrained range. SeeWork with Signal Ranges in Blocks(Simulink)

  • Certain transitions in Stateflow®charts can be unreachable.

If you include handwritten C/C++ code in S-function blocks, the Polyspace analysis can reveal possible integration issues between the handwritten and generated code. You can also analyze the handwritten code in isolation. See运行Polyspace分析功能的代码

Annotate Blocks to Justify Issues

如果你不想改变模型response to a Polyspace result, annotate the relevant blocks. After you annotate a block, code operations generated from the block show results that are prepopulated with your comments. If you annotate a subsystem block or a block that leads to a function call, code operations generated from the block do not show your comments in the analysis results. If the block is a Lookup Table, enable theStub lookup tablesinstead of using annotations. SeeStub lookup tables

In code generated by using Embedded Coder, there are known deviations from MISRA C®:2012. SeeDeviations Rationale for MISRA C:2012 Compliance(Embedded Coder)。Justify these known issues by annotating blocks.

Annotations in Simulink blocks or in generated code do not take the history of the analysis into account. If you update your model, the Polyspace results might change while the annotations do not. Updating the model might render the existing annotations outdated. Check your annotations when you update your model or generated code.

Annotate Blocks ThroughPolyspaceUser Interface

If you use Embedded Coder to generate code, you can annotate Simulink blocks directly through the Polyspace User Interface. Locate the issue that you want to annotate, and then enter review information by addingSeverity,Status, and optional notes in theResult Detailspane. For instance, in the Polyspace User Interface:

  • Set theStatusof the issue toTo Investigate

  • Set theCommentfor the issue toMight Impact "Module"

In the source code, right-click the variable showing the issue and from the context menu, selectAnnotate Block

The review information carries over to the Simulink Editor as block annotation where the annotated block is highlighted.

You can annotate a Simulink block multiple times. Subsequent annotations on a block are appended to previous annotations. These annotations cannot be seen in the Simulink Editor. When you analyze the generated code by using Polyspace, these annotations are displayed as review information in theResult detailspane of the Polyspace User Interface.

Polyspace uses the user-provided information to prepopulate the annotations in Simulink. Comments that are set in the Polyspace User Interface appear within double quotes in theCommentfield in Simulink. If you have double quotes in the comment in Polyspace User interface, those are replaced by single quotes in Simulink.

The optionAnnotate Blockis available for code elements that can be traced to a Simulink block. For more information seeTrace Simulink Model Elements in Generated Code(Embedded Coder)

Annotate Blocks in金宝appEditor

To annotate a block in the Simulink Editor, select the block and on thePolyspacetab, selectAdd Annotation。In thePolyspace Annotationwindow:

  • Select the type of Polyspace result that you want to annotate from the drop-down menuAnnotation Type

  • If you want to annotate multiple results of the same type, enter a comma separated list of result acronym in the text box. See:

  • If you want to annotate only one result, selectOnly 1 check。The text box is converted into a dropdown menu. Select the result that you want to annotate from this dropdown menu.

  • In the corresponding text boxes, enter the status, severity, and comment that you want to assign to the results.

In thePolyspace Annotationwindow, you can annotate a single type of Polyspace result at a time. To annotate multiple types of results. open thePolyspace Annotationwindow multiple times. Each time, add an annotation corresponding to one type of Polyspace result. The different annotations are appended to each other. These annotations cannot be seen in the Simulink Editor. When you analyze the generated code by using Polyspace, these annotations are displayed as review information in theResult detailspane of the Polyspace User Interface.

Sometimes operations in the generated code cause orange checks in Code Prover. Suppose an operation potentially overflows. The generated code protects against the overflow by following the operation with a saturation. Polyspace still flags the possible overflow as an orange check. To justify these checks through code comments, specify the configuration parameterOperator annotations(Embedded Coder)

When you copy an annotated block and then use it in a different model or in a different position in the same model, the changed context can render the annotation incorrect.

  • Polyspace does not allow annotation in blocks inside libraries and nonatomic subsystems because these blocks are reused in many different contexts. For instance, you cannot annotate a block inside a library block and justify results on all instances of the library block.

  • Simulink does not retain Polyspace annotations in blocks that are copied to a different model or in a different position in the same model.

Related Topics