Debian Codegraph @

clang/test/SemaCXX/constexpr-string.cpp

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