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

Using code tracing, you can:

  • Verify that 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 the design requirements. You can link requirements to the model element and use code tracing to verify that the generated code for a model element meets the assigned 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 either the:

  • Code View: View the generated code in the Code view of the Code perspective.

  • Code Generation Report: View the generated code as an HTML report in the MATLAB® web browser.

The generated code includes 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 in the header files.

  • 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 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 C Code 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 C Code tab, click Settings 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 C Code tab, click Build. 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 C/C++ Code > Navigate to C/C++ Code. 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 C/C++ Code > Navigate to C/C++ Code. 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, under Reference 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>/block_name.

  • system is one of the following:

    • The text Root

    • A unique system number assigned by the Simulink engine

  • block_name is the name of the source block.

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.
 *
 * <Root> : foo
 * <S1>   : foo/Outer
 * <S2>   : foo/Outer/Inner
 */

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: '<Root>/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: '<S1>/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 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[]

Traceability Limitations

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 (/).

    • 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