メインコンテンツ

コード生成向けにサポートされている MISRA C:2023 コーディング命令およびルール

MATLAB® コードから C コードを生成するとき、MISRA™ C:2023 ガイドラインの "必要" と "必須" のコーディング ルールに最大限準拠したコードを生成するようにコード ジェネレーターを構成できます。

MISRA C:2023 規約では、次のガイドラインが統合されています。

  • MISRA C:2012 ルールおよび命令

  • MISRA C:2012 Technical Corrigendum 1 (TC1) および Technical Corrigendum 2 (TC2) で明確化された項目

  • MISRA C:2012 Amendment 1 (AMD1)、Amendment 2 (AMD2)、Amendment 3 (AMD3)、Amendment 4 (AMD4) で追加された新しいルールおよび変更

以下の表は、MISRA C:2012 Amendment 4 のルールを除く、MISRA C:2023 規格の "必要" と "必須" のすべてのコーディング命令およびルールをリストしたものです。これらの表の "カテゴリ" 列は、MISRA ガイドラインの付録 E の規定に従って自動生成コードに適用されます。

それぞれの命令またはルールについて、"準拠" 列は次のエントリのいずれかになります。

  • 準拠: 生成コードはこの命令/ルールに準拠します。

  • 非準拠: 状況によっては、生成コードがこの命令/ルールに準拠しないことがあります。

  • 該当なし: この命令/ルールはコード生成プロセスに該当しません。これらの命令/ルールの一部について、生成コードの準拠性を高めるために実行できる推奨されるアクションを "追加メモ" セクションに示してあります。

メモ

これらの表では、既に次の手順に従って生成コードの MISRA への準拠を最大限に高めていることを前提としています。

生成コードに、MISRA ガイドラインに準拠しないファイルが 1 つ以上含まれることがあります。それらのファイルの完全な一覧については、Scope of Code Generation Supportを参照してください。

特定の状況では、rtwtypes.h ファイルに含まれるカスタム型定義を生成コードで使用しないようにコード ジェネレーターを構成できる場合があります。例については、Additional Settings for MISRA C++ Complianceを参照してください。

これらの表の情報は、Simulink® モデルの MATLAB Function ブロックおよび MATLAB System ブロックの内部の MATLAB コードに対して生成される C コードには適用されません。Simulink モデルから生成されるコードの MISRA への準拠の詳細については、MISRA C:2023 準拠の概要表を参照してください。

コード ジェネレーターは、これらのルールを Polyspace® Bug Finder™ 製品と同じ方法で解釈します。特定のコーディング ルールがコード ジェネレーターでどのように解釈されるかについては、Polyspace Bug Finder ドキュメンテーション (MISRA C:2023 命令およびルール (Polyspace Bug Finder)) の対応するリファレンス ページで "Polyspace 実装" のセクションを参照してください。

実装

命令説明aカテゴリ準拠追加メモ
命令 1.1Any implementation-defined behavior on which the output of the program depends shall be documented and understood.必要非準拠 

a Copyright © The MISRA Consortium Limited, 2023.

コンパイルとビルド

命令説明aカテゴリ準拠追加メモ
命令 2.1All source files shall compile without any compilation errors.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

要件のトレーサビリティ

命令説明aカテゴリ準拠追加メモ
命令 3.1All code shall be traceable to documented requirements.必要該当なし

生成コードを準拠させるには、Requirements Toolbox™ を使用して、要件に関する情報を含むコメントを生成コードに含めます。Requirements Traceability for Code Generated from MATLAB Code (Requirements Toolbox)を参照してください。

a Copyright © The MISRA Consortium Limited, 2023.

コード設計

命令説明aカテゴリ準拠追加メモ
命令 4.1Run-time failures shall be minimized.必要非準拠 
命令 4.3Assembly language shall be encapsulated and isolated.必要準拠 
命令 4.7If a function returns error information, then that error information shall be tested.必要非準拠

この違反を減らすには、MATLAB コードで、coder.ceval 呼び出しの出力に対するテストを含めます。

