Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / constexpr-string.cpp
blobc456740ef7551f70c930ce64f5782ac351232896
1 // RUN: %clang_cc1 %s -triple x86_64-linux-gnu -std=c++2a -fsyntax-only -verify -pedantic -Wno-vla-extension
2 // RUN: %clang_cc1 %s -triple x86_64-linux-gnu -std=gnu++2a -fsyntax-only -verify -pedantic -Wno-vla-extension -DGNUMODE
3 // RUN: %clang_cc1 %s -triple x86_64-linux-gnu -std=c++2a -fsyntax-only -verify -pedantic -Wno-vla-extension -fno-signed-char
4 // RUN: %clang_cc1 %s -triple x86_64-linux-gnu -std=c++2a -fsyntax-only -verify -pedantic -Wno-vla-extension -fno-wchar -DNO_PREDEFINED_WCHAR_T
5 // RUN: %clang_cc1 %s -triple armebv7-unknown-linux -std=c++2a -fsyntax-only -verify -pedantic -Wno-vla-extension
6 // RUN: %clang_cc1 %s -triple armebv7-unknown-linux -std=gnu++2a -fsyntax-only -verify -pedantic -Wno-vla-extension -DGNUMODE
7 // RUN: %clang_cc1 %s -triple armebv7-unknown-linux -std=c++2a -fsyntax-only -verify -pedantic -Wno-vla-extension -fno-signed-char
8 // RUN: %clang_cc1 %s -triple armebv7-unknown-linux -std=c++2a -fsyntax-only -verify -pedantic -Wno-vla-extension -fno-wchar -DNO_PREDEFINED_WCHAR_T
10 # 9 "/usr/include/string.h" 1 3 4 // expected-warning {{this style of line directive is a GNU extension}}
11 extern "C" {
12 typedef decltype(sizeof(int)) size_t;
14 extern size_t strlen(const char *p);
16 extern int strcmp(const char *s1, const char *s2);
17 extern int strncmp(const char *s1, const char *s2, size_t n);
18 extern int memcmp(const void *s1, const void *s2, size_t n);
20 #ifdef GNUMODE
21 extern int bcmp(const void *s1, const void *s2, size_t n);
22 #endif
24 extern char *strchr(const char *s, int c);
25 extern void *memchr(const void *s, int c, size_t n);
27 extern void *memcpy(void *d, const void *s, size_t n);
28 extern void *memmove(void *d, const void *s, size_t n);
30 # 25 "SemaCXX/constexpr-string.cpp" 2
32 # 27 "/usr/include/wchar.h" 1 3 4 // expected-warning {{this style of line directive is a GNU extension}}
33 extern "C" {
34 #if NO_PREDEFINED_WCHAR_T
35 typedef decltype(L'0') wchar_t;
36 #endif
37 extern size_t wcslen(const wchar_t *p);
39 extern int wcscmp(const wchar_t *s1, const wchar_t *s2);
40 extern int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n);
41 extern int wmemcmp(const wchar_t *s1, const wchar_t *s2, size_t n);
43 extern wchar_t *wcschr(const wchar_t *s, wchar_t c);
44 extern wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n);
46 extern wchar_t *wmemcpy(wchar_t *d, const wchar_t *s, size_t n);
47 extern wchar_t *wmemmove(wchar_t *d, const wchar_t *s, size_t n);
50 # 51 "SemaCXX/constexpr-string.cpp" 2
51 namespace Strlen {
52 constexpr int n = __builtin_strlen("hello"); // ok
53 static_assert(n == 5);
54 constexpr int wn = __builtin_wcslen(L"hello"); // ok
55 static_assert(wn == 5);
56 constexpr int m = strlen("hello"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strlen' cannot be used in a constant expression}}
57 constexpr int wm = wcslen(L"hello"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'wcslen' cannot be used in a constant expression}}
59 // Make sure we can evaluate a call to strlen.
60 int arr[3]; // expected-note 2{{here}}
61 int k = arr[strlen("hello")]; // expected-warning {{array index 5}}
62 int wk = arr[wcslen(L"hello")]; // expected-warning {{array index 5}}
65 namespace StrcmpEtc {
66 constexpr char kFoobar[6] = {'f','o','o','b','a','r'};
67 constexpr char kFoobazfoobar[12] = {'f','o','o','b','a','z','f','o','o','b','a','r'};
69 static_assert(__builtin_strcmp("abab", "abab") == 0);
70 static_assert(__builtin_strcmp("abab", "abba") == -1);
71 static_assert(__builtin_strcmp("abab", "abaa") == 1);
72 static_assert(__builtin_strcmp("ababa", "abab") == 1);
73 static_assert(__builtin_strcmp("abab", "ababa") == -1);
74 static_assert(__builtin_strcmp("a\203", "a") == 1);
75 static_assert(__builtin_strcmp("a\203", "a\003") == 1);
76 static_assert(__builtin_strcmp("abab\0banana", "abab") == 0);
77 static_assert(__builtin_strcmp("abab", "abab\0banana") == 0);
78 static_assert(__builtin_strcmp("abab\0banana", "abab\0canada") == 0);
79 static_assert(__builtin_strcmp(0, "abab") == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
80 static_assert(__builtin_strcmp("abab", 0) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
82 static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar) == -1); // FIXME: Should we reject this?
83 static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar + 6) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
85 static_assert(__builtin_strncmp("abaa", "abba", 5) == -1);
86 static_assert(__builtin_strncmp("abaa", "abba", 4) == -1);
87 static_assert(__builtin_strncmp("abaa", "abba", 3) == -1);
88 static_assert(__builtin_strncmp("abaa", "abba", 2) == 0);
89 static_assert(__builtin_strncmp("abaa", "abba", 1) == 0);
90 static_assert(__builtin_strncmp("abaa", "abba", 0) == 0);
91 static_assert(__builtin_strncmp(0, 0, 0) == 0);
92 static_assert(__builtin_strncmp("abab\0banana", "abab\0canada", 100) == 0);
94 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 6) == -1);
95 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 7) == -1); // FIXME: Should we reject this?
96 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 6) == 0);
97 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 7) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
99 static_assert(__builtin_memcmp("abaa", "abba", 3) == -1);
100 static_assert(__builtin_memcmp("abaa", "abba", 2) == 0);
101 static_assert(__builtin_memcmp("a\203", "a", 2) == 1);
102 static_assert(__builtin_memcmp("a\203", "a\003", 2) == 1);
103 static_assert(__builtin_memcmp(0, 0, 0) == 0);
104 static_assert(__builtin_memcmp("abab\0banana", "abab\0banana", 100) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
105 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 100) == -1); // FIXME: Should we reject this?
106 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 7) == -1);
107 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 6) == -1);
108 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 5) == 0);
110 static_assert(__builtin_memcmp(u8"abaa", u8"abba", 3) == -1);
111 static_assert(__builtin_memcmp(u8"abaa", u8"abba", 2) == 0);
112 static_assert(__builtin_memcmp(u8"a\203", u8"a", 2) == 1);
113 static_assert(__builtin_memcmp(u8"a\203", u8"a\003", 2) == 1);
114 static_assert(__builtin_memcmp(0, 0, 0) == 0);
115 static_assert(__builtin_memcmp(u8"abab\0banana", u8"abab\0banana", 100) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
116 static_assert(__builtin_memcmp(u8"abab\0banana", u8"abab\0canada", 100) == -1); // FIXME: Should we reject this?
117 static_assert(__builtin_memcmp(u8"abab\0banana", u8"abab\0canada", 7) == -1);
118 static_assert(__builtin_memcmp(u8"abab\0banana", u8"abab\0canada", 6) == -1);
119 static_assert(__builtin_memcmp(u8"abab\0banana", u8"abab\0canada", 5) == 0);
121 static_assert(__builtin_memcmp(u8"\u1234", "\xE1\x88\xB4", 4) == 0);
122 static_assert(__builtin_memcmp(u8"\u1234", "\xE1\x88\xB3", 4) == 1);
124 static_assert(__builtin_bcmp("abaa", "abba", 3) != 0);
125 static_assert(__builtin_bcmp("abaa", "abba", 2) == 0);
126 static_assert(__builtin_bcmp("a\203", "a", 2) != 0);
127 static_assert(__builtin_bcmp("a\203", "a\003", 2) != 0);
128 static_assert(__builtin_bcmp(0, 0, 0) == 0);
129 static_assert(__builtin_bcmp("abab\0banana", "abab\0banana", 100) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
130 static_assert(__builtin_bcmp("abab\0banana", "abab\0canada", 100) != 0); // FIXME: Should we reject this?
131 static_assert(__builtin_bcmp("abab\0banana", "abab\0canada", 7) != 0);
132 static_assert(__builtin_bcmp("abab\0banana", "abab\0canada", 6) != 0);
133 static_assert(__builtin_bcmp("abab\0banana", "abab\0canada", 5) == 0);
135 extern struct Incomplete incomplete;
136 static_assert(__builtin_memcmp(&incomplete, "", 0u) == 0);
137 static_assert(__builtin_memcmp("", &incomplete, 0u) == 0);
138 static_assert(__builtin_memcmp(&incomplete, "", 1u) == 42); // expected-error {{not an integral constant}} expected-note {{not supported}}
139 static_assert(__builtin_memcmp("", &incomplete, 1u) == 42); // expected-error {{not an integral constant}} expected-note {{not supported}}
141 static_assert(__builtin_bcmp(&incomplete, "", 0u) == 0);
142 static_assert(__builtin_bcmp("", &incomplete, 0u) == 0);
143 static_assert(__builtin_bcmp(&incomplete, "", 1u) == 42); // expected-error {{not an integral constant}} expected-note {{not supported}}
144 static_assert(__builtin_bcmp("", &incomplete, 1u) == 42); // expected-error {{not an integral constant}} expected-note {{not supported}}
146 constexpr unsigned char ku00fe00[] = {0x00, 0xfe, 0x00};
147 constexpr unsigned char ku00feff[] = {0x00, 0xfe, 0xff};
148 constexpr signed char ks00fe00[] = {0, -2, 0};
149 constexpr signed char ks00feff[] = {0, -2, -1};
150 static_assert(__builtin_memcmp(ku00feff, ks00fe00, 2) == 0);
151 static_assert(__builtin_memcmp(ku00feff, ks00fe00, 99) == 1);
152 static_assert(__builtin_memcmp(ku00fe00, ks00feff, 99) == -1);
153 static_assert(__builtin_memcmp(ks00feff, ku00fe00, 2) == 0);
154 static_assert(__builtin_memcmp(ks00feff, ku00fe00, 99) == 1);
155 static_assert(__builtin_memcmp(ks00fe00, ku00feff, 99) == -1);
156 static_assert(__builtin_memcmp(ks00fe00, ks00feff, 2) == 0);
157 static_assert(__builtin_memcmp(ks00feff, ks00fe00, 99) == 1);
158 static_assert(__builtin_memcmp(ks00fe00, ks00feff, 99) == -1);
160 static_assert(__builtin_bcmp(ku00feff, ks00fe00, 2) == 0);
161 static_assert(__builtin_bcmp(ku00feff, ks00fe00, 99) != 0);
162 static_assert(__builtin_bcmp(ku00fe00, ks00feff, 99) != 0);
163 static_assert(__builtin_bcmp(ks00feff, ku00fe00, 2) == 0);
164 static_assert(__builtin_bcmp(ks00feff, ku00fe00, 99) != 0);
165 static_assert(__builtin_bcmp(ks00fe00, ku00feff, 99) != 0);
166 static_assert(__builtin_bcmp(ks00fe00, ks00feff, 2) == 0);
167 static_assert(__builtin_bcmp(ks00feff, ks00fe00, 99) != 0);
168 static_assert(__builtin_bcmp(ks00fe00, ks00feff, 99) != 0);
170 struct Bool3Tuple { bool bb[3]; };
171 constexpr Bool3Tuple kb000100 = {{false, true, false}};
172 static_assert(sizeof(bool) != 1u || __builtin_memcmp(ks00fe00, kb000100.bb, 1) == 0); // expected-error {{constant}} expected-note {{not supported}}
173 static_assert(sizeof(bool) != 1u || __builtin_memcmp(ks00fe00, kb000100.bb, 2) == 1); // expected-error {{constant}} expected-note {{not supported}}
175 static_assert(sizeof(bool) != 1u || __builtin_bcmp(ks00fe00, kb000100.bb, 1) == 0); // expected-error {{constant}} expected-note {{not supported}}
176 static_assert(sizeof(bool) != 1u || __builtin_bcmp(ks00fe00, kb000100.bb, 2) != 0); // expected-error {{constant}} expected-note {{not supported}}
178 constexpr long ksl[] = {0, -1};
179 constexpr unsigned int kui[] = {0, 0u - 1};
180 constexpr unsigned long long kull[] = {0, 0ull - 1};
181 constexpr const auto *kuSizeofLong(void) {
182 if constexpr(sizeof(long) == sizeof(int)) {
183 return kui;
184 } else if constexpr(sizeof(long) == sizeof(long long)) {
185 return kull;
186 } else {
187 return nullptr;
190 static_assert(__builtin_memcmp(ksl, kuSizeofLong(), sizeof(long) - 1) == 0); // expected-error {{constant}} expected-note {{not supported}}
191 static_assert(__builtin_memcmp(ksl, kuSizeofLong(), sizeof(long) + 0) == 0); // expected-error {{constant}} expected-note {{not supported}}
192 static_assert(__builtin_memcmp(ksl, kuSizeofLong(), sizeof(long) + 1) == 0); // expected-error {{constant}} expected-note {{not supported}}
193 static_assert(__builtin_memcmp(ksl, kuSizeofLong(), 2*sizeof(long) - 1) == 0); // expected-error {{constant}} expected-note {{not supported}}
194 static_assert(__builtin_memcmp(ksl, kuSizeofLong(), 2*sizeof(long) + 0) == 0); // expected-error {{constant}} expected-note {{not supported}}
195 static_assert(__builtin_memcmp(ksl, kuSizeofLong(), 2*sizeof(long) + 1) == 42); // expected-error {{constant}} expected-note {{not supported}}
196 static_assert(__builtin_memcmp(ksl + 1, kuSizeofLong() + 1, sizeof(long) - 1) == 0); // expected-error {{constant}} expected-note {{not supported}}
197 static_assert(__builtin_memcmp(ksl + 1, kuSizeofLong() + 1, sizeof(long) + 0) == 0); // expected-error {{constant}} expected-note {{not supported}}
198 static_assert(__builtin_memcmp(ksl + 1, kuSizeofLong() + 1, sizeof(long) + 1) == 42); // expected-error {{constant}} expected-note {{not supported}}
200 static_assert(__builtin_bcmp(ksl, kuSizeofLong(), sizeof(long) - 1) == 0); // expected-error {{constant}} expected-note {{not supported}}
201 static_assert(__builtin_bcmp(ksl, kuSizeofLong(), sizeof(long) + 0) == 0); // expected-error {{constant}} expected-note {{not supported}}
202 static_assert(__builtin_bcmp(ksl, kuSizeofLong(), sizeof(long) + 1) == 0); // expected-error {{constant}} expected-note {{not supported}}
203 static_assert(__builtin_bcmp(ksl, kuSizeofLong(), 2*sizeof(long) - 1) == 0); // expected-error {{constant}} expected-note {{not supported}}
204 static_assert(__builtin_bcmp(ksl, kuSizeofLong(), 2*sizeof(long) + 0) == 0); // expected-error {{constant}} expected-note {{not supported}}
205 static_assert(__builtin_bcmp(ksl, kuSizeofLong(), 2*sizeof(long) + 1) == 42); // expected-error {{constant}} expected-note {{not supported}}
206 static_assert(__builtin_bcmp(ksl + 1, kuSizeofLong() + 1, sizeof(long) - 1) == 0); // expected-error {{constant}} expected-note {{not supported}}
207 static_assert(__builtin_bcmp(ksl + 1, kuSizeofLong() + 1, sizeof(long) + 0) == 0); // expected-error {{constant}} expected-note {{not supported}}
208 static_assert(__builtin_bcmp(ksl + 1, kuSizeofLong() + 1, sizeof(long) + 1) == 42); // expected-error {{constant}} expected-note {{not supported}}
210 constexpr int a = strcmp("hello", "world"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strcmp' cannot be used in a constant expression}}
211 constexpr int b = strncmp("hello", "world", 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strncmp' cannot be used in a constant expression}}
212 constexpr int c = memcmp("hello", "world", 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'memcmp' cannot be used in a constant expression}}
214 #ifdef GNUMODE
215 constexpr int d = bcmp("hello", "world", 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'bcmp' cannot be used in a constant expression}}
216 #endif
219 namespace MultibyteElementTests {
220 inline namespace Util {
221 #define STR2(X) #X
222 #define STR(X) STR2(X)
223 constexpr const char ByteOrderString[] = STR(__BYTE_ORDER__);
224 #undef STR
225 #undef STR2
226 constexpr bool LittleEndian{*ByteOrderString == '1'};
228 constexpr size_t GoodFoldArraySize = 42, BadFoldArraySize = 43;
229 struct NotBadFoldResult {};
230 template <size_t> struct FoldResult;
231 template <> struct FoldResult<GoodFoldArraySize> : NotBadFoldResult {};
232 template <typename T, size_t N>
233 FoldResult<N> *foldResultImpl(T (*ptrToConstantSizeArray)[N]);
234 struct NotFolded : NotBadFoldResult {};
235 NotFolded *foldResultImpl(bool anyPtr);
236 template <auto Value> struct MetaValue;
237 template <typename Callable, size_t N, auto ExpectedFoldResult>
238 auto foldResult(const Callable &, MetaValue<N> *,
239 MetaValue<ExpectedFoldResult> *) {
240 int (*maybeVLAPtr)[Callable{}(N) == ExpectedFoldResult
241 ? GoodFoldArraySize
242 : BadFoldArraySize] = 0;
243 return foldResultImpl(maybeVLAPtr);
245 template <typename FoldResultKind, typename Callable, typename NWrap,
246 typename ExpectedWrap>
247 constexpr bool checkFoldResult(const Callable &c, NWrap *n, ExpectedWrap *e) {
248 decltype(static_cast<FoldResultKind *>(foldResult(c, n, e))) *chk{};
249 return true;
251 template <size_t N> constexpr MetaValue<N> *withN() { return nullptr; }
252 template <auto Expected> constexpr MetaValue<Expected> *withExpected() {
253 return nullptr;
255 } // namespace Util
256 } // namespace MultibyteElementTests
258 namespace MultibyteElementTests::Memcmp {
259 #ifdef __SIZEOF_INT128__
260 constexpr __int128 i128_ff_8_00_8 = -(__int128)1 - -1ull;
261 constexpr __int128 i128_00_16 = 0;
262 static_assert(checkFoldResult<NotBadFoldResult>(
263 [](size_t n) constexpr {
264 return __builtin_memcmp(&i128_ff_8_00_8, &i128_00_16, n);
266 withN<1u>(), withExpected<LittleEndian ? 0 : 1>()));
267 #endif
269 constexpr const signed char ByteOrderStringReduced[] = {
270 ByteOrderString[0] - '0', ByteOrderString[1] - '0',
271 ByteOrderString[2] - '0', ByteOrderString[3] - '0',
273 constexpr signed int i04030201 = 0x04030201;
274 constexpr unsigned int u04030201 = 0x04030201u;
275 static_assert(checkFoldResult<NotBadFoldResult>(
276 [](size_t n) constexpr {
277 return __builtin_memcmp(ByteOrderStringReduced, &i04030201, n);
279 withN<sizeof(int)>(), withExpected<0>()));
280 static_assert(checkFoldResult<NotBadFoldResult>(
281 [](size_t n) constexpr {
282 return __builtin_memcmp(&u04030201, ByteOrderStringReduced, n);
284 withN<sizeof(int)>(), withExpected<0>()));
286 constexpr unsigned int ui0000FEFF = 0x0000feffU;
287 constexpr unsigned short usFEFF = 0xfeffU;
288 static_assert(checkFoldResult<NotBadFoldResult>(
289 [](size_t n) constexpr {
290 return __builtin_memcmp(&ui0000FEFF, &usFEFF, n);
292 withN<1u>(), withExpected<LittleEndian ? 0 : -1>()));
294 constexpr unsigned int ui08038700 = 0x08038700u;
295 constexpr unsigned int ui08048600 = 0x08048600u;
296 static_assert(checkFoldResult<NotBadFoldResult>(
297 [](size_t n) constexpr {
298 return __builtin_memcmp(&ui08038700, &ui08048600, n);
300 withN<sizeof(int)>(), withExpected<LittleEndian ? 1 : -1>()));
303 namespace WcscmpEtc {
304 constexpr wchar_t kFoobar[6] = {L'f',L'o',L'o',L'b',L'a',L'r'};
305 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'};
307 static_assert(__builtin_wcscmp(L"abab", L"abab") == 0);
308 static_assert(__builtin_wcscmp(L"abab", L"abba") == -1);
309 static_assert(__builtin_wcscmp(L"abab", L"abaa") == 1);
310 static_assert(__builtin_wcscmp(L"ababa", L"abab") == 1);
311 static_assert(__builtin_wcscmp(L"abab", L"ababa") == -1);
312 static_assert(__builtin_wcscmp(L"abab\0banana", L"abab") == 0);
313 static_assert(__builtin_wcscmp(L"abab", L"abab\0banana") == 0);
314 static_assert(__builtin_wcscmp(L"abab\0banana", L"abab\0canada") == 0);
315 #if __WCHAR_WIDTH__ == 32
316 static_assert(__builtin_wcscmp(L"a\x83838383", L"a") == (wchar_t)-1U >> 31);
317 #endif
318 static_assert(__builtin_wcscmp(0, L"abab") == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
319 static_assert(__builtin_wcscmp(L"abab", 0) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
321 static_assert(__builtin_wcscmp(kFoobar, kFoobazfoobar) == -1); // FIXME: Should we reject this?
322 static_assert(__builtin_wcscmp(kFoobar, kFoobazfoobar + 6) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
324 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 5) == -1);
325 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 4) == -1);
326 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 3) == -1);
327 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 2) == 0);
328 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 1) == 0);
329 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 0) == 0);
330 static_assert(__builtin_wcsncmp(0, 0, 0) == 0);
331 static_assert(__builtin_wcsncmp(L"abab\0banana", L"abab\0canada", 100) == 0);
332 #if __WCHAR_WIDTH__ == 32
333 static_assert(__builtin_wcsncmp(L"a\x83838383", L"aa", 2) ==
334 (wchar_t)-1U >> 31);
335 #endif
337 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar, 6) == -1);
338 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar, 7) == -1); // FIXME: Should we reject this?
339 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar + 6, 6) == 0);
340 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar + 6, 7) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
342 static_assert(__builtin_wmemcmp(L"abaa", L"abba", 3) == -1);
343 static_assert(__builtin_wmemcmp(L"abaa", L"abba", 2) == 0);
344 static_assert(__builtin_wmemcmp(0, 0, 0) == 0);
345 #if __WCHAR_WIDTH__ == 32
346 static_assert(__builtin_wmemcmp(L"a\x83838383", L"aa", 2) ==
347 (wchar_t)-1U >> 31);
348 #endif
349 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0banana", 100) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
350 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 100) == -1); // FIXME: Should we reject this?
351 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 7) == -1);
352 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 6) == -1);
353 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 5) == 0);
355 constexpr int a = wcscmp(L"hello", L"world"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'wcscmp' cannot be used in a constant expression}}
356 constexpr int b = wcsncmp(L"hello", L"world", 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'wcsncmp' cannot be used in a constant expression}}
357 constexpr int c = wmemcmp(L"hello", L"world", 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'wmemcmp' cannot be used in a constant expression}}
360 namespace StrchrEtc {
361 constexpr const char *kStr = "abca\xff\0d";
362 constexpr char kFoo[] = {'f', 'o', 'o'};
363 static_assert(__builtin_strchr(kStr, 'a') == kStr);
364 static_assert(__builtin_strchr(kStr, 'b') == kStr + 1);
365 static_assert(__builtin_strchr(kStr, 'c') == kStr + 2);
366 static_assert(__builtin_strchr(kStr, 'd') == nullptr);
367 static_assert(__builtin_strchr(kStr, 'e') == nullptr);
368 static_assert(__builtin_strchr(kStr, '\0') == kStr + 5);
369 static_assert(__builtin_strchr(kStr, 'a' + 256) == nullptr);
370 static_assert(__builtin_strchr(kStr, 'a' - 256) == nullptr);
371 static_assert(__builtin_strchr(kStr, '\xff') == kStr + 4);
372 static_assert(__builtin_strchr(kStr, '\xff' + 256) == nullptr);
373 static_assert(__builtin_strchr(kStr, '\xff' - 256) == nullptr);
374 static_assert(__builtin_strchr(kFoo, 'o') == kFoo + 1);
375 static_assert(__builtin_strchr(kFoo, 'x') == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
376 static_assert(__builtin_strchr(nullptr, 'x') == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
378 static_assert(__builtin_memchr(kStr, 'a', 0) == nullptr);
379 static_assert(__builtin_memchr(kStr, 'a', 1) == kStr);
380 static_assert(__builtin_memchr(kStr, '\0', 5) == nullptr);
381 static_assert(__builtin_memchr(kStr, '\0', 6) == kStr + 5);
382 static_assert(__builtin_memchr(kStr, '\xff', 8) == kStr + 4);
383 static_assert(__builtin_memchr(kStr, '\xff' + 256, 8) == kStr + 4);
384 static_assert(__builtin_memchr(kStr, '\xff' - 256, 8) == kStr + 4);
385 static_assert(__builtin_memchr(kFoo, 'x', 3) == nullptr);
386 static_assert(__builtin_memchr(kFoo, 'x', 4) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
387 static_assert(__builtin_memchr(nullptr, 'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
388 static_assert(__builtin_memchr(nullptr, 'x', 0) == nullptr); // FIXME: Should we reject this?
390 constexpr const char8_t *kU8Str = u8"abca\xff\0d";
391 constexpr char8_t kU8Foo[] = {u8'f', u8'o', u8'o'};
392 static_assert(__builtin_memchr(kU8Str, u8'a', 0) == nullptr);
393 static_assert(__builtin_memchr(kU8Str, u8'a', 1) == kU8Str);
394 static_assert(__builtin_memchr(kU8Str, u8'\0', 5) == nullptr);
395 static_assert(__builtin_memchr(kU8Str, u8'\0', 6) == kU8Str + 5);
396 static_assert(__builtin_memchr(kU8Str, u8'\xff', 8) == kU8Str + 4);
397 static_assert(__builtin_memchr(kU8Str, u8'\xff' + 256, 8) == kU8Str + 4);
398 static_assert(__builtin_memchr(kU8Str, u8'\xff' - 256, 8) == kU8Str + 4);
399 static_assert(__builtin_memchr(kU8Foo, u8'x', 3) == nullptr);
400 static_assert(__builtin_memchr(kU8Foo, u8'x', 4) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
401 static_assert(__builtin_memchr(nullptr, u8'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
402 static_assert(__builtin_memchr(nullptr, u8'x', 0) == nullptr); // FIXME: Should we reject this?
404 extern struct Incomplete incomplete;
405 static_assert(__builtin_memchr(&incomplete, 0, 0u) == nullptr);
406 static_assert(__builtin_memchr(&incomplete, 0, 1u) == nullptr); // expected-error {{not an integral constant}} expected-note {{read of incomplete type 'struct Incomplete'}}
408 const unsigned char &u1 = 0xf0;
409 auto &&i1 = (const signed char []){-128}; // expected-warning {{compound literals are a C99-specific feature}}
410 static_assert(__builtin_memchr(&u1, -(0x0f + 1), 1) == &u1);
411 static_assert(__builtin_memchr(i1, 0x80, 1) == i1);
413 enum class E : unsigned char {};
414 struct EPair { E e, f; };
415 constexpr EPair ee{E{240}};
416 static_assert(__builtin_memchr(&ee.e, 240, 1) == &ee.e); // expected-error {{constant}} expected-note {{not supported}}
418 constexpr bool kBool[] = {false, true, false};
419 constexpr const bool *const kBoolPastTheEndPtr = kBool + 3;
420 static_assert(sizeof(bool) != 1u || __builtin_memchr(kBoolPastTheEndPtr - 3, 1, 99) == kBool + 1); // expected-error {{constant}} expected-note {{not supported}}
421 static_assert(sizeof(bool) != 1u || __builtin_memchr(kBool + 1, 0, 99) == kBoolPastTheEndPtr - 1); // expected-error {{constant}} expected-note {{not supported}}
422 static_assert(sizeof(bool) != 1u || __builtin_memchr(kBoolPastTheEndPtr - 3, -1, 3) == nullptr); // expected-error {{constant}} expected-note {{not supported}}
423 static_assert(sizeof(bool) != 1u || __builtin_memchr(kBoolPastTheEndPtr, 0, 1) == nullptr); // expected-error {{constant}} expected-note {{not supported}}
425 static_assert(__builtin_char_memchr(kStr, 'a', 0) == nullptr);
426 static_assert(__builtin_char_memchr(kStr, 'a', 1) == kStr);
427 static_assert(__builtin_char_memchr(kStr, '\0', 5) == nullptr);
428 static_assert(__builtin_char_memchr(kStr, '\0', 6) == kStr + 5);
429 static_assert(__builtin_char_memchr(kStr, '\xff', 8) == kStr + 4);
430 static_assert(__builtin_char_memchr(kStr, '\xff' + 256, 8) == kStr + 4);
431 static_assert(__builtin_char_memchr(kStr, '\xff' - 256, 8) == kStr + 4);
432 static_assert(__builtin_char_memchr(kFoo, 'x', 3) == nullptr);
433 static_assert(__builtin_char_memchr(kFoo, 'x', 4) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
434 static_assert(__builtin_char_memchr(nullptr, 'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
435 static_assert(__builtin_char_memchr(nullptr, 'x', 0) == nullptr); // FIXME: Should we reject this?
437 static_assert(*__builtin_char_memchr(kStr, '\xff', 8) == '\xff');
438 constexpr bool char_memchr_mutable() {
439 char buffer[] = "mutable";
440 *__builtin_char_memchr(buffer, 't', 8) = 'r';
441 *__builtin_char_memchr(buffer, 'm', 8) = 'd';
442 return __builtin_strcmp(buffer, "durable") == 0;
444 static_assert(char_memchr_mutable());
446 constexpr bool a = !strchr("hello", 'h'); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strchr' cannot be used in a constant expression}}
447 constexpr bool b = !memchr("hello", 'h', 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'memchr' cannot be used in a constant expression}}
450 namespace MultibyteElementTests::Memchr {
451 constexpr unsigned int u04030201 = 0x04030201;
452 static_assert(checkFoldResult<NotBadFoldResult>(
453 [](size_t n) constexpr {
454 return __builtin_memchr(&u04030201, *ByteOrderString - '0', n);
456 withN<1u>(), withExpected<&u04030201>()));
458 constexpr unsigned int uED = 0xEDU;
459 static_assert(checkFoldResult<NotBadFoldResult>(
460 [](size_t n) constexpr {
461 return __builtin_memchr(&uED, 0xED, n);
463 withN<1u>(), withExpected<LittleEndian ? &uED : nullptr>()));
466 namespace WcschrEtc {
467 constexpr const wchar_t *kStr = L"abca\xffff\0dL";
468 constexpr wchar_t kFoo[] = {L'f', L'o', L'o'};
469 static_assert(__builtin_wcschr(kStr, L'a') == kStr);
470 static_assert(__builtin_wcschr(kStr, L'b') == kStr + 1);
471 static_assert(__builtin_wcschr(kStr, L'c') == kStr + 2);
472 static_assert(__builtin_wcschr(kStr, L'd') == nullptr);
473 static_assert(__builtin_wcschr(kStr, L'e') == nullptr);
474 static_assert(__builtin_wcschr(kStr, L'\0') == kStr + 5);
475 static_assert(__builtin_wcschr(kStr, L'a' + 256) == nullptr);
476 static_assert(__builtin_wcschr(kStr, L'a' - 256) == nullptr);
477 static_assert(__builtin_wcschr(kStr, L'\xffff') == kStr + 4);
478 static_assert(__builtin_wcschr(kFoo, L'o') == kFoo + 1);
479 static_assert(__builtin_wcschr(kFoo, L'x') == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
480 static_assert(__builtin_wcschr(nullptr, L'x') == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
482 static_assert(__builtin_wmemchr(kStr, L'a', 0) == nullptr);
483 static_assert(__builtin_wmemchr(kStr, L'a', 1) == kStr);
484 static_assert(__builtin_wmemchr(kStr, L'\0', 5) == nullptr);
485 static_assert(__builtin_wmemchr(kStr, L'\0', 6) == kStr + 5);
486 static_assert(__builtin_wmemchr(kStr, L'\xffff', 8) == kStr + 4);
487 static_assert(__builtin_wmemchr(kFoo, L'x', 3) == nullptr);
488 static_assert(__builtin_wmemchr(kFoo, L'x', 4) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
489 static_assert(__builtin_wmemchr(nullptr, L'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
490 static_assert(__builtin_wmemchr(nullptr, L'x', 0) == nullptr); // FIXME: Should we reject this?
492 constexpr bool a = !wcschr(L"hello", L'h'); // expected-error {{constant expression}} expected-note {{non-constexpr function 'wcschr' cannot be used in a constant expression}}
493 constexpr bool b = !wmemchr(L"hello", L'h', 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'wmemchr' cannot be used in a constant expression}}
496 namespace MemcpyEtc {
497 template<typename T>
498 constexpr T result(T (&arr)[4]) {
499 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
502 constexpr int test_memcpy(int a, int b, int n) {
503 int arr[4] = {1, 2, 3, 4};
504 __builtin_memcpy(arr + a, arr + b, n);
505 // expected-note@-1 2{{overlapping memory regions}}
506 // expected-note@-2 {{size to copy (1) is not a multiple of size of element type 'int'}}
507 // expected-note@-3 {{source is not a contiguous array of at least 2 elements of type 'int'}}
508 // expected-note@-4 {{destination is not a contiguous array of at least 3 elements of type 'int'}}
509 return result(arr);
511 constexpr int test_memmove(int a, int b, int n) {
512 int arr[4] = {1, 2, 3, 4};
513 __builtin_memmove(arr + a, arr + b, n);
514 // expected-note@-1 {{size to copy (1) is not a multiple of size of element type 'int'}}
515 // expected-note@-2 {{source is not a contiguous array of at least 2 elements of type 'int'}}
516 // expected-note@-3 {{destination is not a contiguous array of at least 3 elements of type 'int'}}
517 return result(arr);
519 constexpr int test_wmemcpy(int a, int b, int n) {
520 wchar_t arr[4] = {1, 2, 3, 4};
521 __builtin_wmemcpy(arr + a, arr + b, n);
522 // expected-note@-1 2{{overlapping memory regions}}
523 // expected-note@-2 {{source is not a contiguous array of at least 2 elements of type 'wchar_t'}}
524 // expected-note@-3 {{destination is not a contiguous array of at least 3 elements of type 'wchar_t'}}
525 return result(arr);
527 constexpr int test_wmemmove(int a, int b, int n) {
528 wchar_t arr[4] = {1, 2, 3, 4};
529 __builtin_wmemmove(arr + a, arr + b, n);
530 // expected-note@-1 {{source is not a contiguous array of at least 2 elements of type 'wchar_t'}}
531 // expected-note@-2 {{destination is not a contiguous array of at least 3 elements of type 'wchar_t'}}
532 return result(arr);
535 static_assert(test_memcpy(1, 2, 4) == 1334);
536 static_assert(test_memcpy(2, 1, 4) == 1224);
537 static_assert(test_memcpy(0, 1, 8) == 2334); // expected-error {{constant}} expected-note {{in call}}
538 static_assert(test_memcpy(1, 0, 8) == 1124); // expected-error {{constant}} expected-note {{in call}}
539 static_assert(test_memcpy(1, 2, 1) == 1334); // expected-error {{constant}} expected-note {{in call}}
540 static_assert(test_memcpy(0, 3, 4) == 4234);
541 static_assert(test_memcpy(0, 3, 8) == 4234); // expected-error {{constant}} expected-note {{in call}}
542 static_assert(test_memcpy(2, 0, 12) == 4234); // expected-error {{constant}} expected-note {{in call}}
544 static_assert(test_memmove(1, 2, 4) == 1334);
545 static_assert(test_memmove(2, 1, 4) == 1224);
546 static_assert(test_memmove(0, 1, 8) == 2334);
547 static_assert(test_memmove(1, 0, 8) == 1124);
548 static_assert(test_memmove(1, 2, 1) == 1334); // expected-error {{constant}} expected-note {{in call}}
549 static_assert(test_memmove(0, 3, 4) == 4234);
550 static_assert(test_memmove(0, 3, 8) == 4234); // expected-error {{constant}} expected-note {{in call}}
551 static_assert(test_memmove(2, 0, 12) == 4234); // expected-error {{constant}} expected-note {{in call}}
553 static_assert(test_wmemcpy(1, 2, 1) == 1334);
554 static_assert(test_wmemcpy(2, 1, 1) == 1224);
555 static_assert(test_wmemcpy(0, 1, 2) == 2334); // expected-error {{constant}} expected-note {{in call}}
556 static_assert(test_wmemcpy(1, 0, 2) == 1124); // expected-error {{constant}} expected-note {{in call}}
557 static_assert(test_wmemcpy(1, 2, 1) == 1334);
558 static_assert(test_wmemcpy(0, 3, 1) == 4234);
559 static_assert(test_wmemcpy(0, 3, 2) == 4234); // expected-error {{constant}} expected-note {{in call}}
560 static_assert(test_wmemcpy(2, 0, 3) == 4234); // expected-error {{constant}} expected-note {{in call}}
562 static_assert(test_wmemmove(1, 2, 1) == 1334);
563 static_assert(test_wmemmove(2, 1, 1) == 1224);
564 static_assert(test_wmemmove(0, 1, 2) == 2334);
565 static_assert(test_wmemmove(1, 0, 2) == 1124);
566 static_assert(test_wmemmove(1, 2, 1) == 1334);
567 static_assert(test_wmemmove(0, 3, 1) == 4234);
568 static_assert(test_wmemmove(0, 3, 2) == 4234); // expected-error {{constant}} expected-note {{in call}}
569 static_assert(test_wmemmove(2, 0, 3) == 4234); // expected-error {{constant}} expected-note {{in call}}
571 #define fold(x) (__builtin_constant_p(0) ? (x) : (x))
573 wchar_t global;
574 constexpr wchar_t *null = 0;
575 static_assert(__builtin_memcpy(&global, null, sizeof(wchar_t))); // expected-error {{}} expected-note {{source of 'memcpy' is nullptr}}
576 static_assert(__builtin_memmove(&global, null, sizeof(wchar_t))); // expected-error {{}} expected-note {{source of 'memmove' is nullptr}}
577 static_assert(__builtin_wmemcpy(&global, null, sizeof(wchar_t))); // expected-error {{}} expected-note {{source of 'wmemcpy' is nullptr}}
578 static_assert(__builtin_wmemmove(&global, null, sizeof(wchar_t))); // expected-error {{}} expected-note {{source of 'wmemmove' is nullptr}}
579 static_assert(__builtin_memcpy(null, &global, sizeof(wchar_t))); // expected-error {{}} expected-note {{destination of 'memcpy' is nullptr}}
580 static_assert(__builtin_memmove(null, &global, sizeof(wchar_t))); // expected-error {{}} expected-note {{destination of 'memmove' is nullptr}}
581 static_assert(__builtin_wmemcpy(null, &global, sizeof(wchar_t))); // expected-error {{}} expected-note {{destination of 'wmemcpy' is nullptr}}
582 static_assert(__builtin_wmemmove(null, &global, sizeof(wchar_t))); // expected-error {{}} expected-note {{destination of 'wmemmove' is nullptr}}
583 static_assert(__builtin_memcpy(&global, fold((wchar_t*)123), sizeof(wchar_t))); // expected-error {{}} expected-note {{source of 'memcpy' is (void *)123}}
584 static_assert(__builtin_memcpy(fold(reinterpret_cast<wchar_t*>(123)), &global, sizeof(wchar_t))); // expected-error {{}} expected-note {{destination of 'memcpy' is (void *)123}}
585 constexpr struct Incomplete *null_incomplete = 0;
586 static_assert(__builtin_memcpy(null_incomplete, null_incomplete, sizeof(wchar_t))); // expected-error {{}} expected-note {{source of 'memcpy' is nullptr}}
588 // Copying is permitted for any trivially-copyable type.
589 struct Trivial { char k; short s; constexpr bool ok() { return k == 3 && s == 4; } };
590 constexpr bool test_trivial() {
591 Trivial arr[3] = {{1, 2}, {3, 4}, {5, 6}};
592 __builtin_memcpy(arr, arr+1, sizeof(Trivial));
593 __builtin_memmove(arr+1, arr, 2 * sizeof(Trivial));
594 return arr[0].ok() && arr[1].ok() && arr[2].ok();
596 static_assert(test_trivial());
598 // But not for a non-trivially-copyable type.
599 struct NonTrivial {
600 constexpr NonTrivial() : n(0) {}
601 constexpr NonTrivial(const NonTrivial &) : n(1) {}
602 int n;
604 constexpr bool test_nontrivial_memcpy() { // expected-error {{never produces a constant}}
605 NonTrivial arr[3] = {};
606 __builtin_memcpy(arr, arr + 1, sizeof(NonTrivial)); // expected-note 2{{non-trivially-copyable}}
607 return true;
609 static_assert(test_nontrivial_memcpy()); // expected-error {{constant}} expected-note {{in call}}
610 constexpr bool test_nontrivial_memmove() { // expected-error {{never produces a constant}}
611 NonTrivial arr[3] = {};
612 __builtin_memcpy(arr, arr + 1, sizeof(NonTrivial)); // expected-note 2{{non-trivially-copyable}}
613 return true;
615 static_assert(test_nontrivial_memmove()); // expected-error {{constant}} expected-note {{in call}}
617 // Type puns via constant evaluated memcpy are not supported yet.
618 constexpr float type_pun(const unsigned &n) {
619 float f = 0.0f;
620 __builtin_memcpy(&f, &n, 4); // expected-note {{cannot constant evaluate 'memcpy' from object of type 'const unsigned int' to object of type 'float'}}
621 return f;
623 static_assert(type_pun(0x3f800000) == 1.0f); // expected-error {{constant}} expected-note {{in call}}
625 // Make sure we're not confused by derived-to-base conversions.
626 struct Base { int a; };
627 struct Derived : Base { int b; };
628 constexpr int test_derived_to_base(int n) {
629 Derived arr[2] = {1, 2, 3, 4};
630 Base *p = &arr[0];
631 Base *q = &arr[1];
632 __builtin_memcpy(p, q, sizeof(Base) * n); // expected-note {{source is not a contiguous array of at least 2 elements of type 'MemcpyEtc::Base'}}
633 return arr[0].a * 1000 + arr[0].b * 100 + arr[1].a * 10 + arr[1].b;
635 static_assert(test_derived_to_base(0) == 1234);
636 static_assert(test_derived_to_base(1) == 3234);
637 // FIXME: We could consider making this work by stripping elements off both
638 // designators until we have a long enough matching size, if both designators
639 // point to the start of their respective final elements.
640 static_assert(test_derived_to_base(2) == 3434); // expected-error {{constant}} expected-note {{in call}}
642 // Check that when address-of an array is passed to a tested function the
643 // array can be fully copied.
644 constexpr int test_address_of_const_array_type() {
645 int arr[4] = {1, 2, 3, 4};
646 __builtin_memmove(&arr, &arr, sizeof(arr));
647 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
649 static_assert(test_address_of_const_array_type() == 1234);
651 // Check that an incomplete array is rejected.
652 constexpr int test_incomplete_array_type() { // expected-error {{never produces a constant}}
653 extern int arr[];
654 __builtin_memmove(arr, arr, 4 * sizeof(arr[0]));
655 // expected-note@-1 2{{'memmove' not supported: source is not a contiguous array of at least 4 elements of type 'int'}}
656 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
658 static_assert(test_incomplete_array_type() == 1234); // expected-error {{constant}} expected-note {{in call}}
660 // Check that a pointer to an incomplete array is rejected.
661 constexpr int test_address_of_incomplete_array_type() { // expected-error {{never produces a constant}}
662 extern int arr[];
663 __builtin_memmove(&arr, &arr, 4 * sizeof(arr[0]));
664 // expected-note@-1 2{{cannot constant evaluate 'memmove' between objects of incomplete type 'int[]'}}
665 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
667 static_assert(test_address_of_incomplete_array_type() == 1234); // expected-error {{constant}} expected-note {{in call}}
669 // Check that a pointer to an incomplete struct is rejected.
670 constexpr bool test_address_of_incomplete_struct_type() { // expected-error {{never produces a constant}}
671 struct Incomplete;
672 extern Incomplete x, y;
673 __builtin_memcpy(&x, &x, 4);
674 // expected-note@-1 2{{cannot constant evaluate 'memcpy' between objects of incomplete type 'Incomplete'}}
675 return true;
677 static_assert(test_address_of_incomplete_struct_type()); // expected-error {{constant}} expected-note {{in call}}
680 #pragma clang diagnostic push
681 #pragma clang diagnostic ignored "-Wconstant-conversion"
682 namespace GH64876 {
683 void f() {
684 __builtin_strncmp(0, 0, 0xffffffffffffffff);
685 __builtin_memcmp(0, 0, 0xffffffffffffffff);
686 __builtin_bcmp(0, 0, 0xffffffffffffffff);
687 __builtin_wmemcmp(0, 0, 0xffffffffffffffff);
688 __builtin_memchr((const void*)0, 1, 0xffffffffffffffff);
689 __builtin_wmemchr((const wchar_t*)0, 1, 0xffffffffffffffff);
691 __builtin_strncmp(0, 0, -511LL);
692 __builtin_memcmp(0, 0, -511LL);
693 __builtin_bcmp(0, 0, -511LL);
694 __builtin_wmemcmp(0, 0, -511LL);
695 __builtin_memchr((const void*)0, 1, -511LL);
696 __builtin_wmemchr((const wchar_t*)0, 1, -511LL);
699 #pragma clang diagnostic pop