print 16 KB

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