diff options
author | Zachary Turner <zturner@google.com> | 2016-11-11 23:57:40 +0000 |
---|---|---|
committer | Zachary Turner <zturner@google.com> | 2016-11-11 23:57:40 +0000 |
commit | 11db2642fb54487ef18c18cc8555d7ff2f5e823b (patch) | |
tree | 0398b217a7902566a3b9309268b2290dd900fc5c /llvm/lib/Support | |
parent | da6b5721b52a9f8d74dceec91e19c230af271fb6 (diff) | |
download | bcm5719-llvm-11db2642fb54487ef18c18cc8555d7ff2f5e823b.tar.gz bcm5719-llvm-11db2642fb54487ef18c18cc8555d7ff2f5e823b.zip |
[Support] Introduce llvm::formatv() function.
This introduces a new type-safe general purpose formatting
library. It provides compile-time type safety, does not require
a format specifier (since the type is deduced), and provides
mechanisms for extending the format capability to user defined
types, and overriding the formatting behavior for existing types.
This patch additionally adds documentation for the API to the
LLVM programmer's manual.
Mailing List Thread:
http://lists.llvm.org/pipermail/llvm-dev/2016-October/105836.html
Differential Revision: https://reviews.llvm.org/D25587
llvm-svn: 286682
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r-- | llvm/lib/Support/CMakeLists.txt | 1 | ||||
-rw-r--r-- | llvm/lib/Support/FormatVariadic.cpp | 156 | ||||
-rw-r--r-- | llvm/lib/Support/NativeFormatting.cpp | 67 | ||||
-rw-r--r-- | llvm/lib/Support/raw_ostream.cpp | 17 |
4 files changed, 213 insertions, 28 deletions
diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt index 5b14c828db0..c31db7cd0e3 100644 --- a/llvm/lib/Support/CMakeLists.txt +++ b/llvm/lib/Support/CMakeLists.txt @@ -55,6 +55,7 @@ add_llvm_library(LLVMSupport FileOutputBuffer.cpp FoldingSet.cpp FormattedStream.cpp + FormatVariadic.cpp GraphWriter.cpp Hashing.cpp IntEqClasses.cpp diff --git a/llvm/lib/Support/FormatVariadic.cpp b/llvm/lib/Support/FormatVariadic.cpp new file mode 100644 index 00000000000..de61dae814b --- /dev/null +++ b/llvm/lib/Support/FormatVariadic.cpp @@ -0,0 +1,156 @@ +//===- FormatVariadic.cpp - Format string parsing and analysis ----*-C++-*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +//===----------------------------------------------------------------------===// + +#include "llvm/Support/FormatVariadic.h" + +using namespace llvm; + +static Optional<AlignStyle> translateLocChar(char C) { + switch (C) { + case '-': + return AlignStyle::Left; + case '=': + return AlignStyle::Center; + case '+': + return AlignStyle::Right; + default: + return None; + } + LLVM_BUILTIN_UNREACHABLE; +} + +bool formatv_object_base::consumeFieldLayout(StringRef &Spec, AlignStyle &Where, + size_t &Align, char &Pad) { + Where = AlignStyle::Right; + Align = 0; + Pad = ' '; + if (Spec.empty()) + return true; + + if (Spec.size() > 1) { + // A maximum of 2 characters at the beginning can be used for something + // other + // than the width. + // If Spec[1] is a loc char, then Spec[0] is a pad char and Spec[2:...] + // contains the width. + // Otherwise, if Spec[0] is a loc char, then Spec[1:...] contains the width. + // Otherwise, Spec[0:...] contains the width. + if (auto Loc = translateLocChar(Spec[1])) { + Pad = Spec[0]; + Where = *Loc; + Spec = Spec.drop_front(2); + } else if (auto Loc = translateLocChar(Spec[0])) { + Where = *Loc; + Spec = Spec.drop_front(1); + } + } + + bool Failed = Spec.consumeInteger(0, Align); + return !Failed; +} + +Optional<ReplacementItem> +formatv_object_base::parseReplacementItem(StringRef Spec) { + StringRef RepString = Spec.trim("{}"); + + // If the replacement sequence does not start with a non-negative integer, + // this is an error. + char Pad = ' '; + std::size_t Align = 0; + AlignStyle Where = AlignStyle::Right; + StringRef Options; + size_t Index = 0; + RepString = RepString.trim(); + if (RepString.consumeInteger(0, Index)) { + assert(false && "Invalid replacement sequence index!"); + return ReplacementItem{}; + } + RepString = RepString.trim(); + if (!RepString.empty() && RepString.front() == ',') { + RepString = RepString.drop_front(); + if (!consumeFieldLayout(RepString, Where, Align, Pad)) + assert(false && "Invalid replacement field layout specification!"); + } + RepString = RepString.trim(); + if (!RepString.empty() && RepString.front() == ':') { + Options = RepString.drop_front().trim(); + RepString = StringRef(); + } + RepString = RepString.trim(); + if (!RepString.empty()) { + assert(false && "Unexpected characters found in replacement string!"); + } + + return ReplacementItem{Spec, Index, Align, Where, Pad, Options}; +} + +std::pair<ReplacementItem, StringRef> +formatv_object_base::splitLiteralAndReplacement(StringRef Fmt) { + StringRef Rep; + StringRef Remainder; + std::size_t From = 0; + while (From < Fmt.size() && From != StringRef::npos) { + std::size_t BO = Fmt.find_first_of('{', From); + // Everything up until the first brace is a literal. + if (BO != 0) + return std::make_pair(ReplacementItem{Fmt.substr(0, BO)}, Fmt.substr(BO)); + + StringRef Braces = + Fmt.drop_front(BO).take_while([](char C) { return C == '{'; }); + // If there is more than one brace, then some of them are escaped. Treat + // these as replacements. + if (Braces.size() > 1) { + size_t NumEscapedBraces = Braces.size() / 2; + StringRef Middle = Fmt.substr(BO, NumEscapedBraces); + StringRef Right = Fmt.drop_front(BO + NumEscapedBraces * 2); + return std::make_pair(ReplacementItem{Middle}, Right); + } + // An unterminated open brace is undefined. We treat the rest of the string + // as a literal replacement, but we assert to indicate that this is + // undefined and that we consider it an error. + std::size_t BC = Fmt.find_first_of('}', BO); + if (BC == StringRef::npos) { + assert( + false && + "Unterminated brace sequence. Escape with {{ for a literal brace."); + return std::make_pair(ReplacementItem{Fmt}, StringRef()); + } + + // Even if there is a closing brace, if there is another open brace before + // this closing brace, treat this portion as literal, and try again with the + // next one. + std::size_t BO2 = Fmt.find_first_of('{', BO + 1); + if (BO2 < BC) + return std::make_pair(ReplacementItem{Fmt.substr(0, BO2)}, + Fmt.substr(BO2)); + + StringRef Spec = Fmt.slice(BO + 1, BC); + StringRef Right = Fmt.substr(BC + 1); + + auto RI = parseReplacementItem(Spec); + if (RI.hasValue()) + return std::make_pair(*RI, Right); + + // If there was an error parsing the replacement item, treat it as an + // invalid replacement spec, and just continue. + From = BC + 1; + } + return std::make_pair(ReplacementItem{Fmt}, StringRef()); +} + +std::vector<ReplacementItem> +formatv_object_base::parseFormatString(StringRef Fmt) { + std::vector<ReplacementItem> Replacements; + ReplacementItem I; + while (!Fmt.empty()) { + std::tie(I, Fmt) = splitLiteralAndReplacement(Fmt); + if (I.Type != ReplacementType::Empty) + Replacements.push_back(I); + } + return Replacements; +} diff --git a/llvm/lib/Support/NativeFormatting.cpp b/llvm/lib/Support/NativeFormatting.cpp index dbfe658e87c..bb868914109 100644 --- a/llvm/lib/Support/NativeFormatting.cpp +++ b/llvm/lib/Support/NativeFormatting.cpp @@ -47,8 +47,8 @@ static void writeWithCommas(raw_ostream &S, ArrayRef<char> Buffer) { } template <typename T> -static void write_unsigned_impl(raw_ostream &S, T N, IntegerStyle Style, - bool IsNegative) { +static void write_unsigned_impl(raw_ostream &S, T N, size_t MinDigits, + IntegerStyle Style, bool IsNegative) { static_assert(std::is_unsigned<T>::value, "Value is not unsigned!"); char NumberBuffer[128]; @@ -59,6 +59,12 @@ static void write_unsigned_impl(raw_ostream &S, T N, IntegerStyle Style, if (IsNegative) S << '-'; + + if (Len < MinDigits && Style != IntegerStyle::Number) { + for (size_t I = Len; I < MinDigits; ++I) + S << '0'; + } + if (Style == IntegerStyle::Number) { writeWithCommas(S, ArrayRef<char>(std::end(NumberBuffer) - Len, Len)); } else { @@ -67,53 +73,60 @@ static void write_unsigned_impl(raw_ostream &S, T N, IntegerStyle Style, } template <typename T> -static void write_unsigned(raw_ostream &S, T N, IntegerStyle Style, - bool IsNegative = false) { +static void write_unsigned(raw_ostream &S, T N, size_t MinDigits, + IntegerStyle Style, bool IsNegative = false) { // Output using 32-bit div/mod if possible. if (N == static_cast<uint32_t>(N)) - write_unsigned_impl(S, static_cast<uint32_t>(N), Style, IsNegative); + write_unsigned_impl(S, static_cast<uint32_t>(N), MinDigits, Style, + IsNegative); else - write_unsigned_impl(S, N, Style, IsNegative); + write_unsigned_impl(S, N, MinDigits, Style, IsNegative); } template <typename T> -static void write_signed(raw_ostream &S, T N, IntegerStyle Style) { +static void write_signed(raw_ostream &S, T N, size_t MinDigits, + IntegerStyle Style) { static_assert(std::is_signed<T>::value, "Value is not signed!"); using UnsignedT = typename std::make_unsigned<T>::type; if (N >= 0) { - write_unsigned(S, static_cast<UnsignedT>(N), Style); + write_unsigned(S, static_cast<UnsignedT>(N), MinDigits, Style); return; } UnsignedT UN = -(UnsignedT)N; - write_unsigned(S, UN, Style, true); + write_unsigned(S, UN, MinDigits, Style, true); } -void llvm::write_integer(raw_ostream &S, unsigned int N, IntegerStyle Style) { - write_unsigned(S, N, Style); +void llvm::write_integer(raw_ostream &S, unsigned int N, size_t MinDigits, + IntegerStyle Style) { + write_unsigned(S, N, MinDigits, Style); } -void llvm::write_integer(raw_ostream &S, int N, IntegerStyle Style) { - write_signed(S, N, Style); +void llvm::write_integer(raw_ostream &S, int N, size_t MinDigits, + IntegerStyle Style) { + write_signed(S, N, MinDigits, Style); } -void llvm::write_integer(raw_ostream &S, unsigned long N, IntegerStyle Style) { - write_unsigned(S, N, Style); +void llvm::write_integer(raw_ostream &S, unsigned long N, size_t MinDigits, + IntegerStyle Style) { + write_unsigned(S, N, MinDigits, Style); } -void llvm::write_integer(raw_ostream &S, long N, IntegerStyle Style) { - write_signed(S, N, Style); +void llvm::write_integer(raw_ostream &S, long N, size_t MinDigits, + IntegerStyle Style) { + write_signed(S, N, MinDigits, Style); } -void llvm::write_integer(raw_ostream &S, unsigned long long N, +void llvm::write_integer(raw_ostream &S, unsigned long long N, size_t MinDigits, IntegerStyle Style) { - write_unsigned(S, N, Style); + write_unsigned(S, N, MinDigits, Style); } -void llvm::write_integer(raw_ostream &S, long long N, IntegerStyle Style) { - write_signed(S, N, Style); +void llvm::write_integer(raw_ostream &S, long long N, size_t MinDigits, + IntegerStyle Style) { + write_signed(S, N, MinDigits, Style); } void llvm::write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style, @@ -178,7 +191,9 @@ void llvm::write_double(raw_ostream &S, double N, FloatStyle Style, #if defined(__MINGW32__) // FIXME: It should be generic to C++11. if (N == 0.0 && std::signbit(N)) { - const char *NegativeZero = "-0.000000e+00"; + char NegativeZero[] = "-0.000000e+00"; + if (Style == FloatStyle::ExponentUpper) + NegativeZero[strlen(NegativeZero) - 4] = 'E'; S << NegativeZero; return; } @@ -187,7 +202,9 @@ void llvm::write_double(raw_ostream &S, double N, FloatStyle Style, // negative zero if (fpcl == _FPCLASS_NZ) { - const char *NegativeZero = "-0.000000e+00"; + char NegativeZero[] = "-0.000000e+00"; + if (Style == FloatStyle::ExponentUpper) + NegativeZero[strlen(NegativeZero) - 4] = 'E'; S << NegativeZero; return; } @@ -231,6 +248,10 @@ void llvm::write_double(raw_ostream &S, double N, FloatStyle Style, S << '%'; } +bool llvm::isPrefixedHexStyle(HexPrintStyle S) { + return (S == HexPrintStyle::PrefixLower || S == HexPrintStyle::PrefixUpper); +} + size_t llvm::getDefaultPrecision(FloatStyle Style) { switch (Style) { case FloatStyle::Exponent: diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp index 218d4986fff..92c7967cd8f 100644 --- a/llvm/lib/Support/raw_ostream.cpp +++ b/llvm/lib/Support/raw_ostream.cpp @@ -20,6 +20,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Format.h" +#include "llvm/Support/FormatVariadic.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/NativeFormatting.h" #include "llvm/Support/Process.h" @@ -114,22 +115,22 @@ void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size, } raw_ostream &raw_ostream::operator<<(unsigned long N) { - write_integer(*this, static_cast<uint64_t>(N), IntegerStyle::Integer); + write_integer(*this, static_cast<uint64_t>(N), 0, IntegerStyle::Integer); return *this; } raw_ostream &raw_ostream::operator<<(long N) { - write_integer(*this, static_cast<int64_t>(N), IntegerStyle::Integer); + write_integer(*this, static_cast<int64_t>(N), 0, IntegerStyle::Integer); return *this; } raw_ostream &raw_ostream::operator<<(unsigned long long N) { - write_integer(*this, static_cast<uint64_t>(N), IntegerStyle::Integer); + write_integer(*this, static_cast<uint64_t>(N), 0, IntegerStyle::Integer); return *this; } raw_ostream &raw_ostream::operator<<(long long N) { - write_integer(*this, static_cast<int64_t>(N), IntegerStyle::Integer); + write_integer(*this, static_cast<int64_t>(N), 0, IntegerStyle::Integer); return *this; } @@ -318,6 +319,12 @@ raw_ostream &raw_ostream::operator<<(const format_object_base &Fmt) { } } +raw_ostream &raw_ostream::operator<<(const formatv_object_base &Obj) { + SmallString<128> S; + Obj.format(*this); + return *this; +} + raw_ostream &raw_ostream::operator<<(const FormattedString &FS) { unsigned Len = FS.Str.size(); int PadAmount = FS.Width - Len; @@ -344,7 +351,7 @@ raw_ostream &raw_ostream::operator<<(const FormattedNumber &FN) { } else { llvm::SmallString<16> Buffer; llvm::raw_svector_ostream Stream(Buffer); - llvm::write_integer(Stream, FN.DecValue, IntegerStyle::Integer); + llvm::write_integer(Stream, FN.DecValue, 0, IntegerStyle::Integer); if (Buffer.size() < FN.Width) indent(FN.Width - Buffer.size()); (*this) << Buffer; |