diff options
author | Patrick Palka <ppalka@gcc.gnu.org> | 2016-03-23 21:08:32 +0000 |
---|---|---|
committer | Patrick Palka <ppalka@gcc.gnu.org> | 2016-03-23 21:08:32 +0000 |
commit | 426b9428081f1ec16d9e7ca6bd34ce50ff8cde37 (patch) | |
tree | c5c84c33aec21b6c26ab9ee3f47e1793de17494c /gcc | |
parent | cd1588c4d60162730a103b6ca3a30139d88a57e3 (diff) | |
download | gcc-426b9428081f1ec16d9e7ca6bd34ce50ff8cde37.zip gcc-426b9428081f1ec16d9e7ca6bd34ce50ff8cde37.tar.gz gcc-426b9428081f1ec16d9e7ca6bd34ce50ff8cde37.tar.bz2 |
Fix PR c++/70347 (default member initializer not picked up by union)
gcc/cp/ChangeLog:
PR c++/70347
* typeck.c (process_init_constructor_union): If the initializer
is empty, use the union's NSDMI if it has one.
gcc/testsuite/ChangeLog:
PR c++/70347
* g++.dg/cpp1y/nsdmi-union1.C: New test.
From-SVN: r234443
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/cp/typeck2.c | 19 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp1y/nsdmi-union1.C | 33 |
4 files changed, 61 insertions, 2 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 984a030..b32c72d 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,11 @@ 2016-03-23 Patrick Palka <ppalka@gcc.gnu.org> + PR c++/70347 + * typeck.c (process_init_constructor_union): If the initializer + is empty, use the union's NSDMI if it has one. + +2016-03-23 Patrick Palka <ppalka@gcc.gnu.org> + PR c++/70332 * pt.c (tsubst_copy) [PARM_DECL]: Handle the use of 'this' in an NSDMI that's part of an aggregrate initialization. diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index 2a76c96..4ab77cd 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -1499,9 +1499,24 @@ process_init_constructor_union (tree type, tree init, constructor_elt *ce; int len; - /* If the initializer was empty, use default zero initialization. */ + /* If the initializer was empty, use the union's NSDMI if it has one. + Otherwise use default zero initialization. */ if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) - return 0; + { + for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) + { + if (DECL_INITIAL (field)) + { + CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), + field, + get_nsdmi (field, /*in_ctor=*/false)); + break; + } + } + + if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) + return 0; + } len = CONSTRUCTOR_ELTS (init)->length (); if (len > 1) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 5e32276..3a5cf65 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,10 @@ 2016-03-23 Patrick Palka <ppalka@gcc.gnu.org> + PR c++/70347 + * g++.dg/cpp1y/nsdmi-union1.C: New test. + +2016-03-23 Patrick Palka <ppalka@gcc.gnu.org> + PR c++/70332 * g++.dg/cpp1y/nsdmi-aggr5.C: New test. diff --git a/gcc/testsuite/g++.dg/cpp1y/nsdmi-union1.C b/gcc/testsuite/g++.dg/cpp1y/nsdmi-union1.C new file mode 100644 index 0000000..d9dd0bf --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/nsdmi-union1.C @@ -0,0 +1,33 @@ +// PR c++/70347 +// { dg-do run { target c++14 } } + +union A { + char a; + long b = -42; +}; + +struct B { + union { + char a = 10; + long b; + }; +}; + +A c1{}; +A c2{4}; +B c3{}; +B c4{{9}}; + +int main() { + if (c1.b != -42) + __builtin_abort (); + + if (c2.a != 4) + __builtin_abort (); + + if (c3.a != 10) + __builtin_abort (); + + if (c4.a != 9) + __builtin_abort (); +} |