LoginSignup
1
0

misra C 2012 permits(許可)を読む

Last updated at Posted at 2021-04-23

ようこそ Misra Cの世界に。

前史

C言語は、魅惑の言語でした。

CPUが16bitから32bitに移行することと、
Unixが16bitから32bitに移行することの、
両方に立ち会って来た言語です。

Portable C compiler

V7/usr/src/cmd/pcc
https://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/pcc

Portable C Compiler.
http://pcc.ludd.ltu.se/

MISRA Cは、空間規定はあるが、時間規定はほとんどない。これは、STARC RTL設計スタイルガイドど比較すると明白である。
時間に厳しいプログラムを書くには、規則を逸脱するしかない。そこで、よいソフトウェアほど、規則の逸脱の割合が一定以上あることは経験則です。

MISRA C 2012で、自動生成の規則を統合した。その際に、各種自動生成処理の開発元との意見調整を十分にしなかったのかもしれない。

permitsは、規則に対する許容範囲を示したもので、逸脱をよく行なっているものを収集している。
//コメントも1999年の時点で、この misrac permitsを発行すべきだったというのが反省点である。

例えば、日本が発行したRTL 設計スタイルガイドでは、/**/ではなく//を使用することを推奨した。同じ会社の中で、Verilog HDLとCとで逆のコードを書かされていた技術者には本当に申し訳ないと思っている。

欧州では、//記述を組織標準(standard deviation)にしていたらしい。

misra C 2012:permits
https://www.misra.org.uk/forum/viewtopic.php?f=241&t=1879

MISRA Compliance:2020を合わせて読まないと理解できないかもしれません。

MISRA Compliance:2020
https://www.misra.org.uk/forum/viewtopic.php?f=241&t=1842

規則文は省略して、許容文だけ示す。

Rule 2.1 permit

A section of code is unreachable in a particular build configuration.
仮訳:特定の構成で、コードの一部に到達できない。

別の構成で到達できる場合に、コードの再利用の観点ら選択することがある。(A.1)
普段は到達できない場所でも、そこに飛んだ時に安全な対応が可能なコードを書くことがある。(B.1)

後者は特に、強調してきた。データとして埋め込んでもいいが、コードとして埋め込んだ方が、可読性が高い。

Rule 5.4 permit

In automatically generated code, an identifier declared in an inner scope hides an identifier declared in an outer scope.
仮訳:自動生成コードで、内部範囲宣言識別子が、外部範囲宣言識別子を隠す。

自動生成コードは、可読性よりも、自動生成論理の簡単化が不具合を少なくする。自動生成コードに、複雑な論理を導入するのは好ましくない。

Rule 6.1 permit

In c90 code, bit-fields are defined with an integer type other than unsigned int or signed int.
仮訳:1990C標準で、ビットフィールドは unsigned int, signed intではなく、一つのint型で定義する。

どの型で宣言すると時間、空間が節約できるかは処理系による。
いちいち逸脱の手続きを取ってきた手間をなるべく削減するように配慮した。

Rule 7.1 permit

In automatically generated code, octal constant are used.
仮訳:自動生成コードでは、8進定数を使う。

通信系では、8進数を使って定数を表現することがある。
ソフトウェアでも、通信仕様にある8進数を使った方が誤りが少なくできたり、可読性を高めることができるかもしれない。
ここでは、自動生成だけに限定しているが、もう少し幅広にしてもいいかも。

Rule 7.2 permit

In automatically generated code, integer constants that are represented in an unsigned type have no "u" or "U" suffix.
自動生成コードは、符号なし整数定数に「u」または「U」の接尾辞をつけない。

自動生成を複雑にしないための方法の一つ。
MISRA C 2012で、自動生成の規則を統合した際に、細かい配慮をしていなかったのかもしれない。

Rule 7.3 permit

In automatically generated code, the lowercase character "l" is used in a literal suffix .
仮訳:自動生成コードでは、小文字の"l"を接尾辞として使う。

手で書く場合は、小文字のl(エル)は数字の1(いち)、大文字のI(アイ)と見間違える可能性があり、好ましくない。
手で修正する可能性がないコードでは、特に問題にしない。

Rule 8.4 permit

In automatically generated code, there is no compatible declaration visible when an object or function with external linkage is defined.
仮訳:自動生成コードでは、外部リンクを持関数またはオブジェクトを定義している場合、互換性のある宣言が見えなくなる。

自動生成コードでは、コンパイラ、リンカがうまく処理できることが一義的で、可読性は二義的である。

Rule 8.5 permit

In automatically generated code, external objects or functions are declared more than once in one or ore files
仮訳:自動生成コードは、外部関数またはオブジェクトが1回を超えまたは複数ファイルで宣言する。

自動生成コードの場合、どのファイルと一緒にコンパイルするかは、リンカスクリプトしか知らない。自動生成時に、リンカスクリプトが決定しているとは限らず、矛盾しない宣言が何度あっても構わない方が、自動生成の論理を単純にでき、間違いを減らす可能性がある。

Rule 8.14 permit

In automatically generated code, the restrict type qualifier is used.
仮訳:自動生成コードでは、restrict(制限)修飾子を使用する。

