This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[C++ PATCH] Allow value initialization of classes with flexible array member (PR c++/87148)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Jason Merrill <jason at redhat dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Tue, 5 Mar 2019 23:23:45 +0100
- Subject: [C++ PATCH] Allow value initialization of classes with flexible array member (PR c++/87148)
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
Hi!
In this PR, Jonathan argues that we should accept value initialization of
classes with flexible array member.
The following patch does that. Bootstrapped/regtested on x86_64-linux
and i686-linux, ok for trunk?
2019-03-05 Jakub Jelinek <jakub@redhat.com>
PR c++/87148
* init.c (build_value_init_noctor): Ignore flexible array members.
* g++.dg/ext/flexary34.C: New test.
--- gcc/cp/init.c.jj 2019-03-05 15:34:17.164490227 +0100
+++ gcc/cp/init.c 2019-03-05 15:34:27.140327205 +0100
@@ -419,6 +419,15 @@ build_value_init_noctor (tree type, tsub
if (ftype == error_mark_node)
continue;
+ /* Ignore flexible array members for value initialization. */
+ if (TREE_CODE (ftype) == ARRAY_TYPE
+ && !COMPLETE_TYPE_P (ftype)
+ && !TYPE_DOMAIN (ftype)
+ && COMPLETE_TYPE_P (TREE_TYPE (ftype))
+ && (next_initializable_field (DECL_CHAIN (field))
+ == NULL_TREE))
+ continue;
+
/* We could skip vfields and fields of types with
user-defined constructors, but I think that won't improve
performance at all; it should be simpler in general just
--- gcc/testsuite/g++.dg/ext/flexary34.C.jj 2019-03-05 15:31:38.731079324 +0100
+++ gcc/testsuite/g++.dg/ext/flexary34.C 2019-03-05 15:32:46.852966084 +0100
@@ -0,0 +1,10 @@
+// PR c++/87148
+// { dg-do compile }
+// { dg-options "-pedantic" }
+
+struct Tst { int i; char t[]; }; // { dg-warning "forbids flexible array member" }
+
+Tst t {}; // { dg-warning "extended initializer lists only available with" "" { target c++98_only } }
+Tst u = Tst();
+void foo () { Tst u = {}; }
+Tst *bar () { return new Tst (); }
Jakub