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"
"Compilation and Build"
"Requirements Traceability"
Directive | Descriptiona | Category | Compliance |
---|---|---|---|
Dir 3.1 | All code shall be traceable to documented requirements. | Required |
Compliant:
|
a Copyright © The MISRA Consortium Limited, 2023. |
"Code Design"
Directive | Descriptiona | Category | Compliance |
---|---|---|---|
Dir 4.1 | Run-time failures shall be minimized. | Required | Compliant
|
Dir 4.3 | Assembly language shall be encapsulated and isolated. | Required |
Compliant:
|
Dir 4.10 | Precautions shall be taken in order to prevent the contents of a header file being included more than once. | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. |
"Standard C Environment"
"Unused Code"
Rule | Descriptiona | Category | Compliance |
---|---|---|---|
2.1 | A project shall not contain unreachable code. | Required |
Compliant:
|
2.2 | A project shall no contain dead code. | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. |
"Comments"
Rule | Descriptiona | Category | Compliance |
---|---|---|---|
3.1 | The character sequences /* and //
shall not be used within a comment. | Required |
Compliant:
|
3.2 | Line-splicing shall not be used in //
comments. | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. |
"Character Sets and Lexical Conventions"
"Identifiers"
Rule | Descriptiona | Categoryb | Compliance |
---|---|---|---|
5.1 | External identifiers shall be distinct. | Required |
Compliant:
|
5.8 | Identifiers 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"
Rule | Descriptiona | Categoryb | Compliance |
---|---|---|---|
8.2 | Function types shall be in prototype form with named parameters. | Required | Compliant |
8.3 | All declarations of an object or function shall use the same names and type qualifiers. | Required | Compliant |
8.6 | An identifier with external linkage shall have exactly one external definition. | Required | Compliant |
8.8 | The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage. | Required | Compliant |
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"
Rule | Descriptiona | Categoryb | Compliance |
---|---|---|---|
9.1 | The 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"
Rule | Descriptiona | Categoryb | Compliance |
---|---|---|---|
11.1 | Conversions shall not be performed between a pointer to a function and any other type. | Required | Compliant |
11.3 | A cast shall not be performed between a pointer to object type and a pointer to a different object type. | Required | Compliant |
11.6 | A cast shall not be performed between pointer to void and an arithmetic type. | Required | Compliant |
11.8 | A 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"
Rule | Descriptiona | Category | Compliance |
---|---|---|---|
12.2 | The 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"
Rule | Descriptiona | Category | Compliance |
---|---|---|---|
13.5 | The 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"
"Control Flow"
"Functions"
Rule | Descriptiona | Categoryb | Compliance |
---|---|---|---|
17.3 | A 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"
"Standard Libraries"
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:
Selects the use of memory sections (Simulink Coder)
Creates a custom storage class with memory sections (Embedded Coder)
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:
Select at least one of these model configuration parameters:
Pack Boolean data into bitfields (Embedded Coder). This parameter is available only for ERT-based system target files (Embedded Coder).
Use bitsets for storing state configuration (Simulink Coder)
Use bitsets for storing Boolean data. (Simulink Coder)
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:
Configure and Run Analysis (Polyspace Code Prover)
Configure and Run Analysis (Polyspace Bug Finder)
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:
External C Functions (Embedded Coder)
Code Replacement Customization (Embedded Coder)
Control Data and Function Placement in Memory by Inserting Pragmas (Embedded Coder)
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:
Manage Replacement of Simulink Data Types in Generated Code (Embedded Coder)
Replace and Rename Simulink Coder Data Types to Conform to Coding Standards (Embedded Coder)
Typedefs (Embedded Coder)
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
MISRA C:2012 Compliance Summary Tables (Embedded Coder) | CERT C Compliance Summary Tables (Embedded Coder)