Am Sonntag, dem 10.11.2024 um 11:32 +0100 schrieb Alejandro Colomar:
> Hi!
>
> Your favourite operator with the most controversial name comes back with
> support for [0], thanks to Martin Uecker. In movie theaters, and
> probably in GCC 16.
>
> For those who fight in a side in the name wars, here's a reminder of a
> fair survey (by JeanHeyd) which might end the war with a peace treaty:
> <https://thephd.dev/the-big-array-size-survey-for-c>.
>
>
> Changes since v17:
>
> - Rebase after the recent patches added by Martin, which made [0][n]
> and [*][n] have distinct representation, and thus allowed making
> __countof__(int [0][n]) be a constant expression.
>
> - Make __countof__(int [0][n]) a constant expression. Thanks, Martin!
> Update the testsuite to reflect this too, of course.
>
> - Rename small function in the testsuite (automatic => completed).
>
> See the range-diff below for the exact differences since v17.
>
>
> Martin, this worked out of the box. I'll reply to this email with the
> regression-test session results; they all passed. [0] works like a
> charm.
Yes, it is nice how everything starts to fall into place
once you remove enough complexity and special cases ...
Martin
>
>
> Have a lovely day!
> Alex
>
>
> Alejandro Colomar (2):
> contrib/: Add support for Cc: and Link: tags
> c: Add __countof__ operator
>
> contrib/gcc-changelog/git_commit.py | 5 +-
> gcc/c-family/c-common.cc | 26 +++++
> gcc/c-family/c-common.def | 3 +
> gcc/c-family/c-common.h | 2 +
> gcc/c/c-decl.cc | 22 +++-
> gcc/c/c-parser.cc | 62 +++++++---
> gcc/c/c-tree.h | 4 +
> gcc/c/c-typeck.cc | 115 ++++++++++++++++++-
> gcc/doc/extend.texi | 30 +++++
> gcc/testsuite/gcc.dg/countof-compile.c | 125 +++++++++++++++++++++
> gcc/testsuite/gcc.dg/countof-vla.c | 45 ++++++++
> gcc/testsuite/gcc.dg/countof.c | 150 +++++++++++++++++++++++++
> 12 files changed, 564 insertions(+), 25 deletions(-)
> create mode 100644 gcc/testsuite/gcc.dg/countof-compile.c
> create mode 100644 gcc/testsuite/gcc.dg/countof-vla.c
> create mode 100644 gcc/testsuite/gcc.dg/countof.c
>
> Range-diff against v17:
> 1: d847dc4a795 = 1: 82100c813c3 contrib/: Add support for Cc: and Link: tags
> 2: 936f7945fae ! 2: f8336e4646a c: Add __countof__ operator
> @@ Commit message
> and somehow magically return the number of elements of the array,
> regardless of it being really a pointer.
>
> - - Fix support for [0].
> -
> gcc/ChangeLog:
>
> * doc/extend.texi: Document __countof__ operator.
> @@ gcc/c/c-decl.cc: finish_enum (tree enumtype, tree values, tree
> attributes)
>
> ## gcc/c/c-parser.cc ##
> @@ gcc/c/c-parser.cc: along with GCC; see the file COPYING3. If not see
> - #include "bitmap.h"
> - #include "analyzer/analyzer-language.h"
> #include "toplev.h"
> + #include "asan.h"
> + #include "c-family/c-ubsan.h"
> +
> +#define c_parser_sizeof_expression(parser)
> \
> +(
> \
> @@ gcc/c/c-typeck.cc: c_expr_sizeof_type (location_t loc, struct
> c_type_name *t)
> +static bool
> +is_top_array_vla (tree type)
> +{
> -+ bool zero, star, var;
> ++ bool zero, var;
> + tree d;
> +
> + if (TREE_CODE (type) != ARRAY_TYPE)
> @@ gcc/c/c-typeck.cc: c_expr_sizeof_type (location_t loc, struct
> c_type_name *t)
> +
> + d = TYPE_DOMAIN (type);
> + zero = !TYPE_MAX_VALUE (d);
> -+ star = (zero && C_TYPE_VARIABLE_SIZE (type));
> -+ if (star)
> -+ return true;
> + if (zero)
> + return false;
> +
> @@ gcc/testsuite/gcc.dg/countof-compile.c (new)
> + _Static_assert (__countof__ (int [n][3]) == 7); /* { dg-error "not
> constant" } */
> + _Static_assert (__countof__ (int [0][3]) == 0);
> + _Static_assert (__countof__ (int [0]) == 0);
> -+
> -+ /* FIXME: countof(int [0][n]) should result in a constant expression.
> */
> -+ _Static_assert (__countof__ (int [0][n]) == 0); /* { dg-error "not
> constant" } */
> ++ _Static_assert (__countof__ (int [0][n]) == 0);
> +}
>
> ## gcc/testsuite/gcc.dg/countof-vla.c (new) ##
> @@ gcc/testsuite/gcc.dg/countof-vla.c (new)
> + char (*a)[*][*],
> + int (*x)[__countof__ (*a)]);
> +
> -+// Can't test due to bug:
> <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116284>
> -+//static int z2[0];
> -+//static int y2[__countof__(z2)];
> ++static int z2[0];
> ++static int y2[__countof__(z2)];
>
> ## gcc/testsuite/gcc.dg/countof.c (new) ##
> @@
> @@ gcc/testsuite/gcc.dg/countof.c (new)
> +}
> +
> +void
> -+automatic(void)
> ++completed (void)
> +{
> + int a[] = {1, 2, 3};
> + int z[] = {};
> @@ gcc/testsuite/gcc.dg/countof.c (new)
> +main (void)
> +{
> + array ();
> -+ automatic ();
> ++ completed ();
> + vla ();
> + member ();
> + vla_eval ();
>
> base-commit: 9cbcf8d1de159e6113fafb5dc2feb4a7e467a302