From 9adb1c023571ff2c432757a9587b8d3dabcf2b34 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 7 May 2020 09:43:41 +0200 Subject: [PATCH 1/3] :checkered_flag: do not include unless old MSVC is used #2089 --- include/nlohmann/detail/boolean_operators.hpp | 12 ++ .../nlohmann/detail/conversions/from_json.hpp | 2 +- .../nlohmann/detail/conversions/to_chars.hpp | 3 +- .../nlohmann/detail/conversions/to_json.hpp | 2 +- .../nlohmann/detail/iterators/iter_impl.hpp | 2 +- include/nlohmann/detail/meta/cpp_future.hpp | 3 +- include/nlohmann/detail/meta/type_traits.hpp | 2 +- include/nlohmann/detail/output/serializer.hpp | 2 +- include/nlohmann/detail/value_t.hpp | 3 +- include/nlohmann/json.hpp | 2 +- single_include/nlohmann/json.hpp | 106 +++++++++++------- 11 files changed, 87 insertions(+), 52 deletions(-) create mode 100644 include/nlohmann/detail/boolean_operators.hpp diff --git a/include/nlohmann/detail/boolean_operators.hpp b/include/nlohmann/detail/boolean_operators.hpp new file mode 100644 index 00000000..ed4b0435 --- /dev/null +++ b/include/nlohmann/detail/boolean_operators.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include + +// Header is needed for older MSVC versions to allow to use the +// alternative operator representations "and", "or", and "not". As the header +// is removed in C++20, we must only include it for old MSVC versions. +// See for more information. + +#if !JSON_HEDLEY_MSVC_VERSION_CHECK(15,5,0) + #include // and, not, or +#endif diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index c389dca7..d6e02798 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -2,7 +2,6 @@ #include // transform #include // array -#include // and, not #include // forward_list #include // inserter, front_inserter, end #include // map @@ -13,6 +12,7 @@ #include // pair, declval #include // valarray +#include #include #include #include diff --git a/include/nlohmann/detail/conversions/to_chars.hpp b/include/nlohmann/detail/conversions/to_chars.hpp index aed587cc..5851fd0a 100644 --- a/include/nlohmann/detail/conversions/to_chars.hpp +++ b/include/nlohmann/detail/conversions/to_chars.hpp @@ -2,12 +2,13 @@ #include // array #include // assert -#include // or, and, not #include // signbit, isfinite #include // intN_t, uintN_t #include // memcpy, memmove #include // numeric_limits #include // conditional + +#include #include namespace nlohmann diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index 10b21d16..7ff1b282 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -1,7 +1,6 @@ #pragma once #include // copy -#include // or, and, not #include // begin, end #include // string #include // tuple, get @@ -10,6 +9,7 @@ #include // valarray #include // vector +#include #include #include #include diff --git a/include/nlohmann/detail/iterators/iter_impl.hpp b/include/nlohmann/detail/iterators/iter_impl.hpp index 3a362971..0b79202a 100644 --- a/include/nlohmann/detail/iterators/iter_impl.hpp +++ b/include/nlohmann/detail/iterators/iter_impl.hpp @@ -1,9 +1,9 @@ #pragma once -#include // not #include // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next #include // conditional, is_const, remove_const +#include #include #include #include diff --git a/include/nlohmann/detail/meta/cpp_future.hpp b/include/nlohmann/detail/meta/cpp_future.hpp index 948cd4fb..11f88acc 100644 --- a/include/nlohmann/detail/meta/cpp_future.hpp +++ b/include/nlohmann/detail/meta/cpp_future.hpp @@ -1,9 +1,10 @@ #pragma once -#include // not #include // size_t #include // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type +#include + namespace nlohmann { namespace detail diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index 13e92cb4..3b93fa8b 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -1,10 +1,10 @@ #pragma once -#include // not #include // numeric_limits #include // false_type, is_constructible, is_integral, is_same, true_type #include // declval +#include #include #include #include diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index f83f4a37..9dbb157b 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -3,7 +3,6 @@ #include // reverse, remove, fill, find, none_of #include // array #include // assert -#include // and, or #include // localeconv, lconv #include // labs, isfinite, isnan, signbit #include // size_t, ptrdiff_t @@ -14,6 +13,7 @@ #include // is_same #include // move +#include #include #include #include diff --git a/include/nlohmann/detail/value_t.hpp b/include/nlohmann/detail/value_t.hpp index 86b4630b..7580d856 100644 --- a/include/nlohmann/detail/value_t.hpp +++ b/include/nlohmann/detail/value_t.hpp @@ -1,11 +1,12 @@ #pragma once #include // array -#include // and #include // size_t #include // uint8_t #include // string +#include + namespace nlohmann { namespace detail diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 237a48b1..0008f18d 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -36,7 +36,6 @@ SOFTWARE. #include // all_of, find, for_each #include // assert -#include // and, not, or #include // nullptr_t, ptrdiff_t, size_t #include // hash, less #include // initializer_list @@ -49,6 +48,7 @@ SOFTWARE. #include // vector #include +#include #include #include #include diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f04091d0..6eeef012 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -36,7 +36,6 @@ SOFTWARE. #include // all_of, find, for_each #include // assert -#include // and, not, or #include // nullptr_t, ptrdiff_t, size_t #include // hash, less #include // initializer_list @@ -58,7 +57,6 @@ SOFTWARE. #include // transform #include // array -#include // and, not #include // forward_list #include // inserter, front_inserter, end #include // map @@ -69,42 +67,9 @@ SOFTWARE. #include // pair, declval #include // valarray -// #include +// #include -#include // exception -#include // runtime_error -#include // to_string - -// #include - - -#include // size_t - -namespace nlohmann -{ -namespace detail -{ -/// struct to capture the start position of the current token -struct position_t -{ - /// the total number of characters read - std::size_t chars_read_total = 0; - /// the number of characters read in the current line - std::size_t chars_read_current_line = 0; - /// the number of lines read - std::size_t lines_read = 0; - - /// conversion to size_t to preserve SAX interface - constexpr operator size_t() const - { - return chars_read_total; - } -}; - -} // namespace detail -} // namespace nlohmann - // #include @@ -2142,6 +2107,49 @@ JSON_HEDLEY_DIAGNOSTIC_POP AllocatorType, JSONSerializer, BinaryType> +#if !JSON_HEDLEY_MSVC_VERSION_CHECK(15,5,0) + #include // and, not, or +#endif + +// #include + + +#include // exception +#include // runtime_error +#include // to_string + +// #include + + +#include // size_t + +namespace nlohmann +{ +namespace detail +{ +/// struct to capture the start position of the current token +struct position_t +{ + /// the total number of characters read + std::size_t chars_read_total = 0; + /// the number of characters read in the current line + std::size_t chars_read_current_line = 0; + /// the number of lines read + std::size_t lines_read = 0; + + /// conversion to size_t to preserve SAX interface + constexpr operator size_t() const + { + return chars_read_total; + } +}; + +} // namespace detail +} // namespace nlohmann + +// #include + + namespace nlohmann { namespace detail @@ -2495,10 +2503,12 @@ class other_error : public exception // #include -#include // not #include // size_t #include // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type +// #include + + namespace nlohmann { namespace detail @@ -2560,11 +2570,12 @@ constexpr T static_const::value; // #include -#include // not #include // numeric_limits #include // false_type, is_constructible, is_integral, is_same, true_type #include // declval +// #include + // #include @@ -3145,11 +3156,13 @@ struct is_constructible_tuple> : conjunction // array -#include // and #include // size_t #include // uint8_t #include // string +// #include + + namespace nlohmann { namespace detail @@ -3599,7 +3612,6 @@ constexpr const auto& from_json = detail::static_const::va #include // copy -#include // or, and, not #include // begin, end #include // string #include // tuple, get @@ -3608,6 +3620,8 @@ constexpr const auto& from_json = detail::static_const::va #include // valarray #include // vector +// #include + // #include @@ -4192,6 +4206,8 @@ struct adl_serializer } // namespace nlohmann +// #include + // #include // #include @@ -10034,10 +10050,11 @@ template struct internal_iterator // #include -#include // not #include // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next #include // conditional, is_const, remove_const +// #include + // #include // #include @@ -13543,7 +13560,6 @@ class binary_writer #include // reverse, remove, fill, find, none_of #include // array #include // assert -#include // and, or #include // localeconv, lconv #include // labs, isfinite, isnan, signbit #include // size_t, ptrdiff_t @@ -13554,17 +13570,21 @@ class binary_writer #include // is_same #include // move +// #include + // #include #include // array #include // assert -#include // or, and, not #include // signbit, isfinite #include // intN_t, uintN_t #include // memcpy, memmove #include // numeric_limits #include // conditional + +// #include + // #include From 8c2b26de4a9886751b8c7d88b788dc33c515f9f0 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 7 May 2020 10:17:32 +0200 Subject: [PATCH 2/3] :alembic: do not include with C++20 #2089 --- include/nlohmann/detail/boolean_operators.hpp | 8 +- single_include/nlohmann/json.hpp | 86 +++++++++---------- 2 files changed, 45 insertions(+), 49 deletions(-) diff --git a/include/nlohmann/detail/boolean_operators.hpp b/include/nlohmann/detail/boolean_operators.hpp index ed4b0435..06335866 100644 --- a/include/nlohmann/detail/boolean_operators.hpp +++ b/include/nlohmann/detail/boolean_operators.hpp @@ -1,12 +1,8 @@ #pragma once -#include - -// Header is needed for older MSVC versions to allow to use the -// alternative operator representations "and", "or", and "not". As the header -// is removed in C++20, we must only include it for old MSVC versions. +// Header is removed in C++20. // See for more information. -#if !JSON_HEDLEY_MSVC_VERSION_CHECK(15,5,0) +#if __cplusplus <= 201703L #include // and, not, or #endif diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 6eeef012..b1ed374f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -70,6 +70,49 @@ SOFTWARE. // #include +// Header is removed in C++20. +// See for more information. + +#if __cplusplus <= 201703L + #include // and, not, or +#endif + +// #include + + +#include // exception +#include // runtime_error +#include // to_string + +// #include + + +#include // size_t + +namespace nlohmann +{ +namespace detail +{ +/// struct to capture the start position of the current token +struct position_t +{ + /// the total number of characters read + std::size_t chars_read_total = 0; + /// the number of characters read in the current line + std::size_t chars_read_current_line = 0; + /// the number of lines read + std::size_t lines_read = 0; + + /// conversion to size_t to preserve SAX interface + constexpr operator size_t() const + { + return chars_read_total; + } +}; + +} // namespace detail +} // namespace nlohmann + // #include @@ -2107,49 +2150,6 @@ JSON_HEDLEY_DIAGNOSTIC_POP AllocatorType, JSONSerializer, BinaryType> -#if !JSON_HEDLEY_MSVC_VERSION_CHECK(15,5,0) - #include // and, not, or -#endif - -// #include - - -#include // exception -#include // runtime_error -#include // to_string - -// #include - - -#include // size_t - -namespace nlohmann -{ -namespace detail -{ -/// struct to capture the start position of the current token -struct position_t -{ - /// the total number of characters read - std::size_t chars_read_total = 0; - /// the number of characters read in the current line - std::size_t chars_read_current_line = 0; - /// the number of lines read - std::size_t lines_read = 0; - - /// conversion to size_t to preserve SAX interface - constexpr operator size_t() const - { - return chars_read_total; - } -}; - -} // namespace detail -} // namespace nlohmann - -// #include - - namespace nlohmann { namespace detail From 28e20bd9e40387574479f753cd46ff66ed8aa21d Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 16 May 2020 14:26:17 +0200 Subject: [PATCH 3/3] :rotating_light: fix warnings #2113 --- Makefile | 14 +++++++++----- include/nlohmann/detail/input/binary_reader.hpp | 2 +- .../nlohmann/detail/output/binary_writer.hpp | 6 +++--- single_include/nlohmann/json.hpp | 8 ++++---- test/src/unit-cbor.cpp | 8 ++++---- test/src/unit-noexcept.cpp | 17 ++++++++++++++++- test/src/unit-udt.cpp | 10 +++++++++- test/thirdparty/doctest/doctest.h | 2 +- 8 files changed, 47 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index a2884812..08c40d2f 100644 --- a/Makefile +++ b/Makefile @@ -102,7 +102,8 @@ doctest: # -Wno-switch-enum -Wno-covered-switch-default: pedantic/contradicting warnings about switches # -Wno-weak-vtables: exception class is defined inline, but has virtual method pedantic_clang: - $(MAKE) json_unit CXX=c++ CXXFLAGS=" \ + rm -fr build_pedantic + CXXFLAGS=" \ -std=c++11 -Wno-c++98-compat -Wno-c++98-compat-pedantic \ -Werror \ -Weverything \ @@ -115,11 +116,13 @@ pedantic_clang: -Wno-padded \ -Wno-range-loop-analysis \ -Wno-switch-enum -Wno-covered-switch-default \ - -Wno-weak-vtables" + -Wno-weak-vtables" cmake -S . -B build_pedantic -GNinja -DCMAKE_BUILD_TYPE=Debug -DJSON_MultipleHeaders=ON + cmake --build build_pedantic # calling GCC with most warnings pedantic_gcc: - $(MAKE) json_unit CXX=/usr/local/bin/g++-9 CXXFLAGS=" \ + rm -fr build_pedantic + CXXFLAGS=" \ -std=c++11 \ -Waddress \ -Waddress-of-packed-member \ @@ -233,7 +236,7 @@ pedantic_gcc: -Wno-system-headers \ -Wno-templates \ -Wno-undef \ - -Wnoexcept \ + -Wno-noexcept \ -Wnoexcept-type \ -Wnon-template-friend \ -Wnon-virtual-dtor \ @@ -340,7 +343,8 @@ pedantic_gcc: -Wvolatile-register-var \ -Wwrite-strings \ -Wzero-as-null-pointer-constant \ - " + " cmake -S . -B build_pedantic -GNinja -DCMAKE_BUILD_TYPE=Debug -DJSON_MultipleHeaders=ON + cmake --build build_pedantic ########################################################################## # benchmarks diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 5f497089..a40693a0 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -32,7 +32,7 @@ namespace detail @note from https://stackoverflow.com/a/1001328/266378 */ -static bool little_endianess(int num = 1) noexcept +static inline bool little_endianess(int num = 1) noexcept { return *reinterpret_cast(&num) == 1; } diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 9c6e615b..c4cf618c 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -194,9 +194,9 @@ class binary_writer } else { - if (j.m_value.number_float >= std::numeric_limits::lowest() and - j.m_value.number_float <= std::numeric_limits::max() and - static_cast(static_cast(j.m_value.number_float)) == j.m_value.number_float) + if (static_cast(j.m_value.number_float) >= static_cast(std::numeric_limits::lowest()) and + static_cast(j.m_value.number_float) <= static_cast(std::numeric_limits::max()) and + static_cast(static_cast(j.m_value.number_float)) == static_cast(j.m_value.number_float)) { oa->write_character(get_cbor_float_prefix(static_cast(j.m_value.number_float))); write_number(static_cast(j.m_value.number_float)); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index c5ef3f7f..4db36ef2 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5596,7 +5596,7 @@ namespace detail @note from https://stackoverflow.com/a/1001328/266378 */ -static bool little_endianess(int num = 1) noexcept +static inline bool little_endianess(int num = 1) noexcept { return *reinterpret_cast(&num) == 1; } @@ -12188,9 +12188,9 @@ class binary_writer } else { - if (j.m_value.number_float >= std::numeric_limits::lowest() and - j.m_value.number_float <= std::numeric_limits::max() and - static_cast(static_cast(j.m_value.number_float)) == j.m_value.number_float) + if (static_cast(j.m_value.number_float) >= static_cast(std::numeric_limits::lowest()) and + static_cast(j.m_value.number_float) <= static_cast(std::numeric_limits::max()) and + static_cast(static_cast(j.m_value.number_float)) == static_cast(j.m_value.number_float)) { oa->write_character(get_cbor_float_prefix(static_cast(j.m_value.number_float))); write_number(static_cast(j.m_value.number_float)); diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index 28003258..2a53bb52 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -939,7 +939,7 @@ TEST_CASE("CBOR") } SECTION("-3.40282e+38(lowest float)") { - double v = std::numeric_limits::lowest(); + double v = static_cast(std::numeric_limits::lowest()); json j = v; std::vector expected = { @@ -953,7 +953,7 @@ TEST_CASE("CBOR") } SECTION("1 + 3.40282e+38(more than max float)") { - double v = std::numeric_limits::max() + 0.1e+34; + double v = static_cast(std::numeric_limits::max()) + 0.1e+34; json j = v; std::vector expected = { @@ -968,7 +968,7 @@ TEST_CASE("CBOR") } SECTION("-1 - 3.40282e+38(less than lowest float)") { - double v = std::numeric_limits::lowest() - 1; + double v = static_cast(std::numeric_limits::lowest()) - 1.0; json j = v; std::vector expected = { @@ -1582,7 +1582,7 @@ TEST_CASE("CBOR") auto j = json::from_cbor(input); CHECK(j.is_binary()); auto k = json::binary_array({0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x99}); - CAPTURE(j.dump(0, ' ', false, json::error_handler_t::strict, true)); + CAPTURE(j.dump(0, ' ', false, json::error_handler_t::strict, true)) CHECK(j == k); } diff --git a/test/src/unit-noexcept.cpp b/test/src/unit-noexcept.cpp index 8fbe9f91..6952c0ee 100644 --- a/test/src/unit-noexcept.cpp +++ b/test/src/unit-noexcept.cpp @@ -46,7 +46,12 @@ void to_json(json&, pod) noexcept; void to_json(json&, pod_bis); void from_json(const json&, pod) noexcept; void from_json(const json&, pod_bis); -static json* j; +void to_json(json&, pod) noexcept {} +void to_json(json&, pod_bis) {} +void from_json(const json&, pod) noexcept {} +void from_json(const json&, pod_bis) {} + +static json* j = nullptr; static_assert(noexcept(json{}), ""); static_assert(noexcept(nlohmann::to_json(*j, 2)), ""); @@ -79,4 +84,14 @@ TEST_CASE("runtime checks") CHECK(std::is_nothrow_copy_constructible::value == std::is_nothrow_copy_constructible::value); CHECK(std::is_nothrow_copy_constructible::value == std::is_nothrow_copy_constructible::value); } + + SECTION("silence -Wunneeded-internal-declaration errors") + { + j = nullptr; + json j2; + to_json(j2, pod()); + to_json(j2, pod_bis()); + from_json(j2, pod()); + from_json(j2, pod_bis()); + } } diff --git a/test/src/unit-udt.cpp b/test/src/unit-udt.cpp index b1da8c37..cd9ce69f 100644 --- a/test/src/unit-udt.cpp +++ b/test/src/unit-udt.cpp @@ -120,6 +120,8 @@ static void to_json(BasicJsonType& j, country c) case country::russia: j = u8"Российская Федерация"; return; + default: + break; } } @@ -803,7 +805,9 @@ class Evil public: Evil() = default; template - Evil(T) {} + Evil(T t) : m_i(sizeof(t)) {} + + int m_i = 0; }; void from_json(const json&, Evil&) {} @@ -816,6 +820,10 @@ TEST_CASE("Issue #924") CHECK_NOTHROW(j.get()); CHECK_NOTHROW(j.get>()); + + // silence Wunused-template warnings + Evil e(1); + CHECK(e.m_i >= 0); } TEST_CASE("Issue #1237") diff --git a/test/thirdparty/doctest/doctest.h b/test/thirdparty/doctest/doctest.h index 4f3a0e33..2f0ff213 100755 --- a/test/thirdparty/doctest/doctest.h +++ b/test/thirdparty/doctest/doctest.h @@ -2913,7 +2913,7 @@ typedef timer_large_integer::type ticks_t; //unsigned int getElapsedMilliseconds() const { // return static_cast(getElapsedMicroseconds() / 1000); //} - double getElapsedSeconds() const { return (getCurrentTicks() - m_ticks) / 1000000.0; } + double getElapsedSeconds() const { return static_cast((getCurrentTicks() - m_ticks)) / 1000000.0; } private: ticks_t m_ticks = 0;