1 // RUN: %clang_cc1 %s -std=c++1z -fsyntax-only -verify -pedantic 2 3 # 4 "/usr/include/string.h" 1 3 4 4 extern "C" { 5 typedef decltype(sizeof(int)) size_t; 6 7 extern size_t strlen(const char *p); 8 9 extern int strcmp(const char *s1, const char *s2); 10 extern int strncmp(const char *s1, const char *s2, size_t n); 11 extern int memcmp(const char *s1, const char *s2, size_t n); // expected-note {{here}} 12 } 13 14 # 15 "SemaCXX/constexpr-string.cpp" 2 15 namespace Strlen { 16 constexpr int n = __builtin_strlen("hello"); // ok 17 constexpr int m = strlen("hello"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strlen' cannot be used in a constant expression}} 18 19 // Make sure we can evaluate a call to strlen. 20 int arr[3]; // expected-note {{here}} 21 int k = arr[strlen("hello")]; // expected-warning {{array index 5}} 22 } 23 24 namespace StrcmpEtc { 25 constexpr char kFoobar[6] = {'f','o','o','b','a','r'}; 26 constexpr char kFoobazfoobar[12] = {'f','o','o','b','a','z','f','o','o','b','a','r'}; 27 28 static_assert(__builtin_strcmp("abab", "abab") == 0); 29 static_assert(__builtin_strcmp("abab", "abba") == -1); 30 static_assert(__builtin_strcmp("abab", "abaa") == 1); 31 static_assert(__builtin_strcmp("ababa", "abab") == 1); 32 static_assert(__builtin_strcmp("abab", "ababa") == -1); 33 static_assert(__builtin_strcmp("abab\0banana", "abab") == 0); 34 static_assert(__builtin_strcmp("abab", "abab\0banana") == 0); 35 static_assert(__builtin_strcmp("abab\0banana", "abab\0canada") == 0); 36 static_assert(__builtin_strcmp(0, "abab") == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}} 37 static_assert(__builtin_strcmp("abab", 0) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced null}} 38 39 static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar) == -1); // FIXME: Should we reject this? 40 static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar + 6) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} 41 42 static_assert(__builtin_strncmp("abaa", "abba", 5) == -1); 43 static_assert(__builtin_strncmp("abaa", "abba", 4) == -1); 44 static_assert(__builtin_strncmp("abaa", "abba", 3) == -1); 45 static_assert(__builtin_strncmp("abaa", "abba", 2) == 0); 46 static_assert(__builtin_strncmp("abaa", "abba", 1) == 0); 47 static_assert(__builtin_strncmp("abaa", "abba", 0) == 0); 48 static_assert(__builtin_strncmp(0, 0, 0) == 0); 49 static_assert(__builtin_strncmp("abab\0banana", "abab\0canada", 100) == 0); 50 51 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 6) == -1); 52 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar, 7) == -1); // FIXME: Should we reject this? 53 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 6) == 0); 54 static_assert(__builtin_strncmp(kFoobar, kFoobazfoobar + 6, 7) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} 55 56 static_assert(__builtin_memcmp("abaa", "abba", 3) == -1); 57 static_assert(__builtin_memcmp("abaa", "abba", 2) == 0); 58 static_assert(__builtin_memcmp(0, 0, 0) == 0); 59 static_assert(__builtin_memcmp("abab\0banana", "abab\0banana", 100) == 0); // expected-error {{not an integral constant}} expected-note {{dereferenced one-past-the-end}} 60 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 100) == -1); // FIXME: Should we reject this? 61 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 7) == -1); 62 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 6) == -1); 63 static_assert(__builtin_memcmp("abab\0banana", "abab\0canada", 5) == 0); 64 65 constexpr int a = strcmp("hello", "world"); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strcmp' cannot be used in a constant expression}} 66 constexpr int b = strncmp("hello", "world", 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'strncmp' cannot be used in a constant expression}} 67 constexpr int c = memcmp("hello", "world", 3); // expected-error {{constant expression}} expected-note {{non-constexpr function 'memcmp' cannot be used in a constant expression}} 68 } 69