From patchwork Tue Aug 20 18:41:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 1974578 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=oTFbCmcr; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4WpJGc73qLz1yXf for ; Wed, 21 Aug 2024 04:42:00 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 29DD13870863 for ; Tue, 20 Aug 2024 18:41:59 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by sourceware.org (Postfix) with ESMTPS id 2ED423870853 for ; Tue, 20 Aug 2024 18:41:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2ED423870853 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 2ED423870853 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2604:1380:4641:c500::1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724179290; cv=none; b=vjtfXy9dtPmC1TaemS6Svw1C/5uhVp4GQtfXyq1GtbLIbjpHuj4yrAgYnUFnz8/qMd3MVmjqWoNlApFkXf0PypMZSJUPXrkK9OBf+9YxPlhwkV9MQ/uXZn0J5r/prv7WFOJejM9BgMbLQqF4KflRwuCCfnpnW5CZbUs5SuCc9Lc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724179290; c=relaxed/simple; bh=mRVDGsUF1ISgG9IReQTqCKDRShaO6W+SyOiJQya3J/s=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Ie4Qp8EgDl7HIGlcsCsK+yqcnrcO2hg/g7OoVQy07ntOhLVe9PmHUeeAQP8aLhE7lPjZ7uGSZqpRWB7HXfxI2aqpAJyab6Pree15DaZtuNdcCi7r031PhrkSV5DYcYsLl0UJWBeLi0DUrU/dst9NYauzXhdbxG0p5l4Tr8n9kY0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 9D79660EE6; Tue, 20 Aug 2024 18:41:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF711C4AF0B; Tue, 20 Aug 2024 18:41:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724179288; bh=mRVDGsUF1ISgG9IReQTqCKDRShaO6W+SyOiJQya3J/s=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=oTFbCmcr79Dlw9LDnsaqx1hVhVV6UaasDk9Cb7vbeiElkcSf508kaACtpi4DeMd6K wtf9LnHR0JzkevJ6CXRa9yKz6aOs/LmcT4sc5ifT2Lo3okM8nvQJ3FfAqRh556b1BV AZc8iRI2TfdVAznWIqiDCZsu0N6RPmstdV36P97C/uffHy6hM+6ok6TosR/6x9tEl+ Fvs8nr+GZM53T0Ms15bUKYdlSmyDEgUHEwfwvfJO1TCOvz9A5sLXR/UJKAZhRlRNox xnNwrAo0y0CZ5qqUEL8zHLPynbXoe2+cUfwIsVAqAqa3vtwdFSpF8selVDTDO2xSKC RKaiDIYh0O4Hw== Date: Tue, 20 Aug 2024 20:41:21 +0200 From: Alejandro Colomar To: gcc-patches@gcc.gnu.org Cc: Alejandro Colomar , Joseph Myers , Gabriel Ravier , Jakub Jelinek , Kees Cook , Qing Zhao , Jens Gustedt , David Brown , Florian Weimer , Andreas Schwab , Timm Baeder , Daniel Plakosh , "A. Jiang" , Eugene Zelenko , Aaron Ballman , Paul Koning , Daniel Lundin , Nikolaos Strimpas , JeanHeyd Meneide , Fernando Borretti , Jonathan Protzenko , Chris Bazley , Ville Voutilainen , Alex Celeste , Jakub =?utf-8?q?=C5=81ukasiewicz?= , Douglas McIlroy , Jason Merrill , Xavier Del Campo Romero , Martin Uecker Subject: [PATCH v11 1/4] contrib/: Add support for Cc: and Link: tags Message-ID: <2e851b8f8d257085ece4e46c49731d2657e41739.1724178789.git.alx@kernel.org> X-Mailer: git-send-email 2.45.2 References: <20240728141547.302478-1-alx@kernel.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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 contrib/ChangeLog: * gcc-changelog/git_commit.py: (GitCommit): Add support for 'Cc: ' and 'Link: ' tags. Cc: Jason Merrill Signed-off-by: Alejandro Colomar --- contrib/gcc-changelog/git_commit.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/contrib/gcc-changelog/git_commit.py b/contrib/gcc-changelog/git_commit.py index 87ecb9e1a17..64fb986b74c 100755 --- a/contrib/gcc-changelog/git_commit.py +++ b/contrib/gcc-changelog/git_commit.py @@ -182,7 +182,8 @@ CO_AUTHORED_BY_PREFIX = 'co-authored-by: ' REVIEW_PREFIXES = ('reviewed-by: ', 'reviewed-on: ', 'signed-off-by: ', 'acked-by: ', 'tested-by: ', 'reported-by: ', - 'suggested-by: ') + 'suggested-by: ', 'cc: ') +LINK_PREFIXES = ('link: ') DATE_FORMAT = '%Y-%m-%d' @@ -524,6 +525,8 @@ class GitCommit: continue elif lowered_line.startswith(REVIEW_PREFIXES): continue + elif lowered_line.startswith(LINK_PREFIXES): + continue else: m = cherry_pick_regex.search(line) if m: From patchwork Tue Aug 20 18:41:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 1974579 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=o8FoTwF0; 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 4WpJGp1jT2z1yXf for ; Wed, 21 Aug 2024 04:42:10 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0D1BB38708A7 for ; Tue, 20 Aug 2024 18:42:08 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by sourceware.org (Postfix) with ESMTPS id B8F69387084C for ; Tue, 20 Aug 2024 18:41:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B8F69387084C 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 B8F69387084C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=145.40.73.55 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724179304; cv=none; b=ow5jlFnTweOxBFUhoYoNHLf6OT7w7HAUCqKbvrfiZ+fycq84K+a271hu4jrQL+6DhHEfnpzCYr/XbojAf4/dt6ibFh4YIU8ygptxQDwQVRtOKUX4UMOgDrmyKD0QuSm4hdhfSX8AC/Y7VK6ofz4bWTdCgHfaHCEwmvNUBvn285I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724179304; c=relaxed/simple; bh=hWmoLDbfi6es8th5YngiVxK74MmXPn9aRgRnLxLnqCE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=okQaRA7yggVjZvEQeNitrfKwGr+50DtXB9P2ow0RmecoFoeBt9/ClnpUb6nwk7JlB/sNhWY4tsh/ETEXxJQKeUKSsaYDCQOHD7B+K2Bg1//VEFLx2+jITeAh5B8LEhFjGdU0KH/Kz/SHnPhxVT4OGmtWv186tB/jzv3OntWiByc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 7207ACE0B9E; Tue, 20 Aug 2024 18:41:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 340A4C4AF0C; Tue, 20 Aug 2024 18:41:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724179296; bh=hWmoLDbfi6es8th5YngiVxK74MmXPn9aRgRnLxLnqCE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=o8FoTwF0OCVV44eCwbZ15JAXAlL8oEssVaqD20PflZ3s+CFFKezrVncyb60fqpvtv fou26qgBxmmiwRyRkU2dd/dEKPOOcz+jmiPic+rY3k1lNyxuehlnxoA55ijbX3b6/1 RLG8bEYSBpTqfozT4wBjL6EDlYvB/oWExP+GiI4H5G3uPyf9GG6vhla4AYWmfk8tOF CydHi7RUeZGOzhlHLH2x5+yL0ZqfCPW5rPsvmB04717xcbHsiDSPOC5yO3Cl7MRWOQ Th2Y9Lq3b5DrEV2/Kfja+Uq4vjjBWjEY5sc4qQKeqaYaB80XhxkrT/ALVAOSJVQXlL fSlUxXxCDyxUw== Date: Tue, 20 Aug 2024 20:41:29 +0200 From: Alejandro Colomar To: gcc-patches@gcc.gnu.org Cc: Alejandro Colomar , Joseph Myers , Gabriel Ravier , Jakub Jelinek , Kees Cook , Qing Zhao , Jens Gustedt , David Brown , Florian Weimer , Andreas Schwab , Timm Baeder , Daniel Plakosh , "A. Jiang" , Eugene Zelenko , Aaron Ballman , Paul Koning , Daniel Lundin , Nikolaos Strimpas , JeanHeyd Meneide , Fernando Borretti , Jonathan Protzenko , Chris Bazley , Ville Voutilainen , Alex Celeste , Jakub =?utf-8?q?=C5=81ukasiewicz?= , Douglas McIlroy , Jason Merrill , Xavier Del Campo Romero , Martin Uecker , Richard Biener Subject: [PATCH v11 2/4] gcc/: Rename array_type_nelts() => array_type_nelts_minus_one() Message-ID: X-Mailer: git-send-email 2.45.2 References: <20240728141547.302478-1-alx@kernel.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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 The old name was misleading. While at it, also rename some temporary variables that are used with this function, for consistency. Link: gcc/ChangeLog: * tree.cc (array_type_nelts, array_type_nelts_minus_one) * tree.h (array_type_nelts, array_type_nelts_minus_one) * expr.cc (count_type_elements) * config/aarch64/aarch64.cc (pure_scalable_type_info::analyze_array) * config/i386/i386.cc (ix86_canonical_va_list_type): Rename array_type_nelts() => array_type_nelts_minus_one() The old name was misleading. gcc/c/ChangeLog: * c-decl.cc (one_element_array_type_p, get_parm_array_spec) * c-fold.cc (c_fold_array_ref): Rename array_type_nelts() => array_type_nelts_minus_one() gcc/cp/ChangeLog: * decl.cc (reshape_init_array) * init.cc (build_zero_init_1) (build_value_init_noctor) (build_vec_init) (build_delete) * lambda.cc (add_capture) * tree.cc (array_type_nelts_top): Rename array_type_nelts() => array_type_nelts_minus_one() gcc/fortran/ChangeLog: * trans-array.cc (structure_alloc_comps) * trans-openmp.cc (gfc_walk_alloc_comps) (gfc_omp_clause_linear_ctor): Rename array_type_nelts() => array_type_nelts_minus_one() gcc/rust/ChangeLog: * backend/rust-tree.cc (array_type_nelts_top): Rename array_type_nelts() => array_type_nelts_minus_one() Cc: Gabriel Ravier Cc: Martin Uecker Cc: Joseph Myers Cc: Xavier Del Campo Romero Cc: Jakub Jelinek Suggested-by: Richard Biener Signed-off-by: Alejandro Colomar --- gcc/c/c-decl.cc | 10 +++++----- gcc/c/c-fold.cc | 7 ++++--- gcc/config/aarch64/aarch64.cc | 2 +- gcc/config/i386/i386.cc | 2 +- gcc/cp/decl.cc | 2 +- gcc/cp/init.cc | 8 ++++---- gcc/cp/lambda.cc | 3 ++- gcc/cp/tree.cc | 2 +- gcc/expr.cc | 8 ++++---- gcc/fortran/trans-array.cc | 2 +- gcc/fortran/trans-openmp.cc | 4 ++-- gcc/rust/backend/rust-tree.cc | 2 +- gcc/tree.cc | 4 ++-- gcc/tree.h | 2 +- 14 files changed, 30 insertions(+), 28 deletions(-) diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 8cef8f2c289..e7c2783e724 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -5309,7 +5309,7 @@ one_element_array_type_p (const_tree type) { if (TREE_CODE (type) != ARRAY_TYPE) return false; - return integer_zerop (array_type_nelts (type)); + return integer_zerop (array_type_nelts_minus_one (type)); } /* Determine whether TYPE is a zero-length array type "[0]". */ @@ -6257,15 +6257,15 @@ get_parm_array_spec (const struct c_parm *parm, tree attrs) for (tree type = parm->specs->type; TREE_CODE (type) == ARRAY_TYPE; type = TREE_TYPE (type)) { - tree nelts = array_type_nelts (type); - if (error_operand_p (nelts)) + tree nelts_minus_one = array_type_nelts_minus_one (type); + if (error_operand_p (nelts_minus_one)) return attrs; - if (TREE_CODE (nelts) != INTEGER_CST) + if (TREE_CODE (nelts_minus_one) != INTEGER_CST) { /* Each variable VLA bound is represented by the dollar sign. */ spec += "$"; - tpbnds = tree_cons (NULL_TREE, nelts, tpbnds); + tpbnds = tree_cons (NULL_TREE, nelts_minus_one, tpbnds); } } tpbnds = nreverse (tpbnds); diff --git a/gcc/c/c-fold.cc b/gcc/c/c-fold.cc index 57b67c74bd8..9ea174f79c4 100644 --- a/gcc/c/c-fold.cc +++ b/gcc/c/c-fold.cc @@ -73,11 +73,12 @@ c_fold_array_ref (tree type, tree ary, tree index) unsigned elem_nchars = (TYPE_PRECISION (elem_type) / TYPE_PRECISION (char_type_node)); unsigned len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars; - tree nelts = array_type_nelts (TREE_TYPE (ary)); + tree nelts_minus_one = array_type_nelts_minus_one (TREE_TYPE (ary)); bool dummy1 = true, dummy2 = true; - nelts = c_fully_fold_internal (nelts, true, &dummy1, &dummy2, false, false); + nelts_minus_one = c_fully_fold_internal (nelts_minus_one, true, &dummy1, + &dummy2, false, false); unsigned HOST_WIDE_INT i = tree_to_uhwi (index); - if (!tree_int_cst_le (index, nelts) + if (!tree_int_cst_le (index, nelts_minus_one) || i >= len || i + elem_nchars > len) return NULL_TREE; diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc index bfd7bcdef7c..f787347b56e 100644 --- a/gcc/config/aarch64/aarch64.cc +++ b/gcc/config/aarch64/aarch64.cc @@ -1083,7 +1083,7 @@ pure_scalable_type_info::analyze_array (const_tree type) /* An array of unknown, flexible or variable length will be passed and returned by reference whatever we do. */ - tree nelts_minus_one = array_type_nelts (type); + tree nelts_minus_one = array_type_nelts_minus_one (type); if (!tree_fits_uhwi_p (nelts_minus_one)) return DOESNT_MATTER; diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index d06e2141e56..e3ba9f533fe 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -24382,7 +24382,7 @@ ix86_canonical_va_list_type (tree type) return ms_va_list_type_node; if ((TREE_CODE (type) == ARRAY_TYPE - && integer_zerop (array_type_nelts (type))) + && integer_zerop (array_type_nelts_minus_one (type))) || POINTER_TYPE_P (type)) { tree elem_type = TREE_TYPE (type); diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index f23b635aec9..b6a3cfdf8c4 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -6907,7 +6907,7 @@ reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p, gcc_assert (TREE_CODE (type) == ARRAY_TYPE); if (TYPE_DOMAIN (type)) - max_index = array_type_nelts (type); + max_index = array_type_nelts_minus_one (type); return reshape_init_array_1 (TREE_TYPE (type), max_index, d, first_initializer_p, complain); diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index 20373d26988..493e64691cd 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -263,7 +263,7 @@ build_zero_init_1 (tree type, tree nelts, bool static_storage_p, else if (TYPE_DOMAIN (type) == NULL_TREE) return NULL_TREE; else - max_index = array_type_nelts (type); + max_index = array_type_nelts_minus_one (type); /* If we have an error_mark here, we should just return error mark as we don't know the size of the array yet. */ @@ -474,7 +474,7 @@ build_value_init_noctor (tree type, tsubst_flags_t complain) vec *v = NULL; /* Iterate over the array elements, building initializations. */ - tree max_index = array_type_nelts (type); + tree max_index = array_type_nelts_minus_one (type); /* If we have an error_mark here, we should just return error mark as we don't know the size of the array yet. */ @@ -4519,7 +4519,7 @@ build_vec_init (tree base, tree maxindex, tree init, : location_of (base)); if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype)) - maxindex = array_type_nelts (atype); + maxindex = array_type_nelts_minus_one (atype); if (maxindex == NULL_TREE || maxindex == error_mark_node) return error_mark_node; @@ -5178,7 +5178,7 @@ build_delete (location_t loc, tree otype, tree addr, error_at (loc, "unknown array size in delete"); return error_mark_node; } - return build_vec_delete (loc, addr, array_type_nelts (type), + return build_vec_delete (loc, addr, array_type_nelts_minus_one (type), auto_delete, use_global_delete, complain); } diff --git a/gcc/cp/lambda.cc b/gcc/cp/lambda.cc index 0770417810e..065113bc122 100644 --- a/gcc/cp/lambda.cc +++ b/gcc/cp/lambda.cc @@ -556,7 +556,8 @@ add_capture (tree lambda, tree id, tree orig_init, bool by_reference_p, integer_zero_node, tf_warning_or_error); initializer = build_constructor_va (init_list_type_node, 2, NULL_TREE, build_address (elt), - NULL_TREE, array_type_nelts (type)); + NULL_TREE, + array_type_nelts_minus_one (type)); type = vla_capture_type (type); } else if (!dependent_type_p (type) diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index 31ecbb1ac79..040136c70ab 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -3088,7 +3088,7 @@ array_type_nelts_top (tree type) { return fold_build2_loc (input_location, PLUS_EXPR, sizetype, - array_type_nelts (type), + array_type_nelts_minus_one (type), size_one_node); } diff --git a/gcc/expr.cc b/gcc/expr.cc index 2089c2b86a9..cd0fcf15d6d 100644 --- a/gcc/expr.cc +++ b/gcc/expr.cc @@ -6991,14 +6991,14 @@ count_type_elements (const_tree type, bool for_ctor_p) { case ARRAY_TYPE: { - tree nelts; + tree nelts_minus_one; - nelts = array_type_nelts (type); - if (nelts && tree_fits_uhwi_p (nelts)) + nelts_minus_one = array_type_nelts_minus_one (type); + if (nelts_minus_one && tree_fits_uhwi_p (nelts_minus_one)) { unsigned HOST_WIDE_INT n; - n = tree_to_uhwi (nelts) + 1; + n = tree_to_uhwi (nelts_minus_one) + 1; if (n == 0 || for_ctor_p) return n; else diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index ea5fff2e0c2..47e0447cae5 100644 --- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -9728,7 +9728,7 @@ structure_alloc_comps (gfc_symbol * der_type, tree decl, tree dest, else { /* Otherwise use the TYPE_DOMAIN information. */ - tmp = array_type_nelts (decl_type); + tmp = array_type_nelts_minus_one (decl_type); tmp = fold_convert (gfc_array_index_type, tmp); } diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc index df1bf144e23..14cd2f9fad7 100644 --- a/gcc/fortran/trans-openmp.cc +++ b/gcc/fortran/trans-openmp.cc @@ -582,7 +582,7 @@ gfc_walk_alloc_comps (tree decl, tree dest, tree var, tem = size_binop (MINUS_EXPR, tem, size_one_node); } else - tem = array_type_nelts (type); + tem = array_type_nelts_minus_one (type); tem = fold_convert (gfc_array_index_type, tem); } @@ -1309,7 +1309,7 @@ gfc_omp_clause_linear_ctor (tree clause, tree dest, tree src, tree add) nelems = size_binop (MINUS_EXPR, nelems, size_one_node); } else - nelems = array_type_nelts (type); + nelems = array_type_nelts_minus_one (type); nelems = fold_convert (gfc_array_index_type, nelems); gfc_omp_linear_clause_add_loop (&block, dest, src, add, nelems); diff --git a/gcc/rust/backend/rust-tree.cc b/gcc/rust/backend/rust-tree.cc index cdb79095da8..8d32e5203ae 100644 --- a/gcc/rust/backend/rust-tree.cc +++ b/gcc/rust/backend/rust-tree.cc @@ -869,7 +869,7 @@ tree array_type_nelts_top (tree type) { return fold_build2_loc (input_location, PLUS_EXPR, sizetype, - array_type_nelts (type), size_one_node); + array_type_nelts_minus_one (type), size_one_node); } // forked from gcc/cp/tree.cc builtin_valid_in_constant_expr_p diff --git a/gcc/tree.cc b/gcc/tree.cc index 17a5cea7c25..ed0a766016a 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -3698,7 +3698,7 @@ int_byte_position (const_tree field) ARRAY_TYPE) minus one. This counts only elements of the top array. */ tree -array_type_nelts (const_tree type) +array_type_nelts_minus_one (const_tree type) { tree index_type, min, max; @@ -14790,7 +14790,7 @@ is_empty_type (const_tree type) return true; } else if (TREE_CODE (type) == ARRAY_TYPE) - return (integer_minus_onep (array_type_nelts (type)) + return (integer_minus_onep (array_type_nelts_minus_one (type)) || TYPE_DOMAIN (type) == NULL_TREE || is_empty_type (TREE_TYPE (type))); return false; diff --git a/gcc/tree.h b/gcc/tree.h index 5dcbb2fb5dd..69d40bb4f04 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -4921,7 +4921,7 @@ extern tree build_method_type_directly (tree, tree, tree); extern tree build_method_type (tree, tree); extern tree build_offset_type (tree, tree); extern tree build_complex_type (tree, bool named = false); -extern tree array_type_nelts (const_tree); +extern tree array_type_nelts_minus_one (const_tree); extern tree value_member (tree, tree); extern tree purpose_member (const_tree, tree); From patchwork Tue Aug 20 18:41:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 1974581 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=ghIETriS; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4WpJJ35LT0z1yXf for ; Wed, 21 Aug 2024 04:43:15 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F0B5F3870870 for ; Tue, 20 Aug 2024 18:43:13 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by sourceware.org (Postfix) with ESMTPS id 6129C3870859 for ; Tue, 20 Aug 2024 18:41:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6129C3870859 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 6129C3870859 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=139.178.84.217 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724179307; cv=none; b=m9hwlK1MsIdfehEN1if6tVmdD8MBH4gK6BfV61FIpXnjQRNczOxCCL0akbFzEU7BnMxbuF81gLmY8rWiz0N4ZuPKYjfSSj+k6x/dEzkYDY55KTH4Lt93EB+6iCTaZtqC/9kkNYILGL7wnlHsjfqQoad2OyN4h3jBVd53tWUHK+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724179307; c=relaxed/simple; bh=XEneiFO1Sq+e3RdpzoNvx7S3RXF5LORWJ0nrpQ2Istk=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Fv+eETFQsPgcphjwtZguoTjVq7netbVZs/7rwnBo8ZRUhXCoF5qL6o6Eh9XPYU7xC09UpK3hD9uqarYf19bTKBCgVhrhFOsGYfaZNcFw7rtzJcyM7r0cFIxDqbjgyewtG3+p0mgRaIGVMpO1fSacLMvbykqfrHJYu19+F1ybRBw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 00A3260EF0; Tue, 20 Aug 2024 18:41:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 657A3C4AF0E; Tue, 20 Aug 2024 18:41:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724179304; bh=XEneiFO1Sq+e3RdpzoNvx7S3RXF5LORWJ0nrpQ2Istk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ghIETriSWuLIpbFgOlYkOo4aEMRnb1vDrDD/0+ZxlKZaJqpAvt89Y9eAqbgm8Ao4B ESLYqrzfWmhC+wYpz8E9kdDFHZnnggu3n8kqorn6C1Dx4w9ywG8HvpbnRqhTofkiC/ TAKvSOHyTTMRiZCQaUtqedN95W/maJY7t2v7Fzvu8L9cPhrIoiphYP/8tCoPFTWfqn UvuBhz4JugtFz6UoBvaE6fe3ZNYwYUo+W0VmeyWM8+UAyu4pB7eQsV1JAbsddY5E8W FdC+CYDRlIRxSS5gUnN1C65/+KFqQxEOJKOqfqLvQrDCovjztbOmkoB8txmz9uZ7Tn mAGnmyGer9EMw== Date: Tue, 20 Aug 2024 20:41:37 +0200 From: Alejandro Colomar To: gcc-patches@gcc.gnu.org Cc: Alejandro Colomar , Joseph Myers , Gabriel Ravier , Jakub Jelinek , Kees Cook , Qing Zhao , Jens Gustedt , David Brown , Florian Weimer , Andreas Schwab , Timm Baeder , Daniel Plakosh , "A. Jiang" , Eugene Zelenko , Aaron Ballman , Paul Koning , Daniel Lundin , Nikolaos Strimpas , JeanHeyd Meneide , Fernando Borretti , Jonathan Protzenko , Chris Bazley , Ville Voutilainen , Alex Celeste , Jakub =?utf-8?q?=C5=81ukasiewicz?= , Douglas McIlroy , Jason Merrill , Xavier Del Campo Romero , Martin Uecker Subject: [PATCH v11 3/4] Merge definitions of array_type_nelts_top() Message-ID: <34d14beb7da22a8642896ebf7886a04a5096fcd2.1724178789.git.alx@kernel.org> X-Mailer: git-send-email 2.45.2 References: <20240728141547.302478-1-alx@kernel.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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 There were two identical definitions, and none of them are available where they are needed for implementing __nelementsof__. Merge them, and provide the single definition in gcc/tree.{h,cc}, where it's available for __nelementsof__, which will be added in the following commit. gcc/ChangeLog: * tree.h (array_type_nelts_top) * tree.cc (array_type_nelts_top): Define function (moved from gcc/cp/). gcc/cp/ChangeLog: * cp-tree.h (array_type_nelts_top) * tree.cc (array_type_nelts_top): Remove function (move to gcc/). gcc/rust/ChangeLog: * backend/rust-tree.h (array_type_nelts_top) * backend/rust-tree.cc (array_type_nelts_top): Remove function. Signed-off-by: Alejandro Colomar --- gcc/cp/cp-tree.h | 1 - gcc/cp/tree.cc | 13 ------------- gcc/rust/backend/rust-tree.cc | 13 ------------- gcc/rust/backend/rust-tree.h | 2 -- gcc/tree.cc | 13 +++++++++++++ gcc/tree.h | 1 + 6 files changed, 14 insertions(+), 29 deletions(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index a53fbcb43ec..39f23b04a78 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -8103,7 +8103,6 @@ extern tree build_exception_variant (tree, tree); extern void fixup_deferred_exception_variants (tree, tree); extern tree bind_template_template_parm (tree, tree); extern tree array_type_nelts_total (tree); -extern tree array_type_nelts_top (tree); extern bool array_of_unknown_bound_p (const_tree); extern tree break_out_target_exprs (tree, bool = false); extern tree build_ctor_subob_ref (tree, tree, tree); diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index 040136c70ab..7d179491476 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -3079,19 +3079,6 @@ cxx_print_statistics (void) depth_reached); } -/* Return, as an INTEGER_CST node, the number of elements for TYPE - (which is an ARRAY_TYPE). This counts only elements of the top - array. */ - -tree -array_type_nelts_top (tree type) -{ - return fold_build2_loc (input_location, - PLUS_EXPR, sizetype, - array_type_nelts_minus_one (type), - size_one_node); -} - /* Return, as an INTEGER_CST node, the number of elements for TYPE (which is an ARRAY_TYPE). This one is a recursive count of all ARRAY_TYPEs that are clumped together. */ diff --git a/gcc/rust/backend/rust-tree.cc b/gcc/rust/backend/rust-tree.cc index 8d32e5203ae..3dc6b076711 100644 --- a/gcc/rust/backend/rust-tree.cc +++ b/gcc/rust/backend/rust-tree.cc @@ -859,19 +859,6 @@ is_empty_class (tree type) return CLASSTYPE_EMPTY_P (type); } -// forked from gcc/cp/tree.cc array_type_nelts_top - -/* Return, as an INTEGER_CST node, the number of elements for TYPE - (which is an ARRAY_TYPE). This counts only elements of the top - array. */ - -tree -array_type_nelts_top (tree type) -{ - return fold_build2_loc (input_location, PLUS_EXPR, sizetype, - array_type_nelts_minus_one (type), size_one_node); -} - // forked from gcc/cp/tree.cc builtin_valid_in_constant_expr_p /* Test whether DECL is a builtin that may appear in a diff --git a/gcc/rust/backend/rust-tree.h b/gcc/rust/backend/rust-tree.h index 26c8b653ac6..e597c3ab81d 100644 --- a/gcc/rust/backend/rust-tree.h +++ b/gcc/rust/backend/rust-tree.h @@ -2993,8 +2993,6 @@ extern location_t rs_expr_location (const_tree); extern int is_empty_class (tree type); -extern tree array_type_nelts_top (tree); - extern bool is_really_empty_class (tree, bool); diff --git a/gcc/tree.cc b/gcc/tree.cc index ed0a766016a..cedf95cc222 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -3729,6 +3729,19 @@ array_type_nelts_minus_one (const_tree type) ? max : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min)); } + +/* Return, as an INTEGER_CST node, the number of elements for TYPE + (which is an ARRAY_TYPE). This counts only elements of the top + array. */ + +tree +array_type_nelts_top (tree type) +{ + return fold_build2_loc (input_location, + PLUS_EXPR, sizetype, + array_type_nelts_minus_one (type), + size_one_node); +} /* If arg is static -- a reference to an object in static storage -- then return the object. This is not the same as the C meaning of `static'. diff --git a/gcc/tree.h b/gcc/tree.h index 69d40bb4f04..9061dafd027 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -4922,6 +4922,7 @@ extern tree build_method_type (tree, tree); extern tree build_offset_type (tree, tree); extern tree build_complex_type (tree, bool named = false); extern tree array_type_nelts_minus_one (const_tree); +extern tree array_type_nelts_top (tree); extern tree value_member (tree, tree); extern tree purpose_member (const_tree, tree); From patchwork Tue Aug 20 18:41:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 1974580 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=KIk0lafa; 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 4WpJHy3C8qz1yXf for ; Wed, 21 Aug 2024 04:43:10 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2CEFA3870884 for ; Tue, 20 Aug 2024 18:43:08 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by sourceware.org (Postfix) with ESMTPS id A8718386D619 for ; Tue, 20 Aug 2024 18:41:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A8718386D619 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 A8718386D619 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2604:1380:40e1:4800::1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724179321; cv=none; b=g2JN+yVja6wlKZmgjoPvnTmK9FzvYd3LAaJIHL9nDf7PExFvBaZxutNBKjlQcYhGJ4StpDjNrmXSSguADHZA+awTs+ALK+sasjxWlZHcciDyrUgcP+sWBjqOC1FAu48hurM3UkKWcF6UEET0SASy3f93zzMqlqGkQf2ZsPr/FZU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724179321; c=relaxed/simple; bh=rWHZ1rTDUcHm8zJOtublXCFs0Eu9t9vfpZlZDycLxF0=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=noVV5xdzD3CbS9Vbn8aCTTiyt814YCpi/PauNkCKlIPpVoE1cjN7RdroNiOIpLbkqBCS9SZtMyEsS/OmX2h6OjZJUoq3v15yLnaSxbQVRscO8rWajXJ9VcDA/kRbiamLf/PO4m/lAriibCmr2Rh3IZoFvzFPKezrgpT576kciQg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id B805DCE0BA9; Tue, 20 Aug 2024 18:41:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86525C4AF0E; Tue, 20 Aug 2024 18:41:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724179312; bh=rWHZ1rTDUcHm8zJOtublXCFs0Eu9t9vfpZlZDycLxF0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=KIk0lafaSu7Yt2zrlN4HVLDy5w1LeGe7fjVdV+eVLeGqsX6ozN7eE1iXM4ATcT0BH fzjaqDgK+VH07zM3En/S6ePFHbFoV7D1xv+hrexEZVLEzEV2S5Wsa4AFJa4YtKAX22 g4HC0mhEq85aH/1CjnuZpa3RUxBBtViJduLtglvyU313xGiQAftL6iBmnQikztTY4u mI6rmbTUUG3dZ/iXp/YX+eBmrdOhGqqr2aV+AmLntFLvQzJA8w5wCSlzxK77kz9xKZ MlQeaob4/koc6qxxNcUj/K6wh0YNWw6n4jZ+5n7VJJ76YAT6ebuDRWN8cwfFfTSYZB 4eMzTD4QZIdRQ== Date: Tue, 20 Aug 2024 20:41:45 +0200 From: Alejandro Colomar To: gcc-patches@gcc.gnu.org Cc: Alejandro Colomar , Joseph Myers , Gabriel Ravier , Jakub Jelinek , Kees Cook , Qing Zhao , Jens Gustedt , David Brown , Florian Weimer , Andreas Schwab , Timm Baeder , Daniel Plakosh , "A. Jiang" , Eugene Zelenko , Aaron Ballman , Paul Koning , Daniel Lundin , Nikolaos Strimpas , JeanHeyd Meneide , Fernando Borretti , Jonathan Protzenko , Chris Bazley , Ville Voutilainen , Alex Celeste , Jakub =?utf-8?q?=C5=81ukasiewicz?= , Douglas McIlroy , Jason Merrill , Xavier Del Campo Romero , Martin Uecker Subject: [PATCH v11 4/4] c: Add __nelementsof__ operator Message-ID: <49b8d51db4a581a9c2d6337f740e4359d666fb9a.1724178789.git.alx@kernel.org> X-Mailer: git-send-email 2.45.2 References: <20240728141547.302478-1-alx@kernel.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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 This operator is similar to sizeof but can only be applied to an array, and returns its number of elements. FUTURE DIRECTIONS: - We should make it work with array parameters to functions, 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 __nelementsof__ operator. * target.h (enum type_context_kind): Add __nelementsof__ operator. gcc/c-family/ChangeLog: * c-common.h * c-common.def: * c-common.cc (c_nelementsof_type): Add __nelementsof__ operator. gcc/c/ChangeLog: * c-tree.h (c_expr_nelementsof_expr, c_expr_nelementsof_type) * c-decl.cc (start_struct, finish_struct) (start_enum, finish_enum) * c-parser.cc (c_parser_sizeof_expression) (c_parser_nelementsof_expression) (c_parser_sizeof_or_nelementsof_expression) (c_parser_unary_expression) * c-typeck.cc (build_external_ref) (record_maybe_used_decl, pop_maybe_used) (is_top_array_vla) (c_expr_nelementsof_expr, c_expr_nelementsof_type): Add __nelementsof__operator. gcc/cp/ChangeLog: * operators.def: Add __nelementsof__ operator. gcc/testsuite/ChangeLog: * gcc.dg/nelementsof-compile.c * gcc.dg/nelementsof-vla.c * gcc.dg/nelementsof.c: Add tests for __nelementsof__ operator. Link: Link: Link: Cc: Joseph Myers Cc: Gabriel Ravier Cc: Jakub Jelinek Cc: Kees Cook Cc: Qing Zhao Cc: Jens Gustedt Cc: David Brown Cc: Florian Weimer Cc: Andreas Schwab Cc: Timm Baeder Cc: Daniel Plakosh Cc: "A. Jiang" Cc: Eugene Zelenko Cc: Aaron Ballman Cc: Paul Koning Cc: Daniel Lundin Cc: Nikolaos Strimpas Cc: JeanHeyd Meneide Cc: Fernando Borretti Cc: Jonathan Protzenko Cc: Chris Bazley Cc: Ville Voutilainen Cc: Alex Celeste Cc: Jakub Ɓukasiewicz Cc: Douglas McIlroy Cc: Jason Merrill Suggested-by: Xavier Del Campo Romero Co-authored-by: Martin Uecker Signed-off-by: Alejandro Colomar --- 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 | 118 +++++++++++++++- gcc/cp/operators.def | 1 + gcc/doc/extend.texi | 30 +++++ gcc/target.h | 3 + gcc/testsuite/gcc.dg/nelementsof-compile.c | 115 ++++++++++++++++ gcc/testsuite/gcc.dg/nelementsof-vla.c | 46 +++++++ gcc/testsuite/gcc.dg/nelementsof.c | 150 +++++++++++++++++++++ 13 files changed, 558 insertions(+), 24 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/nelementsof-compile.c create mode 100644 gcc/testsuite/gcc.dg/nelementsof-vla.c create mode 100644 gcc/testsuite/gcc.dg/nelementsof.c diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index e7e371fd26f..188bb9b65ef 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -465,6 +465,7 @@ const struct c_common_resword c_common_reswords[] = { "__inline", RID_INLINE, 0 }, { "__inline__", RID_INLINE, 0 }, { "__label__", RID_LABEL, 0 }, + { "__nelementsof__", RID_NELEMENTSOF, 0 }, { "__null", RID_NULL, 0 }, { "__real", RID_REALPART, 0 }, { "__real__", RID_REALPART, 0 }, @@ -4070,6 +4071,31 @@ c_alignof_expr (location_t loc, tree expr) return fold_convert_loc (loc, size_type_node, t); } + +/* Implement the lementsof keyword: + Return the number of elements of an array. */ + +tree +c_nelementsof_type (location_t loc, tree type) +{ + enum tree_code type_code; + + type_code = TREE_CODE (type); + if (type_code != ARRAY_TYPE) + { + error_at (loc, "invalid application of % to type %qT", type); + return error_mark_node; + } + if (!COMPLETE_TYPE_P (type)) + { + error_at (loc, + "invalid application of % to incomplete type %qT", + type); + return error_mark_node; + } + + return array_type_nelts_top (type); +} /* Handle C and C++ default attributes. */ diff --git a/gcc/c-family/c-common.def b/gcc/c-family/c-common.def index 5de96e5d4a8..12ee0d2adb3 100644 --- a/gcc/c-family/c-common.def +++ b/gcc/c-family/c-common.def @@ -50,6 +50,9 @@ DEFTREECODE (EXCESS_PRECISION_EXPR, "excess_precision_expr", tcc_expression, 1) number. */ DEFTREECODE (USERDEF_LITERAL, "userdef_literal", tcc_exceptional, 3) +/* Represents a 'nelementsof' expression. */ +DEFTREECODE (NELEMENTSOF_EXPR, "nelementsof_expr", tcc_expression, 1) + /* Represents a 'sizeof' expression during C++ template expansion, or for the purpose of -Wsizeof-pointer-memaccess warning. */ DEFTREECODE (SIZEOF_EXPR, "sizeof_expr", tcc_expression, 1) diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index 2510ee4dbc9..3b36d8b6513 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -105,6 +105,7 @@ enum rid /* C extensions */ RID_ASM, RID_TYPEOF, RID_TYPEOF_UNQUAL, RID_ALIGNOF, RID_ATTRIBUTE, + RID_NELEMENTSOF, RID_VA_ARG, RID_EXTENSION, RID_IMAGPART, RID_REALPART, RID_LABEL, RID_CHOOSE_EXPR, RID_TYPES_COMPATIBLE_P, RID_BUILTIN_COMPLEX, RID_BUILTIN_SHUFFLE, @@ -885,6 +886,7 @@ extern tree c_common_truthvalue_conversion (location_t, tree); extern void c_apply_type_quals_to_decl (int, tree); extern tree c_sizeof_or_alignof_type (location_t, tree, bool, bool, int); extern tree c_alignof_expr (location_t, tree); +extern tree c_nelementsof_type (location_t, tree); /* Print an error message for invalid operands to arith operation CODE. NOP_EXPR is used as a special case (see truthvalue_conversion). */ extern void binary_op_error (rich_location *, enum tree_code, tree, tree); diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index e7c2783e724..73449292203 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -8943,12 +8943,17 @@ start_struct (location_t loc, enum tree_code code, tree name, within a statement expr used within sizeof, et. al. This is not terribly serious as C++ doesn't permit statement exprs within sizeof anyhow. */ - if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof)) + if (warn_cxx_compat + && (in_sizeof || in_typeof || in_alignof || in_nelementsof)) warning_at (loc, OPT_Wc___compat, "defining type in %qs expression is invalid in C++", (in_sizeof ? "sizeof" - : (in_typeof ? "typeof" : "alignof"))); + : (in_typeof + ? "typeof" + : (in_alignof + ? "alignof" + : "nelementsof")))); if (in_underspecified_init) error_at (loc, "%qT defined in underspecified object initializer", ref); @@ -9908,7 +9913,7 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, struct_types. */ if (warn_cxx_compat && struct_parse_info != NULL - && !in_sizeof && !in_typeof && !in_alignof) + && !in_sizeof && !in_typeof && !in_alignof && !in_nelementsof) struct_parse_info->struct_types.safe_push (t); } @@ -10082,12 +10087,17 @@ start_enum (location_t loc, struct c_enum_contents *the_enum, tree name, /* FIXME: This will issue a warning for a use of a type defined within sizeof in a statement expr. This is not terribly serious as C++ doesn't permit statement exprs within sizeof anyhow. */ - if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof)) + if (warn_cxx_compat + && (in_sizeof || in_typeof || in_alignof || in_nelementsof)) warning_at (loc, OPT_Wc___compat, "defining type in %qs expression is invalid in C++", (in_sizeof ? "sizeof" - : (in_typeof ? "typeof" : "alignof"))); + : (in_typeof + ? "typeof" + : (in_alignof + ? "alignof" + : "nelementsof")))); if (in_underspecified_init) error_at (loc, "%qT defined in underspecified object initializer", @@ -10281,7 +10291,7 @@ finish_enum (tree enumtype, tree values, tree attributes) struct_types. */ if (warn_cxx_compat && struct_parse_info != NULL - && !in_sizeof && !in_typeof && !in_alignof) + && !in_sizeof && !in_typeof && !in_alignof && !in_nelementsof) struct_parse_info->struct_types.safe_push (enumtype); /* Check for consistency with previous definition */ diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index 9b9284b1ba4..d6031d947a8 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -74,7 +74,17 @@ along with GCC; see the file COPYING3. If not see #include "bitmap.h" #include "analyzer/analyzer-language.h" #include "toplev.h" + +#define c_parser_sizeof_expression(parser) \ +( \ + c_parser_sizeof_or_nelementsof_expression (parser, RID_SIZEOF) \ +) +#define c_parser_nelementsof_expression(parser) \ +( \ + c_parser_sizeof_or_nelementsof_expression (parser, RID_NELEMENTSOF) \ +) + /* We need to walk over decls with incomplete struct/union/enum types after parsing the whole translation unit. In finish_decl(), if the decl is static, has incomplete @@ -1694,7 +1704,8 @@ static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *, tree); static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *); static struct c_expr c_parser_unary_expression (c_parser *); -static struct c_expr c_parser_sizeof_expression (c_parser *); +static struct c_expr c_parser_sizeof_or_nelementsof_expression (c_parser *, + enum rid); static struct c_expr c_parser_alignof_expression (c_parser *); static struct c_expr c_parser_postfix_expression (c_parser *); static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *, @@ -9909,6 +9920,8 @@ c_parser_unary_expression (c_parser *parser) case CPP_KEYWORD: switch (c_parser_peek_token (parser)->keyword) { + case RID_NELEMENTSOF: + return c_parser_nelementsof_expression (parser); case RID_SIZEOF: return c_parser_sizeof_expression (parser); case RID_ALIGNOF: @@ -9948,12 +9961,13 @@ c_parser_unary_expression (c_parser *parser) /* Parse a sizeof expression. */ static struct c_expr -c_parser_sizeof_expression (c_parser *parser) +c_parser_sizeof_or_nelementsof_expression (c_parser *parser, enum rid rid) { + const char *op_name = (rid == RID_NELEMENTSOF) ? "nelementsof" : "sizeof"; struct c_expr expr; struct c_expr result; location_t expr_loc; - gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF)); + gcc_assert (c_parser_next_token_is_keyword (parser, rid)); location_t start; location_t finish = UNKNOWN_LOCATION; @@ -9962,7 +9976,10 @@ c_parser_sizeof_expression (c_parser *parser) c_parser_consume_token (parser); c_inhibit_evaluation_warnings++; - in_sizeof++; + if (rid == RID_NELEMENTSOF) + in_nelementsof++; + else + in_sizeof++; if (c_parser_next_token_is (parser, CPP_OPEN_PAREN) && c_token_starts_compound_literal (c_parser_peek_2nd_token (parser))) { @@ -9981,7 +9998,10 @@ c_parser_sizeof_expression (c_parser *parser) { struct c_expr ret; c_inhibit_evaluation_warnings--; - in_sizeof--; + if (rid == RID_NELEMENTSOF) + in_nelementsof--; + else + in_sizeof--; ret.set_error (); ret.original_code = ERROR_MARK; ret.original_type = NULL; @@ -9993,31 +10013,45 @@ c_parser_sizeof_expression (c_parser *parser) type_name, expr_loc); finish = expr.get_finish (); - goto sizeof_expr; + goto Xof_expr; } /* sizeof ( type-name ). */ if (scspecs) - error_at (expr_loc, "storage class specifier in %"); + error_at (expr_loc, "storage class specifier in %qs", op_name); if (type_name->specs->alignas_p) error_at (type_name->specs->locations[cdw_alignas], - "alignment specified for type name in %"); + "alignment specified for type name in %qs", op_name); c_inhibit_evaluation_warnings--; - in_sizeof--; - result = c_expr_sizeof_type (expr_loc, type_name); + if (rid == RID_NELEMENTSOF) + { + in_nelementsof--; + result = c_expr_nelementsof_type (expr_loc, type_name); + } + else + { + in_sizeof--; + result = c_expr_sizeof_type (expr_loc, type_name); + } } else { expr_loc = c_parser_peek_token (parser)->location; expr = c_parser_unary_expression (parser); finish = expr.get_finish (); - sizeof_expr: + Xof_expr: c_inhibit_evaluation_warnings--; - in_sizeof--; + if (rid == RID_NELEMENTSOF) + in_nelementsof--; + else + in_sizeof--; mark_exp_read (expr.value); if (TREE_CODE (expr.value) == COMPONENT_REF && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1))) - error_at (expr_loc, "% applied to a bit-field"); - result = c_expr_sizeof_expr (expr_loc, expr); + error_at (expr_loc, "%qs applied to a bit-field", op_name); + if (rid == RID_NELEMENTSOF) + result = c_expr_nelementsof_expr (expr_loc, expr); + else + result = c_expr_sizeof_expr (expr_loc, expr); } if (finish == UNKNOWN_LOCATION) finish = start; diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index 3dc6338bf06..a0149ccae77 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -736,6 +736,7 @@ extern int c_type_dwarf_attribute (const_tree, int); /* in c-typeck.cc */ extern int in_alignof; extern int in_sizeof; +extern int in_nelementsof; extern int in_typeof; extern bool c_in_omp_for; extern bool c_omp_array_section_p; @@ -786,6 +787,9 @@ extern tree build_external_ref (location_t, tree, bool, tree *); extern void pop_maybe_used (bool); extern struct c_expr c_expr_sizeof_expr (location_t, struct c_expr); extern struct c_expr c_expr_sizeof_type (location_t, struct c_type_name *); +extern struct c_expr c_expr_nelementsof_expr (location_t, struct c_expr); +extern struct c_expr c_expr_nelementsof_type (location_t loc, + struct c_type_name *); extern struct c_expr parser_build_unary_op (location_t, enum tree_code, struct c_expr); extern struct c_expr parser_build_binary_op (location_t, diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 094e41fa202..651e5c15b87 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -71,6 +71,9 @@ int in_alignof; /* The level of nesting inside "sizeof". */ int in_sizeof; +/* The level of nesting inside "nelementsof". */ +int in_nelementsof; + /* The level of nesting inside "typeof". */ int in_typeof; @@ -3255,7 +3258,7 @@ build_external_ref (location_t loc, tree id, bool fun, tree *type) if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof) { - if (!in_sizeof && !in_typeof) + if (!in_sizeof && !in_typeof && !in_nelementsof) C_DECL_USED (ref) = 1; else if (DECL_INITIAL (ref) == NULL_TREE && DECL_EXTERNAL (ref) @@ -3311,7 +3314,7 @@ struct maybe_used_decl { /* The decl. */ tree decl; - /* The level seen at (in_sizeof + in_typeof). */ + /* The level seen at (in_sizeof + in_typeof + in_nelementsof). */ int level; /* The next one at this level or above, or NULL. */ struct maybe_used_decl *next; @@ -3329,7 +3332,7 @@ record_maybe_used_decl (tree decl) { struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl); t->decl = decl; - t->level = in_sizeof + in_typeof; + t->level = in_sizeof + in_typeof + in_nelementsof; t->next = maybe_used_decls; maybe_used_decls = t; } @@ -3343,7 +3346,7 @@ void pop_maybe_used (bool used) { struct maybe_used_decl *p = maybe_used_decls; - int cur_level = in_sizeof + in_typeof; + int cur_level = in_sizeof + in_typeof + in_nelementsof; while (p && p->level > cur_level) { if (used) @@ -3453,6 +3456,113 @@ c_expr_sizeof_type (location_t loc, struct c_type_name *t) return ret; } +static bool +is_top_array_vla (tree type) +{ + bool zero, star, var; + tree d; + + if (TREE_CODE (type) != ARRAY_TYPE) + return false; + if (!COMPLETE_TYPE_P (type)) + return false; + + d = TYPE_DOMAIN (type); + zero = !TYPE_MAX_VALUE (d); + star = (zero && C_TYPE_VARIABLE_SIZE (type)); + if (star) + return true; + if (zero) + return false; + + var = (TREE_CODE (TYPE_MIN_VALUE (d)) != INTEGER_CST + || TREE_CODE (TYPE_MAX_VALUE (d)) != INTEGER_CST); + return var; +} + +/* Return the result of nelementsof applied to EXPR. */ + +struct c_expr +c_expr_nelementsof_expr (location_t loc, struct c_expr expr) +{ + struct c_expr ret; + if (expr.value == error_mark_node) + { + ret.value = error_mark_node; + ret.original_code = ERROR_MARK; + ret.original_type = NULL; + ret.m_decimal = 0; + pop_maybe_used (false); + } + else + { + bool expr_const_operands = true; + + tree folded_expr = c_fully_fold (expr.value, require_constant_value, + &expr_const_operands); + ret.value = c_nelementsof_type (loc, TREE_TYPE (folded_expr)); + c_last_sizeof_arg = expr.value; + c_last_sizeof_loc = loc; + ret.original_code = NELEMENTSOF_EXPR; + ret.original_type = NULL; + ret.m_decimal = 0; + if (is_top_array_vla (TREE_TYPE (folded_expr))) + { + /* nelementsof is evaluated when given a vla. */ + ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value), + folded_expr, ret.value); + 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))); + } + return ret; +} + +/* Return the result of nelementsof applied to T, a structure for the type + name passed to nelementsof (rather than the type itself). LOC is the + location of the original expression. */ + +struct c_expr +c_expr_nelementsof_type (location_t loc, struct c_type_name *t) +{ + tree type; + struct c_expr ret; + tree type_expr = NULL_TREE; + bool type_expr_const = true; + type = groktypename (t, &type_expr, &type_expr_const); + ret.value = c_nelementsof_type (loc, type); + c_last_sizeof_arg = type; + c_last_sizeof_loc = loc; + ret.original_code = NELEMENTSOF_EXPR; + ret.original_type = NULL; + ret.m_decimal = 0; + if (type == error_mark_node) + { + ret.value = error_mark_node; + ret.original_code = ERROR_MARK; + } + else + if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST) + && is_top_array_vla (type)) + { + /* 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 nelementsof 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 nelementsof 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), + type_expr, ret.value); + C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const; + } + pop_maybe_used (type != error_mark_node ? is_top_array_vla (type) : false); + return ret; +} + /* Build a function call to function FUNCTION with parameters PARAMS. The function call is at LOC. PARAMS is a list--a chain of TREE_LIST nodes--in which the diff --git a/gcc/cp/operators.def b/gcc/cp/operators.def index d8878923602..7f1d24779c8 100644 --- a/gcc/cp/operators.def +++ b/gcc/cp/operators.def @@ -91,6 +91,7 @@ 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 ("__nelementsof__", NELEMENTSOF_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) diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 0ea7a87053c..85bcb059730 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -10481,6 +10481,36 @@ If the operand of the @code{__alignof__} expression is a function, the expression evaluates to the alignment of the function which may be specified by attribute @code{aligned} (@pxref{Common Function Attributes}). +@node nelementsof +@section Determining the Number of Elements of Arrays +@cindex nelementsof +@cindex number of elements + +The keyword @code{__elemetsf__} determines the length of an array operand, +that is, the number of elements in the array. +Its syntax is similar to @code{sizeof}. +The operand must be +a parenthesized complete array type name +or an expression of such a type. +For example: + +@smallexample +int a[n]; +__elemetsf__ (a); // returns n +__elemetsf__ (int [7][3]); // returns 7 +@end smallexample + +The result of this operator is an integer constant expression, +unless the top-level array is a variable-length array. +The operand is only evaluated +if the top-level array is a variable-length array. +For example: + +@smallexample +__elemetsf__ (int [7][n++]); // integer constant expression +__elemetsf__ (int [n++][7]); // run-time value; n++ is evaluated +@end smallexample + @node Inline @section An Inline Function is As Fast As a Macro @cindex inline functions diff --git a/gcc/target.h b/gcc/target.h index 837651d273a..09245d70c1f 100644 --- a/gcc/target.h +++ b/gcc/target.h @@ -245,6 +245,9 @@ enum type_context_kind { /* Directly measuring the alignment of T. */ TCTX_ALIGNOF, + /* Directly measuring the number of elements of array T. */ + TCTX_NELEMENTSOF, + /* Creating objects of type T with static storage duration. */ TCTX_STATIC_STORAGE, diff --git a/gcc/testsuite/gcc.dg/nelementsof-compile.c b/gcc/testsuite/gcc.dg/nelementsof-compile.c new file mode 100644 index 00000000000..211b63fa7b5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/nelementsof-compile.c @@ -0,0 +1,115 @@ +/* { dg-do compile } */ +/* { dg-options "-Wno-declaration-after-statement -Wno-pedantic -Wno-vla" } */ + +extern int x[]; + +static int w[] = {1, 2, 3}; + +static int z[0]; +static int y[__nelementsof__(z)]; + +void +automatic(void) +{ + __nelementsof__ (w); +} + +void +incomplete (int p[]) +{ + __nelementsof__ (x); /* { dg-error "incomplete" } */ + + /* We want to support the following one in the future, + but for now it should fail. */ + __nelementsof__ (p); /* { dg-error "invalid" } */ +} + +void +fam (void) +{ + struct { + int x; + int fam[]; + } s; + + __nelementsof__ (s.fam); /* { dg-error "incomplete" } */ +} + +void fix_fix (int i, char (*a)[3][5], int (*x)[__nelementsof__ (*a)]); +void fix_var (int i, char (*a)[3][i], int (*x)[__nelementsof__ (*a)]); +void fix_uns (int i, char (*a)[3][*], int (*x)[__nelementsof__ (*a)]); + +void +func (void) +{ + int i3[3]; + int i5[5]; + char c35[3][5]; + + fix_fix (5, &c35, &i3); + fix_fix (5, &c35, &i5); /* { dg-error "incompatible-pointer-types" } */ + + fix_var (5, &c35, &i3); + fix_var (5, &c35, &i5); /* { dg-error "incompatible-pointer-types" } */ + + fix_uns (5, &c35, &i3); + fix_uns (5, &c35, &i5); /* { dg-error "incompatible-pointer-types" } */ +} + +void +non_arr(void) +{ + int x; + int *p; + struct s { + int x[3]; + } s; + + __nelementsof__ (x); /* { dg-error "invalid" } */ + __nelementsof__ (int); /* { dg-error "invalid" } */ + __nelementsof__ (s); /* { dg-error "invalid" } */ + __nelementsof__ (struct s); /* { dg-error "invalid" } */ + __nelementsof__ (&x); /* { dg-error "invalid" } */ + __nelementsof__ (p); /* { dg-error "invalid" } */ + __nelementsof__ (int *); /* { dg-error "invalid" } */ + __nelementsof__ (&s.x); /* { dg-error "invalid" } */ + __nelementsof__ (int (*)[3]); /* { dg-error "invalid" } */ +} + +static int f1(); +static int f2(); /* { dg-warning "never defined" } */ +int a[10][10]; +int n; + +void +syms(void) +{ + int b[n][n]; + + __nelementsof__ (a[f1()]); + __nelementsof__ (b[f2()]); +} + +void +no_parens(void) +{ + __nelementsof__ a; + __nelementsof__ *a; + __nelementsof__ (int [3]) {}; + + __nelementsof__ int [3]; /* { dg-error "expected expression before" } */ +} + +void +const_expr(void) +{ + int n = 7; + + _Static_assert (__nelementsof__ (int [3][n]) == 3); + _Static_assert (__nelementsof__ (int [n][3]) == 7); /* { dg-error "not constant" } */ + _Static_assert (__nelementsof__ (int [0][3]) == 0); + _Static_assert (__nelementsof__ (int [0]) == 0); + + /* FIXME: nelementsof(int [0][n]) should result in a constant expression. */ + _Static_assert (__nelementsof__ (int [0][n]) == 0); /* { dg-error "not constant" } */ +} diff --git a/gcc/testsuite/gcc.dg/nelementsof-vla.c b/gcc/testsuite/gcc.dg/nelementsof-vla.c new file mode 100644 index 00000000000..0cd4dfd37fa --- /dev/null +++ b/gcc/testsuite/gcc.dg/nelementsof-vla.c @@ -0,0 +1,46 @@ +/* { dg-do compile } */ +/* { dg-options "-Wno-pedantic -Wvla-parameter" } */ + +void fix_fix (int i, + char (*a)[3][5], + int (*x)[__nelementsof__ (*a)]); +void fix_var (int i, + char (*a)[3][i], /* dg-warn "variable" */ + int (*x)[__nelementsof__ (*a)]); +void fix_uns (int i, + char (*a)[3][*], + int (*x)[__nelementsof__ (*a)]); + +void zro_fix (int i, + char (*a)[0][5], + int (*x)[__nelementsof__ (*a)]); +void zro_var (int i, + char (*a)[0][i], /* dg-warn "variable" */ + int (*x)[__nelementsof__ (*a)]); +void zro_uns (int i, + char (*a)[0][*], + int (*x)[__nelementsof__ (*a)]); + +void var_fix (int i, + char (*a)[i][5], /* dg-warn "variable" */ + int (*x)[__nelementsof__ (*a)]); /* dg-warn "variable" */ +void var_var (int i, + char (*a)[i][i], /* dg-warn "variable" */ + int (*x)[__nelementsof__ (*a)]); /* dg-warn "variable" */ +void var_uns (int i, + char (*a)[i][*], /* dg-warn "variable" */ + int (*x)[__nelementsof__ (*a)]); /* dg-warn "variable" */ + +void uns_fix (int i, + char (*a)[*][5], + int (*x)[__nelementsof__ (*a)]); +void uns_var (int i, + char (*a)[*][i], /* dg-warn "variable" */ + int (*x)[__nelementsof__ (*a)]); +void uns_uns (int i, + char (*a)[*][*], + int (*x)[__nelementsof__ (*a)]); + +// Can't test due to bug: +//static int z2[0]; +//static int y2[__nelementsof__(z2)]; diff --git a/gcc/testsuite/gcc.dg/nelementsof.c b/gcc/testsuite/gcc.dg/nelementsof.c new file mode 100644 index 00000000000..292311e2822 --- /dev/null +++ b/gcc/testsuite/gcc.dg/nelementsof.c @@ -0,0 +1,150 @@ +/* { dg-do run } */ +/* { dg-options "-Wno-declaration-after-statement -Wno-pedantic -Wno-vla" } */ + +#undef NDEBUG +#include + +void +array (void) +{ + short a[7]; + + static_assert (__nelementsof__ (a) == 7); + static_assert (__nelementsof__ (long [0]) == 0); + static_assert (__nelementsof__ (unsigned [99]) == 99); +} + +void +automatic(void) +{ + int a[] = {1, 2, 3}; + int z[] = {}; + + static_assert (__nelementsof__ (a) == 3); + static_assert (__nelementsof__ (z) == 0); +} + +void +vla (void) +{ + unsigned n; + + n = 99; + assert (__nelementsof__ (short [n - 10]) == 99 - 10); + + int v[n / 2]; + assert (__nelementsof__ (v) == 99 / 2); + + n = 0; + int z[n]; + assert (__nelementsof__ (z) == 0); +} + +void +member (void) +{ + struct { + int a[8]; + } s; + + static_assert (__nelementsof__ (s.a) == 8); +} + +void +vla_eval (void) +{ + int i; + + i = 7; + assert (__nelementsof__ (struct {int x;}[i++]) == 7); + assert (i == 7 + 1); + + int v[i]; + int (*p)[i]; + p = &v; + assert (__nelementsof__ (*p++) == i); + assert (p - 1 == &v); +} + +void +inner_vla_noeval (void) +{ + int i; + + i = 3; + static_assert (__nelementsof__ (struct {int x[i++];}[3]) == 3); + assert (i == 3); +} + +void +array_noeval (void) +{ + long a[5]; + long (*p)[__nelementsof__ (a)]; + + p = &a; + static_assert (__nelementsof__ (*p++) == 5); + assert (p == &a); +} + +void +matrix_zero (void) +{ + int i; + + static_assert (__nelementsof__ (int [0][4]) == 0); + i = 3; + assert (__nelementsof__ (int [0][i]) == 0); +} + +void +matrix_fixed (void) +{ + int i; + + static_assert (__nelementsof__ (int [7][4]) == 7); + i = 3; + static_assert (__nelementsof__ (int [7][i]) == 7); +} + +void +matrix_vla (void) +{ + int i, j; + + i = 7; + assert (__nelementsof__ (int [i++][4]) == 7); + assert (i == 7 + 1); + + i = 9; + j = 3; + assert (__nelementsof__ (int [i++][j]) == 9); + assert (i == 9 + 1); +} + +void +no_parens(void) +{ + int n = 3; + int a[7]; + int v[n]; + + static_assert (__nelementsof__ a == 7); + assert (__nelementsof__ v == 3); +} + +int +main (void) +{ + array (); + automatic (); + vla (); + member (); + vla_eval (); + inner_vla_noeval (); + array_noeval (); + matrix_zero (); + matrix_fixed (); + matrix_vla (); + no_parens (); +}