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 #define MKSTR(Str)                                                             \
28   {                                                                            \
29     Str, TEST_CONCAT(L, Str),                                                  \
30         CHAR8_ONLY(TEST_CONCAT(u8, Str)) TEST_CONCAT(u, Str),                  \
31         TEST_CONCAT(U, Str)                                                    \
32   }
33 
34 struct MultiStringType {
35   const char* s;
36   const wchar_t* w;
37 #if TEST_STD_VER > 17 && defined(__cpp_char8_t)
38   const char8_t* u8;
39 #endif
40   const char16_t* u16;
41   const char32_t* u32;
42 
43   constexpr operator const char*() const { return s; }
44   constexpr operator const wchar_t*() const { return w; }
45 #if TEST_STD_VER > 17 && defined(__cpp_char8_t)
46   constexpr operator const char8_t*() const { return u8; }
47 #endif
48   constexpr operator const char16_t*() const { return u16; }
49   constexpr operator const char32_t*() const { return u32; }
50 };
51 
52 // Helper to convert a const char* string to a basic_string<CharT>.
53 // This helper is used in unit tests to make them generic. The input should be
54 // valid ASCII which means the input is also valid UTF-8.
55 #define MAKE_STRING(CharT, Str)                                                \
56   std::basic_string<CharT> {                                                   \
57     static_cast<const CharT*>(MultiStringType MKSTR(Str))                      \
58   }
59 
60 #define MAKE_STRING_VIEW(CharT, Str)                                           \
61   std::basic_string_view<CharT> {                                              \
62     static_cast<const CharT*>(MultiStringType MKSTR(Str))                      \
63   }
64 
65 // Like MAKE_STRING but converts to a const CharT*.
66 #define MAKE_CSTRING(CharT, Str)                                               \
67   static_cast<const CharT*>(MultiStringType MKSTR(Str))
68 
69 #endif
70