annotate clang/test/SemaCXX/constexpr-string.cpp @ 150:1d019706d866

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