|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#include <algorithm> |
| 5 | +#include <cassert> |
| 6 | +#include <concepts> |
| 7 | +#include <cstddef> |
| 8 | +#include <format> |
| 9 | +#include <memory_resource> |
| 10 | +#include <string> |
| 11 | +#include <string_view> |
| 12 | +#include <type_traits> |
| 13 | +#include <utility> |
| 14 | + |
| 15 | +#include <test_format_support.hpp> |
| 16 | + |
| 17 | +#define STR(Str) TYPED_LITERAL(CharT, Str) |
| 18 | + |
| 19 | +using namespace std; |
| 20 | + |
| 21 | +template <class F> |
| 22 | +concept DebugEnabledSpecialization = is_default_constructible_v<F> && requires(F& fmt) { |
| 23 | + { fmt.set_debug_format() } noexcept -> same_as<void>; |
| 24 | +}; |
| 25 | + |
| 26 | +template <class CharT> |
| 27 | +consteval bool check_debug_enabled_specializations() { |
| 28 | + static_assert(DebugEnabledSpecialization<formatter<char, CharT>>); |
| 29 | + static_assert(DebugEnabledSpecialization<formatter<CharT, CharT>>); |
| 30 | + static_assert(DebugEnabledSpecialization<formatter<CharT*, CharT>>); |
| 31 | + static_assert(DebugEnabledSpecialization<formatter<const CharT*, CharT>>); |
| 32 | + static_assert(DebugEnabledSpecialization<formatter<CharT[3], CharT>>); |
| 33 | + static_assert(DebugEnabledSpecialization<formatter<basic_string<CharT>, CharT>>); |
| 34 | + static_assert(DebugEnabledSpecialization<formatter<pmr::basic_string<CharT>, CharT>>); |
| 35 | + static_assert(DebugEnabledSpecialization<formatter<basic_string_view<CharT>, CharT>>); |
| 36 | + |
| 37 | + static_assert(!DebugEnabledSpecialization<formatter<signed char, CharT>>); |
| 38 | + static_assert(!DebugEnabledSpecialization<formatter<short, CharT>>); |
| 39 | + static_assert(!DebugEnabledSpecialization<formatter<int, CharT>>); |
| 40 | + static_assert(!DebugEnabledSpecialization<formatter<long, CharT>>); |
| 41 | + static_assert(!DebugEnabledSpecialization<formatter<long long, CharT>>); |
| 42 | + |
| 43 | + static_assert(!DebugEnabledSpecialization<formatter<unsigned char, CharT>>); |
| 44 | + // NB: formatter<unsigned short, CharT> is special case, see below |
| 45 | + static_assert(!DebugEnabledSpecialization<formatter<unsigned int, CharT>>); |
| 46 | + static_assert(!DebugEnabledSpecialization<formatter<unsigned long, CharT>>); |
| 47 | + static_assert(!DebugEnabledSpecialization<formatter<unsigned long long, CharT>>); |
| 48 | + |
| 49 | + static_assert(!DebugEnabledSpecialization<formatter<bool, CharT>>); |
| 50 | + static_assert(!DebugEnabledSpecialization<formatter<nullptr_t, CharT>>); |
| 51 | + static_assert(!DebugEnabledSpecialization<formatter<void*, CharT>>); |
| 52 | + static_assert(!DebugEnabledSpecialization<formatter<const void*, CharT>>); |
| 53 | + |
| 54 | + // NB: wchar_t might be defined as a typedef for unsigned short (with '/Zc:wchar_t-') |
| 55 | + static_assert(!DebugEnabledSpecialization<formatter<unsigned short, CharT>> |
| 56 | + == (same_as<CharT, char> || !same_as<CharT, unsigned short>) ); |
| 57 | + |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +template <class CharT> |
| 62 | +struct Holder { |
| 63 | + char narrow_ch; |
| 64 | + CharT ch; |
| 65 | + const CharT* const_ptr; |
| 66 | + basic_string<CharT> str; |
| 67 | + CharT* non_const_ptr; |
| 68 | + basic_string_view<CharT> str_view; |
| 69 | + CharT arr[11]; |
| 70 | +}; |
| 71 | + |
| 72 | +// holder-format-specs: |
| 73 | +// member debug-format(opt) |
| 74 | +// member: |
| 75 | +// 0 1 2 ... N (index of member object, single digit) |
| 76 | +// debug-format: |
| 77 | +// $ (use debug format) |
| 78 | +template <class CharT> |
| 79 | +struct std::formatter<Holder<CharT>, CharT> { |
| 80 | +public: |
| 81 | + constexpr formatter() : member_index{-1} {} |
| 82 | + |
| 83 | + constexpr auto parse(basic_format_parse_context<CharT>& ctx) { |
| 84 | + auto it = ctx.begin(); |
| 85 | + if (it == ctx.end() || *it == STR('}')) { |
| 86 | + throw format_error{"Invalid holder-format-specs."}; |
| 87 | + } |
| 88 | + |
| 89 | + if (STR('0') <= *it && *it <= STR('9')) { |
| 90 | + member_index = *it - STR('0'); |
| 91 | + } else { |
| 92 | + throw format_error{"Expected member index in holder-format-specs."}; |
| 93 | + } |
| 94 | + |
| 95 | + ++it; |
| 96 | + if (it == ctx.end() || *it == STR('}')) { |
| 97 | + return it; |
| 98 | + } |
| 99 | + |
| 100 | + if (*it == '$') { |
| 101 | + switch (member_index) { |
| 102 | + case 0: |
| 103 | + fmt0.set_debug_format(); |
| 104 | + break; |
| 105 | + |
| 106 | + case 1: |
| 107 | + fmt1.set_debug_format(); |
| 108 | + break; |
| 109 | + |
| 110 | + case 2: |
| 111 | + fmt2.set_debug_format(); |
| 112 | + break; |
| 113 | + |
| 114 | + case 3: |
| 115 | + fmt3.set_debug_format(); |
| 116 | + break; |
| 117 | + |
| 118 | + case 4: |
| 119 | + fmt4.set_debug_format(); |
| 120 | + break; |
| 121 | + |
| 122 | + case 5: |
| 123 | + fmt5.set_debug_format(); |
| 124 | + break; |
| 125 | + |
| 126 | + case 6: |
| 127 | + fmt6.set_debug_format(); |
| 128 | + break; |
| 129 | + } |
| 130 | + } else { |
| 131 | + throw format_error{"Unexpected symbols in holder-format-specs."}; |
| 132 | + } |
| 133 | + |
| 134 | + ++it; |
| 135 | + if (it != ctx.end() && *it != STR('}')) { |
| 136 | + throw format_error{"Expected '}' at the end of holder-format-specs."}; |
| 137 | + } |
| 138 | + |
| 139 | + return it; |
| 140 | + } |
| 141 | + |
| 142 | + template <class FormatContext> |
| 143 | + auto format(const Holder<CharT>& val, FormatContext& ctx) const { |
| 144 | + switch (member_index) { |
| 145 | + case 0: |
| 146 | + return fmt0.format(val.narrow_ch, ctx); |
| 147 | + case 1: |
| 148 | + return fmt1.format(val.ch, ctx); |
| 149 | + case 2: |
| 150 | + return fmt2.format(val.const_ptr, ctx); |
| 151 | + case 3: |
| 152 | + return fmt3.format(val.str, ctx); |
| 153 | + case 4: |
| 154 | + return fmt4.format(val.non_const_ptr, ctx); |
| 155 | + case 5: |
| 156 | + return fmt5.format(val.str_view, ctx); |
| 157 | + case 6: |
| 158 | + return fmt6.format(val.arr, ctx); |
| 159 | + } |
| 160 | + |
| 161 | + unreachable(); |
| 162 | + } |
| 163 | + |
| 164 | +private: |
| 165 | + int member_index; |
| 166 | + |
| 167 | + formatter<char, CharT> fmt0; |
| 168 | + formatter<CharT, CharT> fmt1; |
| 169 | + formatter<const CharT*, CharT> fmt2; |
| 170 | + formatter<basic_string<CharT>, CharT> fmt3; |
| 171 | + formatter<CharT*, CharT> fmt4; |
| 172 | + formatter<basic_string_view<CharT>, CharT> fmt5; |
| 173 | + formatter<CharT[11], CharT> fmt6; |
| 174 | +}; |
| 175 | + |
| 176 | +template <class CharT> |
| 177 | +void check_set_debug_format_function() { |
| 178 | + Holder<CharT> val; |
| 179 | + |
| 180 | + val.narrow_ch = '\t'; |
| 181 | + val.ch = STR('\t'); |
| 182 | + val.const_ptr = STR("const\tCharT\t*"); |
| 183 | + val.str = STR("basic\tstring"); |
| 184 | + val.non_const_ptr = val.str.data(); |
| 185 | + val.str_view = STR("basic\tstring\tview"); |
| 186 | + ranges::copy(STR("CharT\t[11]\0"sv), val.arr); |
| 187 | + |
| 188 | + assert(format(STR("{:0}"), val) == STR("\t")); |
| 189 | + assert(format(STR("{:1}"), val) == STR("\t")); |
| 190 | + assert(format(STR("{:2}"), val) == STR("const\tCharT\t*")); |
| 191 | + assert(format(STR("{:3}"), val) == STR("basic\tstring")); |
| 192 | + assert(format(STR("{:4}"), val) == STR("basic\tstring")); |
| 193 | + assert(format(STR("{:5}"), val) == STR("basic\tstring\tview")); |
| 194 | + assert(format(STR("{:6}"), val) == STR("CharT\t[11]")); |
| 195 | + |
| 196 | + assert(format(STR("{:0$}"), val) == STR(R"('\t')")); |
| 197 | + assert(format(STR("{:1$}"), val) == STR(R"('\t')")); |
| 198 | + assert(format(STR("{:2$}"), val) == STR(R"("const\tCharT\t*")")); |
| 199 | + assert(format(STR("{:3$}"), val) == STR(R"("basic\tstring")")); |
| 200 | + assert(format(STR("{:4$}"), val) == STR(R"("basic\tstring")")); |
| 201 | + assert(format(STR("{:5$}"), val) == STR(R"("basic\tstring\tview")")); |
| 202 | + assert(format(STR("{:6$}"), val) == STR(R"("CharT\t[11]")")); |
| 203 | +} |
| 204 | + |
| 205 | +int main() { |
| 206 | + static_assert(check_debug_enabled_specializations<char>()); |
| 207 | + static_assert(check_debug_enabled_specializations<wchar_t>()); |
| 208 | + |
| 209 | + check_set_debug_format_function<char>(); |
| 210 | + check_set_debug_format_function<wchar_t>(); |
| 211 | +} |
0 commit comments