mbox series

[v15,0/4] c: Add __countof__ operator

Message ID cover.1729073399.git.alx@kernel.org
Headers show
Series c: Add __countof__ operator | expand

Message

Alejandro Colomar Oct. 16, 2024, 10:13 a.m. UTC
v15 changes:

-  Rebase.

-  Remove (unused) changes under <gcc/cp/*> and <gcc/target.h>.  It was
   dead code.

-  Fix typos in comments.

-  Make format of changelog more consistent.

-  Add some links and CCs to the commit message.

-  Add Acked-by James K. Lowden.  Quoting him:

	> Just to say, [...], you're absolutely right.  "Size" has a meaning.
	> "Count" has a meaning.  They're equal only if the unit size is 1.
	>
	> "Length" is ambiguous.  Often, it means "size within capacity", as
	> strlen(3).  I cannot think of a single example in C++ where "length"
	> means "number of elements allocated".  The STL uses size to mean count,
	> including std::size, afaik.
	>
	> It is said that in 1956, someone once told Adlai Stevenson,
	> the Democratic presidential candidate, ?Every thinking person in America
	> will vote for you.? Stevenson supposed replied, "That's not enough.
	> I need a majority.?
	>
	> 'Twas always thus.

	> I would go with __countof__().  It's short and unambiguous.

-  Remove some remanent uses of length in documentation and comments.

Alejandro Colomar (4):
  contrib/: Add support for Cc: and Link: tags
  gcc/: Rename array_type_nelts() => array_type_nelts_minus_one()
  Merge definitions of array_type_nelts_top()
  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                        |  32 ++++--
 gcc/c/c-fold.cc                        |   7 +-
 gcc/c/c-parser.cc                      |  62 +++++++---
 gcc/c/c-tree.h                         |   4 +
 gcc/c/c-typeck.cc                      | 118 ++++++++++++++++++-
 gcc/config/aarch64/aarch64.cc          |   2 +-
 gcc/config/i386/i386.cc                |   2 +-
 gcc/cp/cp-tree.h                       |   1 -
 gcc/cp/decl.cc                         |   2 +-
 gcc/cp/init.cc                         |   8 +-
 gcc/cp/lambda.cc                       |   3 +-
 gcc/cp/tree.cc                         |  13 ---
 gcc/doc/extend.texi                    |  30 +++++
 gcc/expr.cc                            |   8 +-
 gcc/fortran/trans-array.cc             |   2 +-
 gcc/fortran/trans-openmp.cc            |   4 +-
 gcc/rust/backend/rust-tree.cc          |  13 ---
 gcc/rust/backend/rust-tree.h           |   2 -
 gcc/testsuite/gcc.dg/countof-compile.c | 115 +++++++++++++++++++
 gcc/testsuite/gcc.dg/countof-vla.c     |  46 ++++++++
 gcc/testsuite/gcc.dg/countof.c         | 150 +++++++++++++++++++++++++
 gcc/tree.cc                            |  17 ++-
 gcc/tree.h                             |   3 +-
 27 files changed, 600 insertions(+), 80 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 v14:
1:  d7fca49888a = 1:  b6da2185675 contrib/: Add support for Cc: and Link: tags
2:  e65245ac294 = 2:  a0fa3f139f9 gcc/: Rename array_type_nelts() => array_type_nelts_minus_one()
3:  03de2d67bb1 = 3:  43a2e18c6a2 Merge definitions of array_type_nelts_top()
4:  6714852dd93 ! 4:  8a6959d2d38 c: Add __countof__ operator
    @@ Commit message
         gcc/ChangeLog:
     
                 * doc/extend.texi: Document __countof__ operator.
    -            * target.h (enum type_context_kind): Add __countof__ operator.
     
         gcc/c-family/ChangeLog:
     
                 * c-common.h
    -            * c-common.def:
    +            * c-common.def
                 * c-common.cc (c_countof_type): Add __countof__ operator.
     
         gcc/c/ChangeLog:
    @@ Commit message
                 (c_parser_unary_expression)
                 * c-typeck.cc
                 (build_external_ref)
    -            (record_maybe_used_decl, pop_maybe_used)
    +            (record_maybe_used_decl)
    +            (pop_maybe_used)
                 (is_top_array_vla)
                 (c_expr_countof_expr, c_expr_countof_type):
                 Add __countof__operator.
     
    -    gcc/cp/ChangeLog:
    -
    -            * operators.def: Add __countof__ operator.
    -
         gcc/testsuite/ChangeLog:
     
                 * gcc.dg/countof-compile.c
                 * gcc.dg/countof-vla.c
                 * gcc.dg/countof.c: Add tests for __countof__ operator.
     
    +    Link: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117025>
    +    Link: <https://inbox.sourceware.org/gcc/M8S4oQy--3-2@tutanota.com/T/>
    +    Link: <https://inbox.sourceware.org/gcc-patches/20240728141547.302478-1-alx@kernel.org/T/#t>
         Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3313.pdf>
         Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3325.pdf>
         Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3369.pdf>
    -    Link: <https://inbox.sourceware.org/gcc/M8S4oQy--3-2@tutanota.com/T/>
         Link: <https://github.com/llvm/llvm-project/issues/102836>
    +    Link: <https://stackoverflow.com/questions/37538/#57537491>
    +    Suggested-by: Xavier Del Campo Romero <xavi.dcr@tutanota.com>
    +    Co-authored-by: Martin Uecker <uecker@tugraz.at>
    +    Acked-by: "James K. Lowden" <jklowden@schemamania.org>
         Cc: Joseph Myers <josmyers@redhat.com>
         Cc: Gabriel Ravier <gabravier@gmail.com>
         Cc: Jakub Jelinek <jakub@redhat.com>
    @@ Commit message
         Cc: Jakub Ɓukasiewicz <jakublukasiewicz@outlook.com>
         Cc: Douglas McIlroy <douglas.mcilroy@dartmouth.edu>
         Cc: Jason Merrill <jason@redhat.com>
    -    Suggested-by: Xavier Del Campo Romero <xavi.dcr@tutanota.com>
    -    Co-authored-by: Martin Uecker <uecker@tugraz.at>
    +    Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
    +    Cc: Patrizia Kaye <patrizia@ethernull.org>
    +    Cc: Ori Bernstein <ori@eigenstate.org>
    +    Cc: Robert Seacord <rcseacord@gmail.com>
    +    Cc: Marek Polacek <mpolacek@gcc.gnu.org>
    +    Cc: Sam James <sam@gentoo.org>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
      ## gcc/c-family/c-common.cc ##
    @@ gcc/c-family/c-common.cc: c_alignof_expr (location_t loc, tree expr)
        return fold_convert_loc (loc, size_type_node, t);
      }
     +
    -+/* Implement the lementsof keyword:
    ++/* Implement the countof keyword:
     +   Return the number of elements of an array.  */
     +
     +tree
    @@ gcc/c/c-typeck.cc: c_expr_sizeof_type (location_t loc, struct c_type_name *t)
     +      /* If the type is a [*] array, it is a VLA but is represented as
     +	 having a size of zero.  In such a case we must ensure that
     +	 the result of countof does not get folded to a constant by
    -+	 c_fully_fold, because if the length is evaluated the result is
    -+	 not constant and so constraints on zero or negative size
    -+	 arrays must not be applied when this countof call is inside
    -+	 another array declarator.  */
    ++	 c_fully_fold, because if the number of elements is evaluated
    ++	 the result is not constant and so
    ++	 constraints on zero or negative size arrays must not be applied
    ++	 when this countof call is inside another array declarator.  */
     +      if (!type_expr)
     +	type_expr = integer_zero_node;
     +      ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
    @@ gcc/c/c-typeck.cc: c_expr_sizeof_type (location_t loc, struct c_type_name *t)
         The function call is at LOC.
         PARAMS is a list--a chain of TREE_LIST nodes--in which the
     
    - ## gcc/cp/operators.def ##
    -@@ gcc/cp/operators.def: DEF_OPERATOR ("co_await", CO_AWAIT_EXPR, "aw", OVL_OP_FLAG_UNARY)
    - 
    - /* These are extensions.  */
    - DEF_OPERATOR ("alignof", ALIGNOF_EXPR, "az", OVL_OP_FLAG_UNARY)
    -+DEF_OPERATOR ("__countof__", COUNTOF_EXPR, "lz", OVL_OP_FLAG_UNARY)
    - DEF_OPERATOR ("__imag__", IMAGPART_EXPR, "v18__imag__", OVL_OP_FLAG_UNARY)
    - DEF_OPERATOR ("__real__", REALPART_EXPR, "v18__real__", OVL_OP_FLAG_UNARY)
    - 
    -
      ## gcc/doc/extend.texi ##
     @@ gcc/doc/extend.texi: If the operand of the @code{__alignof__} expression is a function,
      the expression evaluates to the alignment of the function which may
    @@ gcc/doc/extend.texi: If the operand of the @code{__alignof__} expression is a fu
     +@cindex countof
     +@cindex number of elements
     +
    -+The keyword @code{__countof__} determines the length of an array operand,
    -+that is, the number of elements in the array.
    ++The keyword @code{__countof__} determines
    ++the number of elements of an array operand.
     +Its syntax is similar to @code{sizeof}.
     +The operand must be
     +a parenthesized complete array type name
    @@ gcc/doc/extend.texi: If the operand of the @code{__alignof__} expression is a fu
      @section An Inline Function is As Fast As a Macro
      @cindex inline functions
     
    - ## gcc/target.h ##
    -@@ gcc/target.h: enum type_context_kind {
    -   /* Directly measuring the alignment of T.  */
    -   TCTX_ALIGNOF,
    - 
    -+  /* Directly measuring the number of elements of array T.  */
    -+  TCTX_COUNTOF,
    -+
    -   /* Creating objects of type T with static storage duration.  */
    -   TCTX_STATIC_STORAGE,
    - 
    -
      ## gcc/testsuite/gcc.dg/countof-compile.c (new) ##
     @@
     +/* { dg-do compile } */

base-commit: 9cbcf8d1de159e6113fafb5dc2feb4a7e467a302