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"
"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))) {
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<unsigned>(code) << type));
fmt::str(fmt::Format("unknown format code '\\x{:02x}' for {} while parsing {}")
<< static_cast<unsigned>(code) << type << format));
}
@ -200,7 +201,7 @@ typename fmt::BasicWriter<Char>::CharPtr
template <typename Char>
template <typename T>
void fmt::BasicWriter<Char>::FormatDouble(
T value, const FormatSpec &spec, int precision) {
T value, const FormatSpec<Char> &spec, int precision) {
// Check type.
char type = spec.type();
bool upper = false;
@ -220,7 +221,7 @@ void fmt::BasicWriter<Char>::FormatDouble(
upper = true;
break;
default:
internal::ReportUnknownType(type, "double");
internal::ReportUnknownType<Char>(spec.format(), type, "double");
break;
}
@ -430,7 +431,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
const Arg &arg = ParseArgIndex(s);
FormatSpec spec;
FormatSpec<Char> spec(format_);
int precision = -1;
if (*s == ':') {
++s;
@ -587,7 +588,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
break;
case CHAR: {
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;
CharPtr out = CharPtr();
if (spec.width_ > 1) {
@ -609,7 +610,7 @@ void fmt::BasicFormatter<Char>::DoFormat() {
}
case STRING: {
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;
std::size_t size = arg.string.size;
if (size == 0) {
@ -623,14 +624,14 @@ void fmt::BasicFormatter<Char>::DoFormat() {
}
case POINTER:
if (spec.type_ && spec.type_ != 'p')
internal::ReportUnknownType(spec.type_, "pointer");
internal::ReportUnknownType<Char>(spec.format_, spec.type_, "pointer");
spec.flags_= HASH_FLAG;
spec.type_ = 'x';
writer.FormatInt(reinterpret_cast<uintptr_t>(arg.pointer_value), spec);
break;
case CUSTOM:
if (spec.type_)
internal::ReportUnknownType(spec.type_, "object");
internal::ReportUnknownType<Char>(spec.format_, spec.type_, "object");
arg.custom.format(writer, arg.custom.value, spec);
break;
default:
@ -643,11 +644,14 @@ void fmt::BasicFormatter<Char>::DoFormat() {
// 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>(
double value, const FormatSpec &spec, int precision);
double value, const FormatSpec<char> &spec, int precision);
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
fmt::BasicWriter<char>::FillPadding(CharPtr buffer,
@ -675,11 +679,14 @@ template void fmt::BasicFormatter<char>::DoFormat();
// 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>(
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>(
long double value, const FormatSpec &spec, int precision);
long double value, const FormatSpec<wchar_t> &spec, int precision);
template fmt::BasicWriter<wchar_t>::CharPtr
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);
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 <>
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
// except for n == 0 in which case CountDigits returns 1.
@ -343,12 +344,14 @@ struct AlignTypeSpec : AlignSpec {
char type() const { return TYPE; }
};
template <typename Char>
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 <typename T, typename SpecT>
@ -528,17 +532,17 @@ class BasicWriter {
// Formats an integer.
template <typename T>
void FormatInt(T value, const FormatSpec &spec) {
*this << IntFormatter<T, FormatSpec>(value, spec);
void FormatInt(T value, const FormatSpec<Char> &spec) {
*this << IntFormatter<T, FormatSpec<Char> >(value, spec);
}
// Formats a floating-point number (double or long double).
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>
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
// 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<Char>(""), -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<Char>(""), -1);
return *this;
}
@ -659,7 +663,7 @@ class BasicWriter {
template <typename T, typename Spec>
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);
}
@ -671,7 +675,7 @@ class BasicWriter {
template <typename Char>
template <typename StringChar>
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();
if (spec.width() > size) {
out = GrowBuffer(spec.width());
@ -772,7 +776,7 @@ BasicWriter<Char> &BasicWriter<Char>::operator<<(
break;
}
default:
internal::ReportUnknownType(f.type(), "integer");
internal::ReportUnknownType<Char>(f.format(), f.type(), "integer");
break;
}
return *this;
@ -788,7 +792,7 @@ typedef BasicWriter<wchar_t> WWriter;
// The default formatting function.
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;
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 <typename Char, typename T>
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));
}
}
@ -825,7 +829,7 @@ class BasicFormatter {
};
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.
class Arg {