From: Alejandro Colomar Date: Wed, 21 May 2025 23:15:43 +0000 (+0200) Subject: c: Add -Wpedantic diagnostic for _Countof [PR117025] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9d9ac1670af224bb9fa2411fe392f98d20fbf751;p=thirdparty%2Fgcc.git c: Add -Wpedantic diagnostic for _Countof [PR117025] It has been standardized in C2y. PR c/117025 gcc/c/ChangeLog: * c-parser.cc (c_parser_sizeof_or_countof_expression): Add -Wpedantic diagnostic for _Countof in <= C23 mode. gcc/testsuite/ChangeLog: * gcc.dg/countof-compat.c: New test. * gcc.dg/countof-no-compat.c: New test. * gcc.dg/countof-pedantic.c: New test. * gcc.dg/countof-pedantic-errors.c: New test. Signed-off-by: Alejandro Colomar --- diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index 733cb312341..98a0c563280 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -10649,6 +10649,10 @@ c_parser_sizeof_or_countof_expression (c_parser *parser, enum rid rid) start = c_parser_peek_token (parser)->location; + if (rid == RID_COUNTOF) + pedwarn_c23 (start, OPT_Wpedantic, + "ISO C does not support %qs before C23", op_name); + c_parser_consume_token (parser); c_inhibit_evaluation_warnings++; if (rid == RID_COUNTOF) diff --git a/gcc/testsuite/gcc.dg/countof-compat.c b/gcc/testsuite/gcc.dg/countof-compat.c new file mode 100644 index 00000000000..ab5b4ae6219 --- /dev/null +++ b/gcc/testsuite/gcc.dg/countof-compat.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c2y -pedantic-errors -Wc23-c2y-compat" } */ + +#include + +int a[1]; +int b[countof(a)]; +int c[_Countof(a)]; /* { dg-warning "ISO C does not support" } */ diff --git a/gcc/testsuite/gcc.dg/countof-no-compat.c b/gcc/testsuite/gcc.dg/countof-no-compat.c new file mode 100644 index 00000000000..4a244cf222f --- /dev/null +++ b/gcc/testsuite/gcc.dg/countof-no-compat.c @@ -0,0 +1,5 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c23 -pedantic-errors -Wno-c23-c2y-compat" } */ + +int a[1]; +int b[_Countof(a)]; diff --git a/gcc/testsuite/gcc.dg/countof-pedantic-errors.c b/gcc/testsuite/gcc.dg/countof-pedantic-errors.c new file mode 100644 index 00000000000..5d5bedbe1f7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/countof-pedantic-errors.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c23 -pedantic-errors" } */ + +#include + +int a[1]; +int b[countof(a)]; +int c[_Countof(a)]; /* { dg-error "ISO C does not support" } */ diff --git a/gcc/testsuite/gcc.dg/countof-pedantic.c b/gcc/testsuite/gcc.dg/countof-pedantic.c new file mode 100644 index 00000000000..408dc6f9366 --- /dev/null +++ b/gcc/testsuite/gcc.dg/countof-pedantic.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c23 -pedantic" } */ + +#include + +int a[1]; +int b[countof(a)]; +int c[_Countof(a)]; /* { dg-warning "ISO C does not support" } */