1 // RUN: %clang_cc1 %s -std=c++1z -fsyntax-only -verify -pedantic 2 // RUN: %clang_cc1 %s -std=c++1z -fsyntax-only -verify -pedantic -fno-signed-char 3 // RUN: %clang_cc1 %s -std=c++1z -fsyntax-only -verify -pedantic -fno-wchar -Dwchar_t=__WCHAR_TYPE__ 4 5 # 6 "/usr/include/string.h" 1 3 4 6 extern "C" { 7 typedef decltype(sizeof(int)) size_t; 8 9 extern size_t strlen(const char *p); 10 11 extern int strcmp(const char *s1, const char *s2); 12 extern int strncmp(const char *s1, const char *s2, size_t n); 13 extern int memcmp(const void *s1, const void *s2, size_t n); 14 15 extern char *strchr(const char *s, int c); 16 extern void *memchr(const void *s, int c, size_t n); 17 } 18 # 19 "SemaCXX/constexpr-string.cpp" 2 19 20 # 21 "/usr/include/wchar.h" 1 3 4 21 extern "C" { 22 extern size_t wcslen(const wchar_t *p); 23 24 extern int wcscmp(const wchar_t *s1, const wchar_t *s2); 25 extern int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n); 26 extern int wmemcmp(const wchar_t *s1, const wchar_t *s2, size_t n); 27 28 extern wchar_t *wcschr(const wchar_t *s, wchar_t c); 29 extern wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n); 30 } 31 32 # 33 "SemaCXX/constexpr-string.cpp" 2 33 namespace Strlen { 34 constexpr int n = __builtin_strlen("hello"); // ok 35 static_assert(n == 5); 36 constexpr int wn = __builtin_wcslen(L"hello"); // ok 37 static_assert(wn == 5); 38 constexpr int m = strlen("hello"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strlen' cannot be used in a constant expression}} 39 constexpr int wm = wcslen(L"hello"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'wcslen' cannot be used in a constant expression}} 40 41 // Make sure we can evaluate a call to strlen. 42 int arr[3]; // expected-note 2{{here}} 43 int k = arr[strlen("hello")]; // expected-warning {{array index 5}} 44 int wk = arr[wcslen(L"hello")]; // expected-warning {{array index 5}} 45 } 46 47 namespace StrcmpEtc { 48 constexpr char kFoobar[6] = {'f','o','o','b','a','r'}; 49 constexpr char kFoobazfoobar[12] = {'f','o','o','b','a','z','f','o','o','b','a','r'}; 50 51 static_assert(__builtin_strcmp("abab", "abab") == 0); 52 static_assert(__builtin_strcmp("abab", "abba") == -1); 53 static_assert(__builtin_strcmp("abab", "abaa") == 1); 54 static_assert(__builtin_strcmp("ababa", "abab") == 1); 55 static_assert(__builtin_strcmp("abab", "ababa") == -1); 56 static_assert(__builtin_strcmp("a\203", "a") == 1); 57 static_assert(__builtin_strcmp("a\203", "a\003") == 1); 58 static_assert(__builtin_strcmp("abab\0banana", "abab") == 0); 59 static_assert(__builtin_strcmp("abab", "abab\0banana") == 0); 60 static_assert(__builtin_strcmp("abab\0banana", "abab\0canada") == 0); 61 static_assert(__builtin_strcmp(0, "abab") == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}} 62 static_assert(__builtin_strcmp("abab", 0) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}} 63 64 static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar) == -1); // FIXME: Should we reject this? 65 static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar + 6) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} 66 67 static_assert(__builtin_strncmp("abaa", "abba", 5) == -1); 68 static_assert(__builtin_strncmp("abaa", "abba", 4) == -1); 69 static_assert(__builtin_strncmp("abaa", "abba", 3) == -1); 70 static_assert(__builtin_strncmp("abaa", "abba", 2) == 0); 71 static_assert(__builtin_strncmp("abaa", "abba", 1) == 0); 72 static_assert(__builtin_strncmp("abaa", "abba", 0) == 0); 73 static_assert(__builtin_strncmp(0, 0, 0) == 0); 74 static_assert(__builtin_strncmp("abab\0banana", "abab\0canada", 100) == 0); 75 76 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 6) == -1); 77 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 7) == -1); // FIXME: Should we reject this? 78 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 6) == 0); 79 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 7) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} 80 81 static_assert(__builtin_memcmp("abaa", "abba", 3) == -1); 82 static_assert(__builtin_memcmp("abaa", "abba", 2) == 0); 83 static_assert(__builtin_memcmp("a\203", "a", 2) == 1); 84 static_assert(__builtin_memcmp("a\203", "a\003", 2) == 1); 85 static_assert(__builtin_memcmp(0, 0, 0) == 0); 86 static_assert(__builtin_memcmp("abab\0banana", "abab\0banana", 100) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} 87 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 100) == -1); // FIXME: Should we reject this? 88 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 7) == -1); 89 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 6) == -1); 90 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 5) == 0); 91 92 constexpr int a = strcmp("hello", "world"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strcmp' cannot be used in a constant expression}} 93 constexpr int b = strncmp("hello", "world", 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strncmp' cannot be used in a constant expression}} 94 constexpr int c = memcmp("hello", "world", 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'memcmp' cannot be used in a constant expression}} 95 } 96 97 namespace WcscmpEtc { 98 constexpr wchar_t kFoobar[6] = {L'f',L'o',L'o',L'b',L'a',L'r'}; 99 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'}; 100 101 static_assert(__builtin_wcscmp(L"abab", L"abab") == 0); 102 static_assert(__builtin_wcscmp(L"abab", L"abba") == -1); 103 static_assert(__builtin_wcscmp(L"abab", L"abaa") == 1); 104 static_assert(__builtin_wcscmp(L"ababa", L"abab") == 1); 105 static_assert(__builtin_wcscmp(L"abab", L"ababa") == -1); 106 static_assert(__builtin_wcscmp(L"abab\0banana", L"abab") == 0); 107 static_assert(__builtin_wcscmp(L"abab", L"abab\0banana") == 0); 108 static_assert(__builtin_wcscmp(L"abab\0banana", L"abab\0canada") == 0); 109 #if __WCHAR_WIDTH__ == 32 110 static_assert(__builtin_wcscmp(L"a\x83838383", L"a") == (wchar_t)-1U >> 31); 111 #endif 112 static_assert(__builtin_wcscmp(0, L"abab") == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}} 113 static_assert(__builtin_wcscmp(L"abab", 0) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}} 114 115 static_assert(__builtin_wcscmp(kFoobar, kFoobazfoobar) == -1); // FIXME: Should we reject this? 116 static_assert(__builtin_wcscmp(kFoobar, kFoobazfoobar + 6) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} 117 118 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 5) == -1); 119 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 4) == -1); 120 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 3) == -1); 121 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 2) == 0); 122 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 1) == 0); 123 static_assert(__builtin_wcsncmp(L"abaa", L"abba", 0) == 0); 124 static_assert(__builtin_wcsncmp(0, 0, 0) == 0); 125 static_assert(__builtin_wcsncmp(L"abab\0banana", L"abab\0canada", 100) == 0); 126 #if __WCHAR_WIDTH__ == 32 127 static_assert(__builtin_wcsncmp(L"a\x83838383", L"aa", 2) == 128 (wchar_t)-1U >> 31); 129 #endif 130 131 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar, 6) == -1); 132 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar, 7) == -1); // FIXME: Should we reject this? 133 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar + 6, 6) == 0); 134 static_assert(__builtin_wcsncmp(kFoobar, kFoobazfoobar + 6, 7) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} 135 136 static_assert(__builtin_wmemcmp(L"abaa", L"abba", 3) == -1); 137 static_assert(__builtin_wmemcmp(L"abaa", L"abba", 2) == 0); 138 static_assert(__builtin_wmemcmp(0, 0, 0) == 0); 139 #if __WCHAR_WIDTH__ == 32 140 static_assert(__builtin_wmemcmp(L"a\x83838383", L"aa", 2) == 141 (wchar_t)-1U >> 31); 142 #endif 143 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}} 144 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 100) == -1); // FIXME: Should we reject this? 145 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 7) == -1); 146 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 6) == -1); 147 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 5) == 0); 148 149 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}} 150 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}} 151 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}} 152 } 153 154 namespace StrchrEtc { 155 constexpr const char *kStr = "abca\xff\0d"; 156 constexpr char kFoo[] = {'f', 'o', 'o'}; 157 static_assert(__builtin_strchr(kStr, 'a') == kStr); 158 static_assert(__builtin_strchr(kStr, 'b') == kStr + 1); 159 static_assert(__builtin_strchr(kStr, 'c') == kStr + 2); 160 static_assert(__builtin_strchr(kStr, 'd') == nullptr); 161 static_assert(__builtin_strchr(kStr, 'e') == nullptr); 162 static_assert(__builtin_strchr(kStr, '\0') == kStr + 5); 163 static_assert(__builtin_strchr(kStr, 'a' + 256) == nullptr); 164 static_assert(__builtin_strchr(kStr, 'a' - 256) == nullptr); 165 static_assert(__builtin_strchr(kStr, '\xff') == kStr + 4); 166 static_assert(__builtin_strchr(kStr, '\xff' + 256) == nullptr); 167 static_assert(__builtin_strchr(kStr, '\xff' - 256) == nullptr); 168 static_assert(__builtin_strchr(kFoo, 'o') == kFoo + 1); 169 static_assert(__builtin_strchr(kFoo, 'x') == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} 170 static_assert(__builtin_strchr(nullptr, 'x') == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}} 171 172 static_assert(__builtin_memchr(kStr, 'a', 0) == nullptr); 173 static_assert(__builtin_memchr(kStr, 'a', 1) == kStr); 174 static_assert(__builtin_memchr(kStr, '\0', 5) == nullptr); 175 static_assert(__builtin_memchr(kStr, '\0', 6) == kStr + 5); 176 static_assert(__builtin_memchr(kStr, '\xff', 8) == kStr + 4); 177 static_assert(__builtin_memchr(kStr, '\xff' + 256, 8) == kStr + 4); 178 static_assert(__builtin_memchr(kStr, '\xff' - 256, 8) == kStr + 4); 179 static_assert(__builtin_memchr(kFoo, 'x', 3) == nullptr); 180 static_assert(__builtin_memchr(kFoo, 'x', 4) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} 181 static_assert(__builtin_memchr(nullptr, 'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}} 182 static_assert(__builtin_memchr(nullptr, 'x', 0) == nullptr); // FIXME: Should we reject this? 183 184 static_assert(__builtin_char_memchr(kStr, 'a', 0) == nullptr); 185 static_assert(__builtin_char_memchr(kStr, 'a', 1) == kStr); 186 static_assert(__builtin_char_memchr(kStr, '\0', 5) == nullptr); 187 static_assert(__builtin_char_memchr(kStr, '\0', 6) == kStr + 5); 188 static_assert(__builtin_char_memchr(kStr, '\xff', 8) == kStr + 4); 189 static_assert(__builtin_char_memchr(kStr, '\xff' + 256, 8) == kStr + 4); 190 static_assert(__builtin_char_memchr(kStr, '\xff' - 256, 8) == kStr + 4); 191 static_assert(__builtin_char_memchr(kFoo, 'x', 3) == nullptr); 192 static_assert(__builtin_char_memchr(kFoo, 'x', 4) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} 193 static_assert(__builtin_char_memchr(nullptr, 'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}} 194 static_assert(__builtin_char_memchr(nullptr, 'x', 0) == nullptr); // FIXME: Should we reject this? 195 196 static_assert(*__builtin_char_memchr(kStr, '\xff', 8) == '\xff'); 197 constexpr bool char_memchr_mutable() { 198 char buffer[] = "mutable"; 199 *__builtin_char_memchr(buffer, 't', 8) = 'r'; 200 *__builtin_char_memchr(buffer, 'm', 8) = 'd'; 201 return __builtin_strcmp(buffer, "durable") == 0; 202 } 203 static_assert(char_memchr_mutable()); 204 205 constexpr bool a = !strchr("hello", 'h'); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strchr' cannot be used in a constant expression}} 206 constexpr bool b = !memchr("hello", 'h', 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'memchr' cannot be used in a constant expression}} 207 } 208 209 namespace WcschrEtc { 210 constexpr const wchar_t *kStr = L"abca\xffff\0dL"; 211 constexpr wchar_t kFoo[] = {L'f', L'o', L'o'}; 212 static_assert(__builtin_wcschr(kStr, L'a') == kStr); 213 static_assert(__builtin_wcschr(kStr, L'b') == kStr + 1); 214 static_assert(__builtin_wcschr(kStr, L'c') == kStr + 2); 215 static_assert(__builtin_wcschr(kStr, L'd') == nullptr); 216 static_assert(__builtin_wcschr(kStr, L'e') == nullptr); 217 static_assert(__builtin_wcschr(kStr, L'\0') == kStr + 5); 218 static_assert(__builtin_wcschr(kStr, L'a' + 256) == nullptr); 219 static_assert(__builtin_wcschr(kStr, L'a' - 256) == nullptr); 220 static_assert(__builtin_wcschr(kStr, L'\xffff') == kStr + 4); 221 static_assert(__builtin_wcschr(kFoo, L'o') == kFoo + 1); 222 static_assert(__builtin_wcschr(kFoo, L'x') == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} 223 static_assert(__builtin_wcschr(nullptr, L'x') == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}} 224 225 static_assert(__builtin_wmemchr(kStr, L'a', 0) == nullptr); 226 static_assert(__builtin_wmemchr(kStr, L'a', 1) == kStr); 227 static_assert(__builtin_wmemchr(kStr, L'\0', 5) == nullptr); 228 static_assert(__builtin_wmemchr(kStr, L'\0', 6) == kStr + 5); 229 static_assert(__builtin_wmemchr(kStr, L'\xffff', 8) == kStr + 4); 230 static_assert(__builtin_wmemchr(kFoo, L'x', 3) == nullptr); 231 static_assert(__builtin_wmemchr(kFoo, L'x', 4) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} 232 static_assert(__builtin_wmemchr(nullptr, L'x', 3) == nullptr); // expected-error {{not an integral constant}} expected-note {{dereferenced null}} 233 static_assert(__builtin_wmemchr(nullptr, L'x', 0) == nullptr); // FIXME: Should we reject this? 234 235 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}} 236 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}} 237 } 238