From 3a1403409fc12d042608b381e891ca377de300b6 Mon Sep 17 00:00:00 2001 From: Trevor Welsby Date: Sat, 23 Jan 2016 17:45:30 +1000 Subject: [PATCH 1/4] Issue #185 - remove approx() and use #pragma to kill warnings --- src/json.hpp | 27 +++++++++++++++------------ src/json.hpp.re2c | 27 +++++++++++++++------------ test/unit.cpp | 5 +++++ 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index ac362dcfe..3af7dbf9c 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -65,6 +65,12 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation. #endif #endif +// disable float-equal warnings on GCC/clang +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wfloat-equal" +#endif + // enable ssize_t for MSVC #ifdef _MSC_VER #include @@ -100,12 +106,6 @@ struct has_mapped_type static constexpr bool value = sizeof(test(0)) == 1; }; -/// "equality" comparison for floating point numbers -template -static bool approx(const T a, const T b) -{ - return not (a > b or a < b); -} } /*! @@ -4785,7 +4785,7 @@ class basic_json } case value_t::number_float: { - return approx(lhs.m_value.number_float, rhs.m_value.number_float); + return lhs.m_value.number_float == rhs.m_value.number_float; } default: { @@ -4795,13 +4795,11 @@ class basic_json } else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float) { - return approx(static_cast(lhs.m_value.number_integer), - rhs.m_value.number_float); + return static_cast(lhs.m_value.number_integer == rhs.m_value.number_float); } else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer) { - return approx(lhs.m_value.number_float, - static_cast(rhs.m_value.number_integer)); + return lhs.m_value.number_float == static_cast(rhs.m_value.number_integer); } return false; } @@ -7560,7 +7558,7 @@ basic_json_parser_64: // check if conversion loses precision const auto int_val = static_cast(float_val); - if (approx(float_val, static_cast(int_val))) + if (float_val == static_cast(int_val)) { // we would not lose precision -> return int result.m_type = value_t::number_integer; @@ -7705,4 +7703,9 @@ inline nlohmann::json operator "" _json(const char* s, std::size_t) return nlohmann::json::parse(reinterpret_cast(s)); } +// restore GCC/clang diagnostic settings +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic pop +#endif + #endif diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index c7ee44f53..bacc3febc 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -65,6 +65,12 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation. #endif #endif +// disable float-equal warnings on GCC/clang +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wfloat-equal" +#endif + // enable ssize_t for MSVC #ifdef _MSC_VER #include @@ -100,12 +106,6 @@ struct has_mapped_type static constexpr bool value = sizeof(test(0)) == 1; }; -/// "equality" comparison for floating point numbers -template -static bool approx(const T a, const T b) -{ - return not (a > b or a < b); -} } /*! @@ -4785,7 +4785,7 @@ class basic_json } case value_t::number_float: { - return approx(lhs.m_value.number_float, rhs.m_value.number_float); + return lhs.m_value.number_float == rhs.m_value.number_float; } default: { @@ -4795,13 +4795,11 @@ class basic_json } else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float) { - return approx(static_cast(lhs.m_value.number_integer), - rhs.m_value.number_float); + return static_cast(lhs.m_value.number_integer == rhs.m_value.number_float); } else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer) { - return approx(lhs.m_value.number_float, - static_cast(rhs.m_value.number_integer)); + return lhs.m_value.number_float == static_cast(rhs.m_value.number_integer); } return false; } @@ -7242,7 +7240,7 @@ class basic_json // check if conversion loses precision const auto int_val = static_cast(float_val); - if (approx(float_val, static_cast(int_val))) + if (float_val == static_cast(int_val)) { // we would not lose precision -> return int result.m_type = value_t::number_integer; @@ -7387,4 +7385,9 @@ inline nlohmann::json operator "" _json(const char* s, std::size_t) return nlohmann::json::parse(reinterpret_cast(s)); } +// restore GCC/clang diagnostic settings +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic pop +#endif + #endif diff --git a/test/unit.cpp b/test/unit.cpp index 8b3bc19b2..8f12eca06 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -25,6 +25,11 @@ #include "json.hpp" using nlohmann::json; +// disable float-equal warnings on GCC/clang +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) +#pragma GCC diagnostic ignored "-Wfloat-equal" +#endif + TEST_CASE("constructors") { SECTION("create an empty value with a given type") From 8a0490a011d806e278b5464c459d277c524337ed Mon Sep 17 00:00:00 2001 From: Niels Date: Sun, 24 Jan 2016 13:04:55 +0100 Subject: [PATCH 2/4] cleanup --- README.md | 2 +- test/unit.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b985bfa4d..6d45c2a21 100644 --- a/README.md +++ b/README.md @@ -388,7 +388,7 @@ I deeply appreciate the help of the following people. - [406345](https://github.com/406345) fixed two small warnings. - [Glen Fernandes](https://github.com/glenfe) noted a potential portability problem in the `has_mapped_type` function. - [Corbin Hughes](https://github.com/nibroc) fixed some typos in the contribution guidelines. -- [twelsby](https://github.com/twelsby) fixed the array subscript operator. +- [twelsby](https://github.com/twelsby) fixed the array subscript operator and an issue that failed the MSVC build. Thanks a lot for helping out! diff --git a/test/unit.cpp b/test/unit.cpp index 949788e93..94b5084c0 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -27,7 +27,7 @@ using nlohmann::json; // disable float-equal warnings on GCC/clang #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) -#pragma GCC diagnostic ignored "-Wfloat-equal" + #pragma GCC diagnostic ignored "-Wfloat-equal" #endif TEST_CASE("constructors") From d7d1e5564861d827316a9f93d31c4e390e86151f Mon Sep 17 00:00:00 2001 From: Niels Date: Sun, 24 Jan 2016 13:28:08 +0100 Subject: [PATCH 3/4] applied include-what-you-use --- src/json.hpp | 3 +++ src/json.hpp.re2c | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/json.hpp b/src/json.hpp index 8755cd33a..36280c16e 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -43,7 +43,9 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation. #include #include #include +#include #include +#include #include #include #include @@ -53,6 +55,7 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation. #include #include #include +#include #include #include #include diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 207208d4f..b88d8fb08 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -43,7 +43,9 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation. #include #include #include +#include #include +#include #include #include #include @@ -53,6 +55,7 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation. #include #include #include +#include #include #include #include From 8b6e0dc02d7f18f761ecea4db977d8bcc3e56bbf Mon Sep 17 00:00:00 2001 From: Niels Date: Sun, 24 Jan 2016 13:28:16 +0100 Subject: [PATCH 4/4] small addition --- CONTRIBUTING.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3e6258d43..0171a244b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -62,6 +62,7 @@ Please understand that I cannot accept pull requests changing only file `src/jso - The C++11 support varies between different **compilers** and versions. Please note the [list of supported compilers](https://github.com/nlohmann/json/blob/master/README.md#supported-compilers). Some compilers like GCC 4.8 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with `#ifdef`s or other means. - Specifically, I am aware of compilation problems with **Microsoft Visual Studio** (there even is an [issue label](https://github.com/nlohmann/json/issues?utf8=✓&q=label%3A%22visual+studio%22+) for these kind of bugs). I understand that even in 2016, complete C++11 support isn't there yet. But please also understand that I do not want to drop features or uglify the code just to make Microsoft's sub-standard compiler happy. The past has shown that there are ways to express the functionality such that the code compiles with the most recent MSVC - unfortunately, this is not the main objective of the project. - Please refrain from proposing changes that would **break [JSON](http://json.org) conformance**. If you propose a conformant extension of JSON to be supported by the library, please motivate this extension. +- Please do not open pull requests that address **multiple issues**. ## Wanted