命令 4.10Precautions shall be taken in order to prevent the contents of a header file being included more than once.必要準拠 
命令 4.11The validity of values passed to library functions shall be checked.必要非準拠

この違反を減らすには、MATLAB コードで、coder.ceval に渡す値の有効性をチェックします。

命令 4.12Dynamic memory allocation shall not be used.必要準拠 
命令 4.14The validity of values received from external sources shall be checked.必要非準拠

この違反を減らすには、MATLAB コードで、coder.ceval から返される値の有効性をチェックします。

命令 4.15Evaluation of floating-point expressions shall not lead to the undetected generation of infinities and NaNs.必要非準拠 

a Copyright © The MISRA Consortium Limited, 2023.

標準 C 環境

ルール説明aカテゴリ準拠追加メモ
1.1The program shall contain no violations of the standard C syntax and constraints, and shall not exceed the implementation's translation limits.必要準拠 
1.3There shall be no occurrence of undefined or critical unspecified behavior.必要準拠 
1.4Emergent language features shall not be used.必要準拠 
1.5Obsolescent language features shall not be used.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

未使用コード

ルール説明aカテゴリ準拠追加メモ
2.1A project shall not contain unreachable code.必要非準拠 
2.2There shall be no dead code.必要非準拠 

a Copyright © The MISRA Consortium Limited, 2023.

コメント