restrictは、コンパイラに最適化を指示するコードである。
register指定のような人手で指定するよりもコンパイラの最適化に任せる方が効率的な修飾子は使用しなくなった。
現在のコンパイラでは、restrict指定がないと、最適化してもよいかどうか分からない論理があり、指定を積極的にしている自動生成を認めることになった。

Rule 9.2 permit

In automatically generated code, initializers for an aggregate of union are not enclosed in braces
仮訳:自動生成コードは、集合体の初期化子または共用体を中括弧で囲まない。

自動生成コードの論理を複雑にしないための緩和。

Rule 9.3 permit

In automatically generated code, an array is partially initialized.
仮訳:自動生成コードで、配列を部分的に初期化する。

Rule 9.5 permit

In automatically generated code, designated initializers are used to initialize an array object but the size of the array is not specified explicitly.
仮訳:自動生成コードで、初期化子を使用して配列を初期化します。配列の大きさは明示的に指定しません。

Rule 10.1 permit

In automatically generated code, operands are of an inappropriate essential type.
仮訳:自動生成コードで、オペランドが不適切な基本型である。

Rule 10.3 permit

In automatically generated code, the value of an expression is assigned to an object with a narrower essential type or of a different essential type category.
仮訳:自動生成コードで、式の値をより狭い要素型または異なる分類要素型のオブジェクトに割り当てる。

Rule 10.4 permit

In automatically generated code, operands of an operator in which the usual arithmetic conversions are performed have different essential type category.
仮訳:自動生成コードで、通常の算術変換を実行する演算子のオペランドは、異なる要素型分類である。

Rule 10.6 permit

In automatically generate code, the value of a composite expression is assigned to an object with wider essential type.
仮訳:自動生成コードで、複合式の値をより広い要素型に割り当てる。

Rule 10.7 permit

In automatically generated code, a composite expression is used as one operand of an operator in which the usual arithmetic conversions are performed and the other operand has wider essential type.
仮訳:自動生成コードで、複合式は通常の算術変換が実行する演算子の1つのオペランドとして使用し、もう1つのオペランドはより広い基本型を持つ。

Rule 10.8 permit

In automatically generated code, the value of a composite expression is cast to a different essential type category or a wider essential type.
仮訳:自動生成コードで、複合式の値は、別の要素型の分類またはより広い要素型にキャストします。

Rule 11.9 permit

In automatically generated code, an integer null pointer constant other than the macro NULL or (void *)0 is used.
仮訳:自動生成コードで、マクロNULLまたは(void *)0 以外の整数NULLポインタ定数を使う。

Rule 14.2 permit

In automatically generated code, a for-loop is not well-formed.
仮訳:自動生成コードでは、forループはよい形式ではない。

Rule 14.3 許可

The result of a logical operation is invariant in a paticular build configuration.
An invariant logical operation is present as a result of defensive coding measures.
An invariant logical operation is introduced at system level as a result of interfacing to adopted code.
仮訳:論理演算の結果は、特定の構成で不変である。
防御的なコーディング手段として、不変論理演算がある。
採用コードとのインタフェースとして、不変論理演算をシステムレベルで導入する。

Rule 15.7 許可

In automatically generated code, as if ... else if construct is not terminated with an else statement.
仮訳:自動生成コードで、if ... elseif構文がelse文で終了していない。

Rule 16.1 許可

In automatically generated code, a switch statement is not well formed.
仮訳:自動生成コードで、switch文が良い構成ではない。

Rule 16.3 許可

In automatically generated code, a switch-clause is not terminated by an unconditional break statement.
仮訳:自動生成コードで、switch句が無条件のbreak文で終了しない。

Rule 16.4 許可

In automatically generated code, a switch statement has no default label.
仮訳:自動生成コードは、switch文にdefault札がない。

RUle 16.5 許可

In automatically generated code, the default label is neither the first nor the last switch label of a switch statement.
仮訳:自動生成コードで、default札は、switch文の最初でも最後でもない。

可読性を問題にするより、現在の自動生成の論理を変更することの方が信頼性を損なう可能性があるから許可する。最初から設計する時には、論理的にも最初か最後の方が、よい自動生成の可能性がある。

Rule 16.6 許可

In automatically generated code, a switch statement has only one switch-clause.
仮訳:自動生成コードで、switch文ににswitch句が1つだけ。

過去の経緯で、#ifdefではなく、switch句を削除した場合は、標準逸脱(standing deviation)の手続きを取っている。許可(permit)より、標準逸脱(standing deviation)の方がいいかも。mandateもそうだけど、お節介すぎる気がしないだろうか。

Rule 16.7 許可

In automatically generated code, a switch-expression has essentially Boolean type.
仮訳:自動生成コードで、switch表現は本来ブール型です。

Rule 18.1 許可

Arithmetic operations are performed on pointers that are used to address a region in memory which is not an array object.
仮訳:算術演算を、配列ではないメモリ内の領域を番地指定ポインタに実行する。

自動生成論理を簡便にするため、配列でない番地指定するポインタによる算術演算する。

#編集履歴(document history)
ver. 0.01 初稿 20210423
ver. 0.02 項目数 5 20210424
ver. 0.03 項目数 10 20210425 夕
ver. 0.04 項目数 15 20210425 夜
ver. 0.04 項目数 20 20210425 真夜中
ver. 0.05 仮訳完了 説明一部。20210502

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0