diff mbox series

[C++] Allow value initialization of classes with flexible array member (PR c++/87148)

Message ID 20190305222345.GE7611@tucnak
State New
Headers show
Series [C++] Allow value initialization of classes with flexible array member (PR c++/87148) | expand

Commit Message

Jakub Jelinek March 5, 2019, 10:23 p.m. UTC
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.


	Jakub

Comments

Jason Merrill March 6, 2019, 3:06 p.m. UTC | #1
On 3/5/19 5:23 PM, Jakub Jelinek wrote:
> 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?

OK.

Jason
diff mbox series

Patch

--- 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 (); }