blob: 408437cbf4d0d798b4fa9863ea161003da2658fc [file] [log] [blame]
Richard Smith06f71b52018-08-04 00:57:17 +00001// RUN: %clang_cc1 %s -triple x86_64-linux-gnu -std=c++1z -fsyntax-only -verify -pedantic
2// RUN: %clang_cc1 %s -triple x86_64-linux-gnu -std=c++1z -fsyntax-only -verify -pedantic -fno-signed-char
3// RUN: %clang_cc1 %s -triple x86_64-linux-gnu -std=c++1z -fsyntax-only -verify -pedantic -fno-wchar -Dwchar_t=__WCHAR_TYPE__
Richard Smithe151bab2016-11-11 23:43:35 +00004
Richard Smith8110c9d2016-11-29 19:45:17 +00005# 6 "/usr/include/string.h" 1 3 4
Richard Smithe151bab2016-11-11 23:43:35 +00006extern "C" {
7 typedef decltype(sizeof(int)) size_t;
8
9 extern size_t strlen(const char *p);
10
11 extern int strcmp(const char *s1, const char *s2);
12 extern int strncmp(const char *s1, const char *s2, size_t n);
Richard Smith8110c9d2016-11-29 19:45:17 +000013 extern int memcmp(const void *s1, const void *s2, size_t n);
Richard Smithe9507952016-11-12 01:39:56 +000014
15 extern char *strchr(const char *s, int c);
16 extern void *memchr(const void *s, int c, size_t n);
Richard Smith96beffb2018-07-31 23:35:09 +000017
Richard Smith06f71b52018-08-04 00:57:17 +000018 extern void *memcpy(void *d, const void *s, size_t n);
19 extern void *memmove(void *d, const void *s, size_t n);
20}
21# 22 "SemaCXX/constexpr-string.cpp" 2
22
23# 24 "/usr/include/wchar.h" 1 3 4
Richard Smith8110c9d2016-11-29 19:45:17 +000024extern "C" {
25 extern size_t wcslen(const wchar_t *p);
26
27 extern int wcscmp(const wchar_t *s1, const wchar_t *s2);
28 extern int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n);
29 extern int wmemcmp(const wchar_t *s1, const wchar_t *s2, size_t n);
30
31 extern wchar_t *wcschr(const wchar_t *s, wchar_t c);
32 extern wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n);
Richard Smith06f71b52018-08-04 00:57:17 +000033
34 extern wchar_t *wmemcpy(wchar_t *d, const wchar_t *s, size_t n);
35 extern wchar_t *wmemmove(wchar_t *d, const wchar_t *s, size_t n);
Richard Smith8110c9d2016-11-29 19:45:17 +000036}
37
Richard Smith06f71b52018-08-04 00:57:17 +000038# 39 "SemaCXX/constexpr-string.cpp" 2
Richard Smithe151bab2016-11-11 23:43:35 +000039namespace Strlen {
40 constexpr int n = __builtin_strlen("hello"); // ok
Richard Smith8110c9d2016-11-29 19:45:17 +000041 static_assert(n == 5);
42 constexpr int wn = __builtin_wcslen(L"hello"); // ok
43 static_assert(wn == 5);
Richard Smithe151bab2016-11-11 23:43:35 +000044 constexpr int m = strlen("hello"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strlen' cannot be used in a constant expression}}
Richard Smith8110c9d2016-11-29 19:45:17 +000045 constexpr int wm = wcslen(L"hello"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'wcslen' cannot be used in a constant expression}}
Richard Smithe151bab2016-11-11 23:43:35 +000046
47 // Make sure we can evaluate a call to strlen.
Richard Smith8110c9d2016-11-29 19:45:17 +000048 int arr[3]; // expected-note 2{{here}}
Richard Smithe151bab2016-11-11 23:43:35 +000049 int k = arr[strlen("hello")]; // expected-warning {{array index 5}}
Richard Smith8110c9d2016-11-29 19:45:17 +000050 int wk = arr[wcslen(L"hello")]; // expected-warning {{array index 5}}
Richard Smithe151bab2016-11-11 23:43:35 +000051}
52
53namespace StrcmpEtc {
54 constexpr char kFoobar[6] = {'f','o','o','b','a','r'};
55 constexpr char kFoobazfoobar[12] = {'f','o','o','b','a','z','f','o','o','b','a','r'};
56
57 static_assert(__builtin_strcmp("abab", "abab") == 0);
58 static_assert(__builtin_strcmp("abab", "abba") == -1);
59 static_assert(__builtin_strcmp("abab", "abaa") == 1);
60 static_assert(__builtin_strcmp("ababa", "abab") == 1);
61 static_assert(__builtin_strcmp("abab", "ababa") == -1);
Benjamin Kramer33b70922018-04-23 22:04:34 +000062 static_assert(__builtin_strcmp("a\203", "a") == 1);
63 static_assert(__builtin_strcmp("a\203", "a\003") == 1);
Richard Smithe151bab2016-11-11 23:43:35 +000064 static_assert(__builtin_strcmp("abab\0banana", "abab") == 0);
65 static_assert(__builtin_strcmp("abab", "abab\0banana") == 0);
66 static_assert(__builtin_strcmp("abab\0banana", "abab\0canada") == 0);
67 static_assert(__builtin_strcmp(0, "abab") == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
68 static_assert(__builtin_strcmp("abab", 0) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
69
70 static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar) == -1); // FIXME: Should we reject this?
71 static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar + 6) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
72
73 static_assert(__builtin_strncmp("abaa", "abba", 5) == -1);
74 static_assert(__builtin_strncmp("abaa", "abba", 4) == -1);
75 static_assert(__builtin_strncmp("abaa", "abba", 3) == -1);
76 static_assert(__builtin_strncmp("abaa", "abba", 2) == 0);
77 static_assert(__builtin_strncmp("abaa", "abba", 1) == 0);
78 static_assert(__builtin_strncmp("abaa", "abba", 0) == 0);
79 static_assert(__builtin_strncmp(0, 0, 0) == 0);
80 static_assert(__builtin_strncmp("abab\0banana", "abab\0canada", 100) == 0);
81
82 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 6) == -1);
83 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 7) == -1); // FIXME: Should we reject this?
84 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 6) == 0);
85 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 7) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
86
87 static_assert(__builtin_memcmp("abaa", "abba", 3) == -1);
88 static_assert(__builtin_memcmp("abaa", "abba", 2) == 0);
Benjamin Kramer33b70922018-04-23 22:04:34 +000089 static_assert(__builtin_memcmp("a\203", "a", 2) == 1);
90 static_assert(__builtin_memcmp("a\203", "a\003", 2) == 1);
Richard Smithe151bab2016-11-11 23:43:35 +000091 static_assert(__builtin_memcmp(0, 0, 0) == 0);
92 static_assert(__builtin_memcmp("abab\0banana", "abab\0banana", 100) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
93 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 100) == -1); // FIXME: Should we reject this?
94 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 7) == -1);
95 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 6) == -1);
96 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 5) == 0);
97
98 constexpr int a = strcmp("hello", "world"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strcmp' cannot be used in a constant expression}}
99 constexpr int b = strncmp("hello", "world", 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strncmp' cannot be used in a constant expression}}
100 constexpr int c = memcmp("hello", "world", 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'memcmp' cannot be used in a constant expression}}
101}
Richard Smithe9507952016-11-12 01:39:56 +0000102
Richard Smith8110c9d2016-11-29 19:45:17 +0000103namespace WcscmpEtc {
104 constexpr wchar_t kFoobar[6] = {L'f',L'o',L'o',L'b',L'a',L'r'};
105 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'};
106
107 static_assert(__builtin_wcscmp(L"abab", L"abab") == 0);
108 static_assert(__builtin_wcscmp(L"abab", L"abba") == -1);
109 static_assert(__builtin_wcscmp(L"abab", L"abaa") == 1);
110 static_assert(__builtin_wcscmp(L"ababa", L"abab") == 1);
111 static_assert(__builtin_wcscmp(L"abab", L"ababa") == -1);
112 static_assert(__builtin_wcscmp(L"abab\0banana", L"abab") == 0);
113 static_assert(__builtin_wcscmp(L"abab", L"abab\0banana") == 0);
114 static_assert(__builtin_wcscmp(L"abab\0banana", L"abab\0canada") == 0);
Benjamin Kramer33b70922018-04-23 22:04:34 +0000115#if __WCHAR_WIDTH__ == 32
Benjamin Kramerbb156b52018-04-24 08:18:32 +0000116 static_assert(__builtin_wcscmp(L"a\x83838383", L"a") == (wchar_t)-1U >> 31);
Benjamin Kramer33b70922018-04-23 22:04:34 +0000117#endif
Richard Smith8110c9d2016-11-29 19:45:17 +0000118 static_assert(__builtin_wcscmp(0, L"abab") == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
119 static_assert(__builtin_wcscmp(L"abab", 0) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
120
121 static_assert(__builtin_wcscmp(kFoobar, kFoobazfoobar) == -1); // FIXME: Should we reject this?
122 static_assert(__builtin_wcscmp(kFoobar, kFoobazfoobar + 6) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
123
124 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 5) == -1);
125 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 4) == -1);
126 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 3) == -1);
127 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 2) == 0);
128 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 1) == 0);
129 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 0) == 0);
130 static_assert(__builtin_wcsncmp(0, 0, 0) == 0);
131 static_assert(__builtin_wcsncmp(L"abab\0banana", L"abab\0canada", 100) == 0);
Benjamin Kramer33b70922018-04-23 22:04:34 +0000132#if __WCHAR_WIDTH__ == 32
Benjamin Kramer2fdf81f2018-04-24 07:53:10 +0000133 static_assert(__builtin_wcsncmp(L"a\x83838383", L"aa", 2) ==
Benjamin Kramerbb156b52018-04-24 08:18:32 +0000134 (wchar_t)-1U >> 31);
Benjamin Kramer33b70922018-04-23 22:04:34 +0000135#endif
Richard Smith8110c9d2016-11-29 19:45:17 +0000136
137 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar, 6) == -1);
138 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar, 7) == -1); // FIXME: Should we reject this?
139 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar + 6, 6) == 0);
140 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar + 6, 7) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
141
142 static_assert(__builtin_wmemcmp(L"abaa", L"abba", 3) == -1);
143 static_assert(__builtin_wmemcmp(L"abaa", L"abba", 2) == 0);
144 static_assert(__builtin_wmemcmp(0, 0, 0) == 0);
Benjamin Kramer33b70922018-04-23 22:04:34 +0000145#if __WCHAR_WIDTH__ == 32
Benjamin Kramer2fdf81f2018-04-24 07:53:10 +0000146 static_assert(__builtin_wmemcmp(L"a\x83838383", L"aa", 2) ==
Benjamin Kramerbb156b52018-04-24 08:18:32 +0000147 (wchar_t)-1U >> 31);
Benjamin Kramer33b70922018-04-23 22:04:34 +0000148#endif
Richard Smith8110c9d2016-11-29 19:45:17 +0000149 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}}
150 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 100) == -1); // FIXME: Should we reject this?
151 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 7) == -1);
152 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 6) == -1);
153 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 5) == 0);
154
155 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}}
156 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}}
157 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}}
158}
159
Richard Smithe9507952016-11-12 01:39:56 +0000160namespace StrchrEtc {
161 constexpr const char *kStr = "abca\xff\0d";
162 constexpr char kFoo[] = {'f', 'o', 'o'};
163 static_assert(__builtin_strchr(kStr, 'a') == kStr);
164 static_assert(__builtin_strchr(kStr, 'b') == kStr + 1);
165 static_assert(__builtin_strchr(kStr, 'c') == kStr + 2);
166 static_assert(__builtin_strchr(kStr, 'd') == nullptr);
167 static_assert(__builtin_strchr(kStr, 'e') == nullptr);
168 static_assert(__builtin_strchr(kStr, '\0') == kStr + 5);
169 static_assert(__builtin_strchr(kStr, 'a' + 256) == nullptr);
170 static_assert(__builtin_strchr(kStr, 'a' - 256) == nullptr);
171 static_assert(__builtin_strchr(kStr, '\xff') == kStr + 4);
172 static_assert(__builtin_strchr(kStr, '\xff' + 256) == nullptr);
173 static_assert(__builtin_strchr(kStr, '\xff' - 256) == nullptr);
174 static_assert(__builtin_strchr(kFoo, 'o') == kFoo + 1);
175 static_assert(__builtin_strchr(kFoo, 'x') == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
176 static_assert(__builtin_strchr(nullptr, 'x') == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
177
178 static_assert(__builtin_memchr(kStr, 'a', 0) == nullptr);
179 static_assert(__builtin_memchr(kStr, 'a', 1) == kStr);
180 static_assert(__builtin_memchr(kStr, '\0', 5) == nullptr);
181 static_assert(__builtin_memchr(kStr, '\0', 6) == kStr + 5);
182 static_assert(__builtin_memchr(kStr, '\xff', 8) == kStr + 4);
183 static_assert(__builtin_memchr(kStr, '\xff' + 256, 8) == kStr + 4);
184 static_assert(__builtin_memchr(kStr, '\xff' - 256, 8) == kStr + 4);
185 static_assert(__builtin_memchr(kFoo, 'x', 3) == nullptr);
186 static_assert(__builtin_memchr(kFoo, 'x', 4) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
187 static_assert(__builtin_memchr(nullptr, 'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
188 static_assert(__builtin_memchr(nullptr, 'x', 0) == nullptr); // FIXME: Should we reject this?
189
Richard Smith5e29dd32017-01-20 00:45:35 +0000190 static_assert(__builtin_char_memchr(kStr, 'a', 0) == nullptr);
191 static_assert(__builtin_char_memchr(kStr, 'a', 1) == kStr);
192 static_assert(__builtin_char_memchr(kStr, '\0', 5) == nullptr);
193 static_assert(__builtin_char_memchr(kStr, '\0', 6) == kStr + 5);
194 static_assert(__builtin_char_memchr(kStr, '\xff', 8) == kStr + 4);
195 static_assert(__builtin_char_memchr(kStr, '\xff' + 256, 8) == kStr + 4);
196 static_assert(__builtin_char_memchr(kStr, '\xff' - 256, 8) == kStr + 4);
197 static_assert(__builtin_char_memchr(kFoo, 'x', 3) == nullptr);
198 static_assert(__builtin_char_memchr(kFoo, 'x', 4) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
199 static_assert(__builtin_char_memchr(nullptr, 'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
200 static_assert(__builtin_char_memchr(nullptr, 'x', 0) == nullptr); // FIXME: Should we reject this?
201
202 static_assert(*__builtin_char_memchr(kStr, '\xff', 8) == '\xff');
203 constexpr bool char_memchr_mutable() {
204 char buffer[] = "mutable";
205 *__builtin_char_memchr(buffer, 't', 8) = 'r';
206 *__builtin_char_memchr(buffer, 'm', 8) = 'd';
207 return __builtin_strcmp(buffer, "durable") == 0;
208 }
209 static_assert(char_memchr_mutable());
210
Richard Smithe9507952016-11-12 01:39:56 +0000211 constexpr bool a = !strchr("hello", 'h'); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strchr' cannot be used in a constant expression}}
212 constexpr bool b = !memchr("hello", 'h', 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'memchr' cannot be used in a constant expression}}
213}
Richard Smith8110c9d2016-11-29 19:45:17 +0000214
215namespace WcschrEtc {
216 constexpr const wchar_t *kStr = L"abca\xffff\0dL";
217 constexpr wchar_t kFoo[] = {L'f', L'o', L'o'};
218 static_assert(__builtin_wcschr(kStr, L'a') == kStr);
219 static_assert(__builtin_wcschr(kStr, L'b') == kStr + 1);
220 static_assert(__builtin_wcschr(kStr, L'c') == kStr + 2);
221 static_assert(__builtin_wcschr(kStr, L'd') == nullptr);
222 static_assert(__builtin_wcschr(kStr, L'e') == nullptr);
223 static_assert(__builtin_wcschr(kStr, L'\0') == kStr + 5);
224 static_assert(__builtin_wcschr(kStr, L'a' + 256) == nullptr);
225 static_assert(__builtin_wcschr(kStr, L'a' - 256) == nullptr);
226 static_assert(__builtin_wcschr(kStr, L'\xffff') == kStr + 4);
227 static_assert(__builtin_wcschr(kFoo, L'o') == kFoo + 1);
228 static_assert(__builtin_wcschr(kFoo, L'x') == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
229 static_assert(__builtin_wcschr(nullptr, L'x') == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
230
231 static_assert(__builtin_wmemchr(kStr, L'a', 0) == nullptr);
232 static_assert(__builtin_wmemchr(kStr, L'a', 1) == kStr);
233 static_assert(__builtin_wmemchr(kStr, L'\0', 5) == nullptr);
234 static_assert(__builtin_wmemchr(kStr, L'\0', 6) == kStr + 5);
235 static_assert(__builtin_wmemchr(kStr, L'\xffff', 8) == kStr + 4);
236 static_assert(__builtin_wmemchr(kFoo, L'x', 3) == nullptr);
237 static_assert(__builtin_wmemchr(kFoo, L'x', 4) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}}
238 static_assert(__builtin_wmemchr(nullptr, L'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}}
239 static_assert(__builtin_wmemchr(nullptr, L'x', 0) == nullptr); // FIXME: Should we reject this?
240
241 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}}
242 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}}
243}
Richard Smith06f71b52018-08-04 00:57:17 +0000244
245namespace MemcpyEtc {
246 template<typename T>
247 constexpr T result(T (&arr)[4]) {
248 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
249 }
250
251 constexpr int test_memcpy(int a, int b, int n) {
252 int arr[4] = {1, 2, 3, 4};
253 __builtin_memcpy(arr + a, arr + b, n);
254 // expected-note@-1 2{{overlapping memory regions}}
255 // expected-note@-2 {{size to copy (1) is not a multiple of size of element type 'int'}}
256 // expected-note@-3 {{source is not a contiguous array of at least 2 elements of type 'int'}}
257 // expected-note@-4 {{destination is not a contiguous array of at least 3 elements of type 'int'}}
258 return result(arr);
259 }
260 constexpr int test_memmove(int a, int b, int n) {
261 int arr[4] = {1, 2, 3, 4};
262 __builtin_memmove(arr + a, arr + b, n);
263 // expected-note@-1 {{size to copy (1) is not a multiple of size of element type 'int'}}
264 // expected-note@-2 {{source is not a contiguous array of at least 2 elements of type 'int'}}
265 // expected-note@-3 {{destination is not a contiguous array of at least 3 elements of type 'int'}}
266 return result(arr);
267 }
268 constexpr int test_wmemcpy(int a, int b, int n) {
269 wchar_t arr[4] = {1, 2, 3, 4};
270 __builtin_wmemcpy(arr + a, arr + b, n);
271 // expected-note@-1 2{{overlapping memory regions}}
272 // expected-note-re@-2 {{source is not a contiguous array of at least 2 elements of type '{{wchar_t|int}}'}}
273 // expected-note-re@-3 {{destination is not a contiguous array of at least 3 elements of type '{{wchar_t|int}}'}}
274 return result(arr);
275 }
276 constexpr int test_wmemmove(int a, int b, int n) {
277 wchar_t arr[4] = {1, 2, 3, 4};
278 __builtin_wmemmove(arr + a, arr + b, n);
279 // expected-note-re@-1 {{source is not a contiguous array of at least 2 elements of type '{{wchar_t|int}}'}}
280 // expected-note-re@-2 {{destination is not a contiguous array of at least 3 elements of type '{{wchar_t|int}}'}}
281 return result(arr);
282 }
283
284 static_assert(test_memcpy(1, 2, 4) == 1334);
285 static_assert(test_memcpy(2, 1, 4) == 1224);
286 static_assert(test_memcpy(0, 1, 8) == 2334); // expected-error {{constant}} expected-note {{in call}}
287 static_assert(test_memcpy(1, 0, 8) == 1124); // expected-error {{constant}} expected-note {{in call}}
288 static_assert(test_memcpy(1, 2, 1) == 1334); // expected-error {{constant}} expected-note {{in call}}
289 static_assert(test_memcpy(0, 3, 4) == 4234);
290 static_assert(test_memcpy(0, 3, 8) == 4234); // expected-error {{constant}} expected-note {{in call}}
291 static_assert(test_memcpy(2, 0, 12) == 4234); // expected-error {{constant}} expected-note {{in call}}
292
293 static_assert(test_memmove(1, 2, 4) == 1334);
294 static_assert(test_memmove(2, 1, 4) == 1224);
295 static_assert(test_memmove(0, 1, 8) == 2334);
296 static_assert(test_memmove(1, 0, 8) == 1124);
297 static_assert(test_memmove(1, 2, 1) == 1334); // expected-error {{constant}} expected-note {{in call}}
298 static_assert(test_memmove(0, 3, 4) == 4234);
299 static_assert(test_memmove(0, 3, 8) == 4234); // expected-error {{constant}} expected-note {{in call}}
300 static_assert(test_memmove(2, 0, 12) == 4234); // expected-error {{constant}} expected-note {{in call}}
301
302 static_assert(test_wmemcpy(1, 2, 1) == 1334);
303 static_assert(test_wmemcpy(2, 1, 1) == 1224);
304 static_assert(test_wmemcpy(0, 1, 2) == 2334); // expected-error {{constant}} expected-note {{in call}}
305 static_assert(test_wmemcpy(1, 0, 2) == 1124); // expected-error {{constant}} expected-note {{in call}}
306 static_assert(test_wmemcpy(1, 2, 1) == 1334);
307 static_assert(test_wmemcpy(0, 3, 1) == 4234);
308 static_assert(test_wmemcpy(0, 3, 2) == 4234); // expected-error {{constant}} expected-note {{in call}}
309 static_assert(test_wmemcpy(2, 0, 3) == 4234); // expected-error {{constant}} expected-note {{in call}}
310
311 static_assert(test_wmemmove(1, 2, 1) == 1334);
312 static_assert(test_wmemmove(2, 1, 1) == 1224);
313 static_assert(test_wmemmove(0, 1, 2) == 2334);
314 static_assert(test_wmemmove(1, 0, 2) == 1124);
315 static_assert(test_wmemmove(1, 2, 1) == 1334);
316 static_assert(test_wmemmove(0, 3, 1) == 4234);
317 static_assert(test_wmemmove(0, 3, 2) == 4234); // expected-error {{constant}} expected-note {{in call}}
318 static_assert(test_wmemmove(2, 0, 3) == 4234); // expected-error {{constant}} expected-note {{in call}}
319
Richard Smith128719c2018-09-13 22:47:33 +0000320#define fold(x) (__builtin_constant_p(0) ? (x) : (x))
321
322 wchar_t global;
323 constexpr wchar_t *null = 0;
324 static_assert(__builtin_memcpy(&global, null, sizeof(wchar_t))); // expected-error {{}} expected-note {{source of 'memcpy' is nullptr}}
325 static_assert(__builtin_memmove(&global, null, sizeof(wchar_t))); // expected-error {{}} expected-note {{source of 'memmove' is nullptr}}
326 static_assert(__builtin_wmemcpy(&global, null, sizeof(wchar_t))); // expected-error {{}} expected-note {{source of 'wmemcpy' is nullptr}}
327 static_assert(__builtin_wmemmove(&global, null, sizeof(wchar_t))); // expected-error {{}} expected-note {{source of 'wmemmove' is nullptr}}
328 static_assert(__builtin_memcpy(null, &global, sizeof(wchar_t))); // expected-error {{}} expected-note {{destination of 'memcpy' is nullptr}}
329 static_assert(__builtin_memmove(null, &global, sizeof(wchar_t))); // expected-error {{}} expected-note {{destination of 'memmove' is nullptr}}
330 static_assert(__builtin_wmemcpy(null, &global, sizeof(wchar_t))); // expected-error {{}} expected-note {{destination of 'wmemcpy' is nullptr}}
331 static_assert(__builtin_wmemmove(null, &global, sizeof(wchar_t))); // expected-error {{}} expected-note {{destination of 'wmemmove' is nullptr}}
332 static_assert(__builtin_memcpy(&global, fold((wchar_t*)123), sizeof(wchar_t))); // expected-error {{}} expected-note {{source of 'memcpy' is (void *)123}}
333 static_assert(__builtin_memcpy(fold(reinterpret_cast<wchar_t*>(123)), &global, sizeof(wchar_t))); // expected-error {{}} expected-note {{destination of 'memcpy' is (void *)123}}
334 constexpr struct Incomplete *null_incomplete = 0;
335 static_assert(__builtin_memcpy(null_incomplete, null_incomplete, sizeof(wchar_t))); // expected-error {{}} expected-note {{source of 'memcpy' is nullptr}}
336
Richard Smith06f71b52018-08-04 00:57:17 +0000337 // Copying is permitted for any trivially-copyable type.
338 struct Trivial { char k; short s; constexpr bool ok() { return k == 3 && s == 4; } };
339 constexpr bool test_trivial() {
340 Trivial arr[3] = {{1, 2}, {3, 4}, {5, 6}};
341 __builtin_memcpy(arr, arr+1, sizeof(Trivial));
342 __builtin_memmove(arr+1, arr, 2 * sizeof(Trivial));
343 return arr[0].ok() && arr[1].ok() && arr[2].ok();
344 }
345 static_assert(test_trivial());
346
347 // But not for a non-trivially-copyable type.
348 struct NonTrivial {
349 constexpr NonTrivial() : n(0) {}
350 constexpr NonTrivial(const NonTrivial &) : n(1) {}
351 int n;
352 };
353 constexpr bool test_nontrivial_memcpy() { // expected-error {{never produces a constant}}
354 NonTrivial arr[3] = {};
355 __builtin_memcpy(arr, arr + 1, sizeof(NonTrivial)); // expected-note 2{{non-trivially-copyable}}
356 return true;
357 }
358 static_assert(test_nontrivial_memcpy()); // expected-error {{constant}} expected-note {{in call}}
359 constexpr bool test_nontrivial_memmove() { // expected-error {{never produces a constant}}
360 NonTrivial arr[3] = {};
361 __builtin_memcpy(arr, arr + 1, sizeof(NonTrivial)); // expected-note 2{{non-trivially-copyable}}
362 return true;
363 }
364 static_assert(test_nontrivial_memmove()); // expected-error {{constant}} expected-note {{in call}}
365
366 // Type puns via constant evaluated memcpy are not supported yet.
367 constexpr float type_pun(const unsigned &n) {
368 float f = 0.0f;
369 __builtin_memcpy(&f, &n, 4); // expected-note {{cannot constant evaluate 'memcpy' from object of type 'const unsigned int' to object of type 'float'}}
370 return f;
371 }
372 static_assert(type_pun(0x3f800000) == 1.0f); // expected-error {{constant}} expected-note {{in call}}
373
374 // Make sure we're not confused by derived-to-base conversions.
375 struct Base { int a; };
376 struct Derived : Base { int b; };
377 constexpr int test_derived_to_base(int n) {
378 Derived arr[2] = {1, 2, 3, 4};
379 Base *p = &arr[0];
380 Base *q = &arr[1];
381 __builtin_memcpy(p, q, sizeof(Base) * n); // expected-note {{source is not a contiguous array of at least 2 elements of type 'MemcpyEtc::Base'}}
382 return arr[0].a * 1000 + arr[0].b * 100 + arr[1].a * 10 + arr[1].b;
383 }
384 static_assert(test_derived_to_base(0) == 1234);
385 static_assert(test_derived_to_base(1) == 3234);
386 // FIXME: We could consider making this work by stripping elements off both
387 // designators until we have a long enough matching size, if both designators
388 // point to the start of their respective final elements.
389 static_assert(test_derived_to_base(2) == 3434); // expected-error {{constant}} expected-note {{in call}}
Petr Pavlued083f22018-10-04 09:25:44 +0000390
391 // Check that when address-of an array is passed to a tested function the
392 // array can be fully copied.
393 constexpr int test_address_of_const_array_type() {
394 int arr[4] = {1, 2, 3, 4};
395 __builtin_memmove(&arr, &arr, sizeof(arr));
396 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
397 }
398 static_assert(test_address_of_const_array_type() == 1234);
399
400 // Check that an incomplete array is rejected.
401 constexpr int test_incomplete_array_type() { // expected-error {{never produces a constant}}
402 extern int arr[];
403 __builtin_memmove(arr, arr, 4 * sizeof(arr[0]));
404 // expected-note@-1 2{{'memmove' not supported: source is not a contiguous array of at least 4 elements of type 'int'}}
405 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
406 }
407 static_assert(test_incomplete_array_type() == 1234); // expected-error {{constant}} expected-note {{in call}}
408
409 // Check that a pointer to an incomplete array is rejected.
410 constexpr int test_address_of_incomplete_array_type() { // expected-error {{never produces a constant}}
411 extern int arr[];
412 __builtin_memmove(&arr, &arr, 4 * sizeof(arr[0]));
413 // expected-note@-1 2{{cannot constant evaluate 'memmove' between objects of incomplete type 'int []'}}
414 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
415 }
416 static_assert(test_address_of_incomplete_array_type() == 1234); // expected-error {{constant}} expected-note {{in call}}
417
418 // Check that a pointer to an incomplete struct is rejected.
419 constexpr bool test_address_of_incomplete_struct_type() { // expected-error {{never produces a constant}}
420 struct Incomplete;
421 extern Incomplete x, y;
422 __builtin_memcpy(&x, &x, 4);
423 // expected-note@-1 2{{cannot constant evaluate 'memcpy' between objects of incomplete type 'Incomplete'}}
424 return true;
425 }
426 static_assert(test_address_of_incomplete_struct_type()); // expected-error {{constant}} expected-note {{in call}}
Richard Smith06f71b52018-08-04 00:57:17 +0000427}