Verify Generated Code by Using Code Tracing
Code tracing (traceability) uses hyperlinks to navigate between a line of generated code and its corresponding elements in a model. To find the lines of code and their corresponding elements, you can use the PLC Coder App Navigate To Code on an element in the model. This two-way navigation is bidirectional traceability.
Using code tracing, you can:
Verify that the generated code is as you expect. You can identify which model elements correspond to a line of code. You can track code from different elements that you have or have not reviewed.
Verify that generated code meets design requirements. You can link requirements to model elements and use code tracing to verify that the generated code for a model element meets the design requirements.
When you generate code from a Simulink® model, traceability information is embedded in the generated code, unless explicitly unspecified. The traceability information includes links for tracing between the generated source code and the model. You can view the generated code by using the code generation report.
The generated code includes these resources that support code tracing:
Code element hyperlinks (indicated by underlining when you place your cursor over the code) to trace variables or types in the generated code to their declarations or definitions.
Tags in code comments that identify elements in a model from which lines of code are generated.
Line number hyperlinks that link to the model component from which the line of code was generated.
Bidirectional traceability is supported for Simulink blocks and these Stateflow® elements:
State transition tables
MATLAB functions. Traceability is not supported for external code that you call from a MATLAB function.
Truth table blocks
Traceability in one direction is supported for these Stateflow elements:
Code-to-model traceability works for explicit events, but not implicit events. Clicking a hyperlink for an explicit event in the generated code highlights that item in the Contents pane of the Model Explorer.
Model-to-code traceability works for junctions with at least one outgoing transition. Right-clicking such a junction in the Stateflow Editor highlights the line of code that corresponds to the first outgoing transition for that junction.
For more information, see
Traceability in Generated Code
This example shows how to verify generated code by using the code generation report.
Open the example
Open the PLC Coder app. Click the PLC Code tab.
Click Settings and navigate to the Code Generation pane.
To enable report generation, select Report > Generate traceability report.
Click Generate PLC Code to initiate code and report generation. The code generation report for the top model opens in a MATLAB® web browser.
In the left navigation pane, select the
Click a comment or line number hyperlink. The Simulink Editor displays and highlights the corresponding block in the model.
To highlight the generated code for a block in the model, select the block and in the PLC Coder tab, click Navigate to Code. The generated code for the block is highlighted in the HTML code generation report.
In the left navigation pane, you can click the Back button to go back to the previous code generation report.
A traceability tag appears in a comment above the corresponding line of generated code.
The format of the tag is
is a unique number assigned by the Simulink engine.
is the name of the source block.
This code shows a tag comment above the generated line of code. A Sum block within a subsystem one level below the root level of the source model generates this code:
(* Sum: '<S1>/Sum' *) rtb_Sum := In1 - In2;
The generated code provides traceability between operators in the generated code and Simulink blocks, Stateflow elements, or MATLAB Function blocks.
To verify the generated code by using operator traceability, in the generated code, click an operator hyperlink to highlight the source block in the model.
These operators are supported.
These operators are not supported.
|Operator Type||Operator Examples|
|Member of and pointer operators||Array subscript: |
|Address of and pointer dereference: |
|Member of: |
|Other operators||Parenthesis in function call: |
|Scope resolution: |
Generate a Traceability Report from the Command Line
To generate a Simulink
PLC Coder™ code generation report from the command-line code for the subsystem
Open a Simulink PLC Coder model, for example:
Enable the code generation parameter
PLC_GenerateReport. To view the output in the model web view, also enable
set_param('plcdemo_simple_subsystem', 'PLC_GenerateReport', 'on'); set_param('plcdemo_simple_subsystem', 'PLC_GenerateWebView', 'on');
Generate the code.
generatedfiles = plcgeneratecode('plcdemo_simple_subsystem/SimpleSubsystem')
A traceability report is displayed. In your model, a View diagnostics hyperlink appears at the bottom of the model window. Click this hyperlink to open the Diagnostic Viewer window.
If the model web view is also enabled, that view is displayed.
These limitations apply to reports generated by Embedded Coder® software:
Under the following conditions, model-to-code traceability is disabled for a block if the block name contains:
A single quote (
An asterisk (
*) that causes a name-mangling ambiguity relative to other names in the model. This name-mangling ambiguity occurs if in a block name or at the end of a block name, an asterisk precedes or follows a slash (
If a block name contains a newline character (
\n), the generated code comment for the block path hyperlink replaces the newline character with a space for readability.
You cannot trace blocks representing these types of subsystems to generated code:
Nonvirtual subsystems for which code has been removed due to optimization
If you cannot trace a subsystem at subsystem level, you can trace individual blocks within the subsystem.
If you open a model on a platform that is different from the platform used to generate code, you cannot use model-to-code and code-to-model traceability.
Inline traceability is not available for files that are generated in