attempt to provide the original format string during errors

causes undefined symbols during runtime
This commit is contained in:
Gregory Czajkowski 2013-11-17 16:23:07 -08:00
parent 7b22272fbf
commit 74d363d2f8
2 changed files with 43 additions and 30 deletions

View File

@ -110,14 +110,15 @@ const char fmt::internal::DIGITS[] =
"6061626364656667686970717273747576777879" "6061626364656667686970717273747576777879"
"8081828384858687888990919293949596979899"; "8081828384858687888990919293949596979899";
void fmt::internal::ReportUnknownType(char code, const char *type) { template <typename Char>
void fmt::internal::ReportUnknownType(const Char *format, char code, const char *type) {
if (std::isprint(static_cast<unsigned char>(code))) { if (std::isprint(static_cast<unsigned char>(code))) {
throw fmt::FormatError(fmt::str( 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( throw fmt::FormatError(
fmt::str(fmt::Format("unknown format code '\\x{:02x}' for {}") fmt::str(fmt::Format("unknown format code '\\x{:02x}' for {} while parsing {}")
<< static_cast<unsigned>(code) << type)); << static_cast<unsigned>(code) << type << format));
} }
@ -200,7 +201,7 @@ typename fmt::BasicWriter<Char>::CharPtr
template <typename Char> template <typename Char>
template <typename T> template <typename T>
void fmt::BasicWriter<Char>::FormatDouble( void fmt::BasicWriter<Char>::FormatDouble(
T value, const FormatSpec &spec, int precision) { T value, const FormatSpec<Char> &spec, int precision) {
// Check type. // Check type.
char type = spec.type(); char type = spec.type();
bool upper = false; bool upper = false;
@ -220,7 +221,7 @@ void fmt::BasicWriter<Char>::FormatDouble(
upper = true; upper = true;
break; break;
default: default:
internal::ReportUnknownType(type, "double"); internal::ReportUnknownType<Char>(spec.format(), type, "double");
break; break;
} }
@ -430,7 +431,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
const Arg &arg = ParseArgIndex(s); const Arg &arg = ParseArgIndex(s);
FormatSpec spec; FormatSpec<Char> spec(format_);
int precision = -1; int precision = -1;
if (*s == ':') { if (*s == ':') {
++s; ++s;
@ -587,7 +588,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
break; break;
case CHAR: { case CHAR: {
if (spec.type_ && spec.type_ != 'c') if (spec.type_ && spec.type_ != 'c')
internal::ReportUnknownType(spec.type_, "char"); internal::ReportUnknownType<Char>(spec.format_, spec.type_, "char");
typedef typename BasicWriter<Char>::CharPtr CharPtr; typedef typename BasicWriter<Char>::CharPtr CharPtr;
CharPtr out = CharPtr(); CharPtr out = CharPtr();
if (spec.width_ > 1) { if (spec.width_ > 1) {
@ -609,7 +610,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
} }
case STRING: { case STRING: {
if (spec.type_ && spec.type_ != 's') if (spec.type_ && spec.type_ != 's')
internal::ReportUnknownType(spec.type_, "string"); internal::ReportUnknownType<Char>(spec.format_, spec.type_, "string");
const Char *str = arg.string.value; const Char *str = arg.string.value;
std::size_t size = arg.string.size; std::size_t size = arg.string.size;
if (size == 0) { if (size == 0) {
@ -623,14 +624,14 @@ void fmt::BasicFormatter<Char>::DoFormat() {
} }
case POINTER: case POINTER:
if (spec.type_ && spec.type_ != 'p') if (spec.type_ && spec.type_ != 'p')
internal::ReportUnknownType(spec.type_, "pointer"); internal::ReportUnknownType<Char>(spec.format_, spec.type_, "pointer");
spec.flags_= HASH_FLAG; spec.flags_= HASH_FLAG;
spec.type_ = 'x'; spec.type_ = 'x';
writer.FormatInt(reinterpret_cast<uintptr_t>(arg.pointer_value), spec); writer.FormatInt(reinterpret_cast<uintptr_t>(arg.pointer_value), spec);
break; break;
case CUSTOM: case CUSTOM:
if (spec.type_) if (spec.type_)
internal::ReportUnknownType(spec.type_, "object"); internal::ReportUnknownType<Char>(spec.format_, spec.type_, "object");
arg.custom.format(writer, arg.custom.value, spec); arg.custom.format(writer, arg.custom.value, spec);
break; break;
default: default:
@ -643,11 +644,14 @@ void fmt::BasicFormatter<Char>::DoFormat() {
// Explicit instantiations for char. // Explicit instantiations for char.
template void fmt::internal::ReportUnknownType<char>(
const char *format, char code, const char *type);
template void fmt::BasicWriter<char>::FormatDouble<double>( template void fmt::BasicWriter<char>::FormatDouble<double>(
double value, const FormatSpec &spec, int precision); double value, const FormatSpec<char> &spec, int precision);
template void fmt::BasicWriter<char>::FormatDouble<long double>( template void fmt::BasicWriter<char>::FormatDouble<long double>(
long double value, const FormatSpec &spec, int precision); long double value, const FormatSpec<char> &spec, int precision);
template fmt::BasicWriter<char>::CharPtr template fmt::BasicWriter<char>::CharPtr
fmt::BasicWriter<char>::FillPadding(CharPtr buffer, fmt::BasicWriter<char>::FillPadding(CharPtr buffer,
@ -675,11 +679,14 @@ template void fmt::BasicFormatter<char>::DoFormat();
// Explicit instantiations for wchar_t. // Explicit instantiations for wchar_t.
template void fmt::internal::ReportUnknownType<wchar_t>(
const wchar_t *format, char code, const char *type);
template void fmt::BasicWriter<wchar_t>::FormatDouble<double>( template void fmt::BasicWriter<wchar_t>::FormatDouble<double>(
double value, const FormatSpec &spec, int precision); double value, const FormatSpec<wchar_t> &spec, int precision);
template void fmt::BasicWriter<wchar_t>::FormatDouble<long double>( template void fmt::BasicWriter<wchar_t>::FormatDouble<long double>(
long double value, const FormatSpec &spec, int precision); long double value, const FormatSpec<wchar_t> &spec, int precision);
template fmt::BasicWriter<wchar_t>::CharPtr template fmt::BasicWriter<wchar_t>::CharPtr
fmt::BasicWriter<wchar_t>::FillPadding(CharPtr buffer, fmt::BasicWriter<wchar_t>::FillPadding(CharPtr buffer,
@ -705,3 +712,5 @@ template void fmt::BasicFormatter<wchar_t>::CheckSign(
const wchar_t *&s, const Arg &arg); const wchar_t *&s, const Arg &arg);
template void fmt::BasicFormatter<wchar_t>::DoFormat(); template void fmt::BasicFormatter<wchar_t>::DoFormat();
//template fmt::BasicFormatter<char>::Arg::Arg(wchar_t const*);

View File

@ -201,7 +201,8 @@ struct IsLongDouble { enum {VALUE = 0}; };
template <> template <>
struct IsLongDouble<long double> { enum {VALUE = 1}; }; struct IsLongDouble<long double> { enum {VALUE = 1}; };
void ReportUnknownType(char code, const char *type); template <typename Char>
void ReportUnknownType(const Char *format, char code, const char *type);
// Returns the number of decimal digits in n. Leading zeros are not counted // Returns the number of decimal digits in n. Leading zeros are not counted
// except for n == 0 in which case CountDigits returns 1. // except for n == 0 in which case CountDigits returns 1.
@ -343,12 +344,14 @@ struct AlignTypeSpec : AlignSpec {
char type() const { return TYPE; } char type() const { return TYPE; }
}; };
template <typename Char>
struct FormatSpec : AlignSpec { struct FormatSpec : AlignSpec {
unsigned flags_; unsigned flags_;
char type_; char type_;
const Char *format_;
FormatSpec(unsigned width = 0, char type = 0, wchar_t fill = ' ') FormatSpec(const Char *format, unsigned width = 0, char type = 0, wchar_t fill = ' ')
: AlignSpec(width, fill), flags_(0), type_(type) {} : AlignSpec(width, fill), flags_(0), type_(type), format_(format) {}
Alignment align() const { return align_; } Alignment align() const { return align_; }
@ -357,6 +360,7 @@ struct FormatSpec : AlignSpec {
bool hash_flag() const { return (flags_ & HASH_FLAG) != 0; } bool hash_flag() const { return (flags_ & HASH_FLAG) != 0; }
char type() const { return type_; } char type() const { return type_; }
const Char* format() const { return format_; }
}; };
template <typename T, typename SpecT> template <typename T, typename SpecT>
@ -528,17 +532,17 @@ class BasicWriter {
// Formats an integer. // Formats an integer.
template <typename T> template <typename T>
void FormatInt(T value, const FormatSpec &spec) { void FormatInt(T value, const FormatSpec<Char> &spec) {
*this << IntFormatter<T, FormatSpec>(value, spec); *this << IntFormatter<T, FormatSpec<Char> >(value, spec);
} }
// Formats a floating-point number (double or long double). // Formats a floating-point number (double or long double).
template <typename T> template <typename T>
void FormatDouble(T value, const FormatSpec &spec, int precision); void FormatDouble(T value, const FormatSpec<Char> &spec, int precision);
template <typename StringChar> template <typename StringChar>
CharPtr FormatString(const StringChar *s, CharPtr FormatString(const StringChar *s,
std::size_t size, const FormatSpec &spec); std::size_t size, const FormatSpec<Char> &spec);
// This method is private to disallow writing a wide string to a // 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 // char stream and vice versa. If you want to print a wide string
@ -628,7 +632,7 @@ class BasicWriter {
} }
BasicWriter &operator<<(double value) { BasicWriter &operator<<(double value) {
FormatDouble(value, FormatSpec(), -1); FormatDouble(value, FormatSpec<Char>(""), -1);
return *this; return *this;
} }
@ -637,7 +641,7 @@ class BasicWriter {
(``'g'``) and writes it to the stream. (``'g'``) and writes it to the stream.
*/ */
BasicWriter &operator<<(long double value) { BasicWriter &operator<<(long double value) {
FormatDouble(value, FormatSpec(), -1); FormatDouble(value, FormatSpec<Char>(""), -1);
return *this; return *this;
} }
@ -659,7 +663,7 @@ class BasicWriter {
template <typename T, typename Spec> template <typename T, typename Spec>
BasicWriter &operator<<(const IntFormatter<T, Spec> &f); BasicWriter &operator<<(const IntFormatter<T, Spec> &f);
void Write(const std::basic_string<char> &s, const FormatSpec &spec) { void Write(const std::basic_string<char> &s, const FormatSpec<char> &spec) {
FormatString(s.data(), s.size(), spec); FormatString(s.data(), s.size(), spec);
} }
@ -671,7 +675,7 @@ class BasicWriter {
template <typename Char> template <typename Char>
template <typename StringChar> template <typename StringChar>
typename BasicWriter<Char>::CharPtr BasicWriter<Char>::FormatString( typename BasicWriter<Char>::CharPtr BasicWriter<Char>::FormatString(
const StringChar *s, std::size_t size, const FormatSpec &spec) { const StringChar *s, std::size_t size, const FormatSpec<Char> &spec) {
CharPtr out = CharPtr(); CharPtr out = CharPtr();
if (spec.width() > size) { if (spec.width() > size) {
out = GrowBuffer(spec.width()); out = GrowBuffer(spec.width());
@ -772,7 +776,7 @@ BasicWriter<Char> &BasicWriter<Char>::operator<<(
break; break;
} }
default: default:
internal::ReportUnknownType(f.type(), "integer"); internal::ReportUnknownType<Char>(f.format(), f.type(), "integer");
break; break;
} }
return *this; return *this;
@ -788,7 +792,7 @@ typedef BasicWriter<wchar_t> WWriter;
// The default formatting function. // The default formatting function.
template <typename Char, typename T> template <typename Char, typename T>
void Format(BasicWriter<Char> &w, const FormatSpec &spec, const T &value) { void Format(BasicWriter<Char> &w, const FormatSpec<Char> &spec, const T &value) {
std::basic_ostringstream<Char> os; std::basic_ostringstream<Char> os;
os << value; os << value;
w.Write(os.str(), spec); w.Write(os.str(), spec);
@ -798,7 +802,7 @@ namespace internal {
// Formats an argument of a custom type, such as a user-defined class. // Formats an argument of a custom type, such as a user-defined class.
template <typename Char, typename T> template <typename Char, typename T>
void FormatCustomArg( void FormatCustomArg(
BasicWriter<Char> &w, const void *arg, const FormatSpec &spec) { BasicWriter<Char> &w, const void *arg, const FormatSpec<Char> &spec) {
Format(w, spec, *static_cast<const T*>(arg)); Format(w, spec, *static_cast<const T*>(arg));
} }
} }
@ -825,7 +829,7 @@ class BasicFormatter {
}; };
typedef void (*FormatFunc)( typedef void (*FormatFunc)(
BasicWriter<Char> &w, const void *arg, const FormatSpec &spec); BasicWriter<Char> &w, const void *arg, const FormatSpec<Char> &spec);
// A format argument. // A format argument.
class Arg { class Arg {