1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef SUPPORT_TEST_MAKE_STRING_H 10 #define SUPPORT_TEST_MAKE_STRING_H 11 12 #include "test_macros.h" 13 14 #if TEST_STD_VER < 11 15 #error This header requires C++11 or greater 16 #endif 17 18 #include <string> 19 #include <string_view> 20 21 #if TEST_STD_VER > 17 && defined(__cpp_char8_t) 22 #define CHAR8_ONLY(x) x, 23 #else 24 #define CHAR8_ONLY(x) 25 #endif 26 27 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 28 # define IF_WIDE_CHARACTERS(...) __VA_ARGS__ 29 #else 30 # define IF_WIDE_CHARACTERS(...) /* nothing */ 31 #endif 32 33 #define MKSTR(Str) \ 34 { \ 35 Str, IF_WIDE_CHARACTERS(TEST_CONCAT(L, Str),) \ 36 CHAR8_ONLY(TEST_CONCAT(u8, Str)) TEST_CONCAT(u, Str), \ 37 TEST_CONCAT(U, Str) \ 38 } 39 40 struct MultiStringType { 41 const char* s; 42 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 43 const wchar_t* w; 44 #endif 45 #if TEST_STD_VER > 17 && defined(__cpp_char8_t) 46 const char8_t* u8; 47 #endif 48 const char16_t* u16; 49 const char32_t* u32; 50 51 constexpr operator const char*() const { return s; } 52 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 53 constexpr operator const wchar_t*() const { return w; } 54 #endif 55 #if TEST_STD_VER > 17 && defined(__cpp_char8_t) 56 constexpr operator const char8_t*() const { return u8; } 57 #endif 58 constexpr operator const char16_t*() const { return u16; } 59 constexpr operator const char32_t*() const { return u32; } 60 }; 61 62 // Helper to convert a const char* string to a basic_string<CharT>. 63 // This helper is used in unit tests to make them generic. The input should be 64 // valid ASCII which means the input is also valid UTF-8. 65 #define MAKE_STRING(CharT, Str) \ 66 std::basic_string<CharT> { \ 67 static_cast<const CharT*>(MultiStringType MKSTR(Str)) \ 68 } 69 70 #define MAKE_STRING_VIEW(CharT, Str) \ 71 std::basic_string_view<CharT> { \ 72 static_cast<const CharT*>(MultiStringType MKSTR(Str)) \ 73 } 74 75 // Like MAKE_STRING but converts to a const CharT*. 76 #define MAKE_CSTRING(CharT, Str) \ 77 static_cast<const CharT*>(MultiStringType MKSTR(Str)) 78 79 #endif 80