8e163ddfd89d30425d3955d88f080728d2655bb2
[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/StringRef.h"
27 #include "llvm/Support/DataTypes.h"
28
29 #include <cassert>
30 #include <cstdio>
31 #ifdef _MSC_VER
32 // FIXME: This define is wrong:
33 //  - _snprintf does not guarantee that trailing null is always added - if
34 //    there is no space for null, it does not report any error.
35 //  - According to C++ standard, snprintf should be visible in the 'std' 
36 //    namespace - this define makes this impossible.
37 #define snprintf _snprintf
38 #endif
39
40 namespace llvm {
41
42 /// This is a helper class used for handling formatted output.  It is the
43 /// abstract base class of a templated derived class.
44 class format_object_base {
45 protected:
46   const char *Fmt;
47   ~format_object_base() {} // Disallow polymorphic deletion.
48   virtual void home(); // Out of line virtual method.
49
50   /// Call snprintf() for this object, on the given buffer and size.
51   virtual int snprint(char *Buffer, unsigned BufferSize) const = 0;
52
53 public:
54   format_object_base(const char *fmt) : Fmt(fmt) {}
55
56   /// Format the object into the specified buffer.  On success, this returns
57   /// the length of the formatted string.  If the buffer is too small, this
58   /// returns a length to retry with, which will be larger than BufferSize.
59   unsigned print(char *Buffer, unsigned BufferSize) const {
60     assert(BufferSize && "Invalid buffer size!");
61
62     // Print the string, leaving room for the terminating null.
63     int N = snprint(Buffer, BufferSize);
64
65     // VC++ and old GlibC return negative on overflow, just double the size.
66     if (N < 0)
67       return BufferSize * 2;
68
69     // Other implementations yield number of bytes needed, not including the
70     // final '\0'.
71     if (unsigned(N) >= BufferSize)
72       return N + 1;
73
74     // Otherwise N is the length of output (not including the final '\0').
75     return N;
76   }
77 };
78
79 /// These are templated helper classes used by the format function that
80 /// capture the object to be formated and the format string. When actually
81 /// printed, this synthesizes the string into a temporary buffer provided and
82 /// returns whether or not it is big enough.
83
84 template <typename T>
85 class format_object1 final : public format_object_base {
86   T Val;
87 public:
88   format_object1(const char *fmt, const T &val)
89     : format_object_base(fmt), Val(val) {
90   }
91
92   int snprint(char *Buffer, unsigned BufferSize) const override {
93     return snprintf(Buffer, BufferSize, Fmt, Val);
94   }
95 };
96
97 template <typename T1, typename T2>
98 class format_object2 final : public format_object_base {
99   T1 Val1;
100   T2 Val2;
101 public:
102   format_object2(const char *fmt, const T1 &val1, const T2 &val2)
103   : format_object_base(fmt), Val1(val1), Val2(val2) {
104   }
105
106   int snprint(char *Buffer, unsigned BufferSize) const override {
107     return snprintf(Buffer, BufferSize, Fmt, Val1, Val2);
108   }
109 };
110
111 template <typename T1, typename T2, typename T3>
112 class format_object3 final : public format_object_base {
113   T1 Val1;
114   T2 Val2;
115   T3 Val3;
116 public:
117   format_object3(const char *fmt, const T1 &val1, const T2 &val2,const T3 &val3)
118     : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3) {
119   }
120
121   int snprint(char *Buffer, unsigned BufferSize) const override {
122     return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3);
123   }
124 };
125
126 template <typename T1, typename T2, typename T3, typename T4>
127 class format_object4 final : public format_object_base {
128   T1 Val1;
129   T2 Val2;
130   T3 Val3;
131   T4 Val4;
132 public:
133   format_object4(const char *fmt, const T1 &val1, const T2 &val2,
134                  const T3 &val3, const T4 &val4)
135     : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3), Val4(val4) {
136   }
137
138   int snprint(char *Buffer, unsigned BufferSize) const override {
139     return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4);
140   }
141 };
142
143 template <typename T1, typename T2, typename T3, typename T4, typename T5>
144 class format_object5 final : public format_object_base {
145   T1 Val1;
146   T2 Val2;
147   T3 Val3;
148   T4 Val4;
149   T5 Val5;
150 public:
151   format_object5(const char *fmt, const T1 &val1, const T2 &val2,
152                  const T3 &val3, const T4 &val4, const T5 &val5)
153     : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3), Val4(val4),
154       Val5(val5) {
155   }
156
157   int snprint(char *Buffer, unsigned BufferSize) const override {
158     return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4, Val5);
159   }
160 };
161
162 template <typename T1, typename T2, typename T3, typename T4, typename T5,
163           typename T6>
164 class format_object6 final : public format_object_base {
165   T1 Val1;
166   T2 Val2;
167   T3 Val3;
168   T4 Val4;
169   T5 Val5;
170   T6 Val6;
171 public:
172   format_object6(const char *Fmt, const T1 &Val1, const T2 &Val2,
173                  const T3 &Val3, const T4 &Val4, const T5 &Val5, const T6 &Val6)
174     : format_object_base(Fmt), Val1(Val1), Val2(Val2), Val3(Val3), Val4(Val4),
175       Val5(Val5), Val6(Val6) { }
176
177   int snprint(char *Buffer, unsigned BufferSize) const override {
178     return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4, Val5, Val6);
179   }
180 };
181
182 /// These are helper functions used to produce formatted output.  They use
183 /// template type deduction to construct the appropriate instance of the
184 /// format_object class to simplify their construction.
185 ///
186 /// This is typically used like:
187 /// \code
188 ///   OS << format("%0.4f", myfloat) << '\n';
189 /// \endcode
190
191 template <typename T>
192 inline format_object1<T> format(const char *Fmt, const T &Val) {
193   return format_object1<T>(Fmt, Val);
194 }
195
196 template <typename T1, typename T2>
197 inline format_object2<T1, T2> format(const char *Fmt, const T1 &Val1,
198                                      const T2 &Val2) {
199   return format_object2<T1, T2>(Fmt, Val1, Val2);
200 }
201
202 template <typename T1, typename T2, typename T3>
203   inline format_object3<T1, T2, T3> format(const char *Fmt, const T1 &Val1,
204                                            const T2 &Val2, const T3 &Val3) {
205   return format_object3<T1, T2, T3>(Fmt, Val1, Val2, Val3);
206 }
207
208 template <typename T1, typename T2, typename T3, typename T4>
209 inline format_object4<T1, T2, T3, T4> format(const char *Fmt, const T1 &Val1,
210                                              const T2 &Val2, const T3 &Val3,
211                                              const T4 &Val4) {
212   return format_object4<T1, T2, T3, T4>(Fmt, Val1, Val2, Val3, Val4);
213 }
214
215 template <typename T1, typename T2, typename T3, typename T4, typename T5>
216 inline format_object5<T1, T2, T3, T4, T5> format(const char *Fmt,const T1 &Val1,
217                                              const T2 &Val2, const T3 &Val3,
218                                              const T4 &Val4, const T5 &Val5) {
219   return format_object5<T1, T2, T3, T4, T5>(Fmt, Val1, Val2, Val3, Val4, Val5);
220 }
221
222 template <typename T1, typename T2, typename T3, typename T4, typename T5,
223           typename T6>
224 inline format_object6<T1, T2, T3, T4, T5, T6>
225 format(const char *Fmt, const T1 &Val1, const T2 &Val2, const T3 &Val3,
226        const T4 &Val4, const T5 &Val5, const T6 &Val6) {
227   return format_object6<T1, T2, T3, T4, T5, T6>(Fmt, Val1, Val2, Val3, Val4,
228                                                 Val5, Val6);
229 }
230
231 /// This is a helper class used for left_justify() and right_justify().
232 class FormattedString {
233   StringRef Str;
234   unsigned Width;
235   bool RightJustify;
236   friend class raw_ostream;
237 public:
238     FormattedString(StringRef S, unsigned W, bool R)
239       : Str(S), Width(W), RightJustify(R) { }
240 };
241
242 /// left_justify - append spaces after string so total output is
243 /// \p Width characters.  If \p Str is larger that \p Width, full string
244 /// is written with no padding.
245 inline FormattedString left_justify(StringRef Str, unsigned Width) {
246   return FormattedString(Str, Width, false);
247 }
248
249 /// right_justify - add spaces before string so total output is
250 /// \p Width characters.  If \p Str is larger that \p Width, full string
251 /// is written with no padding.
252 inline FormattedString right_justify(StringRef Str, unsigned Width) {
253   return FormattedString(Str, Width, true);
254 }
255
256 /// This is a helper class used for format_hex() and format_decimal().
257 class FormattedNumber {
258   uint64_t HexValue;
259   int64_t DecValue;
260   unsigned Width;
261   bool Hex;
262   bool Upper;
263   friend class raw_ostream;
264 public:
265     FormattedNumber(uint64_t HV, int64_t DV, unsigned W, bool H, bool U)
266       : HexValue(HV), DecValue(DV), Width(W), Hex(H), Upper(U) { }
267 };
268
269 /// format_hex - Output \p N as a fixed width hexadecimal. If number will not
270 /// fit in width, full number is still printed.  Examples:
271 ///   OS << format_hex(255, 4)        => 0xff
272 ///   OS << format_hex(255, 4, true)  => 0xFF
273 ///   OS << format_hex(255, 6)        => 0x00ff
274 ///   OS << format_hex(255, 2)        => 0xff
275 inline FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false) {
276   assert(Width <= 18 && "hex width must be <= 18");
277   return FormattedNumber(N, 0, Width, true, Upper);
278 }
279
280 /// format_decimal - Output \p N as a right justified, fixed-width decimal. If 
281 /// number will not fit in width, full number is still printed.  Examples:
282 ///   OS << format_decimal(0, 5)     => "    0"
283 ///   OS << format_decimal(255, 5)   => "  255"
284 ///   OS << format_decimal(-1, 3)    => " -1"
285 ///   OS << format_decimal(12345, 3) => "12345"
286 inline FormattedNumber format_decimal(int64_t N, unsigned Width) {
287   return FormattedNumber(0, N, Width, false, false);
288 }
289
290
291 } // end namespace llvm
292
293 #endif