Skip to Main Content Skip to Search
Product Documentation

Code Generation Pane: Code Style

Code Generation: Code Style Tab Overview

Control optimizations for readability in generated code.

Configuration

This tab appears only if you specify an ERT based system target file.

See Also

Parentheses level

Specify parenthesization style for generated code.

Settings

Default: Nominal (Optimize for readability)

Minimum (Rely on C/C++ operators for precedence)

Inserts parentheses only where required by ANSI[1] C or C++, or to override default precedence. For example:

isZero = var == 0;
if (isZero == 1 && (value < 3.7 ||value > 9.27)) {
   /* code */
   }
Nominal (Optimize for readability)

Inserts parentheses in a way that compromises between readability and visual complexity. The exact definition can change between releases.

Maximum (Specify precedence with parentheses)

Includes parentheses to specify meaning without relying on operator precedence. Code generated with this setting conforms to MISRA®[2] requirements. For example:

isZero = (var == 0);
if ((isZero == 1) && ((value < 3.7) || (value > 9.27))) {
   /* code */
   }

Command-Line Information

Parameter: ParenthesesLevel
Type: string
Value: 'Minimum' | 'Nominal' | 'Maximum'
Default: 'Nominal'

Recommended Settings

ApplicationSetting
DebuggingNominal (Optimized for readability)
TraceabilityNominal (Optimized for readability)
EfficiencyMinimum (Rely on C/C++ operators for precedence)
Safety precautionMaximum (Specify precedence with parentheses)

See Also

Controlling Parenthesization

Preserve operand order in expression

Specify whether to preserve order of operands in expressions.

Settings

Default: off

On

Preserves the expression order specified in the model. Select this option to increase readability of the code or for code traceability purposes.

A*(B+C)
Off

Optimizes efficiency of code for nonoptimized compilers by reordering commutable operands to make expressions left-recursive. For example:

(B+C)*A

Command-Line Information

Parameter: PreserveExpressionOrder
Type: string
Value: 'on' | 'off'
Default: 'off'

Recommended Settings

ApplicationSetting
DebuggingOn
TraceabilityOn
EfficiencyOff
Safety precautionOn

Preserve condition expression in if statement

Specify whether to preserve empty primary condition expressions in if statements.

Settings

Default: off

On

Preserves empty primary condition expressions in if statements, such as the following, to increase the readability of the code or for code traceability purposes.

if expression1
else
       statements2;
end
Off

Optimizes empty primary condition expressions in if statements by negating them. For example, consider the following if statement:

if expression1
else
       statements2;
end

By default, the code generator negates this statement as follows:

if ~expression1
        statements2;
end

Command-Line Information

Parameter: PreserveIfCondition
Type: string
Value: 'on' | 'off'
Default: 'off'

Recommended Settings

ApplicationSetting
DebuggingOn
TraceabilityOn
EfficiencyOff
Safety precautionOn

Convert if-elseif-else patterns to switch-case statements

Specify whether to generate code for if-elseif-else decision logic as switch-case statements.

This readability optimization works on a per-model basis and applies only to:

Settings

Default: off

On

Generate code for if-elseif-else decision logic as switch-case statements.

For example, assume that you have the following logic pattern:

if (x == 1) {
       y = 1;
} else if (x == 2) {
       y = 2;
} else if (x == 3) {
       y = 3;
} else {
       y = 4;
}

Selecting this check box converts the if-elseif-else pattern to the following switch-case statements:

switch (x) {
     case 1:
       y = 1; break;
     case 2:
       y = 2; break;
     case 3:
       y = 3; break;
     default:
       y = 4; break;
}
Off

Preserve if-elseif-else decision logic in generated code.

Command-Line Information

Parameter: ConvertIfToSwitch
Type: string
Value: 'on' | 'off'
Default: 'off'

Recommended Settings

ApplicationSetting
DebuggingNo impact
TraceabilityOff
EfficiencyOn (execution, ROM), No impact (RAM)
Safety precautionNo impact

See Also

Preserve extern keyword in function declarations

Specify whether to include the extern keyword in function declarations in the generated code.

Settings

Default: on

On

Include the extern keyword in function declarations in the generated code. For example, the generated code for the demo model rtwdemo_hyperlinks contains the following function declarations in rtwdemo_hyperlinks.h:

/* Model entry point functions */
extern void rtwdemo_hyperlinks_initialize(void);
extern void rtwdemo_hyperlinks_step(void);

The extern keyword explicitly indicates that the function has external linkage. The function definitions in this example are in the generated file rtwdemo_hyperlinks.c.

Off

Remove the extern keyword from function declarations in the generated code.

Command-Line Information

Parameter: PreserveExternInFcnDecls
Type: string
Value: 'on' | 'off'
Default: 'on'

Recommended Settings

ApplicationSetting
DebuggingNo impact
TraceabilityNo impact
EfficiencyNo impact
Safety precautionNo impact

See Also

For more information on code style options, see Code Generation Pane: Code Style.

Suppress generation of default cases for Stateflow switch statements if unreachable

Specify whether or not to generate default cases for switch-case statements in the code for Stateflow charts. This optimization works on a per-model basis and applies to the code generated for a state that has multiple substates. For a list of the state functions in the generated code, see Controlling Inlining of State Functions in Generated Code in the Stateflow documentation.

Settings

Default: off

On

Do not generate the default case when it is unreachable. This setting enables better code coverage because every branch in the generated code is falsifiable.

Off

Always generate a default case whether or not it is reachable. This setting supports MISRA C® compliance and provides a fallback in case of RAM corruption.

For example, when the state has a nontrivial entry function, the following default case appears in the generated code for the during function:

default:
 entry_internal();
 break;

In this case, the code marks the the corresponding substate as active.

Command-Line Information

Parameter: SuppressUnreachableDefaultCases
Type: string
Value: 'on' | 'off'
Default: 'off'

Recommended Settings

ApplicationSetting
DebuggingNo impact
TraceabilityOn
EfficiencyOn (execution, ROM), No impact (RAM)
Safety precautionOff

See Also

For more information on code style options, see Code Generation Pane: Code Style.


[1] ANSI is a registered trademark of the American National Standards Institute, Inc.

[2] MISRA is a registered trademarks of MIRA Ltd, held on behalf of the MISRA Consortium.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS