LLVM API Documentation

Format.h
Go to the documentation of this file.
00001 //===- Format.h - Efficient printf-style formatting for streams -*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the format() function, which can be used with other
00011 // LLVM subsystems to provide printf-style formatting.  This gives all the power
00012 // and risk of printf.  This can be used like this (with raw_ostreams as an
00013 // example):
00014 //
00015 //    OS << "mynumber: " << format("%4.5f", 1234.412) << '\n';
00016 //
00017 // Or if you prefer:
00018 //
00019 //  OS << format("mynumber: %4.5f\n", 1234.412);
00020 //
00021 //===----------------------------------------------------------------------===//
00022 
00023 #ifndef LLVM_SUPPORT_FORMAT_H
00024 #define LLVM_SUPPORT_FORMAT_H
00025 
00026 #include "llvm/ADT/StringRef.h"
00027 #include "llvm/Support/DataTypes.h"
00028 #include <cassert>
00029 #include <cstdio>
00030 #ifdef _MSC_VER
00031 // FIXME: This define is wrong:
00032 //  - _snprintf does not guarantee that trailing null is always added - if
00033 //    there is no space for null, it does not report any error.
00034 //  - According to C++ standard, snprintf should be visible in the 'std' 
00035 //    namespace - this define makes this impossible.
00036 #define snprintf _snprintf
00037 #endif
00038 
00039 namespace llvm {
00040 
00041 /// This is a helper class used for handling formatted output.  It is the
00042 /// abstract base class of a templated derived class.
00043 class format_object_base {
00044 protected:
00045   const char *Fmt;
00046   ~format_object_base() {} // Disallow polymorphic deletion.
00047   virtual void home(); // Out of line virtual method.
00048 
00049   /// Call snprintf() for this object, on the given buffer and size.
00050   virtual int snprint(char *Buffer, unsigned BufferSize) const = 0;
00051 
00052 public:
00053   format_object_base(const char *fmt) : Fmt(fmt) {}
00054 
00055   /// Format the object into the specified buffer.  On success, this returns
00056   /// the length of the formatted string.  If the buffer is too small, this
00057   /// returns a length to retry with, which will be larger than BufferSize.
00058   unsigned print(char *Buffer, unsigned BufferSize) const {
00059     assert(BufferSize && "Invalid buffer size!");
00060 
00061     // Print the string, leaving room for the terminating null.
00062     int N = snprint(Buffer, BufferSize);
00063 
00064     // VC++ and old GlibC return negative on overflow, just double the size.
00065     if (N < 0)
00066       return BufferSize * 2;
00067 
00068     // Other implementations yield number of bytes needed, not including the
00069     // final '\0'.
00070     if (unsigned(N) >= BufferSize)
00071       return N + 1;
00072 
00073     // Otherwise N is the length of output (not including the final '\0').
00074     return N;
00075   }
00076 };
00077 
00078 /// These are templated helper classes used by the format function that
00079 /// capture the object to be formated and the format string. When actually
00080 /// printed, this synthesizes the string into a temporary buffer provided and
00081 /// returns whether or not it is big enough.
00082 
00083 template <typename T>
00084 class format_object1 final : public format_object_base {
00085   T Val;
00086 public:
00087   format_object1(const char *fmt, const T &val)
00088     : format_object_base(fmt), Val(val) {
00089   }
00090 
00091   int snprint(char *Buffer, unsigned BufferSize) const override {
00092     return snprintf(Buffer, BufferSize, Fmt, Val);
00093   }
00094 };
00095 
00096 template <typename T1, typename T2>
00097 class format_object2 final : public format_object_base {
00098   T1 Val1;
00099   T2 Val2;
00100 public:
00101   format_object2(const char *fmt, const T1 &val1, const T2 &val2)
00102   : format_object_base(fmt), Val1(val1), Val2(val2) {
00103   }
00104 
00105   int snprint(char *Buffer, unsigned BufferSize) const override {
00106     return snprintf(Buffer, BufferSize, Fmt, Val1, Val2);
00107   }
00108 };
00109 
00110 template <typename T1, typename T2, typename T3>
00111 class format_object3 final : public format_object_base {
00112   T1 Val1;
00113   T2 Val2;
00114   T3 Val3;
00115 public:
00116   format_object3(const char *fmt, const T1 &val1, const T2 &val2,const T3 &val3)
00117     : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3) {
00118   }
00119 
00120   int snprint(char *Buffer, unsigned BufferSize) const override {
00121     return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3);
00122   }
00123 };
00124 
00125 template <typename T1, typename T2, typename T3, typename T4>
00126 class format_object4 final : public format_object_base {
00127   T1 Val1;
00128   T2 Val2;
00129   T3 Val3;
00130   T4 Val4;
00131 public:
00132   format_object4(const char *fmt, const T1 &val1, const T2 &val2,
00133                  const T3 &val3, const T4 &val4)
00134     : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3), Val4(val4) {
00135   }
00136 
00137   int snprint(char *Buffer, unsigned BufferSize) const override {
00138     return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4);
00139   }
00140 };
00141 
00142 template <typename T1, typename T2, typename T3, typename T4, typename T5>
00143 class format_object5 final : public format_object_base {
00144   T1 Val1;
00145   T2 Val2;
00146   T3 Val3;
00147   T4 Val4;
00148   T5 Val5;
00149 public:
00150   format_object5(const char *fmt, const T1 &val1, const T2 &val2,
00151                  const T3 &val3, const T4 &val4, const T5 &val5)
00152     : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3), Val4(val4),
00153       Val5(val5) {
00154   }
00155 
00156   int snprint(char *Buffer, unsigned BufferSize) const override {
00157     return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4, Val5);
00158   }
00159 };
00160 
00161 template <typename T1, typename T2, typename T3, typename T4, typename T5,
00162           typename T6>
00163 class format_object6 final : public format_object_base {
00164   T1 Val1;
00165   T2 Val2;
00166   T3 Val3;
00167   T4 Val4;
00168   T5 Val5;
00169   T6 Val6;
00170 public:
00171   format_object6(const char *Fmt, const T1 &Val1, const T2 &Val2,
00172                  const T3 &Val3, const T4 &Val4, const T5 &Val5, const T6 &Val6)
00173     : format_object_base(Fmt), Val1(Val1), Val2(Val2), Val3(Val3), Val4(Val4),
00174       Val5(Val5), Val6(Val6) { }
00175 
00176   int snprint(char *Buffer, unsigned BufferSize) const override {
00177     return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4, Val5, Val6);
00178   }
00179 };
00180 
00181 /// These are helper functions used to produce formatted output.  They use
00182 /// template type deduction to construct the appropriate instance of the
00183 /// format_object class to simplify their construction.
00184 ///
00185 /// This is typically used like:
00186 /// \code
00187 ///   OS << format("%0.4f", myfloat) << '\n';
00188 /// \endcode
00189 
00190 template <typename T>
00191 inline format_object1<T> format(const char *Fmt, const T &Val) {
00192   return format_object1<T>(Fmt, Val);
00193 }
00194 
00195 template <typename T1, typename T2>
00196 inline format_object2<T1, T2> format(const char *Fmt, const T1 &Val1,
00197                                      const T2 &Val2) {
00198   return format_object2<T1, T2>(Fmt, Val1, Val2);
00199 }
00200 
00201 template <typename T1, typename T2, typename T3>
00202   inline format_object3<T1, T2, T3> format(const char *Fmt, const T1 &Val1,
00203                                            const T2 &Val2, const T3 &Val3) {
00204   return format_object3<T1, T2, T3>(Fmt, Val1, Val2, Val3);
00205 }
00206 
00207 template <typename T1, typename T2, typename T3, typename T4>
00208 inline format_object4<T1, T2, T3, T4> format(const char *Fmt, const T1 &Val1,
00209                                              const T2 &Val2, const T3 &Val3,
00210                                              const T4 &Val4) {
00211   return format_object4<T1, T2, T3, T4>(Fmt, Val1, Val2, Val3, Val4);
00212 }
00213 
00214 template <typename T1, typename T2, typename T3, typename T4, typename T5>
00215 inline format_object5<T1, T2, T3, T4, T5> format(const char *Fmt,const T1 &Val1,
00216                                              const T2 &Val2, const T3 &Val3,
00217                                              const T4 &Val4, const T5 &Val5) {
00218   return format_object5<T1, T2, T3, T4, T5>(Fmt, Val1, Val2, Val3, Val4, Val5);
00219 }
00220 
00221 template <typename T1, typename T2, typename T3, typename T4, typename T5,
00222           typename T6>
00223 inline format_object6<T1, T2, T3, T4, T5, T6>
00224 format(const char *Fmt, const T1 &Val1, const T2 &Val2, const T3 &Val3,
00225        const T4 &Val4, const T5 &Val5, const T6 &Val6) {
00226   return format_object6<T1, T2, T3, T4, T5, T6>(Fmt, Val1, Val2, Val3, Val4,
00227                                                 Val5, Val6);
00228 }
00229 
00230 /// This is a helper class used for left_justify() and right_justify().
00231 class FormattedString {
00232   StringRef Str;
00233   unsigned Width;
00234   bool RightJustify;
00235   friend class raw_ostream;
00236 public:
00237     FormattedString(StringRef S, unsigned W, bool R)
00238       : Str(S), Width(W), RightJustify(R) { }
00239 };
00240 
00241 /// left_justify - append spaces after string so total output is
00242 /// \p Width characters.  If \p Str is larger that \p Width, full string
00243 /// is written with no padding.
00244 inline FormattedString left_justify(StringRef Str, unsigned Width) {
00245   return FormattedString(Str, Width, false);
00246 }
00247 
00248 /// right_justify - add spaces before string so total output is
00249 /// \p Width characters.  If \p Str is larger that \p Width, full string
00250 /// is written with no padding.
00251 inline FormattedString right_justify(StringRef Str, unsigned Width) {
00252   return FormattedString(Str, Width, true);
00253 }
00254 
00255 /// This is a helper class used for format_hex() and format_decimal().
00256 class FormattedNumber {
00257   uint64_t HexValue;
00258   int64_t DecValue;
00259   unsigned Width;
00260   bool Hex;
00261   bool Upper;
00262   bool HexPrefix;
00263   friend class raw_ostream;
00264 public:
00265   FormattedNumber(uint64_t HV, int64_t DV, unsigned W, bool H, bool U,
00266                   bool Prefix)
00267       : HexValue(HV), DecValue(DV), Width(W), Hex(H), Upper(U),
00268         HexPrefix(Prefix) {}
00269 };
00270 
00271 /// format_hex - Output \p N as a fixed width hexadecimal. If number will not
00272 /// fit in width, full number is still printed.  Examples:
00273 ///   OS << format_hex(255, 4)              => 0xff
00274 ///   OS << format_hex(255, 4, true)        => 0xFF
00275 ///   OS << format_hex(255, 6)              => 0x00ff
00276 ///   OS << format_hex(255, 2)              => 0xff
00277 inline FormattedNumber format_hex(uint64_t N, unsigned Width,
00278                                   bool Upper = false) {
00279   assert(Width <= 18 && "hex width must be <= 18");
00280   return FormattedNumber(N, 0, Width, true, Upper, true);
00281 }
00282 
00283 /// format_hex_no_prefix - Output \p N as a fixed width hexadecimal. Does not
00284 /// prepend '0x' to the outputted string.  If number will not fit in width,
00285 /// full number is still printed.  Examples:
00286 ///   OS << format_hex_no_prefix(255, 4)              => ff
00287 ///   OS << format_hex_no_prefix(255, 4, true)        => FF
00288 ///   OS << format_hex_no_prefix(255, 6)              => 00ff
00289 ///   OS << format_hex_no_prefix(255, 2)              => ff
00290 inline FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width,
00291                                             bool Upper = false) {
00292   assert(Width <= 18 && "hex width must be <= 18");
00293   return FormattedNumber(N, 0, Width, true, Upper, false);
00294 }
00295 
00296 /// format_decimal - Output \p N as a right justified, fixed-width decimal. If 
00297 /// number will not fit in width, full number is still printed.  Examples:
00298 ///   OS << format_decimal(0, 5)     => "    0"
00299 ///   OS << format_decimal(255, 5)   => "  255"
00300 ///   OS << format_decimal(-1, 3)    => " -1"
00301 ///   OS << format_decimal(12345, 3) => "12345"
00302 inline FormattedNumber format_decimal(int64_t N, unsigned Width) {
00303   return FormattedNumber(0, N, Width, false, false, false);
00304 }
00305 
00306 
00307 } // end namespace llvm
00308 
00309 #endif