mirror of
https://github.com/RPCS3/llvm.git
synced 2024-11-29 06:30:30 +00:00
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
This commit is contained in:
parent
d898d31ebc
commit
aee01b35e4
@ -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 <class T, T... I> struct integer_sequence {
|
||||
typedef T value_type;
|
||||
|
||||
static LLVM_CONSTEXPR size_t size() { return sizeof...(I); }
|
||||
};
|
||||
|
||||
template <std::size_t N, std::size_t... I>
|
||||
struct build_index_impl : build_index_impl<N - 1, N - 1, I...> {};
|
||||
template <std::size_t... I>
|
||||
struct build_index_impl<0, I...> : integer_sequence<std::size_t, I...> {};
|
||||
|
||||
/// \brief Alias for the common case of a sequence of size_ts.
|
||||
template <size_t... I>
|
||||
using index_sequence = integer_sequence<std::size_t, I...>;
|
||||
|
||||
/// \brief Creates a compile-time integer sequence for a parameter pack.
|
||||
template <class... Ts>
|
||||
using index_sequence_for = build_index_impl<sizeof...(Ts)>;
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Extra additions for arrays
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
@ -95,14 +95,14 @@ public:
|
||||
void setPrintImmHex(HexStyle::Style Value) { PrintHexStyle = Value; }
|
||||
|
||||
/// Utility function to print immediates in decimal or hex.
|
||||
format_object1<int64_t> formatImm(const int64_t Value) const {
|
||||
format_object<int64_t> formatImm(int64_t Value) const {
|
||||
return PrintImmHex ? formatHex(Value) : formatDec(Value);
|
||||
}
|
||||
|
||||
/// Utility functions to print decimal/hexadecimal values.
|
||||
format_object1<int64_t> formatDec(const int64_t Value) const;
|
||||
format_object1<int64_t> formatHex(const int64_t Value) const;
|
||||
format_object1<uint64_t> formatHex(const uint64_t Value) const;
|
||||
format_object<int64_t> formatDec(int64_t Value) const;
|
||||
format_object<int64_t> formatHex(int64_t Value) const;
|
||||
format_object<uint64_t> formatHex(uint64_t Value) const;
|
||||
};
|
||||
|
||||
} // namespace llvm
|
||||
|
@ -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 <cassert>
|
||||
#include <cstdio>
|
||||
#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 <tuple>
|
||||
|
||||
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 <typename T>
|
||||
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) {
|
||||
template <typename... Ts>
|
||||
class format_object final : public format_object_base {
|
||||
std::tuple<Ts...> Vals;
|
||||
|
||||
template <std::size_t... Is>
|
||||
int snprint_tuple(char *Buffer, unsigned BufferSize,
|
||||
index_sequence<Is...>) const {
|
||||
#ifdef _MSC_VER
|
||||
return _snprintf(Buffer, BufferSize, Fmt, std::get<Is>(Vals)...);
|
||||
#else
|
||||
return std::snprintf(Buffer, BufferSize, Fmt, std::get<Is>(Vals)...);
|
||||
#endif
|
||||
}
|
||||
|
||||
public:
|
||||
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, Val);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T1, typename T2>
|
||||
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 <typename T1, typename T2, typename T3>
|
||||
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 <typename T1, typename T2, typename T3, typename T4>
|
||||
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) {
|
||||
}
|
||||
|
||||
int snprint(char *Buffer, unsigned BufferSize) const override {
|
||||
return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T1, typename T2, typename T3, typename T4, typename T5>
|
||||
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 <typename T1, typename T2, typename T3, typename T4, typename T5,
|
||||
typename T6>
|
||||
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<Ts...>());
|
||||
}
|
||||
};
|
||||
|
||||
@ -187,44 +106,9 @@ public:
|
||||
/// OS << format("%0.4f", myfloat) << '\n';
|
||||
/// \endcode
|
||||
|
||||
template <typename T>
|
||||
inline format_object1<T> format(const char *Fmt, const T &Val) {
|
||||
return format_object1<T>(Fmt, Val);
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
inline format_object2<T1, T2> format(const char *Fmt, const T1 &Val1,
|
||||
const T2 &Val2) {
|
||||
return format_object2<T1, T2>(Fmt, Val1, Val2);
|
||||
}
|
||||
|
||||
template <typename T1, typename T2, typename T3>
|
||||
inline format_object3<T1, T2, T3> format(const char *Fmt, const T1 &Val1,
|
||||
const T2 &Val2, const T3 &Val3) {
|
||||
return format_object3<T1, T2, T3>(Fmt, Val1, Val2, Val3);
|
||||
}
|
||||
|
||||
template <typename T1, typename T2, typename T3, typename T4>
|
||||
inline format_object4<T1, T2, T3, T4> format(const char *Fmt, const T1 &Val1,
|
||||
const T2 &Val2, const T3 &Val3,
|
||||
const T4 &Val4) {
|
||||
return format_object4<T1, T2, T3, T4>(Fmt, Val1, Val2, Val3, Val4);
|
||||
}
|
||||
|
||||
template <typename T1, typename T2, typename T3, typename T4, typename T5>
|
||||
inline format_object5<T1, T2, T3, T4, T5> format(const char *Fmt,const T1 &Val1,
|
||||
const T2 &Val2, const T3 &Val3,
|
||||
const T4 &Val4, const T5 &Val5) {
|
||||
return format_object5<T1, T2, T3, T4, T5>(Fmt, Val1, Val2, Val3, Val4, Val5);
|
||||
}
|
||||
|
||||
template <typename T1, typename T2, typename T3, typename T4, typename T5,
|
||||
typename T6>
|
||||
inline format_object6<T1, T2, T3, T4, T5, T6>
|
||||
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<T1, T2, T3, T4, T5, T6>(Fmt, Val1, Val2, Val3, Val4,
|
||||
Val5, Val6);
|
||||
template <typename... Ts>
|
||||
inline format_object<Ts...> format(const char *Fmt, const Ts &... Vals) {
|
||||
return format_object<Ts...>(Fmt, Vals...);
|
||||
}
|
||||
|
||||
/// This is a helper class used for left_justify() and right_justify().
|
||||
|
@ -69,11 +69,11 @@ static bool needsLeadingZero(uint64_t Value)
|
||||
return false;
|
||||
}
|
||||
|
||||
format_object1<int64_t> MCInstPrinter::formatDec(const int64_t Value) const {
|
||||
format_object<int64_t> MCInstPrinter::formatDec(int64_t Value) const {
|
||||
return format("%" PRId64, Value);
|
||||
}
|
||||
|
||||
format_object1<int64_t> MCInstPrinter::formatHex(const int64_t Value) const {
|
||||
format_object<int64_t> MCInstPrinter::formatHex(int64_t Value) const {
|
||||
switch(PrintHexStyle) {
|
||||
case HexStyle::C:
|
||||
if (Value < 0)
|
||||
@ -96,7 +96,7 @@ format_object1<int64_t> MCInstPrinter::formatHex(const int64_t Value) const {
|
||||
llvm_unreachable("unsupported print style");
|
||||
}
|
||||
|
||||
format_object1<uint64_t> MCInstPrinter::formatHex(const uint64_t Value) const {
|
||||
format_object<uint64_t> MCInstPrinter::formatHex(uint64_t Value) const {
|
||||
switch(PrintHexStyle) {
|
||||
case HexStyle::C:
|
||||
return format("0x%" PRIx64, Value);
|
||||
|
@ -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];
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user