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


やってきました。

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_05_07.c

まず、そのままで、コンパイルエラーになる版の結果。

$ clang 
R_05_03.c:23:12: warning: unused variable 'i' [-Wunused-variable]
   int16_t i;
           ^
R_05_03.c:36:16: warning: unused variable 'xyz' [-Wunused-variable]
static int16_t xyz = 0;                /* Declare an object "xyz" */
               ^
R_05_03.c:45:17: warning: unused variable 'speed' [-Wunused-variable]
static uint16_t speed;
                ^
3 warnings generated.
R_05_06.c:68:20: warning: suggest braces around initialization of subobject
      [-Wmissing-braces]
   chain cable = { 0 };
                   ^
                   {}
1 warning generated.
R_05_07.c:58:8: error: redefinition of 'elk'
struct elk      /* Non-compliant - Constraint violation in C99 */
       ^
R_05_07.c:53:8: note: previous definition is here
struct elk
       ^
R_05_07.c:67:4: error: use of 'stag' with tag type that does not match previous
      declaration
   union  stag a2 = { 0, 0 }; /* Non-compliant - Constraint Violation in C99  */
   ^~~~~
   struct
R_05_07.c:21:8: note: previous use is here
struct stag
       ^
2 errors generated.

$ gcc 
R_05_03.c: In function 'fn1':
R_05_03.c:23:12: warning: unused variable 'i' [-Wunused-variable]
   23 |    int16_t i;
      |            ^
At top level:
R_05_03.c:45:17: warning: 'speed' defined but not used [-Wunused-variable]
   45 | static uint16_t speed;
      |                 ^~~~~
R_05_03.c:36:16: warning: 'xyz' defined but not used [-Wunused-variable]
   36 | static int16_t xyz = 0;                /* Declare an object "xyz" */
      |                ^~~
R_05_07.c:58:8: error: redefinition of 'struct elk'
   58 | struct elk      /* Non-compliant - Constraint violation in C99 */
      |        ^~~
R_05_07.c:53:8: note: originally defined here
   53 | struct elk
      |        ^~~
R_05_07.c: In function 'R_5_7':
R_05_07.c:67:11: error: 'stag' defined as wrong kind of tag
   67 |    union  stag a2 = { 0, 0 }; /* Non-compliant - Constraint Violation in C99  */
      |           ^~~~
R_05_07.c:67:11: error: variable 'a2' has initializer but incomplete type
R_05_07.c:67:23: warning: excess elements in union initializer
   67 |    union  stag a2 = { 0, 0 }; /* Non-compliant - Constraint Violation in C99  */
      |                       ^
R_05_07.c:67:23: note: (near initialization for 'a2')
R_05_07.c:67:26: warning: excess elements in union initializer
   67 |    union  stag a2 = { 0, 0 }; /* Non-compliant - Constraint Violation in C99  */
      |                          ^
R_05_07.c:67:26: note: (near initialization for 'a2')
R_05_07.c:67:16: error: storage size of 'a2' isn't known
   67 |    union  stag a2 = { 0, 0 }; /* Non-compliant - Constraint Violation in C99  */
      |                ^~
R_05_07.c:67:16: warning: unused variable 'a2' [-Wunused-variable]

ひとまずコンパイルエラー行を除外。

/*
* Release: 2016-11-01
*
* Example from MISRA C:2012 ( THIS IS NOT A TEST SUITE )
*
* Copyright HORIBA MIRA Limited.
*
* See file READ_ME.txt for full copyright, license and release instructions.
*/

/*
* R.5.7
*
* A tag name shall be a unique identifier
*/
// https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite
// add #ifdef DEBUG for compile error Dr. OGAWA Kiyoshi(O.K.) 20200901

include "mc3_types.h"

include "mc3_header.h"

include "misra_c.h"

struct stag
{
int16_t a;
int16_t b;
};

struct deer
{
uint16_t a;
uint16_t b;
};

static void herd ( void )
{
struct deer
{
uint16_t a;
}; /* Non-compliant, also breaks R.5.3 */

struct deer bambi = { 1U };
use_uint16 ( bambi.a );
}

typedef struct coord
{
uint16_t x;
uint16_t y;
} coord; /* Compliant by Exception */

struct elk
{
uint16_t x;
};

ifdef DEBUG

struct elk /* Non-compliant - Constraint violation in C99 */
{
uint32_t x;
};

endif

void R_5_7 ( void )
{
struct stag a1 = { 0, 0 }; /* Compliant */

ifdef DEBUG

union stag a2 = { 0, 0 }; /* Non-compliant - Constraint Violation in C99 */

else

struct stag a2 = { 0, 0 }; /* Non-compliant - Constraint Violation in C99 */

endif

use_int16 ( a1.a + a1.b );
use_int16 ( a2.a + a2.b );
coord c1 = { 1U, 2U };
struct coord c2 = { 3U, 4U };
use_uint16 ( c1.x + c1.y + c2.x + c2.y );
struct deer rudolph = { 1U, 20U };
use_uint16 ( rudolph.a + rudolph.b );
struct elk ellie = { 0U };
use_uint32 ( ellie.x );

herd ( );
}

/* end of R_05_07.c */
```

次の行をclear_lib.cに追加。

clear_lib.c
// R_05_02.c

int32_t engin2_exhaust_gas_temperature_raw;

// R_05_03.c

void g ( struct astruct * p ){
    PR1((int)p,d);
}

警告の違いを確認。

$ clang 
R_05_03.c:23:12: warning: unused variable 'i' [-Wunused-variable]
   int16_t i;
           ^
R_05_03.c:36:16: warning: unused variable 'xyz' [-Wunused-variable]
static int16_t xyz = 0;                /* Declare an object "xyz" */
               ^
R_05_03.c:45:17: warning: unused variable 'speed' [-Wunused-variable]
static uint16_t speed;
                ^
3 warnings generated.
R_05_06.c:68:20: warning: suggest braces around initialization of subobject [-Wmissing-braces]
   chain cable = { 0 };
                   ^
                   {}
1 warning generated.
clear_lib.c:258:17: warning: declaration of 'struct astruct' will not be visible outside of this function [-Wvisibility]
void g ( struct astruct * p ){
                ^
1 warning generated.

$ gcc 
R_05_03.c: In function 'fn1':
R_05_03.c:23:12: warning: unused variable 'i' [-Wunused-variable]
   23 |    int16_t i;
      |            ^
At top level:
R_05_03.c:45:17: warning: 'speed' defined but not used [-Wunused-variable]
   45 | static uint16_t speed;
      |                 ^~~~~
R_05_03.c:36:16: warning: 'xyz' defined but not used [-Wunused-variable]
   36 | static int16_t xyz = 0;                /* Declare an object "xyz" */
      |                ^~~
clear_lib.c:258:17: warning: 'struct astruct' declared inside parameter list will not be visible outside of this definition or declaration
  258 | void g ( struct astruct * p ){
      |                 ^~~~~~~
In file included from clear_lib.c:10:
clear_lib.c: In function 'g':
clear_lib.c:259:6: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  259 |  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)
      |                                                      ^

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