From b6e0524516e367076ccfb5373a3181fa7ab03372 Mon Sep 17 00:00:00 2001 From: "xanthos@durruti" Date: Wed, 9 Oct 2024 15:00:11 +0300 Subject: [PATCH] var --- CMakeLists.txt | 26 ++++++++++++++++++++++++++ include/cdatetime.hpp | 4 ++-- include/dtfund.hpp | 17 ++++++++++++++--- src/dat.cpp | 11 +++++++++-- src/datetime_io_core.cpp | 14 +++++++++----- src/modified_julian_day.cpp | 19 ++++++++++++------- src/strmonth.cpp | 4 ++-- src/utc2tai.cpp | 4 ++-- src/ydoy_date.cpp | 2 +- src/ymd_date.cpp | 4 ++-- 10 files changed, 79 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 65c1d2a..f5e4eb1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,9 @@ project( LANGUAGES CXX ) +# Enable clang-tidy option +option(ENABLE_CLANG_TIDY "Enable clang-tidy checks" OFF) + set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED On) set(CMAKE_CXX_EXTENSIONS Off) @@ -26,6 +29,23 @@ add_compile_definitions( $<$:DEBUG> ) +# clang-tidy (targets that follow will be checked) +if(ENABLE_CLANG_TIDY) + set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=bugprone-*,\ + clang-analyzer-*,\ + cppcoreguidelines-*,\ + llvm-*,\ + modernize-*,\ + performance-*,\ + -modernize-use-trailing-return-type,\ + -cppcoreguidelines-pro-bounds-pointer-arithmetic,\ + -cppcoreguidelines-pro-bounds-constant-array-index,\ + -cppcoreguidelines-pro-type-vararg") + message(STATUS "clang-tidy is enabled.") +else() + message(STATUS "clang-tidy is disabled. To enable it, use -DENABLE_CLANG_TIDY=ON.") +endif() + add_library(datetime) target_include_directories(datetime PUBLIC $ @@ -34,6 +54,9 @@ target_include_directories(datetime PUBLIC add_subdirectory(src) +# disable clang-tidy (targets that follow will not be checked) +set(CMAKE_CXX_CLANG_TIDY "") + # The tests include(CTest) add_subdirectory(test/unit_tests) @@ -41,6 +64,9 @@ add_subdirectory(test/should_not_compile) find_library(sofa sofa_c) if (sofa) add_subdirectory(test/sofa) + message(STATUS "found sofa lib, will compile relevant tests") +else() + message(STATUS "sofa lib not found; tests in test/sofa will not be compiled.") endif() enable_testing() diff --git a/include/cdatetime.hpp b/include/cdatetime.hpp index 6b6c30c..09d3b59 100644 --- a/include/cdatetime.hpp +++ b/include/cdatetime.hpp @@ -8,10 +8,10 @@ namespace dso { /** Jan 1st 1980 for GPS Time */ -constexpr const long JAN61980 = 44'244L; +constexpr const int JAN61980 = 44'244L; /** */ -constexpr const long JAN11901 = 15'385L; +constexpr const int JAN11901 = 15'385L; /** @brief Seconds per day. * @warning This is not always true in case of UTC dates; the day a leap diff --git a/include/dtfund.hpp b/include/dtfund.hpp index c47a4ee..92ed857 100644 --- a/include/dtfund.hpp +++ b/include/dtfund.hpp @@ -16,6 +16,7 @@ #include #include #include +#include namespace dso { @@ -37,7 +38,7 @@ class nanoseconds; namespace core { /** Number of days past at the end of non-leap and leap years. */ -constexpr const long month_day[2][13] = { +constexpr const int month_day[2][13] = { {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}}; @@ -560,11 +561,15 @@ class month { constexpr static const char *short_names[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + constexpr static const int SHORT_NAMES_LEN = + sizeof(short_names) / sizeof(short_names[0]); /** long month names. */ constexpr static const char *long_names[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; + constexpr static const int LONG_NAMES_LEN = + sizeof(short_names) / sizeof(short_names[0]); /** The month as underlying_type. */ underlying_type m_month; @@ -982,8 +987,14 @@ constexpr ymd_date mjd2ymd(long mjd) noexcept { */ class modified_julian_day { public: - /** MJDs are represented as long ints. */ - typedef long underlying_type; + /** Decide on the INT (underlying) type of modified_julian_day. + * + * underlying_type should be at least as long as int_32_t. Else + * underlying_type will be a long. + * A 32bit int, has a range of -2147483648 to 2147483647. Should be enough! + */ + typedef std::conditional= sizeof(int32_t), int, int64_t>::type + underlying_type; /** Is fundamental datetime type */ static constexpr bool is_dt_fundamental_type = true; diff --git a/src/dat.cpp b/src/dat.cpp index 15d7706..d270c4e 100644 --- a/src/dat.cpp +++ b/src/dat.cpp @@ -4,12 +4,19 @@ #include #include +namespace { +/** just so we do not have magic numbers */ +constexpr const int MONTHS_IN_YEAR = 12; +} /* unnamed namespace */ + namespace calendar_dat { /** Dates and Delta(AT)s */ struct change { int iyear, month, delat; /** Combine year and month to form a date-ordered integer */ - int ordered_int() const noexcept { return 12 * iyear + month; } + [[nodiscard]] int ordered_int() const noexcept { + return MONTHS_IN_YEAR * iyear + month; + } }; constexpr const std::array changes = { {{1972, 1, 10}, {1972, 7, 11}, {1973, 1, 12}, {1974, 1, 13}, {1975, 1, 14}, @@ -42,7 +49,7 @@ int dso::dat(dso::year iy, dso::month im) noexcept { assert(iy >= dso::year(1972)); /* Combine year and month to form a date-ordered integer... */ - int m = 12 * iy.as_underlying_type() + im.as_underlying_type(); + int m = MONTHS_IN_YEAR * iy.as_underlying_type() + im.as_underlying_type(); /* ...and use it to find the preceding table entry. */ auto it = std::find_if( diff --git a/src/datetime_io_core.cpp b/src/datetime_io_core.cpp index 9e1bdb1..dfb80a6 100644 --- a/src/datetime_io_core.cpp +++ b/src/datetime_io_core.cpp @@ -3,6 +3,11 @@ #include #include +namespace { + /** just so it is not a magic number */ + constexpr const int MONTHS_IN_YEAR = 12; +}/* unnamed namespace */ + inline const char *skipws(const char *line) noexcept { const char *c = line; while (*c && (*c == ' ' || *c == '/' || *c == '-' || *c == 'T' || *c == ':' || @@ -31,11 +36,10 @@ inline int count_decimal_digits(const char *fltstr) noexcept { const char *dgtc = fltstr; while (std::isdigit(*dgtc)) ++dgtc; - return (dgtc - fltstr); - } else { - /* no decimal digits */ - return 0; + return (int)(dgtc - fltstr); } + /* no decimal digits */ + return 0; } int dso::datetime_io_core::get_one_int(const char *str, int *ints, @@ -151,7 +155,7 @@ int dso::datetime_io_core::get_two_ints_double(const char *str, int *ints, /* before parsing the next floating point number, count its decimal digits * If more than nanoseconds, issue a warning */ - if (count_decimal_digits(skipws(c)) > 12) { + if (count_decimal_digits(skipws(c)) > MONTHS_IN_YEAR) { fprintf(stderr, "[WARNING] Reading in date with resolution larger than " "nanoseconds will lead to loss of precision!\n"); fprintf(stderr, diff --git a/src/modified_julian_day.cpp b/src/modified_julian_day.cpp index f19ac35..4c0ea73 100644 --- a/src/modified_julian_day.cpp +++ b/src/modified_julian_day.cpp @@ -1,12 +1,17 @@ #include "dtfund.hpp" +namespace { + /** avoid magic numbers */ + constexpr const int DAYS_IN_YEAR = 365; +} /* unnamed namespace */ + dso::ydoy_date dso::modified_julian_day::to_ydoy() const noexcept { - const long days_fr_jan1_1901 = m_mjd - dso::JAN11901; - const long num_four_yrs = days_fr_jan1_1901 / 1461L; - const long years_so_far = 1901L + 4 * num_four_yrs; - const long days_left = days_fr_jan1_1901 - 1461 * num_four_yrs; - const long delta_yrs = days_left / 365 - days_left / 1460; + const int days_fr_jan1_1901 = m_mjd - dso::JAN11901; + const int num_four_yrs = days_fr_jan1_1901 / 1461; + const int years_so_far = 1901 + 4 * num_four_yrs; + const int days_left = days_fr_jan1_1901 - 1461 * num_four_yrs; + const int delta_yrs = days_left / DAYS_IN_YEAR - days_left / 1460; - return dso::ydoy_date(dso::year(years_so_far + delta_yrs), - dso::day_of_year(days_left - 365 * delta_yrs + 1)); + return {dso::year(years_so_far + delta_yrs), + dso::day_of_year(days_left - DAYS_IN_YEAR * delta_yrs + 1)}; } diff --git a/src/strmonth.cpp b/src/strmonth.cpp index 68144e9..d8d158e 100644 --- a/src/strmonth.cpp +++ b/src/strmonth.cpp @@ -9,14 +9,14 @@ dso::month::month(const char *str) : m_month(0) { if (std::strlen(str) == 3) { - for (int i = 0; i < 12; i++) { + for (int i = 0; i < SHORT_NAMES_LEN; i++) { if (!strcasecmp(str, short_names[i])) { m_month = i + 1; break; } } } else if (std::strlen(str) > 3) { - for (int i = 0; i < 12; ++i) { + for (int i = 0; i < LONG_NAMES_LEN; ++i) { if (!strcasecmp(str, long_names[i])) { m_month = i + 1; break; diff --git a/src/utc2tai.cpp b/src/utc2tai.cpp index 8a7c6d5..9340345 100644 --- a/src/utc2tai.cpp +++ b/src/utc2tai.cpp @@ -1,13 +1,13 @@ #include "tpdate.hpp" dso::TwoPartDate dso::TwoPartDateUTC::utc2tai() const noexcept { - FDOUBLE taisec; + FDOUBLE taisec=0e0; int taimjd = this->utc2tai(taisec); return dso::TwoPartDate(taimjd, dso::FractionalSeconds{taisec}); } dso::TwoPartDate dso::TwoPartDateUTC::utc2tt() const noexcept { - FDOUBLE ttsec; + FDOUBLE ttsec=0e0; int ttmjd = this->utc2tai(ttsec); return dso::TwoPartDate(ttmjd, dso::FractionalSeconds{ttsec}); } diff --git a/src/ydoy_date.cpp b/src/ydoy_date.cpp index 10252e2..0b79195 100644 --- a/src/ydoy_date.cpp +++ b/src/ydoy_date.cpp @@ -1,7 +1,7 @@ #include "dtfund.hpp" dso::ymd_date dso::ydoy_date::to_ymd() const noexcept { - int guess = static_cast(__doy.as_underlying_type() * 0.032); + int guess = __doy.as_underlying_type() * 0.032; int leap = yr().is_leap(); int more = ((dy().as_underlying_type() - dso::core::month_day[leap][guess + 1]) > 0); diff --git a/src/ymd_date.cpp b/src/ymd_date.cpp index a33807f..a4913e7 100644 --- a/src/ymd_date.cpp +++ b/src/ymd_date.cpp @@ -23,6 +23,6 @@ dso::ydoy_date dso::ymd_date::to_ydoy() const { } int leap = yr().is_leap(); int md = mn().as_underlying_type() - 1; - return dso::ydoy_date(yr(), dso::day_of_year(core::month_day[leap][md] + - dm().as_underlying_type())); + return {yr(), dso::day_of_year(core::month_day[leap][md] + + dm().as_underlying_type())}; }