verify generated code by using code tracing -凯发k8网页登录

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 .

matlab function blocks that you insert directly into a simulink model are also traceable. for more information, see .

traceability in generated code

code view

  1. open the model .

  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. open the model .

  2. open the embedded coder app. on the c code tab, click settings to open the configuration parameters dialog box. select if it is not already selected. by default, , , and are selected.

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

  4. build the model. on the c code tab, click build. the build process opens the code generation report in a matlab web browser.

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

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

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

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

  9. 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.
 *
 *  : foo
 *    : foo/outer
 *    : 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: '/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 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

    网站地图