Debian Codegraph @

clang/test/AST/ByteCode/builtin-functions.cpp

raw
1 // RUN: %clang_cc1 -Wno-string-plus-int -fexperimental-new-constant-interpreter -triple x86_64 %s -verify=expected,both
2 // RUN: %clang_cc1 -Wno-string-plus-int -triple x86_64 %s -verify=ref,both
3 //
4 // RUN: %clang_cc1 -Wno-string-plus-int -fexperimental-new-constant-interpreter -triple i686 %s -verify=expected,both
5 // RUN: %clang_cc1 -Wno-string-plus-int -triple i686 %s -verify=ref,both
6 //
7 // RUN: %clang_cc1 -std=c++20 -Wno-string-plus-int -fexperimental-new-constant-interpreter -triple x86_64 %s -verify=expected,both
8 // RUN: %clang_cc1 -std=c++20 -Wno-string-plus-int -triple x86_64 %s -verify=ref,both
9 //
10 // RUN: %clang_cc1 -std=c++20 -Wno-string-plus-int -fexperimental-new-constant-interpreter -triple i686 %s -verify=expected,both
11 // RUN: %clang_cc1 -std=c++20 -Wno-string-plus-int -triple i686 %s -verify=ref,both
12 //
13 // RUN: %clang_cc1 -triple avr -std=c++20 -Wno-string-plus-int -fexperimental-new-constant-interpreter %s -verify=expected,both
14 // RUN: %clang_cc1 -triple avr -std=c++20 -Wno-string-plus-int -verify=ref,both %s
15
16 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
17 #define LITTLE_END 1
18 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
19 #define LITTLE_END 0
20 #else
21 #error "huh?"
22 #endif
23
24
25 inline constexpr void* operator new(__SIZE_TYPE__, void* p) noexcept { return p; }
26 namespace std {
27 using size_t = decltype(sizeof(0));
28 template<typename T> struct allocator {
29 constexpr T *allocate(size_t N) {
30 return (T*)__builtin_operator_new(sizeof(T) * N); // #alloc
31 }
32 constexpr void deallocate(void *p, __SIZE_TYPE__) {
33 __builtin_operator_delete(p);
34 }
35 };
36 template<typename T, typename... Args>
37 constexpr T* construct_at(T* p, Args&&... args) { return ::new((void*)p) T(static_cast<Args&&>(args)...); }
38
39 template<typename T>
40 constexpr void destroy_at(T* p) {
41 p->~T();
42 }
43 }
44
45 extern "C" {
46 typedef decltype(sizeof(int)) size_t;
47 extern size_t wcslen(const wchar_t *p);
48 extern void *memchr(const void *s, int c, size_t n);
49 extern char *strchr(const char *s, int c);
50 extern wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n);
51 extern wchar_t *wcschr(const wchar_t *s, wchar_t c);
52 extern int wcscmp(const wchar_t *s1, const wchar_t *s2);
53 extern int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n);
54 extern wchar_t *wmemcpy(wchar_t *d, const wchar_t *s, size_t n);
55 }
56
57
58 constexpr int test_address_of_incomplete_array_type() { // both-error {{never produces a constant expression}}
59 extern int arr[];
60 __builtin_memmove(&arr, &arr, 4 * sizeof(arr[0])); // both-note 2{{cannot constant evaluate 'memmove' between objects of incomplete type 'int[]'}}
61 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
62 }
63 static_assert(test_address_of_incomplete_array_type() == 1234, ""); // both-error {{constant}} \
64 // both-note {{in call}}
65
66 namespace LocalExternRedecl {
67 constexpr int externRedecl1() {
68 extern int arr[];
69 return 0;
70 }
71 constexpr int externRedecl2() { // both-error {{never produces a constant expression}}
72 extern int arr[];
73 __builtin_memmove(&arr, &arr, 4 * sizeof(arr[0])); // both-note 2{{incomplete type}}
74 return 1234;
75 }
76 static_assert(externRedecl2() == 1234); // both-error {{not an integral constant expression}} \
77 // both-note {{in call to}}
78 }
79
80 struct NonTrivial {
81 constexpr NonTrivial() : n(0) {}
82 constexpr NonTrivial(const NonTrivial &) : n(1) {}
83 int n;
84 };
85 constexpr bool test_nontrivial_memcpy() { // both-error {{never produces a constant}}
86 NonTrivial arr[3] = {};
87 __builtin_memcpy(arr, arr + 1, sizeof(NonTrivial)); // both-note {{non-trivially-copyable}} \
88 // both-note {{non-trivially-copyable}}
89 return true;
90 }
91 static_assert(test_nontrivial_memcpy()); // both-error {{constant}} \
92 // both-note {{in call}}
93
94 namespace strcmp {
95 constexpr char kFoobar[6] = {'f','o','o','b','a','r'};
96 constexpr char kFoobazfoobar[12] = {'f','o','o','b','a','z','f','o','o','b','a','r'};
97
98 static_assert(__builtin_strcmp("", "") == 0, "");
99 static_assert(__builtin_strcmp("abab", "abab") == 0, "");
100 static_assert(__builtin_strcmp("abab", "abba") == -1, "");
101 static_assert(__builtin_strcmp("abab", "abaa") == 1, "");
102 static_assert(__builtin_strcmp("ababa", "abab") == 1, "");
103 static_assert(__builtin_strcmp("abab", "ababa") == -1, "");
104 static_assert(__builtin_strcmp("a\203", "a") == 1, "");
105 static_assert(__builtin_strcmp("a\203", "a\003") == 1, "");
106 static_assert(__builtin_strcmp("abab\0banana", "abab") == 0, "");
107 static_assert(__builtin_strcmp("abab", "abab\0banana") == 0, "");
108 static_assert(__builtin_strcmp("abab\0banana", "abab\0canada") == 0, "");
109 static_assert(__builtin_strcmp(0, "abab") == 0, ""); // both-error {{not an integral constant}} \
110 // both-note {{dereferenced null}}
111 static_assert(__builtin_strcmp("abab", 0) == 0, ""); // both-error {{not an integral constant}} \
112 // both-note {{dereferenced null}}
113
114 static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar) == -1, "");
115 static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar + 6) == 0, ""); // both-error {{not an integral constant}} \
116 // both-note {{dereferenced one-past-the-end}}
117
118 /// Used to assert because we're passing a dummy pointer to
119 /// __builtin_strcmp() when evaluating the return statement.
120 constexpr bool char_memchr_mutable() {
121 char buffer[] = "mutable";
122 return __builtin_strcmp(buffer, "mutable") == 0;
123 }
124 static_assert(char_memchr_mutable(), "");
125
126 static_assert(__builtin_strncmp("abaa", "abba", 5) == -1);
127 static_assert(__builtin_strncmp("abaa", "abba", 4) == -1);
128 static_assert(__builtin_strncmp("abaa", "abba", 3) == -1);
129 static_assert(__builtin_strncmp("abaa", "abba", 2) == 0);
130 static_assert(__builtin_strncmp("abaa", "abba", 1) == 0);
131 static_assert(__builtin_strncmp("abaa", "abba", 0) == 0);
132 static_assert(__builtin_strncmp(0, 0, 0) == 0);
133 static_assert(__builtin_strncmp("abab\0banana", "abab\0canada", 100) == 0);
134 }
135
136 namespace WcsCmp {
137 constexpr wchar_t kFoobar[6] = {L'f',L'o',L'o',L'b',L'a',L'r'};
138 constexpr wchar_t kFoobazfoobar[12] = {L'f',L'o',L'o',L'b',L'a',L'z',L'f',L'o',L'o',L'b',L'a',L'r'};
139
140 static_assert(__builtin_wcscmp(L"abab", L"abab") == 0);
141 static_assert(__builtin_wcscmp(L"abab", L"abba") == -1);
142 static_assert(__builtin_wcscmp(L"abab", L"abaa") == 1);
143 static_assert(__builtin_wcscmp(L"ababa", L"abab") == 1);
144 static_assert(__builtin_wcscmp(L"abab", L"ababa") == -1);
145 static_assert(__builtin_wcscmp(L"abab\0banana", L"abab") == 0);
146 static_assert(__builtin_wcscmp(L"abab", L"abab\0banana") == 0);
147 static_assert(__builtin_wcscmp(L"abab\0banana", L"abab\0canada") == 0);
148 #if __WCHAR_WIDTH__ == 32
149 static_assert(__builtin_wcscmp(L"a\x83838383", L"a") == (wchar_t)-1U >> 31);
150 #endif
151 static_assert(__builtin_wcscmp(0, L"abab") == 0); // both-error {{not an integral constant}} \
152 // both-note {{dereferenced null}}
153 static_assert(__builtin_wcscmp(L"abab", 0) == 0); // both-error {{not an integral constant}} \
154 // both-note {{dereferenced null}}
155
156 static_assert(__builtin_wcscmp(kFoobar, kFoobazfoobar) == -1);
157 static_assert(__builtin_wcscmp(kFoobar, kFoobazfoobar + 6) == 0); // both-error {{not an integral constant}} \
158 // both-note {{dereferenced one-past-the-end}}
159
160 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 5) == -1);
161 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 4) == -1);
162 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 3) == -1);
163 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 2) == 0);
164 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 1) == 0);
165 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 0) == 0);
166 static_assert(__builtin_wcsncmp(0, 0, 0) == 0);
167 static_assert(__builtin_wcsncmp(L"abab\0banana", L"abab\0canada", 100) == 0);
168 #if __WCHAR_WIDTH__ == 32
169 static_assert(__builtin_wcsncmp(L"a\x83838383", L"aa", 2) ==
170 (wchar_t)-1U >> 31);
171 #endif
172
173 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar, 6) == -1);
174 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar, 7) == -1);
175 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar + 6, 6) == 0);
176 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar + 6, 7) == 0); // both-error {{not an integral constant}} \
177 // both-note {{dereferenced one-past-the-end}}
178 }
179
180 /// Copied from constant-expression-cxx11.cpp
181 namespace strlen {
182 constexpr const char *a = "foo\0quux";
183 constexpr char b[] = "foo\0quux";
184 constexpr int f() { return 'u'; }
185 constexpr char c[] = { 'f', 'o', 'o', 0, 'q', f(), 'u', 'x', 0 };
186
187 static_assert(__builtin_strlen("foo") == 3, "");
188 static_assert(__builtin_strlen("foo\0quux") == 3, "");
189 static_assert(__builtin_strlen("foo\0quux" + 4) == 4, "");
190
191 constexpr bool check(const char *p) {
192 return __builtin_strlen(p) == 3 &&
193 __builtin_strlen(p + 1) == 2 &&
194 __builtin_strlen(p + 2) == 1 &&
195 __builtin_strlen(p + 3) == 0 &&
196 __builtin_strlen(p + 4) == 4 &&
197 __builtin_strlen(p + 5) == 3 &&
198 __builtin_strlen(p + 6) == 2 &&
199 __builtin_strlen(p + 7) == 1 &&
200 __builtin_strlen(p + 8) == 0;
201 }
202
203 static_assert(check(a), "");
204 static_assert(check(b), "");
205 static_assert(check(c), "");
206
207 constexpr int over1 = __builtin_strlen(a + 9); // both-error {{constant expression}} \
208 // both-note {{one-past-the-end}}
209 constexpr int over2 = __builtin_strlen(b + 9); // both-error {{constant expression}} \
210 // both-note {{one-past-the-end}}
211 constexpr int over3 = __builtin_strlen(c + 9); // both-error {{constant expression}} \
212 // both-note {{one-past-the-end}}
213
214 constexpr int under1 = __builtin_strlen(a - 1); // both-error {{constant expression}} \
215 // both-note {{cannot refer to element -1}}
216 constexpr int under2 = __builtin_strlen(b - 1); // both-error {{constant expression}} \
217 // both-note {{cannot refer to element -1}}
218 constexpr int under3 = __builtin_strlen(c - 1); // both-error {{constant expression}} \
219 // both-note {{cannot refer to element -1}}
220
221 constexpr char d[] = { 'f', 'o', 'o' }; // no nul terminator.
222 constexpr int bad = __builtin_strlen(d); // both-error {{constant expression}} \
223 // both-note {{one-past-the-end}}
224
225 constexpr int wn = __builtin_wcslen(L"hello");
226 static_assert(wn == 5);
227 constexpr int wm = wcslen(L"hello"); // both-error {{constant expression}} \
228 // both-note {{non-constexpr function 'wcslen' cannot be used in a constant expression}}
229
230 int arr[3]; // both-note {{here}}
231 int wk = arr[wcslen(L"hello")]; // both-warning {{array index 5}}
232 }
233
234 namespace nan {
235 constexpr double NaN1 = __builtin_nan("");
236
237 /// The current interpreter does not accept this, but it should.
238 constexpr float NaN2 = __builtin_nans([](){return "0xAE98";}()); // ref-error {{must be initialized by a constant expression}}
239 #if __cplusplus < 201703L
240 // expected-error@-2 {{must be initialized by a constant expression}}
241 #endif
242
243 constexpr double NaN3 = __builtin_nan("foo"); // both-error {{must be initialized by a constant expression}}
244 constexpr float NaN4 = __builtin_nanf("");
245 constexpr long double NaN5 = __builtin_nanf128("");
246
247 /// FIXME: This should be accepted by the current interpreter as well.
248 constexpr char f[] = {'0', 'x', 'A', 'E', '\0'};
249 constexpr double NaN6 = __builtin_nan(f); // ref-error {{must be initialized by a constant expression}}
250
251 /// FIXME: Current interpreter misses diagnostics.
252 constexpr char f2[] = {'0', 'x', 'A', 'E'}; /// No trailing 0 byte.
253 constexpr double NaN7 = __builtin_nan(f2); // both-error {{must be initialized by a constant expression}} \
254 // expected-note {{read of dereferenced one-past-the-end pointer}}
255 static_assert(!__builtin_issignaling(__builtin_nan("")), "");
256 static_assert(__builtin_issignaling(__builtin_nans("")), "");
257 }
258
259 namespace fmin {
260 constexpr float f1 = __builtin_fmin(1.0, 2.0f);
261 static_assert(f1 == 1.0f, "");
262
263 constexpr float min = __builtin_fmin(__builtin_nan(""), 1);
264 static_assert(min == 1, "");
265 constexpr float min2 = __builtin_fmin(1, __builtin_nan(""));
266 static_assert(min2 == 1, "");
267 constexpr float min3 = __builtin_fmin(__builtin_inf(), __builtin_nan(""));
268 static_assert(min3 == __builtin_inf(), "");
269 }
270
271 namespace inf {
272 static_assert(__builtin_isinf(__builtin_inf()), "");
273 static_assert(!__builtin_isinf(1.0), "");
274
275 static_assert(__builtin_isfinite(1.0), "");
276 static_assert(!__builtin_isfinite(__builtin_inf()), "");
277
278 static_assert(__builtin_isnormal(1.0), "");
279 static_assert(!__builtin_isnormal(__builtin_inf()), "");
280
281 #ifndef __AVR__
282 static_assert(__builtin_issubnormal(0x1p-1070), "");
283 #endif
284 static_assert(!__builtin_issubnormal(__builtin_inf()), "");
285
286 static_assert(__builtin_iszero(0.0), "");
287 static_assert(!__builtin_iszero(__builtin_inf()), "");
288
289 static_assert(__builtin_issignaling(__builtin_nans("")), "");
290 static_assert(!__builtin_issignaling(__builtin_inf()), "");
291 }
292
293 namespace isfpclass {
294 char isfpclass_inf_pos_0[__builtin_isfpclass(__builtin_inf(), 0x0200) ? 1 : -1]; // fcPosInf
295 char isfpclass_inf_pos_1[!__builtin_isfpclass(__builtin_inff(), 0x0004) ? 1 : -1]; // fcNegInf
296 char isfpclass_inf_pos_2[__builtin_isfpclass(__builtin_infl(), 0x0207) ? 1 : -1]; // fcSNan|fcQNan|fcNegInf|fcPosInf
297 char isfpclass_inf_pos_3[!__builtin_isfpclass(__builtin_inf(), 0x01F8) ? 1 : -1]; // fcFinite
298 char isfpclass_pos_0 [__builtin_isfpclass(1.0, 0x0100) ? 1 : -1]; // fcPosNormal
299 char isfpclass_pos_1 [!__builtin_isfpclass(1.0f, 0x0008) ? 1 : -1]; // fcNegNormal
300 char isfpclass_pos_2 [__builtin_isfpclass(1.0L, 0x01F8) ? 1 : -1]; // fcFinite
301 char isfpclass_pos_3 [!__builtin_isfpclass(1.0, 0x0003) ? 1 : -1]; // fcSNan|fcQNan
302 #ifndef __AVR__
303 char isfpclass_pdenorm_0[__builtin_isfpclass(1.0e-40f, 0x0080) ? 1 : -1]; // fcPosSubnormal
304 char isfpclass_pdenorm_1[__builtin_isfpclass(1.0e-310, 0x01F8) ? 1 : -1]; // fcFinite
305 char isfpclass_pdenorm_2[!__builtin_isfpclass(1.0e-40f, 0x003C) ? 1 : -1]; // fcNegative
306 char isfpclass_pdenorm_3[!__builtin_isfpclass(1.0e-310, 0x0207) ? 1 : -1]; // ~fcFinite
307 #endif
308 char isfpclass_pzero_0 [__builtin_isfpclass(0.0f, 0x0060) ? 1 : -1]; // fcZero
309 char isfpclass_pzero_1 [__builtin_isfpclass(0.0, 0x01F8) ? 1 : -1]; // fcFinite
310 char isfpclass_pzero_2 [!__builtin_isfpclass(0.0L, 0x0020) ? 1 : -1]; // fcNegZero
311 char isfpclass_pzero_3 [!__builtin_isfpclass(0.0, 0x0003) ? 1 : -1]; // fcNan
312 char isfpclass_nzero_0 [__builtin_isfpclass(-0.0f, 0x0060) ? 1 : -1]; // fcZero
313 char isfpclass_nzero_1 [__builtin_isfpclass(-0.0, 0x01F8) ? 1 : -1]; // fcFinite
314 char isfpclass_nzero_2 [!__builtin_isfpclass(-0.0L, 0x0040) ? 1 : -1]; // fcPosZero
315 char isfpclass_nzero_3 [!__builtin_isfpclass(-0.0, 0x0003) ? 1 : -1]; // fcNan
316 char isfpclass_ndenorm_0[__builtin_isfpclass(-1.0e-40f, 0x0010) ? 1 : -1]; // fcNegSubnormal
317 char isfpclass_ndenorm_2[!__builtin_isfpclass(-1.0e-40f, 0x03C0) ? 1 : -1]; // fcPositive
318 #ifndef __AVR__
319 char isfpclass_ndenorm_1[__builtin_isfpclass(-1.0e-310, 0x01F8) ? 1 : -1]; // fcFinite
320 char isfpclass_ndenorm_3[!__builtin_isfpclass(-1.0e-310, 0x0207) ? 1 : -1]; // ~fcFinite
321 #endif
322 char isfpclass_neg_0 [__builtin_isfpclass(-1.0, 0x0008) ? 1 : -1]; // fcNegNormal
323 char isfpclass_neg_1 [!__builtin_isfpclass(-1.0f, 0x00100) ? 1 : -1]; // fcPosNormal
324 char isfpclass_neg_2 [__builtin_isfpclass(-1.0L, 0x01F8) ? 1 : -1]; // fcFinite
325 char isfpclass_neg_3 [!__builtin_isfpclass(-1.0, 0x0003) ? 1 : -1]; // fcSNan|fcQNan
326 char isfpclass_inf_neg_0[__builtin_isfpclass(-__builtin_inf(), 0x0004) ? 1 : -1]; // fcNegInf
327 char isfpclass_inf_neg_1[!__builtin_isfpclass(-__builtin_inff(), 0x0200) ? 1 : -1]; // fcPosInf
328 char isfpclass_inf_neg_2[__builtin_isfpclass(-__builtin_infl(), 0x0207) ? 1 : -1]; // ~fcFinite
329 char isfpclass_inf_neg_3[!__builtin_isfpclass(-__builtin_inf(), 0x03C0) ? 1 : -1]; // fcPositive
330 char isfpclass_qnan_0 [__builtin_isfpclass(__builtin_nan(""), 0x0002) ? 1 : -1]; // fcQNan
331 char isfpclass_qnan_1 [!__builtin_isfpclass(__builtin_nanf(""), 0x0001) ? 1 : -1]; // fcSNan
332 char isfpclass_qnan_2 [__builtin_isfpclass(__builtin_nanl(""), 0x0207) ? 1 : -1]; // ~fcFinite
333 char isfpclass_qnan_3 [!__builtin_isfpclass(__builtin_nan(""), 0x01F8) ? 1 : -1]; // fcFinite
334 char isfpclass_snan_0 [__builtin_isfpclass(__builtin_nansf(""), 0x0001) ? 1 : -1]; // fcSNan
335 char isfpclass_snan_1 [!__builtin_isfpclass(__builtin_nans(""), 0x0002) ? 1 : -1]; // fcQNan
336 char isfpclass_snan_2 [__builtin_isfpclass(__builtin_nansl(""), 0x0207) ? 1 : -1]; // ~fcFinite
337 char isfpclass_snan_3 [!__builtin_isfpclass(__builtin_nans(""), 0x01F8) ? 1 : -1]; // fcFinite
338 }
339
340 namespace signbit {
341 static_assert(
342 !__builtin_signbit(1.0) && __builtin_signbit(-1.0) && !__builtin_signbit(0.0) && __builtin_signbit(-0.0) &&
343 !__builtin_signbitf(1.0f) && __builtin_signbitf(-1.0f) && !__builtin_signbitf(0.0f) && __builtin_signbitf(-0.0f) &&
344 !__builtin_signbitl(1.0L) && __builtin_signbitf(-1.0L) && !__builtin_signbitf(0.0L) && __builtin_signbitf(-0.0L) &&
345 !__builtin_signbit(1.0f) && __builtin_signbit(-1.0f) && !__builtin_signbit(0.0f) && __builtin_signbit(-0.0f) &&
346 !__builtin_signbit(1.0L) && __builtin_signbit(-1.0L) && !__builtin_signbit(0.0L) && __builtin_signbit(-0.0L) &&
347 true, ""
348 );
349 }
350
351 namespace floating_comparison {
352 #define LESS(X, Y) \
353 !__builtin_isgreater(X, Y) && __builtin_isgreater(Y, X) && \
354 !__builtin_isgreaterequal(X, Y) && __builtin_isgreaterequal(Y, X) && \
355 __builtin_isless(X, Y) && !__builtin_isless(Y, X) && \
356 __builtin_islessequal(X, Y) && !__builtin_islessequal(Y, X) && \
357 __builtin_islessgreater(X, Y) && __builtin_islessgreater(Y, X) && \
358 !__builtin_isunordered(X, Y) && !__builtin_isunordered(Y, X)
359 #define EQUAL(X, Y) \
360 !__builtin_isgreater(X, Y) && !__builtin_isgreater(Y, X) && \
361 __builtin_isgreaterequal(X, Y) && __builtin_isgreaterequal(Y, X) && \
362 !__builtin_isless(X, Y) && !__builtin_isless(Y, X) && \
363 __builtin_islessequal(X, Y) && __builtin_islessequal(Y, X) && \
364 !__builtin_islessgreater(X, Y) && !__builtin_islessgreater(Y, X) && \
365 !__builtin_isunordered(X, Y) && !__builtin_isunordered(Y, X)
366 #define UNORDERED(X, Y) \
367 !__builtin_isgreater(X, Y) && !__builtin_isgreater(Y, X) && \
368 !__builtin_isgreaterequal(X, Y) && !__builtin_isgreaterequal(Y, X) && \
369 !__builtin_isless(X, Y) && !__builtin_isless(Y, X) && \
370 !__builtin_islessequal(X, Y) && !__builtin_islessequal(Y, X) && \
371 !__builtin_islessgreater(X, Y) && !__builtin_islessgreater(Y, X) && \
372 __builtin_isunordered(X, Y) && __builtin_isunordered(Y, X)
373
374 static_assert(LESS(0.0, 1.0));
375 static_assert(LESS(0.0, __builtin_inf()));
376 static_assert(LESS(0.0f, 1.0f));
377 static_assert(LESS(0.0f, __builtin_inff()));
378 static_assert(LESS(0.0L, 1.0L));
379 static_assert(LESS(0.0L, __builtin_infl()));
380
381 static_assert(EQUAL(1.0, 1.0));
382 static_assert(EQUAL(0.0, -0.0));
383 static_assert(EQUAL(1.0f, 1.0f));
384 static_assert(EQUAL(0.0f, -0.0f));
385 static_assert(EQUAL(1.0L, 1.0L));
386 static_assert(EQUAL(0.0L, -0.0L));
387
388 static_assert(UNORDERED(__builtin_nan(""), 1.0));
389 static_assert(UNORDERED(__builtin_nan(""), __builtin_inf()));
390 static_assert(UNORDERED(__builtin_nanf(""), 1.0f));
391 static_assert(UNORDERED(__builtin_nanf(""), __builtin_inff()));
392 static_assert(UNORDERED(__builtin_nanl(""), 1.0L));
393 static_assert(UNORDERED(__builtin_nanl(""), __builtin_infl()));
394 }
395
396 namespace fpclassify {
397 char classify_nan [__builtin_fpclassify(+1, -1, -1, -1, -1, __builtin_nan(""))];
398 char classify_snan [__builtin_fpclassify(+1, -1, -1, -1, -1, __builtin_nans(""))];
399 char classify_inf [__builtin_fpclassify(-1, +1, -1, -1, -1, __builtin_inf())];
400 char classify_neg_inf [__builtin_fpclassify(-1, +1, -1, -1, -1, -__builtin_inf())];
401 char classify_normal [__builtin_fpclassify(-1, -1, +1, -1, -1, 1.539)];
402 #ifndef __AVR__
403 char classify_normal2 [__builtin_fpclassify(-1, -1, +1, -1, -1, 1e-307)];
404 char classify_denorm [__builtin_fpclassify(-1, -1, -1, +1, -1, 1e-308)];
405 char classify_denorm2 [__builtin_fpclassify(-1, -1, -1, +1, -1, -1e-308)];
406 #endif
407 char classify_zero [__builtin_fpclassify(-1, -1, -1, -1, +1, 0.0)];
408 char classify_neg_zero[__builtin_fpclassify(-1, -1, -1, -1, +1, -0.0)];
409 char classify_subnorm [__builtin_fpclassify(-1, -1, -1, +1, -1, 1.0e-38f)];
410 }
411
412 namespace abs {
413 static_assert(__builtin_abs(14) == 14, "");
414 static_assert(__builtin_labs(14L) == 14L, "");
415 static_assert(__builtin_llabs(14LL) == 14LL, "");
416 static_assert(__builtin_abs(-14) == 14, "");
417 static_assert(__builtin_labs(-0x14L) == 0x14L, "");
418 static_assert(__builtin_llabs(-0x141414141414LL) == 0x141414141414LL, "");
419 #define BITSIZE(x) (sizeof(x) * 8)
420 constexpr int abs4 = __builtin_abs(1 << (BITSIZE(int) - 1)); // both-error {{must be initialized by a constant expression}}
421 constexpr long abs6 = __builtin_labs(1L << (BITSIZE(long) - 1)); // both-error {{must be initialized by a constant expression}}
422 constexpr long long abs8 = __builtin_llabs(1LL << (BITSIZE(long long) - 1)); // both-error {{must be initialized by a constant expression}}
423 #undef BITSIZE
424 } // namespace abs
425
426 namespace fabs {
427 static_assert(__builtin_fabs(-14.0) == 14.0, "");
428 }
429
430 namespace std {
431 struct source_location {
432 struct __impl {
433 unsigned int _M_line;
434 const char *_M_file_name;
435 signed char _M_column;
436 const char *_M_function_name;
437 };
438 using BuiltinT = decltype(__builtin_source_location()); // OK.
439 };
440 }
441
442 namespace SourceLocation {
443 constexpr auto A = __builtin_source_location();
444 static_assert(A->_M_line == __LINE__ -1, "");
445 static_assert(A->_M_column == 22, "");
446 static_assert(__builtin_strcmp(A->_M_function_name, "") == 0, "");
447 static_assert(__builtin_strcmp(A->_M_file_name, __FILE__) == 0, "");
448
449 static_assert(__builtin_LINE() == __LINE__, "");
450
451 struct Foo {
452 int a = __builtin_LINE();
453 };
454
455 static_assert(Foo{}.a == __LINE__, "");
456
457 struct AA {
458 int n = __builtin_LINE();
459 };
460 struct B {
461 AA a = {};
462 };
463 constexpr void f() {
464 constexpr B c = {};
465 static_assert(c.a.n == __LINE__ - 1, "");
466 }
467 }
468
469 #define BITSIZE(x) (sizeof(x) * 8)
470 constexpr bool __attribute__((ext_vector_type(4))) v4b{};
471 namespace popcount {
472 static_assert(__builtin_popcount(~0u) == __CHAR_BIT__ * sizeof(unsigned int), "");
473 static_assert(__builtin_popcount(0) == 0, "");
474 static_assert(__builtin_popcountl(~0ul) == __CHAR_BIT__ * sizeof(unsigned long), "");
475 static_assert(__builtin_popcountl(0) == 0, "");
476 static_assert(__builtin_popcountll(~0ull) == __CHAR_BIT__ * sizeof(unsigned long long), "");
477 static_assert(__builtin_popcountll(0) == 0, "");
478 static_assert(__builtin_popcountg((unsigned char)~0) == __CHAR_BIT__ * sizeof(unsigned char), "");
479 static_assert(__builtin_popcountg((unsigned char)0) == 0, "");
480 static_assert(__builtin_popcountg((unsigned short)~0) == __CHAR_BIT__ * sizeof(unsigned short), "");
481 static_assert(__builtin_popcountg((unsigned short)0) == 0, "");
482 static_assert(__builtin_popcountg(~0u) == __CHAR_BIT__ * sizeof(unsigned int), "");
483 static_assert(__builtin_popcountg(0u) == 0, "");
484 static_assert(__builtin_popcountg(~0ul) == __CHAR_BIT__ * sizeof(unsigned long), "");
485 static_assert(__builtin_popcountg(0ul) == 0, "");
486 static_assert(__builtin_popcountg(~0ull) == __CHAR_BIT__ * sizeof(unsigned long long), "");
487 static_assert(__builtin_popcountg(0ull) == 0, "");
488 static_assert(__builtin_popcountg(v4b) == 0, "");
489 #ifdef __SIZEOF_INT128__
490 static_assert(__builtin_popcountg(~(unsigned __int128)0) == __CHAR_BIT__ * sizeof(unsigned __int128), "");
491 static_assert(__builtin_popcountg((unsigned __int128)0) == 0, "");
492 #endif
493 #ifndef __AVR__
494 static_assert(__builtin_popcountg(~(unsigned _BitInt(128))0) == __CHAR_BIT__ * sizeof(unsigned _BitInt(128)), "");
495 static_assert(__builtin_popcountg((unsigned _BitInt(128))0) == 0, "");
496 #endif
497
498 /// From test/Sema/constant-builtins-2.c
499 char popcount1[__builtin_popcount(0) == 0 ? 1 : -1];
500 char popcount2[__builtin_popcount(0xF0F0) == 8 ? 1 : -1];
501 char popcount3[__builtin_popcount(~0) == BITSIZE(int) ? 1 : -1];
502 char popcount4[__builtin_popcount(~0L) == BITSIZE(int) ? 1 : -1];
503 char popcount5[__builtin_popcountl(0L) == 0 ? 1 : -1];
504 char popcount6[__builtin_popcountl(0xF0F0L) == 8 ? 1 : -1];
505 char popcount7[__builtin_popcountl(~0L) == BITSIZE(long) ? 1 : -1];
506 char popcount8[__builtin_popcountll(0LL) == 0 ? 1 : -1];
507 char popcount9[__builtin_popcountll(0xF0F0LL) == 8 ? 1 : -1];
508 char popcount10[__builtin_popcountll(~0LL) == BITSIZE(long long) ? 1 : -1];
509 char popcount11[__builtin_popcountg(0U) == 0 ? 1 : -1];
510 char popcount12[__builtin_popcountg(0xF0F0U) == 8 ? 1 : -1];
511 char popcount13[__builtin_popcountg(~0U) == BITSIZE(int) ? 1 : -1];
512 char popcount14[__builtin_popcountg(~0UL) == BITSIZE(long) ? 1 : -1];
513 char popcount15[__builtin_popcountg(~0ULL) == BITSIZE(long long) ? 1 : -1];
514 #ifdef __SIZEOF_INT128__
515 char popcount16[__builtin_popcountg(~(unsigned __int128)0) == BITSIZE(__int128) ? 1 : -1];
516 #endif
517 #ifndef __AVR__
518 char popcount17[__builtin_popcountg(~(unsigned _BitInt(128))0) == BITSIZE(_BitInt(128)) ? 1 : -1];
519 #endif
520 }
521
522 namespace parity {
523 /// From test/Sema/constant-builtins-2.c
524 char parity1[__builtin_parity(0) == 0 ? 1 : -1];
525 char parity2[__builtin_parity(0xb821) == 0 ? 1 : -1];
526 char parity3[__builtin_parity(0xb822) == 0 ? 1 : -1];
527 char parity4[__builtin_parity(0xb823) == 1 ? 1 : -1];
528 char parity5[__builtin_parity(0xb824) == 0 ? 1 : -1];
529 char parity6[__builtin_parity(0xb825) == 1 ? 1 : -1];
530 char parity7[__builtin_parity(0xb826) == 1 ? 1 : -1];
531 char parity8[__builtin_parity(~0) == 0 ? 1 : -1];
532 char parity9[__builtin_parityl(1L << (BITSIZE(long) - 1)) == 1 ? 1 : -1];
533 char parity10[__builtin_parityll(1LL << (BITSIZE(long long) - 1)) == 1 ? 1 : -1];
534 }
535
536 namespace clrsb {
537 char clrsb1[__builtin_clrsb(0) == BITSIZE(int) - 1 ? 1 : -1];
538 char clrsb2[__builtin_clrsbl(0L) == BITSIZE(long) - 1 ? 1 : -1];
539 char clrsb3[__builtin_clrsbll(0LL) == BITSIZE(long long) - 1 ? 1 : -1];
540 char clrsb4[__builtin_clrsb(~0) == BITSIZE(int) - 1 ? 1 : -1];
541 char clrsb5[__builtin_clrsbl(~0L) == BITSIZE(long) - 1 ? 1 : -1];
542 char clrsb6[__builtin_clrsbll(~0LL) == BITSIZE(long long) - 1 ? 1 : -1];
543 char clrsb7[__builtin_clrsb(1) == BITSIZE(int) - 2 ? 1 : -1];
544 char clrsb8[__builtin_clrsb(~1) == BITSIZE(int) - 2 ? 1 : -1];
545 char clrsb9[__builtin_clrsb(1 << (BITSIZE(int) - 1)) == 0 ? 1 : -1];
546 char clrsb10[__builtin_clrsb(~(1 << (BITSIZE(int) - 1))) == 0 ? 1 : -1];
547 char clrsb11[__builtin_clrsb(0xf) == BITSIZE(int) - 5 ? 1 : -1];
548 char clrsb12[__builtin_clrsb(~0x1f) == BITSIZE(int) - 6 ? 1 : -1];
549 }
550
551 namespace bitreverse {
552 char bitreverse1[__builtin_bitreverse8(0x01) == 0x80 ? 1 : -1];
553 char bitreverse2[__builtin_bitreverse16(0x3C48) == 0x123C ? 1 : -1];
554 char bitreverse3[__builtin_bitreverse32(0x12345678) == 0x1E6A2C48 ? 1 : -1];
555 char bitreverse4[__builtin_bitreverse64(0x0123456789ABCDEFULL) == 0xF7B3D591E6A2C480 ? 1 : -1];
556 }
557
558 namespace expect {
559 constexpr int a() {
560 return 12;
561 }
562 static_assert(__builtin_expect(a(),1) == 12, "");
563 static_assert(__builtin_expect_with_probability(a(), 1, 1.0) == 12, "");
564 }
565
566 namespace rotateleft {
567 char rotateleft1[__builtin_rotateleft8(0x01, 5) == 0x20 ? 1 : -1];
568 char rotateleft2[__builtin_rotateleft16(0x3210, 11) == 0x8190 ? 1 : -1];
569 char rotateleft3[__builtin_rotateleft32(0x76543210, 22) == 0x841D950C ? 1 : -1];
570 char rotateleft4[__builtin_rotateleft64(0xFEDCBA9876543210ULL, 55) == 0x87F6E5D4C3B2A19ULL ? 1 : -1];
571 }
572
573 namespace rotateright {
574 char rotateright1[__builtin_rotateright8(0x01, 5) == 0x08 ? 1 : -1];
575 char rotateright2[__builtin_rotateright16(0x3210, 11) == 0x4206 ? 1 : -1];
576 char rotateright3[__builtin_rotateright32(0x76543210, 22) == 0x50C841D9 ? 1 : -1];
577 char rotateright4[__builtin_rotateright64(0xFEDCBA9876543210ULL, 55) == 0xB97530ECA86421FDULL ? 1 : -1];
578 }
579
580 namespace ffs {
581 char ffs1[__builtin_ffs(0) == 0 ? 1 : -1];
582 char ffs2[__builtin_ffs(1) == 1 ? 1 : -1];
583 char ffs3[__builtin_ffs(0xfbe71) == 1 ? 1 : -1];
584 char ffs4[__builtin_ffs(0xfbe70) == 5 ? 1 : -1];
585 char ffs5[__builtin_ffs(1U << (BITSIZE(int) - 1)) == BITSIZE(int) ? 1 : -1];
586 char ffs6[__builtin_ffsl(0x10L) == 5 ? 1 : -1];
587 char ffs7[__builtin_ffsll(0x100LL) == 9 ? 1 : -1];
588 }
589
590 namespace EhReturnDataRegno {
591 void test11(int X) {
592 switch (X) {
593 case __builtin_eh_return_data_regno(0): // constant foldable.
594 break;
595 }
596 __builtin_eh_return_data_regno(X); // both-error {{argument to '__builtin_eh_return_data_regno' must be a constant integer}}
597 }
598 }
599
600 /// From test/SemaCXX/builtins.cpp
601 namespace test_launder {
602 #define TEST_TYPE(Ptr, Type) \
603 static_assert(__is_same(decltype(__builtin_launder(Ptr)), Type), "expected same type")
604
605 struct Dummy {};
606
607 using FnType = int(char);
608 using MemFnType = int (Dummy::*)(char);
609 using ConstMemFnType = int (Dummy::*)() const;
610
611 void foo() {}
612
613 void test_builtin_launder_diags(void *vp, const void *cvp, FnType *fnp,
614 MemFnType mfp, ConstMemFnType cmfp, int (&Arr)[5]) {
615 __builtin_launder(vp); // both-error {{void pointer argument to '__builtin_launder' is not allowed}}
616 __builtin_launder(cvp); // both-error {{void pointer argument to '__builtin_launder' is not allowed}}
617 __builtin_launder(fnp); // both-error {{function pointer argument to '__builtin_launder' is not allowed}}
618 __builtin_launder(mfp); // both-error {{non-pointer argument to '__builtin_launder' is not allowed}}
619 __builtin_launder(cmfp); // both-error {{non-pointer argument to '__builtin_launder' is not allowed}}
620 (void)__builtin_launder(&fnp);
621 __builtin_launder(42); // both-error {{non-pointer argument to '__builtin_launder' is not allowed}}
622 __builtin_launder(nullptr); // both-error {{non-pointer argument to '__builtin_launder' is not allowed}}
623 __builtin_launder(foo); // both-error {{function pointer argument to '__builtin_launder' is not allowed}}
624 (void)__builtin_launder(Arr);
625 }
626
627 void test_builtin_launder(char *p, const volatile int *ip, const float *&fp,
628 double *__restrict dp) {
629 int x;
630 __builtin_launder(x); // both-error {{non-pointer argument to '__builtin_launder' is not allowed}}
631
632 TEST_TYPE(p, char*);
633 TEST_TYPE(ip, const volatile int*);
634 TEST_TYPE(fp, const float*);
635 TEST_TYPE(dp, double *__restrict);
636
637 char *d = __builtin_launder(p);
638 const volatile int *id = __builtin_launder(ip);
639 int *id2 = __builtin_launder(ip); // both-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const volatile int *'}}
640 const float* fd = __builtin_launder(fp);
641 }
642
643 void test_launder_return_type(const int (&ArrayRef)[101], int (&MArrRef)[42][13],
644 void (**&FuncPtrRef)()) {
645 TEST_TYPE(ArrayRef, const int *);
646 TEST_TYPE(MArrRef, int(*)[13]);
647 TEST_TYPE(FuncPtrRef, void (**)());
648 }
649
650 template <class Tp>
651 constexpr Tp *test_constexpr_launder(Tp *tp) {
652 return __builtin_launder(tp);
653 }
654 constexpr int const_int = 42;
655 constexpr int const_int2 = 101;
656 constexpr const int *const_ptr = test_constexpr_launder(&const_int);
657 static_assert(&const_int == const_ptr, "");
658 static_assert(const_ptr != test_constexpr_launder(&const_int2), "");
659
660 void test_non_constexpr() {
661 constexpr int i = 42; // both-note {{address of non-static constexpr variable 'i' may differ on each invocation}}
662 constexpr const int *ip = __builtin_launder(&i); // both-error {{constexpr variable 'ip' must be initialized by a constant expression}}
663 // both-note@-1 {{pointer to 'i' is not a constant expression}}
664 }
665
666 constexpr bool test_in_constexpr(const int &i) {
667 return (__builtin_launder(&i) == &i);
668 }
669
670 static_assert(test_in_constexpr(const_int), "");
671 void f() {
672 constexpr int i = 42;
673 static_assert(test_in_constexpr(i), "");
674 }
675
676 struct Incomplete; // both-note {{forward declaration}}
677 struct IncompleteMember {
678 Incomplete &i;
679 };
680 void test_incomplete(Incomplete *i, IncompleteMember *im) {
681 // both-error@+1 {{incomplete type 'Incomplete' where a complete type is required}}
682 __builtin_launder(i);
683 __builtin_launder(&i); // OK
684 __builtin_launder(im); // OK
685 }
686
687 void test_noexcept(int *i) {
688 static_assert(noexcept(__builtin_launder(i)), "");
689 }
690 #undef TEST_TYPE
691 } // end namespace test_launder
692
693
694 namespace clz {
695 char clz1[__builtin_clz(1) == BITSIZE(int) - 1 ? 1 : -1];
696 char clz2[__builtin_clz(7) == BITSIZE(int) - 3 ? 1 : -1];
697 char clz3[__builtin_clz(1 << (BITSIZE(int) - 1)) == 0 ? 1 : -1];
698 int clz4 = __builtin_clz(0);
699 char clz5[__builtin_clzl(0xFL) == BITSIZE(long) - 4 ? 1 : -1];
700 char clz6[__builtin_clzll(0xFFLL) == BITSIZE(long long) - 8 ? 1 : -1];
701 char clz7[__builtin_clzs(0x1) == BITSIZE(short) - 1 ? 1 : -1];
702 char clz8[__builtin_clzs(0xf) == BITSIZE(short) - 4 ? 1 : -1];
703 char clz9[__builtin_clzs(0xfff) == BITSIZE(short) - 12 ? 1 : -1];
704
705 int clz10 = __builtin_clzg((unsigned char)0);
706 char clz11[__builtin_clzg((unsigned char)0, 42) == 42 ? 1 : -1];
707 char clz12[__builtin_clzg((unsigned char)0x1) == BITSIZE(char) - 1 ? 1 : -1];
708 char clz13[__builtin_clzg((unsigned char)0x1, 42) == BITSIZE(char) - 1 ? 1 : -1];
709 char clz14[__builtin_clzg((unsigned char)0xf) == BITSIZE(char) - 4 ? 1 : -1];
710 char clz15[__builtin_clzg((unsigned char)0xf, 42) == BITSIZE(char) - 4 ? 1 : -1];
711 char clz16[__builtin_clzg((unsigned char)(1 << (BITSIZE(char) - 1))) == 0 ? 1 : -1];
712 char clz17[__builtin_clzg((unsigned char)(1 << (BITSIZE(char) - 1)), 42) == 0 ? 1 : -1];
713 int clz18 = __builtin_clzg((unsigned short)0);
714 char clz19[__builtin_clzg((unsigned short)0, 42) == 42 ? 1 : -1];
715 char clz20[__builtin_clzg((unsigned short)0x1) == BITSIZE(short) - 1 ? 1 : -1];
716 char clz21[__builtin_clzg((unsigned short)0x1, 42) == BITSIZE(short) - 1 ? 1 : -1];
717 char clz22[__builtin_clzg((unsigned short)0xf) == BITSIZE(short) - 4 ? 1 : -1];
718 char clz23[__builtin_clzg((unsigned short)0xf, 42) == BITSIZE(short) - 4 ? 1 : -1];
719 char clz24[__builtin_clzg((unsigned short)(1 << (BITSIZE(short) - 1))) == 0 ? 1 : -1];
720 char clz25[__builtin_clzg((unsigned short)(1 << (BITSIZE(short) - 1)), 42) == 0 ? 1 : -1];
721 int clz26 = __builtin_clzg(0U);
722 char clz27[__builtin_clzg(0U, 42) == 42 ? 1 : -1];
723 char clz28[__builtin_clzg(0x1U) == BITSIZE(int) - 1 ? 1 : -1];
724 char clz29[__builtin_clzg(0x1U, 42) == BITSIZE(int) - 1 ? 1 : -1];
725 char clz30[__builtin_clzg(0xfU) == BITSIZE(int) - 4 ? 1 : -1];
726 char clz31[__builtin_clzg(0xfU, 42) == BITSIZE(int) - 4 ? 1 : -1];
727 char clz32[__builtin_clzg(1U << (BITSIZE(int) - 1)) == 0 ? 1 : -1];
728 char clz33[__builtin_clzg(1U << (BITSIZE(int) - 1), 42) == 0 ? 1 : -1];
729 int clz34 = __builtin_clzg(0UL);
730 char clz35[__builtin_clzg(0UL, 42) == 42 ? 1 : -1];
731 char clz36[__builtin_clzg(0x1UL) == BITSIZE(long) - 1 ? 1 : -1];
732 char clz37[__builtin_clzg(0x1UL, 42) == BITSIZE(long) - 1 ? 1 : -1];
733 char clz38[__builtin_clzg(0xfUL) == BITSIZE(long) - 4 ? 1 : -1];
734 char clz39[__builtin_clzg(0xfUL, 42) == BITSIZE(long) - 4 ? 1 : -1];
735 char clz40[__builtin_clzg(1UL << (BITSIZE(long) - 1)) == 0 ? 1 : -1];
736 char clz41[__builtin_clzg(1UL << (BITSIZE(long) - 1), 42) == 0 ? 1 : -1];
737 int clz42 = __builtin_clzg(0ULL);
738 char clz43[__builtin_clzg(0ULL, 42) == 42 ? 1 : -1];
739 char clz44[__builtin_clzg(0x1ULL) == BITSIZE(long long) - 1 ? 1 : -1];
740 char clz45[__builtin_clzg(0x1ULL, 42) == BITSIZE(long long) - 1 ? 1 : -1];
741 char clz46[__builtin_clzg(0xfULL) == BITSIZE(long long) - 4 ? 1 : -1];
742 char clz47[__builtin_clzg(0xfULL, 42) == BITSIZE(long long) - 4 ? 1 : -1];
743 char clz48[__builtin_clzg(1ULL << (BITSIZE(long long) - 1)) == 0 ? 1 : -1];
744 char clz49[__builtin_clzg(1ULL << (BITSIZE(long long) - 1), 42) == 0 ? 1 : -1];
745 #ifdef __SIZEOF_INT128__
746 int clz50 = __builtin_clzg((unsigned __int128)0);
747 char clz51[__builtin_clzg((unsigned __int128)0, 42) == 42 ? 1 : -1];
748 char clz52[__builtin_clzg((unsigned __int128)0x1) == BITSIZE(__int128) - 1 ? 1 : -1];
749 char clz53[__builtin_clzg((unsigned __int128)0x1, 42) == BITSIZE(__int128) - 1 ? 1 : -1];
750 char clz54[__builtin_clzg((unsigned __int128)0xf) == BITSIZE(__int128) - 4 ? 1 : -1];
751 char clz55[__builtin_clzg((unsigned __int128)0xf, 42) == BITSIZE(__int128) - 4 ? 1 : -1];
752 #endif
753 #ifndef __AVR__
754 int clz58 = __builtin_clzg((unsigned _BitInt(128))0);
755 char clz59[__builtin_clzg((unsigned _BitInt(128))0, 42) == 42 ? 1 : -1];
756 char clz60[__builtin_clzg((unsigned _BitInt(128))0x1) == BITSIZE(_BitInt(128)) - 1 ? 1 : -1];
757 char clz61[__builtin_clzg((unsigned _BitInt(128))0x1, 42) == BITSIZE(_BitInt(128)) - 1 ? 1 : -1];
758 char clz62[__builtin_clzg((unsigned _BitInt(128))0xf) == BITSIZE(_BitInt(128)) - 4 ? 1 : -1];
759 char clz63[__builtin_clzg((unsigned _BitInt(128))0xf, 42) == BITSIZE(_BitInt(128)) - 4 ? 1 : -1];
760 #endif
761 char clz64[__builtin_clzg(v4b, 0) == 0 ? 1 : -1];
762 }
763
764 namespace ctz {
765 char ctz1[__builtin_ctz(1) == 0 ? 1 : -1];
766 char ctz2[__builtin_ctz(8) == 3 ? 1 : -1];
767 char ctz3[__builtin_ctz(1 << (BITSIZE(int) - 1)) == BITSIZE(int) - 1 ? 1 : -1];
768 int ctz4 = __builtin_ctz(0);
769 char ctz5[__builtin_ctzl(0x10L) == 4 ? 1 : -1];
770 char ctz6[__builtin_ctzll(0x100LL) == 8 ? 1 : -1];
771 char ctz7[__builtin_ctzs(1 << (BITSIZE(short) - 1)) == BITSIZE(short) - 1 ? 1 : -1];
772 int ctz8 = __builtin_ctzg((unsigned char)0);
773 char ctz9[__builtin_ctzg((unsigned char)0, 42) == 42 ? 1 : -1];
774 char ctz10[__builtin_ctzg((unsigned char)0x1) == 0 ? 1 : -1];
775 char ctz11[__builtin_ctzg((unsigned char)0x1, 42) == 0 ? 1 : -1];
776 char ctz12[__builtin_ctzg((unsigned char)0x10) == 4 ? 1 : -1];
777 char ctz13[__builtin_ctzg((unsigned char)0x10, 42) == 4 ? 1 : -1];
778 char ctz14[__builtin_ctzg((unsigned char)(1 << (BITSIZE(char) - 1))) == BITSIZE(char) - 1 ? 1 : -1];
779 char ctz15[__builtin_ctzg((unsigned char)(1 << (BITSIZE(char) - 1)), 42) == BITSIZE(char) - 1 ? 1 : -1];
780 int ctz16 = __builtin_ctzg((unsigned short)0);
781 char ctz17[__builtin_ctzg((unsigned short)0, 42) == 42 ? 1 : -1];
782 char ctz18[__builtin_ctzg((unsigned short)0x1) == 0 ? 1 : -1];
783 char ctz19[__builtin_ctzg((unsigned short)0x1, 42) == 0 ? 1 : -1];
784 char ctz20[__builtin_ctzg((unsigned short)0x10) == 4 ? 1 : -1];
785 char ctz21[__builtin_ctzg((unsigned short)0x10, 42) == 4 ? 1 : -1];
786 char ctz22[__builtin_ctzg((unsigned short)(1 << (BITSIZE(short) - 1))) == BITSIZE(short) - 1 ? 1 : -1];
787 char ctz23[__builtin_ctzg((unsigned short)(1 << (BITSIZE(short) - 1)), 42) == BITSIZE(short) - 1 ? 1 : -1];
788 int ctz24 = __builtin_ctzg(0U);
789 char ctz25[__builtin_ctzg(0U, 42) == 42 ? 1 : -1];
790 char ctz26[__builtin_ctzg(0x1U) == 0 ? 1 : -1];
791 char ctz27[__builtin_ctzg(0x1U, 42) == 0 ? 1 : -1];
792 char ctz28[__builtin_ctzg(0x10U) == 4 ? 1 : -1];
793 char ctz29[__builtin_ctzg(0x10U, 42) == 4 ? 1 : -1];
794 char ctz30[__builtin_ctzg(1U << (BITSIZE(int) - 1)) == BITSIZE(int) - 1 ? 1 : -1];
795 char ctz31[__builtin_ctzg(1U << (BITSIZE(int) - 1), 42) == BITSIZE(int) - 1 ? 1 : -1];
796 int ctz32 = __builtin_ctzg(0UL);
797 char ctz33[__builtin_ctzg(0UL, 42) == 42 ? 1 : -1];
798 char ctz34[__builtin_ctzg(0x1UL) == 0 ? 1 : -1];
799 char ctz35[__builtin_ctzg(0x1UL, 42) == 0 ? 1 : -1];
800 char ctz36[__builtin_ctzg(0x10UL) == 4 ? 1 : -1];
801 char ctz37[__builtin_ctzg(0x10UL, 42) == 4 ? 1 : -1];
802 char ctz38[__builtin_ctzg(1UL << (BITSIZE(long) - 1)) == BITSIZE(long) - 1 ? 1 : -1];
803 char ctz39[__builtin_ctzg(1UL << (BITSIZE(long) - 1), 42) == BITSIZE(long) - 1 ? 1 : -1];
804 int ctz40 = __builtin_ctzg(0ULL);
805 char ctz41[__builtin_ctzg(0ULL, 42) == 42 ? 1 : -1];
806 char ctz42[__builtin_ctzg(0x1ULL) == 0 ? 1 : -1];
807 char ctz43[__builtin_ctzg(0x1ULL, 42) == 0 ? 1 : -1];
808 char ctz44[__builtin_ctzg(0x10ULL) == 4 ? 1 : -1];
809 char ctz45[__builtin_ctzg(0x10ULL, 42) == 4 ? 1 : -1];
810 char ctz46[__builtin_ctzg(1ULL << (BITSIZE(long long) - 1)) == BITSIZE(long long) - 1 ? 1 : -1];
811 char ctz47[__builtin_ctzg(1ULL << (BITSIZE(long long) - 1), 42) == BITSIZE(long long) - 1 ? 1 : -1];
812 #ifdef __SIZEOF_INT128__
813 int ctz48 = __builtin_ctzg((unsigned __int128)0);
814 char ctz49[__builtin_ctzg((unsigned __int128)0, 42) == 42 ? 1 : -1];
815 char ctz50[__builtin_ctzg((unsigned __int128)0x1) == 0 ? 1 : -1];
816 char ctz51[__builtin_ctzg((unsigned __int128)0x1, 42) == 0 ? 1 : -1];
817 char ctz52[__builtin_ctzg((unsigned __int128)0x10) == 4 ? 1 : -1];
818 char ctz53[__builtin_ctzg((unsigned __int128)0x10, 42) == 4 ? 1 : -1];
819 char ctz54[__builtin_ctzg((unsigned __int128)1 << (BITSIZE(__int128) - 1)) == BITSIZE(__int128) - 1 ? 1 : -1];
820 char ctz55[__builtin_ctzg((unsigned __int128)1 << (BITSIZE(__int128) - 1), 42) == BITSIZE(__int128) - 1 ? 1 : -1];
821 #endif
822 #ifndef __AVR__
823 int ctz56 = __builtin_ctzg((unsigned _BitInt(128))0);
824 char ctz57[__builtin_ctzg((unsigned _BitInt(128))0, 42) == 42 ? 1 : -1];
825 char ctz58[__builtin_ctzg((unsigned _BitInt(128))0x1) == 0 ? 1 : -1];
826 char ctz59[__builtin_ctzg((unsigned _BitInt(128))0x1, 42) == 0 ? 1 : -1];
827 char ctz60[__builtin_ctzg((unsigned _BitInt(128))0x10) == 4 ? 1 : -1];
828 char ctz61[__builtin_ctzg((unsigned _BitInt(128))0x10, 42) == 4 ? 1 : -1];
829 char ctz62[__builtin_ctzg((unsigned _BitInt(128))1 << (BITSIZE(_BitInt(128)) - 1)) == BITSIZE(_BitInt(128)) - 1 ? 1 : -1];
830 char ctz63[__builtin_ctzg((unsigned _BitInt(128))1 << (BITSIZE(_BitInt(128)) - 1), 42) == BITSIZE(_BitInt(128)) - 1 ? 1 : -1];
831 #endif
832 char clz64[__builtin_ctzg(v4b, 0) == 0 ? 1 : -1];
833 }
834
835 namespace bswap {
836 extern int f(void);
837 int h3 = __builtin_bswap16(0x1234) == 0x3412 ? 1 : f();
838 int h4 = __builtin_bswap32(0x1234) == 0x34120000 ? 1 : f();
839 int h5 = __builtin_bswap64(0x1234) == 0x3412000000000000 ? 1 : f();
840 int h6 = __builtin_bswapg(0x12) == 0x12 ? 1 : f();
841 int h7 = __builtin_bswapg(0x1234) == 0x3412 ? 1 : f();
842 int h8 = __builtin_bswapg(0x00001234) == 0x34120000 ? 1 : f();
843 int h9 = __builtin_bswapg(0x0000000000001234) == 0x3412000000000000 ? 1 : f();
844 #ifndef __AVR__
845 int h10 = __builtin_bswapg((_BitInt(8))0x12) == (_BitInt(8))0x12 ? 1 : f();
846 int h11 = __builtin_bswapg((_BitInt(16))0x1234) == (_BitInt(16))0x3412 ? 1 : f();
847 int h12 = __builtin_bswapg((_BitInt(32))0x00001234) == (_BitInt(32))0x34120000 ? 1 : f();
848 int h13 = __builtin_bswapg((_BitInt(64))0x0000000000001234) == (_BitInt(64))0x3412000000000000 ? 1 : f();
849 int h14 = __builtin_bswapg(~(_BitInt(128))0) == (~(_BitInt(128))0) ? 1 : f();
850 int h15 = __builtin_bswapg((_BitInt(24))0x1234) == (_BitInt(24))0x3412 ? 1 : f();
851 // expected-error@-1 {{_BitInt type '_BitInt(24)' (24 bits) must be a multiple of 16 bits for byte swapping}}
852 // ref-error@-2 {{_BitInt type '_BitInt(24)' (24 bits) must be a multiple of 16 bits for byte swapping}}
853 #endif
854
855 constexpr const int const_expr = 0x1234;
856
857 void test_constexpr_reference() {
858 const int expr = 0x1234;
859 const int& ref = expr; // #declare
860
861 constexpr const int& const_ref = const_expr;
862
863 constexpr auto result2 = __builtin_bswapg(ref);
864 //expected-error@-1 {{constexpr variable 'result2' must be initialized by a constant expression}}
865 //expected-note@-2 {{initializer of 'ref' is not a constant expression}}
866 //expected-note@#declare {{declared here}}
867 //ref-error@-4 {{constexpr variable 'result2' must be initialized by a constant expression}}
868 //ref-note@-5 {{initializer of 'ref' is not a constant expression}}
869 //ref-note@#declare {{declared here}}
870
871 constexpr auto result3 = __builtin_bswapg(const_ref);
872 }
873 }
874
875 #define CFSTR __builtin___CFStringMakeConstantString
876 void test7(void) {
877 const void *X;
878 #if !defined(_AIX)
879 X = CFSTR("\242"); // both-warning {{input conversion stopped}}
880 X = CFSTR("\0"); // no-warning
881 X = CFSTR(242); // both-error {{cannot initialize a parameter of type 'const char *' with an rvalue of type 'int'}}
882 X = CFSTR("foo", "bar"); // both-error {{too many arguments to function call}}
883 #endif
884 }
885
886 /// The actual value on my machine is 22, but I have a feeling this will be different
887 /// on other targets, so just checking for != 0 here. Light testing is fine since
888 /// the actual implementation uses analyze_os_log::computeOSLogBufferLayout(), which
889 /// is tested elsewhere.
890 static_assert(__builtin_os_log_format_buffer_size("%{mask.xyz}s", "abc") != 0, "");
891
892 /// Copied from test/Sema/constant_builtins_vector.cpp.
893 /// Some tests are missing since we run this for multiple targets,
894 /// some of which do not support _BitInt.
895 #ifndef __AVR__
896 typedef _BitInt(128) BitInt128;
897 typedef double vector4double __attribute__((__vector_size__(32)));
898 typedef float vector4float __attribute__((__vector_size__(16)));
899 typedef long long vector4long __attribute__((__vector_size__(32)));
900 typedef int vector4int __attribute__((__vector_size__(16)));
901 typedef short vector4short __attribute__((__vector_size__(8)));
902 typedef char vector4char __attribute__((__vector_size__(4)));
903 typedef BitInt128 vector4BitInt128 __attribute__((__vector_size__(64)));
904 typedef double vector8double __attribute__((__vector_size__(64)));
905 typedef float vector8float __attribute__((__vector_size__(32)));
906 typedef long long vector8long __attribute__((__vector_size__(64)));
907 typedef int vector8int __attribute__((__vector_size__(32)));
908 typedef short vector8short __attribute__((__vector_size__(16)));
909 typedef char vector8char __attribute__((__vector_size__(8)));
910 typedef BitInt128 vector8BitInt128 __attribute__((__vector_size__(128)));
911
912 namespace convertvector {
913 constexpr vector4double from_vector4double_to_vector4double_var =
914 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4double);
915 constexpr vector4float from_vector4double_to_vector4float_var =
916 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4float);
917 constexpr vector4long from_vector4double_to_vector4long_var =
918 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4long);
919 constexpr vector4int from_vector4double_to_vector4int_var =
920 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4int);
921 constexpr vector4short from_vector4double_to_vector4short_var =
922 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4short);
923 constexpr vector4char from_vector4double_to_vector4char_var =
924 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4char);
925 constexpr vector4BitInt128 from_vector4double_to_vector4BitInt128_var =
926 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4BitInt128);
927 constexpr vector4double from_vector4float_to_vector4double_var =
928 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4double);
929 constexpr vector4float from_vector4float_to_vector4float_var =
930 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4float);
931 constexpr vector4long from_vector4float_to_vector4long_var =
932 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4long);
933 constexpr vector4int from_vector4float_to_vector4int_var =
934 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4int);
935 constexpr vector4short from_vector4float_to_vector4short_var =
936 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4short);
937 constexpr vector4char from_vector4float_to_vector4char_var =
938 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4char);
939 constexpr vector4BitInt128 from_vector4float_to_vector4BitInt128_var =
940 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4BitInt128);
941 constexpr vector4double from_vector4long_to_vector4double_var =
942 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4double);
943 constexpr vector4float from_vector4long_to_vector4float_var =
944 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4float);
945 constexpr vector4long from_vector4long_to_vector4long_var =
946 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4long);
947 constexpr vector4int from_vector4long_to_vector4int_var =
948 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4int);
949 constexpr vector4short from_vector4long_to_vector4short_var =
950 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4short);
951 constexpr vector4char from_vector4long_to_vector4char_var =
952 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4char);
953 constexpr vector4BitInt128 from_vector4long_to_vector4BitInt128_var =
954 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4BitInt128);
955 constexpr vector4double from_vector4int_to_vector4double_var =
956 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4double);
957 constexpr vector4float from_vector4int_to_vector4float_var =
958 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4float);
959 constexpr vector4long from_vector4int_to_vector4long_var =
960 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4long);
961 constexpr vector4int from_vector4int_to_vector4int_var =
962 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4int);
963 constexpr vector4short from_vector4int_to_vector4short_var =
964 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4short);
965 constexpr vector4char from_vector4int_to_vector4char_var =
966 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4char);
967 constexpr vector4BitInt128 from_vector4int_to_vector4BitInt128_var =
968 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4BitInt128);
969 constexpr vector4double from_vector4short_to_vector4double_var =
970 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4double);
971 constexpr vector4float from_vector4short_to_vector4float_var =
972 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4float);
973 constexpr vector4long from_vector4short_to_vector4long_var =
974 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4long);
975 constexpr vector4int from_vector4short_to_vector4int_var =
976 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4int);
977 constexpr vector4short from_vector4short_to_vector4short_var =
978 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4short);
979 constexpr vector4char from_vector4short_to_vector4char_var =
980 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4char);
981 constexpr vector4BitInt128 from_vector4short_to_vector4BitInt128_var =
982 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4BitInt128);
983 constexpr vector4double from_vector4char_to_vector4double_var =
984 __builtin_convertvector((vector4char){0, 1, 2, 3}, vector4double);
985 constexpr vector4float from_vector4char_to_vector4float_var =
986 __builtin_convertvector((vector4char){0, 1, 2, 3}, vector4float);
987 constexpr vector4long from_vector4char_to_vector4long_var =
988 __builtin_convertvector((vector4char){0, 1, 2, 3}, vector4long);
989 constexpr vector4int from_vector4char_to_vector4int_var =
990 __builtin_convertvector((vector4char){0, 1, 2, 3}, vector4int);
991 constexpr vector4short from_vector4char_to_vector4short_var =
992 __builtin_convertvector((vector4char){0, 1, 2, 3}, vector4short);
993 constexpr vector4char from_vector4char_to_vector4char_var =
994 __builtin_convertvector((vector4char){0, 1, 2, 3}, vector4char);
995 constexpr vector8double from_vector8double_to_vector8double_var =
996 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7},
997 vector8double);
998 constexpr vector8float from_vector8double_to_vector8float_var =
999 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7},
1000 vector8float);
1001 constexpr vector8long from_vector8double_to_vector8long_var =
1002 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7},
1003 vector8long);
1004 constexpr vector8int from_vector8double_to_vector8int_var =
1005 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7},
1006 vector8int);
1007 constexpr vector8short from_vector8double_to_vector8short_var =
1008 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7},
1009 vector8short);
1010 constexpr vector8char from_vector8double_to_vector8char_var =
1011 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7},
1012 vector8char);
1013 constexpr vector8BitInt128 from_vector8double_to_vector8BitInt128_var =
1014 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7},
1015 vector8BitInt128);
1016 constexpr vector8double from_vector8float_to_vector8double_var =
1017 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7},
1018 vector8double);
1019 constexpr vector8float from_vector8float_to_vector8float_var =
1020 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7},
1021 vector8float);
1022 constexpr vector8long from_vector8float_to_vector8long_var =
1023 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7},
1024 vector8long);
1025 constexpr vector8int from_vector8float_to_vector8int_var =
1026 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7}, vector8int);
1027 constexpr vector8short from_vector8float_to_vector8short_var =
1028 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7},
1029 vector8short);
1030 constexpr vector8char from_vector8float_to_vector8char_var =
1031 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7},
1032 vector8char);
1033 constexpr vector8BitInt128 from_vector8float_to_vector8BitInt128_var =
1034 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7},
1035 vector8BitInt128);
1036 constexpr vector8double from_vector8long_to_vector8double_var =
1037 __builtin_convertvector((vector8long){0, 1, 2, 3, 4, 5, 6, 7},
1038 vector8double);
1039 constexpr vector8float from_vector8long_to_vector8float_var =
1040 __builtin_convertvector((vector8long){0, 1, 2, 3, 4, 5, 6, 7},
1041 vector8float);
1042 constexpr vector8long from_vector8long_to_vector8long_var =
1043 __builtin_convertvector((vector8long){0, 1, 2, 3, 4, 5, 6, 7}, vector8long);
1044 constexpr vector8int from_vector8long_to_vector8int_var =
1045 __builtin_convertvector((vector8long){0, 1, 2, 3, 4, 5, 6, 7}, vector8int);
1046 constexpr vector8short from_vector8long_to_vector8short_var =
1047 __builtin_convertvector((vector8long){0, 1, 2, 3, 4, 5, 6, 7},
1048 vector8short);
1049 constexpr vector8char from_vector8long_to_vector8char_var =
1050 __builtin_convertvector((vector8long){0, 1, 2, 3, 4, 5, 6, 7}, vector8char);
1051 constexpr vector8double from_vector8int_to_vector8double_var =
1052 __builtin_convertvector((vector8int){0, 1, 2, 3, 4, 5, 6, 7},
1053 vector8double);
1054 constexpr vector8float from_vector8int_to_vector8float_var =
1055 __builtin_convertvector((vector8int){0, 1, 2, 3, 4, 5, 6, 7}, vector8float);
1056 constexpr vector8long from_vector8int_to_vector8long_var =
1057 __builtin_convertvector((vector8int){0, 1, 2, 3, 4, 5, 6, 7}, vector8long);
1058 constexpr vector8int from_vector8int_to_vector8int_var =
1059 __builtin_convertvector((vector8int){0, 1, 2, 3, 4, 5, 6, 7}, vector8int);
1060 constexpr vector8short from_vector8int_to_vector8short_var =
1061 __builtin_convertvector((vector8int){0, 1, 2, 3, 4, 5, 6, 7}, vector8short);
1062 constexpr vector8char from_vector8int_to_vector8char_var =
1063 __builtin_convertvector((vector8int){0, 1, 2, 3, 4, 5, 6, 7}, vector8char);
1064 constexpr vector8double from_vector8short_to_vector8double_var =
1065 __builtin_convertvector((vector8short){0, 1, 2, 3, 4, 5, 6, 7},
1066 vector8double);
1067 constexpr vector8float from_vector8short_to_vector8float_var =
1068 __builtin_convertvector((vector8short){0, 1, 2, 3, 4, 5, 6, 7},
1069 vector8float);
1070 constexpr vector8long from_vector8short_to_vector8long_var =
1071 __builtin_convertvector((vector8short){0, 1, 2, 3, 4, 5, 6, 7},
1072 vector8long);
1073 constexpr vector8int from_vector8short_to_vector8int_var =
1074 __builtin_convertvector((vector8short){0, 1, 2, 3, 4, 5, 6, 7}, vector8int);
1075 constexpr vector8short from_vector8short_to_vector8short_var =
1076 __builtin_convertvector((vector8short){0, 1, 2, 3, 4, 5, 6, 7},
1077 vector8short);
1078 constexpr vector8char from_vector8short_to_vector8char_var =
1079 __builtin_convertvector((vector8short){0, 1, 2, 3, 4, 5, 6, 7},
1080 vector8char);
1081
1082 constexpr vector8double from_vector8char_to_vector8double_var =
1083 __builtin_convertvector((vector8char){0, 1, 2, 3, 4, 5, 6, 7},
1084 vector8double);
1085 constexpr vector8float from_vector8char_to_vector8float_var =
1086 __builtin_convertvector((vector8char){0, 1, 2, 3, 4, 5, 6, 7},
1087 vector8float);
1088 constexpr vector8long from_vector8char_to_vector8long_var =
1089 __builtin_convertvector((vector8char){0, 1, 2, 3, 4, 5, 6, 7}, vector8long);
1090 constexpr vector8int from_vector8char_to_vector8int_var =
1091 __builtin_convertvector((vector8char){0, 1, 2, 3, 4, 5, 6, 7}, vector8int);
1092 constexpr vector8short from_vector8char_to_vector8short_var =
1093 __builtin_convertvector((vector8char){0, 1, 2, 3, 4, 5, 6, 7},
1094 vector8short);
1095 constexpr vector8char from_vector8char_to_vector8char_var =
1096 __builtin_convertvector((vector8char){0, 1, 2, 3, 4, 5, 6, 7}, vector8char);
1097 constexpr vector8double from_vector8BitInt128_to_vector8double_var =
1098 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7},
1099 vector8double);
1100 constexpr vector8float from_vector8BitInt128_to_vector8float_var =
1101 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7},
1102 vector8float);
1103 constexpr vector8long from_vector8BitInt128_to_vector8long_var =
1104 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7},
1105 vector8long);
1106 constexpr vector8int from_vector8BitInt128_to_vector8int_var =
1107 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7},
1108 vector8int);
1109 constexpr vector8short from_vector8BitInt128_to_vector8short_var =
1110 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7},
1111 vector8short);
1112 constexpr vector8char from_vector8BitInt128_to_vector8char_var =
1113 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7},
1114 vector8char);
1115 constexpr vector8BitInt128 from_vector8BitInt128_to_vector8BitInt128_var =
1116 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7},
1117 vector8BitInt128);
1118 static_assert(from_vector8BitInt128_to_vector8BitInt128_var[0] == 0, "");
1119 static_assert(from_vector8BitInt128_to_vector8BitInt128_var[1] == 1, "");
1120 static_assert(from_vector8BitInt128_to_vector8BitInt128_var[2] == 2, "");
1121 static_assert(from_vector8BitInt128_to_vector8BitInt128_var[3] == 3, "");
1122 static_assert(from_vector8BitInt128_to_vector8BitInt128_var[4] == 4, "");
1123 }
1124
1125 namespace shufflevector {
1126 constexpr vector4char vector4charConst1 = {0, 1, 2, 3};
1127 constexpr vector4char vector4charConst2 = {4, 5, 6, 7};
1128 constexpr vector8char vector8intConst = {8, 9, 10, 11, 12, 13, 14, 15};
1129 constexpr vector4char vectorShuffle1 =
1130 __builtin_shufflevector(vector4charConst1, vector4charConst2, 0, 1, 2, 3);
1131 constexpr vector4char vectorShuffle2 =
1132 __builtin_shufflevector(vector4charConst1, vector4charConst2, 4, 5, 6, 7);
1133 constexpr vector4char vectorShuffle3 =
1134 __builtin_shufflevector(vector4charConst1, vector4charConst2, 0, 2, 4, 6);
1135 constexpr vector8char vectorShuffle4 = __builtin_shufflevector(
1136 vector8intConst, vector8intConst, 0, 2, 4, 6, 8, 10, 12, 14);
1137 constexpr vector4char vectorShuffle5 =
1138 __builtin_shufflevector(vector8intConst, vector8intConst, 0, 2, 4, 6);
1139 constexpr vector8char vectorShuffle6 = __builtin_shufflevector(
1140 vector4charConst1, vector4charConst2, 0, 2, 4, 6, 1, 3, 5, 7);
1141
1142 static_assert(vectorShuffle6[0] == 0, "");
1143 static_assert(vectorShuffle6[1] == 2, "");
1144 static_assert(vectorShuffle6[2] == 4, "");
1145 static_assert(vectorShuffle6[3] == 6, "");
1146 static_assert(vectorShuffle6[4] == 1, "");
1147 static_assert(vectorShuffle6[5] == 3, "");
1148 static_assert(vectorShuffle6[6] == 5, "");
1149 static_assert(vectorShuffle6[7] == 7, "");
1150
1151 constexpr vector4char vectorShuffleFail1 = __builtin_shufflevector( // both-error {{must be initialized by a constant expression}}\
1152 // both-error {{index for __builtin_shufflevector not within the bounds of the input vectors; index of -1 found at position 0 is not permitted in a constexpr context}}
1153 vector4charConst1,
1154 vector4charConst2, -1, -1, -1, -1);
1155 }
1156
1157 #endif
1158
1159 namespace FunctionStart {
1160 void a(void) {}
1161 static_assert(__builtin_function_start(a) == a, ""); // both-error {{not an integral constant expression}} \
1162 // both-note {{comparison against opaque constant address '&__builtin_function_start(a)'}}
1163 }
1164
1165 namespace BuiltinInImplicitCtor {
1166 constexpr struct {
1167 int a = __builtin_isnan(1.0);
1168 } Foo;
1169 static_assert(Foo.a == 0, "");
1170 }
1171
1172 typedef double vector4double __attribute__((__vector_size__(32)));
1173 typedef float vector4float __attribute__((__vector_size__(16)));
1174 typedef long long vector4long __attribute__((__vector_size__(32)));
1175 typedef int vector4int __attribute__((__vector_size__(16)));
1176 typedef unsigned long long vector4ulong __attribute__((__vector_size__(32)));
1177 typedef unsigned int vector4uint __attribute__((__vector_size__(16)));
1178 typedef short vector4short __attribute__((__vector_size__(8)));
1179 typedef char vector4char __attribute__((__vector_size__(4)));
1180 typedef double vector8double __attribute__((__vector_size__(64)));
1181 typedef float vector8float __attribute__((__vector_size__(32)));
1182 typedef long long vector8long __attribute__((__vector_size__(64)));
1183 typedef int vector8int __attribute__((__vector_size__(32)));
1184 typedef short vector8short __attribute__((__vector_size__(16)));
1185 typedef char vector8char __attribute__((__vector_size__(8)));
1186
1187 namespace RecuceAdd {
1188 static_assert(__builtin_reduce_add((vector4char){}) == 0);
1189 static_assert(__builtin_reduce_add((vector4char){1, 2, 3, 4}) == 10);
1190 static_assert(__builtin_reduce_add((vector4short){10, 20, 30, 40}) == 100);
1191 static_assert(__builtin_reduce_add((vector4int){100, 200, 300, 400}) == 1000);
1192 static_assert(__builtin_reduce_add((vector4long){1000, 2000, 3000, 4000}) == 10000);
1193 constexpr int reduceAddInt1 = __builtin_reduce_add((vector4int){~(1 << (sizeof(int) * 8 - 1)), 0, 0, 1});
1194 // both-error@-1 {{must be initialized by a constant expression}} \
1195 // both-note@-1 {{outside the range of representable values of type 'int'}}
1196 constexpr long long reduceAddLong1 = __builtin_reduce_add((vector4long){~(1LL << (sizeof(long long) * 8 - 1)), 0, 0, 1});
1197 // both-error@-1 {{must be initialized by a constant expression}} \
1198 // both-note@-1 {{outside the range of representable values of type 'long long'}}
1199 constexpr int reduceAddInt2 = __builtin_reduce_add((vector4int){(1 << (sizeof(int) * 8 - 1)), 0, 0, -1});
1200 // both-error@-1 {{must be initialized by a constant expression}} \
1201 // both-note@-1 {{outside the range of representable values of type 'int'}}
1202 constexpr long long reduceAddLong2 = __builtin_reduce_add((vector4long){(1LL << (sizeof(long long) * 8 - 1)), 0, 0, -1});
1203 // both-error@-1 {{must be initialized by a constant expression}} \
1204 // both-note@-1 {{outside the range of representable values of type 'long long'}}
1205 static_assert(__builtin_reduce_add((vector4uint){~0U, 0, 0, 1}) == 0);
1206 static_assert(__builtin_reduce_add((vector4ulong){~0ULL, 0, 0, 1}) == 0);
1207
1208
1209 #ifdef __SIZEOF_INT128__
1210 typedef __int128 v4i128 __attribute__((__vector_size__(128 * 2)));
1211 constexpr __int128 reduceAddInt3 = __builtin_reduce_add((v4i128){});
1212 static_assert(reduceAddInt3 == 0);
1213 #endif
1214 }
1215
1216 namespace ReduceMul {
1217 static_assert(__builtin_reduce_mul((vector4char){}) == 0);
1218 static_assert(__builtin_reduce_mul((vector4char){1, 2, 3, 4}) == 24);
1219 static_assert(__builtin_reduce_mul((vector4short){1, 2, 30, 40}) == 2400);
1220 #ifndef __AVR__
1221 static_assert(__builtin_reduce_mul((vector4int){10, 20, 300, 400}) == 24'000'000);
1222 #endif
1223 static_assert(__builtin_reduce_mul((vector4long){1000L, 2000L, 3000L, 4000L}) == 24'000'000'000'000L);
1224 constexpr int reduceMulInt1 = __builtin_reduce_mul((vector4int){~(1 << (sizeof(int) * 8 - 1)), 1, 1, 2});
1225 // both-error@-1 {{must be initialized by a constant expression}} \
1226 // both-note@-1 {{outside the range of representable values of type 'int'}}
1227 constexpr long long reduceMulLong1 = __builtin_reduce_mul((vector4long){~(1LL << (sizeof(long long) * 8 - 1)), 1, 1, 2});
1228 // both-error@-1 {{must be initialized by a constant expression}} \
1229 // both-note@-1 {{outside the range of representable values of type 'long long'}}
1230 constexpr int reduceMulInt2 = __builtin_reduce_mul((vector4int){(1 << (sizeof(int) * 8 - 1)), 1, 1, 2});
1231 // both-error@-1 {{must be initialized by a constant expression}} \
1232 // both-note@-1 {{outside the range of representable values of type 'int'}}
1233 constexpr long long reduceMulLong2 = __builtin_reduce_mul((vector4long){(1LL << (sizeof(long long) * 8 - 1)), 1, 1, 2});
1234 // both-error@-1 {{must be initialized by a constant expression}} \
1235 // both-note@-1 {{outside the range of representable values of type 'long long'}}
1236 static_assert(__builtin_reduce_mul((vector4uint){~0U, 1, 1, 2}) ==
1237 #ifdef __AVR__
1238 0);
1239 #else
1240 (~0U - 1));
1241 #endif
1242 static_assert(__builtin_reduce_mul((vector4ulong){~0ULL, 1, 1, 2}) == ~0ULL - 1);
1243 }
1244
1245 namespace ReduceAnd {
1246 static_assert(__builtin_reduce_and((vector4char){}) == 0);
1247 static_assert(__builtin_reduce_and((vector4char){(char)0x11, (char)0x22, (char)0x44, (char)0x88}) == 0);
1248 static_assert(__builtin_reduce_and((vector4short){(short)0x1111, (short)0x2222, (short)0x4444, (short)0x8888}) == 0);
1249 static_assert(__builtin_reduce_and((vector4int){(int)0x11111111, (int)0x22222222, (int)0x44444444, (int)0x88888888}) == 0);
1250 #if __INT_WIDTH__ == 32
1251 static_assert(__builtin_reduce_and((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0x8888888888888888L}) == 0L);
1252 static_assert(__builtin_reduce_and((vector4char){(char)-1, (char)~0x22, (char)~0x44, (char)~0x88}) == 0x11);
1253 static_assert(__builtin_reduce_and((vector4short){(short)~0x1111, (short)-1, (short)~0x4444, (short)~0x8888}) == 0x2222);
1254 static_assert(__builtin_reduce_and((vector4int){(int)~0x11111111, (int)~0x22222222, (int)-1, (int)~0x88888888}) == 0x44444444);
1255 static_assert(__builtin_reduce_and((vector4long){(long long)~0x1111111111111111L, (long long)~0x2222222222222222L, (long long)~0x4444444444444444L, (long long)-1}) == 0x8888888888888888L);
1256 static_assert(__builtin_reduce_and((vector4uint){0x11111111U, 0x22222222U, 0x44444444U, 0x88888888U}) == 0U);
1257 static_assert(__builtin_reduce_and((vector4ulong){0x1111111111111111UL, 0x2222222222222222UL, 0x4444444444444444UL, 0x8888888888888888UL}) == 0L);
1258 #endif
1259 }
1260
1261 namespace ReduceOr {
1262 static_assert(__builtin_reduce_or((vector4char){}) == 0);
1263 static_assert(__builtin_reduce_or((vector4char){(char)0x11, (char)0x22, (char)0x44, (char)0x88}) == (char)0xFF);
1264 static_assert(__builtin_reduce_or((vector4short){(short)0x1111, (short)0x2222, (short)0x4444, (short)0x8888}) == (short)0xFFFF);
1265 static_assert(__builtin_reduce_or((vector4int){(int)0x11111111, (int)0x22222222, (int)0x44444444, (int)0x88888888}) == (int)0xFFFFFFFF);
1266 #if __INT_WIDTH__ == 32
1267 static_assert(__builtin_reduce_or((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0x8888888888888888L}) == (long long)0xFFFFFFFFFFFFFFFFL);
1268 static_assert(__builtin_reduce_or((vector4char){(char)0, (char)0x22, (char)0x44, (char)0x88}) == ~0x11);
1269 static_assert(__builtin_reduce_or((vector4short){(short)0x1111, (short)0, (short)0x4444, (short)0x8888}) == ~0x2222);
1270 static_assert(__builtin_reduce_or((vector4int){(int)0x11111111, (int)0x22222222, (int)0, (int)0x88888888}) == ~0x44444444);
1271 static_assert(__builtin_reduce_or((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0}) == ~0x8888888888888888L);
1272 static_assert(__builtin_reduce_or((vector4uint){0x11111111U, 0x22222222U, 0x44444444U, 0x88888888U}) == 0xFFFFFFFFU);
1273 static_assert(__builtin_reduce_or((vector4ulong){0x1111111111111111UL, 0x2222222222222222UL, 0x4444444444444444UL, 0x8888888888888888UL}) == 0xFFFFFFFFFFFFFFFFL);
1274 #endif
1275 }
1276
1277 namespace ReduceXor {
1278 static_assert(__builtin_reduce_xor((vector4char){}) == 0);
1279 static_assert(__builtin_reduce_xor((vector4char){(char)0x11, (char)0x22, (char)0x44, (char)0x88}) == (char)0xFF);
1280 static_assert(__builtin_reduce_xor((vector4short){(short)0x1111, (short)0x2222, (short)0x4444, (short)0x8888}) == (short)0xFFFF);
1281 #if __INT_WIDTH__ == 32
1282 static_assert(__builtin_reduce_xor((vector4int){(int)0x11111111, (int)0x22222222, (int)0x44444444, (int)0x88888888}) == (int)0xFFFFFFFF);
1283 static_assert(__builtin_reduce_xor((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0x8888888888888888L}) == (long long)0xFFFFFFFFFFFFFFFFL);
1284 static_assert(__builtin_reduce_xor((vector4uint){0x11111111U, 0x22222222U, 0x44444444U, 0x88888888U}) == 0xFFFFFFFFU);
1285 static_assert(__builtin_reduce_xor((vector4ulong){0x1111111111111111UL, 0x2222222222222222UL, 0x4444444444444444UL, 0x8888888888888888UL}) == 0xFFFFFFFFFFFFFFFFUL);
1286 #endif
1287 }
1288
1289 namespace ElementwisePopcount {
1290 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4int){1, 2, 3, 4})) == 5);
1291 #if __INT_WIDTH__ == 32
1292 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4int){0, 0xF0F0, ~0, ~0xF0F0})) == 16 * sizeof(int));
1293 #endif
1294 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4long){1L, 2L, 3L, 4L})) == 5L);
1295 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4long){0L, 0xF0F0L, ~0L, ~0xF0F0L})) == 16 * sizeof(long long));
1296 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4uint){1U, 2U, 3U, 4U})) == 5U);
1297 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4uint){0U, 0xF0F0U, ~0U, ~0xF0F0U})) == 16 * sizeof(int));
1298 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4ulong){1UL, 2UL, 3UL, 4UL})) == 5UL);
1299 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4ulong){0ULL, 0xF0F0ULL, ~0ULL, ~0xF0F0ULL})) == 16 * sizeof(unsigned long long));
1300 static_assert(__builtin_elementwise_popcount(0) == 0);
1301 static_assert(__builtin_elementwise_popcount(0xF0F0) == 8);
1302 static_assert(__builtin_elementwise_popcount(~0) == 8 * sizeof(int));
1303 static_assert(__builtin_elementwise_popcount(0U) == 0);
1304 static_assert(__builtin_elementwise_popcount(0xF0F0U) == 8);
1305 static_assert(__builtin_elementwise_popcount(~0U) == 8 * sizeof(int));
1306 static_assert(__builtin_elementwise_popcount(0L) == 0);
1307 static_assert(__builtin_elementwise_popcount(0xF0F0L) == 8);
1308 static_assert(__builtin_elementwise_popcount(~0LL) == 8 * sizeof(long long));
1309
1310 #if __INT_WIDTH__ == 32
1311 static_assert(__builtin_bit_cast(unsigned, __builtin_elementwise_popcount((vector4char){1, 2, 3, 4})) == (LITTLE_END ? 0x01020101 : 0x01010201));
1312 #endif
1313 }
1314
1315 namespace BuiltinMemcpy {
1316 constexpr int simple() {
1317 int a = 12;
1318 int b = 0;
1319 __builtin_memcpy(&b, &a, sizeof(a));
1320 return b;
1321 }
1322 static_assert(simple() == 12);
1323
1324 constexpr bool arrayMemcpy() {
1325 char src[] = "abc";
1326 char dst[4] = {};
1327 __builtin_memcpy(dst, src, 4);
1328 return dst[0] == 'a' && dst[1] == 'b' && dst[2] == 'c' && dst[3] == '\0';
1329 }
1330 static_assert(arrayMemcpy());
1331
1332 extern struct Incomplete incomplete;
1333 constexpr struct Incomplete *null_incomplete = 0;
1334 static_assert(__builtin_memcpy(null_incomplete, null_incomplete, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
1335 // both-note {{source of 'memcpy' is nullptr}}
1336
1337 wchar_t global;
1338 constexpr wchar_t *null = 0;
1339 static_assert(__builtin_memcpy(&global, null, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
1340 // both-note {{source of 'memcpy' is nullptr}}
1341
1342 constexpr int simpleMove() {
1343 int a = 12;
1344 int b = 0;
1345 __builtin_memmove(&b, &a, sizeof(a));
1346 return b;
1347 }
1348 static_assert(simpleMove() == 12);
1349
1350 constexpr int memcpyTypeRem() { // both-error {{never produces a constant expression}}
1351 int a = 12;
1352 int b = 0;
1353 __builtin_memmove(&b, &a, 1); // both-note {{'memmove' not supported: size to copy (1) is not a multiple of size of element type 'int'}} \
1354 // both-note {{not supported}}
1355 return b;
1356 }
1357 static_assert(memcpyTypeRem() == 12); // both-error {{not an integral constant expression}} \
1358 // both-note {{in call to}}
1359
1360 template<typename T>
1361 constexpr T result(T (&arr)[4]) {
1362 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
1363 }
1364
1365 constexpr int test_memcpy(int a, int b, int n) {
1366 int arr[4] = {1, 2, 3, 4};
1367 __builtin_memcpy(arr + a, arr + b, n); // both-note {{overlapping memory regions}}
1368 return result(arr);
1369 }
1370
1371 static_assert(test_memcpy(1, 2, sizeof(int)) == 1334);
1372 static_assert(test_memcpy(0, 1, sizeof(int) * 2) == 2334); // both-error {{not an integral constant expression}} \
1373 // both-note {{in call}}
1374
1375 /// Both memcpy and memmove must support pointers.
1376 constexpr bool moveptr() {
1377 int a = 0;
1378 void *x = &a;
1379 void *z = nullptr;
1380
1381 __builtin_memmove(&z, &x, sizeof(void*));
1382 return z == x;
1383 }
1384 static_assert(moveptr());
1385
1386 constexpr bool cpyptr() {
1387 int a = 0;
1388 void *x = &a;
1389 void *z = nullptr;
1390
1391 __builtin_memcpy(&z, &x, sizeof(void*));
1392 return z == x;
1393 }
1394 static_assert(cpyptr());
1395
1396 #ifndef __AVR__
1397 constexpr int test_memmove(int a, int b, int n) {
1398 int arr[4] = {1, 2, 3, 4};
1399 __builtin_memmove(arr + a, arr + b, n); // both-note {{destination is not a contiguous array of at least 3 elements of type 'int'}}
1400 return result(arr);
1401 }
1402 static_assert(test_memmove(2, 0, 12) == 4234); // both-error {{constant}} \
1403 // both-note {{in call}}
1404 #endif
1405
1406 struct Trivial { char k; short s; constexpr bool ok() { return k == 3 && s == 4; } };
1407 constexpr bool test_trivial() {
1408 Trivial arr[3] = {{1, 2}, {3, 4}, {5, 6}};
1409 __builtin_memcpy(arr, arr+1, sizeof(Trivial));
1410 __builtin_memmove(arr+1, arr, 2 * sizeof(Trivial));
1411
1412 return arr[0].ok() && arr[1].ok() && arr[2].ok();
1413 }
1414 static_assert(test_trivial());
1415
1416 // Check that an incomplete array is rejected.
1417 constexpr int test_incomplete_array_type() { // both-error {{never produces a constant}}
1418 extern int arr[];
1419 __builtin_memmove(arr, arr, 4 * sizeof(arr[0]));
1420 // both-note@-1 2{{'memmove' not supported: source is not a contiguous array of at least 4 elements of type 'int'}}
1421 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
1422 }
1423 static_assert(test_incomplete_array_type() == 1234); // both-error {{constant}} both-note {{in call}}
1424
1425
1426 constexpr bool memmoveOverlapping() {
1427 char s1[] {1, 2, 3};
1428 __builtin_memmove(s1, s1 + 1, 2 * sizeof(char));
1429 // Now: 2, 3, 3
1430 bool Result1 = (s1[0] == 2 && s1[1] == 3 && s1[2]== 3);
1431
1432 __builtin_memmove(s1 + 1, s1, 2 * sizeof(char));
1433 // Now: 2, 2, 3
1434 bool Result2 = (s1[0] == 2 && s1[1] == 2 && s1[2]== 3);
1435
1436 return Result1 && Result2;
1437 }
1438 static_assert(memmoveOverlapping());
1439
1440 #define fold(x) (__builtin_constant_p(0) ? (x) : (x))
1441 static_assert(__builtin_memcpy(&global, fold((wchar_t*)123), sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
1442 // both-note {{source of 'memcpy' is (void *)123}}
1443 static_assert(__builtin_memcpy(fold(reinterpret_cast<wchar_t*>(123)), &global, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
1444 // both-note {{destination of 'memcpy' is (void *)123}}
1445
1446
1447 constexpr float type_pun(const unsigned &n) {
1448 float f = 0.0f;
1449 __builtin_memcpy(&f, &n, 4); // both-note {{cannot constant evaluate 'memcpy' from object of type 'const unsigned int' to object of type 'float'}}
1450 return f;
1451 }
1452 static_assert(type_pun(0x3f800000) == 1.0f); // both-error {{constant}} \
1453 // both-note {{in call}}
1454
1455 struct Base { int a; };
1456 struct Derived : Base { int b; };
1457 constexpr int test_derived_to_base(int n) {
1458 Derived arr[2] = {1, 2, 3, 4};
1459 Base *p = &arr[0];
1460 Base *q = &arr[1];
1461 __builtin_memcpy(p, q, sizeof(Base) * n); // both-note {{source is not a contiguous array of at least 2 elements of type 'BuiltinMemcpy::Base'}}
1462 return arr[0].a * 1000 + arr[0].b * 100 + arr[1].a * 10 + arr[1].b;
1463 }
1464 static_assert(test_derived_to_base(0) == 1234);
1465 static_assert(test_derived_to_base(1) == 3234);
1466 static_assert(test_derived_to_base(2) == 3434); // both-error {{constant}} \
1467 // both-note {{in call}}
1468 }
1469
1470 namespace Memcmp {
1471 constexpr unsigned char ku00fe00[] = {0x00, 0xfe, 0x00};
1472 constexpr unsigned char ku00feff[] = {0x00, 0xfe, 0xff};
1473 constexpr signed char ks00fe00[] = {0, -2, 0};
1474 constexpr signed char ks00feff[] = {0, -2, -1};
1475 static_assert(__builtin_memcmp(ku00feff, ks00fe00, 2) == 0);
1476 static_assert(__builtin_memcmp(ku00feff, ks00fe00, 99) == 1);
1477 static_assert(__builtin_memcmp(ku00fe00, ks00feff, 99) == -1);
1478 static_assert(__builtin_memcmp(ks00feff, ku00fe00, 2) == 0);
1479 static_assert(__builtin_memcmp(ks00feff, ku00fe00, 99) == 1);
1480 static_assert(__builtin_memcmp(ks00fe00, ku00feff, 99) == -1);
1481 static_assert(__builtin_memcmp(ks00fe00, ks00feff, 2) == 0);
1482 static_assert(__builtin_memcmp(ks00feff, ks00fe00, 99) == 1);
1483 static_assert(__builtin_memcmp(ks00fe00, ks00feff, 99) == -1);
1484
1485 struct Bool3Tuple { bool bb[3]; };
1486 constexpr Bool3Tuple kb000100 = {{false, true, false}};
1487 static_assert(sizeof(bool) != 1u || __builtin_memcmp(ks00fe00, kb000100.bb, 1) == 0); // both-error {{constant}} \
1488 // both-note {{not supported}}
1489
1490 constexpr char a = 'a';
1491 constexpr char b = 'a';
1492 static_assert(__builtin_memcmp(&a, &b, 1) == 0);
1493
1494 extern struct Incomplete incomplete;
1495 static_assert(__builtin_memcmp(&incomplete, "", 0u) == 0);
1496 static_assert(__builtin_memcmp("", &incomplete, 0u) == 0);
1497 static_assert(__builtin_memcmp(&incomplete, "", 1u) == 42); // both-error {{not an integral constant}} \
1498 // both-note {{not supported}}
1499 static_assert(__builtin_memcmp("", &incomplete, 1u) == 42); // both-error {{not an integral constant}} \
1500 // both-note {{not supported}}
1501
1502 static_assert(__builtin_memcmp(u8"abab\0banana", u8"abab\0banana", 100) == 0); // both-error {{not an integral constant}} \
1503 // both-note {{dereferenced one-past-the-end}}
1504
1505 static_assert(__builtin_bcmp("abaa", "abba", 3) != 0);
1506 static_assert(__builtin_bcmp("abaa", "abba", 2) == 0);
1507 static_assert(__builtin_bcmp("a\203", "a", 2) != 0);
1508 static_assert(__builtin_bcmp("a\203", "a\003", 2) != 0);
1509 static_assert(__builtin_bcmp(0, 0, 0) == 0);
1510 static_assert(__builtin_bcmp("abab\0banana", "abab\0banana", 100) == 0); // both-error {{not an integral constant}}\
1511 // both-note {{dereferenced one-past-the-end}}
1512 static_assert(__builtin_bcmp("abab\0banana", "abab\0canada", 100) != 0); // FIXME: Should we reject this?
1513 static_assert(__builtin_bcmp("abab\0banana", "abab\0canada", 7) != 0);
1514 static_assert(__builtin_bcmp("abab\0banana", "abab\0canada", 6) != 0);
1515 static_assert(__builtin_bcmp("abab\0banana", "abab\0canada", 5) == 0);
1516
1517
1518 static_assert(__builtin_wmemcmp(L"abaa", L"abba", 3) == -1);
1519 static_assert(__builtin_wmemcmp(L"abaa", L"abba", 2) == 0);
1520 static_assert(__builtin_wmemcmp(0, 0, 0) == 0);
1521 #if __WCHAR_WIDTH__ == 32
1522 static_assert(__builtin_wmemcmp(L"a\x83838383", L"aa", 2) ==
1523 (wchar_t)-1U >> 31);
1524 #endif
1525 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0banana", 100) == 0); // both-error {{not an integral constant}} \
1526 // both-note {{dereferenced one-past-the-end}}
1527 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 100) == -1); // FIXME: Should we reject this?
1528 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 7) == -1);
1529 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 6) == -1);
1530 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 5) == 0);
1531
1532 #if __cplusplus >= 202002L
1533 constexpr bool f() {
1534 char *c = new char[12];
1535 c[0] = 'b';
1536
1537 char n = 'a';
1538 bool b = __builtin_memcmp(c, &n, 1) == 0;
1539
1540 delete[] c;
1541 return !b;
1542 }
1543 static_assert(f());
1544 #endif
1545
1546 int unknown;
1547 void foo(void) { unknown *= __builtin_memcmp(0, 0, 2); }
1548
1549 constexpr int onepasttheend(char a) {
1550 __builtin_memcmp(&a, &a + 1, 1); // both-note {{read of dereferenced one-past-the-end pointer}}
1551 return 1;
1552 }
1553 static_assert(onepasttheend(10)); // both-error {{not an integral constant expression}} \
1554 // both-note {{in call to}}
1555 }
1556
1557 namespace Memchr {
1558 constexpr const char *kStr = "abca\xff\0d";
1559 constexpr char kFoo[] = {'f', 'o', 'o'};
1560
1561 static_assert(__builtin_memchr(kStr, 'a', 0) == nullptr);
1562 static_assert(__builtin_memchr(kStr, 'a', 1) == kStr);
1563 static_assert(__builtin_memchr(kStr, '\0', 5) == nullptr);
1564 static_assert(__builtin_memchr(kStr, '\0', 6) == kStr + 5);
1565 static_assert(__builtin_memchr(kStr, '\xff', 8) == kStr + 4);
1566 static_assert(__builtin_memchr(kStr, '\xff' + 256, 8) == kStr + 4);
1567 static_assert(__builtin_memchr(kStr, '\xff' - 256, 8) == kStr + 4);
1568 static_assert(__builtin_memchr(kFoo, 'x', 3) == nullptr);
1569 static_assert(__builtin_memchr(kFoo, 'x', 4) == nullptr); // both-error {{not an integral constant}} \
1570 // both-note {{dereferenced one-past-the-end}}
1571 static_assert(__builtin_memchr(nullptr, 'x', 3) == nullptr); // both-error {{not an integral constant}} \
1572 // both-note {{dereferenced null}}
1573 static_assert(__builtin_memchr(nullptr, 'x', 0) == nullptr);
1574
1575
1576 #if defined(CHAR8_T)
1577 constexpr const char8_t *kU8Str = u8"abca\xff\0d";
1578 constexpr char8_t kU8Foo[] = {u8'f', u8'o', u8'o'};
1579 static_assert(__builtin_memchr(kU8Str, u8'a', 0) == nullptr);
1580 static_assert(__builtin_memchr(kU8Str, u8'a', 1) == kU8Str);
1581 static_assert(__builtin_memchr(kU8Str, u8'\0', 5) == nullptr);
1582 static_assert(__builtin_memchr(kU8Str, u8'\0', 6) == kU8Str + 5);
1583 static_assert(__builtin_memchr(kU8Str, u8'\xff', 8) == kU8Str + 4);
1584 static_assert(__builtin_memchr(kU8Str, u8'\xff' + 256, 8) == kU8Str + 4);
1585 static_assert(__builtin_memchr(kU8Str, u8'\xff' - 256, 8) == kU8Str + 4);
1586 static_assert(__builtin_memchr(kU8Foo, u8'x', 3) == nullptr);
1587 static_assert(__builtin_memchr(kU8Foo, u8'x', 4) == nullptr); // both-error {{not an integral constant}} \
1588 // both-note {{dereferenced one-past-the-end}}
1589 static_assert(__builtin_memchr(nullptr, u8'x', 3) == nullptr); // both-error {{not an integral constant}} \
1590 // both-note {{dereferenced null}}
1591 static_assert(__builtin_memchr(nullptr, u8'x', 0) == nullptr);
1592 #endif
1593
1594 extern struct Incomplete incomplete;
1595 static_assert(__builtin_memchr(&incomplete, 0, 0u) == nullptr);
1596 static_assert(__builtin_memchr(&incomplete, 0, 1u) == nullptr); // both-error {{not an integral constant}} \
1597 // both-note {{read of incomplete type 'struct Incomplete'}}
1598
1599 const unsigned char &u1 = 0xf0;
1600 auto &&i1 = (const signed char []){-128};
1601 static_assert(__builtin_memchr(&u1, -(0x0f + 1), 1) == &u1);
1602 static_assert(__builtin_memchr(i1, 0x80, 1) == i1);
1603
1604 enum class E : unsigned char {};
1605 struct EPair { E e, f; };
1606 constexpr EPair ee{E{240}};
1607 static_assert(__builtin_memchr(&ee.e, 240, 1) == &ee.e); // both-error {{constant}} \
1608 // both-note {{not supported}}
1609
1610 constexpr bool kBool[] = {false, true, false};
1611 constexpr const bool *const kBoolPastTheEndPtr = kBool + 3;
1612 static_assert(sizeof(bool) != 1u || __builtin_memchr(kBoolPastTheEndPtr - 3, 1, 99) == kBool + 1); // both-error {{constant}} \
1613 // both-note {{not supported}}
1614 static_assert(sizeof(bool) != 1u || __builtin_memchr(kBool + 1, 0, 99) == kBoolPastTheEndPtr - 1); // both-error {{constant}} \
1615 // both-note {{not supported}}
1616 static_assert(sizeof(bool) != 1u || __builtin_memchr(kBoolPastTheEndPtr - 3, -1, 3) == nullptr); // both-error {{constant}} \
1617 // both-note {{not supported}}
1618 static_assert(sizeof(bool) != 1u || __builtin_memchr(kBoolPastTheEndPtr, 0, 1) == nullptr); // both-error {{constant}} \
1619 // both-note {{not supported}}
1620
1621 static_assert(__builtin_char_memchr(kStr, 'a', 0) == nullptr);
1622 static_assert(__builtin_char_memchr(kStr, 'a', 1) == kStr);
1623 static_assert(__builtin_char_memchr(kStr, '\0', 5) == nullptr);
1624 static_assert(__builtin_char_memchr(kStr, '\0', 6) == kStr + 5);
1625 static_assert(__builtin_char_memchr(kStr, '\xff', 8) == kStr + 4);
1626 static_assert(__builtin_char_memchr(kStr, '\xff' + 256, 8) == kStr + 4);
1627 static_assert(__builtin_char_memchr(kStr, '\xff' - 256, 8) == kStr + 4);
1628 static_assert(__builtin_char_memchr(kFoo, 'x', 3) == nullptr);
1629 static_assert(__builtin_char_memchr(kFoo, 'x', 4) == nullptr); // both-error {{not an integral constant}} \
1630 // both-note {{dereferenced one-past-the-end}}
1631 static_assert(__builtin_char_memchr(nullptr, 'x', 3) == nullptr); // both-error {{not an integral constant}} \
1632 // both-note {{dereferenced null}}
1633 static_assert(__builtin_char_memchr(nullptr, 'x', 0) == nullptr);
1634
1635 static_assert(*__builtin_char_memchr(kStr, '\xff', 8) == '\xff');
1636 constexpr bool char_memchr_mutable() {
1637 char buffer[] = "mutable";
1638 *__builtin_char_memchr(buffer, 't', 8) = 'r';
1639 *__builtin_char_memchr(buffer, 'm', 8) = 'd';
1640 return __builtin_strcmp(buffer, "durable") == 0;
1641 }
1642 static_assert(char_memchr_mutable());
1643
1644 constexpr bool b = !memchr("hello", 'h', 3); // both-error {{constant expression}} \
1645 // both-note {{non-constexpr function 'memchr' cannot be used in a constant expression}}
1646
1647 constexpr bool f() {
1648 const char *c = "abcdef";
1649 return __builtin_char_memchr(c + 1, 'f', 1) == nullptr;
1650 }
1651 static_assert(f());
1652
1653
1654 extern const char char_memchr_arg[0l];
1655 char *memchr_result = __builtin_char_memchr(char_memchr_arg, 123, 32);
1656 }
1657
1658 namespace Strchr {
1659 constexpr const char *kStr = "abca\xff\0d";
1660 constexpr char kFoo[] = {'f', 'o', 'o'};
1661 static_assert(__builtin_strchr(kStr, 'a') == kStr);
1662 static_assert(__builtin_strchr(kStr, 'b') == kStr + 1);
1663 static_assert(__builtin_strchr(kStr, 'c') == kStr + 2);
1664 static_assert(__builtin_strchr(kStr, 'd') == nullptr);
1665 static_assert(__builtin_strchr(kStr, 'e') == nullptr);
1666 static_assert(__builtin_strchr(kStr, '\0') == kStr + 5);
1667 static_assert(__builtin_strchr(kStr, 'a' + 256) == nullptr);
1668 static_assert(__builtin_strchr(kStr, 'a' - 256) == nullptr);
1669 static_assert(__builtin_strchr(kStr, '\xff') == kStr + 4);
1670 static_assert(__builtin_strchr(kStr, '\xff' + 256) == nullptr);
1671 static_assert(__builtin_strchr(kStr, '\xff' - 256) == nullptr);
1672 static_assert(__builtin_strchr(kFoo, 'o') == kFoo + 1);
1673 static_assert(__builtin_strchr(kFoo, 'x') == nullptr); // both-error {{not an integral constant}} \
1674 // both-note {{dereferenced one-past-the-end}}
1675 static_assert(__builtin_strchr(nullptr, 'x') == nullptr); // both-error {{not an integral constant}} \
1676 // both-note {{dereferenced null}}
1677
1678 constexpr bool a = !strchr("hello", 'h'); // both-error {{constant expression}} \
1679 // both-note {{non-constexpr function 'strchr' cannot be used in a constant expression}}
1680 }
1681
1682 namespace WMemChr {
1683 constexpr const wchar_t *kStr = L"abca\xffff\0dL";
1684 constexpr wchar_t kFoo[] = {L'f', L'o', L'o'};
1685
1686 static_assert(__builtin_wmemchr(kStr, L'a', 0) == nullptr);
1687 static_assert(__builtin_wmemchr(kStr, L'a', 1) == kStr);
1688 static_assert(__builtin_wmemchr(kStr, L'\0', 5) == nullptr);
1689 static_assert(__builtin_wmemchr(kStr, L'\0', 6) == kStr + 5);
1690 static_assert(__builtin_wmemchr(kStr, L'\xffff', 8) == kStr + 4);
1691 static_assert(__builtin_wmemchr(kFoo, L'x', 3) == nullptr);
1692 static_assert(__builtin_wmemchr(kFoo, L'x', 4) == nullptr); // both-error {{not an integral constant}} \
1693 // both-note {{dereferenced one-past-the-end}}
1694 static_assert(__builtin_wmemchr(nullptr, L'x', 3) == nullptr); // both-error {{not an integral constant}} \
1695 // both-note {{dereferenced null}}
1696 static_assert(__builtin_wmemchr(nullptr, L'x', 0) == nullptr);
1697
1698 constexpr bool b = !wmemchr(L"hello", L'h', 3); // both-error {{constant expression}} \
1699 // both-note {{non-constexpr function 'wmemchr' cannot be used in a constant expression}}
1700
1701 constexpr wchar_t kStr2[] = {L'f', L'o', L'\xffff', L'o'};
1702 static_assert(__builtin_wmemchr(kStr2, L'\xffff', 4) == kStr2 + 2);
1703
1704
1705 static_assert(__builtin_wcschr(kStr, L'a') == kStr);
1706 static_assert(__builtin_wcschr(kStr, L'b') == kStr + 1);
1707 static_assert(__builtin_wcschr(kStr, L'c') == kStr + 2);
1708 static_assert(__builtin_wcschr(kStr, L'd') == nullptr);
1709 static_assert(__builtin_wcschr(kStr, L'e') == nullptr);
1710 static_assert(__builtin_wcschr(kStr, L'\0') == kStr + 5);
1711 static_assert(__builtin_wcschr(kStr, L'a' + 256) == nullptr);
1712 static_assert(__builtin_wcschr(kStr, L'a' - 256) == nullptr);
1713 static_assert(__builtin_wcschr(kStr, L'\xffff') == kStr + 4);
1714 static_assert(__builtin_wcschr(kFoo, L'o') == kFoo + 1);
1715 static_assert(__builtin_wcschr(kFoo, L'x') == nullptr); // both-error {{not an integral constant}} \
1716 // both-note {{dereferenced one-past-the-end}}
1717 static_assert(__builtin_wcschr(nullptr, L'x') == nullptr); // both-error {{not an integral constant}} \
1718 // both-note {{dereferenced null}}
1719
1720
1721 constexpr bool c = !wcschr(L"hello", L'h'); // both-error {{constant expression}} \
1722 // both-note {{non-constexpr function 'wcschr' cannot be used in a constant expression}}
1723 }
1724
1725 namespace WMemCpy {
1726 template<typename T>
1727 constexpr T result(T (&arr)[4]) {
1728 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
1729 }
1730 constexpr int test_wmemcpy(int a, int b, int n) {
1731 wchar_t arr[4] = {1, 2, 3, 4};
1732 __builtin_wmemcpy(arr + a, arr + b, n);
1733 // both-note@-1 2{{overlapping memory regions}}
1734 // both-note@-2 {{source is not a contiguous array of at least 2 elements of type 'wchar_t'}}
1735 // both-note@-3 {{destination is not a contiguous array of at least 3 elements of type 'wchar_t'}}
1736 return result(arr);
1737 }
1738 static_assert(test_wmemcpy(1, 2, 1) == 1334);
1739 static_assert(test_wmemcpy(2, 1, 1) == 1224);
1740 static_assert(test_wmemcpy(0, 1, 2) == 2334); // both-error {{constant}} both-note {{in call}}
1741 static_assert(test_wmemcpy(1, 0, 2) == 1124); // both-error {{constant}} both-note {{in call}}
1742 static_assert(test_wmemcpy(1, 2, 1) == 1334);
1743 static_assert(test_wmemcpy(0, 3, 1) == 4234);
1744 static_assert(test_wmemcpy(0, 3, 2) == 4234); // both-error {{constant}} both-note {{in call}}
1745 static_assert(test_wmemcpy(2, 0, 3) == 4234); // both-error {{constant}} both-note {{in call}}
1746
1747 wchar_t global;
1748 constexpr wchar_t *null = 0;
1749 static_assert(__builtin_wmemcpy(&global, null, sizeof(wchar_t))); // both-error {{}} \
1750 // both-note {{source of 'wmemcpy' is nullptr}}
1751 static_assert(__builtin_wmemcpy(null, &global, sizeof(wchar_t))); // both-error {{}} \
1752 // both-note {{destination of 'wmemcpy' is nullptr}}
1753 }
1754
1755 namespace WMemMove {
1756 template<typename T>
1757 constexpr T result(T (&arr)[4]) {
1758 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
1759 }
1760
1761 constexpr int test_wmemmove(int a, int b, int n) {
1762 wchar_t arr[4] = {1, 2, 3, 4};
1763 __builtin_wmemmove(arr + a, arr + b, n);
1764 // both-note@-1 {{source is not a contiguous array of at least 2 elements of type 'wchar_t'}}
1765 // both-note@-2 {{destination is not a contiguous array of at least 3 elements of type 'wchar_t'}}
1766 return result(arr);
1767 }
1768
1769 static_assert(test_wmemmove(1, 2, 1) == 1334);
1770 static_assert(test_wmemmove(2, 1, 1) == 1224);
1771 static_assert(test_wmemmove(0, 1, 2) == 2334);
1772 static_assert(test_wmemmove(1, 0, 2) == 1124);
1773 static_assert(test_wmemmove(1, 2, 1) == 1334);
1774 static_assert(test_wmemmove(0, 3, 1) == 4234);
1775 static_assert(test_wmemmove(0, 3, 2) == 4234); // both-error {{constant}} both-note {{in call}}
1776 static_assert(test_wmemmove(2, 0, 3) == 4234); // both-error {{constant}} both-note {{in call}}
1777
1778 wchar_t global;
1779 constexpr wchar_t *null = 0;
1780 static_assert(__builtin_wmemmove(&global, null, sizeof(wchar_t))); // both-error {{}} \
1781 // both-note {{source of 'wmemmove' is nullptr}}
1782 static_assert(__builtin_wmemmove(null, &global, sizeof(wchar_t))); // both-error {{}} \
1783 // both-note {{destination of 'wmemmove' is nullptr}}
1784
1785 // Check that a pointer to an incomplete array is rejected.
1786 constexpr int test_address_of_incomplete_array_type() { // both-error {{never produces a constant}}
1787 extern int arr[];
1788 __builtin_memmove(&arr, &arr, 4 * sizeof(arr[0])); // both-note 2{{cannot constant evaluate 'memmove' between objects of incomplete type 'int[]'}}
1789 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
1790 }
1791 static_assert(test_address_of_incomplete_array_type() == 1234); // both-error {{constant}} \
1792 // both-note {{in call}}
1793 }
1794
1795 namespace Invalid {
1796 constexpr int test() { // both-error {{never produces a constant expression}}
1797 __builtin_abort(); // both-note 2{{subexpression not valid in a constant expression}}
1798 return 0;
1799 }
1800 static_assert(test() == 0); // both-error {{not an integral constant expression}} \
1801 // both-note {{in call to}}
1802 }
1803
1804 #if __cplusplus >= 202002L
1805 namespace WithinLifetime {
1806 constexpr int a = 10;
1807 static_assert(__builtin_is_within_lifetime(&a));
1808
1809 consteval int IsActive(bool ReadB) {
1810 union {
1811 int a, b;
1812 } A;
1813 A.a = 10;
1814 if (ReadB)
1815 return __builtin_is_within_lifetime(&A.b);
1816 return __builtin_is_within_lifetime(&A.a);
1817 }
1818 static_assert(IsActive(false));
1819 static_assert(!IsActive(true));
1820
1821 static_assert(__builtin_is_within_lifetime((void*)nullptr)); // both-error {{not an integral constant expression}} \
1822 // both-note {{'__builtin_is_within_lifetime' cannot be called with a null pointer}}
1823
1824 constexpr int i = 2;
1825 constexpr int arr[2]{};
1826 void f() {
1827 __builtin_is_within_lifetime(&i + 1); // both-error {{call to consteval function '__builtin_is_within_lifetime' is not a constant expression}} \
1828 // both-note {{'__builtin_is_within_lifetime' cannot be called with a one-past-the-end pointer}} \
1829 // both-warning {{expression result unused}}
1830 __builtin_is_within_lifetime(arr + 2); // both-error {{call to consteval function '__builtin_is_within_lifetime' is not a constant expression}} \
1831 // both-note {{'__builtin_is_within_lifetime' cannot be called with a one-past-the-end pointer}} \
1832 // both-warning {{expression result unused}}
1833 }
1834
1835
1836 constexpr bool self = __builtin_is_within_lifetime(&self); // both-error {{must be initialized by a constant expression}} \
1837 // both-note {{'__builtin_is_within_lifetime' cannot be called with a pointer to an object whose lifetime has not yet begun}} \
1838 // ref-error {{call to consteval function '__builtin_is_within_lifetime' is not a constant expression}} \
1839 // ref-note {{initializer of 'self' is not a constant expression}} \
1840 // ref-note {{declared here}}
1841
1842 int nontCE(int p) { // both-note {{declared here}}
1843 return __builtin_is_within_lifetime(&p); // both-error {{call to consteval function}} \
1844 // both-note {{function parameter 'p' with unknown value cannot be used in a constant expression}}
1845 }
1846
1847
1848 struct XStd {
1849 consteval XStd() {
1850 __builtin_is_within_lifetime(this); // both-note {{cannot be called with a pointer to an object whose lifetime has not yet begun}}
1851 }
1852 } xstd; // both-error {{is not a constant expression}} \
1853 // both-note {{in call to}}
1854
1855 /// FIXME: We do not have per-element lifetime information for primitive arrays.
1856 /// See https://github.com/llvm/llvm-project/issues/147528
1857 consteval bool test_dynamic(bool read_after_deallocate) {
1858 std::allocator<int> a;
1859 int* p = a.allocate(1); // expected-note 2{{allocation performed here was not deallocated}}
1860 // a.allocate starts the lifetime of an array,
1861 // the complete object of *p has started its lifetime
1862 if (__builtin_is_within_lifetime(p))
1863 return false;
1864 std::construct_at(p);
1865 if (!__builtin_is_within_lifetime(p))
1866 return false;
1867 std::destroy_at(p);
1868 if (__builtin_is_within_lifetime(p))
1869 return false;
1870 a.deallocate(p, 1);
1871 if (read_after_deallocate)
1872 __builtin_is_within_lifetime(p); // ref-note {{read of heap allocated object that has been deleted}}
1873 return true;
1874 }
1875 static_assert(test_dynamic(false)); // expected-error {{not an integral constant expression}}
1876 static_assert(test_dynamic(true)); // both-error {{not an integral constant expression}} \
1877 // ref-note {{in call to}}
1878 }
1879
1880 #ifdef __SIZEOF_INT128__
1881 namespace I128Mul {
1882 constexpr int mul() {
1883 __int128 A = 10;
1884 __int128 B = 10;
1885 __int128 R;
1886 __builtin_mul_overflow(A, B, &R);
1887 return 1;
1888 }
1889 static_assert(mul() == 1);
1890 }
1891 #endif
1892
1893 namespace InitParam {
1894 constexpr int foo(int a) {
1895 __builtin_mul_overflow(20, 10, &a);
1896 return a;
1897 }
1898 static_assert(foo(10) == 200);
1899 }
1900
1901 #endif
1902
1903 namespace NonBlockPointerStore {
1904 int a;
1905 void foo(void) { a *= __builtin_sadd_overflow(1, 2, 0); }
1906 void foo2(void) { a *= __builtin_addc(1, 2, 0, 0); }
1907 }