Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into fuzz
Browse files Browse the repository at this point in the history
# Conflicts:
#	CMakeLists.txt
  • Loading branch information
pauldreik committed Jun 28, 2019
2 parents f404079 + 037b84f commit 1ece641
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 197 deletions.
13 changes: 12 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,18 @@ if (MASTER_PROJECT AND CMAKE_GENERATOR MATCHES "Visual Studio")
${CMAKE_MAKE_PROGRAM} -p:FrameworkPathOverride=\"${netfxpath}\" %*")
endif ()

set(strtod_l_headers stdlib.h)
if (APPLE)
set(strtod_l_headers ${strtod_l_headers} xlocale.h)
endif ()

include(CheckSymbolExists)
if (WIN32)
check_symbol_exists(open io.h HAVE_OPEN)
check_symbol_exists(_strtod_l "${strtod_l_headers}" HAVE_STRTOD_L)
else ()
check_symbol_exists(open fcntl.h HAVE_OPEN)
check_symbol_exists(strtod_l "${strtod_l_headers}" HAVE_STRTOD_L)
endif ()

function(add_headers VAR)
Expand All @@ -145,7 +152,7 @@ endfunction()

# Define the fmt library, its includes and the needed defines.
add_headers(FMT_HEADERS chrono.h color.h core.h format.h format-inl.h locale.h
ostream.h prepare.h printf.h time.h ranges.h safe-duration-cast.h)
ostream.h prepare.h printf.h ranges.h safe-duration-cast.h)
set(FMT_SOURCES src/format.cc)
if (HAVE_OPEN)
add_headers(FMT_HEADERS posix.h)
Expand All @@ -155,6 +162,10 @@ endif ()
add_library(fmt ${FMT_SOURCES} ${FMT_HEADERS} README.rst ChangeLog.rst)
add_library(fmt::fmt ALIAS fmt)

if (HAVE_STRTOD_L)
target_compile_definitions(fmt PUBLIC FMT_LOCALE)
endif ()

