From 74d363d2f88a2166c2dc7c7c233db64b21c23431 Mon Sep 17 00:00:00 2001 From: Gregory Czajkowski Date: Sun, 17 Nov 2013 16:23:07 -0800 Subject: [PATCH] attempt to provide the original format string during errors causes undefined symbols during runtime --- format.cc | 39 ++++++++++++++++++++++++--------------- format.h | 34 +++++++++++++++++++--------------- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/format.cc b/format.cc index f58ef618..f6689e54 100644 --- a/format.cc +++ b/format.cc @@ -110,14 +110,15 @@ const char fmt::internal::DIGITS[] = "6061626364656667686970717273747576777879" "8081828384858687888990919293949596979899"; -void fmt::internal::ReportUnknownType(char code, const char *type) { +template +void fmt::internal::ReportUnknownType(const Char *format, char code, const char *type) { if (std::isprint(static_cast(code))) { throw fmt::FormatError(fmt::str( - fmt::Format("unknown format code '{}' for {}") << code << type)); + fmt::Format("unknown format code '{}' for {} while parsing {}") << code << type << format)); } throw fmt::FormatError( - fmt::str(fmt::Format("unknown format code '\\x{:02x}' for {}") - << static_cast(code) << type)); + fmt::str(fmt::Format("unknown format code '\\x{:02x}' for {} while parsing {}") + << static_cast(code) << type << format)); } @@ -200,7 +201,7 @@ typename fmt::BasicWriter::CharPtr template template void fmt::BasicWriter::FormatDouble( - T value, const FormatSpec &spec, int precision) { + T value, const FormatSpec &spec, int precision) { // Check type. char type = spec.type(); bool upper = false; @@ -220,7 +221,7 @@ void fmt::BasicWriter::FormatDouble( upper = true; break; default: - internal::ReportUnknownType(type, "double"); + internal::ReportUnknownType(spec.format(), type, "double"); break; } @@ -430,7 +431,7 @@ void fmt::BasicFormatter::DoFormat() { const Arg &arg = ParseArgIndex(s); - FormatSpec spec; + FormatSpec spec(format_); int precision = -1; if (*s == ':') { ++s; @@ -587,7 +588,7 @@ void fmt::BasicFormatter::DoFormat() { break; case CHAR: { if (spec.type_ && spec.type_ != 'c') - internal::ReportUnknownType(spec.type_, "char"); + internal::ReportUnknownType(spec.format_, spec.type_, "char"); typedef typename BasicWriter::CharPtr CharPtr; CharPtr out = CharPtr(); if (spec.width_ > 1) { @@ -609,7 +610,7 @@ void fmt::BasicFormatter::DoFormat() { } case STRING: { if (spec.type_ && spec.type_ != 's') - internal::ReportUnknownType(spec.type_, "string"); + internal::ReportUnknownType(spec.format_, spec.type_, "string"); const Char *str = arg.string.value; std::size_t size = arg.string.size; if (size == 0) { @@ -623,14 +624,14 @@ void fmt::BasicFormatter::DoFormat() { } case POINTER: if (spec.type_ && spec.type_ != 'p') - internal::ReportUnknownType(spec.type_, "pointer"); + internal::ReportUnknownType(spec.format_, spec.type_, "pointer"); spec.flags_= HASH_FLAG; spec.type_ = 'x'; writer.FormatInt(reinterpret_cast(arg.pointer_value), spec); break; case CUSTOM: if (spec.type_) - internal::ReportUnknownType(spec.type_, "object"); + internal::ReportUnknownType(spec.format_, spec.type_, "object"); arg.custom.format(writer, arg.custom.value, spec); break; default: @@ -643,11 +644,14 @@ void fmt::BasicFormatter::DoFormat() { // Explicit instantiations for char. +template void fmt::internal::ReportUnknownType( + const char *format, char code, const char *type); + template void fmt::BasicWriter::FormatDouble( - double value, const FormatSpec &spec, int precision); + double value, const FormatSpec &spec, int precision); template void fmt::BasicWriter::FormatDouble( - long double value, const FormatSpec &spec, int precision); + long double value, const FormatSpec &spec, int precision); template fmt::BasicWriter::CharPtr fmt::BasicWriter::FillPadding(CharPtr buffer, @@ -675,11 +679,14 @@ template void fmt::BasicFormatter::DoFormat(); // Explicit instantiations for wchar_t. +template void fmt::internal::ReportUnknownType( + const wchar_t *format, char code, const char *type); + template void fmt::BasicWriter::FormatDouble( - double value, const FormatSpec &spec, int precision); + double value, const FormatSpec &spec, int precision); template void fmt::BasicWriter::FormatDouble( - long double value, const FormatSpec &spec, int precision); + long double value, const FormatSpec &spec, int precision); template fmt::BasicWriter::CharPtr fmt::BasicWriter::FillPadding(CharPtr buffer, @@ -705,3 +712,5 @@ template void fmt::BasicFormatter::CheckSign( const wchar_t *&s, const Arg &arg); template void fmt::BasicFormatter::DoFormat(); + +//template fmt::BasicFormatter::Arg::Arg(wchar_t const*); diff --git a/format.h b/format.h index da010302..507e6dfe 100644 --- a/format.h +++ b/format.h @@ -201,7 +201,8 @@ struct IsLongDouble { enum {VALUE = 0}; }; template <> struct IsLongDouble { enum {VALUE = 1}; }; -void ReportUnknownType(char code, const char *type); +template +void ReportUnknownType(const Char *format, char code, const char *type); // Returns the number of decimal digits in n. Leading zeros are not counted // except for n == 0 in which case CountDigits returns 1. @@ -343,12 +344,14 @@ struct AlignTypeSpec : AlignSpec { char type() const { return TYPE; } }; +template struct FormatSpec : AlignSpec { unsigned flags_; char type_; + const Char *format_; - FormatSpec(unsigned width = 0, char type = 0, wchar_t fill = ' ') - : AlignSpec(width, fill), flags_(0), type_(type) {} + FormatSpec(const Char *format, unsigned width = 0, char type = 0, wchar_t fill = ' ') + : AlignSpec(width, fill), flags_(0), type_(type), format_(format) {} Alignment align() const { return align_; } @@ -357,6 +360,7 @@ struct FormatSpec : AlignSpec { bool hash_flag() const { return (flags_ & HASH_FLAG) != 0; } char type() const { return type_; } + const Char* format() const { return format_; } }; template @@ -528,17 +532,17 @@ class BasicWriter { // Formats an integer. template - void FormatInt(T value, const FormatSpec &spec) { - *this << IntFormatter(value, spec); + void FormatInt(T value, const FormatSpec &spec) { + *this << IntFormatter >(value, spec); } // Formats a floating-point number (double or long double). template - void FormatDouble(T value, const FormatSpec &spec, int precision); + void FormatDouble(T value, const FormatSpec &spec, int precision); template CharPtr FormatString(const StringChar *s, - std::size_t size, const FormatSpec &spec); + std::size_t size, const FormatSpec &spec); // This method is private to disallow writing a wide string to a // char stream and vice versa. If you want to print a wide string @@ -628,7 +632,7 @@ class BasicWriter { } BasicWriter &operator<<(double value) { - FormatDouble(value, FormatSpec(), -1); + FormatDouble(value, FormatSpec(""), -1); return *this; } @@ -637,7 +641,7 @@ class BasicWriter { (``'g'``) and writes it to the stream. */ BasicWriter &operator<<(long double value) { - FormatDouble(value, FormatSpec(), -1); + FormatDouble(value, FormatSpec(""), -1); return *this; } @@ -659,7 +663,7 @@ class BasicWriter { template BasicWriter &operator<<(const IntFormatter &f); - void Write(const std::basic_string &s, const FormatSpec &spec) { + void Write(const std::basic_string &s, const FormatSpec &spec) { FormatString(s.data(), s.size(), spec); } @@ -671,7 +675,7 @@ class BasicWriter { template template typename BasicWriter::CharPtr BasicWriter::FormatString( - const StringChar *s, std::size_t size, const FormatSpec &spec) { + const StringChar *s, std::size_t size, const FormatSpec &spec) { CharPtr out = CharPtr(); if (spec.width() > size) { out = GrowBuffer(spec.width()); @@ -772,7 +776,7 @@ BasicWriter &BasicWriter::operator<<( break; } default: - internal::ReportUnknownType(f.type(), "integer"); + internal::ReportUnknownType(f.format(), f.type(), "integer"); break; } return *this; @@ -788,7 +792,7 @@ typedef BasicWriter WWriter; // The default formatting function. template -void Format(BasicWriter &w, const FormatSpec &spec, const T &value) { +void Format(BasicWriter &w, const FormatSpec &spec, const T &value) { std::basic_ostringstream os; os << value; w.Write(os.str(), spec); @@ -798,7 +802,7 @@ namespace internal { // Formats an argument of a custom type, such as a user-defined class. template void FormatCustomArg( - BasicWriter &w, const void *arg, const FormatSpec &spec) { + BasicWriter &w, const void *arg, const FormatSpec &spec) { Format(w, spec, *static_cast(arg)); } } @@ -825,7 +829,7 @@ class BasicFormatter { }; typedef void (*FormatFunc)( - BasicWriter &w, const void *arg, const FormatSpec &spec); + BasicWriter &w, const void *arg, const FormatSpec &spec); // A format argument. class Arg {