From 8cec55a271d4aa7db644a0e4180643b24413d31d Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 22 Feb 2017 18:14:29 +0100 Subject: [PATCH] :construction: fixed more warnings --- Makefile | 22 ++++++++++++++++++++++ src/json.hpp | 30 ++++++++++++++++++++++-------- src/json.hpp.re2c | 30 ++++++++++++++++++++++-------- test/src/unit-allocator.cpp | 6 +++--- test/src/unit-cbor.cpp | 2 +- test/src/unit-constructor1.cpp | 2 +- test/src/unit-msgpack.cpp | 4 ++-- test/src/unit-readme.cpp | 2 +- test/src/unit-regression.cpp | 12 ++++++------ test/src/unit-testsuites.cpp | 2 ++ 10 files changed, 82 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index 8bd7a2dd2..ac75bcaf7 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,28 @@ doctest: $(MAKE) check_output -C doc +########################################################################## +# warning detector +########################################################################## + +# calling Clang with all warnings, except: +# -Wno-documentation-unknown-command: code uses user-defined commands like @complexity +# -Wno-exit-time-destructors: warning in Catch code +# -Wno-keyword-macro: unit-tests use "#define private public" +# -Wno-deprecated-declarations: some functions are deprecated until 3.0.0 +# -Wno-range-loop-analysis: iterator_wrapper tests tests "for(const auto i...)" +pedantic: + $(MAKE) json_unit CXXFLAGS="\ + -std=c++11 \ + -Werror \ + -Weverything \ + -Wno-documentation-unknown-command \ + -Wno-exit-time-destructors \ + -Wno-keyword-macro \ + -Wno-deprecated-declarations \ + -Wno-range-loop-analysis" + + ########################################################################## # fuzzing ########################################################################## diff --git a/src/json.hpp b/src/json.hpp index 6fcfc3e12..ab07ed901 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -673,7 +673,7 @@ template::value, int> = 0> void from_json(const BasicJsonType& j, UnscopedEnumType& e) { - typename std::underlying_type::type val = e; + typename std::underlying_type::type val; get_arithmetic_value(j, val); e = static_cast(val); } @@ -917,7 +917,7 @@ struct adl_serializer @ref basic_json class (either explicit or via conversion operators). @param[in] j JSON value to read from - @param[in, out] val value to write to + @param[in,out] val value to write to */ template static void from_json(BasicJsonType&& j, ValueType& val) noexcept( @@ -932,7 +932,7 @@ struct adl_serializer This function is usually called by the constructors of the @ref basic_json class. - @param[in, out] j JSON value to write to + @param[in,out] j JSON value to write to @param[in] val value to read from */ template @@ -6537,6 +6537,11 @@ class basic_json /// @{ private: + /*! + @note Some code in the switch cases has been copied, because otherwise + copilers would complain about implicit fallthrough and there is no + portable attribute to mute such warnings. + */ template static void add_to_vector(std::vector& vec, size_t bytes, const T number) { @@ -6550,20 +6555,27 @@ class basic_json vec.push_back(static_cast((static_cast(number) >> 060) & 0xff)); vec.push_back(static_cast((static_cast(number) >> 050) & 0xff)); vec.push_back(static_cast((static_cast(number) >> 040) & 0xff)); - // intentional fall-through + vec.push_back(static_cast((number >> 030) & 0xff)); + vec.push_back(static_cast((number >> 020) & 0xff)); + vec.push_back(static_cast((number >> 010) & 0xff)); + vec.push_back(static_cast(number & 0xff)); + break; } case 4: { vec.push_back(static_cast((number >> 030) & 0xff)); vec.push_back(static_cast((number >> 020) & 0xff)); - // intentional fall-through + vec.push_back(static_cast((number >> 010) & 0xff)); + vec.push_back(static_cast(number & 0xff)); + break; } case 2: { vec.push_back(static_cast((number >> 010) & 0xff)); - // intentional fall-through + vec.push_back(static_cast(number & 0xff)); + break; } case 1: @@ -7865,7 +7877,9 @@ class basic_json } else { - val = mant == 0 ? INFINITY : NAN; + val = mant == 0 + ? std::numeric_limits::infinity() + : std::numeric_limits::quiet_NaN(); } return (half & 0x8000) != 0 ? -val : val; } @@ -11181,7 +11195,7 @@ basic_json_parser_74: // of characters determined by the lexer (len) const bool ok = (endptr == (data + len)); - if (ok and (value == 0.0) and (*data == '-')) + if (ok and (value == static_cast(0.0)) and (*data == '-')) { // some implementations forget to negate the zero value = -0.0; diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index a33da4b3d..6fda82904 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -673,7 +673,7 @@ template::value, int> = 0> void from_json(const BasicJsonType& j, UnscopedEnumType& e) { - typename std::underlying_type::type val = e; + typename std::underlying_type::type val; get_arithmetic_value(j, val); e = static_cast(val); } @@ -917,7 +917,7 @@ struct adl_serializer @ref basic_json class (either explicit or via conversion operators). @param[in] j JSON value to read from - @param[in, out] val value to write to + @param[in,out] val value to write to */ template static void from_json(BasicJsonType&& j, ValueType& val) noexcept( @@ -932,7 +932,7 @@ struct adl_serializer This function is usually called by the constructors of the @ref basic_json class. - @param[in, out] j JSON value to write to + @param[in,out] j JSON value to write to @param[in] val value to read from */ template @@ -6537,6 +6537,11 @@ class basic_json /// @{ private: + /*! + @note Some code in the switch cases has been copied, because otherwise + copilers would complain about implicit fallthrough and there is no + portable attribute to mute such warnings. + */ template static void add_to_vector(std::vector& vec, size_t bytes, const T number) { @@ -6550,20 +6555,27 @@ class basic_json vec.push_back(static_cast((static_cast(number) >> 060) & 0xff)); vec.push_back(static_cast((static_cast(number) >> 050) & 0xff)); vec.push_back(static_cast((static_cast(number) >> 040) & 0xff)); - // intentional fall-through + vec.push_back(static_cast((number >> 030) & 0xff)); + vec.push_back(static_cast((number >> 020) & 0xff)); + vec.push_back(static_cast((number >> 010) & 0xff)); + vec.push_back(static_cast(number & 0xff)); + break; } case 4: { vec.push_back(static_cast((number >> 030) & 0xff)); vec.push_back(static_cast((number >> 020) & 0xff)); - // intentional fall-through + vec.push_back(static_cast((number >> 010) & 0xff)); + vec.push_back(static_cast(number & 0xff)); + break; } case 2: { vec.push_back(static_cast((number >> 010) & 0xff)); - // intentional fall-through + vec.push_back(static_cast(number & 0xff)); + break; } case 1: @@ -7865,7 +7877,9 @@ class basic_json } else { - val = mant == 0 ? INFINITY : NAN; + val = mant == 0 + ? std::numeric_limits::infinity() + : std::numeric_limits::quiet_NaN(); } return (half & 0x8000) != 0 ? -val : val; } @@ -10215,7 +10229,7 @@ class basic_json // of characters determined by the lexer (len) const bool ok = (endptr == (data + len)); - if (ok and (value == 0.0) and (*data == '-')) + if (ok and (value == static_cast(0.0)) and (*data == '-')) { // some implementations forget to negate the zero value = -0.0; diff --git a/test/src/unit-allocator.cpp b/test/src/unit-allocator.cpp index 9fc2681f4..78a481155 100644 --- a/test/src/unit-allocator.cpp +++ b/test/src/unit-allocator.cpp @@ -63,9 +63,9 @@ TEST_CASE("bad_alloc") } } -bool next_construct_fails = false; -bool next_destroy_fails = false; -bool next_deallocate_fails = false; +static bool next_construct_fails = false; +static bool next_destroy_fails = false; +static bool next_deallocate_fails = false; template struct my_allocator : std::allocator diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index b22f744e9..da5603e84 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -237,7 +237,7 @@ TEST_CASE("CBOR") const auto result = json::to_cbor(j); CHECK(result == expected); - int16_t restored = -1 - ((result[1] << 8) + result[2]); + int16_t restored = static_cast(-1 - ((result[1] << 8) + result[2])); CHECK(restored == -9263); // roundtrip diff --git a/test/src/unit-constructor1.cpp b/test/src/unit-constructor1.cpp index 93546c5f4..366667469 100644 --- a/test/src/unit-constructor1.cpp +++ b/test/src/unit-constructor1.cpp @@ -725,7 +725,7 @@ TEST_CASE("constructors") SECTION("long double") { - long double n = 42.23; + long double n = 42.23l; json j(n); CHECK(j.type() == json::value_t::number_float); CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); diff --git a/test/src/unit-msgpack.cpp b/test/src/unit-msgpack.cpp index cc459feef..2f79d31b1 100644 --- a/test/src/unit-msgpack.cpp +++ b/test/src/unit-msgpack.cpp @@ -342,7 +342,7 @@ TEST_CASE("MessagePack") const auto result = json::to_msgpack(j); CHECK(result == expected); - int16_t restored = (result[1] << 8) + result[2]; + int16_t restored = static_cast((result[1] << 8) + result[2]); CHECK(restored == -9263); // roundtrip @@ -374,7 +374,7 @@ TEST_CASE("MessagePack") // check individual bytes CHECK(result[0] == 0xd1); - int16_t restored = (result[1] << 8) + result[2]; + int16_t restored = static_cast((result[1] << 8) + result[2]); CHECK(restored == i); // roundtrip diff --git a/test/src/unit-readme.cpp b/test/src/unit-readme.cpp index 061aadb4a..954b74400 100644 --- a/test/src/unit-readme.cpp +++ b/test/src/unit-readme.cpp @@ -163,7 +163,7 @@ TEST_CASE("README", "[hide]") j.clear(); // the array is empty again // comparison - j == "[\"foo\", 1, true]"_json; // true + bool x = (j == "[\"foo\", 1, true]"_json); // true // create an object json o; diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index e476d9334..bfa4d4970 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -350,8 +350,8 @@ TEST_CASE("regression tests") // double nlohmann::basic_json j_double = - 1.23e35f; - CHECK(j_double.get() == 1.23e35f); + 1.23e35; + CHECK(j_double.get() == 1.23e35); // long double nlohmann::basic_json @@ -641,7 +641,7 @@ TEST_CASE("regression tests") CHECK_THROWS_AS(json::from_msgpack(vec1), std::out_of_range); // more test cases for MessagePack - for (uint8_t b : + for (auto b : { 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, // fixmap 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, // fixarray @@ -649,12 +649,12 @@ TEST_CASE("regression tests") 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf }) { - std::vector vec(1, b); + std::vector vec(1, static_cast(b)); CHECK_THROWS_AS(json::from_msgpack(vec), std::out_of_range); } // more test cases for CBOR - for (uint8_t b : + for (auto b : { 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, // UTF-8 string @@ -664,7 +664,7 @@ TEST_CASE("regression tests") 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7 // map }) { - std::vector vec(1, b); + std::vector vec(1, static_cast(b)); CHECK_THROWS_AS(json::from_cbor(vec), std::out_of_range); } diff --git a/test/src/unit-testsuites.cpp b/test/src/unit-testsuites.cpp index 62e7e37e9..ab1f689c7 100644 --- a/test/src/unit-testsuites.cpp +++ b/test/src/unit-testsuites.cpp @@ -816,6 +816,8 @@ TEST_CASE("nst's JSONTestSuite") } } +std::string trim(const std::string& str); + // from http://stackoverflow.com/a/25829178/266378 std::string trim(const std::string& str) {