Commit 8ed264fc authored by Victor Zverovich's avatar Victor Zverovich

Rename type enum constants to prevent collision with poorly written C libs (#644)

parent 4ba3f7db
......@@ -394,22 +394,23 @@ template <typename T, typename Char>
struct is_named_arg<named_arg<T, Char>> : std::true_type {};
enum type {
NONE, NAMED_ARG,
none_type, name_arg_type,
// Integer types should go first,
INT, UINT, LONG_LONG, ULONG_LONG, BOOL, CHAR, LAST_INTEGER_TYPE = CHAR,
int_type, uint_type, long_long_type, ulong_long_type, bool_type, char_type,
last_integer_type = char_type,
// followed by floating-point types.
DOUBLE, LONG_DOUBLE, LAST_NUMERIC_TYPE = LONG_DOUBLE,
CSTRING, STRING, POINTER, CUSTOM
double_type, long_double_type, last_numeric_type = long_double_type,
cstring_type, string_type, pointer_type, custom_type
};
FMT_CONSTEXPR bool is_integral(type t) {
FMT_ASSERT(t != internal::NAMED_ARG, "invalid argument type");
return t > internal::NONE && t <= internal::LAST_INTEGER_TYPE;
FMT_ASSERT(t != internal::name_arg_type, "invalid argument type");
return t > internal::none_type && t <= internal::last_integer_type;
}
FMT_CONSTEXPR bool is_arithmetic(type t) {
FMT_ASSERT(t != internal::NAMED_ARG, "invalid argument type");
return t > internal::NONE && t <= internal::LAST_NUMERIC_TYPE;
FMT_ASSERT(t != internal::name_arg_type, "invalid argument type");
return t > internal::none_type && t <= internal::last_numeric_type;
}
template <typename T, bool ENABLE = true>
......@@ -519,61 +520,63 @@ template <typename Context, typename T>
FMT_CONSTEXPR basic_arg<Context> make_arg(const T &value);
#define FMT_MAKE_VALUE(TAG, ArgType, ValueType) \
template <typename C, typename char_type = typename C::char_type> \
template <typename C, typename Char = typename C::char_type> \
FMT_CONSTEXPR typed_value<C, TAG> make_value(ArgType val) { \
return static_cast<ValueType>(val); \
}
FMT_MAKE_VALUE(BOOL, bool, int)
FMT_MAKE_VALUE(INT, short, int)
FMT_MAKE_VALUE(UINT, unsigned short, unsigned)
FMT_MAKE_VALUE(INT, int, int)
FMT_MAKE_VALUE(UINT, unsigned, unsigned)
FMT_MAKE_VALUE(bool_type, bool, int)
FMT_MAKE_VALUE(int_type, short, int)
FMT_MAKE_VALUE(uint_type, unsigned short, unsigned)
FMT_MAKE_VALUE(int_type, int, int)
FMT_MAKE_VALUE(uint_type, unsigned, unsigned)
// To minimize the number of types we need to deal with, long is translated
// either to int or to long long depending on its size.
typedef std::conditional<sizeof(long) == sizeof(int), int, long long>::type
long_type;
FMT_MAKE_VALUE((sizeof(long) == sizeof(int) ? INT : LONG_LONG), long, long_type)
FMT_MAKE_VALUE(
(sizeof(long) == sizeof(int) ? int_type : long_long_type), long, long_type)
typedef std::conditional<sizeof(unsigned long) == sizeof(unsigned),
unsigned, unsigned long long>::type ulong_type;
FMT_MAKE_VALUE((sizeof(unsigned long) == sizeof(unsigned) ? UINT : ULONG_LONG),
FMT_MAKE_VALUE(
(sizeof(unsigned long) == sizeof(unsigned) ? uint_type : ulong_long_type),
unsigned long, ulong_type)
FMT_MAKE_VALUE(LONG_LONG, long long, long long)
FMT_MAKE_VALUE(ULONG_LONG, unsigned long long, unsigned long long)
FMT_MAKE_VALUE(INT, signed char, int)
FMT_MAKE_VALUE(UINT, unsigned char, unsigned)
FMT_MAKE_VALUE(CHAR, char, int)
FMT_MAKE_VALUE(long_long_type, long long, long long)
FMT_MAKE_VALUE(ulong_long_type, unsigned long long, unsigned long long)
FMT_MAKE_VALUE(int_type, signed char, int)
FMT_MAKE_VALUE(uint_type, unsigned char, unsigned)
FMT_MAKE_VALUE(char_type, char, int)
#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
template <typename C>
inline typed_value<C, CHAR> make_value(wchar_t val) {
inline typed_value<C, char_type> make_value(wchar_t val) {
require_wchar<typename C::char_type>();
return static_cast<int>(val);
}
#endif
FMT_MAKE_VALUE(DOUBLE, float, double)
FMT_MAKE_VALUE(DOUBLE, double, double)
FMT_MAKE_VALUE(LONG_DOUBLE, long double, long double)
FMT_MAKE_VALUE(double_type, float, double)
FMT_MAKE_VALUE(double_type, double, double)
FMT_MAKE_VALUE(long_double_type, long double, long double)
// Formatting of wide strings into a narrow buffer and multibyte strings
// into a wide buffer is disallowed (https://github.com/fmtlib/fmt/pull/606).
FMT_MAKE_VALUE(CSTRING, char_type*, const char_type*)
FMT_MAKE_VALUE(CSTRING, const char_type*, const char_type*)
FMT_MAKE_VALUE(CSTRING, signed char*, const signed char*)
FMT_MAKE_VALUE(CSTRING, const signed char*, const signed char*)
FMT_MAKE_VALUE(CSTRING, unsigned char*, const unsigned char*)
FMT_MAKE_VALUE(CSTRING, const unsigned char*, const unsigned char*)
FMT_MAKE_VALUE(STRING, basic_string_view<char_type>,
basic_string_view<char_type>)
FMT_MAKE_VALUE(STRING, const std::basic_string<char_type>&,
basic_string_view<char_type>)
FMT_MAKE_VALUE(POINTER, void*, const void*)
FMT_MAKE_VALUE(POINTER, const void*, const void*)
FMT_MAKE_VALUE(POINTER, std::nullptr_t, const void*)
FMT_MAKE_VALUE(cstring_type, Char*, const Char*)
FMT_MAKE_VALUE(cstring_type, const Char*, const Char*)
FMT_MAKE_VALUE(cstring_type, signed char*, const signed char*)
FMT_MAKE_VALUE(cstring_type, const signed char*, const signed char*)
FMT_MAKE_VALUE(cstring_type, unsigned char*, const unsigned char*)
FMT_MAKE_VALUE(cstring_type, const unsigned char*, const unsigned char*)
FMT_MAKE_VALUE(string_type, basic_string_view<Char>,
basic_string_view<Char>)
FMT_MAKE_VALUE(string_type, const std::basic_string<Char>&,
basic_string_view<Char>)
FMT_MAKE_VALUE(pointer_type, void*, const void*)
FMT_MAKE_VALUE(pointer_type, const void*, const void*)
FMT_MAKE_VALUE(pointer_type, std::nullptr_t, const void*)
// Formatting of arbitrary pointers is disallowed. If you want to output a
// pointer cast it to "void *" or "const void *". In particular, this forbids
......@@ -587,16 +590,16 @@ void make_value(const T *p) {
template <typename C, typename T>
inline typename std::enable_if<
convert_to_int<T>::value && std::is_enum<T>::value,
typed_value<C, INT>>::type
typed_value<C, int_type>>::type
make_value(const T &val) { return static_cast<int>(val); }
template <typename C, typename T>
inline typename std::enable_if<
!convert_to_int<T>::value, typed_value<C, CUSTOM>>::type
!convert_to_int<T>::value, typed_value<C, custom_type>>::type
make_value(const T &val) { return val; }
template <typename C, typename T>
typed_value<C, NAMED_ARG>
typed_value<C, name_arg_type>
make_value(const named_arg<T, typename C::char_type> &val) {
basic_arg<C> arg = make_arg<C>(val.value);
std::memcpy(val.data, &arg, sizeof(arg));
......@@ -641,17 +644,17 @@ class basic_arg {
internal::custom_value<Context> custom_;
};
FMT_CONSTEXPR basic_arg() : type_(internal::NONE) {}
FMT_CONSTEXPR basic_arg() : type_(internal::none_type) {}
explicit operator bool() const FMT_NOEXCEPT {
return type_ != internal::NONE;
return type_ != internal::none_type;
}
internal::type type() const { return type_; }
bool is_integral() const { return internal::is_integral(type_); }
bool is_arithmetic() const { return internal::is_arithmetic(type_); }
bool is_pointer() const { return type_ == internal::POINTER; }
bool is_pointer() const { return type_ == internal::pointer_type; }
};
// Parsing context consisting of a format string range being parsed and an
......@@ -982,7 +985,7 @@ class basic_format_args {
if (index > internal::MAX_PACKED_ARGS)
return arg;
arg.type_ = type(index);
if (arg.type_ == internal::NONE)
if (arg.type_ == internal::none_type)
return arg;
internal::value<Context> &val = arg.value_;
val = values_[index];
......@@ -1001,7 +1004,7 @@ class basic_format_args {
/** Returns the argument at specified index. */
format_arg operator[](size_type index) const {
format_arg arg = get(index);
return arg.type_ == internal::NAMED_ARG ?
return arg.type_ == internal::name_arg_type ?
arg.value_.as_named_arg().template deserialize<Context>() : arg;
}
......
......@@ -1006,35 +1006,35 @@ FMT_CONSTEXPR typename std::result_of<Visitor(int)>::type
visit(Visitor &&vis, basic_arg<Context> arg) {
typedef typename Context::char_type char_type;
switch (arg.type_) {
case internal::NONE:
case internal::none_type:
return vis(monostate());
case internal::NAMED_ARG:
case internal::name_arg_type:
FMT_ASSERT(false, "invalid argument type");
break;
case internal::INT:
case internal::int_type:
return vis(arg.value_.int_value);
case internal::UINT:
case internal::uint_type:
return vis(arg.value_.uint_value);
case internal::LONG_LONG:
case internal::long_long_type:
return vis(arg.value_.long_long_value);
case internal::ULONG_LONG:
case internal::ulong_long_type:
return vis(arg.value_.ulong_long_value);
case internal::BOOL:
case internal::bool_type:
return vis(arg.value_.int_value != 0);
case internal::CHAR:
case internal::char_type:
return vis(static_cast<char_type>(arg.value_.int_value));
case internal::DOUBLE:
case internal::double_type:
return vis(arg.value_.double_value);
case internal::LONG_DOUBLE:
case internal::long_double_type:
return vis(arg.value_.long_double_value);
case internal::CSTRING:
case internal::cstring_type:
return vis(arg.value_.string.value);
case internal::STRING:
case internal::string_type:
return vis(basic_string_view<char_type>(
arg.value_.string.value, arg.value_.string.size));
case internal::POINTER:
case internal::pointer_type:
return vis(arg.value_.pointer);
case internal::CUSTOM:
case internal::custom_type:
return vis(typename basic_arg<Context>::handle(arg.value_.custom));
}
return typename std::result_of<Visitor(int)>::type();
......@@ -1293,14 +1293,14 @@ void arg_map<Context>::init(const basic_format_args<Context> &args) {
if (map_)
return;
map_ = new entry[args.max_size()];
bool use_values = args.type(MAX_PACKED_ARGS - 1) == internal::NONE;
bool use_values = args.type(MAX_PACKED_ARGS - 1) == internal::none_type;
if (use_values) {
for (unsigned i = 0;/*nothing*/; ++i) {
internal::type arg_type = args.type(i);
switch (arg_type) {
case internal::NONE:
case internal::none_type:
return;
case internal::NAMED_ARG:
case internal::name_arg_type:
push_back(args.values_[i]);
break;
default:
......@@ -1310,14 +1310,14 @@ void arg_map<Context>::init(const basic_format_args<Context> &args) {
return;
}
for (unsigned i = 0; i != MAX_PACKED_ARGS; ++i) {
if (args.type(i) == internal::NAMED_ARG)
if (args.type(i) == internal::name_arg_type)
push_back(args.args_[i].value_);
}
for (unsigned i = MAX_PACKED_ARGS; ; ++i) {
switch (args.args_[i].type_) {
case internal::NONE:
case internal::none_type:
return;
case internal::NAMED_ARG:
case internal::name_arg_type:
push_back(args.args_[i].value_);
break;
default:
......@@ -1614,7 +1614,7 @@ class specs_checker : public Handler {
}
FMT_CONSTEXPR void end_precision() {
if (is_integral(arg_type_) || arg_type_ == POINTER)
if (is_integral(arg_type_) || arg_type_ == pointer_type)
this->on_error("precision not allowed for this argument type");
}
......@@ -1626,8 +1626,8 @@ class specs_checker : public Handler {
FMT_CONSTEXPR void check_sign() {
require_numeric_argument();
if (is_integral(arg_type_) && arg_type_ != INT && arg_type_ != LONG_LONG &&
arg_type_ != CHAR) {
if (is_integral(arg_type_) && arg_type_ != int_type &&
arg_type_ != long_long_type && arg_type_ != char_type) {
this->on_error("format specifier requires signed argument");
}
}
......@@ -2062,7 +2062,7 @@ FMT_CONSTEXPR bool check_format_string(
// because of a bug in MSVC.
template <typename Context, typename T>
struct format_type :
std::integral_constant<bool, get_type<Context, T>::value != CUSTOM> {};
std::integral_constant<bool, get_type<Context, T>::value != custom_type> {};
// Specifies whether to format enums.
template <typename T, typename Enable = void>
......@@ -2942,38 +2942,38 @@ struct formatter<
auto type_spec = specs_.type();
auto eh = ctx.error_handler();
switch (type) {
case internal::NONE:
case internal::NAMED_ARG:
case internal::none_type:
case internal::name_arg_type:
FMT_ASSERT(false, "invalid argument type");
break;
case internal::INT:
case internal::UINT:
case internal::LONG_LONG:
case internal::ULONG_LONG:
case internal::BOOL:
case internal::int_type:
case internal::uint_type:
case internal::long_long_type:
case internal::ulong_long_type:
case internal::bool_type:
handle_int_type_spec(
type_spec, internal::int_type_checker<decltype(eh)>(eh));
break;
case internal::CHAR:
case internal::char_type:
handle_char_specs(specs_, internal::char_specs_checker<decltype(eh)>(
type_spec, eh));
break;
case internal::DOUBLE:
case internal::LONG_DOUBLE:
case internal::double_type:
case internal::long_double_type:
handle_float_type_spec(
type_spec, internal::float_type_checker<decltype(eh)>(eh));
break;
case internal::CSTRING:
case internal::cstring_type:
internal::handle_cstring_type_spec(
type_spec, internal::cstring_type_checker<decltype(eh)>(eh));
break;
case internal::STRING:
case internal::string_type:
internal::check_string_type_spec(type_spec, eh);
break;
case internal::POINTER:
case internal::pointer_type:
internal::check_pointer_type_spec(type_spec, eh);
break;
case internal::CUSTOM:
case internal::custom_type:
// Custom format specifiers should be checked in parse functions of
// formatter specializations.
break;
......@@ -3080,7 +3080,7 @@ typename basic_context<Range, Char>::format_arg
basic_context<Range, Char>::get_arg(basic_string_view<char_type> name) {
map_.init(this->args());
format_arg arg = map_.find(name);
if (arg.type() == internal::NONE)
if (arg.type() == internal::none_type)
this->on_error("argument not found");
return arg;
}
......
......@@ -1811,7 +1811,7 @@ TEST(FormatTest, ConstexprDynamicSpecsHandler) {
FMT_CONSTEXPR test_format_specs_handler check_specs(const char *s) {
fmt::internal::specs_checker<test_format_specs_handler>
checker(test_format_specs_handler(), fmt::internal::DOUBLE);
checker(test_format_specs_handler(), fmt::internal::double_type);
parse_format_specs(s, checker);
return checker;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment