From patchwork Sat Dec 15 08:36:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 1013863 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-492563-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=hotmail.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="VxztKJdn"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43H13x4vjGz9s3C for ; Sat, 15 Dec 2018 19:36:43 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:content-type:mime-version; q=dns; s= default; b=K2zEtgIVyH4yjJ0WhVVQeSLEkfHZnk+GCjdrbrqvu7eE7bHvqZhiW dxRvRSqXnhxBlbHTAYNUoMboS47Rr5Jn9aae9nC92nUtHZ1aD5+daID0rsR1wM/x p8j4tdKFhoPRZzgqYolNxFzqJhSjzS1TTbCCPt+XdtFLKT8EZeWODc= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:content-type:mime-version; s= default; bh=uKTv+Nh2Eihec2nUamgYn1FsKJg=; b=VxztKJdnvvnJoD1Icf0k zfx7mP4n0h4MUmkZwDouGCXWMXhUKINv0T/Aj8wfHQlE1RispPRyjJ4U2yUTVKAh uIxAJn3ouipr5i/JGqw43qGyMsJl9b7qmfdFFPdJYYa7O5/PEJNA9OAUWVEbCNDI iroBYVeaFL1BOVAJnOFIy3A= Received: (qmail 95895 invoked by alias); 15 Dec 2018 08:36:35 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 95880 invoked by uid 89); 15 Dec 2018 08:36:35 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-15.6 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=brace, recurse, UD:s.a, semantics.c X-HELO: EUR02-VE1-obe.outbound.protection.outlook.com Received: from mail-oln040092069049.outbound.protection.outlook.com (HELO EUR02-VE1-obe.outbound.protection.outlook.com) (40.92.69.49) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 15 Dec 2018 08:36:28 +0000 Received: from AM5EUR02FT028.eop-EUR02.prod.protection.outlook.com (10.152.8.58) by AM5EUR02HT188.eop-EUR02.prod.protection.outlook.com (10.152.9.120) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.1446.11; Sat, 15 Dec 2018 08:36:25 +0000 Received: from DB7PR07MB5353.eurprd07.prod.outlook.com (10.152.8.60) by AM5EUR02FT028.mail.protection.outlook.com (10.152.8.115) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.1446.11 via Frontend Transport; Sat, 15 Dec 2018 08:36:25 +0000 Received: from DB7PR07MB5353.eurprd07.prod.outlook.com ([fe80::c011:41fd:3ef:e08b]) by DB7PR07MB5353.eurprd07.prod.outlook.com ([fe80::c011:41fd:3ef:e08b%2]) with mapi id 15.20.1446.013; Sat, 15 Dec 2018 08:36:25 +0000 From: Bernd Edlinger To: "gcc-patches@gcc.gnu.org" , Jeff Law , Jason Merrill , Nathan Sidwell Subject: [PATCH, C++] Fix PR c++/88261 Date: Sat, 15 Dec 2018 08:36:25 +0000 Message-ID: MIME-Version: 1.0 Hi, this patch implements an error message, for non-static initialization of a flexible array member. This duplicates the existing error message from the C-FE, to avoid ICE and wrong code generation issues, as pointed out in the PR. It is a bit funny that a non-functional feature like that has already rather much test coverage. The most easy adjustment seems to change the existing test cases to use static declarations. Bootstrapped and reg-tested on x86_64-pc-linux-gnu. Is it OK for trunk? Thanks Bernd. gcc/cp: 2018-12-15 Bernd Edlinger PR c++/88261 * typeck2.c (digest_init_r): Add a decl parameter. Raise an error for non-static initialization of a flexible array member. (process_init_constructor, digest_init_flags, massage_init_elt, process_init_constructor_array, process_init_constructor_record, process_init_constructor_union, process_init_constructor): Add a decl parameter and pass it thru. (digest_nsdmi_init): Pass decl parameter to digest_init_flags. (digest_init): Pass NULL as decl parameter to digest_init_r. * semantics.c (finish_compound_literal): Likewise. * cp-tree.h (digest_init_flags): Adjust prototype. gcc/testsuite: 2018-12-15 Bernd Edlinger PR c++/88261 * gcc.dg/array-6.c: Move to ... * c-c++-common/array-6.c: ... here. * g++.dg/ext/flexary3.C: Adjust test. * g++.dg/ext/flexary12.C: Likewise. * g++.dg/ext/flexary13.C: Likewise. * g++.dg/ext/flexary15.C: Likewise. * g++.dg/warn/Wplacement-new-size-1.C: Likewise. * g++.dg/warn/Wplacement-new-size-2.C: Likewise. * g++.dg/warn/Wplacement-new-size-6.C: Likewise. Index: gcc/cp/cp-tree.h =================================================================== --- gcc/cp/cp-tree.h (revision 266971) +++ gcc/cp/cp-tree.h (working copy) @@ -7483,7 +7483,8 @@ extern tree split_nonconstant_init (tree, tree); extern bool check_narrowing (tree, tree, tsubst_flags_t, bool = false); extern tree digest_init (tree, tree, tsubst_flags_t); -extern tree digest_init_flags (tree, tree, int, tsubst_flags_t); +extern tree digest_init_flags (tree, tree, int, + tsubst_flags_t, tree); extern tree digest_nsdmi_init (tree, tree, tsubst_flags_t); extern tree build_scoped_ref (tree, tree, tree *); extern tree build_x_arrow (location_t, tree, Index: gcc/cp/semantics.c =================================================================== --- gcc/cp/semantics.c (revision 266971) +++ gcc/cp/semantics.c (working copy) @@ -2828,7 +2828,7 @@ finish_compound_literal (tree type, tree compound_ return error_mark_node; } compound_literal = digest_init_flags (type, compound_literal, LOOKUP_NORMAL, - complain); + complain, NULL_TREE); if (TREE_CODE (compound_literal) == CONSTRUCTOR) { TREE_HAS_CONSTRUCTOR (compound_literal) = true; Index: gcc/cp/typeck2.c =================================================================== --- gcc/cp/typeck2.c (revision 266971) +++ gcc/cp/typeck2.c (working copy) @@ -36,7 +36,7 @@ along with GCC; see the file COPYING3. If not see static tree process_init_constructor (tree type, tree init, int nested, - tsubst_flags_t complain); + tsubst_flags_t complain, tree decl); /* Print an error message stemming from an attempt to use @@ -813,7 +813,7 @@ store_init_value (tree decl, tree init, vecindex = error_mark_node; gcc_assert (ce->value); ce->value - = massage_init_elt (TREE_TYPE (type), ce->value, nested, complain); + = massage_init_elt (TREE_TYPE (type), ce->value, nested, complain, + decl); gcc_checking_assert (ce->value == error_mark_node @@ -1371,7 +1388,8 @@ process_init_constructor_array (tree type, tree in we can't rely on the back end to do it for us, so make the initialization explicit by list-initializing from T{}. */ next = build_constructor (init_list_type_node, NULL); - next = massage_init_elt (TREE_TYPE (type), next, nested, complain); + next = massage_init_elt (TREE_TYPE (type), next, nested, complain, + decl); if (initializer_zerop (next)) /* The default zero-initialization is fine for us; don't add anything to the CONSTRUCTOR. */ @@ -1417,7 +1435,7 @@ process_init_constructor_array (tree type, tree in static int process_init_constructor_record (tree type, tree init, int nested, - tsubst_flags_t complain) + tsubst_flags_t complain, tree decl) { vec *v = NULL; tree field; @@ -1499,7 +1517,7 @@ process_init_constructor_record (tree type, tree i if (ce) { gcc_assert (ce->value); - next = massage_init_elt (type, next, nested, complain); + next = massage_init_elt (type, next, nested, complain, decl); ++idx; } } @@ -1528,7 +1546,8 @@ process_init_constructor_record (tree type, tree i for us, so build up TARGET_EXPRs. If the type in question is a class, just build one up; if it's an array, recurse. */ next = build_constructor (init_list_type_node, NULL); - next = massage_init_elt (TREE_TYPE (field), next, nested, complain); + next = massage_init_elt (TREE_TYPE (field), next, nested, complain, + decl); /* Warn when some struct elements are implicitly initialized. */ if ((complain & tf_warning) @@ -1644,7 +1663,7 @@ process_init_constructor_record (tree type, tree i static int process_init_constructor_union (tree type, tree init, int nested, - tsubst_flags_t complain) + tsubst_flags_t complain, tree decl) { constructor_elt *ce; int len; @@ -1731,7 +1750,7 @@ process_init_constructor_union (tree type, tree in if (ce->value && ce->value != error_mark_node) ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested, - complain); + complain, decl); return picflag_from_initializer (ce->value); } @@ -1752,7 +1771,7 @@ process_init_constructor_union (tree type, tree in static tree process_init_constructor (tree type, tree init, int nested, - tsubst_flags_t complain) + tsubst_flags_t complain, tree decl) { int flags; @@ -1759,11 +1778,11 @@ process_init_constructor (tree type, tree init, in gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init)); if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type)) - flags = process_init_constructor_array (type, init, nested, complain); + flags = process_init_constructor_array (type, init, nested, complain, decl); else if (TREE_CODE (type) == RECORD_TYPE) - flags = process_init_constructor_record (type, init, nested, complain); + flags = process_init_constructor_record (type, init, nested, complain, decl); else if (TREE_CODE (type) == UNION_TYPE) - flags = process_init_constructor_union (type, init, nested, complain); + flags = process_init_constructor_union (type, init, nested, complain, decl); else gcc_unreachable (); Index: gcc/testsuite/g++.dg/ext/flexary12.C =================================================================== --- gcc/testsuite/g++.dg/ext/flexary12.C (revision 266971) +++ gcc/testsuite/g++.dg/ext/flexary12.C (working copy) @@ -12,7 +12,7 @@ struct A { void f1 () { // This is the meat of the test from c++/69290: - struct A a + static struct A a = { "c" }; // { dg-error "invalid conversion from .const char\\*. to .int." } (void)&a; @@ -27,13 +27,13 @@ struct B { void f2 () { - struct B b1 + static struct B b1 = { 0, "c" }; // { dg-error "invalid conversion from .const char\\*. to .int." } (void)&b1; const char s[] = "c"; - struct B b2 + static struct B b2 = { 0, s }; // { dg-error "invalid conversion from .const char\\*. to .int." } (void)&b2; @@ -57,7 +57,7 @@ struct C { void f3 () { - struct C cd + static struct C cd = { "c" }; // { dg-error "cannot convert .const char\\*. to .double." } (void)&cd; Index: gcc/testsuite/g++.dg/ext/flexary13.C =================================================================== --- gcc/testsuite/g++.dg/ext/flexary13.C (revision 266971) +++ gcc/testsuite/g++.dg/ext/flexary13.C (working copy) @@ -19,33 +19,33 @@ int main () ASSERT (s.n == 0); } { - Ax s = + static Ax s = { 0, { } }; // dg-warning "initialization of a flexible array member" } ASSERT (s.n == 0); } { - Ax s = + static Ax s = { 1, { 2 } }; // dg-warning "initialization of a flexible array member" } ASSERT (s.n == 1 && s.a [0] == 2); } { - Ax s = + static Ax s = { 2, { 3, 4 } }; // dg-warning "initialization of a flexible array member" } ASSERT (s.n = 2 && s.a [0] == 3 && s.a [1] == 4); } { - Ax s = + static Ax s = { 123, i }; // dg-warning "initialization of a flexible array member" } ASSERT (s.n == 123 && s.a [0] == i); } { - Ax s = + static Ax s = { 456, { i } }; // dg-warning "initialization of a flexible array member" } ASSERT (s.n == 456 && s.a [0] == i); } { int j = i + 1, k = j + 1; - Ax s = + static Ax s = { 3, { i, j, k } }; // dg-warning "initialization of a flexible array member" } ASSERT (s.n == 3 && s.a [0] == i && s.a [1] == j && s.a [2] == k); } Index: gcc/testsuite/g++.dg/ext/flexary15.C =================================================================== --- gcc/testsuite/g++.dg/ext/flexary15.C (revision 266971) +++ gcc/testsuite/g++.dg/ext/flexary15.C (working copy) @@ -10,5 +10,5 @@ struct S { void foo (const char *a) { - const S s = { 1, { a, "b" } }; // { dg-warning "invalid conversion" } + static const S s = { 1, { a, "b" } }; // { dg-warning "invalid conversion" } } Index: gcc/testsuite/g++.dg/ext/flexary3.C =================================================================== --- gcc/testsuite/g++.dg/ext/flexary3.C (revision 266971) +++ gcc/testsuite/g++.dg/ext/flexary3.C (working copy) @@ -17,5 +17,6 @@ struct s { int main() { struct s s = { .c = 0 }; // { dg-error "initializer" } + // { dg-error "non-static initialization of a flexible array member" "" { target *-*-* } .-1 } return 0; } Index: gcc/testsuite/g++.dg/warn/Wplacement-new-size-1.C =================================================================== --- gcc/testsuite/g++.dg/warn/Wplacement-new-size-1.C (revision 266971) +++ gcc/testsuite/g++.dg/warn/Wplacement-new-size-1.C (working copy) @@ -28,7 +28,7 @@ void fAx (Ax *px, Ax &rx) void fAx2 () { - Ax ax2 = { 1, { 2, 3 } }; + static Ax ax2 = { 1, { 2, 3 } }; new (ax2.a) Int16; new (ax2.a) Int32; // { dg-warning "placement" } @@ -82,7 +82,7 @@ void fBx (BAx *pbx, BAx &rbx) void fBx1 () { - BAx bax1 = { 1, /* Ax = */ { 2, /* a[] = */ {} } }; + static BAx bax1 = { 1, /* Ax = */ { 2, /* a[] = */ {} } }; new (bax1.ax.a) char; // { dg-warning "placement" } new (bax1.ax.a) char[2]; // { dg-warning "placement" } Index: gcc/testsuite/g++.dg/warn/Wplacement-new-size-2.C =================================================================== --- gcc/testsuite/g++.dg/warn/Wplacement-new-size-2.C (revision 266971) +++ gcc/testsuite/g++.dg/warn/Wplacement-new-size-2.C (working copy) @@ -36,10 +36,10 @@ void fAx2 () // isn't allowed in C and should perhaps be disallowed in C++ as // well to avoid c++/69696 - incorrect initialization of block-scope // flexible array members. - Ax ax2 = { 1, { 2, 3 } }; + Ax ax2 = { 1, { 2, 3 } }; // { dg-error "non-static initialization of a flexible array member" } - new (ax2.a) Int16; - new (ax2.a) Int16[1]; + new (ax2.a) Int16; // { dg-warning "placement" } + new (ax2.a) Int16[1]; // { dg-warning "placement" } new (ax2.a) Int16[2]; // { dg-warning "placement" } new (ax2.a) Int32; // { dg-warning "placement" } new (ax2.a) Int32[2]; // { dg-warning "placement" } @@ -140,7 +140,7 @@ void fBx (BAx *pbx, BAx &rbx) void fBx1 () { - BAx bax1 = { 1, /* Ax = */ { 2, /* a[] = */ {} } }; + static BAx bax1 = { 1, /* Ax = */ { 2, /* a[] = */ {} } }; new (bax1.ax.a) char; // { dg-warning "placement" } new (bax1.ax.a) char[2]; // { dg-warning "placement" } Index: gcc/testsuite/g++.dg/warn/Wplacement-new-size-6.C =================================================================== --- gcc/testsuite/g++.dg/warn/Wplacement-new-size-6.C (revision 266971) +++ gcc/testsuite/g++.dg/warn/Wplacement-new-size-6.C (working copy) @@ -15,7 +15,7 @@ struct BAx { int i; Ax ax; }; void fBx1 () { - BAx bax1 = { 1, /* Ax = */ { 2, /* a[] = */ { 3 } } }; // { dg-error "initialization of flexible array member in a nested context" } + static BAx bax1 = { 1, /* Ax = */ { 2, /* a[] = */ { 3 } } }; // { dg-error "initialization of flexible array member in a nested context" } new (bax1.ax.a) char; // { dg-warning "placement" } new (bax1.ax.a) char[2]; // { dg-warning "placement" } @@ -25,7 +25,7 @@ void fBx1 () void fBx2 () { - BAx bax2 = { 1, /* Ax = */ { 2, /* a[] = */ { 3, 4 } } }; // { dg-error "initialization of flexible array member in a nested context" } + static BAx bax2 = { 1, /* Ax = */ { 2, /* a[] = */ { 3, 4 } } }; // { dg-error "initialization of flexible array member in a nested context" } new (bax2.ax.a) char; // { dg-warning "placement" } new (bax2.ax.a) char[2]; // { dg-warning "placement" } @@ -37,7 +37,7 @@ void fBx2 () void fBx3 () { - BAx bax2 = { 1, /* Ax = */ { 3, /* a[] = */ { 4, 5, 6 } } }; // { dg-error "initialization of flexible array member in a nested context" } + static BAx bax2 = { 1, /* Ax = */ { 3, /* a[] = */ { 4, 5, 6 } } }; // { dg-error "initialization of flexible array member in a nested context" } new (bax2.ax.a) char; // { dg-warning "placement" } new (bax2.ax.a) char[2]; // { dg-warning "placement" } Index: gcc/testsuite/gcc.dg/array-6.c =================================================================== --- gcc/testsuite/gcc.dg/array-6.c (revision 266971) +++ gcc/testsuite/gcc.dg/array-6.c (working copy) @@ -1,18 +0,0 @@ -/* PR c/5597 */ -/* { dg-do compile } */ -/* { dg-options "" } */ - -/* Verify that GCC forbids non-static initialization of - flexible array members. */ - -struct str { int len; char s[]; }; - -struct str a = { 2, "a" }; - -void foo() -{ - static struct str b = { 2, "b" }; - struct str c = { 2, "c" }; /* { dg-error "(non-static)|(near initialization)" } */ - struct str d = (struct str) { 2, "d" }; /* { dg-error "(non-static)|(near initialization)" } */ - struct str e = (struct str) { d.len, "e" }; /* { dg-error "(non-static)|(initialization)" } */ -} Index: gcc/testsuite/c-c++-common/array-6.c =================================================================== --- gcc/testsuite/c-c++-common/array-6.c (revision 0) +++ gcc/testsuite/c-c++-common/array-6.c (working copy) @@ -0,0 +1,18 @@ +/* PR c/5597 */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +/* Verify that GCC forbids non-static initialization of + flexible array members. */ + +struct str { int len; char s[]; }; + +struct str a = { 2, "a" }; + +void foo() +{ + static struct str b = { 2, "b" }; + struct str c = { 2, "c" }; /* { dg-error "(non-static)|(near initialization)" } */ + struct str d = (struct str) { 2, "d" }; /* { dg-error "(non-static)|(near initialization)" } */ + struct str e = (struct str) { d.len, "e" }; /* { dg-error "(non-static)|(initialization)" } */ +}