Main Content

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.

Traceable Elements

Bidirectional traceability is supported for Simulink blocks and these Stateflow® elements:

  • States

  • Transitions

  • State transition tables

  • MATLAB functions. Traceability is not supported for external code that you call from a MATLAB function.

  • Truth table blocks

  • Graphical functions

  • Simulink functions

Traceability in one direction is supported for these Stateflow elements:

  • Events (code-to-model)

    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.

  • Junctions (model-to-code)

    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.

  1. Open the example GeneratingStructuredTextForAFeedForwardPIDControllerExample.

    openExample('plccoder/GeneratingStructuredTextForAFeedforwardPIDControllerExample')
  2. Open the PLC Coder app. Click the PLC Code tab.

  3. Click Settings and navigate to the Code Generation pane.

  4. To enable report generation, select Report > Generate traceability report.

  5. Click OK.

  6. Click Generate PLC Code to initiate code and report generation. The code generation report for the top model opens in a MATLAB® web browser.

  7. In the left navigation pane, select the plcdemo_pid_feedforward.exp file.

  8. Click a comment or line number hyperlink. The Simulink Editor displays and highlights the corresponding block in the model.

  9. 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.

  10. In the left navigation pane, you can click the Back button to go back to the previous code generation report.

Traceability Tags

A traceability tag appears in a comment above the corresponding line of generated code. The format of the tag is <system>/block_name.

  • system is a unique number assigned by the Simulink engine.

  • block_name 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;

Operator Traceability

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.

Operator TypeOperators
Arithmetic+, -, *, /, %
+=, -=, *=, /=, %=
++, -- (prefix and postfix)
Logical!, &&, ||
Relational==, !=, <, >, <=, >=
Bit~, |, ^, &, >>, <<
&=, ^=, |=, <<=, >>=
Conditional?:

These operators are not supported.

Operator TypeOperator Examples
Assignment operator=
Member of and pointer operatorsArray subscript: a[b]
Address of and pointer dereference: &a, *a
Member of: a.b, a->b
Other operatorsParenthesis in function call: foo(a,b)
Comma: a, b
Scope resolution: a::b
Cast: type(a)
new, new[]
delete, delete[]

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 plcdemo_simple_subsystem/SimpleSubsystem:

  1. Open a Simulink PLC Coder model, for example:

    openExample('plcdemo_simple_subsystem');
  2. Enable the code generation parameter PLC_GenerateReport. To view the output in the model web view, also enable PLC_GenerateWebview:

    set_param('plcdemo_simple_subsystem', 'PLC_GenerateReport', 'on');
    set_param('plcdemo_simple_subsystem', 'PLC_GenerateWebView', 'on');
  3. 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.

Traceability Limitations

These limitations apply to reports generated by the Simulink PLC 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 (/).

    • The character ÿ (char(255)).

  • 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:

    • Virtual subsystems

    • Masked subsystems

    • 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 shared_utils folder.

Related Topics