print 15 KB

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