MISRA C++:2023 Compliance Summary Tables
MathWorks® evaluates C++ code generated by Embedded Coder® from Simulink® models and Stateflow® charts against the MISRA™ C++:2023 coding standards. The results from this effort are available in the following compliance summary tables, which align with the MISRA C++:2023 Guidelines for the Use of C++17 in Critical Systems document.
The tables list rules and directives in the Mandatory and Required categories only. They
apply only to production code generated under the
build directory and model_target_rtwslprj/ project
directory. They do not apply to the following files:target
target_main.cpprtmodel.cpphalf_type.hhalf_type.cpp
The tables also do not apply to custom code, including the following:
Custom S-functions
Custom storage classes for signals and parameters
Custom memory section strings
coder.cevalcalls in MATLAB® Function blocks
Model Settings for Compliance
The tables in the Rules and Directives section assume that you have preconfigured your model to improve compliance. Before referencing the tables, verify that:
Model configuration parameters meet the
MISRA C++:2023 guidelinesCode Generation Advisor objective. For more information, see Configure Model for Code Generation Objectives by Using Code Generation Advisor.C++ code interface settings are correct. To verify the settings:
Open the Embedded Coder app. On the C++ Code tab, click the Code Interface menu.
From the Code Interface menu, select Class Name & Namespace. For C++ Class Namespace, enter the name of a C++ class namespace.
From the Code Interface menu, select Code Mappings. In the Data tab:
For the Inports and Outports categories, set Data Visibility to
privateand Member Access Method toInline structure-based method.For the Model parameter arguments, Model parameters, and Signals, states, and internal data categories, set Data Visibility to
privateand Member Access Method toNone.
In the Functions tab, for the Periodic category (if one exists), set Method Name to be the empty string. Then select Method Preview and set the function prototype to have no arguments.
Alternatively, run the following commands at the MATLAB prompt, where
is the name of your model.modelcm = coder.mapping.utils.create(model); setClassName(cm,"myModelClass"); setClassNamespace(cm,"myModelNamespace"); setData(cm,"Inports",DataVisibility="private"); setData(cm,"Inports",MemberAccessMethod="Inlined structure-based method"); setData(cm,"Outports",DataVisibility="private"); setData(cm,"Outports",MemberAccessMethod="Inlined structure-based method"); setData(cm,"ModelParameterArguments",DataVisibility="private"); setData(cm,"ModelParameterArguments",MemberAccessMethod="None"); setData(cm,"ModelParameters",DataVisibility="private"); setData(cm,"ModelParameters",MemberAccessMethod="None"); setData(cm,"InternalData",DataVisibility="private"); setData(cm,"InternalData",MemberAccessMethod="None"); setFunction(cm,"Periodic",MethodName=""); setFunction(cm,"Periodic",Arguments="()");
Rules and Directives
You can use Polyspace® Bug Finder™ to identify violations of rules and directives. For more information, see Running Bug Finder (Polyspace Bug Finder).
Run-Time Failures
Copy and Move Assignment Operators
Path Feasibility
Unused Values and Declarations
Implementation Compliance
Program Execution
Comments
Identifiers
Literals
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 5.13.1 | Within character literals and non raw-string literals,
\ shall only be used to form a defined escape
sequence or universal character name | Required | Compliant |
| 5.13.2 | Octal escape sequences, hexadecimal escape sequences and universal character names shall be terminated | Required | Noncompliant:
|
| 5.13.3 | Octal constants shall not be used | Required | Compliant |
| 5.13.4 | Unsigned integer literals shall be appropriately suffixed | Required | Compliant |
| 5.13.5 | The lowercase form of L shall not be used as the
first character in a literal suffix | Required | Compliant |
| 5.13.6 | An integer-literal of type long long shall not use
a single L or l in any suffix | Required | Compliant |
| 5.13.7 | String literals with different encoding prefixes shall not be concatenated | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Declarations and Definitions
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 6.0.1 | Block scope declarations shall not be visually ambiguous | Required | Compliant |
| 6.0.4 | The identifier main shall not be used for a function other than the
global function main | Required | Compliant |
| 6.2.1 | The one-definition rule shall not be violated | Required | Compliant |
| 6.2.2 | All declarations of a variable or function shall have the same type | Required | Compliant |
| 6.2.3 | The source code used to implement an entity shall appear only once | Required | Noncompliant:
|
| 6.2.4 | A header file shall not contain definitions of functions or objects that are non-inline and have external linkage | Required | Compliant |
| 6.4.1 | A variable declared in an inner scope shall not hide a variable declared in an outer scope | Required | Noncompliant |
| 6.4.2 | Derived classes shall not conceal functions that are inherited from their bases | Required | Compliant |
| 6.4.3 | A name that is present in a dependent base shall not be resolved by unqualified lookup | Required | Noncompliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Storage Duration and Object Lifetime
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 6.7.1 | Local variables shall not have static storage duration | Required | Noncompliant |
| 6.7.2 | Global variables shall not be used | Required | Noncompliant:
|
| 6.8.1 | An object shall not be accessed outside of its lifetime | Required | Compliant |
| 6.8.2 | A function must not return a reference or a pointer to a local variable with automatic storage duration | Mandatory | Compliant |
| 6.8.3 | An assignment operator shall not assign the address of an object with automatic storage duration to an object with a greater lifetime | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Types
Standard Conversions
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 7.0.1 | There shall be no conversion from type
bool | Required | Noncompliant:
|
| 7.0.2 | There shall be no conversion to type bool | Required | Noncompliant:
|
| 7.0.3 | The numerical value of a character shall not be used | Required | Noncompliant |
| 7.0.4 | The operands of bitwise operators and shift operators shall be appropriate | Required | Noncompliant:
|
| 7.0.5 | Integral promotion and the usual arithmetic conversions shall not change the signedness or the type category of an operand | Required | Noncompliant |
| 7.0.6 | Assignment between numeric types shall be appropriate | Required | Noncompliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Pointer Conversions
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 7.11.1 | nullptr shall be the only form of the
null-pointer-constant | Required | Noncompliant |
| 7.11.2 | An array passed as a function argument shall not decay to a pointer | Required | Noncompliant |
| 7.11.3 | A conversion from function type to pointer-to-function type shall only occur in appropriate contexts | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Primary Expressions
Postfix Expressions
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 8.2.1 | A virtual base class shall only be cast to a derived class by means
of dynamic_cast | Required | Compliant |
| 8.2.2 | C-style casts and functional notation casts shall not be used | Required | Noncompliant |
| 8.2.3 | A cast shall not remove any const or
volatile qualification from the type accessed via
a pointer or by reference | Required | Noncompliant |
| 8.2.4 | Casts shall not be performed between a pointer to function and any other type | Required | Compliant |
| 8.2.5 | reinterpret_cast shall not be used | Required | Noncompliant |
| 8.2.6 | An object with integral, enumerated, or pointer to
void type shall not be cast to a pointer
type | Required | Noncompliant:
|
| 8.2.8 | An object pointer type shall not be cast to an integral type other
than std::uintptr_t or
std::intptr_t | Required | Compliant |
| 8.2.9 | The operand to typeid shall not be an expression
of polymorphic class type | Required | Compliant |
| 8.2.10 | Functions shall not call themselves, either directly or indirectly | Required | Noncompliant:
|
| 8.2.11 | An argument passed via ellipsis shall have an appropriate type | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Arithmetic, Relational, and Logical Operators
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 8.7.1 | Pointer arithmetic shall not form an invalid pointer | Required | Compliant:
|
| 8.7.2 | Subtraction between pointers shall only be applied to pointers that address elements of the same array | Required | Compliant |
| 8.9.1 | The built-in relational operators >,
>=, < and
<= shall not be applied to objects of pointer
type, except where they point to elements of the same array | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Assignment and Compound Assignment
Statements
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 9.2.1 | An explicit type conversion shall not be an expression statement | Required | Compliant |
| 9.3.1 | The body of an iteration-statement or a selection-statement shall be a compound-statement | Required | Compliant |
| 9.4.1 | All if ... else if constructs shall be terminated
with an else statement | Required | Compliant |
| 9.4.2 | The structure of a switch statement shall be
appropriate | Required | Compliant |
| 9.5.2 | A for-range-initializer shall contain at most one
function call | Required | Compliant |
| 9.6.2 | A goto statement shall reference a label in a
surrounding block | Required | Compliant |
| 9.6.3 | The goto statement shall jump to a label declared
later in the function body | Required | Compliant |
| 9.6.4 | A function declared with the [[noreturn]]
attribute shall not return | Required | Compliant |
| 9.6.5 | A function with non-void return type shall return
a value on all paths | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Declarations
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 10.1.2 | The volatile qualifier shall be used
appropriately | Required | Compliant |
| 10.2.1 | An enumeration shall be defined with an explicit underlying type | Required | Noncompliant |
| 10.2.3 | The numeric value of an unscoped enumeration with no fixed underlying type shall not be used | Required | Compliant |
| 10.4.1 | The asm declaration shall not be used | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Declarators
Classes
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 12.2.2 | A bit-field shall have an appropriate type | Required | Compliant |
| 12.2.3 | A named bit-field with signed integer type shall not have a length of one bit | Required | Compliant |
| 12.3.1 | The union keyword shall not be used | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Derived Classes
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 13.1.2 | An accessible base class shall not be both virtual and non-virtual in the same hierarchy | Required | Compliant |
| 13.3.1 | User-declared member functions shall use the
virtual,override and
final specifiers appropriately | Required | Noncompliant:
|
| 13.3.2 | Parameters in an overriding virtual function shall not specify different default arguments | Required | Compliant |
| 13.3.3 | The parameters in all declarations or overrides of a function shall either be unnamed or have identical names | Required | Noncompliant |
| 13.3.4 | A comparison of a potentially virtual pointer to member function
shall only be with nullptr | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Special Member Functions
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 15.0.1 | Special member functions shall be provided appropriately | Required | Noncompliant:
|
| 15.1.1 | An object’s dynamic type shall not be used from within its constructor or destructor | Required | Compliant |
| 15.1.3 | Conversion operators and constructors that are callable with a single
argument shall be explicit | Required | Noncompliant |
| 15.1.5 | A class shall only define an initializer-list constructor when it is the only constructor | Required | Noncompliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Overloading
Templates
Exception Handling
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 18.1.1 | An exception object shall not have pointer type | Required | Compliant |
| 18.1.2 | An empty throw shall only occur within the compound-statement of a catch handler | Required | Compliant |
| 18.3.2 | An exception of class type shall be caught by
const reference or reference | Required | Compliant |
| 18.3.3 | Handlers for a function-try-block of a constructor or destructor shall not refer to non-static members from their class or its bases | Required | Compliant |
| 18.4.1 | Exception-unfriendly functions shall be noexcept
| Required | Noncompliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Preprocessing Directives
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 19.0.1 | A line whose first token is # shall be a valid
preprocessing directive | Required | Compliant:
|
| 19.0.2 | Function-like macros shall not be defined | Required | Noncompliant:
|
| 19.1.1 | The defined preprocessor operator shall be used
appropriately | Required | Compliant |
| 19.1.2 | All #else, #elif and
#endif preprocessor directives shall reside in
the same file as the #if, #ifdef
or #ifndef directive to which they are
related | Required | Compliant |
| 19.1.3 | All identifiers used in the controlling expression of
#if or #elif preprocessing
directives shall be defined prior to evaluation | Required | Noncompliant |
| 19.2.1 | Precautions shall be taken in order to prevent the contents of a header file being included more than once | Required | Compliant |
| 19.2.2 | The #include directive shall be followed by either
a <filename> or "filename"
sequence | Required | Compliant |
| 19.2.3 | The ' or " or
\ characters and the /* or
// character sequences shall not occur in a
header file name | Required | Compliant |
| 19.3.2 | A macro parameter immediately following a #
operator shall not be immediately followed by a ##
operator | Required | Compliant |
| 19.3.3 | The argument to a mixed-use macro parameter shall not be subject to further expansion | Required | Compliant |
| 19.3.4 | Parentheses shall be used to ensure macro arguments are expanded appropriately | Required | Compliant |
| 19.3.5 | Tokens that look like a preprocessing directive shall not occur within a macro argument | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Language Support Libraries
| Rule | Descriptiona | Category | Compliance |
|---|---|---|---|
| 21.2.1 | The library functions atof,
atoi, atol and
atoll from shall not be used | Required | Compliant |
| 21.2.2 | The string handling functions from <cstring>,
cstdlib>, <cwchar>, and
<cinttypes> shall not be used | Required | Noncompliant:
|
| 21.2.3 | The library function system from
<cstdlib>shall not be used | Required | Compliant |
| 21.2.4 | The macro offsetof shall not be used | Required | Compliant |
| 21.6.2 | Dynamic memory shall be managed automatically | Required | Compliant |
| 21.6.3 | Advanced memory management shall not be used | Required | Noncompliant |
| 21.6.4 | If a project defines either a sized or unsized version of a global
operator delete, then both shall be
defined | Required | Compliant |
| 21.6.5 | A pointer to an incomplete class type shall not be
deleted | Required | Compliant |
| 21.10.1 | The features of <cstdarg> shall not be used | Required | Compliant |
| 21.10.2 | The standard header file <csetjmp> shall not be
used | Required | Compliant |
| 21.10.3 | The facilities provided by the standard header file
<csignal> shall not be used | Required | Compliant |
| 22.3.1 | The assert macro shall not be used with a
constant-expression | Required | Compliant |
| 22.4.1 | The literal value zero shall be the only value assigned to
errno | Required | Compliant |
| 24.5.1 | The character handling functions from <cctype>
and <cwctype> shall not be used | Required | Compliant |
| 24.5.2 | The C++ Standard Library functions memcpy,
memmove and memcmp from shall
not be used | Required | Noncompliant:
|
| 25.5.1 | The setlocale and
std::locale::global functions shall not be
called | Required | Compliant |
| 25.5.2 | The pointers returned by the C++ Standard Library functions
localeconv, getenv,
setlocale or strerror must
only be used as if they have pointer to const-qualified type | Mandatory | Compliant |
| 25.5.3 | The pointer returned by the C++ Standard Library functions
asctime, ctime,
gmtime, localtime,
localeconv, getenv,
setlocale or strerror must not
be used following a subsequent call to the same function | Mandatory | Compliant |
| 28.3.1 | Predicates shall not have persistent side effects | Required | Compliant |
| 28.6.1 | The argument to std::move shall be a non-const
lvalue | Required | Compliant |
| 28.6.2 | Forwarding references and std::forward shall be
used together | Required | Compliant |
| 28.6.3 | An object shall not be used while in a potentially moved-from state | Required | Compliant |
| 28.6.4 | The result of std::remove,
std::remove_if, std::unique
and empty shall be used | Required | Compliant |
| 30.0.1 | The C Library input/output functions shall not be used | Required | Noncompliant:
|
| 30.0.2 | Reads and writes on the same file stream shall be separated by a positioning operation | Required | Compliant |
a Copyright © The MISRA Consortium Limited, 2023. | |||
Explanatory Notes
Explanatory Note for Directive 0.3.2, Rule 0.0.1, and Rule 8.7.1
To avoid noncompliant code, verify that model simulation does not error due to invalid inputs or out-of-range accesses.
Explanatory Note for Rule 0.1.2
To reduce violations, avoid using the String Concatenate block and blocks that produce zero crossings, such as triggered subsystems.
Explanatory Note for Rule 4.6.1
To reduce violations, avoid using the Rate Transition block.
Explanatory Note for Rule 5.10.1
To reduce violations, choose names for signals, subsystem functions, and parameters according to MISRA rules.
Explanatory Note for Rule 5.13.2
To reduce violations, do not use octal escape sequences, hexadecimal escape sequences, or universal character names as inputs for String Constant blocks.
Explanatory Note for Rule 6.2.3
To reduce violations, specify header files for your custom-defined enumeration, bus, and alias types. For more information, see Control File Placement of Custom Data Types.
Explanatory Note for Rule 6.7.2
To reduce violations, avoid assigning modeling elements to storage classes that generate global variables.
For a list of global storage classes, see Choose Storage Class for Controlling Data Representation in Generated Code and Use Built-In and Predefined Storage Classes to Represent Data in Generated Code.
Explanatory Note for Rule 7.0.1, 7.0.4, and 19.0.2
To reduce violations, avoid using blocks that produce zero crossings, such as triggered subsystems.
Explanatory Note for Rule 7.0.2
To reduce violations, avoid using the Discrete State-Space block.
Explanatory Note for Rule 8.2.6
To reduce violations, avoid using:
Message data types in Stateflow
n-D Lookup Table blocks
Explanatory Note for Rule 8.2.10
To reduce violations, avoid using recursive calls in Stateflow charts. For any 2-D Median (Computer Vision Toolbox) blocks, set the
Sort algorithm block parameter to Insertion
sort.
Explanatory Note for Rule 13.3.1
To reduce violations, avoid using:
Message data types in Simulink
Function Element blocks
Explanatory Note for Rule 15.0.1
To reduce violations, avoid using message data types in Simulink.
Explanatory Note for Rule 19.0.1
To avoid noncompliant code, assign memory sections with appropriate pre- and post-statements to your data. For more information, see Control Data and Function Placement in Memory by Inserting Pragmas.
Explanatory Note for Rule 21.2.2
To reduce violations, avoid using the String Concatenate and String to ASCII blocks.
Explanatory Note for Rule 24.5.2
To reduce violations, avoid using:
Message data types in Simulink and Stateflow
Discrete Zero-Pole and Discrete State-Space blocks
Explanatory Note for Rule 30.0.1
To reduce violations, avoid using:
Message data types in Simulink
C functions
fprintfandprintfin Stateflow charts
See Also
MISRA C:2012 Compliance Summary Tables | CERT C Compliance Summary Tables | MISRA C:2023 Compliance Summary Tables