From patchwork Mon Nov 22 02:51:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: will wray X-Patchwork-Id: 47983 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AB75D385841A for ; Mon, 22 Nov 2021 02:53:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AB75D385841A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1637549582; bh=OGuVGW0YcOuC97syf68quPjIiuIZ33uPSW+f5FEqi4I=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=eWpHKemRpMhhSJM/sKu+cOiCTlhsTvfwuKxnHCRv7LlwuBm/2xhO+jHwnmlnOvmI+ XE8HrMARLEMqzjK9CWV8yKO86CCP87sLIkoOWYvReZPNK+WPcTYaq09r62K5lFvpPB 42lxoX5IKUBB/BqCs1BA/fQ8523gUJieADIDJNfk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by sourceware.org (Postfix) with ESMTPS id AA5293858013 for ; Mon, 22 Nov 2021 02:51:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AA5293858013 Received: by mail-qv1-xf29.google.com with SMTP id bu11so11551724qvb.0 for ; Sun, 21 Nov 2021 18:51:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OGuVGW0YcOuC97syf68quPjIiuIZ33uPSW+f5FEqi4I=; b=biLS98+eFiF16RQiqL+tcDBkN8Ihad45IEoEqKeaFrt3T9zHnt750ebT1ro6/fT3fx 6A+QfOHnX1AR2U2v2xx4URJFqgdUltIpvIc5SAXYcyqHIxnYlgozEUufIIWhauIog+/p Q6kXIFJ3J9EOfPmBOFJPH23lBZxX28tmebva0DWDLTvSMaYhJ90tarcIN2x4Q6HDmqck c57df9+uNWs3CvwBcoBEeJpTplYsvhWe++gsqTO9xDAkFIPIPCMPl8McDZXEDMsyUlJb /Qxi1uuqhFs7xu+bXLnNs967QwGHj/X2CnBlWXy1pmDUwM7ao2dsag61fWOf5naHcQq6 HN3A== X-Gm-Message-State: AOAM533r0Adz7SWbGpOhm4toI7ZbG8nEh7/Unxt99oIhbEVD6so7DRBe rQImqAHR20cwCtQBZ+6DKT65vZT3ot8= X-Google-Smtp-Source: ABdhPJxbEpw56bdC7OS1jotUVAEVN93GA31driIVIBtq955dgHpu4OTYviqZPs7YlQMMPGV4tzE0sA== X-Received: by 2002:a05:6214:ace:: with SMTP id g14mr96754680qvi.12.1637549505969; Sun, 21 Nov 2021 18:51:45 -0800 (PST) Received: from localhost.localdomain (bras-base-sttnpq8102w-grc-16-142-115-13-133.dsl.bell.ca. [142.115.13.133]) by smtp.googlemail.com with ESMTPSA id t35sm3947822qtc.83.2021.11.21.18.51.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Nov 2021 18:51:45 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH 1/3] c++: designated init of char array by string constant [PR55227] Date: Sun, 21 Nov 2021 21:51:12 -0500 Message-Id: <20211122025114.3167997-2-wjwray@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211122025114.3167997-1-wjwray@gmail.com> References: <20211122025114.3167997-1-wjwray@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Will Wray via Gcc-patches From: will wray Reply-To: Will Wray Cc: Will Wray Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Also address "FIXME: this code is duplicated from reshape_init" in cp_complete_array_type by always calling reshape_init on init-list. PR c++/55227 gcc/cp/ChangeLog: * decl.c (reshape_init_r): Only call has_designator_check when first_initializer_p or for the inner constructor element. (cp_complete_array_type): Call reshape_init on braced-init-list. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/desig20.C: New test. --- gcc/cp/decl.c | 42 +++++++++++++------------------ gcc/testsuite/g++.dg/cpp2a/desig20.C | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 25 deletions(-) diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 2ddf0e4a524..83a2d3bf8f1 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -6824,28 +6824,31 @@ reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p, if (TREE_CODE (type) == ARRAY_TYPE && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))) { - tree str_init = init; - tree stripped_str_init = stripped_init; + tree arr_init = init; + tree stripped_arr_init = stripped_init; + reshape_iter stripd = {}; /* Strip one level of braces if and only if they enclose a single element (as allowed by [dcl.init.string]). */ if (!first_initializer_p - && TREE_CODE (stripped_str_init) == CONSTRUCTOR - && CONSTRUCTOR_NELTS (stripped_str_init) == 1) + && TREE_CODE (stripped_arr_init) == CONSTRUCTOR + && CONSTRUCTOR_NELTS (stripped_arr_init) == 1) { - str_init = (*CONSTRUCTOR_ELTS (stripped_str_init))[0].value; - stripped_str_init = tree_strip_any_location_wrapper (str_init); + stripd.cur = CONSTRUCTOR_ELT (stripped_arr_init, 0); + arr_init = stripd.cur->value; + stripped_arr_init = tree_strip_any_location_wrapper (arr_init); } /* If it's a string literal, then it's the initializer for the array as a whole. Otherwise, continue with normal initialization for array types (one value per array element). */ - if (TREE_CODE (stripped_str_init) == STRING_CST) + if (TREE_CODE (stripped_arr_init) == STRING_CST) { - if (has_designator_problem (d, complain)) + if ((first_initializer_p && has_designator_problem (d, complain)) + || (stripd.cur && has_designator_problem (&stripd, complain))) return error_mark_node; d->cur++; - return str_init; + return arr_init; } } @@ -9545,22 +9548,11 @@ cp_complete_array_type (tree *ptype, tree initial_value, bool do_default) if (initial_value) { /* An array of character type can be initialized from a - brace-enclosed string constant. - - FIXME: this code is duplicated from reshape_init. Probably - we should just call reshape_init here? */ - if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype))) - && TREE_CODE (initial_value) == CONSTRUCTOR - && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value))) - { - vec *v = CONSTRUCTOR_ELTS (initial_value); - tree value = (*v)[0].value; - STRIP_ANY_LOCATION_WRAPPER (value); - - if (TREE_CODE (value) == STRING_CST - && v->length () == 1) - initial_value = value; - } + brace-enclosed string constant so call reshape_init to + remove the optional braces from a braced string literal. */ + if (BRACE_ENCLOSED_INITIALIZER_P (initial_value)) + initial_value = reshape_init (*ptype, initial_value, + tf_warning_or_error); /* If any of the elements are parameter packs, we can't actually complete this type now because the array size is dependent. */ diff --git a/gcc/testsuite/g++.dg/cpp2a/desig20.C b/gcc/testsuite/g++.dg/cpp2a/desig20.C new file mode 100644 index 00000000000..daadfa58855 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig20.C @@ -0,0 +1,48 @@ +// PR c++/55227 +// Test designated initializer for char array by string constant + +// { dg-options "" } + +struct C {char a[2];}; + +/* Case a, designated, unbraced, string-literal of the exact same size + as the initialized char array; valid and accepted before and after. */ +C a = {.a="a"}; + +/* Cases b,c,d, designated, braced or mimatched-size, string literal, + previously rejected; "C99 designator 'a' outside aggregate initializer". */ +C b = {.a=""}; +C c = {.a={""}}; +C d = {.a={"a"}}; + +/* Case e, designated char array field and braced, designated array element(s) + (with GNU [N]= extension) valid and accepted before and after. */ +C e = {.a={[0]='a'}}; + +/* Cases f,g,h, braced string literal, 'designated' within inner braces; + invalid, previously accepted as positional with 'designator' ignored. */ +C f = {{[0]="a"}}; // { dg-error "C99 designator .0. outside aggregate initializer" } +C g = {{.a="a"}}; // { dg-error "C99 designator .a. outside aggregate initializer" } +C h = {{.b="a"}}; // { dg-error "C99 designator .b. outside aggregate initializer" } + +char a2[][10] = { [0] = { "aaa" } }; + +struct D { C c; int a[8]; }; + +D x = { .c {.a={"a"}}, .a={1,2,3,4,5,6,7,8} }; + +struct A { union { int a; char c[4]; }; }; + +A non = { .c = "c++" }; + +template +void t() +{ + C ca[] = { {.a=""}, {.a={""}}, }; + +} + +void u() +{ + return t(); +}