gcc(gnu), clang(llvm)コンパイルエラー・警告比較(7) R_10_0x.c。docker(161)


やってきました。

gcc(gnu) clang(llvm)コンパイルエラー比較

長らくごぶさたしていました。

MISRA の解説書は有料ですが、Codeは無償であげてくれています。

MISRA C:2012 Example-Suite
https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite

全体の記録は

Misra Example Suite at docker コンパイル完了までの道のり
https://qiita.com/kaizen_nagoya/items/71f04a0204d5a1114577

順にコンパイルエラーが出たものを比較し、
エラーを取るのなら取ってみます。

-Wall でコンパイルしています。

R_10_0x.c

まず、そのまんまだとコンパイルエラーが出た状況。

$ clang 
R_10_01.c:52:16: error: invalid operands to binary expression ('float32_t' (aka 'float') and 'unsigned int')
   f32b = f32a & 2U;       /* Constraint Error, also breaks R.10.4 */
          ~~~~ ^ ~~
R_10_01.c:54:16: error: invalid operands to binary expression ('float32_t' (aka 'float') and 'int')
   f32b = f32a << 2;       /* Constraint Error */
          ~~~~ ^  ~
R_10_01.c:93:14: warning: shift count is negative [-Wshift-count-negative]
   u8b = u8a << -1;        /* Non-compliant; also breaks R.12.2 */
             ^  ~~
1 warning and 2 errors generated.
R_10_03.c:100:10: warning: implicit conversion from 'int' to 'int8_t' (aka 'signed char') changes value from 128 to -128 [-Wconstant-conversion]
   s8a = K2;                    /*  Non-compliant - Constant value does not fit */
       ~ ^~
R_10_03.c:119:28: warning: implicit conversion from 'int' to 'uint16_t' (aka 'unsigned short') changes value from 100000 to 34464 [-Wconstant-conversion]
   u16a = (uint16_t)50000U + (uint16_t)50000U; /*  Non-compliant */
        ~ ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
2 warnings generated.
R_10_04.c:61:13: warning: comparison of two values with different enumeration types ('enum enuma' and 'enum enumb') [-Wenum-compare]
   if ( ena == enb )            /* Non-compliant - Enum<enumb> to enum<enuma>      */
        ~~~ ^  ~~~
1 warning generated.

$ gcc 
R_10_01.c: In function 'R_10_1':
R_10_01.c:52:16: error: invalid operands to binary & (have 'float32_t' {aka 'float'} and 'unsigned int')
   52 |    f32b = f32a & 2U;       /* Constraint Error, also breaks R.10.4 */
      |                ^
R_10_01.c:54:16: error: invalid operands to binary << (have 'float32_t' {aka 'float'} and 'int')
   54 |    f32b = f32a << 2;       /* Constraint Error */
      |                ^~
R_10_01.c:72:14: warning: '*' in boolean context, suggest '&&' instead [-Wint-in-bool-context]
   72 |    blc = bla * blb;        /* Non-compliant, returns standard type */
      |          ~~~~^~~~~
R_10_01.c:93:14: warning: left shift count is negative [-Wshift-count-negative]
   93 |    u8b = u8a << -1;        /* Non-compliant; also breaks R.12.2 */
      |              ^~
R_10_03.c: In function 'R_10_3':
R_10_03.c:119:11: warning: unsigned conversion from 'int' to 'uint16_t' {aka 'short unsigned int'} changes value from '100000' to '34464' [-Woverflow]
  119 |    u16a = (uint16_t)50000U + (uint16_t)50000U; /*  Non-compliant */
      |           ^
R_10_04.c: In function 'R_10_4':
R_10_04.c:57:13: warning: comparison between 'enum enumb' and 'enum enuma' [-Wenum-compare]
   57 |    if ( enb > A1 )              /* Non-compliant - Enum<enuma> to enum<enumb>    */
      |             ^
R_10_04.c:61:13: warning: comparison between 'enum enuma' and 'enum enumb' [-Wenum-compare]
   61 |    if ( ena == enb )            /* Non-compliant - Enum<enumb> to enum<enuma>      */
      |             ^~
In file included from clear_lib.c:10:
clear_lib.c: In function 'g':
clear_lib.c:261:6: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  261 |  PR1((int)p,d);
      |      ^
misra_c.h:66:54: note: in definition of macro 'PR1'
   66 | #define PR1(a,b) (void)printf(" "#a  " = %" #b "\n", a)
      |                                                      ^
clear_lib.c: In function 'use_const_volatile_char_ptr':
clear_lib.c:274:6: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  274 |  PR1((int)cv_cp,d);
      |      ^
misra_c.h:66:54: note: in definition of macro 'PR1'
   66 | #define PR1(a,b) (void)printf(" "#a  " = %" #b "\n", a)
      |                                                      ^

./R_10.sh: 19: ./R_10.sh: R_10_01.c: not found

ifdef DEBUG

で除外しちゃったら。

