Main Content

MISRA C:2023 Compliance Summary Tables

MathWorks® evaluates C code generated by C2000™ Microcontroller Blockset from Simulink® models and Stateflow® charts against the MISRA™ C:2023 coding standards. The results from this effort are available in these compliance summary tables. These tables also identify methods used to obtain compliance:

  • Compliant: Compliance to the rule/directive is achieved through adherence to the code generation process, modeling guidelines, or Model Advisor checks. When applicable, there are explanatory notes that provide information relevant to compliance methods or actions that you can perform to satisfy the directive or rule.

  • Deviation: The rule or directive is not compliant.

These tables align with the published MISRA C:2023 document. The MISRA C:2023 standard consolidates these guidelines:

  • MISRA C:2012 rules and directives.

  • Clarifications added in technical corrigendum 1 (TC1) and technical corrigendum 2 (TC2).

  • New rules and modifications added in amendment 1 (AMD1), amendment 2 (AMD2), amendment 3 (AMD3), and amendment 4 (AMD4).

You can use these tables when preparing the MISRA compliance statement for your project, as required per section 5.3 of the MISRA C:2012 Guidelines for the Use of C Language in Critical Systems document. The tables do not include MISRA C:2012 Amendment 4, as C2000 Microcontroller Blockset does not generate code that includes multi-threading and atomic operations. The categories in the table are based on Appendix E of the MISRA guidelines, identified for automatically generated code. They can be used for mandatory and required categories only (not for advisory and readability categories).

Note

The generated code from C2000 Microcontroller Blockset is compliant with a large subset of these coding guidelines. The noncompliant status of a rule/directive in these tables does not come under mandatory and required categories identified for automatically generated code. The evaluation does not include user-defined functions.

"The Implementation"

DirectiveDescriptiona CategoryCompliance
Dir 1.1Any implementation-defined behavior on which the output of the program depends shall be documented and understood.Required

Compliant

a Copyright © The MISRA Consortium Limited, 2023.

"Compilation and Build"

DirectiveDescriptiona CategoryCompliance
Dir 2.1All source files shall compile without any compilation errors.RequiredCompliant

a Copyright © The MISRA Consortium Limited, 2023.

"Requirements Traceability"

DirectiveDescriptiona CategoryCompliance
Dir 3.1All code shall be traceable to documented requirements.Required

Compliant:

a Copyright © The MISRA Consortium Limited, 2023.

"Code Design"

DirectiveDescriptiona CategoryCompliance
Dir 4.1Run-time failures shall be minimized.Required

Compliant

Dir 4.3Assembly language shall be encapsulated and isolated.Required

Compliant:

Dir 4.10Precautions shall be taken in order to prevent the contents of a header file being included more than once.RequiredCompliant

a Copyright © The MISRA Consortium Limited, 2023.

"Standard C Environment"

RuleDescriptiona CategoryCompliance
1.5Obsolescent language features shall not be used.RequiredNoncompliant

a Copyright © The MISRA Consortium Limited, 2023.

"Unused Code"

RuleDescriptiona CategoryCompliance
2.1A project shall not contain unreachable code.Required

Compliant:

2.2A project shall no contain dead code.RequiredCompliant

a Copyright © The MISRA Consortium Limited, 2023.

"Comments"

