From patchwork Thu Feb 11 08:50:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1439349 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=iWjOt6J8; dkim-atps=neutral Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Dbr2V0HLrz9sB4 for ; Thu, 11 Feb 2021 19:51:13 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6AC95385803F; Thu, 11 Feb 2021 08:51:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6AC95385803F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1613033468; bh=bEOLvz9wokeTSP48knaQJ7dEBSfNvJGke5wgKBufvfA=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=iWjOt6J8APn5HmiOjtO/2dh3ZruCIF8/xIpnGXg+cTZGj68xDf2sNjxSfl7KEPeDV hIP7xeIkGZdpGYhKJ7AEtVAoI64hgIVLrcH42na4N8excVKwQC08iJr88X1ITvxVPO XA5yL6jj38uYCbnrj/64cUXtFGWsnKYXLE1zfVG0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 9DC5A385803F for ; Thu, 11 Feb 2021 08:51:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 9DC5A385803F Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-523-E_m3MEu6PMiSxMZAPBHvMw-1; Thu, 11 Feb 2021 03:51:01 -0500 X-MC-Unique: E_m3MEu6PMiSxMZAPBHvMw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4F114801962 for ; Thu, 11 Feb 2021 08:51:00 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-112-197.ams2.redhat.com [10.36.112.197]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DB73572162; Thu, 11 Feb 2021 08:50:59 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 11B8ou9C3044080 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 11 Feb 2021 09:50:57 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 11B8ouoY3044079; Thu, 11 Feb 2021 09:50:56 +0100 Date: Thu, 11 Feb 2021 09:50:56 +0100 To: Jason Merrill Subject: [PATCH] c++: Fix zero initialization of flexible array members [PR99033] Message-ID: <20210211085056.GR4020736@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-6.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Hi! array_type_nelts returns error_mark_node for type of flexible array members and build_zero_init_1 was placing an error_mark_node into the CONSTRUCTOR, on which e.g. varasm ICEs. I think there is nothing erroneous on zero initialization of flexible array members though, such arrays should simply get no elements, like they do if such classes are constructed (everything except when some larger initializer comes from an explicit initializer). So, this patch handles [] arrays in zero initialization like [0] arrays and fixes handling of the [0] arrays - the tree_int_cst_equal (max_index, integer_minus_one_node) check didn't do what it thought it would do, max_index is typically unsigned integer (sizetype) and so it is never equal to a -1. What the patch doesn't do and maybe would be desirable is if it returns error_mark_node for other reasons let the recursive callers not stick that into CONSTRUCTOR but return error_mark_node instead. But I don't have a testcase where that would be needed right now. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-02-11 Jakub Jelinek PR c++/99033 * init.c (build_zero_init_1): Handle zero initialiation of flexible array members like initialization of [0] arrays. Use integer_all_onesp instead of comparison to integer_minus_one_node and integer_zerop instead of comparison against size_zero_node. Formatting fixes. * g++.dg/ext/flexary38.C: New test. Jakub --- gcc/cp/init.c.jj 2021-01-27 10:05:35.273942499 +0100 +++ gcc/cp/init.c 2021-02-10 15:03:02.668042115 +0100 @@ -247,9 +247,12 @@ build_zero_init_1 (tree type, tree nelts /* Iterate over the array elements, building initializations. */ if (nelts) - max_index = fold_build2_loc (input_location, - MINUS_EXPR, TREE_TYPE (nelts), - nelts, integer_one_node); + max_index = fold_build2_loc (input_location, MINUS_EXPR, + TREE_TYPE (nelts), nelts, + build_one_cst (TREE_TYPE (nelts))); + /* Treat flexible array members like [0] arrays. */ + else if (TYPE_DOMAIN (type) == NULL_TREE) + max_index = build_all_ones_cst (sizetype); else max_index = array_type_nelts (type); @@ -261,20 +264,19 @@ build_zero_init_1 (tree type, tree nelts /* A zero-sized array, which is accepted as an extension, will have an upper bound of -1. */ - if (!tree_int_cst_equal (max_index, integer_minus_one_node)) + if (!integer_all_onesp (max_index)) { constructor_elt ce; /* If this is a one element array, we just use a regular init. */ - if (tree_int_cst_equal (size_zero_node, max_index)) + if (integer_zerop (max_index)) ce.index = size_zero_node; else ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node, - max_index); + max_index); - ce.value = build_zero_init_1 (TREE_TYPE (type), - /*nelts=*/NULL_TREE, - static_storage_p, NULL_TREE); + ce.value = build_zero_init_1 (TREE_TYPE (type), /*nelts=*/NULL_TREE, + static_storage_p, NULL_TREE); if (ce.value) { vec_alloc (v, 1); --- gcc/testsuite/g++.dg/ext/flexary38.C.jj 2021-02-10 15:16:40.700102841 +0100 +++ gcc/testsuite/g++.dg/ext/flexary38.C 2021-02-10 15:16:22.243304533 +0100 @@ -0,0 +1,18 @@ +// PR c++/99033 +// { dg-do compile } +// { dg-options "" } + +struct T { int t; }; +struct S { char c; int T::*b[]; } a; +struct U { char c; int T::*b[0]; } b; +struct V { char c; int T::*b[1]; } c; +struct W { char c; int T::*b[2]; } d; + +void +foo () +{ + a.c = 1; + b.c = 2; + c.c = 3; + d.c = 4; +}