Generate C/C++ Code with Improved MISRA and AUTOSAR Compliance
The Motor Industry Software Reliability Association (MISRA™)1 has established “Guidelines for the Use of the C Language in Critical Systems” (MISRA C™) and “Guidelines for the Use of the C++ Language in Critical Systems”. The AUTomotive Open System ARchitecture (AUTOSAR) development partnership has also established "Guidelines for the Use of the C++14 Language in Critical and Safety-Related Systems". When generating C or C++ code for your MATLAB® code base, you can configure the code generator to produce code that is compliant with a large subset of these coding rules.
Scope of Code Generation Support
When you generate C or C++ code from MATLAB code, you can configure the code generator to maximize the compliance of the generated code with the MISRA C:2012, MISRA C:2023, MISRA C++:2008, and AUTOSAR C++14 guidelines.
To maximize compliance of the generated code:
Set code generation configuration properties to the recommended values. You can do this either at the command line or in the MATLAB Coder™ app. See Configure Code Generation Configuration Object Properties at Command Line and Configure MATLAB Coder App Settings.
Make sure any user-written C/C++ files or third-party C/C++ files that you integrate with the generated code by using
coder.ceval
are compliant with the MISRA and AUTOSAR guidelines.
The generated code might include one or more of these files that are not compliant with MISRA and AUTOSAR guidelines:
coder_posix_time.h
coder_posix_time.c
rt_defines.h
rt_nonfinite.h
rtGetInf.h
rtGetNaN.h
rtwhalf.h
rtwhalf.c
rtwhalf.cpp
rtwtypes.h
In certain situations, you might be able to configure the code generator to avoid
using the custom type definitions contained in the rtwtypes.h
file in
the generated code. For example, see Additional Settings for MISRA C++ Compliance.
The code generator supports a large subset of the required and mandatory coding rules in the MISRA and AUTOSAR guidelines. To see a complete list, refer to these topics:
Code generation interprets these rules in the same way as the Polyspace® Bug Finder™ product. To learn about how the code generator interprets a particular coding rule, see the Polyspace Implementation section in the corresponding reference page in the Polyspace Bug Finder documentation:
MISRA C:2012 Directives and Rules (Polyspace Bug Finder)
MISRA C:2023 Directives and Rules (Polyspace Bug Finder)
MISRA C++:2008 Rules (Polyspace Bug Finder)
AUTOSAR C++14 Rules (Polyspace Bug Finder)
Configure Code Generation Configuration Object Properties at Command Line
Create an Embedded Coder® code generation configuration object.
cfg = coder.config('lib','ecoder',true);
Set the properties in the following table to the values for improved MISRA compliance by using the
coder.setupMISRAConfig
function.coder.setupMISRAConfig(cfg);
Property Value for Improved MISRA Compliance CastingMode
'Standards'
CppGenerateEnumClass
(if target language is C++)true
CppInterfaceClassName
(if target language is C++)''
CppInterfaceStyle
(if target language is C++)'Functions'
CppNamespace
(if target language is C++)Valid namespace name for C++
DataTypeReplacement
'CoderTypedefs'
EnableDynamicMemoryAllocation
false
EnableOpenMP
false
EnableRuntimeRecursion
false
EnableSignedLeftShifts
false
EnableSignedRightShifts
false
GenerateDefaultInSwitch
true
HeaderGuardStyle
'UseIncludeGuard'
JustifyMISRAViolations
true
MaxIdLength
(if target language is C)31
ParenthesesLevel
'Maximum'
RuntimeChecks
false
TargetLangStandard
'C99 (ISO)'
for C,'C++11 (ISO)'
for C++Note
GenerateComments
must be set totrue
to add MISRA and AUTOSAR annotations usingJustifyMISRAViolations
.coder.setupMISRAConfig
sets the parameters in this table to the recommended values for improved MISRA compliance. At this point, if you want to override any of these settings, you can modify those property values manually.If the
CppNamespace
property is unset, and theTargetLang
property is'C++'
, thencoder.setupMISRAConfig
sets theCppNamespace
property to a default character vector,'Codegen'
. Modify this value to a namespace name that is particular to your project.Pass the configuration object to
codegen
by using the-config
option.codegen myFunction -config cfg ...
Configure MATLAB Coder App Settings
On the Generate Code page, set Build type to
Source Code
,Static Library
,Dynamic Library
, orExecutable
.Click More Settings.
Click MISRA Compliance.
The app displays the settings that can impact MISRA compliance.
To set all of these settings to the recommended values, click Set to Recommended Values.
To modify an individual setting, click All Settings, and then search for the setting.
Additional Settings for MISRA C++ Compliance
By default, the C++ code that you generate uses a custom type definition for
bool
to maximize compatibility with certain precompiled
libraries. This custom type definition can cause violations for MISRA C++:2008 Rule 5-0-13 and Rule 5-3-1.
To use the built-in bool
type in generated C++ code and avoid these
violations, change the Hardware Board setting in the configuration
object from MATLAB Host Computer
. To see how to change this setting in the App,
see Access Hardware Implementation Properties with a Dialog Box. For example,
for code generation and build on a 64-bit Windows® platform, change the Hardware Board to
None - Select device below
, and then set Device
vendor to Intel
and Device
type to x86-64 (Windows64)
.
Check Compliance of Generated Code Using Static Analysis Tool
The best practice is to verify compliance of the generated code independently of the code generator. After you generate code, use a static analysis tool (such as Polyspace Bug Finder) to check the extent of compliance to the MISRA and AUTOSAR coding standards.
To learn how to use Polyspace Bug Finder to check C/C++ code for coding rule violations, see:
See Also
coder.EmbeddedCodeConfig
| coder.setupMISRAConfig
Related Topics
- MISRA C:2012 Coding Directives and Rules Supported for Code Generation
- MISRA C:2023 Coding Directives and Rules Supported for Code Generation
- MISRA C++:2008 and AUTOSAR C++14 Coding Rules Supported for Code Generation
- Polyspace Verification of C/C++ Code Generated by MATLAB Coder
- Run Polyspace Bug Finder on Desktop (Polyspace Bug Finder)
- Generate MISRA C:2012 Compliant Code and Verify with Polyspace (Polyspace Bug Finder)
External Websites
1 MISRA and MISRA C are registered trademarks of HORIBA MIRA Ltd., held on behalf of the MISRA Consortium.