2727#include " llvm/ADT/StringRef.h"
2828#include " llvm/Support/Compiler.h"
2929#include " llvm/Support/DataTypes.h"
30+ #include " llvm/Support/raw_ostream.h"
3031#include < cassert>
3132#include < cstdio>
3233#include < optional>
3536
3637namespace llvm {
3738
38- // / This is a helper class used for handling formatted output. It is the
39- // / abstract base class of a templated derived class.
40- class LLVM_ABI format_object_base {
41- protected:
42- const char *Fmt;
43- ~format_object_base () = default ; // Disallow polymorphic deletion.
44- format_object_base (const format_object_base &) = default;
45- virtual void home (); // Out of line virtual method.
46-
47- // / Call snprintf() for this object, on the given buffer and size.
48- virtual int snprint (char *Buffer, unsigned BufferSize) const = 0;
49-
50- public:
51- format_object_base (const char *fmt) : Fmt(fmt) {}
52-
53- // / Format the object into the specified buffer. On success, this returns
54- // / the length of the formatted string. If the buffer is too small, this
55- // / returns a length to retry with, which will be larger than BufferSize.
56- unsigned print (char *Buffer, unsigned BufferSize) const {
57- assert (BufferSize && " Invalid buffer size!" );
58-
59- // Print the string, leaving room for the terminating null.
60- int N = snprint (Buffer, BufferSize);
61-
62- // VC++ and old GlibC return negative on overflow, just double the size.
63- if (N < 0 )
64- return BufferSize * 2 ;
65-
66- // Other implementations yield number of bytes needed, not including the
67- // final '\0'.
68- if (unsigned (N) >= BufferSize)
69- return N + 1 ;
70-
71- // Otherwise N is the length of output (not including the final '\0').
72- return N;
73- }
74- };
75-
7639// / These are templated helper classes used by the format function that
7740// / capture the object to be formatted and the format string. When actually
7841// / printed, this synthesizes the string into a temporary buffer provided and
@@ -89,8 +52,8 @@ template <typename T>
8952using decay_if_c_char_array_t = typename decay_if_c_char_array<T>::type;
9053} // namespace detail
9154
92- template <typename ... Ts>
93- class format_object final : public format_object_base {
55+ template <typename ... Ts> class format_object {
56+ const char *Fmt;
9457 std::tuple<detail::decay_if_c_char_array_t <Ts>...> Vals;
9558
9659 template <std::size_t ... Is>
@@ -104,18 +67,43 @@ class format_object final : public format_object_base {
10467 }
10568
10669public:
107- format_object (const char *fmt, const Ts &... vals)
108- : format_object_base(fmt), Vals(vals...) {
70+ format_object (const char *fmt, const Ts &...vals) : Fmt(fmt), Vals(vals...) {
10971 static_assert (
11072 (std::is_scalar_v<detail::decay_if_c_char_array_t <Ts>> && ...),
11173 " format can't be used with non fundamental / non pointer type" );
11274 }
11375
114- int snprint (char *Buffer, unsigned BufferSize) const override {
76+ int snprint (char *Buffer, unsigned BufferSize) const {
11577 return snprint_tuple (Buffer, BufferSize, std::index_sequence_for<Ts...>());
11678 }
79+
80+ unsigned print (char *Buffer, unsigned BufferSize) const {
81+ assert (BufferSize && " Invalid buffer size!" );
82+
83+ // Print the string, leaving room for the terminating null.
84+ int N = snprint (Buffer, BufferSize);
85+
86+ // VC++ and old GlibC return negative on overflow, just double the size.
87+ if (N < 0 )
88+ return BufferSize * 2 ;
89+
90+ // Other implementations yield number of bytes needed, not including the
91+ // final '\0'.
92+ if (unsigned (N) >= BufferSize)
93+ return N + 1 ;
94+
95+ // Otherwise N is the length of output (not including the final '\0').
96+ return N;
97+ }
11798};
11899
100+ template <typename ... Ts>
101+ raw_ostream &operator <<(raw_ostream &OS , format_object<Ts...> Fmt) {
102+ OS <<
103+ [&Fmt](char *Buf, size_t Size) -> size_t { return Fmt.print (Buf, Size); };
104+ return OS ;
105+ }
106+
119107// / These are helper functions used to produce formatted output. They use
120108// / template type deduction to construct the appropriate instance of the
121109// / format_object class to simplify their construction.
0 commit comments