if (FMT_WERROR)
target_compile_options(fmt PRIVATE ${WERROR_FLAG})
endif ()
Expand Down
18 changes: 10 additions & 8 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,15 @@ inline bool isfinite(T value) {
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
inline int to_nonnegative_int(T value, int upper) {
FMT_ASSERT(value >= 0 && value <= upper, "invalid value");
(void)upper;
return static_cast<int>(value);
}
template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
inline int to_nonnegative_int(T value, int upper) {
FMT_ASSERT(
std::isnan(value) || (value >= 0 && value <= static_cast<T>(upper)),
"invalid value");
(void)upper;
return static_cast<int>(value);
}

Expand All @@ -423,7 +425,7 @@ inline T mod(T x, int y) {
}
template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
inline T mod(T x, int y) {
return std::fmod(x, y);
return std::fmod(x, static_cast<T>(y));
}

// If T is an integral type, maps T to its unsigned counterpart, otherwise
Expand Down Expand Up @@ -477,8 +479,8 @@ template <typename Rep, typename Period,
FMT_ENABLE_IF(std::is_floating_point<Rep>::value)>
inline std::chrono::duration<Rep, std::milli> get_milliseconds(
std::chrono::duration<Rep, Period> d) {
return std::chrono::duration<Rep, std::milli>(
mod(d.count() * Period::num / Period::den * 1000, 1000));
auto ms = mod(d.count() * Period::num / Period::den * 1000, 1000);
return std::chrono::duration<Rep, std::milli>(static_cast<Rep>(ms));
}

template <typename Rep, typename OutputIt>
Expand Down Expand Up @@ -551,15 +553,15 @@ struct chrono_formatter {
return true;
}

Rep hour() const { return mod((s.count() / 3600), 24); }
Rep hour() const { return static_cast<Rep>(mod((s.count() / 3600), 24)); }

Rep hour12() const {
Rep hour = mod((s.count() / 3600), 12);
Rep hour = static_cast<Rep>(mod((s.count() / 3600), 12));
return hour <= 0 ? 12 : hour;
}

Rep minute() const { return mod((s.count() / 60), 60); }
Rep second() const { return mod(s.count(), 60); }
Rep minute() const { return static_cast<Rep>(mod((s.count() / 60), 60)); }
Rep second() const { return static_cast<Rep>(mod(s.count(), 60)); }

std::tm time() const {
auto time = std::tm();
Expand Down Expand Up @@ -819,7 +821,7 @@ struct formatter<std::chrono::duration<Rep, Period>, Char> {
// is not specified.
basic_memory_buffer<Char> buf;
auto out = std::back_inserter(buf);
typedef output_range<decltype(ctx.out()), Char> range;
using range = internal::output_range<decltype(ctx.out()), Char>;
basic_writer<range> w(range(ctx.out()));
internal::handle_dynamic_spec<internal::width_checker>(
spec.width_, width_ref, ctx, format_str.begin());
Expand Down
4 changes: 2 additions & 2 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ struct error_handler {
FMT_CONSTEXPR error_handler(const error_handler&) {}

// This function is intentionally not constexpr to give a compile-time error.
FMT_API FMT_NORETURN void on_error(const char* message);
FMT_NORETURN FMT_API void on_error(const char* message);
};
} // namespace internal

Expand Down Expand Up @@ -766,7 +766,7 @@ template <typename Context> struct arg_mapper {
return val;
}

FMT_CONSTEXPR double map(float val) { return val; }
FMT_CONSTEXPR double map(float val) { return static_cast<double>(val); }
FMT_CONSTEXPR double map(double val) { return val; }
FMT_CONSTEXPR long double map(long double val) { return val; }

Expand Down
18 changes: 11 additions & 7 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ typedef void (*FormatFunc)(internal::buffer<char>&, int, string_view);
// ERANGE - buffer is not large enough to store the error message
// other - failure
// Buffer should be at least of size 1.
int safe_strerror(int error_code, char*& buffer,
std::size_t buffer_size) FMT_NOEXCEPT {
FMT_FUNC int safe_strerror(int error_code, char*& buffer,
std::size_t buffer_size) FMT_NOEXCEPT {
FMT_ASSERT(buffer != nullptr && buffer_size != 0, "invalid buffer");

class dispatcher {
Expand Down Expand Up @@ -147,8 +147,8 @@ int safe_strerror(int error_code, char*& buffer,
return dispatcher(error_code, buffer, buffer_size).run();
}

void format_error_code(internal::buffer<char>& out, int error_code,
string_view message) FMT_NOEXCEPT {
FMT_FUNC void format_error_code(internal::buffer<char>& out, int error_code,
string_view message) FMT_NOEXCEPT {
// Report error code making sure that the output fits into
// inline_buffer_size to avoid dynamic memory allocation and potential
// bad_alloc.
Expand All @@ -175,15 +175,16 @@ void format_error_code(internal::buffer<char>& out, int error_code,
}

// try an fwrite, FMT_THROW on failure
void fwrite_fully(const void* ptr, size_t size, size_t count, FILE* stream) {
FMT_FUNC void fwrite_fully(const void* ptr, size_t size, size_t count,
FILE* stream) {
size_t written = std::fwrite(ptr, size, count, stream);
if (written < count) {
FMT_THROW(system_error(errno, "cannot write to file"));
}
}

void report_error(FormatFunc func, int error_code,
string_view message) FMT_NOEXCEPT {
FMT_FUNC void report_error(FormatFunc func, int error_code,
string_view message) FMT_NOEXCEPT {
memory_buffer full_message;
func(full_message, error_code, message);
// Use Writer::data instead of Writer::c_str to avoid potential memory
Expand Down Expand Up @@ -218,6 +219,9 @@ FMT_FUNC Char internal::thousands_sep_impl(locale_ref) {
}
#endif

FMT_API FMT_FUNC format_error::~format_error() FMT_NOEXCEPT {}
FMT_API FMT_FUNC system_error::~system_error() FMT_NOEXCEPT {}

FMT_FUNC void system_error::init(int err_code, string_view format_str,
format_args args) {
error_code_ = err_code;
Expand Down
Loading

0 comments on commit 1ece641

Please sign in to comment.