X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=include%2Fllvm%2FSupport%2FFormat.h;h=f0b437a0cbed21a92cd76904b58a82b3e71353c9;hp=86904ad325686976ca8ff2825c4df850affbcef7;hb=35b01daadc4f50671fe7f5acbd975c7bdc7da0fb;hpb=019c097d1b3a826a504c84de6a5f48655be30266 diff --git a/include/llvm/Support/Format.h b/include/llvm/Support/Format.h index 86904ad3256..f0b437a0cbe 100644 --- a/include/llvm/Support/Format.h +++ b/include/llvm/Support/Format.h @@ -23,18 +23,12 @@ #ifndef LLVM_SUPPORT_FORMAT_H #define LLVM_SUPPORT_FORMAT_H +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/DataTypes.h" #include #include -#ifdef _MSC_VER -// FIXME: This define is wrong: -// - _snprintf does not guarantee that trailing null is always added - if -// there is no space for null, it does not report any error. -// - According to C++ standard, snprintf should be visible in the 'std' -// namespace - this define makes this impossible. -#define snprintf _snprintf -#endif +#include namespace llvm { @@ -43,7 +37,8 @@ namespace llvm { class format_object_base { protected: const char *Fmt; - ~format_object_base() {} // Disallow polymorphic deletion. + ~format_object_base() = default; // Disallow polymorphic deletion. + format_object_base(const format_object_base &) = default; virtual void home(); // Out of line virtual method. /// Call snprintf() for this object, on the given buffer and size. @@ -80,101 +75,26 @@ public: /// printed, this synthesizes the string into a temporary buffer provided and /// returns whether or not it is big enough. -template -class format_object1 final : public format_object_base { - T Val; -public: - format_object1(const char *fmt, const T &val) - : format_object_base(fmt), Val(val) { - } - - int snprint(char *Buffer, unsigned BufferSize) const override { - return snprintf(Buffer, BufferSize, Fmt, Val); - } -}; - -template -class format_object2 final : public format_object_base { - T1 Val1; - T2 Val2; -public: - format_object2(const char *fmt, const T1 &val1, const T2 &val2) - : format_object_base(fmt), Val1(val1), Val2(val2) { - } - - int snprint(char *Buffer, unsigned BufferSize) const override { - return snprintf(Buffer, BufferSize, Fmt, Val1, Val2); - } -}; - -template -class format_object3 final : public format_object_base { - T1 Val1; - T2 Val2; - T3 Val3; -public: - format_object3(const char *fmt, const T1 &val1, const T2 &val2,const T3 &val3) - : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3) { - } - - int snprint(char *Buffer, unsigned BufferSize) const override { - return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3); - } -}; - -template -class format_object4 final : public format_object_base { - T1 Val1; - T2 Val2; - T3 Val3; - T4 Val4; -public: - format_object4(const char *fmt, const T1 &val1, const T2 &val2, - const T3 &val3, const T4 &val4) - : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3), Val4(val4) { - } +template +class format_object final : public format_object_base { + std::tuple Vals; - int snprint(char *Buffer, unsigned BufferSize) const override { - return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4); - } -}; - -template -class format_object5 final : public format_object_base { - T1 Val1; - T2 Val2; - T3 Val3; - T4 Val4; - T5 Val5; -public: - format_object5(const char *fmt, const T1 &val1, const T2 &val2, - const T3 &val3, const T4 &val4, const T5 &val5) - : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3), Val4(val4), - Val5(val5) { - } - - int snprint(char *Buffer, unsigned BufferSize) const override { - return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4, Val5); + template + int snprint_tuple(char *Buffer, unsigned BufferSize, + index_sequence) const { +#ifdef _MSC_VER + return _snprintf(Buffer, BufferSize, Fmt, std::get(Vals)...); +#else + return snprintf(Buffer, BufferSize, Fmt, std::get(Vals)...); +#endif } -}; -template -class format_object6 final : public format_object_base { - T1 Val1; - T2 Val2; - T3 Val3; - T4 Val4; - T5 Val5; - T6 Val6; public: - format_object6(const char *Fmt, const T1 &Val1, const T2 &Val2, - const T3 &Val3, const T4 &Val4, const T5 &Val5, const T6 &Val6) - : format_object_base(Fmt), Val1(Val1), Val2(Val2), Val3(Val3), Val4(Val4), - Val5(Val5), Val6(Val6) { } + format_object(const char *fmt, const Ts &... vals) + : format_object_base(fmt), Vals(vals...) {} int snprint(char *Buffer, unsigned BufferSize) const override { - return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4, Val5, Val6); + return snprint_tuple(Buffer, BufferSize, index_sequence_for()); } }; @@ -187,44 +107,9 @@ public: /// OS << format("%0.4f", myfloat) << '\n'; /// \endcode -template -inline format_object1 format(const char *Fmt, const T &Val) { - return format_object1(Fmt, Val); -} - -template -inline format_object2 format(const char *Fmt, const T1 &Val1, - const T2 &Val2) { - return format_object2(Fmt, Val1, Val2); -} - -template - inline format_object3 format(const char *Fmt, const T1 &Val1, - const T2 &Val2, const T3 &Val3) { - return format_object3(Fmt, Val1, Val2, Val3); -} - -template -inline format_object4 format(const char *Fmt, const T1 &Val1, - const T2 &Val2, const T3 &Val3, - const T4 &Val4) { - return format_object4(Fmt, Val1, Val2, Val3, Val4); -} - -template -inline format_object5 format(const char *Fmt,const T1 &Val1, - const T2 &Val2, const T3 &Val3, - const T4 &Val4, const T5 &Val5) { - return format_object5(Fmt, Val1, Val2, Val3, Val4, Val5); -} - -template -inline format_object6 -format(const char *Fmt, const T1 &Val1, const T2 &Val2, const T3 &Val3, - const T4 &Val4, const T5 &Val5, const T6 &Val6) { - return format_object6(Fmt, Val1, Val2, Val3, Val4, - Val5, Val6); +template +inline format_object format(const char *Fmt, const Ts &... Vals) { + return format_object(Fmt, Vals...); } /// This is a helper class used for left_justify() and right_justify(). @@ -233,6 +118,7 @@ class FormattedString { unsigned Width; bool RightJustify; friend class raw_ostream; + public: FormattedString(StringRef S, unsigned W, bool R) : Str(S), Width(W), RightJustify(R) { } @@ -261,6 +147,7 @@ class FormattedNumber { bool Upper; bool HexPrefix; friend class raw_ostream; + public: FormattedNumber(uint64_t HV, int64_t DV, unsigned W, bool H, bool U, bool Prefix) @@ -293,7 +180,7 @@ inline FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width, return FormattedNumber(N, 0, Width, true, Upper, false); } -/// format_decimal - Output \p N as a right justified, fixed-width decimal. If +/// format_decimal - Output \p N as a right justified, fixed-width decimal. If /// number will not fit in width, full number is still printed. Examples: /// OS << format_decimal(0, 5) => " 0" /// OS << format_decimal(255, 5) => " 255" @@ -303,7 +190,6 @@ inline FormattedNumber format_decimal(int64_t N, unsigned Width) { return FormattedNumber(0, N, Width, false, false, false); } - } // end namespace llvm #endif