Index: include/llvm/Support/ScopedPrinter.h =================================================================== --- include/llvm/Support/ScopedPrinter.h +++ include/llvm/Support/ScopedPrinter.h @@ -11,12 +11,14 @@ #include "llvm/ADT/APSInt.h" #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/DataTypes.h" #include "llvm/Support/Endian.h" #include "llvm/Support/raw_ostream.h" #include +#include namespace llvm { @@ -58,13 +60,34 @@ raw_ostream &operator<<(raw_ostream &OS, const HexNumber &Value); const std::string to_hexString(uint64_t Value, bool UpperCase = true); -template const std::string to_string(const T &Value) { +template ::is_specialized>::type> +const std::string to_string(const T &Value) { std::string number; llvm::raw_string_ostream stream(number); stream << Value; return stream.str(); } +template ::is_specialized>::type, + void * = nullptr> +const std::string to_string(const T &Value) { + static constexpr auto NumDigits = std::numeric_limits::is_integer + ? std::numeric_limits::digits10 + : std::numeric_limits::max_digits10; + // Optional char for sign bit, plus the required number of base-10 digits. + // If integer - then +1 is for rounding up. Else, for the decimal separator. + static constexpr auto BufLen = + std::numeric_limits::is_signed + NumDigits + 1; + llvm::SmallString number; + llvm::raw_svector_ostream stream(number); + stream << Value; + assert(stream.str().size() <= BufLen && "small size prediction failed"); + return stream.str(); +} + class ScopedPrinter { public: ScopedPrinter(raw_ostream &OS) : OS(OS), IndentLevel(0) {}