annotate clang/test/SemaCXX/constexpr-string.cpp @ 222:81f6424ef0e3 llvm-original

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