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
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
|Source of Name-Mangling Text
Automatically assigned Simulink identifier
Full name of parameter owner (model or block) and parameter name
Signal name, full name of source block, and port number
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 best practices:
Avoid using default block names when the model includes multiple blocks of the same type.
Make subsystems atomic and reusable.
Increase the value of the Maximum identifier length 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
$Rtoken (in addition to the
$Mtoken) in the Identifier format control parameter specifications for the higher-level model. The
$Rtoken expands to the root model name.
The Maximum identifier length parameter value must be large enough to accommodate full expansions of the
$Mtokens. 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
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
In the Modeling tab, click Model Advisor.
Select By Task.
Select Model Referencing
Run the Check code generation identifier formats used for model reference check.