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


やってきました。
gcc(gnu) clang(llvm)コンパイルエラー比較

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

ISO/IEC JTC1 SC7から ISO/IEC JTC1 SC22へのリエゾン担当だった小川清です。

リエゾン担当になった理由は、一つには、MISRA からの意見が通らないという声を聞いて、品質の観点で何かお手伝いすることがないか調べるためでした。

IEEEのJim Mooreと二人での担当です。
数年で成果がでないまま退任したのは残念です。

MISRAはその後、リエゾンになり、今では毎年報告をあげてくれています。

MISRA の解説書は有料ですが、Codeは無償であげてくれています。
MISRA C:2012 Example-Suite
https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite

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

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

全体の作業は

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

まず、エラー・警告の違いがあるものをまず記録。
D_04_03.c

clang
D_04_03.c:24:8: error: expected '(' after 'asm'
   asm { "CLI" };                      /* Non-compliant  */
       ^
D_04_03.c:26:8: error: expected '(' after 'asm'
   asm { "SEI" };                      /* Non-compliant  */
       ^
2 errors generated.
gcc
D_04_03.c: In function 'D_4_3_1':
D_04_03.c:24:8: error: expected '(' before '{' token
   24 |    asm { "CLI" };                      /* Non-compliant  */
      |        ^
      |        (
D_04_03.c:24:10: warning: statement with no effect [-Wunused-value]
   24 |    asm { "CLI" };                      /* Non-compliant  */
      |          ^~~~~
D_04_03.c:24:15: error: expected ';' before '}' token
   24 |    asm { "CLI" };                      /* Non-compliant  */
      |               ^~
      |               ;
D_04_03.c:26:8: error: expected '(' before '{' token
   26 |    asm { "SEI" };                      /* Non-compliant  */
      |        ^
      |        (
D_04_03.c:26:10: warning: statement with no effect [-Wunused-value]
   26 |    asm { "SEI" };                      /* Non-compliant  */
      |          ^~~~~
D_04_03.c:26:15: error: expected ';' before '}' token
   26 |    asm { "SEI" };                      /* Non-compliant  */
      |               ^~
      |               ;

Intel CPUで作業している。
CLI(clear interrupt flag)はそのまま
SEIはSTI(set interrupt flag)に変更。

https://www.intel.co.jp/content/dam/www/public/ijkk/jp/ja/documents/developer/IA32_Arh_Dev_Man_Vol2B_i.pdf
p.4-238

直したソースはこちら。The C Puzzle Bookのdefs.hから引用したマクロを含む misra_c.hもinclude

MISRA C まとめ #include
https://qiita.com/kaizen_nagoya/items/f1a79a7cbd281607c7c9

なお、別作業でVisual Cでもコンパイルしている。そちらの修正も合わせて記載。

D_04_03.c
/*
 * Release: 2016-01-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.
 */

/*
 * D.4.3
 *
 * Assembly language shall be encapsulated and isolated
*/

// change around assembler for gcc, clang and visual c by Dr. OGAWA Kiyoshi (O.K.) 20200901

#include "mc3_types.h"
#include "mc3_header.h"
#include "misra_c.h"

#ifdef MSC_VER // O.K. 
#define NOP _asm(NOP)               /* Compliant, but breaks R.1.2 */
#else // O.K.
#define NOP asm(" NOP")               /* Compliant, but breaks R.1.2 */
#endif // O.K.

static void D_4_3_1 ( void )
{
#ifdef DEBUG    // O.K.
    asm { "CLI" };                      /* Non-compliant  */
   use_int16 ( 1 );
   asm { "SEI" };                      /* Non-compliant */
#elif MSC_VER
    _asm { "CLI" };                      /* Non-compliant  */
   use_int16 ( 1 );
   _asm { "STI" };                      /* Non-compliant  */   
#else // O.K.
    asm ("CLI");                      /* Non-compliant CLI:clear interrupt*/
   use_int16 ( 1 );
    asm ("STI");                      /* Non-compliant  STI:Set interrupt */


#endif  // O.K. 
   }

static void D_4_3_2 ( void )
{
   NOP;
}

void D_4_3 ( void )
{
   D_4_3_1 ( );
   D_4_3_2 ( );
}

/* end of D_04_03.c */

コンパイル結果は

clang
/usr/bin/ld: /tmp/D_04_01-575bf2.o: in function `D_4_1':
D_04_01.c:(.text+0x99): undefined reference to `use_float32'
/usr/bin/ld: D_04_01.c:(.text+0xa3): undefined reference to `use_float32'
/usr/bin/ld: /tmp/D_04_01-575bf2.o: in function `msg_body':
D_04_01.c:(.text+0x124): undefined reference to `msg_header_valid'
/usr/bin/ld: /tmp/D_04_03-b69b44.o: in function `D_4_3_1':
D_04_03.c:(.text+0x1b): undefined reference to `use_int16'
/usr/bin/ld: /tmp/D_04_04-0a8aaf.o: in function `D_4_4':
D_04_04.c:(.text+0x13): undefined reference to `use_int32'
/usr/bin/ld: /tmp/D_04_05-386199.o: in function `D_4_5':
D_04_05.c:(.text+0x98): undefined reference to `use_int32'
/usr/bin/ld: D_04_05.c:(.text+0xb1): undefined reference to `use_int32'
/usr/bin/ld: D_04_05.c:(.text+0xca): undefined reference to `use_int32'
/usr/bin/ld: D_04_05.c:(.text+0xd7): undefined reference to `use_int32'
/usr/bin/ld: /tmp/D_04_06-5f0ba0.o: in function `D_4_6':
D_04_06.c:(.text+0x2c): undefined reference to `use_char'
/usr/bin/ld: D_04_06.c:(.text+0x43): undefined reference to `use_int32'
/usr/bin/ld: /tmp/D_04_07-da13ac.o: in function `D_4_7':
D_04_07.c:(.text+0x9): undefined reference to `get_uint16'
/usr/bin/ld: D_04_07.c:(.text+0x12): undefined reference to `get_uint16'
/usr/bin/ld: D_04_07.c:(.text+0x62): undefined reference to `use_uint16'
/usr/bin/ld: /tmp/D_04_08_1-662f16.o: in function `D_4_8':
D_04_08_1.c:(.text+0x2d): undefined reference to `get_ptrX'
/usr/bin/ld: D_04_08_1.c:(.text+0x36): undefined reference to `get_ptrX'
/usr/bin/ld: /tmp/D_04_08_1-662f16.o: in function `D_4_8_1':
D_04_08_1.c:(.text+0x75): undefined reference to `use_ptrX'
/usr/bin/ld: D_04_08_1.c:(.text+0x8a): undefined reference to `use_int32'
/usr/bin/ld: /tmp/D_04_08_2-410cba.o: in function `UseObject':
D_04_08_2.c:(.text+0x3f): undefined reference to `use_int32'
/usr/bin/ld: D_04_08_2.c:(.text+0x54): undefined reference to `use_int32'
/usr/bin/ld: /tmp/D_04_08_2-410cba.o: in function `UseObject_notop':
D_04_08_2.c:(.text+0x9f): undefined reference to `use_int32'
/usr/bin/ld: D_04_08_2.c:(.text+0xb4): undefined reference to `use_int32'
/usr/bin/ld: /tmp/D_04_09-be21fc.o: in function `D_4_9':
D_04_09.c:(.text+0x13): undefined reference to `use_uint32'
/usr/bin/ld: /tmp/D_04_09-be21fc.o: in function `fn':
D_04_09.c:(.text+0x53): undefined reference to `use_uint16'
/usr/bin/ld: /tmp/D_04_10-a5bb05.o: in function `D_4_10':
D_04_10.c:(.text+0xa): undefined reference to `use_int32'
/usr/bin/ld: /tmp/D_04_11-bdfc0a.o: in function `D_4_11':
D_04_11.c:(.text+0x9): undefined reference to `get_int32'
/usr/bin/ld: D_04_11.c:(.text+0x38): undefined reference to `use_int32'
/usr/bin/ld: D_04_11.c:(.text+0x3d): undefined reference to `get_int32'
/usr/bin/ld: D_04_11.c:(.text+0x53): undefined reference to `use_int32'
/usr/bin/ld: D_04_11.c:(.text+0x5b): undefined reference to `use_int32'
/usr/bin/ld: /tmp/D_04_12-860a61.o: in function `f':
D_04_12.c:(.text+0x3e): undefined reference to `use_char_ptr'
/usr/bin/ld: /tmp/D_04_13-0a206e.o: in function `D_4_13':
D_04_13.c:(.text+0x5): undefined reference to `get_int16'
/usr/bin/ld: /tmp/D_04_13-0a206e.o: in function `fpair':
D_04_13.c:(.text+0x29): undefined reference to `mutex_lock'
/usr/bin/ld: D_04_13.c:(.text+0x45): undefined reference to `mutex_unlock'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
gcc
/usr/bin/ld: /tmp/ccDYld3h.o: in function `msg_body':
D_04_01.c:(.text+0x23): undefined reference to `msg_header_valid'
/usr/bin/ld: /tmp/ccDYld3h.o: in function `D_4_1':
D_04_01.c:(.text+0xd9): undefined reference to `use_float32'
/usr/bin/ld: D_04_01.c:(.text+0xe5): undefined reference to `use_float32'
/usr/bin/ld: /tmp/ccvW53hV.o: in function `D_4_3_1':
D_04_03.c:(.text+0xb): undefined reference to `use_int16'
/usr/bin/ld: /tmp/ccvRlc3J.o: in function `D_4_4':
D_04_04.c:(.text+0x15): undefined reference to `use_int32'
/usr/bin/ld: /tmp/ccnfWPUy.o: in function `D_4_5':
D_04_05.c:(.text+0x9b): undefined reference to `use_int32'
/usr/bin/ld: D_04_05.c:(.text+0xbe): undefined reference to `use_int32'
/usr/bin/ld: D_04_05.c:(.text+0xe1): undefined reference to `use_int32'
/usr/bin/ld: D_04_05.c:(.text+0xf0): undefined reference to `use_int32'
/usr/bin/ld: /tmp/ccKM2qSn.o: in function `D_4_6':
D_04_06.c:(.text+0x2e): undefined reference to `use_char'
/usr/bin/ld: D_04_06.c:(.text+0x47): undefined reference to `use_int32'
/usr/bin/ld: /tmp/cc5fahWc.o: in function `D_4_7':
D_04_07.c:(.text+0x4d): undefined reference to `get_uint16'
/usr/bin/ld: D_04_07.c:(.text+0x56): undefined reference to `get_uint16'
/usr/bin/ld: D_04_07.c:(.text+0xb3): undefined reference to `use_uint16'
/usr/bin/ld: /tmp/ccJ9GPY2.o: in function `D_4_8_1':
D_04_08_1.c:(.text+0x18): undefined reference to `use_ptrX'
/usr/bin/ld: D_04_08_1.c:(.text+0x2f): undefined reference to `use_int32'
/usr/bin/ld: /tmp/ccJ9GPY2.o: in function `D_4_8':
D_04_08_1.c:(.text+0x69): undefined reference to `get_ptrX'
/usr/bin/ld: D_04_08_1.c:(.text+0x72): undefined reference to `get_ptrX'
/usr/bin/ld: /tmp/cceVzD8S.o: in function `UseObject':
D_04_08_2.c:(.text+0x3e): undefined reference to `use_int32'
/usr/bin/ld: D_04_08_2.c:(.text+0x53): undefined reference to `use_int32'
/usr/bin/ld: /tmp/cceVzD8S.o: in function `UseObject_notop':
D_04_08_2.c:(.text+0x98): undefined reference to `use_int32'
/usr/bin/ld: D_04_08_2.c:(.text+0xad): undefined reference to `use_int32'
/usr/bin/ld: /tmp/ccEJbXoJ.o: in function `fn':
D_04_09.c:(.text+0x21): undefined reference to `use_uint16'
/usr/bin/ld: /tmp/ccEJbXoJ.o: in function `D_4_9':
D_04_09.c:(.text+0x3d): undefined reference to `use_uint32'
/usr/bin/ld: /tmp/ccT95GMz.o: in function `D_4_10':
D_04_10.c:(.text+0xa): undefined reference to `use_int32'
/usr/bin/ld: /tmp/cchgDOhs.o: in function `D_4_11':
D_04_11.c:(.text+0x9): undefined reference to `get_int32'
/usr/bin/ld: D_04_11.c:(.text+0x36): undefined reference to `use_int32'
/usr/bin/ld: D_04_11.c:(.text+0x3b): undefined reference to `get_int32'
/usr/bin/ld: D_04_11.c:(.text+0x56): undefined reference to `use_int32'
/usr/bin/ld: D_04_11.c:(.text+0x60): undefined reference to `use_int32'
/usr/bin/ld: /tmp/cctJQPTk.o: in function `f':
D_04_12.c:(.text+0x32): undefined reference to `use_char_ptr'
/usr/bin/ld: /tmp/ccyQkZCd.o: in function `fpair':
D_04_13.c:(.text+0x9): undefined reference to `mutex_lock'
/usr/bin/ld: D_04_13.c:(.text+0x22): undefined reference to `mutex_unlock'
/usr/bin/ld: /tmp/ccyQkZCd.o: in function `D_4_13':
D_04_13.c:(.text+0x2e): undefined reference to `get_int16'
collect2: error: ld returned 1 exit status



作ったソースはこちら。

clear_lib.c
// Addition to Example from MISRA C:2012 ( THIS IS NOT A TEST SUITE )
// (c) Dr. Kiyoshi Ogawa 20200901

/* Copyright HORIBA MIRA Limited.
 * See file READ_ME.txt for full copyright, license and release instructions.
*/

#include "mc3_types.h"
#include "mc3_header.h"
#include "misra_c.h"

#include "D_04_01.h"
#include "D_04_08.h"
#include "D_04_13.h"

bool b1 = true;

bool get_bool ( void ){
    return b1;
}
char_t get_char ( void ){
    return (char_t) b1;
}
int8_t get_int8 ( void ){
    return (int8_t) b1;
}
int16_t get_int16 ( void ){
    return (int16_t) b1;
}
int32_t get_int32 ( void ){
    return (int32_t) b1;
}
int64_t get_int64 ( void ){
    return (int64_t) b1;
}
uint8_t get_uint8 ( void ){
    return (uint8_t) b1;
}
uint16_t get_uint16 ( void ){
    return (uint16_t) b1;
}
uint32_t get_uint32 ( void ){
    return (uint32_t) b1;
}
uint64_t get_uint64 ( void ){
    return (uint64_t) b1;
}
float32_t get_float32 ( void ){
    return (float32_t) b1;
}
float64_t get_float64 ( void ){
    return (float64_t) b1;
}
float128_t get_float128 ( void ){
    return (float128_t) b1;
}

bool *get_bool_ptr ( void ){
    return & b1 ;
}
char_t *get_char_ptr ( void ){
    return (char_t *) & b1;
}
int8_t *get_int8_ptr ( void ){
    return (int8_t *) & b1;
}
int16_t *get_int16_ptr ( void ){
    return (int16_t * ) & b1;
}
int32_t *get_int32_ptr ( void ){
    return (int32_t *) & b1;
}
int64_t *get_int64_ptr ( void ){
    return (int64_t *) & b1;
}
uint8_t *get_uint8_ptr ( void ){
    return (uint8_t *) & b1;
}
uint16_t *get_uint16_ptr ( void ){
    return (uint16_t * ) & b1;
}
uint32_t *get_uint32_ptr ( void ){
    return (uint32_t *) & b1;
}
uint64_t *get_uint64_ptr ( void ){
    return (uint64_t * ) & b1;
}
float32_t *get_float32_ptr ( void ){
    return (float32_t *) & b1;
}
float64_t *get_float64_ptr ( void ){
    return (float64_t * ) & b1;
}
float128_t *get_float128_ptr ( void ){
    return (float128_t *) & b1;
}

enum enum_t{ One, Two, Three};

enum_t get_enum ( void ){
    return (enum_t) One;
}

/* Functions that use a variable */

void use_bool ( bool use_bool_param ){
    use_bool_param++;
}
void use_char ( char_t use_char_param ){
    use_char_param++;
}
void use_int8 ( int8_t use_int8_param ){
    use_int8_param++;
}
void use_int16 ( int16_t use_int16_param ){
    use_int16_param++;
}
void use_int32 ( int32_t use_int32_param ){
    use_int32_param++;
}
void use_int64 ( int64_t use_int64_param ){
    use_int64_param++;
}
void use_int128( int128_t use_int128_param ){
    use_int128_param++;
}
void use_uint8 ( uint8_t use_uint8_param ){
    use_uint8_param++;
}
void use_uint16 ( uint16_t use_uint16_param ){
    use_uint16_param++;
}
void use_uint32 ( uint32_t use_uint32_param ){
    use_uint32_param++;
}
void use_uint64 ( uint64_t use_uint64_param ){
    use_uint64_param++;
}
void use_uint128 ( uint128_t use_uint128_param ){
    use_uint128_param++;
}
void use_float32 ( float32_t use_float32_param ){
    use_float32_param++;
}
void use_float64 ( float64_t use_float64_param ){
    use_float64_param++;
}
void use_float128 ( float128_t use_float128_param ){
    use_float128_param++;
}

void use_void_ptr ( void * void_ptr_param ){
    void_ptr_param++;
}
void use_bool_ptr ( bool *use_bool_ptr_param ){
    use_bool_ptr_param++;
}
void use_char_ptr ( char_t *use_char_ptr_param ){
    use_char_ptr_param++;
}
void use_int8_ptr ( int8_t *use_int8_ptr_param ){
    use_int8_ptr_param++;
}
void use_int16_ptr ( int16_t *use_int16_ptr_param ){
    use_int16_ptr_param++;
}
void use_int32_ptr ( int32_t *use_int32_ptr_param ){
    use_int32_ptr_param++;
}
void use_int64_ptr ( int64_t *use_int64_ptr_param ){
    use_int64_ptr_param++;
}
void use_uint8_ptr ( uint8_t *use_uint8_ptr_param ){
    use_uint8_ptr_param++;
}
void use_uint16_ptr ( uint16_t *use_uint16_ptr_param ){
    use_uint16_ptr_param++;
}
void use_uint32_ptr ( uint32_t *use_uint32_ptr_param ){
    use_uint32_ptr_param++;
}
void use_uint64_ptr ( uint64_t *use_uint64_ptr_param ){
    use_uint64_ptr_param++;
}
void use_float32_ptr ( float32_t *use_float32_ptr_param ){
    use_float32_ptr_param++;
}
void use_float64_ptr ( float64_t *use_float64_ptr_param ){
    use_float64_ptr_param++;
}
void use_float128_ptr ( float128_t *use_float128_ptr_param ){
    use_float128_ptr_param++;
}


void use_const_char_ptr ( const char_t *use_c_char_ptr_param ){
    use_c_char_ptr_param++;
}


void use_enum ( enum_t use_enum_param ){
    use_enum_param++;
}
void use_sizet ( size_t st ){
    st++;
}
void use_ptrdiff ( ptrdiff_t pt ){
    pt++;
}

// D_04_01.h

bool msg_header_valid ( const char * c ){
    return (bool) c++;
}

// D_04_08.h


ptrX pX;

void use_ptrX ( ptrX p ){
    p++;
}

ptrX get_ptrX ( void ){
    return pX;
}

// D_04_13.h

mutex_t mt;

mutex_t mutex_lock ( void ){
    return mt;
}
void mutex_unlock ( mutex_t m ){
    m.count++;
}

なぜかgccだけ警告が出る。

$ clang 

$ gcc 
clear_lib.c: In function 'use_bool':
clear_lib.c:107:16: warning: increment of a boolean expression [-Wbool-operation]
  107 |  use_bool_param++;
      |   

後の祭り

また、なぜか、Clangは次のシェルスクリプトでエラーがでる。

D_01.sh
#!/bin/sh
# (c) @kaizen_nagoya
echo "https://qiita.com/kaizen_nagoya/items/c6a54925fd3510dc0710"
echo "https://qiita.com/kaizen_nagoya/items/79e771ff681fb27f6070"
echo "https://qiita.com/kaizen_nagoya/items/71f04a0204d5a1114577"
echo "$ clang "
clang D_04_01.c D_04_01.h D_04_02.c D_04_03.c D_04_04.c D_04_05.c D_04_06.c D_04_07.c D_04_08.h D_04_08_1.c D_04_08_2.c D_04_09.c D_04_10.c D_04_10_1.h D_04_10_2.h D_04_10_3.h D_04_11.c D_04_12.c D_04_13.c D_04_13.h D_04_14.c D_04_support.c D_04_system.c clear_lib.c -Wall -o D_04l
if [  -e D_04l ]; then
./D_04l
fi
echo "\r"
echo "$ gcc "
gcc D_04_01.c D_04_01.h D_04_02.c D_04_03.c D_04_04.c D_04_05.c D_04_06.c D_04_07.c D_04_08.h D_04_08_1.c D_04_08_2.c D_04_09.c D_04_10.c D_04_10_1.h D_04_10_2.h D_04_10_3.h D_04_11.c D_04_12.c D_04_13.c D_04_13.h D_04_14.c D_04_support.c D_04_system.c clear_lib.c -Wall -o D_04g
if [  -e D_04g ]; then
./D_04g
fi
echo "\r"

実行結果はこちら。

docker/ubuntu
clang: error: cannot specify -o when generating multiple output files

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

関連資料

https://www.intel.co.jp/content/dam/www/public/ijkk/jp/ja/documents/developer/IA32_Arh_Dev_Man_Vol3_i.pdf
https://www.intel.co.jp/content/dam/www/public/ijkk/jp/ja/documents/developer/248966-024JA.pdf
https://www.intel.co.jp/content/dam/www/public/ijkk/jp/ja/documents/developer/IA32_Arh_Dev_Man_Vol2A_i.pdf