RuleDescriptiona CategoryCompliance
3.1The character sequences /* and // shall not be used within a comment.Required

Compliant:

3.2Line-splicing shall not be used in // comments.RequiredCompliant

a Copyright © The MISRA Consortium Limited, 2023.

"Character Sets and Lexical Conventions"

RuleDescriptiona CategoryCompliance
4.1Octal and hexadecimal escape sequences shall be terminated.RequiredCompliant

a Copyright © The MISRA Consortium Limited, 2023.

"Identifiers"

RuleDescriptiona Categoryb Compliance
5.1External identifiers shall be distinct.Required

Compliant:

5.8Identifiers that define objects or functions with external linkage shall be unique.Required

Compliant:

a Copyright © The MISRA Consortium Limited, 2023.

b Guideline identifies Rule 5.3 as Required for manually generated code.

"Declarations and Definitions"

RuleDescriptiona Categoryb Compliance
8.2Function types shall be in prototype form with named parameters.RequiredCompliant
8.3All declarations of an object or function shall use the same names and type qualifiers.RequiredCompliant
8.6An identifier with external linkage shall have exactly one external definition.RequiredCompliant
8.8The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage.RequiredCompliant

a Copyright © The MISRA Consortium Limited, 2023.

b Guideline identifies Rule 8.4, 8.5, and 8.14 as Required for manually generated code.

"Initialization"

RuleDescriptiona Categoryb Compliance
9.1The value of an object with automatic storage duration shall not be read before it has been set.Mandatory

Compliant:

a Copyright © The MISRA Consortium Limited, 2023.

b Guideline identifies Rule 9.2, 9.3, and 9.5 as Required for manually generated code.

"Pointer Type Conversion"

RuleDescriptiona Categoryb Compliance
11.1Conversions shall not be performed between a pointer to a function and any other type.RequiredCompliant
11.3A cast shall not be performed between a pointer to object type and a pointer to a different object type.Required

Compliant

11.6A cast shall not be performed between pointer to void and an arithmetic type.RequiredCompliant
11.8A cast shall not remove any const or volatile qualification from the type pointed to by a pointer.Required

Compliant:

a Copyright © The MISRA Consortium Limited, 2023.

b Guideline identifies Rule 11.9 as Required for manually generated code.

"Expressions"

RuleDescriptiona CategoryCompliance
12.2The right hand operand of a shift operator shall lie in the range zero to one less than the width in bits of the essential type of the left hand operand. Required

Compliant:

a Copyright © The MISRA Consortium Limited, 2023.

"Side Effects"

RuleDescriptiona CategoryCompliance
13.5The right hand operand of a logical && or || operator shall not contain persistent side effects.Required

Compliant:

Deviation:

a Copyright © The MISRA Consortium Limited, 2023.

"Control Statement Expressions"

RuleDescriptiona Categoryb Compliance
14.3Controlling expressions shall not be invariant.Required

Compliant

a Copyright © The MISRA Consortium Limited, 2023.

b Guideline identifies Rule 14.1, 14.2, and 14.4 as Required for manually generated code.

"Control Flow"

RuleDescriptiona Categoryb Compliance
15.6The body of an iteration-statement or a selection-statement shall be a compound-statement.RequiredCompliant

a Copyright © The MISRA Consortium Limited, 2023.

b Guideline identifies Rule 15.2, 15.3, and 15.7 as Required for manually generated code.

"Functions"

RuleDescriptiona Categoryb Compliance
17.3A function shall not be declared implicitly. Function 'InitGpio' does not have complete prototype when called (argument list is missing).Mandatory

Compliant:

a Copyright © The MISRA Consortium Limited, 2023.

b Guideline identifies Rule 17.7 as Required for manually generated code.

"Preprocessing Directives"

RuleDescriptiona CategoryCompliance
20.9All identifiers used in the controlling expression of #if or #elif preprocessing directives shall be #define'd before evaluation.RequiredCompliant

a Copyright © The MISRA Consortium Limited, 2023.

"Standard Libraries"

RuleDescriptiona CategoryCompliance
21.1#define and #undef shall not be used on a reserved identifier or reserved macro name.Required

Compliant:

21.2A reserved identifier or reserved macro name shall not be declared.Required

Compliant

Explanatory Note for Rule 17.3 and 21.2

a Copyright © The MISRA Consortium Limited, 2023.

Explanatory Notes

Explanatory Note for Directive 1.1

Information about the implementation-defined behavior for Embedded Coder® is available in Configure Run-Time Environment Options (Embedded Coder). Compiler documentation is out of scope.

Character set encoding is managed by using the SavedCharacterEncoding model parameter. For additional information, see slCharacterEncoding.

Configure the integer division method in the Model Configuration Parameters dialog box, on the Hardware Implementation pane. For additional information, see Configure Run-Time Environment Options (Embedded Coder)

Embedded Coder generates #pragma when the user:

In both instances, you are responsible for documenting the intended use of the #pragma. For more information, see Control Data and Function Placement in Memory by Inserting Pragmas (Embedded Coder).

To enable the generation of bitfields:

  1. Select at least one of these model configuration parameters:

  2. Create a custom storage class with defined bitfields. See Create Storage Classes by Using the Custom Storage Class Designer (Embedded Coder) for more information.

If your model uses conversion of one datatype to another, Polyspace® Checker might show violation of Dir 1.1.

Explanatory Note for Directive 3.1

You can link requirements model elements. These links are included in the generated C code to provide traceability from a requirements document, to the model elements, and to the generated code. For additional information, see View Requirements Toolbox Links Associated with Model Elements.

Explanatory Note for Directive 4.1

You can use Polyspace Bug Finder™ to identify run-time errors and Polyspace Code Prover™ to prove the absence of run-time errors. For information, see:

Simulink Design Verifier can be used to detect design errors at the model level.

Explanatory Note for Directive 4.3

Embedded Coder does not directly call assembly language code. You can add calls to assembly language functions through S-functions, code replacement libraries, Stateflow, and in MATLAB® blocks. These calls are documented as calls to External C Functions (Embedded Coder). In these cases, you are responsible for encapsulation.

For additional information, see:

Explanatory Note for Directive 4.6

Embedded Coder provides three options, which are compatible with Directive 4.6. You can:

  • Use typedefs that use specified-width integer data types defined in the generated rtwtypes.h file. For example, uint8_T.

  • Use specified-width integer data types from stdint.h. For example, uint8_t.

  • Specify custom names for fixed-width integer data types.

With the first and second options, the generated code might contain integers that are not specified-width. For example, int_T or int in a generated main file or Stateflow counter variable. To replace the integers with specified-width integers, use data type replacement.

If you configure the model to generate AUTOSAR code, Embedded Coder generates code that uses only specified-width integer data types.

For more information, see:

Explanatory Note for Directive 4.11

The requirements of this directive are satisfied by:

“Demonstrate statically that the input parameters can never take invalid values”.

You can use Polyspace Code Prover to analyze parameter ranges and prove the absence of run-time errors caused by out-of-range values. For additional information, see Run Polyspace Analysis on Code Generated with Embedded Coder (Polyspace Code Prover).

Explanatory Note for Rule 2.1

Embedded Coder does not check user defined code from the Stateflow. You must take care of the violations reported.

Explanatory Note for Rules 5.1, 5.2, 5.4, 5.5, 5.6, 5.7, and 5.8

Embedded Coder is configurable to limit the number of characters imposed by the implementation. For additional information, see Maximum identifier length (Simulink Coder).

To ensure unique names for different types of variables (local scope variables, global scope variables, macros, and so on), implement a naming convention. For additional information, see Model Configuration Parameters: Code Generation Symbols (Simulink Coder).

In case of long identifiers, it is recommended to use shared utilities. For more information, see Generate Shared Utility Code (Embedded Coder)

Explanatory Note for Rule 8.12

Embedded Coder supports the use of enumerated data. The file used to define the enumeration can be either manually or automatically generated. Files defining enumerations generated by Embedded Coder are compliant with MISRA C:2012 Rule 8.12 by design. If you manually create the definition file, you are responsible for ensuring compliance. For additional information, see Use Enumerated Data in Simulink Models.

Explanatory Note for Rule 9.1

Simulink models configured for support packages like Simulink Support Package for Raspberry Pi® Hardware, C2000 Microcontroller Blockset, or such configurations may generate noncompliant code.

Note

Third-party files such as drivers used with code that is generated using Embedded Coder are not considered in the evaluation.

Explanatory Note for Rule 10.1, 10.3, and 10.4

Usage of functional mockup units (FMUs) version 1.0 may cause noncompliant code.

Explanatory Note for Rule 11.8

If your model uses any storage class with Const or Volatile qualifiers, Polyspace may show violation of Rule 11.8 from the generated code of the model.

Explanatory Note for Rule 12.2

If the simulation diagnostic reports an overflow from the Simulink model, the code generator may not generate MISRA compliant code.

Explanatory Note for Rule 13.2

Use of volatile variables may result in noncompliant code.

Explanatory Note for Rule 17.3 and 21.2

When the model configuration parameter Leverage target hardware instruction set extensions is set to anything other than None, the generated code may show violation of MISRA C:2012 rules.

Explanatory Note for Rule 21.1

If Polyspace show violation of Rule 21.1, run set_param(model, 'DataTypeReplacement', 'CDataTypesFixedWidth').

See Also

(Embedded Coder) | (Embedded Coder)