diff mbox

[C++] Add tests for a const member and a reference member for launder.

Message ID CAFk2RUabJo6QRLKb9wt-mh9E9Z4iavvDb30-=vibL9pOUj2RWg@mail.gmail.com
State New
Headers show

Commit Message

Ville Voutilainen Oct. 30, 2016, 6:53 p.m. UTC
So, how about adding these? They should give us regression tests
that make sure launder does the right in in case some optimizations
attempt to reuse const/reference members.

Tested on Linux-x64.

2016-10-30  Ville Voutilainen  <ville.voutilainen@gmail.com>

    Add tests for a const member and a reference member for launder.
    * g++.dg/cpp1z/launder3.C: New.
    * g++.dg/cpp1z/launder4.C: Likewise.

Comments

Jason Merrill Oct. 31, 2016, 1:31 p.m. UTC | #1
On Sun, Oct 30, 2016 at 2:53 PM, Ville Voutilainen
<ville.voutilainen@gmail.com> wrote:
> So, how about adding these? They should give us regression tests
> that make sure launder does the right in in case some optimizations
> attempt to reuse const/reference members.

Good idea.  You might put the reuse in a separate function in order to
hide it from the optimizer.

Jason
diff mbox

Patch

diff --git a/gcc/testsuite/g++.dg/cpp1z/launder3.C b/gcc/testsuite/g++.dg/cpp1z/launder3.C
new file mode 100644
index 0000000..2a2afc5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/launder3.C
@@ -0,0 +1,38 @@ 
+// { dg-do run { target c++11 } }
+// { dg-additional-options "-O2" }
+
+#include <cassert>
+
+void *
+operator new (decltype (sizeof (0)), void *p)
+{
+  return p;
+}
+
+namespace std
+{
+  template <typename T>
+  T *
+  launder (T *p)
+  {
+    return __builtin_launder (p);
+  }
+}
+
+struct A
+{
+  const int x;
+};
+
+struct B
+{
+  A a;
+};
+
+int
+main ()
+{
+  B b{{42}};
+  new (&b.a) A{666};
+  assert(std::launder(&b.a)->x == 666);
+}
diff --git a/gcc/testsuite/g++.dg/cpp1z/launder4.C b/gcc/testsuite/g++.dg/cpp1z/launder4.C
new file mode 100644
index 0000000..3a65eb2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/launder4.C
@@ -0,0 +1,40 @@ 
+// { dg-do run { target c++11 } }
+// { dg-additional-options "-O2" }
+
+#include <cassert>
+
+void *
+operator new (decltype (sizeof (0)), void *p)
+{
+  return p;
+}
+
+namespace std
+{
+  template <typename T>
+  T *
+  launder (T *p)
+  {
+    return __builtin_launder (p);
+  }
+}
+
+struct A
+{
+  int& x;
+};
+
+struct B
+{
+  A a;
+};
+
+int
+main ()
+{
+  int x = 42;
+  B b{{x}};
+  int y = 666;
+  new (&b.a) A{y};
+  assert(std::launder(&b.a)->x == 666);
+}