Main Content

Avoid Identifier Naming Collisions

When you generate code from a model, the code generator attempts to avoid naming collisions by adding name-mangling text to identifiers that would otherwise share the same name. The name-mangling text minimizes the impact that model revisions have on identifier names in the generated code, which helps maintain traceability. However, the best practice is to minimize the use of name-mangling text by choosing unique names for Simulink objects as much as possible.

For models for which you cannot avoid using name-mangling text, use model configuration parameters to configure the name-mangling text.

Name-Mangling Text Generation Overview

The position of the $M token in an identifier specification determines where the code generator inserts name-mangling text. You must include the $M token in identifier format specifications.

The length of the name-mangling text scales with the number of identifiers involved in a naming collision. This allows for unique name-mangling text for each generated identifier. For example, the name-mangling text xyz might change to xyzQ if the number of collisions increases and might change to xy if the number of collisions decreases.

Minimize Changes to Name-Mangling Text

You can minimize changes to the name-mangling text in the generated code by using the Minimum mangle length configuration parameter to set a minimum length for the name-mangling text. For example, the value 4 is a conservative option that allows for over 1.5 million naming collisions for a particular identifier before the mangle length increases.

Each Simulink® object has an associated string that the code generator uses to seed the generation of name-mangling text. This string is the source of mangling text.

  • If you do not change the source of mangling text, the code generator generates the same name-mangling text each time you generate code.

  • If you change the source of mangling text associated with a Simulink object, the code generator generates different name-mangling text for that object.

To keep name-mangling text consistent between iterations, avoid changing the source.

Source of Name-Mangling Text per Object

Object TypeSource of Name-Mangling Text


Model name

Simulink block

Automatically assigned Simulink identifier

Simulink parameter

Full name of parameter owner (model or block) and parameter name

Simulink signal

Signal name, full name of source block, and port number

Stateflow® objects

Complete path to Stateflow block and Stateflow computed name (unique within chart)

Avoid Truncated Identifiers

If the name-mangling text causes a generated identifier name to be longer than the maximum number of characters that you specify in the Maximum identifier length model configuration parameter, the code generator truncates parts of the identifier name other than the name-mangling text. To avoid truncating identifier names, follow these practices:

  • Avoid using default block names when the model includes multiple blocks of the same type.

  • Make subsystems atomic and reusable.

  • Set the System-generated identifiers model configuration parameter to Shortened.

  • Increase the value of the Maximum identifier length model configuration parameter if necessary to accommodate the identifiers you expect to generate.

If the Maximum identifier length value is too small to fully accommodate the name-mangling text, the code generator returns an error.

Avoid Identifier Naming Collisions Between Referenced Models

In a model that uses referenced models, the code generator does not allow naming collisions between the names of the models. When you generate code from a model that uses model referencing:

  • You must include the $R token (in addition to the $M token) in the Identifier format control parameter specifications for the higher-level model. The $R token expands to the root model name.

  • The Maximum identifier length parameter value must be large enough to accommodate full expansions of the $R and $M tokens. If the value is too small, the code generator returns an error.

When a naming collision occurs between an identifier within the scope of a higher-level model and an identifier within the scope of a referenced model, the code generator:

  • Preserves the identifier from the referenced model

  • Adds name-mangling text to the identifier from the higher-level model

If your model contains two referenced models with the same inport or outport names and one of the referenced models contains an atomic subsystem that has Function packaging set to Nonreusable function, when you generate code, the naming collision might cause an error.

Use Model Advisor to Detect Identifier Names Changed During Code Generation

For a referenced model, if these parameters have specifications that do not contain a $R token, the code generator prepends the $R token to the identifier format specification:

You can use the Model Advisor to identify referenced models in a model referencing hierarchy for which the code generator prepends $R.

  1. In the Modeling tab, click Model Advisor.

  2. Select By Task.

  3. Select Model Referencing

  4. Run the Check code generation identifier formats used for model reference check.

Name Mangling Limitations

The code generator cannot detect identifier naming collisions that involve certain types of identifiers. Generated code might contain uncompilable conflicts with these types of identifiers:

  • Model methods

  • Reentrant model function arguments

  • Local functions and variables generated by S-functions or by products such as DSP System Toolbox™ that rely on S-functions

  • Fixed-point shared utility macros and shared utility functions

  • Header guard macros

  • Generated typedef statements for global data types

  • Identifiers that you define in custom code

  • Identifiers that you define in custom header files

  • Identifiers that you define in custom TLC code

  • Identifiers from a non-ANSI® C standard library

See Also


Related Topics