From: Benjamin Kramer Date: Sun, 15 Feb 2015 22:15:41 +0000 (+0000) Subject: Format: Modernize using variadic templates. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=aee01b35e4156316681a3f74042d00f701441068;p=oota-llvm.git Format: Modernize using variadic templates. Introduces a subset of C++14 integer sequences in STLExtras. This is just enough to support unpacking a std::tuple into the arguments of snprintf, we can add more of it when it's actually needed. Also removes an ancient macro hack that leaks a macro into the global namespace. Clean up users that made use of the convenient hack. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@229337 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/STLExtras.h b/include/llvm/ADT/STLExtras.h index 804bdae0c7c..265cffd4c19 100644 --- a/include/llvm/ADT/STLExtras.h +++ b/include/llvm/ADT/STLExtras.h @@ -194,6 +194,28 @@ struct less_second { } }; +// A subset of N3658. More stuff can be added as-needed. + +/// \brief Represents a compile-time sequence of integers. +template struct integer_sequence { + typedef T value_type; + + static LLVM_CONSTEXPR size_t size() { return sizeof...(I); } +}; + +template +struct build_index_impl : build_index_impl {}; +template +struct build_index_impl<0, I...> : integer_sequence {}; + +/// \brief Alias for the common case of a sequence of size_ts. +template +using index_sequence = integer_sequence; + +/// \brief Creates a compile-time integer sequence for a parameter pack. +template +using index_sequence_for = build_index_impl; + //===----------------------------------------------------------------------===// // Extra additions for arrays //===----------------------------------------------------------------------===// diff --git a/include/llvm/MC/MCInstPrinter.h b/include/llvm/MC/MCInstPrinter.h index cf4150b9b96..dce3a0690b3 100644 --- a/include/llvm/MC/MCInstPrinter.h +++ b/include/llvm/MC/MCInstPrinter.h @@ -95,14 +95,14 @@ public: void setPrintImmHex(HexStyle::Style Value) { PrintHexStyle = Value; } /// Utility function to print immediates in decimal or hex. - format_object1 formatImm(const int64_t Value) const { + format_object formatImm(int64_t Value) const { return PrintImmHex ? formatHex(Value) : formatDec(Value); } /// Utility functions to print decimal/hexadecimal values. - format_object1 formatDec(const int64_t Value) const; - format_object1 formatHex(const int64_t Value) const; - format_object1 formatHex(const uint64_t Value) const; + format_object formatDec(int64_t Value) const; + format_object formatHex(int64_t Value) const; + format_object formatHex(uint64_t Value) const; }; } // namespace llvm diff --git a/include/llvm/Support/Format.h b/include/llvm/Support/Format.h index 86904ad3256..21bb4f9f486 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 { @@ -80,101 +74,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 + int snprint_tuple(char *Buffer, unsigned BufferSize, + index_sequence) const { +#ifdef _MSC_VER + return _snprintf(Buffer, BufferSize, Fmt, std::get(Vals)...); +#else + return std::snprintf(Buffer, BufferSize, Fmt, std::get(Vals)...); +#endif } -}; -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) { - } + 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); - } -}; - -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) { } - - 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 +106,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(). diff --git a/lib/MC/MCInstPrinter.cpp b/lib/MC/MCInstPrinter.cpp index ba71245d018..0dc31215ce9 100644 --- a/lib/MC/MCInstPrinter.cpp +++ b/lib/MC/MCInstPrinter.cpp @@ -69,11 +69,11 @@ static bool needsLeadingZero(uint64_t Value) return false; } -format_object1 MCInstPrinter::formatDec(const int64_t Value) const { +format_object MCInstPrinter::formatDec(int64_t Value) const { return format("%" PRId64, Value); } -format_object1 MCInstPrinter::formatHex(const int64_t Value) const { +format_object MCInstPrinter::formatHex(int64_t Value) const { switch(PrintHexStyle) { case HexStyle::C: if (Value < 0) @@ -96,7 +96,7 @@ format_object1 MCInstPrinter::formatHex(const int64_t Value) const { llvm_unreachable("unsupported print style"); } -format_object1 MCInstPrinter::formatHex(const uint64_t Value) const { +format_object MCInstPrinter::formatHex(uint64_t Value) const { switch(PrintHexStyle) { case HexStyle::C: return format("0x%" PRIx64, Value); diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp index aa618b9d392..051e2dd252f 100644 --- a/lib/Support/raw_ostream.cpp +++ b/lib/Support/raw_ostream.cpp @@ -242,7 +242,7 @@ raw_ostream &raw_ostream::operator<<(double N) { char buf[16]; unsigned len; - len = snprintf(buf, sizeof(buf), "%e", N); + len = format("%e", N).snprint(buf, sizeof(buf)); if (len <= sizeof(buf) - 2) { if (len >= 5 && buf[len - 5] == 'e' && buf[len - 3] == '0') { int cs = buf[len - 4]; diff --git a/unittests/IR/UseTest.cpp b/unittests/IR/UseTest.cpp index b575a597840..d9d20af941d 100644 --- a/unittests/IR/UseTest.cpp +++ b/unittests/IR/UseTest.cpp @@ -50,7 +50,7 @@ TEST(UseTest, sort) { }); unsigned I = 0; for (User *U : X.users()) { - snprintf(vnbuf, sizeof(vnbuf), "v%u", I++); + format("v%u", I++).snprint(vnbuf, sizeof(vnbuf)); EXPECT_EQ(vnbuf, U->getName()); } ASSERT_EQ(8u, I); @@ -60,7 +60,7 @@ TEST(UseTest, sort) { }); I = 0; for (User *U : X.users()) { - snprintf(vnbuf, sizeof(vnbuf), "v%u", (7 - I++)); + format("v%u", (7 - I++)).snprint(vnbuf, sizeof(vnbuf)); EXPECT_EQ(vnbuf, U->getName()); } ASSERT_EQ(8u, I); @@ -95,7 +95,7 @@ TEST(UseTest, reverse) { }); unsigned I = 0; for (User *U : X.users()) { - snprintf(vnbuf, sizeof(vnbuf), "v%u", I++); + format("v%u", I++).snprint(vnbuf, sizeof(vnbuf)); EXPECT_EQ(vnbuf, U->getName()); } ASSERT_EQ(8u, I); @@ -103,7 +103,7 @@ TEST(UseTest, reverse) { X.reverseUseList(); I = 0; for (User *U : X.users()) { - snprintf(vnbuf, sizeof(vnbuf), "v%u", (7 - I++)); + format("v%u", (7 - I++)).snprint(vnbuf, sizeof(vnbuf)); EXPECT_EQ(vnbuf, U->getName()); } ASSERT_EQ(8u, I);