]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Add -Wpedantic diagnostic for _Countof [PR117025]
authorAlejandro Colomar <alx@kernel.org>
Wed, 21 May 2025 23:15:43 +0000 (01:15 +0200)
committerJoseph Myers <josmyers@redhat.com>
Tue, 27 May 2025 20:20:42 +0000 (20:20 +0000)
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 <alx@kernel.org>
gcc/c/c-parser.cc
gcc/testsuite/gcc.dg/countof-compat.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/countof-no-compat.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/countof-pedantic-errors.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/countof-pedantic.c [new file with mode: 0644]

index 733cb312341e57c1cb8b7854f7c713052909854c..98a0c5632805f143fd11b0819440614c217d4bf6 100644 (file)
@@ -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 (file)
index 0000000..ab5b4ae
--- /dev/null
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c2y -pedantic-errors -Wc23-c2y-compat" } */
+
+#include <stdcountof.h>
+
+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 (file)
index 0000000..4a244cf
--- /dev/null
@@ -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 (file)
index 0000000..5d5bedb
--- /dev/null
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c23 -pedantic-errors" } */
+
+#include <stdcountof.h>
+
+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 (file)
index 0000000..408dc6f
--- /dev/null
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c23 -pedantic" } */
+
+#include <stdcountof.h>
+
+int a[1];
+int b[countof(a)];
+int c[_Countof(a)];  /* { dg-warning "ISO C does not support" } */