> Date: 2026-11-01 23:57:15+0100 > From: Alejandro Colomar <[email protected]> > > [Resend, with correct address, and with 'v7' in the Subject line.] > > Hi, > > Changes in v7 compared to v5 (v6 never existed): > > - Add tests to make sure that _Countof() works reasonably well with > forward declarations of array parameters. > - Rebase on top of master. > > See the range-diff at the bottom. > > Joseph, as we agreed recently, I want to make _Countof() support array > parameters where the length is declared only in the forward declaration. > However, that seems non-trivial, so it might be interesting to do a > first release that doesn't yet compile for that. I've added tests to > make sure that it never produces UB, but rather just compiler errors or > reasonable behavior (one of the specified lengths). > > I think it would be interesting to release this feature already, because > it works decently well for most reasonable code. If we have this > feature already in GCC 17, then programs could start using this feature > in some (~5 for many projects) years from now, while delaying it until > we have better support for forward declarations could delay it by one or > two years, which would be bad for bounds safety in general. > > Do you think we could release this, with the acknowledgement that we > want to eventually fix the test which has a 'FIXME'? I plan to work on > that, but can't promise when I'll have it done. > > The current patch set is robust to be released, IMO.
Tests pass:
alx@devuan:~/src/gnu/gcc/countof_ap$ git log --oneline master^..cap7
026b76ce72ec (HEAD -> countof_ap, tag: cap7) c: Add support for array
parameters in _Countof
53c196b9667d c: Refactor implementation of _Countof
1892effc2f4b (gnu/trunk, gnu/master, gnu/HEAD, master) fortran: Use
span indexing for polymorphic target dummies [PR127827]
alx@devuan:~/src/gnu/gcc/countof_ap$ git reset master --h
HEAD is now at 1892effc2f4b fortran: Use span indexing for polymorphic
target dummies [PR127827]
alx@devuan:~/src/gnu/gcc/countof_ap$ mkdir ../cap7
alx@devuan:~/src/gnu/gcc/countof_ap$ cd ../cap7/
alx@devuan:~/src/gnu/gcc/cap7$ set -o pipefail
alx@devuan:~/src/gnu/gcc/cap7$ ../countof_ap/configure
--disable-multilib --prefix=/opt/local/gnu/gcc/cap7 |& ts -s | tail -n1; echo
$?; make -j24 bootstrap |& ts -s | tail -n1; echo $?; make -j24 check |& ts -s
| tail -n1; echo $?;
00:00:01 config.status: creating Makefile
0
00:22:03 make[1]: Leaving directory '/srv/alx/src/gnu/gcc/cap7'
0
00:43:40 make[1]: Leaving directory '/srv/alx/src/gnu/gcc/cap7'
0
alx@devuan:~/src/gnu/gcc/cap7$ cd ../countof_ap/
alx@devuan:~/src/gnu/gcc/countof_ap$ git merge --ff-only cap7
Updating 1892effc2f4b..026b76ce72ec
Fast-forward
gcc/c/c-typeck.cc | 45 +++++++-
gcc/doc/extend.texi | 11 ++
gcc/testsuite/gcc.dg/countof-compile.c | 8 +-
gcc/testsuite/gcc.dg/countof-param-compile.c | 104 ++++++++++++++++++
gcc/testsuite/gcc.dg/countof-param-pedantic.c | 11 ++
gcc/testsuite/gcc.dg/countof-param.c | 25 +++++
6 files changed, 191 insertions(+), 13 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/countof-param-compile.c
create mode 100644 gcc/testsuite/gcc.dg/countof-param-pedantic.c
create mode 100644 gcc/testsuite/gcc.dg/countof-param.c
alx@devuan:~/src/gnu/gcc/countof_ap$ cd ../
alx@devuan:~/src/gnu/gcc$ mv cap7/ cap7_b4
alx@devuan:~/src/gnu/gcc$ mkdir cap7
alx@devuan:~/src/gnu/gcc$ cd cap7
alx@devuan:~/src/gnu/gcc/cap7$ ../countof_ap/configure
--disable-multilib --prefix=/opt/local/gnu/gcc/cap7 |& ts -s | tail -n1; echo
$?; make -j24 bootstrap |& ts -s | tail -n1; echo $?; make -j24 check |& ts -s
| tail -n1; echo $?;
00:00:02 config.status: creating Makefile
0
00:21:55 make[1]: Leaving directory '/srv/alx/src/gnu/gcc/cap7'
0
00:43:59 make[1]: Leaving directory '/srv/alx/src/gnu/gcc/cap7'
0
alx@devuan:~/src/gnu/gcc/cap7$ find -type f | grep '\.sum$' | while
read f; do diff -u "../cap7_b4/$f" "$f"; done;
--- ../cap7_b4/./x86_64-pc-linux-gnu/libitm/testsuite/libitm.sum
2026-11-01 21:05:38.712249381 +0100
+++ ./x86_64-pc-linux-gnu/libitm/testsuite/libitm.sum 2026-11-01
22:36:53.892865284 +0100
@@ -1,4 +1,4 @@
-Test run by alx on Sun Nov 1 21:05:35 2026
+Test run by alx on Sun Nov 1 22:36:50 2026
Native configuration is x86_64-pc-linux-gnu
=== libitm tests ===
--- ../cap7_b4/./x86_64-pc-linux-gnu/libatomic/testsuite/libatomic.sum
2026-11-01 21:05:37.423006904 +0100
+++ ./x86_64-pc-linux-gnu/libatomic/testsuite/libatomic.sum
2026-11-01 22:36:52.290750836 +0100
@@ -1,4 +1,4 @@
-Test run by alx on Sun Nov 1 21:05:35 2026
+Test run by alx on Sun Nov 1 22:36:50 2026
Native configuration is x86_64-pc-linux-gnu
=== libatomic tests ===
--- ../cap7_b4/./x86_64-pc-linux-gnu/libgomp/testsuite/libgomp.sum
2026-11-01 21:07:57.973840085 +0100
+++ ./x86_64-pc-linux-gnu/libgomp/testsuite/libgomp.sum 2026-11-01
22:39:22.971716165 +0100
@@ -1,4 +1,4 @@
-Test run by alx on Sun Nov 1 21:05:35 2026
+Test run by alx on Sun Nov 1 22:36:50 2026
Native configuration is x86_64-pc-linux-gnu
=== libgomp tests ===
---
../cap7_b4/./x86_64-pc-linux-gnu/libstdc++-v3/testsuite/libstdc++.sum
2026-11-01 21:49:15.263253088 +0100
+++ ./x86_64-pc-linux-gnu/libstdc++-v3/testsuite/libstdc++.sum
2026-11-01 23:20:49.595924539 +0100
@@ -1,4 +1,4 @@
-Test run by alx on Sun Nov 1 21:05:37 2026
+Test run by alx on Sun Nov 1 22:36:52 2026
Native configuration is x86_64-pc-linux-gnu
=== libstdc++ tests ===
--- ../cap7_b4/./gcc/testsuite/objc/objc.sum 2026-11-01
21:06:57.013057336 +0100
+++ ./gcc/testsuite/objc/objc.sum 2026-11-01 22:37:36.806268067
+0100
@@ -1,4 +1,4 @@
-Test run by alx on Sun Nov 1 21:05:39 2026
+Test run by alx on Sun Nov 1 22:36:51 2026
Native configuration is x86_64-pc-linux-gnu
=== objc tests ===
--- ../cap7_b4/./gcc/testsuite/g++/g++.sum 2026-11-01
21:24:18.393431841 +0100
+++ ./gcc/testsuite/g++/g++.sum 2026-11-01 22:55:10.418300316 +0100
@@ -1,4 +1,4 @@
-Test run by alx on Sun Nov 1 21:05:36 2026
+Test run by alx on Sun Nov 1 22:36:51 2026
Native configuration is x86_64-pc-linux-gnu
=== g++ tests ===
--- ../cap7_b4/./gcc/testsuite/gcc/gcc.sum 2026-11-01
21:30:36.935589250 +0100
+++ ./gcc/testsuite/gcc/gcc.sum 2026-11-01 23:01:22.972017943 +0100
@@ -1,4 +1,4 @@
-Test run by alx on Sun Nov 1 21:05:36 2026
+Test run by alx on Sun Nov 1 22:36:51 2026
Native configuration is x86_64-pc-linux-gnu
=== gcc tests ===
@@ -91657,27 +91657,54 @@
PASS: gcc.dg/counted-by-unary.c execution test
PASS: gcc.dg/countof-compat.c (test for warnings, line 8)
PASS: gcc.dg/countof-compat.c (test for excess errors)
-PASS: gcc.dg/countof-compile.c (test for errors, line 114)
-PASS: gcc.dg/countof-compile.c (test for errors, line 123)
+PASS: gcc.dg/countof-compile.c (test for errors, line 108)
+PASS: gcc.dg/countof-compile.c (test for errors, line 117)
PASS: gcc.dg/countof-compile.c (test for errors, line 23)
-PASS: gcc.dg/countof-compile.c (test for errors, line 27)
-PASS: gcc.dg/countof-compile.c (test for errors, line 38)
-PASS: gcc.dg/countof-compile.c (test for errors, line 46)
+PASS: gcc.dg/countof-compile.c (test for errors, line 34)
+PASS: gcc.dg/countof-compile.c (test for errors, line 40)
+PASS: gcc.dg/countof-compile.c (test for errors, line 58)
+PASS: gcc.dg/countof-compile.c (test for errors, line 61)
PASS: gcc.dg/countof-compile.c (test for errors, line 64)
-PASS: gcc.dg/countof-compile.c (test for errors, line 67)
-PASS: gcc.dg/countof-compile.c (test for errors, line 70)
+PASS: gcc.dg/countof-compile.c (test for errors, line 76)
+PASS: gcc.dg/countof-compile.c (test for errors, line 77)
+PASS: gcc.dg/countof-compile.c (test for errors, line 78)
+PASS: gcc.dg/countof-compile.c (test for errors, line 79)
+PASS: gcc.dg/countof-compile.c (test for errors, line 80)
+PASS: gcc.dg/countof-compile.c (test for errors, line 81)
PASS: gcc.dg/countof-compile.c (test for errors, line 82)
PASS: gcc.dg/countof-compile.c (test for errors, line 83)
PASS: gcc.dg/countof-compile.c (test for errors, line 84)
-PASS: gcc.dg/countof-compile.c (test for errors, line 85)
-PASS: gcc.dg/countof-compile.c (test for errors, line 86)
-PASS: gcc.dg/countof-compile.c (test for errors, line 87)
PASS: gcc.dg/countof-compile.c (test for errors, line 88)
-PASS: gcc.dg/countof-compile.c (test for errors, line 89)
-PASS: gcc.dg/countof-compile.c (test for errors, line 90)
-PASS: gcc.dg/countof-compile.c (test for errors, line 94)
PASS: gcc.dg/countof-compile.c (test for excess errors)
PASS: gcc.dg/countof-no-compat.c (test for excess errors)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 10)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 18)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 21)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 22)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 27)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 28)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 29)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 30)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 31)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 32)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 33)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 34)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 38)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 42)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 57)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 71)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 8)
+PASS: gcc.dg/countof-param-compile.c (test for errors, line 9)
+PASS: gcc.dg/countof-param-compile.c (test for excess errors)
+PASS: gcc.dg/countof-param-pedantic.c (test for warnings, line 10)
+PASS: gcc.dg/countof-param-pedantic.c (test for warnings, line 9)
+PASS: gcc.dg/countof-param-pedantic.c (test for excess errors)
+PASS: gcc.dg/countof-param.c (test for warnings, line 10)
+PASS: gcc.dg/countof-param.c (test for warnings, line 15)
+PASS: gcc.dg/countof-param.c (test for warnings, line 16)
+PASS: gcc.dg/countof-param.c (test for warnings, line 9)
+PASS: gcc.dg/countof-param.c (test for excess errors)
+PASS: gcc.dg/countof-param.c execution test
PASS: gcc.dg/countof-pedantic-errors.c (test for errors, line 8)
PASS: gcc.dg/countof-pedantic-errors.c (test for excess errors)
PASS: gcc.dg/countof-pedantic.c (test for warnings, line 8)
@@ -237510,7 +237537,7 @@
=== gcc Summary ===
-# of expected passes 231295
+# of expected passes 231322
# of unexpected failures 86
# of unexpected successes 24
# of expected failures 1657
--- ../cap7_b4/./gcc/testsuite/gfortran/gfortran.sum 2026-11-01
21:21:38.542879775 +0100
+++ ./gcc/testsuite/gfortran/gfortran.sum 2026-11-01
22:51:49.534122387 +0100
@@ -1,4 +1,4 @@
-Test run by alx on Sun Nov 1 21:05:36 2026
+Test run by alx on Sun Nov 1 22:36:51 2026
Native configuration is x86_64-pc-linux-gnu
=== gfortran tests ===
Cheers,
Alex
>
>
> Have a lovely night!
> Alex
>
>
> Alejandro Colomar (2):
> c: Refactor implementation of _Countof
> c: Add support for array parameters in _Countof
>
> gcc/c/c-typeck.cc | 45 +++++++-
> gcc/doc/extend.texi | 11 ++
> gcc/testsuite/gcc.dg/countof-compile.c | 8 +-
> gcc/testsuite/gcc.dg/countof-param-compile.c | 104 ++++++++++++++++++
> gcc/testsuite/gcc.dg/countof-param-pedantic.c | 11 ++
> gcc/testsuite/gcc.dg/countof-param.c | 25 +++++
> 6 files changed, 191 insertions(+), 13 deletions(-)
> create mode 100644 gcc/testsuite/gcc.dg/countof-param-compile.c
> create mode 100644 gcc/testsuite/gcc.dg/countof-param-pedantic.c
> create mode 100644 gcc/testsuite/gcc.dg/countof-param.c
>
> Range-diff:
> 1: 31191ae061c2 ! 1: 53c196b9667d c: Refactor implementation of _Countof()
> @@ Metadata
> Author: Alejandro Colomar <[email protected]>
>
> ## Commit message ##
> - c: Refactor implementation of _Countof()
> + c: Refactor implementation of _Countof
>
> No changes intended. This refactor prepares the code so that a
> future
> - patch extending the semantic of _Countof() will be smaller.
> + patch extending the semantics of _Countof() will be smaller.
>
> gcc/c/ChangeLog:
>
> @@ gcc/c/c-typeck.cc: c_expr_countof_expr (location_t loc, struct c_expr
> expr)
> - tree folded_expr = c_fully_fold (expr.value,
> require_constant_value,
> - &expr_const_operands);
> - ret.value = c_countof_type (loc, TREE_TYPE (folded_expr));
> -- c_last_sizeof_arg = expr.value;
> -- c_last_sizeof_loc = loc;
> + c_last_sizeof_arg = expr.value;
> + c_last_sizeof_loc = loc;
> ret.original_code = COUNTOF_EXPR;
> ret.original_type = NULL;
> ret.m_decimal = 0;
> -- if (is_top_array_vla (TREE_TYPE (folded_expr)))
> +- if (top_array_vla_p (TREE_TYPE (folded_expr)))
> +
> + tree folded_expr = c_fully_fold (expr.value,
> require_constant_value,
> + &expr_const_operands);
> -+
> + tree type = TREE_TYPE (folded_expr);
> + ret.value = c_countof_type (loc, type);
> -+ c_last_sizeof_arg = expr.value;
> -+ c_last_sizeof_loc = loc;
> +
> -+ if (is_top_array_vla (type))
> ++ if (top_array_vla_p (type))
> {
> /* countof is evaluated when given a vla. */
> ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
> @@ gcc/c/c-typeck.cc: c_expr_countof_expr (location_t loc, struct c_expr
> expr)
> C_MAYBE_CONST_EXPR_NON_CONST (ret.value) =
> !expr_const_operands;
> SET_EXPR_LOCATION (ret.value, loc);
> }
> -- pop_maybe_used (is_top_array_vla (TREE_TYPE (folded_expr)));
> -+ pop_maybe_used (is_top_array_vla (type));
> +- pop_maybe_used (top_array_vla_p (TREE_TYPE (folded_expr)));
> ++ pop_maybe_used (top_array_vla_p (type));
> }
> return ret;
> }
> 2: b1ca7e6c7135 ! 2: 026b76ce72ec c: Add support for array parameters in
> _Countof
> @@ Commit message
> This extension will allow using _Countof() in more places, reducing
> the
> chances of specifying an incorrect size accidentally.
>
> - We should eventually add -farray-parameters-are-const to make array
> - parameters be implicitly const-qualified (similar to
> -Wwrite-strings).
> + If an array parameter has a forward declaration, it is unspecified
> which
> + of the lengths is taken. It is generally unwise to specify the
> length
> + in the forward declarations, and it is even more unwise to specify it
> + more than once. Currently, the implementation takes the length from
> + the actual declaration, and if it doesn't specify the length, it
> won't
> + compile. In the future, we want to improve this so that the length
> + from a forward declaration is taken if the actual declaration lacks
> one.
> + But that's more complex, so let's keep it as is for now.
>
> Link: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121271>
>
> @@ Commit message
> Signed-off-by: Alejandro Colomar <[email protected]>
>
> ## gcc/c/c-typeck.cc ##
> -@@ gcc/c/c-typeck.cc: is_top_array_vla (tree type)
> - return var;
> +@@ gcc/c/c-typeck.cc: top_array_vla_p (const_tree type)
> + || TREE_CODE (TYPE_MIN_VALUE (d)) != INTEGER_CST;
> }
>
> +
> @@ gcc/c/c-typeck.cc: is_top_array_vla (tree type)
>
> struct c_expr
> @@ gcc/c/c-typeck.cc: c_expr_countof_expr (location_t loc, struct c_expr
> expr)
> + tree folded_expr = c_fully_fold (expr.value,
> require_constant_value,
> &expr_const_operands);
> -
> tree type = TREE_TYPE (folded_expr);
> + if (POINTER_TYPE_P (type))
> + {
> -+ if (tree rv = c_array_parm_orig_type (expr.value))
> ++ if (tree t = c_array_parm_orig_type (expr.value))
> + {
> + pedwarn (input_location, OPT_Wpedantic,
> + "ISO C does not support array parameters in %qs",
> + "_Countof");
> -+ type = rv;
> ++ type = t;
> + }
> + }
> +
> ret.value = c_countof_type (loc, type);
> - c_last_sizeof_arg = expr.value;
> - c_last_sizeof_loc = loc;
> +
> + if (top_array_vla_p (type))
>
> ## gcc/doc/extend.texi ##
> @@ gcc/doc/extend.texi: _Countof (int [7][n++]); // integer constant
> expression
> @@ gcc/testsuite/gcc.dg/countof-param-compile.c (new)
> +{
> + a++;
> + _Countof (a);
> ++}
> ++
> ++void
> ++fwd_a_first (char a[6]; char a[])
> ++{
> ++ _Countof (a); /* { dg-error "incomplete" } */
> ++ // FIXME: This should pass:
> ++ //_Static_assert (_Countof (a) == 6);
> ++}
> ++
> ++void
> ++fwd_ap_first (char (*a)[6]; char (*a)[])
> ++{
> ++ _Static_assert (_Countof (*a) == 6);
> ++}
> ++
> ++void
> ++fwd_a_second (char a[]; char a[7])
> ++{
> ++ _Static_assert (_Countof (a) == 7);
> ++}
> ++
> ++void
> ++fwd_ap_second (char (*a)[]; char (*a)[7])
> ++{
> ++ _Static_assert (_Countof (*a) == 7);
> ++}
> ++
> ++void
> ++fwd_a_both (char a[6]; char a[7])
> ++{
> ++ _Static_assert (_Countof (a) == 6 || _Countof (a) == 7);
> ++}
> ++
> ++void
> ++fwd_ap_both (char (*a)[6]; char (*a)[6])
> ++{
> ++ _Static_assert (_Countof (*a) == 6);
> +}
>
> ## gcc/testsuite/gcc.dg/countof-param-pedantic.c (new) ##
>
> base-commit: a440b382e43203857de9195eb526c4a16f21ceb1
> --
> 2.55.0
>
--
<https://www.alejandro-colomar.es>
signature.asc
Description: PGP signature
