Improve error reporting (#357)

This commit is contained in:
Victor Zverovich
2016-08-14 07:28:20 -07:00
parent 2bf59a97c6
commit f19d8f9655
6 changed files with 46 additions and 34 deletions

View File

@@ -1121,6 +1121,9 @@ struct Not { enum { value = 0 }; };
template <> template <>
struct Not<false> { enum { value = 1 }; }; struct Not<false> { enum { value = 1 }; };
template <typename T>
struct False { enum { value = 0 }; };
template <typename T, T> struct LConvCheck { template <typename T, T> struct LConvCheck {
LConvCheck(int) {} LConvCheck(int) {}
}; };
@@ -1136,6 +1139,35 @@ inline StringRef thousands_sep(
inline fmt::StringRef thousands_sep(...) { return ""; } inline fmt::StringRef thousands_sep(...) { return ""; }
#define FMT_CONCAT(a, b) a##b
#if FMT_GCC_VERSION >= 407
# define FMT_UNUSED __attribute__((unused))
#else
# define FMT_UNUSED
#endif
#ifndef FMT_USE_STATIC_ASSERT
# define FMT_USE_STATIC_ASSERT 0
#endif
#if FMT_USE_STATIC_ASSERT || FMT_HAS_FEATURE(cxx_static_assert) || \
(FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1600
# define FMT_STATIC_ASSERT(cond, message) static_assert(cond, message)
#else
# define FMT_CONCAT_(a, b) FMT_CONCAT(a, b)
# define FMT_STATIC_ASSERT(cond, message) \
typedef int FMT_CONCAT_(Assert, __LINE__)[(cond) ? 1 : -1] FMT_UNUSED
#endif
template <typename Formatter, typename Char, typename T>
void format_arg(Formatter &, const Char *, const T &) {
FMT_STATIC_ASSERT(False<T>::value,
"Cannot format argument. To enable the use of ostream "
"operator<< include fmt/ostream.h. Otherwise provide "
"an overload of format_arg.");
}
// Makes an Arg object from any type. // Makes an Arg object from any type.
template <typename Formatter> template <typename Formatter>
class MakeValue : public Arg { class MakeValue : public Arg {
@@ -1179,7 +1211,7 @@ class MakeValue : public Arg {
template <typename T> template <typename T>
static void format_custom_arg( static void format_custom_arg(
void *formatter, const void *arg, void *format_str_ptr) { void *formatter, const void *arg, void *format_str_ptr) {
format(*static_cast<Formatter*>(formatter), format_arg(*static_cast<Formatter*>(formatter),
*static_cast<const Char**>(format_str_ptr), *static_cast<const Char**>(format_str_ptr),
*static_cast<const T*>(arg)); *static_cast<const T*>(arg));
} }
@@ -3323,7 +3355,6 @@ void arg(WStringRef, const internal::NamedArg<Char>&) FMT_DELETED_OR_UNDEFINED;
#define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N #define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 #define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
#define FMT_CONCAT(a, b) a##b
#define FMT_FOR_EACH_(N, f, ...) \ #define FMT_FOR_EACH_(N, f, ...) \
FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__)) FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__))
#define FMT_FOR_EACH(f, ...) \ #define FMT_FOR_EACH(f, ...) \

View File

@@ -73,7 +73,7 @@ void write(std::ostream &os, Writer &w);
// Formats a value. // Formats a value.
template <typename Char, typename ArgFormatter, typename T> template <typename Char, typename ArgFormatter, typename T>
void format(BasicFormatter<Char, ArgFormatter> &f, void format_arg(BasicFormatter<Char, ArgFormatter> &f,
const Char *&format_str, const T &value) { const Char *&format_str, const T &value) {
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer; internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;

View File

@@ -51,25 +51,6 @@
# endif # endif
#endif #endif
#if FMT_GCC_VERSION >= 407
# define FMT_UNUSED __attribute__((unused))
#else
# define FMT_UNUSED
#endif
#ifndef FMT_USE_STATIC_ASSERT
# define FMT_USE_STATIC_ASSERT 0
#endif
#if FMT_USE_STATIC_ASSERT || FMT_HAS_FEATURE(cxx_static_assert) || \
(FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1600
# define FMT_STATIC_ASSERT(cond, message) static_assert(cond, message)
#else
# define FMT_CONCAT_(a, b) FMT_CONCAT(a, b)
# define FMT_STATIC_ASSERT(cond, message) \
typedef int FMT_CONCAT_(Assert, __LINE__)[(cond) ? 1 : -1] FMT_UNUSED
#endif
// Retries the expression while it evaluates to error_result and errno // Retries the expression while it evaluates to error_result and errno
// equals to EINTR. // equals to EINTR.
#ifndef _WIN32 #ifndef _WIN32

View File

@@ -15,7 +15,7 @@
namespace fmt { namespace fmt {
template <typename ArgFormatter> template <typename ArgFormatter>
void format(BasicFormatter<char, ArgFormatter> &f, void format_arg(BasicFormatter<char, ArgFormatter> &f,
const char *&format_str, const std::tm &tm) { const char *&format_str, const std::tm &tm) {
if (*format_str == ':') if (*format_str == ':')
++format_str; ++format_str;

View File

@@ -1366,7 +1366,7 @@ TEST(FormatterTest, FormatCStringRef) {
EXPECT_EQ("test", format("{0}", CStringRef("test"))); EXPECT_EQ("test", format("{0}", CStringRef("test")));
} }
void format(fmt::BasicFormatter<char> &f, const char *, const Date &d) { void format_arg(fmt::BasicFormatter<char> &f, const char *, const Date &d) {
f.writer() << d.year() << '-' << d.month() << '-' << d.day(); f.writer() << d.year() << '-' << d.month() << '-' << d.day();
} }
@@ -1379,7 +1379,7 @@ TEST(FormatterTest, FormatCustom) {
class Answer {}; class Answer {};
template <typename Char> template <typename Char>
void format(fmt::BasicFormatter<Char> &f, const Char *, Answer) { void format_arg(fmt::BasicFormatter<Char> &f, const Char *, Answer) {
f.writer() << "42"; f.writer() << "42";
} }

View File

@@ -64,7 +64,7 @@ namespace {
struct Test {}; struct Test {};
template <typename Char> template <typename Char>
void format(fmt::BasicFormatter<Char> &f, const Char *, Test) { void format_arg(fmt::BasicFormatter<Char> &f, const Char *, Test) {
f.writer() << "test"; f.writer() << "test";
} }
@@ -581,7 +581,7 @@ struct CustomFormatter {
typedef char Char; typedef char Char;
}; };
void format(CustomFormatter &, const char *&s, const Test &) { void format_arg(CustomFormatter &, const char *&s, const Test &) {
s = "custom_format"; s = "custom_format";
} }