Skip to content

Commit

Permalink
review comment: turn cmake option into macro for SAFE_DURATION_CAST
Browse files Browse the repository at this point in the history
decided to have it on by default
  • Loading branch information
pauldreik committed Jun 17, 2019
1 parent c3a1594 commit 84eea80
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ endif ()
option(FMT_PEDANTIC "Enable extra warnings and expensive tests." OFF)
option(FMT_WERROR "Halt the compilation with an error on compiler warnings."
OFF)
option(FMT_SAFE_DURATION_CAST "verify chrono duration casts" OFF)

# Options that control generation of various targets.
option(FMT_DOC "Generate the doc target." ${MASTER_PROJECT})
Expand Down
13 changes: 9 additions & 4 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
#include <locale>
#include <sstream>

#ifdef FMT_SAFE_DURATION_CAST
// enable safe chrono durations, unless explicitly disabled
#ifndef FMT_SAFE_DURATION_CAST
# define FMT_SAFE_DURATION_CAST 1
#endif

#if FMT_SAFE_DURATION_CAST
# include "safe-duration-cast.h"
#endif

Expand Down Expand Up @@ -438,7 +443,7 @@ inline std::chrono::duration<Rep, std::milli> get_milliseconds(
std::chrono::duration<Rep, Period> d) {
// this may overflow and/or the result may not fit in the
// target type.
#ifdef FMT_SAFE_DURATION_CAST
#if FMT_SAFE_DURATION_CAST
// if(std::ratio_less<Period,std::ratio<1>>::value) {
using CommonSecondsType =
typename std::common_type<decltype(d), std::chrono::seconds>::type;
Expand Down Expand Up @@ -519,7 +524,7 @@ struct chrono_formatter {

// this may overflow and/or the result may not fit in the
// target type.
#ifdef FMT_SAFE_DURATION_CAST
#if FMT_SAFE_DURATION_CAST
int ec;
// might need checked conversion (rep!=Rep)
auto tmpval = std::chrono::duration<rep, Period>(val);
Expand Down Expand Up @@ -663,7 +668,7 @@ struct chrono_formatter {

if (ns == numeric_system::standard) {
write(second(), 2);
#ifdef FMT_SAFE_DURATION_CAST
#if FMT_SAFE_DURATION_CAST
int ec;
// convert rep->Rep
using Crep = std::chrono::duration<rep, Period>;
Expand Down

0 comments on commit 84eea80

Please sign in to comment.