ルール説明aカテゴリ準拠追加メモ
3.1The character sequences /* and // shall not be used within a comment.必要非準拠

この違反を減らすには、MATLAB コードまたは外部 C/C+ コードに含めるコメントがこのルールを満たすことを確認します。

あるいは、構成プロパティ GenerateCommentsfalse に設定してコメント生成を無効にすることを検討してください。

3.2Line-splicing shall not be used in // comments.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

文字セットと字句規則

ルール説明aカテゴリ準拠追加メモ
4.1Octal and hexadecimal escape sequences shall be terminated.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

識別子

ルール説明aカテゴリb準拠追加メモ
5.1External identifiers shall be distinct.必要非準拠

この違反を減らすには、MATLAB エントリポイント関数の名前が 5 文字未満であることを確認します。

5.2Identifiers declared in the same scope and name space shall be distinct.必要準拠 
5.3An identifier declared in an inner scope shall not hide an identifier declared in an outer scope.勧告準拠 
5.4Macro identifiers shall be distinct.必要準拠 
5.5Identifiers shall be distinct from macro names.必要準拠 
5.6A typedef name shall be a unique identifier.必要準拠 
5.7A tag name shall be a unique identifier.必要準拠 
5.8Identifiers that define objects or functions with external linkage shall be unique.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

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

ルール説明aカテゴリ準拠追加メモ
6.1Bit-fields shall only be declared with an appropriate type.必要準拠 
6.2Single-bit named bit fields shall not be of a signed type.必要準拠 
6.3A bit field shall not be declared as a member of a union.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

リテラルと定数

ルール説明aカテゴリb準拠追加メモ
7.1Octal constants shall not be used.勧告準拠 
7.2A "u" or "U" suffix shall be applied to all integer constants that are represented in an unsigned type.可読性準拠 
7.3The lowercase character "l" shall not be used in a literal suffix.可読性準拠 
7.4A string literal shall not be assigned to an object unless the object's type is "pointer to const-qualified char".必要準拠 
7.5The argument of an integer constant macro shall have an appropriate form.必須準拠 

a Copyright © The MISRA Consortium Limited, 2023.

b Guideline identifies Rules 7.1, 7.2, and 7.3 as Required for manually generated code.

宣言と定義

ルール説明aカテゴリb準拠追加メモ
8.1Types shall be explicitly specified.必要準拠 
8.2Function types shall be in prototype form with named parameters.必要準拠 
8.3All declarations of an object or function shall use the same names and type qualifiers.必要準拠 
8.4A compatible declaration shall be visible when an object or function with external linkage is defined.勧告準拠 
8.5An external object or function shall be declared once in one and only one file.勧告準拠 
8.6An identifier with external linkage shall have exactly one external definition.必要準拠 
8.8The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage.必要準拠 
8.10An inline function shall be declared with the static storage class.必要準拠 
8.12Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique.必要準拠 
8.14The restrict type qualifier shall not be used.勧告準拠 
8.15All declarations of an object with an explicit alignment specification shall specify the same alignment.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

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

初期化

ルール説明aカテゴリb準拠追加メモ
9.1The value of an object with automatic storage duration shall not be read before it has been set.必須非準拠 
9.2The initializer for an aggregate or union shall be enclosed in braces.可読性準拠 
9.3Arrays shall not be partially initialized.可読性準拠 
9.4An element of an object shall not be initialized more than once.必要準拠 
9.5Where designated initializers are used to initialize an array object the size of the array shall be specified explicitly.可読性準拠 

a Copyright © The MISRA Consortium Limited, 2023.

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

実質的な型モデル

ルール説明aカテゴリb準拠追加メモ
10.1Operands shall not be of an inappropriate essential type.勧告準拠 
10.2Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations.勧告準拠 
10.3The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category.勧告準拠 
10.4Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category.勧告準拠 
10.6The value of a composite expression shall not be assigned to an object with wider essential type.勧告準拠 
10.7If a composite expression is used as one operand of an operator in which the usual arithmetic conversions are performed then the other operand shall not have wider essential type.勧告準拠 
10.8The value of a composite expression shall not be cast to a different essential type category or a wider essential type.勧告準拠 

a Copyright © The MISRA Consortium Limited, 2023.

b Guideline identifies Rule 10.1, 10.2, 10.3, 10.4, 10.6, 10.7, and 10.8 as Required for manually generated code.

ポインター型の変換

ルール説明aカテゴリb準拠追加メモ
11.1Conversions shall not be performed between a pointer to a function and any other type.必要準拠 
11.2Conversions shall not be performed between a pointer to an incomplete type and any other type.必要準拠 
11.3A cast shall not be performed between a pointer to object type and a pointer to a different object type.必要非準拠 
11.6A cast shall not be performed between pointer to void and an arithmetic type.必要準拠 
11.7A cast shall not be performed between pointer to object and a non-integer arithmetic type.必要準拠 
11.8A cast shall not remove any const or volatile qualification from the type pointed to by a pointer.必要非準拠 
11.9The macro NULL shall be the only permitted form of integer null pointer constant.可読性準拠 

a Copyright © The MISRA Consortium Limited, 2023.

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

ルール説明aカテゴリ準拠追加メモ
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.必要非準拠 
12.5The sizeof operator shall not have an operand which is a function parameter declared as "array of type".必須準拠 

a Copyright © The MISRA Consortium Limited, 2023.

副作用

ルール説明aカテゴリ準拠追加メモ
13.1Initializer lists shall not contain persistent side effects.必要準拠 
13.2The value of an expression and its persistent side effects shall be the same under all permitted evaluation orders.必要非準拠 
13.5The right hand operand of a logical && or || operator shall not contain persistent side effects.必要非準拠

特定の状況では、生成コードのスタック使用量を減らすために、ローカル変数がコード ジェネレーターによって静的ローカル変数にプロモートされます。このプロモーションにより、生成コードがこのルールに違反することがあります。

StackUsageMax コード構成パラメーターの値を大きくすると、生成コードがこのルールに準拠する可能性があります。

13.6The operand of the sizeof operator shall not contain any expression which has potential side effects.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

コントロール ステートメントの式

ルール説明aカテゴリb準拠追加メモ
14.1A loop counter shall not have essentially floating type.勧告準拠 
14.2A for loop shall be well-formed.可読性準拠 
14.3Controlling expressions shall not be invariant.必要非準拠 
14.4The controlling expression of an if statement and the controlling expression of an iteration-statement shall have essentially Boolean type.勧告準拠 

a Copyright © The MISRA Consortium Limited, 2023.

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

フロー制御

ルール説明aカテゴリb準拠追加メモ
15.2The goto statement shall jump to a label declared later in the same function.勧告準拠 
15.3Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statement.勧告準拠 
15.6The body of an iteration-statement or a selection-statement shall be a compound-statement.必要準拠 
15.7All if ... else if constructs shall be terminated with an else statement.可読性準拠 

a Copyright © The MISRA Consortium Limited, 2023.

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

switch ステートメント

ルール説明aカテゴリb準拠追加メモ
16.1All switch statements shall be well-formed.勧告準拠 
16.2A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement.勧告準拠 
16.3An unconditional break statement shall terminate every switch-clause.勧告準拠 
16.4Every switch statement shall have a default label.勧告準拠 
16.5A default label shall appear as either the first or the last switch label of a switch statement.勧告準拠 
16.6Every switch statement shall have at least two switch-clauses.勧告準拠 
16.7A switch-expression shall not have essentially Boolean type.勧告準拠 

a Copyright © The MISRA Consortium Limited, 2023.

b Guideline identifies Rule 16.1, 16.2, 16.3, 16.4, 16.5, 16.6, and 16.7 as Required for manually generated code.

関数

ルール説明aカテゴリb準拠追加メモ
17.1The features of <stdarg.h> shall not be used.必要準拠 
17.2Functions shall not call themselves, either directly or indirectly.必要準拠 
17.3A function shall not be declared implicitly.必須準拠 
17.4All exit paths from a function with non-void return type shall have an explicit return statement with an expression.必須準拠 
17.5The function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elements.必要準拠 
17.6The declaration of an array parameter shall not contain the static keyword between the [ ].必須準拠 
17.7The value returned by a function having non-void return type shall be used.可読性準拠 
17.9A function declared with a _Noreturn function specifier shall not return to its caller.必須準拠 
17.10A function declared with a _Noreturn function specifier shall have void return type.必要準拠 
17.13A function type shall not be type qualified.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

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

ポインターと配列

ルール説明aカテゴリ準拠追加メモ
18.1A pointer resulting from arithmetic on a pointer operand shall address an element of the same array as that pointer operand.必要非準拠 
18.2Subtraction between pointers shall only be applied to pointers that address elements of the same array.必要準拠 
18.3The relational operators >, >=, < and <= shall not be applied to objects of pointer type except where they point into the same object.必要準拠 
18.6The address of an object with automatic storage shall not be copied to another object that persists after the first object has ceased to exist.必要非準拠 
18.7Flexible array members shall not be declared.必要準拠 
18.8Variable-length array types shall not be used.必要準拠 
18.9An object with temporary lifetime shall not undergo array-to-pointer conversion.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

重複するストレージ

ルール説明aカテゴリ準拠追加メモ
19.1An object shall not be assigned or copied to an overlapping object.必須準拠 

a Copyright © The MISRA Consortium Limited, 2023.

プリプロセッサ命令

ルール説明aカテゴリ準拠追加メモ
20.2The ', " or \ characters and the /* or // character sequences shall not occur in a header file name.必要準拠 
20.3The #include directive shall be followed by either a <filename> or "filename"sequence.必要準拠 
20.4A macro shall not be defined with the same name as a keyword.必要準拠 
20.6Tokens that look like a preprocessing directive shall not occur within a macro argument.必要準拠 
20.7Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses.必要準拠 
20.8The controlling expression of a #if or #elif preprocessing directive shall evaluate to 0 or 1.必要準拠 
20.9All identifiers used in the controlling expression of #if or #elif preprocessing directives shall be #define'd before evaluation.必要準拠 
20.11A macro parameter immediately following a # operator shall not immediately be followed by a ## operator.必要準拠 
20.12A macro parameter used as an operand to the # or ## operators, which is itself subject to further macro replacement, shall only be used as an operand to these operators.必要準拠 
20.13A line whose first token is # shall be a valid preprocessing directive.必要準拠 
20.14All #else, #elif and #endif preprocessor directives shall reside in the same file as the #if, #ifdef or #ifndef directive to which they are related.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

標準ライブラリ

ルール説明aカテゴリ準拠追加メモ
21.1#define and #undef shall not be used on a reserved identifier or reserved macro name.必要準拠 
21.2A reserved identifier or reserved macro name shall not be declared.必要準拠 
21.3The memory allocation and deallocation functions of <stdlib.h> shall not be used.必要準拠 
21.4The standard header file <setjmp.h> shall not be used.必要準拠 
21.5The standard header file <signal.h> shall not be used.必要準拠 
21.6The Standard Library input/output functions shall not be used.必要非準拠

この違反を減らすには、MATLAB コードで、fopenfclosefprintffscanffreadfwritecoder.read などの関数を使用しないようにします。

21.7The Standard Library functions atof, atoi, atol and atoll of <stdlib.h> shall not be used.必要準拠 
21.8The Standard Library termination functions of <stdlib.h> shall not be used.必要準拠 
21.9The Standard Library functions bsearch and qsort of <stdlib.h> shall not be used.必要準拠 
21.10The Standard Library time and date functions shall not be used.必要非準拠

この違反を減らすには、MATLAB コードで、tictocpause などの関数を使用しないようにします。

21.12The standard header file <fenv.h> shall not be used.必要準拠 
21.13Any value passed to a function in <ctype.h> shall be representable as an unsigned char or be the value EOF.必須準拠 
21.14The Standard Library function memcmp shall not be used to compare null terminated strings.必要準拠 
21.15The pointer arguments to the Standard Library functions memcpy, memmove and memcmp shall be pointers to qualified or unqualified versions of compatible types.必要準拠 
21.16The pointer arguments to the Standard Library function memcmp shall point to either a pointer type, an essentially signed type, an essentially unsigned type, an essentially Boolean type or an essentially enum type.必要非準拠 
21.17Use of the string handling functions from <string.h> shall not result in accesses beyond the bounds of the objects referenced by their pointer parameters.必須準拠 
21.18The size_t argument passed to any function in <string.h> shall have an appropriate value.必須非準拠 
21.19The pointers returned by the Standard Library functions localeconv, getenv, setlocale or, strerror shall only be used as if they have pointer to const-qualified type.必須準拠 
21.20The pointer returned by the Standard Library functions asctime, ctime, gmtime, localtime, localeconv, getenv, setlocale or strerror shall not be used following a subsequent call to the same function.必須準拠 
21.21The Standard Library function system of <stdlib.h> shall not be used.必要準拠 
21.22All operand arguments to any type-generic macros declared in <tgmath.h> shall have an appropriate essential type.必須準拠 
21.23All operand arguments to any multi-argument type-generic macros declared in <tgmath.h> shall have the same standard type.必要準拠 
21.24The random number generator functions of <stdlib.h> shall not be used.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

リソース

ルール説明aカテゴリ準拠追加メモ
22.1All resources obtained dynamically by means of Standard Library functions shall be explicitly released.必要準拠 
22.2A block of memory shall only be freed if it was allocated by means of a Standard Library function.必須準拠 
22.3The same file shall not be open for read and write access at the same time on different streams.必要準拠 
22.4There shall be no attempt to write to a stream which has been opened as read-only.必須準拠 
22.5A pointer to a FILE object shall not be dereferenced.必須準拠 
22.6The value of a pointer to a FILE shall not be used after the associated stream has been closed.必須準拠 
22.7The macro EOF shall only be compared with the unmodified return value from any Standard Library function capable of returning EOF.必要準拠 
22.8The value of errno shall be set to zero prior to a call to an errno-setting-function.必要準拠 
22.9The value of errno shall be tested against zero after calling an errno-setting-function.必要準拠 
22.10The value of errno shall only be tested when the last function to be called was an errno-setting-function.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

総称選択

ルール説明aカテゴリ準拠追加メモ
23.2A generic selection that is not expanded from a macro shall not contain potential side effects in the controlling expression.必要準拠 
23.4A generic association shall list an appropriate type.必要準拠 
23.6The controlling expression of a generic selection shall have an essential type that matches its standard type.必要準拠 
23.8A default association shall appear as either the first or the last association of a generic selection.必要準拠 

a Copyright © The MISRA Consortium Limited, 2023.

参考

|

トピック

外部の Web サイト