Revert "Remove the explicit SDNodeIterator::operator= in favor of the implicit default"
[oota-llvm.git] / include / llvm / Support / Format.h
1 //===- Format.h - Efficient printf-style formatting for streams -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the format() function, which can be used with other
11 // LLVM subsystems to provide printf-style formatting.  This gives all the power
12 // and risk of printf.  This can be used like this (with raw_ostreams as an
13 // example):
14 //
15 //    OS << "mynumber: " << format("%4.5f", 1234.412) << '\n';
16 //
17 // Or if you prefer:
18 //
19 //  OS << format("mynumber: %4.5f\n", 1234.412);
20 //
21 //===----------------------------------------------------------------------===//
22
23 #ifndef LLVM_SUPPORT_FORMAT_H
24 #define LLVM_SUPPORT_FORMAT_H
25
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/Support/DataTypes.h"
29 #include <cassert>
30 #include <cstdio>
31 #include <tuple>
32
33 namespace llvm {
34
35 /// This is a helper class used for handling formatted output.  It is the
36 /// abstract base class of a templated derived class.
37 class format_object_base {
38 protected:
39   const char *Fmt;
40   ~format_object_base() {} // Disallow polymorphic deletion.
41   virtual void home(); // Out of line virtual method.
42
43   /// Call snprintf() for this object, on the given buffer and size.
44   virtual int snprint(char *Buffer, unsigned BufferSize) const = 0;
45
46 public:
47   format_object_base(const char *fmt) : Fmt(fmt) {}
48
49   /// Format the object into the specified buffer.  On success, this returns
50   /// the length of the formatted string.  If the buffer is too small, this
51   /// returns a length to retry with, which will be larger than BufferSize.
52   unsigned print(char *Buffer, unsigned BufferSize) const {
53     assert(BufferSize && "Invalid buffer size!");
54
55     // Print the string, leaving room for the terminating null.
56     int N = snprint(Buffer, BufferSize);
57
58     // VC++ and old GlibC return negative on overflow, just double the size.
59     if (N < 0)
60       return BufferSize * 2;
61
62     // Other implementations yield number of bytes needed, not including the
63     // final '\0'.
64     if (unsigned(N) >= BufferSize)
65       return N + 1;
66
67     // Otherwise N is the length of output (not including the final '\0').
68     return N;
69   }
70 };
71
72 /// These are templated helper classes used by the format function that
73 /// capture the object to be formated and the format string. When actually
74 /// printed, this synthesizes the string into a temporary buffer provided and
75 /// returns whether or not it is big enough.
76
77 template <typename... Ts>
78 class format_object final : public format_object_base {
79   std::tuple<Ts...> Vals;
80
81   template <std::size_t... Is>
82   int snprint_tuple(char *Buffer, unsigned BufferSize,
83                     index_sequence<Is...>) const {
84 #ifdef _MSC_VER
85     return _snprintf(Buffer, BufferSize, Fmt, std::get<Is>(Vals)...);
86 #else
87     return snprintf(Buffer, BufferSize, Fmt, std::get<Is>(Vals)...);
88 #endif
89   }
90
91 public:
92   format_object(const char *fmt, const Ts &... vals)
93       : format_object_base(fmt), Vals(vals...) {}
94
95   int snprint(char *Buffer, unsigned BufferSize) const override {
96     return snprint_tuple(Buffer, BufferSize, index_sequence_for<Ts...>());
97   }
98 };
99
100 /// These are helper functions used to produce formatted output.  They use
101 /// template type deduction to construct the appropriate instance of the
102 /// format_object class to simplify their construction.
103 ///
104 /// This is typically used like:
105 /// \code
106 ///   OS << format("%0.4f", myfloat) << '\n';
107 /// \endcode
108
109 template <typename... Ts>
110 inline format_object<Ts...> format(const char *Fmt, const Ts &... Vals) {
111   return format_object<Ts...>(Fmt, Vals...);
112 }
113
114 /// This is a helper class used for left_justify() and right_justify().
115 class FormattedString {
116   StringRef Str;
117   unsigned Width;
118   bool RightJustify;
119   friend class raw_ostream;
120 public:
121     FormattedString(StringRef S, unsigned W, bool R)
122       : Str(S), Width(W), RightJustify(R) { }
123 };
124
125 /// left_justify - append spaces after string so total output is
126 /// \p Width characters.  If \p Str is larger that \p Width, full string
127 /// is written with no padding.
128 inline FormattedString left_justify(StringRef Str, unsigned Width) {
129   return FormattedString(Str, Width, false);
130 }
131
132 /// right_justify - add spaces before string so total output is
133 /// \p Width characters.  If \p Str is larger that \p Width, full string
134 /// is written with no padding.
135 inline FormattedString right_justify(StringRef Str, unsigned Width) {
136   return FormattedString(Str, Width, true);
137 }
138
139 /// This is a helper class used for format_hex() and format_decimal().
140 class FormattedNumber {
141   uint64_t HexValue;
142   int64_t DecValue;
143   unsigned Width;
144   bool Hex;
145   bool Upper;
146   bool HexPrefix;
147   friend class raw_ostream;
148 public:
149   FormattedNumber(uint64_t HV, int64_t DV, unsigned W, bool H, bool U,
150                   bool Prefix)
151       : HexValue(HV), DecValue(DV), Width(W), Hex(H), Upper(U),
152         HexPrefix(Prefix) {}
153 };
154
155 /// format_hex - Output \p N as a fixed width hexadecimal. If number will not
156 /// fit in width, full number is still printed.  Examples:
157 ///   OS << format_hex(255, 4)              => 0xff
158 ///   OS << format_hex(255, 4, true)        => 0xFF
159 ///   OS << format_hex(255, 6)              => 0x00ff
160 ///   OS << format_hex(255, 2)              => 0xff
161 inline FormattedNumber format_hex(uint64_t N, unsigned Width,
162                                   bool Upper = false) {
163   assert(Width <= 18 && "hex width must be <= 18");
164   return FormattedNumber(N, 0, Width, true, Upper, true);
165 }
166
167 /// format_hex_no_prefix - Output \p N as a fixed width hexadecimal. Does not
168 /// prepend '0x' to the outputted string.  If number will not fit in width,
169 /// full number is still printed.  Examples:
170 ///   OS << format_hex_no_prefix(255, 4)              => ff
171 ///   OS << format_hex_no_prefix(255, 4, true)        => FF
172 ///   OS << format_hex_no_prefix(255, 6)              => 00ff
173 ///   OS << format_hex_no_prefix(255, 2)              => ff
174 inline FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width,
175                                             bool Upper = false) {
176   assert(Width <= 18 && "hex width must be <= 18");
177   return FormattedNumber(N, 0, Width, true, Upper, false);
178 }
179
180 /// format_decimal - Output \p N as a right justified, fixed-width decimal. If 
181 /// number will not fit in width, full number is still printed.  Examples:
182 ///   OS << format_decimal(0, 5)     => "    0"
183 ///   OS << format_decimal(255, 5)   => "  255"
184 ///   OS << format_decimal(-1, 3)    => " -1"
185 ///   OS << format_decimal(12345, 3) => "12345"
186 inline FormattedNumber format_decimal(int64_t N, unsigned Width) {
187   return FormattedNumber(0, N, Width, false, false, false);
188 }
189
190
191 } // end namespace llvm
192
193 #endif