summaryrefslogtreecommitdiff
path: root/test/SemaCXX/constant-expression-cxx2a.cpp
diff options
context:
space:
mode:
authorNandor Licker <n@ndor.email>2019-08-30 15:41:45 +0000
committerNandor Licker <n@ndor.email>2019-08-30 15:41:45 +0000
commit25e19b08c139bc67e0b09d4e9fe472909aee4719 (patch)
treeba2132a0e1546bf936e5802b83b581b22f051c6c /test/SemaCXX/constant-expression-cxx2a.cpp
parentff0c62802f36635b42ee98b6de52106468031c73 (diff)
downloadclang-25e19b08c139bc67e0b09d4e9fe472909aee4719.tar.gz
Revert [Clang Interpreter] Initial patch for the constexpr interpreter
This reverts r370476 (git commit a5590950549719d0d9ea69ed164b0c8c0f4e02e6) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@370481 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/SemaCXX/constant-expression-cxx2a.cpp')
-rw-r--r--test/SemaCXX/constant-expression-cxx2a.cpp119
1 files changed, 119 insertions, 0 deletions
diff --git a/test/SemaCXX/constant-expression-cxx2a.cpp b/test/SemaCXX/constant-expression-cxx2a.cpp
index cc6b196048..dba877ff21 100644
--- a/test/SemaCXX/constant-expression-cxx2a.cpp
+++ b/test/SemaCXX/constant-expression-cxx2a.cpp
@@ -414,6 +414,125 @@ namespace TypeId {
static_assert(side_effects());
}
+namespace Union {
+ struct Base {
+ int y; // expected-note {{here}}
+ };
+ struct A : Base {
+ int x;
+ int arr[3];
+ union { int p, q; };
+ };
+ union B {
+ A a;
+ int b;
+ };
+ constexpr int read_wrong_member() { // expected-error {{never produces a constant}}
+ B b = {.b = 1};
+ return b.a.x; // expected-note {{read of member 'a' of union with active member 'b'}}
+ }
+ constexpr int change_member() {
+ B b = {.b = 1};
+ b.a.x = 1;
+ return b.a.x;
+ }
+ static_assert(change_member() == 1);
+ constexpr int change_member_then_read_wrong_member() { // expected-error {{never produces a constant}}
+ B b = {.b = 1};
+ b.a.x = 1;
+ return b.b; // expected-note {{read of member 'b' of union with active member 'a'}}
+ }
+ constexpr int read_wrong_member_indirect() { // expected-error {{never produces a constant}}
+ B b = {.b = 1};
+ int *p = &b.a.y;
+ return *p; // expected-note {{read of member 'a' of union with active member 'b'}}
+ }
+ constexpr int read_uninitialized() {
+ B b = {.b = 1};
+ int *p = &b.a.y;
+ b.a.x = 1;
+ return *p; // expected-note {{read of uninitialized object}}
+ }
+ static_assert(read_uninitialized() == 0); // expected-error {{constant}} expected-note {{in call}}
+ constexpr void write_wrong_member_indirect() { // expected-error {{never produces a constant}}
+ B b = {.b = 1};
+ int *p = &b.a.y;
+ *p = 1; // expected-note {{assignment to member 'a' of union with active member 'b'}}
+ }
+ constexpr int write_uninitialized() {
+ B b = {.b = 1};
+ int *p = &b.a.y;
+ b.a.x = 1;
+ *p = 1;
+ return *p;
+ }
+ static_assert(write_uninitialized() == 1);
+ constexpr int change_member_indirectly() {
+ B b = {.b = 1};
+ b.a.arr[1] = 1;
+ int &r = b.a.y;
+ r = 123;
+
+ b.b = 2;
+ b.a.y = 3;
+ b.a.arr[2] = 4;
+ return b.a.arr[2];
+ }
+ static_assert(change_member_indirectly() == 4);
+ constexpr B return_uninit() {
+ B b = {.b = 1};
+ b.a.x = 2;
+ return b;
+ }
+ constexpr B uninit = return_uninit(); // expected-error {{constant expression}} expected-note {{subobject of type 'int' is not initialized}}
+ static_assert(return_uninit().a.x == 2);
+ constexpr A return_uninit_struct() {
+ B b = {.b = 1};
+ b.a.x = 2;
+ return b.a;
+ }
+ // FIXME: It's unclear that this should be valid. Copying a B involves
+ // copying the object representation of the union, but copying an A invokes a
+ // copy constructor that copies the object elementwise, and reading from
+ // b.a.y is undefined.
+ static_assert(return_uninit_struct().x == 2);
+ constexpr B return_init_all() {
+ B b = {.b = 1};
+ b.a.x = 2;
+ b.a.y = 3;
+ b.a.arr[0] = 4;
+ b.a.arr[1] = 5;
+ b.a.arr[2] = 6;
+ return b;
+ }
+ static_assert(return_init_all().a.x == 2);
+ static_assert(return_init_all().a.y == 3);
+ static_assert(return_init_all().a.arr[0] == 4);
+ static_assert(return_init_all().a.arr[1] == 5);
+ static_assert(return_init_all().a.arr[2] == 6);
+ static_assert(return_init_all().a.p == 7); // expected-error {{}} expected-note {{read of member 'p' of union with no active member}}
+ static_assert(return_init_all().a.q == 8); // expected-error {{}} expected-note {{read of member 'q' of union with no active member}}
+ constexpr B init_all = return_init_all();
+
+ constexpr bool test_no_member_change = []{
+ union U { char dummy = {}; };
+ U u1;
+ U u2;
+ u1 = u2;
+ return true;
+ }();
+
+ struct S1 {
+ int n;
+ };
+ struct S2 : S1 {};
+ struct S3 : S2 {};
+ void f() {
+ S3 s;
+ s.n = 0;
+ }
+}
+
namespace TwosComplementShifts {
using uint32 = __UINT32_TYPE__;
using int32 = __INT32_TYPE__;