-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc++][print] Adds ostream overloads. #73262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,13 +159,24 @@ basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, cons | |
template<class traits> | ||
basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, const char32_t*) = delete; // since C++20 | ||
|
||
// [ostream.formatted.print], print functions | ||
template<class... Args> // since C++23 | ||
void print(ostream& os, format_string<Args...> fmt, Args&&... args); | ||
template<class... Args> // since C++23 | ||
void println(ostream& os, format_string<Args...> fmt, Args&&... args); | ||
|
||
void vprint_unicode(ostream& os, string_view fmt, format_args args); // since C++23 | ||
void vprint_nonunicode(ostream& os, string_view fmt, format_args args); // since C++23 | ||
} // std | ||
|
||
*/ | ||
|
||
#include <__assert> // all public C++ headers provide the assertion handler | ||
#include <__availability> | ||
#include <__config> | ||
#include <__exception/operations.h> | ||
#include <__format/format_args.h> | ||
#include <__format/format_functions.h> | ||
#include <__fwd/ostream.h> | ||
#include <__memory/shared_ptr.h> | ||
#include <__memory/unique_ptr.h> | ||
|
@@ -176,10 +187,13 @@ basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, cons | |
#include <__type_traits/void_t.h> | ||
#include <__utility/declval.h> | ||
#include <bitset> | ||
#include <cstdio> | ||
#include <ios> | ||
#include <locale> | ||
#include <new> | ||
#include <print> | ||
#include <streambuf> | ||
#include <string_view> | ||
#include <version> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
|
@@ -1005,6 +1019,151 @@ extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>; | |
extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>; | ||
#endif | ||
|
||
#if _LIBCPP_STD_VER >= 23 | ||
|
||
template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). | ||
_LIBCPP_HIDE_FROM_ABI inline void | ||
__vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) { | ||
// [ostream.formatted.print]/3 | ||
// Effects: Behaves as a formatted output function | ||
// ([ostream.formatted.reqmts]) of os, except that: | ||
// - failure to generate output is reported as specified below, and | ||
// - any exception thrown by the call to vformat is propagated without regard | ||
// to the value of os.exceptions() and without turning on ios_base::badbit | ||
// in the error state of os. | ||
// After constructing a sentry object, the function initializes an automatic | ||
// variable via | ||
// string out = vformat(os.getloc(), fmt, args); | ||
|
||
ostream::sentry __s(__os); | ||
if (__s) { | ||
mordante marked this conversation as resolved.
Show resolved
Hide resolved
|
||
string __o = std::vformat(__os.getloc(), __fmt, __args); | ||
if (__write_nl) | ||
__o += '\n'; | ||
|
||
const char* __str = __o.data(); | ||
size_t __len = __o.size(); | ||
|
||
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS | ||
try { | ||
# endif // _LIBCPP_HAS_NO_EXCEPTIONS | ||
typedef ostreambuf_iterator<char> _Ip; | ||
if (std::__pad_and_output( | ||
_Ip(__os), | ||
__str, | ||
(__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str, | ||
__str + __len, | ||
__os, | ||
__os.fill()) | ||
.failed()) | ||
__os.setstate(ios_base::badbit | ios_base::failbit); | ||
|
||
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS | ||
} catch (...) { | ||
__os.__set_badbit_and_consider_rethrow(); | ||
} | ||
# endif // _LIBCPP_HAS_NO_EXCEPTIONS | ||
} | ||
} | ||
|
||
template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). | ||
_LIBCPP_HIDE_FROM_ABI inline void vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args) { | ||
std::__vprint_nonunicode(__os, __fmt, __args, false); | ||
} | ||
|
||
// Returns the FILE* associated with the __os. | ||
// Returns a nullptr when no FILE* is associated with __os. | ||
// This function is in the dylib since the type of the buffer associated | ||
// with std::cout, std::cerr, and std::clog is only known in the dylib. | ||
// | ||
// This function implements part of the implementation-defined behavior | ||
// of [ostream.formatted.print]/3 | ||
// If the function is vprint_unicode and os is a stream that refers to | ||
// a terminal capable of displaying Unicode which is determined in an | ||
// implementation-defined manner, writes out to the terminal using the | ||
// native Unicode API; | ||
// Whether the returned FILE* is "a terminal capable of displaying Unicode" | ||
// is determined in the same way as the print(FILE*, ...) overloads. | ||
_LIBCPP_AVAILABILITY_PRINT _LIBCPP_EXPORTED_FROM_ABI FILE* __get_ostream_file(ostream& __os); | ||
|
||
# ifndef _LIBCPP_HAS_NO_UNICODE | ||
template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). | ||
_LIBCPP_AVAILABILITY_PRINT _LIBCPP_HIDE_FROM_ABI void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a follow-up, we could use We could then even remove the availability annotations on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created #75225 for this. |
||
__vprint_unicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) { | ||
FILE* __file = std::__get_ostream_file(__os); | ||
if (!__file || !__print::__is_terminal(__file)) | ||
return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl); | ||
|
||
// [ostream.formatted.print]/3 | ||
// If the function is vprint_unicode and os is a stream that refers to a | ||
// terminal capable of displaying Unicode which is determined in an | ||
// implementation-defined manner, writes out to the terminal using the | ||
// native Unicode API; if out contains invalid code units, the behavior is | ||
// undefined and implementations are encouraged to diagnose it. If the | ||
// native Unicode API is used, the function flushes os before writing out. | ||
// | ||
// This is the path for the native API, start with flushing. | ||
__os.flush(); | ||
|
||
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS | ||
try { | ||
# endif // _LIBCPP_HAS_NO_EXCEPTIONS | ||
ostream::sentry __s(__os); | ||
if (__s) { | ||
# ifndef _LIBCPP_WIN32API | ||
__print::__vprint_unicode_posix(__file, __fmt, __args, __write_nl, true); | ||
# elif !defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS) | ||
__print::__vprint_unicode_windows(__file, __fmt, __args, __write_nl, true); | ||
# else | ||
# error "Windows builds with wchar_t disabled are not supported." | ||
# endif | ||
} | ||
|
||
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS | ||
} catch (...) { | ||
__os.__set_badbit_and_consider_rethrow(); | ||
} | ||
# endif // _LIBCPP_HAS_NO_EXCEPTIONS | ||
} | ||
|
||
template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). | ||
_LIBCPP_AVAILABILITY_PRINT _LIBCPP_HIDE_FROM_ABI inline void | ||
vprint_unicode(ostream& __os, string_view __fmt, format_args __args) { | ||
std::__vprint_unicode(__os, __fmt, __args, false); | ||
} | ||
# endif // _LIBCPP_HAS_NO_UNICODE | ||
|
||
template <class... _Args> | ||
_LIBCPP_AVAILABILITY_PRINT _LIBCPP_HIDE_FROM_ABI void | ||
print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) { | ||
# ifndef _LIBCPP_HAS_NO_UNICODE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason why we have both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest renaming this to something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #76290 |
||
if constexpr (__print::__use_unicode) | ||
std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), false); | ||
else | ||
std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false); | ||
# else // _LIBCPP_HAS_NO_UNICODE | ||
std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false); | ||
# endif // _LIBCPP_HAS_NO_UNICODE | ||
} | ||
|
||
template <class... _Args> | ||
_LIBCPP_AVAILABILITY_PRINT _LIBCPP_HIDE_FROM_ABI void | ||
println(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) { | ||
# ifndef _LIBCPP_HAS_NO_UNICODE | ||
// Note the wording in the Standard is inefficient. The output of | ||
// std::format is a std::string which is then copied. This solution | ||
// just appends a newline at the end of the output. | ||
if constexpr (__print::__use_unicode) | ||
std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), true); | ||
else | ||
std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true); | ||
# else // _LIBCPP_HAS_NO_UNICODE | ||
std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true); | ||
# endif // _LIBCPP_HAS_NO_UNICODE | ||
} | ||
|
||
#endif // _LIBCPP_STD_VER >= 23 | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 | ||
|
Uh oh!
There was an error while loading. Please reload this page.