From patchwork Wed Oct 16 10:13:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 1997950 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=JkG2x6g/; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XT6Jd3TM4z1xvV for ; Wed, 16 Oct 2024 21:14:24 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3CA8D385841C for ; Wed, 16 Oct 2024 10:14:21 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by sourceware.org (Postfix) with ESMTPS id 8ACBE3858D28 for ; Wed, 16 Oct 2024 10:13:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8ACBE3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=kernel.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kernel.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8ACBE3858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=147.75.193.91 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729073624; cv=none; b=uv2evfUKJ2nqTmF+0mUNb4sMbxOlxjeSYcPE8HynxC2nOEyQW3kaR5CZg/pLVVLb1U3QR14aBxkkdpERFdVfncV1LzYVVTyLp+qsytRoOZdl0eOwnwSFvhqZ8qJZRk5RGbvhT0cAoOiShxd3c5wTtHnIKsA0tIMan3YNpvZAfFs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729073624; c=relaxed/simple; bh=/BU8Ux2oikL1X5vAnv1jiR8TKw9Pf7gPuaCu+tiknMw=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=oI26+s03ySlu1sEyTKtMN5hEK3qltmsMtLea9fL0GcQqlVxAqpHCygZaPi+uawBXmYm0IrspQbyNlkBpOoRi/Qj+GS2AqUDmFwZn1o4lC8mV0dTYfBskkOJ5g3lOYPfNIPm2OIn/bNV8rZcD8pN9HMxD37TCrw5inxmJqk36O/I= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id D41DBA43C40; Wed, 16 Oct 2024 10:13:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D3184C4CECD; Wed, 16 Oct 2024 10:13:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729073620; bh=/BU8Ux2oikL1X5vAnv1jiR8TKw9Pf7gPuaCu+tiknMw=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=JkG2x6g/Vx2wgVFVzmxhezTWPq7wcAOitsYjDK1z3ch+eGYGZL2Kry6nqWGOjl/TB NARcQc7zkFb1jZJku1s1RO6XXki1f2/HKLvKQbiLk+t6UGEM6hELiRT8CITvy9j6yW 8ivQvGW727m/2W74kshkg6MfOdj7mh9LP2hTDBv3JDtk9EAFy7yYH/26dlO7mJiu+g PRdMrLBJGRjnC+kMmyMkqoJJJxVppz7KJPb3EdWgKQhA+cUbXxjXs/vck+UVn7eZAf 5xw0qKO6SGTWIeq3+8CEwI9jaREo0huYdIZF+kNXnVzNhJqy2eJr6UjzOzRfQ35IRh J8HO9DvpPmtdA== Date: Wed, 16 Oct 2024 12:13:38 +0200 From: Alejandro Colomar To: gcc-patches@gcc.gnu.org Cc: Richard Biener Subject: [PATCH v15 0/4] c: Add __countof__ operator Message-ID: X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240728141547.302478-1-alx@kernel.org> X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org v15 changes: - Rebase. - Remove (unused) changes under and . 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: + Link: + Link: Link: Link: Link: - Link: Link: + Link: + Suggested-by: Xavier Del Campo Romero + Co-authored-by: Martin Uecker + Acked-by: "James K. Lowden" Cc: Joseph Myers Cc: Gabriel Ravier Cc: Jakub Jelinek @@ Commit message Cc: Jakub Ɓukasiewicz Cc: Douglas McIlroy Cc: Jason Merrill - Suggested-by: Xavier Del Campo Romero - Co-authored-by: Martin Uecker + Cc: "Gustavo A. R. Silva" + Cc: Patrizia Kaye + Cc: Ori Bernstein + Cc: Robert Seacord + Cc: Marek Polacek + Cc: Sam James Signed-off-by: Alejandro Colomar ## 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