diff options
author | Jason Merrill <jason@redhat.com> | 2016-07-21 23:45:37 -0400 |
---|---|---|
committer | Jason Merrill <jason@gcc.gnu.org> | 2016-07-21 23:45:37 -0400 |
commit | a93f3513c2c1fef4541306f24597f1fc154548b3 (patch) | |
tree | 27ca6255ab8e5023a9107be93aafb36e5fce54a0 | |
parent | d5ec842cde69dedaf9a447d7d7a00735c9b4da40 (diff) | |
download | gcc-a93f3513c2c1fef4541306f24597f1fc154548b3.zip gcc-a93f3513c2c1fef4541306f24597f1fc154548b3.tar.gz gcc-a93f3513c2c1fef4541306f24597f1fc154548b3.tar.bz2 |
PR c++/71913 - missing copy elision with new.
* call.c (unsafe_copy_elision_p): It's OK to elide when
initializing an unknown object.
From-SVN: r238621
-rw-r--r-- | gcc/cp/ChangeLog | 4 | ||||
-rw-r--r-- | gcc/cp/call.c | 7 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/init/elide5.C | 27 |
3 files changed, 35 insertions, 3 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 3d3e85f..b196d54 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,9 @@ 2016-07-21 Jason Merrill <jason@redhat.com> + PR c++/71913 + * call.c (unsafe_copy_elision_p): It's OK to elide when + initializing an unknown object. + * call.c (build_over_call): Check unsafe_copy_elision_p even for trivial constructors. * method.c (do_build_copy_constructor): Don't copy tail padding diff --git a/gcc/cp/call.c b/gcc/cp/call.c index d917d9a..061e708 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -7275,10 +7275,11 @@ unsafe_copy_elision_p (tree target, tree exp) if (TREE_CODE (exp) != TARGET_EXPR) return false; tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp)); - if (type == CLASSTYPE_AS_BASE (type)) + /* It's safe to elide the copy for a class with no tail padding. */ + if (tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type))) return false; - if (!is_base_field_ref (target) - && resolves_to_fixed_type_p (target, NULL)) + /* It's safe to elide the copy if we aren't initializing a base object. */ + if (!is_base_field_ref (target)) return false; tree init = TARGET_EXPR_INITIAL (exp); /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */ diff --git a/gcc/testsuite/g++.dg/init/elide5.C b/gcc/testsuite/g++.dg/init/elide5.C new file mode 100644 index 0000000..0a9978c --- /dev/null +++ b/gcc/testsuite/g++.dg/init/elide5.C @@ -0,0 +1,27 @@ +// PR c++/71913 +// { dg-do link { target c++11 } } + +void* operator new(unsigned long, void* p) { return p; } + +struct IndirectReturn { + IndirectReturn() {} + // Undefined so we get a link error if the indirect return value is copied + IndirectReturn(const IndirectReturn&); + IndirectReturn& operator=(const IndirectReturn&) = delete; + ~IndirectReturn() {} +}; + +IndirectReturn foo() { return IndirectReturn(); } + +void bar(void* ptr) { + new (ptr) IndirectReturn(foo()); +} + +alignas (alignof (IndirectReturn)) +unsigned char c[sizeof(IndirectReturn)]; + +int main() +{ + bar(c); +} + |