Last Update 2021/04/17
-Wpedantic(-pedantic)
ISOにより定められた言語標準に関する警告を出力
テスト概要
-Wpedanticオプション無しの場合を含む各種出力例
GNU C 拡張機能に対する-Wpedanticオプションの挙動確認
"__" で始まり "__" で終わるキーワードに対する-Wpedanticオプションの挙動確認
実行環境
GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1
GNU C Library 2.28
GNU Binutils 2.31.1
コード例・出力内容中の表記
・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「︙」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。
・「︙」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。
使用ファイル
sample1.c
sample1.c
#include <stdio.h>
int sampFunc(int n)
{
int a[n]; /* 可変長配列 */
long long int i; /* long long int */
int j;
int *pn = (int []){4, 5, 6}; /* 複合リテラル */
for (i=0; i<n; i++)
{
a[i] = i * i;
}
for (i=0; i<n; i++)
{
for (j=0; j<3; j++)
{
printf("a[%d] = %d : b[%d] = %d\n", i, a[i], j, *(pn + j)) ;
}
}
return n;
}
int main(void)
{
int n = sampFunc(3);
printf("n = %d\n", n);
return 0;
}
-Wpedanticオプション無し、-std=c90を指定して実行
$ gcc -std=c90 sample1.c エラー・警告無し
$ ./a.out エラー・警告無し。実行ファイルを生成
a[0] = 0 : b[0] = 4
a[0] = 0 : b[1] = 5
a[0] = 0 : b[2] = 6
a[1] = 1 : b[0] = 4
a[1] = 1 : b[1] = 5
a[1] = 1 : b[2] = 6
a[2] = 4 : b[0] = 4
a[2] = 4 : b[1] = 5
a[2] = 4 : b[2] = 6
n = 3
$
-Wpedanticオプション有り、-std=c90を指定して実行
着色部(青)は-Wpedanticオプションによる警告
着色部(青)は-Wpedanticオプションによる警告
$ gcc -std=c90 -Wpedantic sample1.c
sample1.c: In function ‘sampFunc’:
sample1.c:5:2: warning: ISO C90 forbids variable length array ‘a’ [-Wvla]
int a[n]; /* 可変長配列 */
^~~
sample1.c:6:7: warning: ISO C90 does not support ‘long long’ [-Wlong-long]
long long int i; /* long long int */
^~~~
sample1.c:9:20: warning: ISO C90 forbids compound literals [-Wpedantic]
int *pn = (int []){4, 5, 6}; /* 複合リテラル */
^
$ ./a.out 警告のみの出力となり、実行ファイルは生成される
a[0] = 0 : b[0] = 4
a[0] = 0 : b[1] = 5
a[0] = 0 : b[2] = 6
a[1] = 1 : b[0] = 4
a[1] = 1 : b[1] = 5
a[1] = 1 : b[2] = 6
a[2] = 4 : b[0] = 4
a[2] = 4 : b[1] = 5
a[2] = 4 : b[2] = 6
n = 3
$
-Wpedanticオプション有り、-std=c99を指定して実行
$ gcc -std=c99 -Wpedantic sample1.c
$ ./a.out エラー・警告無し。実行ファイルを生成
a[0] = 0 : b[0] = 4
a[0] = 0 : b[1] = 5
a[0] = 0 : b[2] = 6
a[1] = 1 : b[0] = 4
a[1] = 1 : b[1] = 5
a[1] = 1 : b[2] = 6
a[2] = 4 : b[0] = 4
a[2] = 4 : b[1] = 5
a[2] = 4 : b[2] = 6
n = 3
$
使用ファイル
sample2.c
sample2.c
#include <stdio.h>
int sampFunc(int n)
{
int a[n]; /* 可変長配列 */
long long int i; /* long long int */
int j;
int *pn = (int []){4, 5, 6}; /* 複合リテラル */
for (i=0; i<n; i++)
{
a[i] = i * i;
}
for (i=0; i<n; i++)
{
for (j=0; j<3; j++)
{
printf("a[%d] = %d : b[%d] = %d\n", i, a[i], j, *(pn + j)) ;
}
}
return n;
}
int main(void)
{
int i;
typeof (int *) p; /* typeofの使用 */
i = 100;
p = &i;
int n = sampFunc(3);
printf("n = %d\n", n);
printf("*p = %d\n", *p);
return 0;
}
-Wpedanticオプション有り、-std=c90を指定して実行
着色部(青)は-Wpedanticオプションによる警告
着色部(青)は-Wpedanticオプションによる警告
$ gcc -std=c90 -Wpedantic sample2.c
sample2.c: In function ‘sampFunc’:
sample2.c:5:2: warning: ISO C90 forbids variable length array ‘a’ [-Wvla]
int a[n]; /* 可変長配列 */
^~~
sample2.c:6:7: warning: ISO C90 does not support ‘long long’ [-Wlong-long]
long long int i; /* long long int */
^~~~
sample2.c:9:20: warning: ISO C90 forbids compound literals [-Wpedantic]
int *pn = (int []){4, 5, 6}; /* 複合リテラル */
^
sample2.c: In function ‘main’:
sample2.c:30:10: error: expected expression before ‘int’
typeof (int *) p; /* typeofの使用 */
^~~
sample2.c:30:16: error: expected ‘;’ before ‘p’
typeof (int *) p; /* typeofの使用 */
^~
;
sample2.c:33:2: error: ‘p’ undeclared (first use in this function)
p = &i;
^
sample2.c:33:2: note: each undeclared identifier is reported only once for each function it appears in
sample2.c:35:2: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
int n = sampFunc(3);
^~~
$ エラー発生のため実行ファイルは生成されない
-Wpedanticオプション有り、-std=c99を指定して実行
$ gcc -std=c99 -Wpedantic sample2.c
sample2.c: In function ‘main’:
sample2.c:30:2: warning: implicit declaration of function ‘typeof’; did you mean ‘feof’? [-Wimplicit-function-declaration]
typeof (int *) p; /* typeofの使用 */
^~~~~~
feof
sample2.c:30:10: error: expected expression before ‘int’
typeof (int *) p; /* typeofの使用 */
^~~
sample2.c:30:16: error: expected ‘;’ before ‘p’
typeof (int *) p; /* typeofの使用 */
^~
;
sample2.c:33:2: error: ‘p’ undeclared (first use in this function)
p = &i;
^
sample2.c:33:2: note: each undeclared identifier is reported only once for each function it appears in
$ エラー発生のため実行ファイルは生成されない
-Wpedanticオプションによる警告は無し
-Wpedanticオプション有り、-std=gnu90を指定して実行
着色部(青)は-Wpedanticオプションによる警告
着色部(青)は-Wpedanticオプションによる警告
$ gcc -std=gnu90 -Wpedantic sample2.c
sample2.c: In function ‘sampFunc’:
sample2.c:5:2: warning: ISO C90 forbids variable length array ‘a’ [-Wvla]
int a[n]; /* 可変長配列 */
^~~
sample2.c:6:7: warning: ISO C90 does not support ‘long long’ [-Wlong-long]
long long int i; /* long long int */
^~~~
sample2.c:9:20: warning: ISO C90 forbids compound literals [-Wpedantic]
int *pn = (int []){4, 5, 6}; /* 複合リテラル */
^
sample2.c: In function ‘main’:
sample2.c:35:2: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
int n = sampFunc(3);
^~~
$ ./a.out エラー無し。実行ファイルを生成
a[0] = 0 : b[0] = 4
a[0] = 0 : b[1] = 5
a[0] = 0 : b[2] = 6
a[1] = 1 : b[0] = 4
a[1] = 1 : b[1] = 5
a[1] = 1 : b[2] = 6
a[2] = 4 : b[0] = 4
a[2] = 4 : b[1] = 5
a[2] = 4 : b[2] = 6
n = 3
*p = 100
$
-Wpedanticオプション有り、-std=gnu90を指定して実行
$ gcc -std=gnu99 -Wpedantic sample2.c
$ ./a.out エラー・警告無し。実行ファイルを生成
a[0] = 0 : b[0] = 4
a[0] = 0 : b[1] = 5
a[0] = 0 : b[2] = 6
a[1] = 1 : b[0] = 4
a[1] = 1 : b[1] = 5
a[1] = 1 : b[2] = 6
a[2] = 4 : b[0] = 4
a[2] = 4 : b[1] = 5
a[2] = 4 : b[2] = 6
n = 3
*p = 100
$
使用ファイル
sample3.c
sample3.c
#include <stdio.h>
static inline int sampFunc1(void) /* inlineの使用 */
{
return 1;
}
static __inline__ int sampFunc2(void) /* __inline__の使用 */
{
return 2;
}
int main(void)
{
printf("sampFunc1 = %d\n", sampFunc1());
printf("sampFunc2 = %d\n", sampFunc2());
return 0;
}
-Wpedanticオプション無し、-std=c90を指定して実行
$ gcc -std=c90 sample3.c
sample3.c:3:14: error: expected ‘;’ before ‘int’
static inline int sampFunc1(void) /* inlineの使用 */
^~~~
;
$ inline がキーワードと認識されずエラー発生
-Wpedanticに関する出力無し
-Wpedanticオプション有り、-std=c90を指定して実行
$ gcc -std=c90 -Wpedantic sample3.c
sample3.c:3:14: error: expected ‘;’ before ‘int’
static inline int sampFunc1(void) /* inlineの使用 */
^~~~
;
$ inline がキーワードと認識されずエラー発生
-Wpedanticに関する出力無し
-Wpedanticオプション無し、-std=c99を指定して実行
$ gcc -std=c99 sample3.c
$ エラー・警告無し
-Wpedanticに関する出力無し
-Wpedanticオプション有り、-std=c99を指定して実行
$ gcc -std=c99 -Wpedantic sample3.c
$ エラー・警告無し
-Wpedanticに関する出力無し