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++: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.cevalare 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:
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:
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
Property Value for Improved MISRA Compliance
CppGenerateEnumClass(if target language is C++)
CppInterfaceClassName(if target language is C++)
CppInterfaceStyle(if target language is C++)
CppNamespace(if target language is C++)
Valid namespace name for C++
MaxIdLength(if target language is C)
'C99 (ISO)'for C,
'C++11 (ISO)'for C++
GenerateCommentsmust be set to
trueto add MISRA and AUTOSAR annotations using
coder.setupMISRAConfigsets 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.
CppNamespaceproperty is unset, and the
CppNamespaceproperty to a default character vector,
'Codegen'. Modify this value to a namespace name that is particular to your project.
Pass the configuration object to
codegenby using the
codegen myFunction -config cfg ...
Configure MATLAB Coder App Settings
On the Generate Code page, set Build type to
Dynamic Library, or
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
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
Intel and Device
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:
- C Coding Directives and Rules Supported for Code Generation
- C++ 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)
1 MISRA and MISRA C are registered trademarks of HORIBA MIRA Ltd., held on behalf of the MISRA Consortium.