https://gcc.gnu.org/g:9d9ac1670af224bb9fa2411fe392f98d20fbf751

commit r16-918-g9d9ac1670af224bb9fa2411fe392f98d20fbf751
Author: Alejandro Colomar <a...@kernel.org>
Date:   Thu May 22 01:15:43 2025 +0200

    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 <a...@kernel.org>

Diff:
---
 gcc/c/c-parser.cc                              | 4 ++++
 gcc/testsuite/gcc.dg/countof-compat.c          | 8 ++++++++
 gcc/testsuite/gcc.dg/countof-no-compat.c       | 5 +++++
 gcc/testsuite/gcc.dg/countof-pedantic-errors.c | 8 ++++++++
 gcc/testsuite/gcc.dg/countof-pedantic.c        | 8 ++++++++
 5 files changed, 33 insertions(+)

diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index 733cb312341e..98a0c5632805 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 000000000000..ab5b4ae6219c
--- /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 <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
index 000000000000..4a244cf222f6
--- /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 000000000000..5d5bedbe1f7e
--- /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 <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
index 000000000000..408dc6f93667
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/countof-pedantic.c
@@ -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" } */

Reply via email to