Main Content

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:

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:

Configure Code Generation Configuration Object Properties at Command Line

  1. Create an Embedded Coder® code generation configuration object.

    cfg = coder.config('lib','ecoder',true);
    

  2. Set the properties in the following table to the values for improved MISRA compliance by using the coder.setupMISRAConfig function.

    coder.setupMISRAConfig(cfg);

    PropertyValue 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'
    EnableDynamicMemoryAllocationfalse
    EnableOpenMPfalse
    EnableRuntimeRecursionfalse
    EnableSignedLeftShiftsfalse
    EnableSignedRightShiftsfalse
    GenerateDefaultInSwitchtrue
    HeaderGuardStyle'UseIncludeGuard'
    JustifyMISRAViolationstrue
    MaxIdLength (if target language is C)31
    ParenthesesLevel'Maximum'
    RuntimeChecksfalse
    TargetLangStandard 'C99 (ISO)' for C, 'C++11 (ISO)' for C++

    Note

    GenerateComments must be set to true to add MISRA and AUTOSAR annotations using JustifyMISRAViolations.

    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 the TargetLang property is 'C++', then coder.setupMISRAConfig sets the CppNamespace property to a default character vector, 'Codegen'. Modify this value to a namespace name that is particular to your project.

  3. Pass the configuration object to codegen by using the -config option.

    codegen myFunction -config cfg ...

Configure MATLAB Coder App Settings

  1. On the Generate Code page, set Build type to Source Code, Static Library, Dynamic Library, or Executable.

  2. Click More Settings.

  3. Click MISRA Compliance.

    The app displays the settings that can impact MISRA compliance.

    Screenshot of MISRA compliance pane in the MATLAB Coder app.

  4. 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

|

Related Topics

External Websites


1 MISRA and MISRA C are registered trademarks of HORIBA MIRA Ltd., held on behalf of the MISRA Consortium.