Last Update 2021/04/28
-Wduplicate-decl-specifier
宣言にconst、volatile、 restrict、_Atomicの各指定子が重複している場合に警告
テスト概要
-Wduplicate-decl-specifier オプションによる警告の出力例
実行環境
GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1
GNU C Library 2.28
GNU Binutils 2.31.1
コード例・出力内容中の表記
・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「︙」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。
・「︙」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。
使用ファイル
sample.c
#include <stdio.h>
int main(void)
{
int n[3] = {1, 2, 3};
/* p1 は読み取りのみ、*p1 も読み取りのみ */
const int *const p1 = &n[0];
printf("p1 = %d\n", *p1);
/* p2 は変更可、*p2 は読み取りのみ */
/* const が重複 */
const int const *p2 = &n[1];
printf("p2 = %d\n", *p2);
/* p1 は読み取りのみ、*p1 も読み取りのみ */
/* 宣言内容は *p1 と同じ */
int const *const p3 = &n[2];
printf("p3 = %d\n", *p3);
return 0;
}
動作テスト
-Wduplicate-decl-specifierオプションを使用せずにコンパイルを実行
$ gcc sample.c
$ エラー・警告無し
-Wduplicate-decl-specifierオプションを使用してコンパイルを実行
$ gcc -Wduplicate-decl-specifier sample.c
sample.c: In function ‘main’:
sample.c:13:12: warning: duplicate ‘const’ declaration specifier [-Wduplicate-decl-specifier]
const int const *p2 = &n[1]; 構文上constが重複しているため警告出力
^~~~~
$
-Wallオプションを使用してコンパイルを実行
$ gcc -Wall sample.c
sample.c: In function ‘main’:
sample.c:13:12: warning: duplicate ‘const’ declaration specifier [-Wduplicate-decl-specifier]
const int const *p2 = &n[1];
^~~~~
$ -Wallに-Wduplicate-decl-specifierが含まれるため同じ内容の警告を出力