Verify Generated Code by Using Code Tracing - MATLAB & Simulink (original) (raw)

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 also right-click an element or elements in a model. This two-way navigation is bidirectional traceability.

Using code tracing, you can:

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 either the:

The generated code includes resources that support code tracing:

Traceable Elements

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

Traceability in one direction is supported for these Stateflow elements:

For more information, see Trace Stateflow Elements in Generated Code.

MATLAB Function blocks that you insert directly into a Simulink model are also traceable. For more information, see Use Traceability in MATLAB Function Blocks.

Traceability in Generated Code

Code View

  1. Open the model.
    openExample(‘CustomCodeComments’)
  2. Open the Embedded Coder® app. Build the model. On the tab, click Build.
    The HTML code generation report opens by default. To use the report, see Code Generation Report.
  3. View the generated code in the Code view in the Code perspective.
  4. Switch between the generated source files by using the drop-down list at the top of the Code view.
  5. The source code contains traceability information such as hyperlinked comments, line numbers, variables, and operators. Place your cursor over or click a comment or line number hyperlink. The Simulink Editor highlights the corresponding block or blocks in the model.
    Code view is open next to the model. Cursor is placed over a comment and corresponding block is highlighted.
  6. To highlight the generated code for a block in the model, select the block. The generated code for the block is highlighted in the Code view.
  7. To open the generated code for a referenced model, open the referenced model and view the generated code in the Code view.

Code Generation Report

  1. On the tab, click to open the Configuration Parameters dialog box. Select Create code generation report if it is not already selected. By default, Open report automatically, Code-to-model, and Model-to-code are selected.
  2. If your model contains referenced models and you want to enable traceability for the referenced model's code generation report, repeat the previous step for each referenced model.
  3. Build the model. On the tab, clickBuild. The build process opens the code generation report in a MATLAB web browser.
  4. In the left navigation pane, select a source code file. The source code and line numbers in the right pane contain hyperlinks to blocks in the model.
  5. Click a comment or line number hyperlink. The Simulink Editor displays and highlights the corresponding block or blocks in the model.
    Code generation report window containing model.c file is open. Mouse cursor is placed on a comment that contains a hyperlink to the corresponding block in the model.
  6. To highlight the generated code for a block in the model, right-click the block and select > . The generated code for the block is then highlighted in the HTML code generation report. To highlight the generated code for multiple blocks that you select, hold the SHIFT key, select multiple blocks, and then right-click any one block to select > . The generated code for the blocks is then highlighted in the HTML code generation report.
  7. If you have a referenced model in your model, in the left navigation pane, underReference Models, click the link to a referenced model. The code generation report for the referenced model is now displayed in the MATLAB web browser.
  8. 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`_>/_`blockname`_.

The code generator documents the tags for a model in the comments section of the generated header file _`model`_.h. For example, this comment appears in the header file for a model, foo, that has a subsystem Outer and a nested subsystem Inner:

/* Here is the system hierarchy for this model. *

This code shows a tag comment above the generated line of code. A Gain block at the root level of a source model generates this code:

/* Gain: '/UnDeadGain1' */ rtb_UnDeadGain1_h = dead_gain_U.In1 *  dead_gain_P.UnDeadGain1_Gain;

This code shows a tag comment above the generated line of code. A Gain block within a subsystem one level below the root level of the source model generates this code:

/* Gain: '/Gain' */ dead_gain_B.temp0 *= (dead_gain_P.s1_Gain_Gain);

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 Type Operators
Arithmetic +, -, *,/, %
+=, -=, *=,/=, %=
++, -- (prefix and postfix)
Logical !, &&,|
Relational ==, !=, <,>, <=, >=
Bit ~, |, ^,&, >>,<<
&=, ^=, |=,<<=, >>=
Conditional ?:

These operators are not supported.

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

Traceability Limitations

These limitations apply to reports generated by Embedded Coder software: