It has been standardized in C2y.
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:
Test _Countof diagnostics with -Wc23-c2y-compat on C2y.
* gcc.dg/countof-no-compat.c:
Test _Countof diagnostics with -Wno-c23-c2y-compat on C23.
* gcc.dg/countof-pedantic.c:
Test _Countof diagnostics with -pedantic on C23.
* gcc.dg/countof-pedantic-errors.c:
Test _Countof diagnostics with -pedantic-errors on C23.
Signed-off-by: Alejandro Colomar <[email protected]>
---
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(+)
create mode 100644 gcc/testsuite/gcc.dg/countof-compat.c
create mode 100644 gcc/testsuite/gcc.dg/countof-no-compat.c
create mode 100644 gcc/testsuite/gcc.dg/countof-pedantic-errors.c
create mode 100644 gcc/testsuite/gcc.dg/countof-pedantic.c
diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index faa03c4903a2..de48a016dcdb 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -10641,6 +10641,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" } */
--
2.49.0