[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / AST / Interp / cxx20.cpp
blobe9505b3cf718876f67352873ca348289b097d7da
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++20 -verify %s
2 // RUN: %clang_cc1 -std=c++20 -verify=ref %s
4 void test_alignas_operand() {
5 alignas(8) char dummy;
6 static_assert(__alignof(dummy) == 8);
9 constexpr int getMinus5() {
10 int a = 10;
11 a = -5;
12 int *p = &a;
13 return *p;
15 static_assert(getMinus5() == -5, "");
17 constexpr int assign() {
18 int m = 10;
19 int k = 12;
21 m = (k = 20);
23 return m;
25 static_assert(assign() == 20, "");
28 constexpr int pointerAssign() {
29 int m = 10;
30 int *p = &m;
32 *p = 12; // modifies m
34 return m;
36 static_assert(pointerAssign() == 12, "");
38 constexpr int pointerDeref() {
39 int m = 12;
40 int *p = &m;
42 return *p;
44 static_assert(pointerDeref() == 12, "");
46 constexpr int pointerAssign2() {
47 int m = 10;
48 int *p = &m;
49 int **pp = &p;
51 **pp = 12;
53 int v = **pp;
55 return v;
57 static_assert(pointerAssign2() == 12, "");
59 constexpr int unInitLocal() {
60 int a;
61 return a; // ref-note {{read of uninitialized object}} \
62 // expected-note {{read of object outside its lifetime}}
63 // FIXME: ^^^ Wrong diagnostic.
65 static_assert(unInitLocal() == 0, ""); // ref-error {{not an integral constant expression}} \
66 // ref-note {{in call to 'unInitLocal()'}} \
67 // expected-error {{not an integral constant expression}} \
68 // expected-note {{in call to 'unInitLocal()'}} \
70 constexpr int initializedLocal() {
71 int a;
72 a = 20;
73 return a;
75 static_assert(initializedLocal() == 20);
77 constexpr int initializedLocal2() {
78 int a[2];
79 return *a; // expected-note {{read of object outside its lifetime}} \
80 // ref-note {{read of uninitialized object is not allowed in a constant expression}}
82 static_assert(initializedLocal2() == 20); // expected-error {{not an integral constant expression}} \
83 // expected-note {{in call to}} \
84 // ref-error {{not an integral constant expression}} \
85 // ref-note {{in call to}}
88 struct Int { int a; }; // expected-note {{subobject declared here}}
89 constexpr int initializedLocal3() {
90 Int i; // expected-note {{subobject of type 'int' is not initialized}}
91 return i.a; // ref-note {{read of uninitialized object is not allowed in a constant expression}}
93 static_assert(initializedLocal3() == 20); // expected-error {{not an integral constant expression}} \
94 // expected-note {{in call to}} \
95 // ref-error {{not an integral constant expression}} \
96 // ref-note {{in call to}}
100 #if 0
101 // FIXME: This code should be rejected because we pass an uninitialized value
102 // as a function parameter.
103 constexpr int inc(int a) { return a + 1; }
104 constexpr int f() {
105 int i;
106 return inc(i);
108 static_assert(f());
109 #endif
111 /// Distinct literals have disctinct addresses.
112 /// see https://github.com/llvm/llvm-project/issues/58754
113 constexpr auto foo(const char *p) { return p; }
114 constexpr auto p1 = "test1";
115 constexpr auto p2 = "test2";
117 constexpr bool b1 = foo(p1) == foo(p1);
118 static_assert(b1);
120 constexpr bool b2 = foo(p1) == foo(p2); // ref-error {{must be initialized by a constant expression}} \
121 // ref-note {{comparison of addresses of literals}} \
122 // ref-note {{declared here}}
123 static_assert(!b2); // ref-error {{not an integral constant expression}} \
124 // ref-note {{not a constant expression}}
126 constexpr auto name1() { return "name1"; }
127 constexpr auto name2() { return "name2"; }
129 constexpr auto b3 = name1() == name1();
130 static_assert(b3);
131 constexpr auto b4 = name1() == name2(); // ref-error {{must be initialized by a constant expression}} \
132 // ref-note {{has unspecified value}} \
133 // ref-note {{declared here}}
134 static_assert(!b4); // ref-error {{not an integral constant expression}} \
135 // ref-note {{not a constant expression}}
137 namespace UninitializedFields {
138 class A {
139 public:
140 int a; // expected-note 2{{subobject declared here}} \
141 // ref-note 2{{subobject declared here}}
142 constexpr A() {}
144 constexpr A a; // expected-error {{must be initialized by a constant expression}} \
145 // expected-note {{subobject of type 'int' is not initialized}} \
146 // ref-error {{must be initialized by a constant expression}} \
147 // ref-note {{subobject of type 'int' is not initialized}}
150 class Base {
151 public:
152 bool b;
153 int a; // expected-note {{subobject declared here}} \
154 // ref-note {{subobject declared here}}
155 constexpr Base() : b(true) {}
158 class Derived : public Base {
159 public:
160 constexpr Derived() : Base() {} // expected-note {{subobject of type 'int' is not initialized}}
163 constexpr Derived D; // expected-error {{must be initialized by a constant expression}} \\
164 // expected-note {{in call to 'Derived()'}} \
165 // ref-error {{must be initialized by a constant expression}} \
166 // ref-note {{subobject of type 'int' is not initialized}}
168 class C2 {
169 public:
170 A a;
171 constexpr C2() {} // expected-note {{subobject of type 'int' is not initialized}}
173 constexpr C2 c2; // expected-error {{must be initialized by a constant expression}} \
174 // expected-note {{in call to 'C2()'}} \
175 // ref-error {{must be initialized by a constant expression}} \
176 // ref-note {{subobject of type 'int' is not initialized}}
179 // FIXME: These two are currently disabled because the array fields
180 // cannot be initialized.
181 #if 0
182 class C3 {
183 public:
184 A a[2];
185 constexpr C3() {}
187 constexpr C3 c3; // expected-error {{must be initialized by a constant expression}} \
188 // expected-note {{subobject of type 'int' is not initialized}} \
189 // ref-error {{must be initialized by a constant expression}} \
190 // ref-note {{subobject of type 'int' is not initialized}}
192 class C4 {
193 public:
194 bool B[2][3]; // expected-note {{subobject declared here}} \
195 // ref-note {{subobject declared here}}
196 constexpr C4(){}
198 constexpr C4 c4; // expected-error {{must be initialized by a constant expression}} \
199 // expected-note {{subobject of type 'bool' is not initialized}} \
200 // ref-error {{must be initialized by a constant expression}} \
201 // ref-note {{subobject of type 'bool' is not initialized}}
202 #endif