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