$ clang 
R_10_01.c:95:14: warning: shift count is negative [-Wshift-count-negative]
   u8b = u8a << -1;        /* Non-compliant; also breaks R.12.2 */
             ^  ~~
R_10_01.c:140:18: warning: variable 'f32b' is uninitialized when used here [-Wuninitialized]
   f32b = f32a + f32b;
                 ^~~~
R_10_01.c:36:18: note: initialize the variable 'f32b' to silence this warning
   float32_t f32b;
                 ^
                  = 0.0
2 warnings generated.
R_10_03.c:100:10: warning: implicit conversion from 'int' to 'int8_t' (aka 'signed char') changes value from 128 to -128 [-Wconstant-conversion]
   s8a = K2;                    /*  Non-compliant - Constant value does not fit */
       ~ ^~
R_10_03.c:119:28: warning: implicit conversion from 'int' to 'uint16_t' (aka 'unsigned short') changes value from 100000 to 34464 [-Wconstant-conversion]
   u16a = (uint16_t)50000U + (uint16_t)50000U; /*  Non-compliant */
        ~ ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
2 warnings generated.
R_10_04.c:61:13: warning: comparison of two values with different enumeration types ('enum enuma' and 'enum enumb') [-Wenum-compare]
   if ( ena == enb )            /* Non-compliant - Enum<enumb> to enum<enuma>      */
        ~~~ ^  ~~~
1 warning generated.

$ gcc 
R_10_01.c: In function 'R_10_1':
R_10_01.c:74:14: warning: '*' in boolean context, suggest '&&' instead [-Wint-in-bool-context]
   74 |    blc = bla * blb;        /* Non-compliant, returns standard type */
      |          ~~~~^~~~~
R_10_01.c:95:14: warning: left shift count is negative [-Wshift-count-negative]
   95 |    u8b = u8a << -1;        /* Non-compliant; also breaks R.12.2 */
      |              ^~
R_10_01.c:140:9: warning: 'f32b' is used uninitialized in this function [-Wuninitialized]
  140 |    f32b = f32a + f32b;
      |    ~~~~~^~~~~~~~~~~~~
R_10_03.c: In function 'R_10_3':
R_10_03.c:119:11: warning: unsigned conversion from 'int' to 'uint16_t' {aka 'short unsigned int'} changes value from '100000' to '34464' [-Woverflow]
  119 |    u16a = (uint16_t)50000U + (uint16_t)50000U; /*  Non-compliant */
      |           ^
R_10_04.c: In function 'R_10_4':
R_10_04.c:57:13: warning: comparison between 'enum enumb' and 'enum enuma' [-Wenum-compare]
   57 |    if ( enb > A1 )              /* Non-compliant - Enum<enuma> to enum<enumb>    */
      |             ^
R_10_04.c:61:13: warning: comparison between 'enum enuma' and 'enum enumb' [-Wenum-compare]
   61 |    if ( ena == enb )            /* Non-compliant - Enum<enumb> to enum<enuma>      */
      |             ^~
In file included from clear_lib.c:10:
clear_lib.c: In function 'g':
clear_lib.c:263:6: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  263 |  PR1((int)p,d);
      |      ^
misra_c.h:66:54: note: in definition of macro 'PR1'
   66 | #define PR1(a,b) (void)printf(" "#a  " = %" #b "\n", a)
      |                                                      ^
clear_lib.c: In function 'use_const_volatile_char_ptr':
clear_lib.c:276:6: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  276 |  PR1((int)cv_cp,d);
      |      ^
misra_c.h:66:54: note: in definition of macro 'PR1'
   66 | #define PR1(a,b) (void)printf(" "#a  " = %" #b "\n", a)
      |                                                  

docker hubでの作業

docker hubからの起動はこちら。

$ docker run it kaizenjapan/misra_c_2012_example /bin/bash

参考資料

仮説・検証(173)C言語(C++)に対する誤解、曲解、無理解、爽快。

[C][C++]の国際規格案の例題をコンパイルするときの課題7つ。
https://qiita.com/kaizen_nagoya/items/5f4b155030259497c4de
C Puzzle Bookの有り難み5つ、C言語規格及びCコンパイラの特性を認識
https://qiita.com/kaizen_nagoya/items/d89a48c1536a02ecdec9
MISRA C まとめ #include
https://qiita.com/kaizen_nagoya/items/f1a79a7cbd281607c7c9
どうやって MISRA Example Suiteをコンパイルするか
https://qiita.com/kaizen_nagoya/items/fbdbff5ff696e2ca7f00

ISO/IEC TS 17961:2013 C Secure Coding Rules(1) All list(to be confirmed)
https://qiita.com/kaizen_nagoya/items/54e056195c4f11b850a1

Autosar Guidelines C++14 example code compile list
https://qiita.com/kaizen_nagoya/items/8ccbf6675c3494d57a76

C++ N4741, N4606, N3242 Source Compile Error List
https://qiita.com/kaizen_nagoya/items/2e736e04339b340ffb4d