1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_PRINT 11#define _LIBCPP_PRINT 12 13/* 14namespace std { 15 // [print.fun], print functions 16 template<class... Args> 17 void print(format_string<Args...> fmt, Args&&... args); 18 template<class... Args> 19 void print(FILE* stream, format_string<Args...> fmt, Args&&... args); 20 21 template<class... Args> 22 void println(format_string<Args...> fmt, Args&&... args); 23 template<class... Args> 24 void println(FILE* stream, format_string<Args...> fmt, Args&&... args); 25 26 void vprint_unicode(string_view fmt, format_args args); 27 void vprint_unicode(FILE* stream, string_view fmt, format_args args); 28 29 void vprint_nonunicode(string_view fmt, format_args args); 30 void vprint_nonunicode(FILE* stream, string_view fmt, format_args args); 31} 32*/ 33 34#include <__assert> // all public C++ headers provide the assertion handler 35#include <__concepts/same_as.h> 36#include <__config> 37#include <__format/buffer.h> 38#include <__format/format_arg_store.h> 39#include <__format/format_args.h> 40#include <__format/format_context.h> 41#include <__format/format_error.h> 42#include <__format/format_functions.h> 43#include <__format/unicode.h> 44#include <__system_error/system_error.h> 45#include <__utility/forward.h> 46#include <cerrno> 47#include <cstdio> 48#include <string> 49#include <string_view> 50#include <version> 51 52#if __has_include(<unistd.h>) 53# include <unistd.h> 54#endif 55 56#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 57# pragma GCC system_header 58#endif 59 60_LIBCPP_BEGIN_NAMESPACE_STD 61 62#ifdef _LIBCPP_WIN32API 63_LIBCPP_EXPORTED_FROM_ABI bool __is_windows_terminal(FILE* __stream); 64 65# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 66// A wrapper for WriteConsoleW which is used to write to the Windows 67// console. This function is in the dylib to avoid pulling in windows.h 68// in the library headers. The function itself uses some private parts 69// of the dylib too. 70// 71// The function does not depend on the language standard used. Guarding 72// it with C++23 would fail since the dylib is currently built using C++20. 73// 74// Note the function is only implemented on the Windows platform. 75_LIBCPP_EXPORTED_FROM_ABI void __write_to_windows_console(FILE* __stream, wstring_view __view); 76# endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS 77 78#endif // _LIBCPP_WIN32API 79 80#if _LIBCPP_STD_VER >= 23 81 82# ifndef _LIBCPP_HAS_NO_UNICODE 83// This is the code to transcode UTF-8 to UTF-16. This is used on 84// Windows for the native Unicode API. The code is modeled to make it 85// easier to extend to 86// 87// P2728R0 Unicode in the Library, Part 1: UTF Transcoding 88// 89// This paper is still under heavy development so it makes no sense yet 90// to strictly follow the paper. 91namespace __unicode { 92 93// The names of these concepts are modelled after P2728R0, but the 94// implementation is not. char16_t may contain 32-bits so depending on the 95// number of bits is an issue. 96# ifdef _LIBCPP_SHORT_WCHAR 97template <class _Tp> 98concept __utf16_code_unit = 99 same_as<_Tp, char16_t> 100# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 101 || same_as<_Tp, wchar_t> 102# endif 103 ; 104template <class _Tp> 105concept __utf32_code_unit = same_as<_Tp, char32_t>; 106# else // _LIBCPP_SHORT_WCHAR 107template <class _Tp> 108concept __utf16_code_unit = same_as<_Tp, char16_t>; 109template <class _Tp> 110concept __utf32_code_unit = 111 same_as<_Tp, char32_t> 112# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 113 || same_as<_Tp, wchar_t> 114# endif 115 ; 116# endif // _LIBCPP_SHORT_WCHAR 117 118// Pass by reference since an output_iterator may not be copyable. 119template <class _OutIt> 120_LIBCPP_HIDE_FROM_ABI constexpr void __encode(_OutIt&, char32_t) = delete; 121 122template <class _OutIt> 123 requires __utf16_code_unit<iter_value_t<_OutIt>> 124_LIBCPP_HIDE_FROM_ABI constexpr void __encode(_OutIt& __out_it, char32_t __value) { 125 // [print.fun]/7 : "if `out` contains invalid code units, the behavior is undefined and implementations are encouraged 126 // to diagnose it". 127 _LIBCPP_ASSERT_UNCATEGORIZED(__is_scalar_value(__value), "an invalid unicode scalar value results in invalid UTF-16"); 128 129 if (__value < 0x10000) { 130 *__out_it++ = __value; 131 return; 132 } 133 134 __value -= 0x10000; 135 *__out_it++ = 0xd800 + (__value >> 10); 136 *__out_it++ = 0xdc00 + (__value & 0x3FF); 137} 138 139template <class _OutIt> 140 requires __utf32_code_unit<iter_value_t<_OutIt>> 141_LIBCPP_HIDE_FROM_ABI constexpr void __encode(_OutIt& __out_it, char32_t __value) { 142 // [print.fun]/7 : "if `out` contains invalid code units, the behavior is undefined and implementations are encouraged 143 // to diagnose it". 144 _LIBCPP_ASSERT_UNCATEGORIZED(__is_scalar_value(__value), "an invalid unicode scalar value results in invalid UTF-32"); 145 *__out_it++ = __value; 146} 147 148template <class _OutIt, input_iterator _InIt> 149 requires output_iterator<_OutIt, const iter_value_t<_OutIt>&> && (!same_as<iter_value_t<_OutIt>, iter_value_t<_InIt>>) 150_LIBCPP_HIDE_FROM_ABI constexpr _OutIt __transcode(_InIt __first, _InIt __last, _OutIt __out_it) { 151 // The __code_point_view has a basic_string_view interface. 152 // When transcoding becomes part of the standard we probably want to 153 // look at smarter algorithms. 154 // For example, when processing a code point that is encoded in 155 // 1 to 3 code units in UTF-8, the result will always be encoded 156 // in 1 code unit in UTF-16 (code points that require 4 code 157 // units in UTF-8 will require 2 code units in UTF-16). 158 // 159 // Note if P2728 is accepted types like int may become valid. In that case 160 // the __code_point_view should use a span. Libc++ will remove support for 161 // char_traits<int>. 162 163 // TODO PRINT Validate with clang-tidy 164 // NOLINTNEXTLINE(bugprone-dangling-handle) 165 basic_string_view<iter_value_t<_InIt>> __data{__first, __last}; 166 __code_point_view<iter_value_t<_InIt>> __view{__data.begin(), __data.end()}; 167 while (!__view.__at_end()) 168 __unicode::__encode(__out_it, __view.__consume().__code_point); 169 return __out_it; 170} 171 172} // namespace __unicode 173 174# endif // _LIBCPP_HAS_NO_UNICODE 175 176namespace __print { 177 178// [print.fun]/2 179// Effects: If the ordinary literal encoding ([lex.charset]) is UTF-8, equivalent to: 180// vprint_unicode(stream, fmt.str, make_format_args(args...)); 181// Otherwise, equivalent to: 182// vprint_nonunicode(stream, fmt.str, make_format_args(args...)); 183// 184// Based on the compiler and its compilation flags this value is or is 185// not true. As mentioned in P2093R14 this only affects Windows. The 186// test below could also be done for 187// - GCC using __GNUC_EXECUTION_CHARSET_NAME 188// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html 189// - Clang using __clang_literal_encoding__ 190// https://clang.llvm.org/docs/LanguageExtensions.html#builtin-macros 191// (note at the time of writing Clang is hard-coded to UTF-8.) 192// 193 194# ifdef _LIBCPP_HAS_NO_UNICODE 195inline constexpr bool __use_unicode = false; 196# elif defined(_MSVC_EXECUTION_CHARACTER_SET) 197// This is the same test MSVC STL uses in their implementation of <print> 198// See: https://learn.microsoft.com/en-us/windows/win32/intl/code-page-identifiers 199inline constexpr bool __use_unicode = _MSVC_EXECUTION_CHARACTER_SET == 65001; 200# else 201inline constexpr bool __use_unicode = true; 202# endif 203 204_LIBCPP_HIDE_FROM_ABI inline bool __is_terminal(FILE* __stream) { 205 // The macro _LIBCPP_TESTING_PRINT_IS_TERMINAL is used to change 206 // the behavior in the test. This is not part of the public API. 207# ifdef _LIBCPP_TESTING_PRINT_IS_TERMINAL 208 return _LIBCPP_TESTING_PRINT_IS_TERMINAL(__stream); 209# elif defined(_LIBCPP_WIN32API) 210 return std::__is_windows_terminal(__stream); 211# elif __has_include(<unistd.h>) 212 return isatty(fileno(__stream)); 213# else 214# error "Provide a way to determine whether a FILE* is a terminal" 215# endif 216} 217 218template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). 219_LIBCPP_HIDE_FROM_ABI inline void 220__vprint_nonunicode(FILE* __stream, string_view __fmt, format_args __args, bool __write_nl) { 221 _LIBCPP_ASSERT_NON_NULL(__stream, "__stream must be a valid pointer to an output C stream"); 222 string __str = std::vformat(__fmt, __args); 223 if (__write_nl) 224 __str.push_back('\n'); 225 226 size_t __size = fwrite(__str.data(), 1, __str.size(), __stream); 227 if (__size < __str.size()) { 228 if (std::feof(__stream)) 229 std::__throw_system_error(EIO, "EOF while writing the formatted output"); 230 std::__throw_system_error(std::ferror(__stream), "failed to write formatted output"); 231 } 232} 233 234# ifndef _LIBCPP_HAS_NO_UNICODE 235 236// Note these helper functions are mainly used to aid testing. 237// On POSIX systems and Windows the output is no longer considered a 238// terminal when the output is redirected. Typically during testing the 239// output is redirected to be able to capture it. This makes it hard to 240// test this code path. 241template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). 242_LIBCPP_HIDE_FROM_ABI inline void 243__vprint_unicode_posix(FILE* __stream, string_view __fmt, format_args __args, bool __write_nl, bool __is_terminal) { 244 // TODO PRINT Should flush errors throw too? 245 if (__is_terminal) 246 std::fflush(__stream); 247 248 __print::__vprint_nonunicode(__stream, __fmt, __args, __write_nl); 249} 250 251# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 252template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). 253_LIBCPP_HIDE_FROM_ABI inline void 254__vprint_unicode_windows(FILE* __stream, string_view __fmt, format_args __args, bool __write_nl, bool __is_terminal) { 255 if (!__is_terminal) 256 return __print::__vprint_nonunicode(__stream, __fmt, __args, __write_nl); 257 258 // TODO PRINT Should flush errors throw too? 259 std::fflush(__stream); 260 261 string __str = std::vformat(__fmt, __args); 262 // UTF-16 uses the same number or less code units than UTF-8. 263 // However the size of the code unit is 16 bits instead of 8 bits. 264 // 265 // The buffer uses the worst-case estimate and should never resize. 266 // However when the string is large this could lead to OOM. Using a 267 // smaller size might work, but since the buffer uses a grow factor 268 // the final size might be larger when the estimate is wrong. 269 // 270 // TODO PRINT profile and improve the speed of this code. 271 __format::__retarget_buffer<wchar_t> __buffer{__str.size()}; 272 __unicode::__transcode(__str.begin(), __str.end(), __buffer.__make_output_iterator()); 273 if (__write_nl) 274 __buffer.push_back(L'\n'); 275 276 [[maybe_unused]] wstring_view __view = __buffer.__view(); 277 278 // The macro _LIBCPP_TESTING_PRINT_WRITE_TO_WINDOWS_CONSOLE_FUNCTION is used to change 279 // the behavior in the test. This is not part of the public API. 280# ifdef _LIBCPP_TESTING_PRINT_WRITE_TO_WINDOWS_CONSOLE_FUNCTION 281 _LIBCPP_TESTING_PRINT_WRITE_TO_WINDOWS_CONSOLE_FUNCTION(__stream, __view); 282# elif defined(_LIBCPP_WIN32API) 283 std::__write_to_windows_console(__stream, __view); 284# else 285 std::__throw_runtime_error("No defintion of _LIBCPP_TESTING_PRINT_WRITE_TO_WINDOWS_CONSOLE_FUNCTION and " 286 "__write_to_windows_console is not available."); 287# endif 288} 289# endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS 290 291template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). 292_LIBCPP_HIDE_FROM_ABI inline void 293__vprint_unicode([[maybe_unused]] FILE* __stream, 294 [[maybe_unused]] string_view __fmt, 295 [[maybe_unused]] format_args __args, 296 [[maybe_unused]] bool __write_nl) { 297 _LIBCPP_ASSERT_NON_NULL(__stream, "__stream must be a valid pointer to an output C stream"); 298 299 // [print.fun] 300 // 7 - Effects: If stream refers to a terminal capable of displaying 301 // Unicode, writes out to the terminal using the native Unicode 302 // API; if out contains invalid code units, the behavior is 303 // undefined and implementations are encouraged to diagnose it. 304 // Otherwise writes out to stream unchanged. If the native 305 // Unicode API is used, the function flushes stream before 306 // writing out. 307 // 8 - Throws: Any exception thrown by the call to vformat 308 // ([format.err.report]). system_error if writing to the terminal 309 // or stream fails. May throw bad_alloc. 310 // 9 - Recommended practice: If invoking the native Unicode API 311 // requires transcoding, implementations should substitute 312 // invalid code units with U+FFFD replacement character per the 313 // Unicode Standard, Chapter 3.9 U+FFFD Substitution in 314 // Conversion. 315 316 // On non-Windows platforms the Unicode API is the normal file I/O API 317 // so there the call can be forwarded to the non_unicode API. On 318 // Windows there is a different API. This API requires transcoding. 319 320# ifndef _LIBCPP_WIN32API 321 __print::__vprint_unicode_posix(__stream, __fmt, __args, __write_nl, __print::__is_terminal(__stream)); 322# elif !defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS) 323 __print::__vprint_unicode_windows(__stream, __fmt, __args, __write_nl, __print::__is_terminal(__stream)); 324# else 325# error "Windows builds with wchar_t disabled are not supported." 326# endif 327} 328 329# endif // _LIBCPP_HAS_NO_UNICODE 330 331} // namespace __print 332 333template <class... _Args> 334_LIBCPP_HIDE_FROM_ABI void print(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args) { 335# ifndef _LIBCPP_HAS_NO_UNICODE 336 if constexpr (__print::__use_unicode) 337 __print::__vprint_unicode(__stream, __fmt.get(), std::make_format_args(__args...), false); 338 else 339 __print::__vprint_nonunicode(__stream, __fmt.get(), std::make_format_args(__args...), false); 340# else // _LIBCPP_HAS_NO_UNICODE 341 __print::__vprint_nonunicode(__stream, __fmt.get(), std::make_format_args(__args...), false); 342# endif // _LIBCPP_HAS_NO_UNICODE 343} 344 345template <class... _Args> 346_LIBCPP_HIDE_FROM_ABI void print(format_string<_Args...> __fmt, _Args&&... __args) { 347 std::print(stdout, __fmt, std::forward<_Args>(__args)...); 348} 349 350template <class... _Args> 351_LIBCPP_HIDE_FROM_ABI void println(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args) { 352# ifndef _LIBCPP_HAS_NO_UNICODE 353 // Note the wording in the Standard is inefficient. The output of 354 // std::format is a std::string which is then copied. This solution 355 // just appends a newline at the end of the output. 356 if constexpr (__print::__use_unicode) 357 __print::__vprint_unicode(__stream, __fmt.get(), std::make_format_args(__args...), true); 358 else 359 __print::__vprint_nonunicode(__stream, __fmt.get(), std::make_format_args(__args...), true); 360# else // _LIBCPP_HAS_NO_UNICODE 361 __print::__vprint_nonunicode(__stream, __fmt.get(), std::make_format_args(__args...), true); 362# endif // _LIBCPP_HAS_NO_UNICODE 363} 364 365template <class... _Args> 366_LIBCPP_HIDE_FROM_ABI void println(format_string<_Args...> __fmt, _Args&&... __args) { 367 std::println(stdout, __fmt, std::forward<_Args>(__args)...); 368} 369 370# ifndef _LIBCPP_HAS_NO_UNICODE 371template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). 372_LIBCPP_HIDE_FROM_ABI inline void vprint_unicode(FILE* __stream, string_view __fmt, format_args __args) { 373 __print::__vprint_unicode(__stream, __fmt, __args, false); 374} 375 376template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). 377_LIBCPP_HIDE_FROM_ABI inline void vprint_unicode(string_view __fmt, format_args __args) { 378 std::vprint_unicode(stdout, __fmt, __args); 379} 380 381# endif // _LIBCPP_HAS_NO_UNICODE 382 383template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). 384_LIBCPP_HIDE_FROM_ABI inline void vprint_nonunicode(FILE* __stream, string_view __fmt, format_args __args) { 385 __print::__vprint_nonunicode(__stream, __fmt, __args, false); 386} 387 388template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). 389_LIBCPP_HIDE_FROM_ABI inline void vprint_nonunicode(string_view __fmt, format_args __args) { 390 std::vprint_nonunicode(stdout, __fmt, __args); 391} 392 393#endif // _LIBCPP_STD_VER >= 23 394 395_LIBCPP_END_NAMESPACE_STD 396 397#endif // _LIBCPP_PRINT 398