Clang Project

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