From 7092890ff1fc6b339a22f90b879e6ffc0369ef33 Mon Sep 17 00:00:00 2001 From: Alexander Karzhenkov Date: Tue, 22 Dec 2020 18:03:50 +0500 Subject: [PATCH 001/143] Properly select the build type for Travis --- .travis.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index f48ee1fd6d..4389c02334 100644 --- a/.travis.yml +++ b/.travis.yml @@ -324,10 +324,13 @@ script: # append CXX_STANDARD to CMAKE_OPTIONS if required - CMAKE_OPTIONS+=${CXX_STANDARD:+ -DCMAKE_CXX_STANDARD=$CXX_STANDARD -DCMAKE_CXX_STANDARD_REQUIRED=ON} + # build configuration + - CMAKE_OPTIONS+=" -DCMAKE_BUILD_TYPE=Debug" + # compile and execute unit tests - mkdir -p build && cd build - - cmake .. ${CMAKE_OPTIONS} -DJSON_MultipleHeaders=${MULTIPLE_HEADERS} -DJSON_ImplicitConversions=${IMPLICIT_CONVERSIONS} -DJSON_BuildTests=On -GNinja && cmake --build . --config Release - - ctest -C Release --timeout 2700 -V -j + - cmake .. ${CMAKE_OPTIONS} -DJSON_MultipleHeaders=${MULTIPLE_HEADERS} -DJSON_ImplicitConversions=${IMPLICIT_CONVERSIONS} -DJSON_BuildTests=On -GNinja && cmake --build . + - ctest --timeout 2700 -V -j - cd .. # check if homebrew works (only checks develop branch) From 10db9184a3d76d6047f9806f047ea3d58ed5c5b9 Mon Sep 17 00:00:00 2001 From: Alexander Karzhenkov Date: Wed, 23 Dec 2020 20:55:02 +0500 Subject: [PATCH 002/143] Configure Travis to build "Release" instead of "Debug" --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4389c02334..a29265a8e8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -325,7 +325,7 @@ script: - CMAKE_OPTIONS+=${CXX_STANDARD:+ -DCMAKE_CXX_STANDARD=$CXX_STANDARD -DCMAKE_CXX_STANDARD_REQUIRED=ON} # build configuration - - CMAKE_OPTIONS+=" -DCMAKE_BUILD_TYPE=Debug" + - CMAKE_OPTIONS+=" -DCMAKE_BUILD_TYPE=Release" # compile and execute unit tests - mkdir -p build && cd build From 903b8a6e04fc99687599b446a05ec0b8569e0a55 Mon Sep 17 00:00:00 2001 From: Alexander Karzhenkov Date: Sat, 26 Dec 2020 21:14:16 +0500 Subject: [PATCH 003/143] Build as "Debug" for coverage test --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index a29265a8e8..e015ac45a8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -97,6 +97,7 @@ matrix: - COMPILER=g++-7 - CMAKE_OPTIONS=-DJSON_Coverage=ON - MULTIPLE_HEADERS=ON + - BUILD_TYPE=Debug # Coverity (only for branch coverity_scan) @@ -324,8 +325,8 @@ script: # append CXX_STANDARD to CMAKE_OPTIONS if required - CMAKE_OPTIONS+=${CXX_STANDARD:+ -DCMAKE_CXX_STANDARD=$CXX_STANDARD -DCMAKE_CXX_STANDARD_REQUIRED=ON} - # build configuration - - CMAKE_OPTIONS+=" -DCMAKE_BUILD_TYPE=Release" + # build configuration (Release by default) + - CMAKE_OPTIONS+=" -DCMAKE_BUILD_TYPE=${BUILD_TYPE:-Release}" # compile and execute unit tests - mkdir -p build && cd build From a4d491e22d9d8e67aa47b7cf8011097d4ff673d9 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 1 Jan 2021 17:23:10 +0100 Subject: [PATCH 004/143] :construction: better diagnostics --- include/nlohmann/json.hpp | 112 ++++++++++++++++++++++++++++++- single_include/nlohmann/json.hpp | 112 ++++++++++++++++++++++++++++++- 2 files changed, 222 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 8c9bef03df..a827b037b6 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1642,6 +1642,9 @@ class basic_json std::for_each(init.begin(), init.end(), [this](const detail::json_ref& element_ref) { auto element = element_ref.moved_or_copied(); +#ifdef JSON_DIAGNOSTICS + (*element.m_value.array)[1].m_parent = this; +#endif m_value.object->emplace( std::move(*((*element.m_value.array)[0].m_value.string)), std::move((*element.m_value.array)[1])); @@ -1652,6 +1655,12 @@ class basic_json // the initializer list describes an array -> create array m_type = value_t::array; m_value.array = create(init.begin(), init.end()); +#ifdef JSON_DIAGNOSTICS + for (auto& element : *m_value.array) + { + element.m_parent = this; + } +#endif } assert_invariant(); @@ -2696,6 +2705,49 @@ class basic_json /// @} private: +#ifdef JSON_DIAGNOSTICS + std::string diagnostics() + { + std::string result; + for (basic_json* current = this; current->m_parent != nullptr; current = current->m_parent) + { + switch (current->m_parent->type()) + { + case value_t::array: + { + for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) + { + if (current->m_parent->m_value.array->operator[](i) == *current) + { + result = "/" + std::to_string(i) + result; + continue; + } + } + break; + } + + case value_t::object: + { + for (auto it : *current->m_parent->m_value.object) + { + if (it.second == *current) + { + result = "/" + it.first + result; + continue; + } + } + break; + } + + default: + break; + } + } + + return result; + } +#endif + ////////////////// // value access // ////////////////// @@ -3318,7 +3370,13 @@ class basic_json { JSON_TRY { +#ifdef JSON_DIAGNOSTICS + reference result = m_value.array->at(idx); + result.m_parent = this; + return result; +#else return m_value.array->at(idx); +#endif } JSON_CATCH (std::out_of_range&) { @@ -3416,7 +3474,13 @@ class basic_json { JSON_TRY { +#ifdef JSON_DIAGNOSTICS + reference result = m_value.object->at(key); + result.m_parent = this; + return result; +#else return m_value.object->at(key); +#endif } JSON_CATCH (std::out_of_range&) { @@ -3525,9 +3589,18 @@ class basic_json m_value.array->insert(m_value.array->end(), idx - m_value.array->size() + 1, basic_json()); +#ifdef JSON_DIAGNOSTICS + m_value.array->back().m_parent = this; +#endif } +#ifdef JSON_DIAGNOSTICS + reference result = m_value.array->operator[](idx); + result.m_parent = this; + return result; +#else return m_value.array->operator[](idx); +#endif } JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()))); @@ -3603,7 +3676,13 @@ class basic_json // operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { +#ifdef JSON_DIAGNOSTICS + reference result = m_value.object->operator[](key); + result.m_parent = this; + return result; +#else return m_value.object->operator[](key); +#endif } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); @@ -3693,7 +3772,13 @@ class basic_json // at only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { +#ifdef JSON_DIAGNOSTICS + reference result = m_value.object->operator[](key); + result.m_parent = this; + return result; +#else return m_value.object->operator[](key); +#endif } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); @@ -5249,6 +5334,9 @@ class basic_json // add element to array (move semantics) m_value.array->push_back(std::move(val)); +#ifdef JSON_DIAGNOSTICS + m_value.array->back().m_parent = this; +#endif // if val is moved from, basic_json move constructor marks it null so we do not call the destructor } @@ -5284,6 +5372,9 @@ class basic_json // add element to array m_value.array->push_back(val); +#ifdef JSON_DIAGNOSTICS + m_value.array->back().m_parent = this; +#endif } /*! @@ -5332,8 +5423,13 @@ class basic_json assert_invariant(); } - // add element to array + // add element to object +#ifdef JSON_DIAGNOSTICS + auto res = m_value.object->insert(val); + res.first->second.m_parent = this; +#else m_value.object->insert(val); +#endif } /*! @@ -5437,9 +5533,18 @@ class basic_json // add element to array (perfect forwarding) #ifdef JSON_HAS_CPP_17 +#ifdef JSON_DIAGNOSTICS + reference result = m_value.array->emplace_back(std::forward(args)...); + result.m_parent = this; + return result; +#else return m_value.array->emplace_back(std::forward(args)...); +#endif #else m_value.array->emplace_back(std::forward(args)...); +#ifdef JSON_DIAGNOSTICS + m_value.array->back().m_parent = this; +#endif return m_value.array->back(); #endif } @@ -6967,6 +7072,11 @@ class basic_json /// the value of the current element json_value m_value = {}; +#ifdef JSON_DIAGNOSTICS + /// a pointer to a parent value (for debugging purposes) + basic_json* m_parent = nullptr; +#endif + ////////////////////////////////////////// // binary serialization/deserialization // ////////////////////////////////////////// diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 8b6344f921..f27a63ff21 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18266,6 +18266,9 @@ class basic_json std::for_each(init.begin(), init.end(), [this](const detail::json_ref& element_ref) { auto element = element_ref.moved_or_copied(); +#ifdef JSON_DIAGNOSTICS + (*element.m_value.array)[1].m_parent = this; +#endif m_value.object->emplace( std::move(*((*element.m_value.array)[0].m_value.string)), std::move((*element.m_value.array)[1])); @@ -18276,6 +18279,12 @@ class basic_json // the initializer list describes an array -> create array m_type = value_t::array; m_value.array = create(init.begin(), init.end()); +#ifdef JSON_DIAGNOSTICS + for (auto& element : *m_value.array) + { + element.m_parent = this; + } +#endif } assert_invariant(); @@ -19320,6 +19329,49 @@ class basic_json /// @} private: +#ifdef JSON_DIAGNOSTICS + std::string diagnostics() + { + std::string result; + for (basic_json* current = this; current->m_parent != nullptr; current = current->m_parent) + { + switch (current->m_parent->type()) + { + case value_t::array: + { + for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) + { + if (current->m_parent->m_value.array->operator[](i) == *current) + { + result = "/" + std::to_string(i) + result; + continue; + } + } + break; + } + + case value_t::object: + { + for (auto it : *current->m_parent->m_value.object) + { + if (it.second == *current) + { + result = "/" + it.first + result; + continue; + } + } + break; + } + + default: + break; + } + } + + return result; + } +#endif + ////////////////// // value access // ////////////////// @@ -19942,7 +19994,13 @@ class basic_json { JSON_TRY { +#ifdef JSON_DIAGNOSTICS + reference result = m_value.array->at(idx); + result.m_parent = this; + return result; +#else return m_value.array->at(idx); +#endif } JSON_CATCH (std::out_of_range&) { @@ -20040,7 +20098,13 @@ class basic_json { JSON_TRY { +#ifdef JSON_DIAGNOSTICS + reference result = m_value.object->at(key); + result.m_parent = this; + return result; +#else return m_value.object->at(key); +#endif } JSON_CATCH (std::out_of_range&) { @@ -20149,9 +20213,18 @@ class basic_json m_value.array->insert(m_value.array->end(), idx - m_value.array->size() + 1, basic_json()); +#ifdef JSON_DIAGNOSTICS + m_value.array->back().m_parent = this; +#endif } +#ifdef JSON_DIAGNOSTICS + reference result = m_value.array->operator[](idx); + result.m_parent = this; + return result; +#else return m_value.array->operator[](idx); +#endif } JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()))); @@ -20227,7 +20300,13 @@ class basic_json // operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { +#ifdef JSON_DIAGNOSTICS + reference result = m_value.object->operator[](key); + result.m_parent = this; + return result; +#else return m_value.object->operator[](key); +#endif } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); @@ -20317,7 +20396,13 @@ class basic_json // at only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { +#ifdef JSON_DIAGNOSTICS + reference result = m_value.object->operator[](key); + result.m_parent = this; + return result; +#else return m_value.object->operator[](key); +#endif } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); @@ -21873,6 +21958,9 @@ class basic_json // add element to array (move semantics) m_value.array->push_back(std::move(val)); +#ifdef JSON_DIAGNOSTICS + m_value.array->back().m_parent = this; +#endif // if val is moved from, basic_json move constructor marks it null so we do not call the destructor } @@ -21908,6 +21996,9 @@ class basic_json // add element to array m_value.array->push_back(val); +#ifdef JSON_DIAGNOSTICS + m_value.array->back().m_parent = this; +#endif } /*! @@ -21956,8 +22047,13 @@ class basic_json assert_invariant(); } - // add element to array + // add element to object +#ifdef JSON_DIAGNOSTICS + auto res = m_value.object->insert(val); + res.first->second.m_parent = this; +#else m_value.object->insert(val); +#endif } /*! @@ -22061,9 +22157,18 @@ class basic_json // add element to array (perfect forwarding) #ifdef JSON_HAS_CPP_17 +#ifdef JSON_DIAGNOSTICS + reference result = m_value.array->emplace_back(std::forward(args)...); + result.m_parent = this; + return result; +#else return m_value.array->emplace_back(std::forward(args)...); +#endif #else m_value.array->emplace_back(std::forward(args)...); +#ifdef JSON_DIAGNOSTICS + m_value.array->back().m_parent = this; +#endif return m_value.array->back(); #endif } @@ -23591,6 +23696,11 @@ class basic_json /// the value of the current element json_value m_value = {}; +#ifdef JSON_DIAGNOSTICS + /// a pointer to a parent value (for debugging purposes) + basic_json* m_parent = nullptr; +#endif + ////////////////////////////////////////// // binary serialization/deserialization // ////////////////////////////////////////// From 7b047861b074b8e54d4aed1278a3fd7b3e10b30d Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 2 Jan 2021 13:44:41 +0100 Subject: [PATCH 005/143] :construction: add diagnostics to exceptions --- .../nlohmann/detail/conversions/from_json.hpp | 30 +++--- include/nlohmann/detail/input/json_sax.hpp | 8 +- .../nlohmann/detail/iterators/iter_impl.hpp | 24 ++--- include/nlohmann/json.hpp | 36 ++++--- single_include/nlohmann/json.hpp | 98 +++++++++++-------- 5 files changed, 112 insertions(+), 84 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 438b84a2e1..0feea28330 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -27,7 +27,7 @@ void from_json(const BasicJsonType& j, typename std::nullptr_t& n) { if (JSON_HEDLEY_UNLIKELY(!j.is_null())) { - JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be null, but is " + std::string(j.type_name()))); } n = nullptr; } @@ -58,7 +58,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be number, but is " + std::string(j.type_name()))); } } @@ -67,7 +67,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { if (JSON_HEDLEY_UNLIKELY(!j.is_boolean())) { - JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be boolean, but is " + std::string(j.type_name()))); } b = *j.template get_ptr(); } @@ -77,7 +77,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be string, but is " + std::string(j.type_name()))); } s = *j.template get_ptr(); } @@ -93,7 +93,7 @@ void from_json(const BasicJsonType& j, ConstructibleStringType& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be string, but is " + std::string(j.type_name()))); } s = *j.template get_ptr(); @@ -133,7 +133,7 @@ void from_json(const BasicJsonType& j, std::forward_list& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); } l.clear(); std::transform(j.rbegin(), j.rend(), @@ -150,7 +150,7 @@ void from_json(const BasicJsonType& j, std::valarray& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); } l.resize(j.size()); std::transform(j.begin(), j.end(), std::begin(l), @@ -241,7 +241,7 @@ void()) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); } @@ -253,7 +253,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin) { if (JSON_HEDLEY_UNLIKELY(!j.is_binary())) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be binary, but is " + std::string(j.type_name()))); } bin = *j.template get_ptr(); @@ -265,7 +265,7 @@ void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) { if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be object, but is " + std::string(j.type_name()))); } ConstructibleObjectType ret; @@ -319,7 +319,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be number, but is " + std::string(j.type_name()))); } } @@ -348,14 +348,14 @@ void from_json(const BasicJsonType& j, std::map& { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(p.type_name()))); } m.emplace(p.at(0).template get(), p.at(1).template get()); } @@ -368,14 +368,14 @@ void from_json(const BasicJsonType& j, std::unordered_map(), p.at(1).template get()); } diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 223acd60eb..316f517234 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -219,7 +219,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, + JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive object size: " + std::to_string(len))); } @@ -245,7 +245,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, + JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive array size: " + std::to_string(len))); } @@ -400,7 +400,7 @@ class json_sax_dom_callback_parser // check object limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive object size: " + std::to_string(len))); } return true; @@ -463,7 +463,7 @@ class json_sax_dom_callback_parser // check array limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive array size: " + std::to_string(len))); } return true; diff --git a/include/nlohmann/detail/iterators/iter_impl.hpp b/include/nlohmann/detail/iterators/iter_impl.hpp index 67134166e5..cceb8d05fc 100644 --- a/include/nlohmann/detail/iterators/iter_impl.hpp +++ b/include/nlohmann/detail/iterators/iter_impl.hpp @@ -257,7 +257,7 @@ class iter_impl } case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); default: { @@ -266,7 +266,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); } } } @@ -300,7 +300,7 @@ class iter_impl return m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); } } } @@ -401,7 +401,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, m_object->diagnostics() + "cannot compare iterators of different containers")); } JSON_ASSERT(m_object != nullptr); @@ -438,7 +438,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, m_object->diagnostics() + "cannot compare iterators of different containers")); } JSON_ASSERT(m_object != nullptr); @@ -446,7 +446,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators")); + JSON_THROW(invalid_iterator::create(213, m_object->diagnostics() + "cannot compare order of object iterators")); case value_t::array: return (m_it.array_iterator < other.m_it.array_iterator); @@ -494,7 +494,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, m_object->diagnostics() + "cannot use offsets with object iterators")); case value_t::array: { @@ -565,7 +565,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, m_object->diagnostics() + "cannot use offsets with object iterators")); case value_t::array: return m_it.array_iterator - other.m_it.array_iterator; @@ -586,13 +586,13 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators")); + JSON_THROW(invalid_iterator::create(208, m_object->diagnostics() + "cannot use operator[] for object iterators")); case value_t::array: return *std::next(m_it.array_iterator, n); case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); default: { @@ -601,7 +601,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); } } } @@ -619,7 +619,7 @@ class iter_impl return m_it.object_iterator->first; } - JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators")); + JSON_THROW(invalid_iterator::create(207, m_object->diagnostics() + "cannot use key() for non-object iterators")); } /*! diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index a827b037b6..be3b1a3e17 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -2155,6 +2155,9 @@ class basic_json basic_json(basic_json&& other) noexcept : m_type(std::move(other.m_type)), m_value(std::move(other.m_value)) +#ifdef JSON_DIAGNOSTICS + , m_parent(other.m_parent) +#endif { // check that passed value is valid other.assert_invariant(); @@ -2704,12 +2707,11 @@ class basic_json /// @} - private: -#ifdef JSON_DIAGNOSTICS - std::string diagnostics() + std::string diagnostics() const { - std::string result; - for (basic_json* current = this; current->m_parent != nullptr; current = current->m_parent) +#ifdef JSON_DIAGNOSTICS + std::vector tokens; + for (const basic_json* current = this; current->m_parent != nullptr; current = current->m_parent) { switch (current->m_parent->type()) { @@ -2719,7 +2721,7 @@ class basic_json { if (current->m_parent->m_value.array->operator[](i) == *current) { - result = "/" + std::to_string(i) + result; + tokens.emplace_back(std::to_string(i)); continue; } } @@ -2728,11 +2730,11 @@ class basic_json case value_t::object: { - for (auto it : *current->m_parent->m_value.object) + for (const auto& element : *current->m_parent->m_value.object) { - if (it.second == *current) + if (element.second == *current) { - result = "/" + it.first + result; + tokens.emplace_back(element.first.c_str()); continue; } } @@ -2744,10 +2746,22 @@ class basic_json } } - return result; - } + if (tokens.empty()) + { + return ""; + } + + return "(" + std::accumulate(tokens.begin(), tokens.end(), std::string{}, + [](const std::string & a, const std::string & b) + { + return a + "/" + b; + }) + ") "; +#else + return ""; #endif + } + private: ////////////////// // value access // ////////////////// diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f27a63ff21..ee90b5fd18 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3513,7 +3513,7 @@ void from_json(const BasicJsonType& j, typename std::nullptr_t& n) { if (JSON_HEDLEY_UNLIKELY(!j.is_null())) { - JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be null, but is " + std::string(j.type_name()))); } n = nullptr; } @@ -3544,7 +3544,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be number, but is " + std::string(j.type_name()))); } } @@ -3553,7 +3553,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { if (JSON_HEDLEY_UNLIKELY(!j.is_boolean())) { - JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be boolean, but is " + std::string(j.type_name()))); } b = *j.template get_ptr(); } @@ -3563,7 +3563,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be string, but is " + std::string(j.type_name()))); } s = *j.template get_ptr(); } @@ -3579,7 +3579,7 @@ void from_json(const BasicJsonType& j, ConstructibleStringType& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be string, but is " + std::string(j.type_name()))); } s = *j.template get_ptr(); @@ -3619,7 +3619,7 @@ void from_json(const BasicJsonType& j, std::forward_list& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); } l.clear(); std::transform(j.rbegin(), j.rend(), @@ -3636,7 +3636,7 @@ void from_json(const BasicJsonType& j, std::valarray& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); } l.resize(j.size()); std::transform(j.begin(), j.end(), std::begin(l), @@ -3727,7 +3727,7 @@ void()) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); } @@ -3739,7 +3739,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin) { if (JSON_HEDLEY_UNLIKELY(!j.is_binary())) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be binary, but is " + std::string(j.type_name()))); } bin = *j.template get_ptr(); @@ -3751,7 +3751,7 @@ void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) { if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be object, but is " + std::string(j.type_name()))); } ConstructibleObjectType ret; @@ -3805,7 +3805,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be number, but is " + std::string(j.type_name()))); } } @@ -3834,14 +3834,14 @@ void from_json(const BasicJsonType& j, std::map& { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()))); + JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(p.type_name()))); } m.emplace(p.at(0).template get(), p.at(1).template get()); } @@ -3854,14 +3854,14 @@ void from_json(const BasicJsonType& j, std::unordered_map(), p.at(1).template get()); } @@ -5511,7 +5511,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, + JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive object size: " + std::to_string(len))); } @@ -5537,7 +5537,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, + JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive array size: " + std::to_string(len))); } @@ -5692,7 +5692,7 @@ class json_sax_dom_callback_parser // check object limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive object size: " + std::to_string(len))); } return true; @@ -5755,7 +5755,7 @@ class json_sax_dom_callback_parser // check array limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive array size: " + std::to_string(len))); } return true; @@ -11144,7 +11144,7 @@ class iter_impl } case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); default: { @@ -11153,7 +11153,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); } } } @@ -11187,7 +11187,7 @@ class iter_impl return m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); } } } @@ -11288,7 +11288,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, m_object->diagnostics() + "cannot compare iterators of different containers")); } JSON_ASSERT(m_object != nullptr); @@ -11325,7 +11325,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, m_object->diagnostics() + "cannot compare iterators of different containers")); } JSON_ASSERT(m_object != nullptr); @@ -11333,7 +11333,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators")); + JSON_THROW(invalid_iterator::create(213, m_object->diagnostics() + "cannot compare order of object iterators")); case value_t::array: return (m_it.array_iterator < other.m_it.array_iterator); @@ -11381,7 +11381,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, m_object->diagnostics() + "cannot use offsets with object iterators")); case value_t::array: { @@ -11452,7 +11452,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, m_object->diagnostics() + "cannot use offsets with object iterators")); case value_t::array: return m_it.array_iterator - other.m_it.array_iterator; @@ -11473,13 +11473,13 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators")); + JSON_THROW(invalid_iterator::create(208, m_object->diagnostics() + "cannot use operator[] for object iterators")); case value_t::array: return *std::next(m_it.array_iterator, n); case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); default: { @@ -11488,7 +11488,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); } } } @@ -11506,7 +11506,7 @@ class iter_impl return m_it.object_iterator->first; } - JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators")); + JSON_THROW(invalid_iterator::create(207, m_object->diagnostics() + "cannot use key() for non-object iterators")); } /*! @@ -18779,6 +18779,9 @@ class basic_json basic_json(basic_json&& other) noexcept : m_type(std::move(other.m_type)), m_value(std::move(other.m_value)) +#ifdef JSON_DIAGNOSTICS + , m_parent(other.m_parent) +#endif { // check that passed value is valid other.assert_invariant(); @@ -19328,12 +19331,11 @@ class basic_json /// @} - private: -#ifdef JSON_DIAGNOSTICS - std::string diagnostics() + std::string diagnostics() const { - std::string result; - for (basic_json* current = this; current->m_parent != nullptr; current = current->m_parent) +#ifdef JSON_DIAGNOSTICS + std::vector tokens; + for (const basic_json* current = this; current->m_parent != nullptr; current = current->m_parent) { switch (current->m_parent->type()) { @@ -19343,7 +19345,7 @@ class basic_json { if (current->m_parent->m_value.array->operator[](i) == *current) { - result = "/" + std::to_string(i) + result; + tokens.emplace_back(std::to_string(i)); continue; } } @@ -19352,11 +19354,11 @@ class basic_json case value_t::object: { - for (auto it : *current->m_parent->m_value.object) + for (const auto& element : *current->m_parent->m_value.object) { - if (it.second == *current) + if (element.second == *current) { - result = "/" + it.first + result; + tokens.emplace_back(element.first.c_str()); continue; } } @@ -19368,10 +19370,22 @@ class basic_json } } - return result; - } + if (tokens.empty()) + { + return ""; + } + + return "(" + std::accumulate(tokens.begin(), tokens.end(), std::string{}, + [](const std::string & a, const std::string & b) + { + return a + "/" + b; + }) + ") "; +#else + return ""; #endif + } + private: ////////////////// // value access // ////////////////// From ecaab32ef05b440eb991b5526fb0b3bf9e521111 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 2 Jan 2021 13:45:00 +0100 Subject: [PATCH 006/143] :construction: add switch for diagnostics --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 44ede3e799..abd3a17c7f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ${M option(JSON_Install "Install CMake targets during install step." ${MAIN_PROJECT}) option(JSON_MultipleHeaders "Use non-amalgamated version of the library." OFF) option(JSON_ImplicitConversions "Enable implicit conversions." ON) +option(JSON_Diagnostics "Enable better diagnostic messages." OFF) ## ## CONFIGURATION @@ -79,6 +80,7 @@ target_compile_definitions( ${NLOHMANN_JSON_TARGET_NAME} INTERFACE JSON_USE_IMPLICIT_CONVERSIONS=$ + JSON_DIAGNOSTICS=$ ) target_include_directories( From c6e7fa21eddf614d530b1dba9acb81855f5d74d5 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 2 Jan 2021 13:58:05 +0100 Subject: [PATCH 007/143] :construction: fix preprocessor check --- include/nlohmann/json.hpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index be3b1a3e17..525939b6f4 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1642,7 +1642,7 @@ class basic_json std::for_each(init.begin(), init.end(), [this](const detail::json_ref& element_ref) { auto element = element_ref.moved_or_copied(); -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS (*element.m_value.array)[1].m_parent = this; #endif m_value.object->emplace( @@ -1655,7 +1655,7 @@ class basic_json // the initializer list describes an array -> create array m_type = value_t::array; m_value.array = create(init.begin(), init.end()); -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS for (auto& element : *m_value.array) { element.m_parent = this; @@ -2155,7 +2155,7 @@ class basic_json basic_json(basic_json&& other) noexcept : m_type(std::move(other.m_type)), m_value(std::move(other.m_value)) -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS , m_parent(other.m_parent) #endif { @@ -2709,7 +2709,7 @@ class basic_json std::string diagnostics() const { -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS std::vector tokens; for (const basic_json* current = this; current->m_parent != nullptr; current = current->m_parent) { @@ -3384,7 +3384,7 @@ class basic_json { JSON_TRY { -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS reference result = m_value.array->at(idx); result.m_parent = this; return result; @@ -3488,7 +3488,7 @@ class basic_json { JSON_TRY { -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS reference result = m_value.object->at(key); result.m_parent = this; return result; @@ -3603,12 +3603,12 @@ class basic_json m_value.array->insert(m_value.array->end(), idx - m_value.array->size() + 1, basic_json()); -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS m_value.array->back().m_parent = this; #endif } -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS reference result = m_value.array->operator[](idx); result.m_parent = this; return result; @@ -3690,7 +3690,7 @@ class basic_json // operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS reference result = m_value.object->operator[](key); result.m_parent = this; return result; @@ -3786,7 +3786,7 @@ class basic_json // at only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS reference result = m_value.object->operator[](key); result.m_parent = this; return result; @@ -5348,7 +5348,7 @@ class basic_json // add element to array (move semantics) m_value.array->push_back(std::move(val)); -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS m_value.array->back().m_parent = this; #endif // if val is moved from, basic_json move constructor marks it null so we do not call the destructor @@ -5386,7 +5386,7 @@ class basic_json // add element to array m_value.array->push_back(val); -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS m_value.array->back().m_parent = this; #endif } @@ -5438,7 +5438,7 @@ class basic_json } // add element to object -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS auto res = m_value.object->insert(val); res.first->second.m_parent = this; #else @@ -5547,7 +5547,7 @@ class basic_json // add element to array (perfect forwarding) #ifdef JSON_HAS_CPP_17 -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS reference result = m_value.array->emplace_back(std::forward(args)...); result.m_parent = this; return result; @@ -5556,7 +5556,7 @@ class basic_json #endif #else m_value.array->emplace_back(std::forward(args)...); -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS m_value.array->back().m_parent = this; #endif return m_value.array->back(); @@ -7086,7 +7086,7 @@ class basic_json /// the value of the current element json_value m_value = {}; -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS /// a pointer to a parent value (for debugging purposes) basic_json* m_parent = nullptr; #endif From 09cd4ed125c76f0214d66507f33fe4daa447ca6c Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 2 Jan 2021 14:10:40 +0100 Subject: [PATCH 008/143] :construction: fix preprocessor check --- single_include/nlohmann/json.hpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ee90b5fd18..f97a95849a 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18266,7 +18266,7 @@ class basic_json std::for_each(init.begin(), init.end(), [this](const detail::json_ref& element_ref) { auto element = element_ref.moved_or_copied(); -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS (*element.m_value.array)[1].m_parent = this; #endif m_value.object->emplace( @@ -18279,7 +18279,7 @@ class basic_json // the initializer list describes an array -> create array m_type = value_t::array; m_value.array = create(init.begin(), init.end()); -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS for (auto& element : *m_value.array) { element.m_parent = this; @@ -18779,7 +18779,7 @@ class basic_json basic_json(basic_json&& other) noexcept : m_type(std::move(other.m_type)), m_value(std::move(other.m_value)) -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS , m_parent(other.m_parent) #endif { @@ -19333,7 +19333,7 @@ class basic_json std::string diagnostics() const { -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS std::vector tokens; for (const basic_json* current = this; current->m_parent != nullptr; current = current->m_parent) { @@ -20008,7 +20008,7 @@ class basic_json { JSON_TRY { -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS reference result = m_value.array->at(idx); result.m_parent = this; return result; @@ -20112,7 +20112,7 @@ class basic_json { JSON_TRY { -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS reference result = m_value.object->at(key); result.m_parent = this; return result; @@ -20227,12 +20227,12 @@ class basic_json m_value.array->insert(m_value.array->end(), idx - m_value.array->size() + 1, basic_json()); -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS m_value.array->back().m_parent = this; #endif } -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS reference result = m_value.array->operator[](idx); result.m_parent = this; return result; @@ -20314,7 +20314,7 @@ class basic_json // operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS reference result = m_value.object->operator[](key); result.m_parent = this; return result; @@ -20410,7 +20410,7 @@ class basic_json // at only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS reference result = m_value.object->operator[](key); result.m_parent = this; return result; @@ -21972,7 +21972,7 @@ class basic_json // add element to array (move semantics) m_value.array->push_back(std::move(val)); -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS m_value.array->back().m_parent = this; #endif // if val is moved from, basic_json move constructor marks it null so we do not call the destructor @@ -22010,7 +22010,7 @@ class basic_json // add element to array m_value.array->push_back(val); -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS m_value.array->back().m_parent = this; #endif } @@ -22062,7 +22062,7 @@ class basic_json } // add element to object -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS auto res = m_value.object->insert(val); res.first->second.m_parent = this; #else @@ -22171,7 +22171,7 @@ class basic_json // add element to array (perfect forwarding) #ifdef JSON_HAS_CPP_17 -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS reference result = m_value.array->emplace_back(std::forward(args)...); result.m_parent = this; return result; @@ -22180,7 +22180,7 @@ class basic_json #endif #else m_value.array->emplace_back(std::forward(args)...); -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS m_value.array->back().m_parent = this; #endif return m_value.array->back(); @@ -23710,7 +23710,7 @@ class basic_json /// the value of the current element json_value m_value = {}; -#ifdef JSON_DIAGNOSTICS +#if JSON_DIAGNOSTICS /// a pointer to a parent value (for debugging purposes) basic_json* m_parent = nullptr; #endif From 7323a8eb4e7405406e79c7e14fc9cf34c3a496fe Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 2 Jan 2021 16:13:04 +0100 Subject: [PATCH 009/143] :construction: add tests --- .../nlohmann/detail/output/binary_writer.hpp | 22 +-- include/nlohmann/json.hpp | 140 +++++++-------- single_include/nlohmann/json.hpp | 162 +++++++++--------- test/src/unit-bson.cpp | 8 + test/src/unit-iterators2.cpp | 88 ++++++++++ test/src/unit-regression1.cpp | 4 + 6 files changed, 262 insertions(+), 162 deletions(-) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 0c6185e048..72ebbeda68 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -57,7 +57,7 @@ class binary_writer default: { - JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(317, j.diagnostics() + "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()))); } } } @@ -901,12 +901,12 @@ class binary_writer @return The size of a BSON document entry header, including the id marker and the entry name size (and its null-terminator). */ - static std::size_t calc_bson_entry_header_size(const string_t& name) + static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j) { const auto it = name.find(static_cast(0)); if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) { - JSON_THROW(out_of_range::create(409, + JSON_THROW(out_of_range::create(409, j.diagnostics() + "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")")); } @@ -1017,21 +1017,21 @@ class binary_writer @brief Writes a BSON element with key @a name and unsigned @a value */ void write_bson_unsigned(const string_t& name, - const std::uint64_t value) + const BasicJsonType& j) { - if (value <= static_cast((std::numeric_limits::max)())) + if (j.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) { write_bson_entry_header(name, 0x10 /* int32 */); - write_number(static_cast(value)); + write_number(static_cast(j.m_value.number_unsigned)); } - else if (value <= static_cast((std::numeric_limits::max)())) + else if (j.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) { write_bson_entry_header(name, 0x12 /* int64 */); - write_number(static_cast(value)); + write_number(static_cast(j.m_value.number_unsigned)); } else { - JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(value) + " cannot be represented by BSON as it does not fit int64")); + JSON_THROW(out_of_range::create(407, j.diagnostics() + "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64")); } } @@ -1108,7 +1108,7 @@ class binary_writer static std::size_t calc_bson_element_size(const string_t& name, const BasicJsonType& j) { - const auto header_size = calc_bson_entry_header_size(name); + const auto header_size = calc_bson_entry_header_size(name, j); switch (j.type()) { case value_t::object: @@ -1177,7 +1177,7 @@ class binary_writer return write_bson_integer(name, j.m_value.number_integer); case value_t::number_unsigned: - return write_bson_unsigned(name, j.m_value.number_unsigned); + return write_bson_unsigned(name, j); case value_t::string: return write_bson_string(name, *j.m_value.string); diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 525939b6f4..80bc871a2f 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1941,7 +1941,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(201, "iterators are not compatible")); + JSON_THROW(invalid_iterator::create(201, diagnostics() + "iterators are not compatible")); } // copy type from first iterator @@ -2774,7 +2774,7 @@ class basic_json return m_value.boolean; } - JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, diagnostics() + "type must be boolean, but is " + std::string(type_name()))); } /// get a pointer to the value (object) @@ -2895,7 +2895,7 @@ class basic_json return *ptr; } - JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()))); + JSON_THROW(type_error::create(303, obj.diagnostics() + "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()))); } public: @@ -3323,7 +3323,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name()))); } return *get_ptr(); @@ -3334,7 +3334,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name()))); } return *get_ptr(); @@ -3395,12 +3395,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); } } @@ -3442,12 +3442,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); } } @@ -3499,12 +3499,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found")); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); } } @@ -3550,12 +3550,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found")); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); } } @@ -3617,7 +3617,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); } /*! @@ -3647,7 +3647,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); } /*! @@ -3699,7 +3699,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); } /*! @@ -3741,7 +3741,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); } /*! @@ -3795,7 +3795,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); } /*! @@ -3839,7 +3839,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); } /*! @@ -3911,7 +3911,7 @@ class basic_json return default_value; } - JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name()))); } /*! @@ -3984,7 +3984,7 @@ class basic_json } } - JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name()))); } /*! @@ -4138,7 +4138,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != pos.m_object)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); } IteratorType result = end(); @@ -4190,7 +4190,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); } return result; @@ -4251,7 +4251,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object)) { - JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value")); + JSON_THROW(invalid_iterator::create(203, diagnostics() + "iterators do not fit current value")); } IteratorType result = end(); @@ -4268,7 +4268,7 @@ class basic_json if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, "iterators out of range")); + JSON_THROW(invalid_iterator::create(204, diagnostics() + "iterators out of range")); } if (is_string()) @@ -4306,7 +4306,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); } return result; @@ -4349,7 +4349,7 @@ class basic_json return m_value.object->erase(key); } - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); } /*! @@ -4383,14 +4383,14 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(idx >= size())) { - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); } m_value.array->erase(m_value.array->begin() + static_cast(idx)); } else { - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); } } @@ -5335,7 +5335,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); } // transform null object into an array @@ -5373,7 +5373,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); } // transform null object into an array @@ -5426,7 +5426,7 @@ class basic_json // push_back only works for null objects or objects if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); } // transform null object into an object @@ -5534,7 +5534,7 @@ class basic_json // emplace_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace_back() with " + std::string(type_name()))); } // transform null object into an array @@ -5596,7 +5596,7 @@ class basic_json // emplace only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace() with " + std::string(type_name()))); } // transform null object into an object @@ -5667,14 +5667,14 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); } // insert to array and return iterator return insert_iterator(pos, val); } - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); } /*! @@ -5718,14 +5718,14 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); } // insert to array and return iterator return insert_iterator(pos, cnt, val); } - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); } /*! @@ -5763,24 +5763,24 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); } if (JSON_HEDLEY_UNLIKELY(first.m_object == this)) { - JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container")); + JSON_THROW(invalid_iterator::create(211, diagnostics() + "passed iterators may not belong to container")); } // insert to array and return iterator @@ -5816,13 +5816,13 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); } // insert to array and return iterator @@ -5857,19 +5857,19 @@ class basic_json // insert only works for objects if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects")); } m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator); @@ -5906,11 +5906,11 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name()))); } if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()))); + JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(j.type_name()))); } for (auto it = j.cbegin(); it != j.cend(); ++it) @@ -5957,20 +5957,20 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name()))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object() || !last.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects")); } for (auto it = first; it != last; ++it) @@ -6065,7 +6065,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); } } @@ -6098,7 +6098,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); } } @@ -6131,7 +6131,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); } } @@ -6164,7 +6164,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); } } @@ -6178,7 +6178,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); } } @@ -8343,7 +8343,7 @@ class basic_json }; // wrapper for "add" operation; add value at ptr - const auto operation_add = [&result](json_pointer & ptr, basic_json val) + const auto operation_add = [this, &result](json_pointer & ptr, basic_json val) { // adding to the root of the target document means replacing it if (ptr.empty()) @@ -8387,7 +8387,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(idx > parent.size())) { // avoid undefined behavior - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); } // default case: insert add offset @@ -8403,7 +8403,7 @@ class basic_json }; // wrapper for "remove" operation; remove value at ptr - const auto operation_remove = [&result](json_pointer & ptr) + const auto operation_remove = [this, &result](json_pointer & ptr) { // get reference to parent of JSON pointer ptr const auto last_path = ptr.back(); @@ -8421,7 +8421,7 @@ class basic_json } else { - JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found")); + JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + last_path + "' not found")); } } else if (parent.is_array()) @@ -8434,16 +8434,16 @@ class basic_json // type check: top level value must be an array if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array())) { - JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects")); } // iterate and apply the operations for (const auto& val : json_patch) { // wrapper to get a value for an operation - const auto get_value = [&val](const std::string & op, - const std::string & member, - bool string_type) -> basic_json & + const auto get_value = [this, &val](const std::string & op, + const std::string & member, + bool string_type) -> basic_json & { // find value auto it = val.m_value.object->find(member); @@ -8454,13 +8454,13 @@ class basic_json // check if desired value is present if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end())) { - JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have member '" + member + "'")); } // check if result is of type string if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string())) { - JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have string member '" + member + "'")); } // no error: return value @@ -8470,7 +8470,7 @@ class basic_json // type check: every element of the array must be an object if (JSON_HEDLEY_UNLIKELY(!val.is_object())) { - JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects")); } // collect mandatory members @@ -8548,7 +8548,7 @@ class basic_json // throw an exception if test fails if (JSON_HEDLEY_UNLIKELY(!success)) { - JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump())); + JSON_THROW(other_error::create(501, diagnostics() + "unsuccessful: " + val.dump())); } break; @@ -8558,7 +8558,7 @@ class basic_json { // op must be "add", "remove", "replace", "move", "copy", or // "test" - JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid")); + JSON_THROW(parse_error::create(105, 0, diagnostics() + "operation value '" + op + "' is invalid")); } } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f97a95849a..16a6049de5 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -12900,7 +12900,7 @@ class binary_writer default: { - JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(317, j.diagnostics() + "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()))); } } } @@ -13744,12 +13744,12 @@ class binary_writer @return The size of a BSON document entry header, including the id marker and the entry name size (and its null-terminator). */ - static std::size_t calc_bson_entry_header_size(const string_t& name) + static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j) { const auto it = name.find(static_cast(0)); if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) { - JSON_THROW(out_of_range::create(409, + JSON_THROW(out_of_range::create(409, j.diagnostics() + "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")")); } @@ -13860,21 +13860,21 @@ class binary_writer @brief Writes a BSON element with key @a name and unsigned @a value */ void write_bson_unsigned(const string_t& name, - const std::uint64_t value) + const BasicJsonType& j) { - if (value <= static_cast((std::numeric_limits::max)())) + if (j.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) { write_bson_entry_header(name, 0x10 /* int32 */); - write_number(static_cast(value)); + write_number(static_cast(j.m_value.number_unsigned)); } - else if (value <= static_cast((std::numeric_limits::max)())) + else if (j.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) { write_bson_entry_header(name, 0x12 /* int64 */); - write_number(static_cast(value)); + write_number(static_cast(j.m_value.number_unsigned)); } else { - JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(value) + " cannot be represented by BSON as it does not fit int64")); + JSON_THROW(out_of_range::create(407, j.diagnostics() + "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64")); } } @@ -13951,7 +13951,7 @@ class binary_writer static std::size_t calc_bson_element_size(const string_t& name, const BasicJsonType& j) { - const auto header_size = calc_bson_entry_header_size(name); + const auto header_size = calc_bson_entry_header_size(name, j); switch (j.type()) { case value_t::object: @@ -14020,7 +14020,7 @@ class binary_writer return write_bson_integer(name, j.m_value.number_integer); case value_t::number_unsigned: - return write_bson_unsigned(name, j.m_value.number_unsigned); + return write_bson_unsigned(name, j); case value_t::string: return write_bson_string(name, *j.m_value.string); @@ -18565,7 +18565,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(201, "iterators are not compatible")); + JSON_THROW(invalid_iterator::create(201, diagnostics() + "iterators are not compatible")); } // copy type from first iterator @@ -19398,7 +19398,7 @@ class basic_json return m_value.boolean; } - JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, diagnostics() + "type must be boolean, but is " + std::string(type_name()))); } /// get a pointer to the value (object) @@ -19519,7 +19519,7 @@ class basic_json return *ptr; } - JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()))); + JSON_THROW(type_error::create(303, obj.diagnostics() + "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()))); } public: @@ -19947,7 +19947,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name()))); } return *get_ptr(); @@ -19958,7 +19958,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name()))); } return *get_ptr(); @@ -20019,12 +20019,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); } } @@ -20066,12 +20066,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); } } @@ -20123,12 +20123,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found")); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); } } @@ -20174,12 +20174,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found")); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); } } @@ -20241,7 +20241,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); } /*! @@ -20271,7 +20271,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); } /*! @@ -20323,7 +20323,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); } /*! @@ -20365,7 +20365,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); } /*! @@ -20419,7 +20419,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); } /*! @@ -20463,7 +20463,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); } /*! @@ -20535,7 +20535,7 @@ class basic_json return default_value; } - JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name()))); } /*! @@ -20608,7 +20608,7 @@ class basic_json } } - JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name()))); } /*! @@ -20762,7 +20762,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != pos.m_object)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); } IteratorType result = end(); @@ -20814,7 +20814,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); } return result; @@ -20875,7 +20875,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object)) { - JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value")); + JSON_THROW(invalid_iterator::create(203, diagnostics() + "iterators do not fit current value")); } IteratorType result = end(); @@ -20892,7 +20892,7 @@ class basic_json if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, "iterators out of range")); + JSON_THROW(invalid_iterator::create(204, diagnostics() + "iterators out of range")); } if (is_string()) @@ -20930,7 +20930,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); } return result; @@ -20973,7 +20973,7 @@ class basic_json return m_value.object->erase(key); } - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); } /*! @@ -21007,14 +21007,14 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(idx >= size())) { - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); } m_value.array->erase(m_value.array->begin() + static_cast(idx)); } else { - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); } } @@ -21959,7 +21959,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); } // transform null object into an array @@ -21997,7 +21997,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); } // transform null object into an array @@ -22050,7 +22050,7 @@ class basic_json // push_back only works for null objects or objects if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); } // transform null object into an object @@ -22158,7 +22158,7 @@ class basic_json // emplace_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace_back() with " + std::string(type_name()))); } // transform null object into an array @@ -22220,7 +22220,7 @@ class basic_json // emplace only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace() with " + std::string(type_name()))); } // transform null object into an object @@ -22291,14 +22291,14 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); } // insert to array and return iterator return insert_iterator(pos, val); } - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); } /*! @@ -22342,14 +22342,14 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); } // insert to array and return iterator return insert_iterator(pos, cnt, val); } - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); } /*! @@ -22387,24 +22387,24 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); } if (JSON_HEDLEY_UNLIKELY(first.m_object == this)) { - JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container")); + JSON_THROW(invalid_iterator::create(211, diagnostics() + "passed iterators may not belong to container")); } // insert to array and return iterator @@ -22440,13 +22440,13 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); } // insert to array and return iterator @@ -22481,19 +22481,19 @@ class basic_json // insert only works for objects if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects")); } m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator); @@ -22530,11 +22530,11 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name()))); } if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()))); + JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(j.type_name()))); } for (auto it = j.cbegin(); it != j.cend(); ++it) @@ -22581,20 +22581,20 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name()))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object() || !last.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects")); } for (auto it = first; it != last; ++it) @@ -22689,7 +22689,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); } } @@ -22722,7 +22722,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); } } @@ -22755,7 +22755,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); } } @@ -22788,7 +22788,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); } } @@ -22802,7 +22802,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); } } @@ -24967,7 +24967,7 @@ class basic_json }; // wrapper for "add" operation; add value at ptr - const auto operation_add = [&result](json_pointer & ptr, basic_json val) + const auto operation_add = [this, &result](json_pointer & ptr, basic_json val) { // adding to the root of the target document means replacing it if (ptr.empty()) @@ -25011,7 +25011,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(idx > parent.size())) { // avoid undefined behavior - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); } // default case: insert add offset @@ -25027,7 +25027,7 @@ class basic_json }; // wrapper for "remove" operation; remove value at ptr - const auto operation_remove = [&result](json_pointer & ptr) + const auto operation_remove = [this, &result](json_pointer & ptr) { // get reference to parent of JSON pointer ptr const auto last_path = ptr.back(); @@ -25045,7 +25045,7 @@ class basic_json } else { - JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found")); + JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + last_path + "' not found")); } } else if (parent.is_array()) @@ -25058,16 +25058,16 @@ class basic_json // type check: top level value must be an array if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array())) { - JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects")); } // iterate and apply the operations for (const auto& val : json_patch) { // wrapper to get a value for an operation - const auto get_value = [&val](const std::string & op, - const std::string & member, - bool string_type) -> basic_json & + const auto get_value = [this, &val](const std::string & op, + const std::string & member, + bool string_type) -> basic_json & { // find value auto it = val.m_value.object->find(member); @@ -25078,13 +25078,13 @@ class basic_json // check if desired value is present if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end())) { - JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have member '" + member + "'")); } // check if result is of type string if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string())) { - JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have string member '" + member + "'")); } // no error: return value @@ -25094,7 +25094,7 @@ class basic_json // type check: every element of the array must be an object if (JSON_HEDLEY_UNLIKELY(!val.is_object())) { - JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects")); } // collect mandatory members @@ -25172,7 +25172,7 @@ class basic_json // throw an exception if test fails if (JSON_HEDLEY_UNLIKELY(!success)) { - JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump())); + JSON_THROW(other_error::create(501, diagnostics() + "unsuccessful: " + val.dump())); } break; @@ -25182,7 +25182,7 @@ class basic_json { // op must be "add", "remove", "replace", "move", "copy", or // "test" - JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid")); + JSON_THROW(parse_error::create(105, 0, diagnostics() + "operation value '" + op + "' is invalid")); } } } diff --git a/test/src/unit-bson.cpp b/test/src/unit-bson.cpp index 3be72c7d47..ef3c8d4088 100644 --- a/test/src/unit-bson.cpp +++ b/test/src/unit-bson.cpp @@ -101,7 +101,11 @@ TEST_CASE("BSON") { std::string("en\0try", 6), true } }; CHECK_THROWS_AS(json::to_bson(j), json::out_of_range&); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.out_of_range.409] (/en) BSON key cannot contain code point U+0000 (at byte 2)"); +#else CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.out_of_range.409] BSON key cannot contain code point U+0000 (at byte 2)"); +#endif } SECTION("string length must be at least 1") @@ -1235,7 +1239,11 @@ TEST_CASE("BSON numerical data") }; CHECK_THROWS_AS(json::to_bson(j), json::out_of_range&); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH_STD_STR(json::to_bson(j), "[json.exception.out_of_range.407] (/entry) integer number " + std::to_string(i) + " cannot be represented by BSON as it does not fit int64"); +#else CHECK_THROWS_WITH_STD_STR(json::to_bson(j), "[json.exception.out_of_range.407] integer number " + std::to_string(i) + " cannot be represented by BSON as it does not fit int64"); +#endif } } diff --git a/test/src/unit-iterators2.cpp b/test/src/unit-iterators2.cpp index 85eb7beb7c..b9dcc22090 100644 --- a/test/src/unit-iterators2.cpp +++ b/test/src/unit-iterators2.cpp @@ -90,6 +90,16 @@ TEST_CASE("iterators 2") CHECK_THROWS_AS(it1_c < it2_c, json::invalid_iterator&); CHECK_THROWS_AS(it2_c < it3_c, json::invalid_iterator&); CHECK_THROWS_AS(it1_c < it3_c, json::invalid_iterator&); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH(it1 < it1, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 < it2, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2 < it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 < it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c < it1_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c < it2_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2_c < it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c < it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); +#else CHECK_THROWS_WITH(it1 < it1, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1 < it2, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2 < it3, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); @@ -98,6 +108,7 @@ TEST_CASE("iterators 2") CHECK_THROWS_WITH(it1_c < it2_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2_c < it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1_c < it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); +#endif } else { @@ -124,6 +135,16 @@ TEST_CASE("iterators 2") CHECK_THROWS_AS(it1_c <= it2_c, json::invalid_iterator&); CHECK_THROWS_AS(it2_c <= it3_c, json::invalid_iterator&); CHECK_THROWS_AS(it1_c <= it3_c, json::invalid_iterator&); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH(it1 <= it1, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 <= it2, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2 <= it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 <= it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c <= it1_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c <= it2_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2_c <= it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c <= it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); +#else CHECK_THROWS_WITH(it1 <= it1, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1 <= it2, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2 <= it3, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); @@ -132,6 +153,7 @@ TEST_CASE("iterators 2") CHECK_THROWS_WITH(it1_c <= it2_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2_c <= it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1_c <= it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); +#endif } else { @@ -159,6 +181,16 @@ TEST_CASE("iterators 2") CHECK_THROWS_AS(it1_c > it2_c, json::invalid_iterator&); CHECK_THROWS_AS(it2_c > it3_c, json::invalid_iterator&); CHECK_THROWS_AS(it1_c > it3_c, json::invalid_iterator&); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH(it1 > it1, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 > it2, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2 > it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 > it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c > it1_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c > it2_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2_c > it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c > it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); +#else CHECK_THROWS_WITH(it1 > it1, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1 > it2, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2 > it3, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); @@ -167,6 +199,7 @@ TEST_CASE("iterators 2") CHECK_THROWS_WITH(it1_c > it2_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2_c > it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1_c > it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); +#endif } else { @@ -194,6 +227,16 @@ TEST_CASE("iterators 2") CHECK_THROWS_AS(it1_c >= it2_c, json::invalid_iterator&); CHECK_THROWS_AS(it2_c >= it3_c, json::invalid_iterator&); CHECK_THROWS_AS(it1_c >= it3_c, json::invalid_iterator&); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH(it1 >= it1, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 >= it2, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2 >= it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 >= it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c >= it1_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c >= it2_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2_c >= it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c >= it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); +#else CHECK_THROWS_WITH(it1 >= it1, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1 >= it2, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2 >= it3, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); @@ -202,6 +245,7 @@ TEST_CASE("iterators 2") CHECK_THROWS_WITH(it1_c >= it2_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2_c >= it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1_c >= it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); +#endif } else { @@ -525,6 +569,16 @@ TEST_CASE("iterators 2") CHECK_THROWS_AS(it1_c < it2_c, json::invalid_iterator&); CHECK_THROWS_AS(it2_c < it3_c, json::invalid_iterator&); CHECK_THROWS_AS(it1_c < it3_c, json::invalid_iterator&); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH(it1 < it1, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 < it2, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2 < it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 < it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c < it1_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c < it2_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2_c < it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c < it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); +#else CHECK_THROWS_WITH(it1 < it1, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1 < it2, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2 < it3, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); @@ -533,6 +587,7 @@ TEST_CASE("iterators 2") CHECK_THROWS_WITH(it1_c < it2_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2_c < it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1_c < it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); +#endif } else { @@ -559,6 +614,16 @@ TEST_CASE("iterators 2") CHECK_THROWS_AS(it1_c <= it2_c, json::invalid_iterator&); CHECK_THROWS_AS(it2_c <= it3_c, json::invalid_iterator&); CHECK_THROWS_AS(it1_c <= it3_c, json::invalid_iterator&); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH(it1 <= it1, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 <= it2, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2 <= it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 <= it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c <= it1_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c <= it2_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2_c <= it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c <= it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); +#else CHECK_THROWS_WITH(it1 <= it1, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1 <= it2, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2 <= it3, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); @@ -567,6 +632,7 @@ TEST_CASE("iterators 2") CHECK_THROWS_WITH(it1_c <= it2_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2_c <= it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1_c <= it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); +#endif } else { @@ -594,6 +660,16 @@ TEST_CASE("iterators 2") CHECK_THROWS_AS(it1_c > it2_c, json::invalid_iterator&); CHECK_THROWS_AS(it2_c > it3_c, json::invalid_iterator&); CHECK_THROWS_AS(it1_c > it3_c, json::invalid_iterator&); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH(it1 > it1, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 > it2, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2 > it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 > it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c > it1_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c > it2_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2_c > it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c > it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); +#else CHECK_THROWS_WITH(it1 > it1, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1 > it2, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2 > it3, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); @@ -602,6 +678,7 @@ TEST_CASE("iterators 2") CHECK_THROWS_WITH(it1_c > it2_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2_c > it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1_c > it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); +#endif } else { @@ -629,6 +706,16 @@ TEST_CASE("iterators 2") CHECK_THROWS_AS(it1_c >= it2_c, json::invalid_iterator&); CHECK_THROWS_AS(it2_c >= it3_c, json::invalid_iterator&); CHECK_THROWS_AS(it1_c >= it3_c, json::invalid_iterator&); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH(it1 >= it1, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 >= it2, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2 >= it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1 >= it3, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c >= it1_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c >= it2_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it2_c >= it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); + CHECK_THROWS_WITH(it1_c >= it3_c, "[json.exception.invalid_iterator.213] (/5) cannot compare order of object iterators"); +#else CHECK_THROWS_WITH(it1 >= it1, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1 >= it2, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2 >= it3, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); @@ -637,6 +724,7 @@ TEST_CASE("iterators 2") CHECK_THROWS_WITH(it1_c >= it2_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it2_c >= it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); CHECK_THROWS_WITH(it1_c >= it3_c, "[json.exception.invalid_iterator.213] cannot compare order of object iterators"); +#endif } else { diff --git a/test/src/unit-regression1.cpp b/test/src/unit-regression1.cpp index df660ddb4b..bcb34ca87c 100644 --- a/test/src/unit-regression1.cpp +++ b/test/src/unit-regression1.cpp @@ -394,7 +394,11 @@ TEST_CASE("regression tests 1") // improve coverage o["int"] = 1; CHECK_THROWS_AS(s2 = o["int"], json::type_error); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH(s2 = o["int"], "[json.exception.type_error.302] (/int) type must be string, but is number"); +#else CHECK_THROWS_WITH(s2 = o["int"], "[json.exception.type_error.302] type must be string, but is number"); +#endif } #endif From ec0b1798bc7e45cd3aa6456e2cc829db08d1e69b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 2 Jan 2021 21:36:11 +0100 Subject: [PATCH 010/143] :construction: implement more parent relations --- CMakeLists.txt | 4 + include/nlohmann/detail/input/json_sax.hpp | 14 ++- include/nlohmann/json.hpp | 86 +++++++++++++++++- single_include/nlohmann/json.hpp | 100 +++++++++++++++++++-- test/src/unit-iterators2.cpp | 18 ++-- test/src/unit-json_patch.cpp | 14 ++- 6 files changed, 219 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index abd3a17c7f..36f1cf7056 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,10 @@ if (NOT JSON_ImplicitConversions) message(STATUS "Implicit conversions are disabled") endif() +if (JSON_Diagnostics) + message(STATUS "Diagnostics enabled") +endif() + ## ## TARGET ## create target and add include path diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 316f517234..61266188b9 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -298,12 +298,18 @@ class json_sax_dom_parser if (ref_stack.back()->is_array()) { ref_stack.back()->m_value.array->emplace_back(std::forward(v)); +#if JSON_DIAGNOSTICS + ref_stack.back()->m_value.array->back().m_parent = ref_stack.back(); +#endif return &(ref_stack.back()->m_value.array->back()); } JSON_ASSERT(ref_stack.back()->is_object()); JSON_ASSERT(object_element); *object_element = BasicJsonType(std::forward(v)); +#if JSON_DIAGNOSTICS + object_element->m_parent = ref_stack.back(); +#endif return object_element; } @@ -574,7 +580,10 @@ class json_sax_dom_callback_parser // array if (ref_stack.back()->is_array()) { - ref_stack.back()->m_value.array->push_back(std::move(value)); + ref_stack.back()->m_value.array->emplace_back(std::move(value)); +#if JSON_DIAGNOSTICS + ref_stack.back()->m_value.array->back().m_parent = ref_stack.back(); +#endif return {true, &(ref_stack.back()->m_value.array->back())}; } @@ -592,6 +601,9 @@ class json_sax_dom_callback_parser JSON_ASSERT(object_element); *object_element = std::move(value); +#if JSON_DIAGNOSTICS + object_element->m_parent = ref_stack.back(); +#endif return {true, object_element}; } diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 80bc871a2f..2fc497bae6 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1872,6 +1872,12 @@ class basic_json : m_type(value_t::array) { m_value.array = create(cnt, val); +#if JSON_DIAGNOSTICS + for (auto& entry : *m_value.array) + { + entry.m_parent = this; + } +#endif assert_invariant(); } @@ -2004,6 +2010,12 @@ class basic_json { m_value.object = create(first.m_it.object_iterator, last.m_it.object_iterator); +#if JSON_DIAGNOSTICS + for (auto& element : *m_value.object) + { + element.second.m_parent = this; + } +#endif break; } @@ -2011,6 +2023,12 @@ class basic_json { m_value.array = create(first.m_it.array_iterator, last.m_it.array_iterator); +#if JSON_DIAGNOSTICS + for (auto& element : *m_value.array) + { + element.m_parent = this; + } +#endif break; } @@ -2074,12 +2092,24 @@ class basic_json case value_t::object: { m_value = *other.m_value.object; +#if JSON_DIAGNOSTICS + for (auto& element : *m_value.object) + { + element.second.m_parent = this; + } +#endif break; } case value_t::array: { m_value = *other.m_value.array; +#if JSON_DIAGNOSTICS + for (auto& element : *m_value.array) + { + element.m_parent = this; + } +#endif break; } @@ -2205,6 +2235,9 @@ class basic_json using std::swap; swap(m_type, other.m_type); swap(m_value, other.m_value); +#if JSON_DIAGNOSTICS + m_parent = other.m_parent; +#endif assert_invariant(); return *this; @@ -2229,6 +2262,9 @@ class basic_json { assert_invariant(); m_value.destroy(m_type); +#if JSON_DIAGNOSTICS + m_parent = nullptr; +#endif } /// @} @@ -2751,7 +2787,7 @@ class basic_json return ""; } - return "(" + std::accumulate(tokens.begin(), tokens.end(), std::string{}, + return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, [](const std::string & a, const std::string & b) { return a + "/" + b; @@ -3604,7 +3640,10 @@ class basic_json idx - m_value.array->size() + 1, basic_json()); #if JSON_DIAGNOSTICS - m_value.array->back().m_parent = this; + for (std::size_t i = idx + 1; i < m_value.array->size(); ++i) + { + m_value.array->operator[](i).m_parent = this; + } #endif } @@ -5609,6 +5648,11 @@ class basic_json // add element to array (perfect forwarding) auto res = m_value.object->emplace(std::forward(args)...); + +#if JSON_DIAGNOSTICS + res.first->second.m_parent = this; +#endif + // create result iterator and set iterator to the result of emplace auto it = begin(); it.m_it.object_iterator = res.first; @@ -5671,7 +5715,13 @@ class basic_json } // insert to array and return iterator +#if JSON_DIAGNOSTICS + iterator result = insert_iterator(pos, val); + result->m_parent = this; + return result; +#else return insert_iterator(pos, val); +#endif } JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); @@ -5722,7 +5772,16 @@ class basic_json } // insert to array and return iterator +#if JSON_DIAGNOSTICS + iterator result = insert_iterator(pos, cnt, val); + for (size_type i = 0; i < cnt; ++i) + { + (result + i)->m_parent = this; + } + return result; +#else return insert_iterator(pos, cnt, val); +#endif } JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); @@ -5784,7 +5843,16 @@ class basic_json } // insert to array and return iterator +#if JSON_DIAGNOSTICS + iterator result = insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); + for (std::size_t i = 0; i < std::distance(first, last); ++i) + { + (result + i)->m_parent = this; + } + return result; +#else return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); +#endif } /*! @@ -5826,7 +5894,17 @@ class basic_json } // insert to array and return iterator +#if JSON_DIAGNOSTICS + const auto size = ilist.size(); + iterator result = insert_iterator(pos, ilist.begin(), ilist.end()); + for (std::size_t i = 0; i < size; ++i) + { + (result + i)->m_parent = this; + } + return result; +#else return insert_iterator(pos, ilist.begin(), ilist.end()); +#endif } /*! @@ -8387,7 +8465,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(idx > parent.size())) { // avoid undefined behavior - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, parent.diagnostics() + "array index " + std::to_string(idx) + " is out of range")); } // default case: insert add offset @@ -8548,7 +8626,7 @@ class basic_json // throw an exception if test fails if (JSON_HEDLEY_UNLIKELY(!success)) { - JSON_THROW(other_error::create(501, diagnostics() + "unsuccessful: " + val.dump())); + JSON_THROW(other_error::create(501, val.diagnostics() + "unsuccessful: " + val.dump())); } break; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 16a6049de5..f9bb662d1e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5590,12 +5590,18 @@ class json_sax_dom_parser if (ref_stack.back()->is_array()) { ref_stack.back()->m_value.array->emplace_back(std::forward(v)); +#if JSON_DIAGNOSTICS + ref_stack.back()->m_value.array->back().m_parent = ref_stack.back(); +#endif return &(ref_stack.back()->m_value.array->back()); } JSON_ASSERT(ref_stack.back()->is_object()); JSON_ASSERT(object_element); *object_element = BasicJsonType(std::forward(v)); +#if JSON_DIAGNOSTICS + object_element->m_parent = ref_stack.back(); +#endif return object_element; } @@ -5866,7 +5872,10 @@ class json_sax_dom_callback_parser // array if (ref_stack.back()->is_array()) { - ref_stack.back()->m_value.array->push_back(std::move(value)); + ref_stack.back()->m_value.array->emplace_back(std::move(value)); +#if JSON_DIAGNOSTICS + ref_stack.back()->m_value.array->back().m_parent = ref_stack.back(); +#endif return {true, &(ref_stack.back()->m_value.array->back())}; } @@ -5884,6 +5893,9 @@ class json_sax_dom_callback_parser JSON_ASSERT(object_element); *object_element = std::move(value); +#if JSON_DIAGNOSTICS + object_element->m_parent = ref_stack.back(); +#endif return {true, object_element}; } @@ -18496,6 +18508,12 @@ class basic_json : m_type(value_t::array) { m_value.array = create(cnt, val); +#if JSON_DIAGNOSTICS + for (auto& entry : *m_value.array) + { + entry.m_parent = this; + } +#endif assert_invariant(); } @@ -18628,6 +18646,12 @@ class basic_json { m_value.object = create(first.m_it.object_iterator, last.m_it.object_iterator); +#if JSON_DIAGNOSTICS + for (auto& element : *m_value.object) + { + element.second.m_parent = this; + } +#endif break; } @@ -18635,6 +18659,12 @@ class basic_json { m_value.array = create(first.m_it.array_iterator, last.m_it.array_iterator); +#if JSON_DIAGNOSTICS + for (auto& element : *m_value.array) + { + element.m_parent = this; + } +#endif break; } @@ -18698,12 +18728,24 @@ class basic_json case value_t::object: { m_value = *other.m_value.object; +#if JSON_DIAGNOSTICS + for (auto& element : *m_value.object) + { + element.second.m_parent = this; + } +#endif break; } case value_t::array: { m_value = *other.m_value.array; +#if JSON_DIAGNOSTICS + for (auto& element : *m_value.array) + { + element.m_parent = this; + } +#endif break; } @@ -18829,6 +18871,9 @@ class basic_json using std::swap; swap(m_type, other.m_type); swap(m_value, other.m_value); +#if JSON_DIAGNOSTICS + m_parent = other.m_parent; +#endif assert_invariant(); return *this; @@ -18853,6 +18898,9 @@ class basic_json { assert_invariant(); m_value.destroy(m_type); +#if JSON_DIAGNOSTICS + m_parent = nullptr; +#endif } /// @} @@ -19375,7 +19423,7 @@ class basic_json return ""; } - return "(" + std::accumulate(tokens.begin(), tokens.end(), std::string{}, + return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, [](const std::string & a, const std::string & b) { return a + "/" + b; @@ -20228,7 +20276,10 @@ class basic_json idx - m_value.array->size() + 1, basic_json()); #if JSON_DIAGNOSTICS - m_value.array->back().m_parent = this; + for (std::size_t i = idx + 1; i < m_value.array->size(); ++i) + { + m_value.array->operator[](i).m_parent = this; + } #endif } @@ -22233,6 +22284,11 @@ class basic_json // add element to array (perfect forwarding) auto res = m_value.object->emplace(std::forward(args)...); + +#if JSON_DIAGNOSTICS + res.first->second.m_parent = this; +#endif + // create result iterator and set iterator to the result of emplace auto it = begin(); it.m_it.object_iterator = res.first; @@ -22295,7 +22351,13 @@ class basic_json } // insert to array and return iterator +#if JSON_DIAGNOSTICS + iterator result = insert_iterator(pos, val); + result->m_parent = this; + return result; +#else return insert_iterator(pos, val); +#endif } JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); @@ -22346,7 +22408,16 @@ class basic_json } // insert to array and return iterator +#if JSON_DIAGNOSTICS + iterator result = insert_iterator(pos, cnt, val); + for (size_type i = 0; i < cnt; ++i) + { + (result + i)->m_parent = this; + } + return result; +#else return insert_iterator(pos, cnt, val); +#endif } JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); @@ -22408,7 +22479,16 @@ class basic_json } // insert to array and return iterator +#if JSON_DIAGNOSTICS + iterator result = insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); + for (std::size_t i = 0; i < std::distance(first, last); ++i) + { + (result + i)->m_parent = this; + } + return result; +#else return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); +#endif } /*! @@ -22450,7 +22530,17 @@ class basic_json } // insert to array and return iterator +#if JSON_DIAGNOSTICS + const auto size = ilist.size(); + iterator result = insert_iterator(pos, ilist.begin(), ilist.end()); + for (std::size_t i = 0; i < size; ++i) + { + (result + i)->m_parent = this; + } + return result; +#else return insert_iterator(pos, ilist.begin(), ilist.end()); +#endif } /*! @@ -25011,7 +25101,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(idx > parent.size())) { // avoid undefined behavior - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, parent.diagnostics() + "array index " + std::to_string(idx) + " is out of range")); } // default case: insert add offset @@ -25172,7 +25262,7 @@ class basic_json // throw an exception if test fails if (JSON_HEDLEY_UNLIKELY(!success)) { - JSON_THROW(other_error::create(501, diagnostics() + "unsuccessful: " + val.dump())); + JSON_THROW(other_error::create(501, val.diagnostics() + "unsuccessful: " + val.dump())); } break; diff --git a/test/src/unit-iterators2.cpp b/test/src/unit-iterators2.cpp index b9dcc22090..c17084c160 100644 --- a/test/src/unit-iterators2.cpp +++ b/test/src/unit-iterators2.cpp @@ -271,13 +271,16 @@ TEST_CASE("iterators 2") { CHECK_THROWS_AS(j.begin() == k.begin(), json::invalid_iterator&); CHECK_THROWS_AS(j.cbegin() == k.cbegin(), json::invalid_iterator&); - CHECK_THROWS_WITH(j.begin() == k.begin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers"); - CHECK_THROWS_WITH(j.cbegin() == k.cbegin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers"); - CHECK_THROWS_AS(j.begin() < k.begin(), json::invalid_iterator&); CHECK_THROWS_AS(j.cbegin() < k.cbegin(), json::invalid_iterator&); +#if JSON_DIAGNOSTICS + // the output differs in each loop, so we cannot fix a string for the expected exception +#else + CHECK_THROWS_WITH(j.begin() == k.begin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers"); + CHECK_THROWS_WITH(j.cbegin() == k.cbegin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers"); CHECK_THROWS_WITH(j.begin() < k.begin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers"); CHECK_THROWS_WITH(j.cbegin() < k.cbegin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers"); +#endif } } } @@ -750,13 +753,16 @@ TEST_CASE("iterators 2") { CHECK_THROWS_AS(j.rbegin() == k.rbegin(), json::invalid_iterator&); CHECK_THROWS_AS(j.crbegin() == k.crbegin(), json::invalid_iterator&); - CHECK_THROWS_WITH(j.rbegin() == k.rbegin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers"); - CHECK_THROWS_WITH(j.crbegin() == k.crbegin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers"); - CHECK_THROWS_AS(j.rbegin() < k.rbegin(), json::invalid_iterator&); CHECK_THROWS_AS(j.crbegin() < k.crbegin(), json::invalid_iterator&); +#if JSON_DIAGNOSTICS + // the output differs in each loop, so we cannot fix a string for the expected exception +#else + CHECK_THROWS_WITH(j.rbegin() == k.rbegin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers"); + CHECK_THROWS_WITH(j.crbegin() == k.crbegin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers"); CHECK_THROWS_WITH(j.rbegin() < k.rbegin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers"); CHECK_THROWS_WITH(j.crbegin() < k.crbegin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers"); +#endif } } } diff --git a/test/src/unit-json_patch.cpp b/test/src/unit-json_patch.cpp index 2ad7aadb82..cf11b5603b 100644 --- a/test/src/unit-json_patch.cpp +++ b/test/src/unit-json_patch.cpp @@ -343,7 +343,11 @@ TEST_CASE("JSON patch") // check that evaluation throws CHECK_THROWS_AS(doc.patch(patch), json::other_error&); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH_STD_STR(doc.patch(patch), "[json.exception.other_error.501] (/0) unsuccessful: " + patch[0].dump()); +#else CHECK_THROWS_WITH_STD_STR(doc.patch(patch), "[json.exception.other_error.501] unsuccessful: " + patch[0].dump()); +#endif } SECTION("A.10. Adding a Nested Member Object") @@ -484,7 +488,11 @@ TEST_CASE("JSON patch") // check that evaluation throws CHECK_THROWS_AS(doc.patch(patch), json::other_error&); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH_STD_STR(doc.patch(patch), "[json.exception.other_error.501] (/0) unsuccessful: " + patch[0].dump()); +#else CHECK_THROWS_WITH_STD_STR(doc.patch(patch), "[json.exception.other_error.501] unsuccessful: " + patch[0].dump()); +#endif } SECTION("A.16. Adding an Array Value") @@ -1183,7 +1191,11 @@ TEST_CASE("JSON patch") // the test will fail CHECK_THROWS_AS(doc.patch(patch), json::other_error&); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH_STD_STR(doc.patch(patch), "[json.exception.other_error.501] (/0) unsuccessful: " + patch[0].dump()); +#else CHECK_THROWS_WITH_STD_STR(doc.patch(patch), "[json.exception.other_error.501] unsuccessful: " + patch[0].dump()); +#endif } } } @@ -1268,7 +1280,7 @@ TEST_CASE("JSON patch") std::ifstream f(filename); json suite = json::parse(f); - for (const auto& test : suite) + for (const auto test : suite) { INFO_WITH_TEMP(test.value("comment", "")); From 294fa343d54634856dfef4a645de5241bd2dcde1 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 3 Jan 2021 20:06:32 +0100 Subject: [PATCH 011/143] :bug: fix bug in move constructor --- include/nlohmann/json.hpp | 30 ++++++++++++++++++++++++++++-- single_include/nlohmann/json.hpp | 30 ++++++++++++++++++++++++++++-- test/src/unit-json_patch.cpp | 2 +- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 2fc497bae6..a8ae989574 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1873,9 +1873,9 @@ class basic_json { m_value.array = create(cnt, val); #if JSON_DIAGNOSTICS - for (auto& entry : *m_value.array) + for (auto& element : *m_value.array) { - entry.m_parent = this; + element.m_parent = this; } #endif assert_invariant(); @@ -2196,6 +2196,32 @@ class basic_json other.m_type = value_t::null; other.m_value = {}; +#if JSON_DIAGNOSTICS + switch (m_type) + { + case value_t::array: + { + for (auto& element : *m_value.array) + { + element.m_parent = this; + } + break; + } + + case value_t::object: + { + for (auto& element : *m_value.object) + { + element.second.m_parent = this; + } + break; + } + + default: + break; + } +#endif + assert_invariant(); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f9bb662d1e..210f33141d 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18509,9 +18509,9 @@ class basic_json { m_value.array = create(cnt, val); #if JSON_DIAGNOSTICS - for (auto& entry : *m_value.array) + for (auto& element : *m_value.array) { - entry.m_parent = this; + element.m_parent = this; } #endif assert_invariant(); @@ -18832,6 +18832,32 @@ class basic_json other.m_type = value_t::null; other.m_value = {}; +#if JSON_DIAGNOSTICS + switch (m_type) + { + case value_t::array: + { + for (auto& element : *m_value.array) + { + element.m_parent = this; + } + break; + } + + case value_t::object: + { + for (auto& element : *m_value.object) + { + element.second.m_parent = this; + } + break; + } + + default: + break; + } +#endif + assert_invariant(); } diff --git a/test/src/unit-json_patch.cpp b/test/src/unit-json_patch.cpp index cf11b5603b..53570113b7 100644 --- a/test/src/unit-json_patch.cpp +++ b/test/src/unit-json_patch.cpp @@ -1280,7 +1280,7 @@ TEST_CASE("JSON patch") std::ifstream f(filename); json suite = json::parse(f); - for (const auto test : suite) + for (const auto& test : suite) { INFO_WITH_TEMP(test.value("comment", "")); From ddc3bb1992a9a2275f6d5e46c4daf33c8cc88e94 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 8 Jan 2021 11:09:58 +0100 Subject: [PATCH 012/143] :ok_hand: remove unnecessary assignment from destructor --- include/nlohmann/json.hpp | 3 --- single_include/nlohmann/json.hpp | 3 --- 2 files changed, 6 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 1eb65e2d7d..a911a62298 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -2288,9 +2288,6 @@ class basic_json { assert_invariant(); m_value.destroy(m_type); -#if JSON_DIAGNOSTICS - m_parent = nullptr; -#endif } /// @} diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 8987ead4f2..50e2581def 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18924,9 +18924,6 @@ class basic_json { assert_invariant(); m_value.destroy(m_type); -#if JSON_DIAGNOSTICS - m_parent = nullptr; -#endif } /// @} From 0617bd248d3f8e0c50aab4ddb5dd41e9132093dc Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 8 Jan 2021 11:10:24 +0100 Subject: [PATCH 013/143] :ok_hand: fix operator[] --- include/nlohmann/json.hpp | 13 +++++++++---- single_include/nlohmann/json.hpp | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index a911a62298..46e70d3e7e 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3659,11 +3659,16 @@ class basic_json // fill up array with null values if given idx is outside range if (idx >= m_value.array->size()) { - m_value.array->insert(m_value.array->end(), - idx - m_value.array->size() + 1, - basic_json()); #if JSON_DIAGNOSTICS - for (std::size_t i = idx + 1; i < m_value.array->size(); ++i) + // remember array size before resizing + const auto previous_size = m_value.array->size(); +#endif + + m_value.array->resize(idx + 1); + +#if JSON_DIAGNOSTICS + // set parent for values added above + for (auto i = previous_size; i <= idx; ++i) { m_value.array->operator[](i).m_parent = this; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 50e2581def..2f8c4a2cbc 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20295,11 +20295,16 @@ class basic_json // fill up array with null values if given idx is outside range if (idx >= m_value.array->size()) { - m_value.array->insert(m_value.array->end(), - idx - m_value.array->size() + 1, - basic_json()); #if JSON_DIAGNOSTICS - for (std::size_t i = idx + 1; i < m_value.array->size(); ++i) + // remember array size before resizing + const auto previous_size = m_value.array->size(); +#endif + + m_value.array->resize(idx + 1); + +#if JSON_DIAGNOSTICS + // set parent for values added above + for (auto i = previous_size; i <= idx; ++i) { m_value.array->operator[](i).m_parent = this; } From 04a0a071592e813d5595afd38be3fa9eeef17e9b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 8 Jan 2021 11:21:03 +0100 Subject: [PATCH 014/143] :ok_hand: fix move constructor and move assignment --- include/nlohmann/json.hpp | 13 ------------- single_include/nlohmann/json.hpp | 6 ------ 2 files changed, 19 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 46e70d3e7e..c1be2f4b1a 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -2185,9 +2185,6 @@ class basic_json basic_json(basic_json&& other) noexcept : m_type(std::move(other.m_type)), m_value(std::move(other.m_value)) -#if JSON_DIAGNOSTICS - , m_parent(other.m_parent) -#endif { // check that passed value is valid other.assert_invariant(); @@ -2261,9 +2258,6 @@ class basic_json using std::swap; swap(m_type, other.m_type); swap(m_value, other.m_value); -#if JSON_DIAGNOSTICS - m_parent = other.m_parent; -#endif assert_invariant(); return *this; @@ -3659,13 +3653,6 @@ class basic_json // fill up array with null values if given idx is outside range if (idx >= m_value.array->size()) { -#if JSON_DIAGNOSTICS - // remember array size before resizing - const auto previous_size = m_value.array->size(); -#endif - - m_value.array->resize(idx + 1); - #if JSON_DIAGNOSTICS // set parent for values added above for (auto i = previous_size; i <= idx; ++i) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2f8c4a2cbc..c17457dc2f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18821,9 +18821,6 @@ class basic_json basic_json(basic_json&& other) noexcept : m_type(std::move(other.m_type)), m_value(std::move(other.m_value)) -#if JSON_DIAGNOSTICS - , m_parent(other.m_parent) -#endif { // check that passed value is valid other.assert_invariant(); @@ -18897,9 +18894,6 @@ class basic_json using std::swap; swap(m_type, other.m_type); swap(m_value, other.m_value); -#if JSON_DIAGNOSTICS - m_parent = other.m_parent; -#endif assert_invariant(); return *this; From e4af1ddb189d75ef07b218f3f353a0b5e23f00f4 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 8 Jan 2021 11:21:41 +0100 Subject: [PATCH 015/143] :ok_hand: fix operator[] --- include/nlohmann/json.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index c1be2f4b1a..8318013c78 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3653,6 +3653,13 @@ class basic_json // fill up array with null values if given idx is outside range if (idx >= m_value.array->size()) { +#if JSON_DIAGNOSTICS + // remember array size before resizing + const auto previous_size = m_value.array->size(); +#endif + + m_value.array->resize(idx + 1); + #if JSON_DIAGNOSTICS // set parent for values added above for (auto i = previous_size; i <= idx; ++i) From d4a91b7445e88d81730ca75a5905e514f58e9d05 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 8 Jan 2021 11:29:28 +0100 Subject: [PATCH 016/143] :ok_hand: clean operator[] --- include/nlohmann/json.hpp | 6 ------ single_include/nlohmann/json.hpp | 6 ------ 2 files changed, 12 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 8318013c78..38bab38067 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3669,13 +3669,7 @@ class basic_json #endif } -#if JSON_DIAGNOSTICS - reference result = m_value.array->operator[](idx); - result.m_parent = this; - return result; -#else return m_value.array->operator[](idx); -#endif } JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index c17457dc2f..2e9768f771 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20305,13 +20305,7 @@ class basic_json #endif } -#if JSON_DIAGNOSTICS - reference result = m_value.array->operator[](idx); - result.m_parent = this; - return result; -#else return m_value.array->operator[](idx); -#endif } JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); From 43cd5c8a4d3c1239902ea4e106acdcf39647a47b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 8 Jan 2021 18:00:23 +0100 Subject: [PATCH 017/143] :ok_hand: fix constructor --- include/nlohmann/json.hpp | 15 +++++++++------ single_include/nlohmann/json.hpp | 15 +++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 38bab38067..13685dd10e 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1639,16 +1639,19 @@ class basic_json m_type = value_t::object; m_value = value_t::object; - std::for_each(init.begin(), init.end(), [this](const detail::json_ref& element_ref) + for (auto& element_ref : init) { auto element = element_ref.moved_or_copied(); + auto res = m_value.object->emplace( + std::move(*((*element.m_value.array)[0].m_value.string)), + std::move((*element.m_value.array)[1])); + #if JSON_DIAGNOSTICS - (*element.m_value.array)[1].m_parent = this; + res.first->second.m_parent = this; +#else + static_cast(res); // unused variable - fix warning #endif - m_value.object->emplace( - std::move(*((*element.m_value.array)[0].m_value.string)), - std::move((*element.m_value.array)[1])); - }); + } } else { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2e9768f771..59a829b5fb 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18275,16 +18275,19 @@ class basic_json m_type = value_t::object; m_value = value_t::object; - std::for_each(init.begin(), init.end(), [this](const detail::json_ref& element_ref) + for (auto& element_ref : init) { auto element = element_ref.moved_or_copied(); + auto res = m_value.object->emplace( + std::move(*((*element.m_value.array)[0].m_value.string)), + std::move((*element.m_value.array)[1])); + #if JSON_DIAGNOSTICS - (*element.m_value.array)[1].m_parent = this; + res.first->second.m_parent = this; +#else + static_cast(res); // unused variable - fix warning #endif - m_value.object->emplace( - std::move(*((*element.m_value.array)[0].m_value.string)), - std::move((*element.m_value.array)[1])); - }); + } } else { From 1e825e4f92b84bea316dbd935bfef0e1299b6ccf Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Sat, 9 Jan 2021 00:08:27 +0100 Subject: [PATCH 018/143] Add support for deserialization of STL containers of non-default constructable types (fixes #2574). --- include/nlohmann/adl_serializer.hpp | 27 ++-- .../nlohmann/detail/conversions/from_json.hpp | 95 +++++++++++- include/nlohmann/detail/meta/tag.hpp | 10 ++ include/nlohmann/detail/meta/type_traits.hpp | 3 +- single_include/nlohmann/json.hpp | 137 +++++++++++++++--- test/src/unit-regression2.cpp | 79 +++++++++- 6 files changed, 312 insertions(+), 39 deletions(-) create mode 100644 include/nlohmann/detail/meta/tag.hpp diff --git a/include/nlohmann/adl_serializer.hpp b/include/nlohmann/adl_serializer.hpp index 4af1c4bb1d..8200c28099 100644 --- a/include/nlohmann/adl_serializer.hpp +++ b/include/nlohmann/adl_serializer.hpp @@ -1,14 +1,16 @@ #pragma once +#include #include #include #include +#include namespace nlohmann { -template +template struct adl_serializer { /*! @@ -20,14 +22,22 @@ struct adl_serializer @param[in] j JSON value to read from @param[in,out] val value to write to */ - template - static auto from_json(BasicJsonType&& j, ValueType& val) noexcept( + template + static auto from_json(BasicJsonType && j, U& val) noexcept( noexcept(::nlohmann::from_json(std::forward(j), val))) -> decltype(::nlohmann::from_json(std::forward(j), val), void()) { ::nlohmann::from_json(std::forward(j), val); } + template + static auto from_json(BasicJsonType && j) noexcept( + noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) + -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) + { + return ::nlohmann::from_json(std::forward(j), detail::tag {}); + } + /*! @brief convert any value type to a JSON value @@ -37,13 +47,12 @@ struct adl_serializer @param[in,out] j JSON value to write to @param[in] val value to read from */ - template - static auto to_json(BasicJsonType& j, ValueType&& val) noexcept( - noexcept(::nlohmann::to_json(j, std::forward(val)))) - -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) + template + static auto to_json(BasicJsonType& j, U && val) noexcept( + noexcept(::nlohmann::to_json(j, std::forward(val)))) + -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) { - ::nlohmann::to_json(j, std::forward(val)); + ::nlohmann::to_json(j, std::forward(val)); } }; - } // namespace nlohmann diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 438b84a2e1..557faa5601 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -248,6 +249,27 @@ void()) from_json_array_impl(j, arr, priority_tag<3> {}); } +template < typename BasicJsonType, typename Array, std::size_t... Is > +Array from_json_array_impl(BasicJsonType&& j, tag /*unused*/, index_sequence /*unused*/) +{ + return { std::forward(j).at(Is).template get()... }; +} + +template < typename BasicJsonType, typename T, std::size_t N, + enable_if_t < !std::is_default_constructible>::value, int > = 0 > +auto from_json(BasicJsonType && j, tag> t) +-> decltype(j.template get(), +from_json_array_impl(std::forward(j), t, make_index_sequence {})) +{ + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + + std::string(j.type_name()))); + } + + return from_json_array_impl(std::forward(j), t, make_index_sequence {}); +} + template void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin) { @@ -323,22 +345,71 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } } -template -void from_json(const BasicJsonType& j, std::pair& p) +template>::value, int> = 0> +void from_json(BasicJsonType && j, std::pair& p) +{ + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + + std::string(j.type_name()))); + } + + p = {std::forward(j).at(0).template get(), + std::forward(j).at(1).template get() + }; +} + +template < typename BasicJsonType, class A1, class A2, + enable_if_t < !std::is_default_constructible>::value, int > = 0 > +std::pair from_json(BasicJsonType && j, tag> /*unused*/) +{ + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + + std::string(j.type_name()))); + } + + return {std::forward(j).at(0).template get(), + std::forward(j).at(1).template get()}; +} + +template +void from_json_tuple_impl(BasicJsonType&& j, Tuple& t, index_sequence /*unused*/) { - p = {j.at(0).template get(), j.at(1).template get()}; + t = std::make_tuple(std::forward(j).at(Idx).template get::type>()...); +} + +template>::value, int > = 0 > +void from_json(BasicJsonType && j, std::tuple& t) +{ + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + + std::string(j.type_name()))); + } + + from_json_tuple_impl(std::forward(j), t, index_sequence_for {}); } template -void from_json_tuple_impl(const BasicJsonType& j, Tuple& t, index_sequence /*unused*/) +Tuple from_json_tuple_impl(BasicJsonType&& j, tag /*unused*/, index_sequence /*unused*/) { - t = std::make_tuple(j.at(Idx).template get::type>()...); + return std::make_tuple(std::forward(j).at(Idx).template get::type>()...); } -template -void from_json(const BasicJsonType& j, std::tuple& t) +template < typename BasicJsonType, typename... Args, + enable_if_t < !std::is_default_constructible>::value, int > = 0 > +std::tuple from_json(BasicJsonType && j, tag> t) { - from_json_tuple_impl(j, t, index_sequence_for {}); + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + + std::string(j.type_name()))); + } + + return from_json_tuple_impl(std::forward(j), t, index_sequence_for {}); } template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, @@ -390,6 +461,14 @@ struct from_json_fn { return from_json(j, val); } + + template + auto operator()(const BasicJsonType& j, detail::tag t) const + noexcept(noexcept(from_json(j, t))) + -> decltype(from_json(j, t)) + { + return from_json(j, t); + } }; } // namespace detail diff --git a/include/nlohmann/detail/meta/tag.hpp b/include/nlohmann/detail/meta/tag.hpp new file mode 100644 index 0000000000..631887d1d2 --- /dev/null +++ b/include/nlohmann/detail/meta/tag.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace nlohmann +{ +namespace detail +{ +// dispatching helper struct +template struct tag {}; +} // namespace detail +} // namespace nlohmann diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index 1706cbdc6a..e30d99e4ce 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -106,8 +106,7 @@ struct is_getable }; template -struct has_from_json < BasicJsonType, T, - enable_if_t < !is_basic_json::value >> +struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> { using serializer = typename BasicJsonType::template json_serializer; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 492118a5f8..fd28115030 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -49,6 +49,7 @@ SOFTWARE. // #include +#include #include // #include @@ -2812,6 +2813,18 @@ constexpr T static_const::value; } // namespace detail } // namespace nlohmann +// #include + + +namespace nlohmann +{ +namespace detail +{ +// dispatching helper struct +template struct tag {}; +} // namespace detail +} // namespace nlohmann + // #include @@ -3129,8 +3142,7 @@ struct is_getable }; template -struct has_from_json < BasicJsonType, T, - enable_if_t < !is_basic_json::value >> +struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> { using serializer = typename BasicJsonType::template json_serializer; @@ -3734,6 +3746,27 @@ void()) from_json_array_impl(j, arr, priority_tag<3> {}); } +template < typename BasicJsonType, typename Array, std::size_t... Is > +Array from_json_array_impl(BasicJsonType&& j, tag /*unused*/, index_sequence /*unused*/) +{ + return { std::forward(j).at(Is).template get()... }; +} + +template < typename BasicJsonType, typename T, std::size_t N, + enable_if_t < !std::is_default_constructible>::value, int > = 0 > +auto from_json(BasicJsonType && j, tag> t) +-> decltype(j.template get(), +from_json_array_impl(std::forward(j), t, make_index_sequence {})) +{ + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + + std::string(j.type_name()))); + } + + return from_json_array_impl(std::forward(j), t, make_index_sequence {}); +} + template void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin) { @@ -3809,22 +3842,71 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } } -template -void from_json(const BasicJsonType& j, std::pair& p) +template>::value, int> = 0> +void from_json(BasicJsonType && j, std::pair& p) { - p = {j.at(0).template get(), j.at(1).template get()}; + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + + std::string(j.type_name()))); + } + + p = {std::forward(j).at(0).template get(), + std::forward(j).at(1).template get() + }; +} + +template < typename BasicJsonType, class A1, class A2, + enable_if_t < !std::is_default_constructible>::value, int > = 0 > +std::pair from_json(BasicJsonType && j, tag> /*unused*/) +{ + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + + std::string(j.type_name()))); + } + + return {std::forward(j).at(0).template get(), + std::forward(j).at(1).template get()}; } template -void from_json_tuple_impl(const BasicJsonType& j, Tuple& t, index_sequence /*unused*/) +void from_json_tuple_impl(BasicJsonType&& j, Tuple& t, index_sequence /*unused*/) { - t = std::make_tuple(j.at(Idx).template get::type>()...); + t = std::make_tuple(std::forward(j).at(Idx).template get::type>()...); } -template -void from_json(const BasicJsonType& j, std::tuple& t) +template>::value, int > = 0 > +void from_json(BasicJsonType && j, std::tuple& t) { - from_json_tuple_impl(j, t, index_sequence_for {}); + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + + std::string(j.type_name()))); + } + + from_json_tuple_impl(std::forward(j), t, index_sequence_for {}); +} + +template +Tuple from_json_tuple_impl(BasicJsonType&& j, tag /*unused*/, index_sequence /*unused*/) +{ + return std::make_tuple(std::forward(j).at(Idx).template get::type>()...); +} + +template < typename BasicJsonType, typename... Args, + enable_if_t < !std::is_default_constructible>::value, int > = 0 > +std::tuple from_json(BasicJsonType && j, tag> t) +{ + if (JSON_HEDLEY_UNLIKELY(!j.is_array())) + { + JSON_THROW(type_error::create(302, "type must be array, but is " + + std::string(j.type_name()))); + } + + return from_json_tuple_impl(std::forward(j), t, index_sequence_for {}); } template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, @@ -3876,6 +3958,14 @@ struct from_json_fn { return from_json(j, val); } + + template + auto operator()(const BasicJsonType& j, detail::tag t) const + noexcept(noexcept(from_json(j, t))) + -> decltype(from_json(j, t)) + { + return from_json(j, t); + } }; } // namespace detail @@ -4448,11 +4538,13 @@ constexpr const auto& to_json = detail::static_const::value; } // namespace } // namespace nlohmann +// #include + namespace nlohmann { -template +template struct adl_serializer { /*! @@ -4464,14 +4556,22 @@ struct adl_serializer @param[in] j JSON value to read from @param[in,out] val value to write to */ - template - static auto from_json(BasicJsonType&& j, ValueType& val) noexcept( + template + static auto from_json(BasicJsonType && j, U& val) noexcept( noexcept(::nlohmann::from_json(std::forward(j), val))) -> decltype(::nlohmann::from_json(std::forward(j), val), void()) { ::nlohmann::from_json(std::forward(j), val); } + template + static auto from_json(BasicJsonType && j) noexcept( + noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) + -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) + { + return ::nlohmann::from_json(std::forward(j), detail::tag {}); + } + /*! @brief convert any value type to a JSON value @@ -4481,15 +4581,14 @@ struct adl_serializer @param[in,out] j JSON value to write to @param[in] val value to read from */ - template - static auto to_json(BasicJsonType& j, ValueType&& val) noexcept( - noexcept(::nlohmann::to_json(j, std::forward(val)))) - -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) + template + static auto to_json(BasicJsonType& j, U && val) noexcept( + noexcept(::nlohmann::to_json(j, std::forward(val)))) + -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) { - ::nlohmann::to_json(j, std::forward(val)); + ::nlohmann::to_json(j, std::forward(val)); } }; - } // namespace nlohmann // #include diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 1e8c4922a7..e2adff8623 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -115,7 +115,7 @@ namespace nlohmann template <> struct adl_serializer { - static NonDefaultFromJsonStruct from_json (json const&) noexcept + static NonDefaultFromJsonStruct from_json (const json&) noexcept { return {}; } @@ -133,6 +133,28 @@ struct NotSerializableData }; +///////////////////////////////////////////////////////////////////// +// for #2574 +///////////////////////////////////////////////////////////////////// +struct NonDefaultConstructible +{ + explicit NonDefaultConstructible (int x) : x(x) { } + int x; +}; + +namespace nlohmann +{ +template <> +struct adl_serializer +{ + static NonDefaultConstructible from_json (const json& j) noexcept + { + return NonDefaultConstructible(j.get()); + } +}; +} + + TEST_CASE("regression tests 2") { SECTION("issue #1001 - Fix memory leak during parser callback") @@ -498,4 +520,59 @@ TEST_CASE("regression tests 2") CHECK(j.dump() == "\"Hello, world!\""); } #endif + + SECTION("issue #2574 - Deserialization to std::array, std::pair, and std::tuple with non-default constructable types fails") + { + SECTION("std::array") + { + json j = { 7, 4 }; + auto arr = j.get>(); + CHECK(arr[0].x == 7); + CHECK(arr[1].x == 4); + } + + SECTION("std::pair") + { + { + json j = { 3, 8 }; + auto x = j.at(0).get(); + CHECK(x.x == 3); + + auto p = j.get>(); + CHECK(p.first.x == 3); + CHECK(p.second.x == 8); + } + + { + json j = { 4, 1 }; + auto p = j.get>(); + CHECK(p.first == 4); + CHECK(p.second.x == 1); + } + + { + json j = { 6, 7 }; + auto p = j.get>(); + CHECK(p.first.x == 6); + CHECK(p.second == 7); + } + } + + SECTION("std::tuple") + { + { + json j = { 9 }; + auto t = j.get>(); + CHECK(std::get<0>(t).x == 9); + } + + { + json j = { 9, 8, 7 }; + auto t = j.get>(); + CHECK(std::get<0>(t).x == 9); + CHECK(std::get<1>(t) == 8); + CHECK(std::get<2>(t).x == 7); + } + } + } } From c0a8b45bbb4c9d857f1672a6199c1e8659ea9cd2 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Sat, 9 Jan 2021 17:45:56 +0100 Subject: [PATCH 019/143] Renamed template parameter and added some comments. --- include/nlohmann/adl_serializer.hpp | 36 ++++++++++++------ .../nlohmann/detail/conversions/from_json.hpp | 2 + single_include/nlohmann/json.hpp | 38 +++++++++++++------ 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/include/nlohmann/adl_serializer.hpp b/include/nlohmann/adl_serializer.hpp index 8200c28099..9eb751b705 100644 --- a/include/nlohmann/adl_serializer.hpp +++ b/include/nlohmann/adl_serializer.hpp @@ -19,23 +19,37 @@ struct adl_serializer This function is usually called by the `get()` function of the @ref basic_json class (either explicit or via conversion operators). + @note This function is chosen for value types which can be default constructed. + @param[in] j JSON value to read from @param[in,out] val value to write to */ - template - static auto from_json(BasicJsonType && j, U& val) noexcept( + template + static auto from_json(BasicJsonType && j, TargetType& val) noexcept( noexcept(::nlohmann::from_json(std::forward(j), val))) -> decltype(::nlohmann::from_json(std::forward(j), val), void()) { ::nlohmann::from_json(std::forward(j), val); } - template + /*! + @brief convert a JSON value to any value type + + This function is usually called by the `get()` function of the + @ref basic_json class (either explicit or via conversion operators). + + @note This function is chosen for value types which can not be default constructed. + + @param[in] j JSON value to read from + + @return copy of the JSON value, converted to @a ValueType + */ + template static auto from_json(BasicJsonType && j) noexcept( - noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) - -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) + noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) + -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) { - return ::nlohmann::from_json(std::forward(j), detail::tag {}); + return ::nlohmann::from_json(std::forward(j), detail::tag {}); } /*! @@ -47,12 +61,12 @@ struct adl_serializer @param[in,out] j JSON value to write to @param[in] val value to read from */ - template - static auto to_json(BasicJsonType& j, U && val) noexcept( - noexcept(::nlohmann::to_json(j, std::forward(val)))) - -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) + template + static auto to_json(BasicJsonType& j, TargetType && val) noexcept( + noexcept(::nlohmann::to_json(j, std::forward(val)))) + -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) { - ::nlohmann::to_json(j, std::forward(val)); + ::nlohmann::to_json(j, std::forward(val)); } }; } // namespace nlohmann diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 557faa5601..c9b88ce5b9 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -462,6 +462,8 @@ struct from_json_fn return from_json(j, val); } + // overload to pass calls to built-in from_json functions for non-default constructible STL + // types (e.g. std::array, where X is not default constructible). template auto operator()(const BasicJsonType& j, detail::tag t) const noexcept(noexcept(from_json(j, t))) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index fd28115030..06f7caa378 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3959,6 +3959,8 @@ struct from_json_fn return from_json(j, val); } + // overload to pass calls to built-in from_json functions for non-default constructible STL + // types (e.g. std::array, where X is not default constructible). template auto operator()(const BasicJsonType& j, detail::tag t) const noexcept(noexcept(from_json(j, t))) @@ -4553,23 +4555,37 @@ struct adl_serializer This function is usually called by the `get()` function of the @ref basic_json class (either explicit or via conversion operators). + @note This function is chosen for value types which can be default constructed. + @param[in] j JSON value to read from @param[in,out] val value to write to */ - template - static auto from_json(BasicJsonType && j, U& val) noexcept( + template + static auto from_json(BasicJsonType && j, TargetType& val) noexcept( noexcept(::nlohmann::from_json(std::forward(j), val))) -> decltype(::nlohmann::from_json(std::forward(j), val), void()) { ::nlohmann::from_json(std::forward(j), val); } - template + /*! + @brief convert a JSON value to any value type + + This function is usually called by the `get()` function of the + @ref basic_json class (either explicit or via conversion operators). + + @note This function is chosen for value types which can not be default constructed. + + @param[in] j JSON value to read from + + @return copy of the JSON value, converted to @a ValueType + */ + template static auto from_json(BasicJsonType && j) noexcept( - noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) - -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) + noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) + -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) { - return ::nlohmann::from_json(std::forward(j), detail::tag {}); + return ::nlohmann::from_json(std::forward(j), detail::tag {}); } /*! @@ -4581,12 +4597,12 @@ struct adl_serializer @param[in,out] j JSON value to write to @param[in] val value to read from */ - template - static auto to_json(BasicJsonType& j, U && val) noexcept( - noexcept(::nlohmann::to_json(j, std::forward(val)))) - -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) + template + static auto to_json(BasicJsonType& j, TargetType && val) noexcept( + noexcept(::nlohmann::to_json(j, std::forward(val)))) + -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) { - ::nlohmann::to_json(j, std::forward(val)); + ::nlohmann::to_json(j, std::forward(val)); } }; } // namespace nlohmann From 1b113f73c2c79e7bd2c659b153becba747007bb1 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Sat, 9 Jan 2021 17:54:56 +0100 Subject: [PATCH 020/143] Added extra tests to improve coverage. --- test/src/unit-regression2.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index e2adff8623..1046bb2e58 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -525,10 +525,18 @@ TEST_CASE("regression tests 2") { SECTION("std::array") { - json j = { 7, 4 }; - auto arr = j.get>(); - CHECK(arr[0].x == 7); - CHECK(arr[1].x == 4); + { + json j = { 7, 4 }; + auto arr = j.get>(); + CHECK(arr[0].x == 7); + CHECK(arr[1].x == 4); + + } + + { + json j = 7; + CHECK_THROWS_AS((j.get>()), json::type_error); + } } SECTION("std::pair") @@ -556,6 +564,11 @@ TEST_CASE("regression tests 2") CHECK(p.first.x == 6); CHECK(p.second == 7); } + + { + json j = 7; + CHECK_THROWS_AS((j.get>()), json::type_error); + } } SECTION("std::tuple") @@ -573,6 +586,11 @@ TEST_CASE("regression tests 2") CHECK(std::get<1>(t) == 8); CHECK(std::get<2>(t).x == 7); } + + { + json j = 7; + CHECK_THROWS_AS((j.get>()), json::type_error); + } } } } From e160749003dd351805cc058b655d50b3b606becd Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 9 Jan 2021 19:21:18 +0100 Subject: [PATCH 021/143] :recycle: move diagnostic code in header --- .../nlohmann/detail/conversions/from_json.hpp | 32 +- include/nlohmann/detail/diagnostics_t.hpp | 85 +++ include/nlohmann/detail/exceptions.hpp | 31 +- .../nlohmann/detail/input/binary_reader.hpp | 38 +- include/nlohmann/detail/input/json_sax.hpp | 13 +- include/nlohmann/detail/input/parser.hpp | 26 +- .../nlohmann/detail/iterators/iter_impl.hpp | 26 +- include/nlohmann/detail/json_pointer.hpp | 49 +- .../nlohmann/detail/output/binary_writer.hpp | 8 +- include/nlohmann/detail/output/serializer.hpp | 5 +- include/nlohmann/json.hpp | 201 ++--- single_include/nlohmann/json.hpp | 687 ++++++++++-------- test/src/unit-json_patch.cpp | 95 ++- 13 files changed, 748 insertions(+), 548 deletions(-) create mode 100644 include/nlohmann/detail/diagnostics_t.hpp diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 0feea28330..2a8d7543e2 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -13,6 +13,7 @@ #include // valarray #include +#include #include #include #include @@ -27,7 +28,7 @@ void from_json(const BasicJsonType& j, typename std::nullptr_t& n) { if (JSON_HEDLEY_UNLIKELY(!j.is_null())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be null, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } n = nullptr; } @@ -58,7 +59,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } } @@ -67,7 +68,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { if (JSON_HEDLEY_UNLIKELY(!j.is_boolean())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be boolean, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } b = *j.template get_ptr(); } @@ -77,7 +78,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } s = *j.template get_ptr(); } @@ -93,7 +94,7 @@ void from_json(const BasicJsonType& j, ConstructibleStringType& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } s = *j.template get_ptr(); @@ -133,7 +134,7 @@ void from_json(const BasicJsonType& j, std::forward_list& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } l.clear(); std::transform(j.rbegin(), j.rend(), @@ -150,7 +151,7 @@ void from_json(const BasicJsonType& j, std::valarray& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } l.resize(j.size()); std::transform(j.begin(), j.end(), std::begin(l), @@ -241,8 +242,7 @@ void()) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + - std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } from_json_array_impl(j, arr, priority_tag<3> {}); @@ -253,7 +253,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin) { if (JSON_HEDLEY_UNLIKELY(!j.is_binary())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be binary, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } bin = *j.template get_ptr(); @@ -265,7 +265,7 @@ void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) { if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } ConstructibleObjectType ret; @@ -319,7 +319,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } } @@ -348,14 +348,14 @@ void from_json(const BasicJsonType& j, std::map& { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(p.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t(j))); } m.emplace(p.at(0).template get(), p.at(1).template get()); } @@ -368,14 +368,14 @@ void from_json(const BasicJsonType& j, std::unordered_map(j))); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(p.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t(j))); } m.emplace(p.at(0).template get(), p.at(1).template get()); } diff --git a/include/nlohmann/detail/diagnostics_t.hpp b/include/nlohmann/detail/diagnostics_t.hpp new file mode 100644 index 0000000000..727e82350f --- /dev/null +++ b/include/nlohmann/detail/diagnostics_t.hpp @@ -0,0 +1,85 @@ +#pragma once + +#include +#include +#include + +namespace nlohmann +{ +namespace detail +{ + +template +class diagnostics_t +{ + public: + diagnostics_t() noexcept = default; + diagnostics_t(const BasicJsonType& j) noexcept + : m_j(&j) + {} + + std::string diagnostics() const + { +#if JSON_DIAGNOSTICS + if (m_j == nullptr) + { + return ""; + } + + std::vector tokens; + for (const auto* current = m_j; current->m_parent != nullptr; current = current->m_parent) + { + switch (current->m_parent->type()) + { + case value_t::array: + { + for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) + { + if (current->m_parent->m_value.array->operator[](i) == *current) + { + tokens.emplace_back(std::to_string(i)); + continue; + } + } + break; + } + + case value_t::object: + { + for (const auto& element : *current->m_parent->m_value.object) + { + if (element.second == *current) + { + tokens.emplace_back(element.first.c_str()); + continue; + } + } + break; + } + + default: + break; + } + } + + if (tokens.empty()) + { + return ""; + } + + return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, + [](const std::string & a, const std::string & b) + { + return a + "/" + b; + }) + ") "; +#else + return ""; +#endif + } + + private: + const BasicJsonType* m_j = static_cast(nullptr); +}; + +} // namespace detail +} // namespace nlohmann diff --git a/include/nlohmann/detail/exceptions.hpp b/include/nlohmann/detail/exceptions.hpp index dd92897d5a..56a582f794 100644 --- a/include/nlohmann/detail/exceptions.hpp +++ b/include/nlohmann/detail/exceptions.hpp @@ -4,6 +4,7 @@ #include // runtime_error #include // to_string +#include #include #include @@ -127,18 +128,20 @@ class parse_error : public exception @param[in] what_arg the explanatory string @return parse_error object */ - static parse_error create(int id_, const position_t& pos, const std::string& what_arg) + template + static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { std::string w = exception::name("parse_error", id_) + "parse error" + - position_string(pos) + ": " + what_arg; + position_string(pos) + ": " + diagnostics.diagnostics() + what_arg; return parse_error(id_, pos.chars_read_total, w.c_str()); } - static parse_error create(int id_, std::size_t byte_, const std::string& what_arg) + template + static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { std::string w = exception::name("parse_error", id_) + "parse error" + (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + - ": " + what_arg; + ": " + diagnostics.diagnostics() + what_arg; return parse_error(id_, byte_, w.c_str()); } @@ -204,9 +207,10 @@ caught.,invalid_iterator} class invalid_iterator : public exception { public: - static invalid_iterator create(int id_, const std::string& what_arg) + template + static invalid_iterator create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("invalid_iterator", id_) + what_arg; + std::string w = exception::name("invalid_iterator", id_) + diagnostics.diagnostics() + what_arg; return invalid_iterator(id_, w.c_str()); } @@ -258,9 +262,10 @@ caught.,type_error} class type_error : public exception { public: - static type_error create(int id_, const std::string& what_arg) + template + static type_error create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("type_error", id_) + what_arg; + std::string w = exception::name("type_error", id_) + diagnostics.diagnostics() + what_arg; return type_error(id_, w.c_str()); } @@ -305,9 +310,10 @@ caught.,out_of_range} class out_of_range : public exception { public: - static out_of_range create(int id_, const std::string& what_arg) + template + static out_of_range create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("out_of_range", id_) + what_arg; + std::string w = exception::name("out_of_range", id_) + diagnostics.diagnostics() + what_arg; return out_of_range(id_, w.c_str()); } @@ -343,9 +349,10 @@ caught.,other_error} class other_error : public exception { public: - static other_error create(int id_, const std::string& what_arg) + template + static other_error create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("other_error", id_) + what_arg; + std::string w = exception::name("other_error", id_) + diagnostics.diagnostics() + what_arg; return other_error(id_, w.c_str()); } diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 806e360306..a896f15586 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -13,6 +13,7 @@ #include // make_pair, move #include // vector +#include #include #include #include @@ -64,6 +65,7 @@ class binary_reader using json_sax_t = SAX; using char_type = typename InputAdapterType::char_type; using char_int_type = typename std::char_traits::int_type; + using diagnostics_t = detail::diagnostics_t; public: /*! @@ -137,7 +139,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current != std::char_traits::eof())) { return sax->parse_error(chars_read, get_token_string(), - parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"))); + parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"), diagnostics_t())); } } @@ -213,7 +215,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 1)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"), diagnostics_t())); } return get_string(input_format_t::bson, len - static_cast(1), result) && get() != std::char_traits::eof(); @@ -234,7 +236,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 0)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"), diagnostics_t())); } // All BSON binary values have a subtype @@ -316,7 +318,7 @@ class binary_reader { std::array cr{{}}; (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type)); - return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()))); + return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), diagnostics_t())); } } } @@ -716,7 +718,7 @@ class binary_reader case cbor_tag_handler_t::error: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } case cbor_tag_handler_t::ignore: @@ -831,7 +833,7 @@ class binary_reader default: // anything else (0xFF is handled inside the other types) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } } } @@ -926,7 +928,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"), diagnostics_t())); } } } @@ -1025,7 +1027,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"), diagnostics_t())); } } } @@ -1492,7 +1494,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } } } @@ -1574,7 +1576,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"), diagnostics_t())); } } } @@ -1824,7 +1826,7 @@ class binary_reader default: auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), diagnostics_t())); } } @@ -1894,7 +1896,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), diagnostics_t())); } } } @@ -1932,7 +1934,7 @@ class binary_reader return false; } auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"), diagnostics_t())); } return get_ubjson_size_value(result.first); @@ -2022,7 +2024,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current > 127)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"), diagnostics_t())); } string_t s(1, static_cast(current)); return sax->string(s); @@ -2043,7 +2045,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } } } @@ -2221,7 +2223,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input)) { - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"))); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), diagnostics_t())); } switch (result_number) @@ -2233,7 +2235,7 @@ class binary_reader case token_type::value_float: return sax->number_float(number_lexer.get_number_float(), std::move(number_string)); default: - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"))); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), diagnostics_t())); } } @@ -2389,7 +2391,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current == std::char_traits::eof())) { return sax->parse_error(chars_read, "", - parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context))); + parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), diagnostics_t())); } return true; } diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 61266188b9..f29aa1a682 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -5,6 +5,7 @@ #include // move #include // vector +#include #include #include @@ -154,6 +155,7 @@ class json_sax_dom_parser using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; using binary_t = typename BasicJsonType::binary_t; + using diagnostics_t = detail::diagnostics_t; /*! @param[in, out] r reference to a JSON value that is manipulated while @@ -219,8 +221,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + - "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -245,8 +246,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + - "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -336,6 +336,7 @@ class json_sax_dom_callback_parser using binary_t = typename BasicJsonType::binary_t; using parser_callback_t = typename BasicJsonType::parser_callback_t; using parse_event_t = typename BasicJsonType::parse_event_t; + using diagnostics_t = detail::diagnostics_t; json_sax_dom_callback_parser(BasicJsonType& r, const parser_callback_t cb, @@ -406,7 +407,7 @@ class json_sax_dom_callback_parser // check object limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -469,7 +470,7 @@ class json_sax_dom_callback_parser // check array limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index ffe483aa1e..40ef371eb9 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -8,6 +8,7 @@ #include // vector #include +#include #include #include #include @@ -57,6 +58,7 @@ class parser using string_t = typename BasicJsonType::string_t; using lexer_t = lexer; using token_type = typename lexer_t::token_type; + using diagnostics_t = detail::diagnostics_t; public: /// a parser reading from an input adapter @@ -96,7 +98,7 @@ class parser sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), diagnostics_t())); } // in case of an error, return discarded value @@ -125,7 +127,7 @@ class parser sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), diagnostics_t())); } // in case of an error, return discarded value @@ -162,7 +164,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), diagnostics_t())); } return result; @@ -209,7 +211,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); + exception_message(token_type::value_string, "object key"), diagnostics_t())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) { @@ -222,7 +224,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); + exception_message(token_type::name_separator, "object separator"), diagnostics_t())); } // remember we are now inside an object @@ -265,7 +267,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'")); + out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'", diagnostics_t())); } if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string()))) @@ -336,7 +338,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::uninitialized, "value"))); + exception_message(token_type::uninitialized, "value"), diagnostics_t())); } default: // the last token was unexpected @@ -344,7 +346,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::literal_or_value, "value"))); + exception_message(token_type::literal_or_value, "value"), diagnostics_t())); } } } @@ -391,7 +393,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_array, "array"))); + exception_message(token_type::end_array, "array"), diagnostics_t())); } else // object { @@ -404,7 +406,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); + exception_message(token_type::value_string, "object key"), diagnostics_t())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) @@ -418,7 +420,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); + exception_message(token_type::name_separator, "object separator"), diagnostics_t())); } // parse values @@ -447,7 +449,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_object, "object"))); + exception_message(token_type::end_object, "object"), diagnostics_t())); } } } diff --git a/include/nlohmann/detail/iterators/iter_impl.hpp b/include/nlohmann/detail/iterators/iter_impl.hpp index cceb8d05fc..565ac64d1a 100644 --- a/include/nlohmann/detail/iterators/iter_impl.hpp +++ b/include/nlohmann/detail/iterators/iter_impl.hpp @@ -3,6 +3,7 @@ #include // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next #include // conditional, is_const, remove_const +#include #include #include #include @@ -51,6 +52,7 @@ class iter_impl // make sure BasicJsonType is basic_json or const basic_json static_assert(is_basic_json::type>::value, "iter_impl only accepts (const) basic_json"); + using diagnostics_t = detail::diagnostics_t; public: @@ -257,7 +259,7 @@ class iter_impl } case value_t::null: - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); default: { @@ -266,7 +268,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); } } } @@ -300,7 +302,7 @@ class iter_impl return m_object; } - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); } } } @@ -401,7 +403,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, m_object->diagnostics() + "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", diagnostics_t(*m_object))); } JSON_ASSERT(m_object != nullptr); @@ -438,7 +440,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, m_object->diagnostics() + "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", diagnostics_t(*m_object))); } JSON_ASSERT(m_object != nullptr); @@ -446,7 +448,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(213, m_object->diagnostics() + "cannot compare order of object iterators")); + JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", diagnostics_t(*m_object))); case value_t::array: return (m_it.array_iterator < other.m_it.array_iterator); @@ -494,7 +496,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, m_object->diagnostics() + "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", diagnostics_t(*m_object))); case value_t::array: { @@ -565,7 +567,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, m_object->diagnostics() + "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", diagnostics_t(*m_object))); case value_t::array: return m_it.array_iterator - other.m_it.array_iterator; @@ -586,13 +588,13 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(208, m_object->diagnostics() + "cannot use operator[] for object iterators")); + JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", diagnostics_t(*m_object))); case value_t::array: return *std::next(m_it.array_iterator, n); case value_t::null: - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); default: { @@ -601,7 +603,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); } } } @@ -619,7 +621,7 @@ class iter_impl return m_it.object_iterator->first; } - JSON_THROW(invalid_iterator::create(207, m_object->diagnostics() + "cannot use key() for non-object iterators")); + JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", diagnostics_t(*m_object))); } /*! diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 865376cf18..f3a70bdcee 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -8,6 +8,7 @@ #include // move #include // vector +#include #include #include #include @@ -21,6 +22,8 @@ class json_pointer NLOHMANN_BASIC_JSON_TPL_DECLARATION friend class basic_json; + using diagnostics_t = detail::diagnostics_t; + public: /*! @brief create JSON pointer @@ -247,7 +250,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); } reference_tokens.pop_back(); @@ -271,7 +274,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); } return reference_tokens.back(); @@ -337,15 +340,13 @@ class json_pointer // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) { - JSON_THROW(detail::parse_error::create(106, 0, - "array index '" + s + - "' must not begin with '0'")); + JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", diagnostics_t())); } // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9'))) { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number")); + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", diagnostics_t())); } std::size_t processed_chars = 0; @@ -356,20 +357,20 @@ class json_pointer } JSON_CATCH(std::out_of_range&) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", diagnostics_t())); } // check if the string was completely read if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size())) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", diagnostics_t())); } // only triggered on special platforms (like 32bit), see also // https://github.com/nlohmann/json/pull/2203 if (res >= static_cast((std::numeric_limits::max)())) { - JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type")); // LCOV_EXCL_LINE + JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", diagnostics_t())); // LCOV_EXCL_LINE } return static_cast(res); @@ -380,7 +381,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); } json_pointer result = *this; @@ -443,7 +444,7 @@ class json_pointer single value; that is, with an empty list of reference tokens. */ default: - JSON_THROW(detail::type_error::create(313, "invalid value to unflatten")); + JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", diagnostics_t(j))); } } @@ -515,7 +516,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -548,7 +549,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + ") is out of range", diagnostics_t(*ptr))); } // note: at performs range check @@ -557,7 +558,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -595,9 +596,7 @@ class json_pointer if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) { // "-" cannot be used for const access - JSON_THROW(detail::out_of_range::create(402, - "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", diagnostics_t(*ptr))); } // use unchecked array access @@ -606,7 +605,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -639,7 +638,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + ") is out of range", diagnostics_t(*ptr))); } // note: at performs range check @@ -648,7 +647,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -752,9 +751,7 @@ class json_pointer // check if nonempty reference string begins with slash if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) { - JSON_THROW(detail::parse_error::create(107, 1, - "JSON pointer must be empty or begin with '/' - was: '" + - reference_string + "'")); + JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", diagnostics_t())); } // extract the reference tokens: @@ -789,7 +786,7 @@ class json_pointer (reference_token[pos + 1] != '0' && reference_token[pos + 1] != '1'))) { - JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'")); + JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", diagnostics_t())); } } @@ -916,7 +913,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!value.is_object())) { - JSON_THROW(detail::type_error::create(314, "only objects can be unflattened")); + JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", diagnostics_t(value))); } BasicJsonType result; @@ -926,7 +923,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive())) { - JSON_THROW(detail::type_error::create(315, "values in object must be primitive")); + JSON_THROW(detail::type_error::create(315, "values in object must be primitive", diagnostics_t(element))); } // assign value to reference pointed to by JSON pointer; Note that if diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 72ebbeda68..10bc34011b 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -8,6 +8,7 @@ #include // string #include // isnan, isinf +#include #include #include #include @@ -57,7 +58,7 @@ class binary_writer default: { - JSON_THROW(type_error::create(317, j.diagnostics() + "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), detail::diagnostics_t(j)));; } } } @@ -906,8 +907,7 @@ class binary_writer const auto it = name.find(static_cast(0)); if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) { - JSON_THROW(out_of_range::create(409, j.diagnostics() + - "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")")); + JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", detail::diagnostics_t(j))); } return /*id*/ 1ul + name.size() + /*zero-terminator*/1u; @@ -1031,7 +1031,7 @@ class binary_writer } else { - JSON_THROW(out_of_range::create(407, j.diagnostics() + "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64")); + JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", detail::diagnostics_t(j))); } } diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 0a34c8011e..3384141805 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -13,6 +13,7 @@ #include // move #include +#include #include #include #include @@ -499,7 +500,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn)); + JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, detail::diagnostics_t())); } case error_handler_t::ignore: @@ -593,7 +594,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn)); + JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, detail::diagnostics_t())); } case error_handler_t::ignore: diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 13685dd10e..086c0f5572 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -51,6 +51,7 @@ SOFTWARE. #include #include #include +#include #include #include #include @@ -189,10 +190,15 @@ class basic_json friend class ::nlohmann::detail::json_sax_dom_parser; template friend class ::nlohmann::detail::json_sax_dom_callback_parser; + template + friend class ::nlohmann::detail::diagnostics_t; /// workaround type for MSVC using basic_json_t = NLOHMANN_BASIC_JSON_TPL; + /// shortcut + using diagnostics_t = ::nlohmann::detail::diagnostics_t; + JSON_PRIVATE_UNLESS_TESTED: // convenience aliases for types residing in namespace detail; using lexer = ::nlohmann::detail::lexer_base; @@ -1060,7 +1066,7 @@ class basic_json object = nullptr; // silence warning, see #821 if (JSON_HEDLEY_UNLIKELY(t == value_t::null)) { - JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1")); // LCOV_EXCL_LINE + JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1", diagnostics_t())); // LCOV_EXCL_LINE } break; } @@ -1629,7 +1635,7 @@ class basic_json // if object is wanted but impossible, throw an exception if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object)) { - JSON_THROW(type_error::create(301, "cannot create object from initializer list")); + JSON_THROW(type_error::create(301, "cannot create object from initializer list", diagnostics_t())); } } @@ -1950,7 +1956,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(201, diagnostics() + "iterators are not compatible")); + JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", diagnostics_t())); } // copy type from first iterator @@ -1968,7 +1974,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, "iterators out of range")); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t())); } break; } @@ -2042,8 +2048,7 @@ class basic_json } default: - JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + - std::string(first.m_object->type_name()))); + JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), diagnostics_t())); } assert_invariant(); @@ -2763,60 +2768,6 @@ class basic_json /// @} - std::string diagnostics() const - { -#if JSON_DIAGNOSTICS - std::vector tokens; - for (const basic_json* current = this; current->m_parent != nullptr; current = current->m_parent) - { - switch (current->m_parent->type()) - { - case value_t::array: - { - for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) - { - if (current->m_parent->m_value.array->operator[](i) == *current) - { - tokens.emplace_back(std::to_string(i)); - continue; - } - } - break; - } - - case value_t::object: - { - for (const auto& element : *current->m_parent->m_value.object) - { - if (element.second == *current) - { - tokens.emplace_back(element.first.c_str()); - continue; - } - } - break; - } - - default: - break; - } - } - - if (tokens.empty()) - { - return ""; - } - - return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, - [](const std::string & a, const std::string & b) - { - return a + "/" + b; - }) + ") "; -#else - return ""; -#endif - } - private: ////////////////// // value access // @@ -2830,7 +2781,7 @@ class basic_json return m_value.boolean; } - JSON_THROW(type_error::create(302, diagnostics() + "type must be boolean, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()), diagnostics_t(*this))); } /// get a pointer to the value (object) @@ -2951,7 +2902,7 @@ class basic_json return *ptr; } - JSON_THROW(type_error::create(303, obj.diagnostics() + "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()))); + JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()), diagnostics_t(obj))); } public: @@ -3379,7 +3330,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this))); } return *get_ptr(); @@ -3390,7 +3341,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this))); } return *get_ptr(); @@ -3451,12 +3402,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -3498,12 +3449,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -3555,12 +3506,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -3606,12 +3557,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -3675,7 +3626,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -3705,7 +3656,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -3757,7 +3708,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -3799,7 +3750,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -3853,7 +3804,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -3897,7 +3848,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -3969,7 +3920,7 @@ class basic_json return default_value; } - JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -4042,7 +3993,7 @@ class basic_json } } - JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -4196,7 +4147,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != pos.m_object)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } IteratorType result = end(); @@ -4212,7 +4163,7 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin())) { - JSON_THROW(invalid_iterator::create(205, "iterator out of range")); + JSON_THROW(invalid_iterator::create(205, "iterator out of range", diagnostics_t(*this))); } if (is_string()) @@ -4248,7 +4199,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } return result; @@ -4309,7 +4260,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object)) { - JSON_THROW(invalid_iterator::create(203, diagnostics() + "iterators do not fit current value")); + JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", diagnostics_t(*this))); } IteratorType result = end(); @@ -4326,7 +4277,7 @@ class basic_json if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, diagnostics() + "iterators out of range")); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t(*this))); } if (is_string()) @@ -4364,7 +4315,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } return result; @@ -4407,7 +4358,7 @@ class basic_json return m_value.object->erase(key); } - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -4441,14 +4392,14 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(idx >= size())) { - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); } m_value.array->erase(m_value.array->begin() + static_cast(idx)); } else { - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -5393,7 +5344,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an array @@ -5431,7 +5382,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an array @@ -5484,7 +5435,7 @@ class basic_json // push_back only works for null objects or objects if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an object @@ -5592,7 +5543,7 @@ class basic_json // emplace_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an array @@ -5654,7 +5605,7 @@ class basic_json // emplace only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an object @@ -5730,7 +5681,7 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // insert to array and return iterator @@ -5743,7 +5694,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -5787,7 +5738,7 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // insert to array and return iterator @@ -5803,7 +5754,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -5841,24 +5792,24 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); } if (JSON_HEDLEY_UNLIKELY(first.m_object == this)) { - JSON_THROW(invalid_iterator::create(211, diagnostics() + "passed iterators may not belong to container")); + JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", diagnostics_t(*this))); } // insert to array and return iterator @@ -5903,13 +5854,13 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // insert to array and return iterator @@ -5954,19 +5905,19 @@ class basic_json // insert only works for objects if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this))); } m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator); @@ -6003,11 +5954,11 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this))); } if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(j.type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()), diagnostics_t(*this))); } for (auto it = j.cbegin(); it != j.cend(); ++it) @@ -6054,20 +6005,20 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object() || !last.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this))); } for (auto it = first; it != last; ++it) @@ -6162,7 +6113,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -6195,7 +6146,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -6228,7 +6179,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -6261,7 +6212,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -6275,7 +6226,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -8484,7 +8435,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(idx > parent.size())) { // avoid undefined behavior - JSON_THROW(out_of_range::create(401, parent.diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(parent))); } // default case: insert add offset @@ -8518,7 +8469,7 @@ class basic_json } else { - JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + last_path + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found", diagnostics_t(*this))); } } else if (parent.is_array()) @@ -8531,7 +8482,7 @@ class basic_json // type check: top level value must be an array if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array())) { - JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(json_patch))); } // iterate and apply the operations @@ -8551,13 +8502,13 @@ class basic_json // check if desired value is present if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end())) { - JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", diagnostics_t(val))); } // check if result is of type string if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string())) { - JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have string member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", diagnostics_t(val))); } // no error: return value @@ -8567,7 +8518,7 @@ class basic_json // type check: every element of the array must be an object if (JSON_HEDLEY_UNLIKELY(!val.is_object())) { - JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(val))); } // collect mandatory members @@ -8645,7 +8596,7 @@ class basic_json // throw an exception if test fails if (JSON_HEDLEY_UNLIKELY(!success)) { - JSON_THROW(other_error::create(501, val.diagnostics() + "unsuccessful: " + val.dump())); + JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump(), diagnostics_t(val))); } break; @@ -8655,7 +8606,7 @@ class basic_json { // op must be "add", "remove", "replace", "move", "copy", or // "test" - JSON_THROW(parse_error::create(105, 0, diagnostics() + "operation value '" + op + "' is invalid")); + JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid", diagnostics_t(val))); } } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 59a829b5fb..bd79bd6fd3 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -73,6 +73,175 @@ SOFTWARE. #include // runtime_error #include // to_string +// #include + + +#include +#include +// #include + + +#include // array +#include // size_t +#include // uint8_t +#include // string + +namespace nlohmann +{ +namespace detail +{ +/////////////////////////// +// JSON type enumeration // +/////////////////////////// + +/*! +@brief the JSON type enumeration + +This enumeration collects the different JSON types. It is internally used to +distinguish the stored values, and the functions @ref basic_json::is_null(), +@ref basic_json::is_object(), @ref basic_json::is_array(), +@ref basic_json::is_string(), @ref basic_json::is_boolean(), +@ref basic_json::is_number() (with @ref basic_json::is_number_integer(), +@ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()), +@ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and +@ref basic_json::is_structured() rely on it. + +@note There are three enumeration entries (number_integer, number_unsigned, and +number_float), because the library distinguishes these three types for numbers: +@ref basic_json::number_unsigned_t is used for unsigned integers, +@ref basic_json::number_integer_t is used for signed integers, and +@ref basic_json::number_float_t is used for floating-point numbers or to +approximate integers which do not fit in the limits of their respective type. + +@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON +value with the default value for a given type + +@since version 1.0.0 +*/ +enum class value_t : std::uint8_t +{ + null, ///< null value + object, ///< object (unordered set of name/value pairs) + array, ///< array (ordered collection of values) + string, ///< string value + boolean, ///< boolean value + number_integer, ///< number value (signed integer) + number_unsigned, ///< number value (unsigned integer) + number_float, ///< number value (floating-point) + binary, ///< binary array (ordered collection of bytes) + discarded ///< discarded by the parser callback function +}; + +/*! +@brief comparison operator for JSON types + +Returns an ordering that is similar to Python: +- order: null < boolean < number < object < array < string < binary +- furthermore, each type is not smaller than itself +- discarded values are not comparable +- binary is represented as a b"" string in python and directly comparable to a + string; however, making a binary array directly comparable with a string would + be surprising behavior in a JSON file. + +@since version 1.0.0 +*/ +inline bool operator<(const value_t lhs, const value_t rhs) noexcept +{ + static constexpr std::array order = {{ + 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */, + 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */, + 6 /* binary */ + } + }; + + const auto l_index = static_cast(lhs); + const auto r_index = static_cast(rhs); + return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index]; +} +} // namespace detail +} // namespace nlohmann + + +namespace nlohmann +{ +namespace detail +{ + +template +class diagnostics_t +{ + public: + diagnostics_t() noexcept = default; + diagnostics_t(const BasicJsonType& j) noexcept + : m_j(&j) + {} + + std::string diagnostics() const + { +#if JSON_DIAGNOSTICS + if (m_j == nullptr) + { + return ""; + } + + std::vector tokens; + for (const auto* current = m_j; current->m_parent != nullptr; current = current->m_parent) + { + switch (current->m_parent->type()) + { + case value_t::array: + { + for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) + { + if (current->m_parent->m_value.array->operator[](i) == *current) + { + tokens.emplace_back(std::to_string(i)); + continue; + } + } + break; + } + + case value_t::object: + { + for (const auto& element : *current->m_parent->m_value.object) + { + if (element.second == *current) + { + tokens.emplace_back(element.first.c_str()); + continue; + } + } + break; + } + + default: + break; + } + } + + if (tokens.empty()) + { + return ""; + } + + return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, + [](const std::string & a, const std::string & b) + { + return a + "/" + b; + }) + ") "; +#else + return ""; +#endif + } + + private: + const BasicJsonType* m_j = static_cast(nullptr); +}; + +} // namespace detail +} // namespace nlohmann + // #include @@ -2500,18 +2669,20 @@ class parse_error : public exception @param[in] what_arg the explanatory string @return parse_error object */ - static parse_error create(int id_, const position_t& pos, const std::string& what_arg) + template + static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { std::string w = exception::name("parse_error", id_) + "parse error" + - position_string(pos) + ": " + what_arg; + position_string(pos) + ": " + diagnostics.diagnostics() + what_arg; return parse_error(id_, pos.chars_read_total, w.c_str()); } - static parse_error create(int id_, std::size_t byte_, const std::string& what_arg) + template + static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { std::string w = exception::name("parse_error", id_) + "parse error" + (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + - ": " + what_arg; + ": " + diagnostics.diagnostics() + what_arg; return parse_error(id_, byte_, w.c_str()); } @@ -2577,9 +2748,10 @@ caught.,invalid_iterator} class invalid_iterator : public exception { public: - static invalid_iterator create(int id_, const std::string& what_arg) + template + static invalid_iterator create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("invalid_iterator", id_) + what_arg; + std::string w = exception::name("invalid_iterator", id_) + diagnostics.diagnostics() + what_arg; return invalid_iterator(id_, w.c_str()); } @@ -2631,9 +2803,10 @@ caught.,type_error} class type_error : public exception { public: - static type_error create(int id_, const std::string& what_arg) + template + static type_error create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("type_error", id_) + what_arg; + std::string w = exception::name("type_error", id_) + diagnostics.diagnostics() + what_arg; return type_error(id_, w.c_str()); } @@ -2678,9 +2851,10 @@ caught.,out_of_range} class out_of_range : public exception { public: - static out_of_range create(int id_, const std::string& what_arg) + template + static out_of_range create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("out_of_range", id_) + what_arg; + std::string w = exception::name("out_of_range", id_) + diagnostics.diagnostics() + what_arg; return out_of_range(id_, w.c_str()); } @@ -2716,9 +2890,10 @@ caught.,other_error} class other_error : public exception { public: - static other_error create(int id_, const std::string& what_arg) + template + static other_error create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("other_error", id_) + what_arg; + std::string w = exception::name("other_error", id_) + diagnostics.diagnostics() + what_arg; return other_error(id_, w.c_str()); } @@ -2729,6 +2904,8 @@ class other_error : public exception } // namespace detail } // namespace nlohmann +// #include + // #include // #include @@ -3423,87 +3600,6 @@ struct is_constructible_tuple> : conjunction -#include // array -#include // size_t -#include // uint8_t -#include // string - -namespace nlohmann -{ -namespace detail -{ -/////////////////////////// -// JSON type enumeration // -/////////////////////////// - -/*! -@brief the JSON type enumeration - -This enumeration collects the different JSON types. It is internally used to -distinguish the stored values, and the functions @ref basic_json::is_null(), -@ref basic_json::is_object(), @ref basic_json::is_array(), -@ref basic_json::is_string(), @ref basic_json::is_boolean(), -@ref basic_json::is_number() (with @ref basic_json::is_number_integer(), -@ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()), -@ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and -@ref basic_json::is_structured() rely on it. - -@note There are three enumeration entries (number_integer, number_unsigned, and -number_float), because the library distinguishes these three types for numbers: -@ref basic_json::number_unsigned_t is used for unsigned integers, -@ref basic_json::number_integer_t is used for signed integers, and -@ref basic_json::number_float_t is used for floating-point numbers or to -approximate integers which do not fit in the limits of their respective type. - -@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON -value with the default value for a given type - -@since version 1.0.0 -*/ -enum class value_t : std::uint8_t -{ - null, ///< null value - object, ///< object (unordered set of name/value pairs) - array, ///< array (ordered collection of values) - string, ///< string value - boolean, ///< boolean value - number_integer, ///< number value (signed integer) - number_unsigned, ///< number value (unsigned integer) - number_float, ///< number value (floating-point) - binary, ///< binary array (ordered collection of bytes) - discarded ///< discarded by the parser callback function -}; - -/*! -@brief comparison operator for JSON types - -Returns an ordering that is similar to Python: -- order: null < boolean < number < object < array < string < binary -- furthermore, each type is not smaller than itself -- discarded values are not comparable -- binary is represented as a b"" string in python and directly comparable to a - string; however, making a binary array directly comparable with a string would - be surprising behavior in a JSON file. - -@since version 1.0.0 -*/ -inline bool operator<(const value_t lhs, const value_t rhs) noexcept -{ - static constexpr std::array order = {{ - 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */, - 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */, - 6 /* binary */ - } - }; - - const auto l_index = static_cast(lhs); - const auto r_index = static_cast(rhs); - return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index]; -} -} // namespace detail -} // namespace nlohmann - - namespace nlohmann { namespace detail @@ -3513,7 +3609,7 @@ void from_json(const BasicJsonType& j, typename std::nullptr_t& n) { if (JSON_HEDLEY_UNLIKELY(!j.is_null())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be null, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } n = nullptr; } @@ -3544,7 +3640,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } } @@ -3553,7 +3649,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { if (JSON_HEDLEY_UNLIKELY(!j.is_boolean())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be boolean, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } b = *j.template get_ptr(); } @@ -3563,7 +3659,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } s = *j.template get_ptr(); } @@ -3579,7 +3675,7 @@ void from_json(const BasicJsonType& j, ConstructibleStringType& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } s = *j.template get_ptr(); @@ -3619,7 +3715,7 @@ void from_json(const BasicJsonType& j, std::forward_list& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } l.clear(); std::transform(j.rbegin(), j.rend(), @@ -3636,7 +3732,7 @@ void from_json(const BasicJsonType& j, std::valarray& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } l.resize(j.size()); std::transform(j.begin(), j.end(), std::begin(l), @@ -3727,8 +3823,7 @@ void()) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + - std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } from_json_array_impl(j, arr, priority_tag<3> {}); @@ -3739,7 +3834,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin) { if (JSON_HEDLEY_UNLIKELY(!j.is_binary())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be binary, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } bin = *j.template get_ptr(); @@ -3751,7 +3846,7 @@ void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) { if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } ConstructibleObjectType ret; @@ -3805,7 +3900,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } } @@ -3834,14 +3929,14 @@ void from_json(const BasicJsonType& j, std::map& { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(p.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t(j))); } m.emplace(p.at(0).template get(), p.at(1).template get()); } @@ -3854,14 +3949,14 @@ void from_json(const BasicJsonType& j, std::unordered_map(j))); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(p.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t(j))); } m.emplace(p.at(0).template get(), p.at(1).template get()); } @@ -4666,6 +4761,8 @@ class byte_container_with_subtype : public BinaryType // #include +// #include + // #include @@ -4805,6 +4902,8 @@ std::size_t hash(const BasicJsonType& j) #include // make_pair, move #include // vector +// #include + // #include // #include @@ -5295,6 +5394,8 @@ class span_input_adapter #include // move #include // vector +// #include + // #include // #include @@ -5446,6 +5547,7 @@ class json_sax_dom_parser using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; using binary_t = typename BasicJsonType::binary_t; + using diagnostics_t = detail::diagnostics_t; /*! @param[in, out] r reference to a JSON value that is manipulated while @@ -5511,8 +5613,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + - "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -5537,8 +5638,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + - "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -5628,6 +5728,7 @@ class json_sax_dom_callback_parser using binary_t = typename BasicJsonType::binary_t; using parser_callback_t = typename BasicJsonType::parser_callback_t; using parse_event_t = typename BasicJsonType::parse_event_t; + using diagnostics_t = detail::diagnostics_t; json_sax_dom_callback_parser(BasicJsonType& r, const parser_callback_t cb, @@ -5698,7 +5799,7 @@ class json_sax_dom_callback_parser // check object limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -5761,7 +5862,7 @@ class json_sax_dom_callback_parser // check array limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -7827,6 +7928,7 @@ class binary_reader using json_sax_t = SAX; using char_type = typename InputAdapterType::char_type; using char_int_type = typename std::char_traits::int_type; + using diagnostics_t = detail::diagnostics_t; public: /*! @@ -7900,7 +8002,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current != std::char_traits::eof())) { return sax->parse_error(chars_read, get_token_string(), - parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"))); + parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"), diagnostics_t())); } } @@ -7976,7 +8078,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 1)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"), diagnostics_t())); } return get_string(input_format_t::bson, len - static_cast(1), result) && get() != std::char_traits::eof(); @@ -7997,7 +8099,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 0)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"), diagnostics_t())); } // All BSON binary values have a subtype @@ -8079,7 +8181,7 @@ class binary_reader { std::array cr{{}}; (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type)); - return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()))); + return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), diagnostics_t())); } } } @@ -8479,7 +8581,7 @@ class binary_reader case cbor_tag_handler_t::error: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } case cbor_tag_handler_t::ignore: @@ -8594,7 +8696,7 @@ class binary_reader default: // anything else (0xFF is handled inside the other types) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } } } @@ -8689,7 +8791,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"), diagnostics_t())); } } } @@ -8788,7 +8890,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"), diagnostics_t())); } } } @@ -9255,7 +9357,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } } } @@ -9337,7 +9439,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"), diagnostics_t())); } } } @@ -9587,7 +9689,7 @@ class binary_reader default: auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), diagnostics_t())); } } @@ -9657,7 +9759,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), diagnostics_t())); } } } @@ -9695,7 +9797,7 @@ class binary_reader return false; } auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"), diagnostics_t())); } return get_ubjson_size_value(result.first); @@ -9785,7 +9887,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current > 127)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"), diagnostics_t())); } string_t s(1, static_cast(current)); return sax->string(s); @@ -9806,7 +9908,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } } } @@ -9984,7 +10086,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input)) { - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"))); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), diagnostics_t())); } switch (result_number) @@ -9996,7 +10098,7 @@ class binary_reader case token_type::value_float: return sax->number_float(number_lexer.get_number_float(), std::move(number_string)); default: - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"))); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), diagnostics_t())); } } @@ -10152,7 +10254,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current == std::char_traits::eof())) { return sax->parse_error(chars_read, "", - parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context))); + parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), diagnostics_t())); } return true; } @@ -10239,6 +10341,8 @@ class binary_reader // #include +// #include + // #include // #include @@ -10294,6 +10398,7 @@ class parser using string_t = typename BasicJsonType::string_t; using lexer_t = lexer; using token_type = typename lexer_t::token_type; + using diagnostics_t = detail::diagnostics_t; public: /// a parser reading from an input adapter @@ -10333,7 +10438,7 @@ class parser sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), diagnostics_t())); } // in case of an error, return discarded value @@ -10362,7 +10467,7 @@ class parser sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), diagnostics_t())); } // in case of an error, return discarded value @@ -10399,7 +10504,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), diagnostics_t())); } return result; @@ -10446,7 +10551,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); + exception_message(token_type::value_string, "object key"), diagnostics_t())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) { @@ -10459,7 +10564,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); + exception_message(token_type::name_separator, "object separator"), diagnostics_t())); } // remember we are now inside an object @@ -10502,7 +10607,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'")); + out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'", diagnostics_t())); } if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string()))) @@ -10573,7 +10678,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::uninitialized, "value"))); + exception_message(token_type::uninitialized, "value"), diagnostics_t())); } default: // the last token was unexpected @@ -10581,7 +10686,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::literal_or_value, "value"))); + exception_message(token_type::literal_or_value, "value"), diagnostics_t())); } } } @@ -10628,7 +10733,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_array, "array"))); + exception_message(token_type::end_array, "array"), diagnostics_t())); } else // object { @@ -10641,7 +10746,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); + exception_message(token_type::value_string, "object key"), diagnostics_t())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) @@ -10655,7 +10760,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); + exception_message(token_type::name_separator, "object separator"), diagnostics_t())); } // parse values @@ -10684,7 +10789,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_object, "object"))); + exception_message(token_type::end_object, "object"), diagnostics_t())); } } } @@ -10895,6 +11000,8 @@ template struct internal_iterator #include // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next #include // conditional, is_const, remove_const +// #include + // #include // #include @@ -10950,6 +11057,7 @@ class iter_impl // make sure BasicJsonType is basic_json or const basic_json static_assert(is_basic_json::type>::value, "iter_impl only accepts (const) basic_json"); + using diagnostics_t = detail::diagnostics_t; public: @@ -11156,7 +11264,7 @@ class iter_impl } case value_t::null: - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); default: { @@ -11165,7 +11273,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); } } } @@ -11199,7 +11307,7 @@ class iter_impl return m_object; } - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); } } } @@ -11300,7 +11408,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, m_object->diagnostics() + "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", diagnostics_t(*m_object))); } JSON_ASSERT(m_object != nullptr); @@ -11337,7 +11445,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, m_object->diagnostics() + "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", diagnostics_t(*m_object))); } JSON_ASSERT(m_object != nullptr); @@ -11345,7 +11453,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(213, m_object->diagnostics() + "cannot compare order of object iterators")); + JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", diagnostics_t(*m_object))); case value_t::array: return (m_it.array_iterator < other.m_it.array_iterator); @@ -11393,7 +11501,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, m_object->diagnostics() + "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", diagnostics_t(*m_object))); case value_t::array: { @@ -11464,7 +11572,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, m_object->diagnostics() + "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", diagnostics_t(*m_object))); case value_t::array: return m_it.array_iterator - other.m_it.array_iterator; @@ -11485,13 +11593,13 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(208, m_object->diagnostics() + "cannot use operator[] for object iterators")); + JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", diagnostics_t(*m_object))); case value_t::array: return *std::next(m_it.array_iterator, n); case value_t::null: - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); default: { @@ -11500,7 +11608,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); } } } @@ -11518,7 +11626,7 @@ class iter_impl return m_it.object_iterator->first; } - JSON_THROW(invalid_iterator::create(207, m_object->diagnostics() + "cannot use key() for non-object iterators")); + JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", diagnostics_t(*m_object))); } /*! @@ -11675,6 +11783,8 @@ class json_reverse_iterator : public std::reverse_iterator #include // move #include // vector +// #include + // #include // #include @@ -11691,6 +11801,8 @@ class json_pointer NLOHMANN_BASIC_JSON_TPL_DECLARATION friend class basic_json; + using diagnostics_t = detail::diagnostics_t; + public: /*! @brief create JSON pointer @@ -11917,7 +12029,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); } reference_tokens.pop_back(); @@ -11941,7 +12053,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); } return reference_tokens.back(); @@ -12007,15 +12119,13 @@ class json_pointer // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) { - JSON_THROW(detail::parse_error::create(106, 0, - "array index '" + s + - "' must not begin with '0'")); + JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", diagnostics_t())); } // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9'))) { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number")); + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", diagnostics_t())); } std::size_t processed_chars = 0; @@ -12026,20 +12136,20 @@ class json_pointer } JSON_CATCH(std::out_of_range&) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", diagnostics_t())); } // check if the string was completely read if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size())) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", diagnostics_t())); } // only triggered on special platforms (like 32bit), see also // https://github.com/nlohmann/json/pull/2203 if (res >= static_cast((std::numeric_limits::max)())) { - JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type")); // LCOV_EXCL_LINE + JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", diagnostics_t())); // LCOV_EXCL_LINE } return static_cast(res); @@ -12050,7 +12160,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); } json_pointer result = *this; @@ -12113,7 +12223,7 @@ class json_pointer single value; that is, with an empty list of reference tokens. */ default: - JSON_THROW(detail::type_error::create(313, "invalid value to unflatten")); + JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", diagnostics_t(j))); } } @@ -12185,7 +12295,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -12218,7 +12328,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + ") is out of range", diagnostics_t(*ptr))); } // note: at performs range check @@ -12227,7 +12337,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -12265,9 +12375,7 @@ class json_pointer if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) { // "-" cannot be used for const access - JSON_THROW(detail::out_of_range::create(402, - "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", diagnostics_t(*ptr))); } // use unchecked array access @@ -12276,7 +12384,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -12309,7 +12417,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + ") is out of range", diagnostics_t(*ptr))); } // note: at performs range check @@ -12318,7 +12426,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -12422,9 +12530,7 @@ class json_pointer // check if nonempty reference string begins with slash if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) { - JSON_THROW(detail::parse_error::create(107, 1, - "JSON pointer must be empty or begin with '/' - was: '" + - reference_string + "'")); + JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", diagnostics_t())); } // extract the reference tokens: @@ -12459,7 +12565,7 @@ class json_pointer (reference_token[pos + 1] != '0' && reference_token[pos + 1] != '1'))) { - JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'")); + JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", diagnostics_t())); } } @@ -12586,7 +12692,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!value.is_object())) { - JSON_THROW(detail::type_error::create(314, "only objects can be unflattened")); + JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", diagnostics_t(value))); } BasicJsonType result; @@ -12596,7 +12702,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive())) { - JSON_THROW(detail::type_error::create(315, "values in object must be primitive")); + JSON_THROW(detail::type_error::create(315, "values in object must be primitive", diagnostics_t(element))); } // assign value to reference pointed to by JSON pointer; Note that if @@ -12736,6 +12842,8 @@ class json_ref #include // string #include // isnan, isinf +// #include + // #include // #include @@ -12912,7 +13020,7 @@ class binary_writer default: { - JSON_THROW(type_error::create(317, j.diagnostics() + "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), detail::diagnostics_t(j)));; } } } @@ -13761,8 +13869,7 @@ class binary_writer const auto it = name.find(static_cast(0)); if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) { - JSON_THROW(out_of_range::create(409, j.diagnostics() + - "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")")); + JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", detail::diagnostics_t(j))); } return /*id*/ 1ul + name.size() + /*zero-terminator*/1u; @@ -13886,7 +13993,7 @@ class binary_writer } else { - JSON_THROW(out_of_range::create(407, j.diagnostics() + "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64")); + JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", detail::diagnostics_t(j))); } } @@ -15574,6 +15681,8 @@ char* to_chars(char* first, const char* last, FloatType value) } // namespace detail } // namespace nlohmann +// #include + // #include // #include @@ -16066,7 +16175,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn)); + JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, detail::diagnostics_t())); } case error_handler_t::ignore: @@ -16160,7 +16269,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn)); + JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, detail::diagnostics_t())); } case error_handler_t::ignore: @@ -16825,10 +16934,15 @@ class basic_json friend class ::nlohmann::detail::json_sax_dom_parser; template friend class ::nlohmann::detail::json_sax_dom_callback_parser; + template + friend class ::nlohmann::detail::diagnostics_t; /// workaround type for MSVC using basic_json_t = NLOHMANN_BASIC_JSON_TPL; + /// shortcut + using diagnostics_t = ::nlohmann::detail::diagnostics_t; + JSON_PRIVATE_UNLESS_TESTED: // convenience aliases for types residing in namespace detail; using lexer = ::nlohmann::detail::lexer_base; @@ -17696,7 +17810,7 @@ class basic_json object = nullptr; // silence warning, see #821 if (JSON_HEDLEY_UNLIKELY(t == value_t::null)) { - JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1")); // LCOV_EXCL_LINE + JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1", diagnostics_t())); // LCOV_EXCL_LINE } break; } @@ -18265,7 +18379,7 @@ class basic_json // if object is wanted but impossible, throw an exception if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object)) { - JSON_THROW(type_error::create(301, "cannot create object from initializer list")); + JSON_THROW(type_error::create(301, "cannot create object from initializer list", diagnostics_t())); } } @@ -18586,7 +18700,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(201, diagnostics() + "iterators are not compatible")); + JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", diagnostics_t())); } // copy type from first iterator @@ -18604,7 +18718,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, "iterators out of range")); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t())); } break; } @@ -18678,8 +18792,7 @@ class basic_json } default: - JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + - std::string(first.m_object->type_name()))); + JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), diagnostics_t())); } assert_invariant(); @@ -19399,60 +19512,6 @@ class basic_json /// @} - std::string diagnostics() const - { -#if JSON_DIAGNOSTICS - std::vector tokens; - for (const basic_json* current = this; current->m_parent != nullptr; current = current->m_parent) - { - switch (current->m_parent->type()) - { - case value_t::array: - { - for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) - { - if (current->m_parent->m_value.array->operator[](i) == *current) - { - tokens.emplace_back(std::to_string(i)); - continue; - } - } - break; - } - - case value_t::object: - { - for (const auto& element : *current->m_parent->m_value.object) - { - if (element.second == *current) - { - tokens.emplace_back(element.first.c_str()); - continue; - } - } - break; - } - - default: - break; - } - } - - if (tokens.empty()) - { - return ""; - } - - return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, - [](const std::string & a, const std::string & b) - { - return a + "/" + b; - }) + ") "; -#else - return ""; -#endif - } - private: ////////////////// // value access // @@ -19466,7 +19525,7 @@ class basic_json return m_value.boolean; } - JSON_THROW(type_error::create(302, diagnostics() + "type must be boolean, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()), diagnostics_t(*this))); } /// get a pointer to the value (object) @@ -19587,7 +19646,7 @@ class basic_json return *ptr; } - JSON_THROW(type_error::create(303, obj.diagnostics() + "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()))); + JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()), diagnostics_t(obj))); } public: @@ -20015,7 +20074,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this))); } return *get_ptr(); @@ -20026,7 +20085,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this))); } return *get_ptr(); @@ -20087,12 +20146,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -20134,12 +20193,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -20191,12 +20250,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -20242,12 +20301,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -20311,7 +20370,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20341,7 +20400,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20393,7 +20452,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20435,7 +20494,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20489,7 +20548,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20533,7 +20592,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20605,7 +20664,7 @@ class basic_json return default_value; } - JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20678,7 +20737,7 @@ class basic_json } } - JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20832,7 +20891,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != pos.m_object)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } IteratorType result = end(); @@ -20848,7 +20907,7 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin())) { - JSON_THROW(invalid_iterator::create(205, "iterator out of range")); + JSON_THROW(invalid_iterator::create(205, "iterator out of range", diagnostics_t(*this))); } if (is_string()) @@ -20884,7 +20943,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } return result; @@ -20945,7 +21004,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object)) { - JSON_THROW(invalid_iterator::create(203, diagnostics() + "iterators do not fit current value")); + JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", diagnostics_t(*this))); } IteratorType result = end(); @@ -20962,7 +21021,7 @@ class basic_json if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, diagnostics() + "iterators out of range")); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t(*this))); } if (is_string()) @@ -21000,7 +21059,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } return result; @@ -21043,7 +21102,7 @@ class basic_json return m_value.object->erase(key); } - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -21077,14 +21136,14 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(idx >= size())) { - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); } m_value.array->erase(m_value.array->begin() + static_cast(idx)); } else { - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -22029,7 +22088,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an array @@ -22067,7 +22126,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an array @@ -22120,7 +22179,7 @@ class basic_json // push_back only works for null objects or objects if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an object @@ -22228,7 +22287,7 @@ class basic_json // emplace_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an array @@ -22290,7 +22349,7 @@ class basic_json // emplace only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an object @@ -22366,7 +22425,7 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // insert to array and return iterator @@ -22379,7 +22438,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -22423,7 +22482,7 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // insert to array and return iterator @@ -22439,7 +22498,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -22477,24 +22536,24 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); } if (JSON_HEDLEY_UNLIKELY(first.m_object == this)) { - JSON_THROW(invalid_iterator::create(211, diagnostics() + "passed iterators may not belong to container")); + JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", diagnostics_t(*this))); } // insert to array and return iterator @@ -22539,13 +22598,13 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // insert to array and return iterator @@ -22590,19 +22649,19 @@ class basic_json // insert only works for objects if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this))); } m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator); @@ -22639,11 +22698,11 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this))); } if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(j.type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()), diagnostics_t(*this))); } for (auto it = j.cbegin(); it != j.cend(); ++it) @@ -22690,20 +22749,20 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object() || !last.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this))); } for (auto it = first; it != last; ++it) @@ -22798,7 +22857,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -22831,7 +22890,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -22864,7 +22923,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -22897,7 +22956,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -22911,7 +22970,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -25120,7 +25179,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(idx > parent.size())) { // avoid undefined behavior - JSON_THROW(out_of_range::create(401, parent.diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(parent))); } // default case: insert add offset @@ -25154,7 +25213,7 @@ class basic_json } else { - JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + last_path + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found", diagnostics_t(*this))); } } else if (parent.is_array()) @@ -25167,7 +25226,7 @@ class basic_json // type check: top level value must be an array if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array())) { - JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(json_patch))); } // iterate and apply the operations @@ -25187,13 +25246,13 @@ class basic_json // check if desired value is present if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end())) { - JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", diagnostics_t(val))); } // check if result is of type string if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string())) { - JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have string member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", diagnostics_t(val))); } // no error: return value @@ -25203,7 +25262,7 @@ class basic_json // type check: every element of the array must be an object if (JSON_HEDLEY_UNLIKELY(!val.is_object())) { - JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(val))); } // collect mandatory members @@ -25281,7 +25340,7 @@ class basic_json // throw an exception if test fails if (JSON_HEDLEY_UNLIKELY(!success)) { - JSON_THROW(other_error::create(501, val.diagnostics() + "unsuccessful: " + val.dump())); + JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump(), diagnostics_t(val))); } break; @@ -25291,7 +25350,7 @@ class basic_json { // op must be "add", "remove", "replace", "move", "copy", or // "test" - JSON_THROW(parse_error::create(105, 0, diagnostics() + "operation value '" + op + "' is invalid")); + JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid", diagnostics_t(val))); } } } diff --git a/test/src/unit-json_patch.cpp b/test/src/unit-json_patch.cpp index 53570113b7..9d1966f0ad 100644 --- a/test/src/unit-json_patch.cpp +++ b/test/src/unit-json_patch.cpp @@ -692,7 +692,11 @@ TEST_CASE("JSON patch") json patch = {"op", "add", "path", "", "value", 1}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.104] parse error: (/0) JSON patch must be an array of objects"); +#else "[json.exception.parse_error.104] parse error: JSON patch must be an array of objects"); +#endif } SECTION("missing 'op'") @@ -701,7 +705,11 @@ TEST_CASE("JSON patch") json patch = {{{"foo", "bar"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation must have member 'op'"); +#else "[json.exception.parse_error.105] parse error: operation must have member 'op'"); +#endif } SECTION("non-string 'op'") @@ -710,7 +718,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation must have string member 'op'"); +#else "[json.exception.parse_error.105] parse error: operation must have string member 'op'"); +#endif } SECTION("invalid operation") @@ -719,7 +731,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "foo"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation value 'foo' is invalid"); +#else "[json.exception.parse_error.105] parse error: operation value 'foo' is invalid"); +#endif } } @@ -731,7 +747,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "add"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'add' must have member 'path'"); +#endif } SECTION("non-string 'path'") @@ -740,7 +760,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "add"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have string member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'add' must have string member 'path'"); +#endif } SECTION("missing 'value'") @@ -749,7 +773,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "add"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'value'"); +#else "[json.exception.parse_error.105] parse error: operation 'add' must have member 'value'"); +#endif } SECTION("invalid array index") @@ -770,7 +798,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "remove"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'remove' must have member 'path'"); +#endif } SECTION("non-string 'path'") @@ -779,7 +811,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "remove"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have string member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'remove' must have string member 'path'"); +#endif } SECTION("nonexisting target location (array)") @@ -818,7 +854,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "replace"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'replace' must have member 'path'"); +#endif } SECTION("non-string 'path'") @@ -827,7 +867,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "replace"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have string member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'replace' must have string member 'path'"); +#endif } SECTION("missing 'value'") @@ -836,7 +880,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "replace"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'value'"); +#else "[json.exception.parse_error.105] parse error: operation 'replace' must have member 'value'"); +#endif } SECTION("nonexisting target location (array)") @@ -866,7 +914,12 @@ TEST_CASE("JSON patch") json patch = {{{"op", "move"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), - "[json.exception.parse_error.105] parse error: operation 'move' must have member 'path'"); +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'path'" +#else + "[json.exception.parse_error.105] parse error: operation 'move' must have member 'path'" +#endif + ); } SECTION("non-string 'path'") @@ -875,7 +928,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "move"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'move' must have string member 'path'"); +#endif } SECTION("missing 'from'") @@ -884,7 +941,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "move"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'from'"); +#else "[json.exception.parse_error.105] parse error: operation 'move' must have member 'from'"); +#endif } SECTION("non-string 'from'") @@ -893,7 +954,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "move"}, {"path", ""}, {"from", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'from'"); +#else "[json.exception.parse_error.105] parse error: operation 'move' must have string member 'from'"); +#endif } SECTION("nonexisting from location (array)") @@ -923,7 +988,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "copy"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'copy' must have member 'path'"); +#endif } SECTION("non-string 'path'") @@ -932,7 +1001,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "copy"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'copy' must have string member 'path'"); +#endif } SECTION("missing 'from'") @@ -941,7 +1014,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "copy"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'from'"); +#else "[json.exception.parse_error.105] parse error: operation 'copy' must have member 'from'"); +#endif } SECTION("non-string 'from'") @@ -950,7 +1027,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "copy"}, {"path", ""}, {"from", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'from'"); +#else "[json.exception.parse_error.105] parse error: operation 'copy' must have string member 'from'"); +#endif } SECTION("nonexisting from location (array)") @@ -980,7 +1061,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "test"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'test' must have member 'path'"); +#endif } SECTION("non-string 'path'") @@ -989,7 +1074,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "test"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have string member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'test' must have string member 'path'"); +#endif } SECTION("missing 'value'") @@ -998,7 +1087,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "test"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'value'"); +#else "[json.exception.parse_error.105] parse error: operation 'test' must have member 'value'"); +#endif } } } From a83404525e3b810c790bdaac97f0a8ca773097b7 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 10 Jan 2021 13:39:36 +0100 Subject: [PATCH 022/143] :rotating_light: fix warnings --- include/nlohmann/detail/value_t.hpp | 2 +- include/nlohmann/json.hpp | 14 ++-- single_include/nlohmann/json.hpp | 16 ++-- test/src/unit-json_patch.cpp | 116 +++++++++++----------------- 4 files changed, 62 insertions(+), 86 deletions(-) diff --git a/include/nlohmann/detail/value_t.hpp b/include/nlohmann/detail/value_t.hpp index 0383df06f8..a98c4355a0 100644 --- a/include/nlohmann/detail/value_t.hpp +++ b/include/nlohmann/detail/value_t.hpp @@ -32,7 +32,7 @@ number_float), because the library distinguishes these three types for numbers: @ref basic_json::number_float_t is used for floating-point numbers or to approximate integers which do not fit in the limits of their respective type. -@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON +@sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON value with the default value for a given type @since version 1.0.0 diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 086c0f5572..5a913c9643 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -5746,7 +5746,7 @@ class basic_json iterator result = insert_iterator(pos, cnt, val); for (size_type i = 0; i < cnt; ++i) { - (result + i)->m_parent = this; + (result + static_cast(i))->m_parent = this; } return result; #else @@ -5815,7 +5815,7 @@ class basic_json // insert to array and return iterator #if JSON_DIAGNOSTICS iterator result = insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); - for (std::size_t i = 0; i < std::distance(first, last); ++i) + for (typename iterator::difference_type i = 0; i < std::distance(first, last); ++i) { (result + i)->m_parent = this; } @@ -5869,7 +5869,7 @@ class basic_json iterator result = insert_iterator(pos, ilist.begin(), ilist.end()); for (std::size_t i = 0; i < size; ++i) { - (result + i)->m_parent = this; + (result + static_cast(i))->m_parent = this; } return result; #else @@ -8391,7 +8391,7 @@ class basic_json }; // wrapper for "add" operation; add value at ptr - const auto operation_add = [this, &result](json_pointer & ptr, basic_json val) + const auto operation_add = [&result](json_pointer & ptr, basic_json val) { // adding to the root of the target document means replacing it if (ptr.empty()) @@ -8489,9 +8489,9 @@ class basic_json for (const auto& val : json_patch) { // wrapper to get a value for an operation - const auto get_value = [this, &val](const std::string & op, - const std::string & member, - bool string_type) -> basic_json & + const auto get_value = [&val](const std::string & op, + const std::string & member, + bool string_type) -> basic_json & { // find value auto it = val.m_value.object->find(member); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index bd79bd6fd3..c8a4116f93 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -113,7 +113,7 @@ number_float), because the library distinguishes these three types for numbers: @ref basic_json::number_float_t is used for floating-point numbers or to approximate integers which do not fit in the limits of their respective type. -@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON +@sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON value with the default value for a given type @since version 1.0.0 @@ -22490,7 +22490,7 @@ class basic_json iterator result = insert_iterator(pos, cnt, val); for (size_type i = 0; i < cnt; ++i) { - (result + i)->m_parent = this; + (result + static_cast(i))->m_parent = this; } return result; #else @@ -22559,7 +22559,7 @@ class basic_json // insert to array and return iterator #if JSON_DIAGNOSTICS iterator result = insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); - for (std::size_t i = 0; i < std::distance(first, last); ++i) + for (typename iterator::difference_type i = 0; i < std::distance(first, last); ++i) { (result + i)->m_parent = this; } @@ -22613,7 +22613,7 @@ class basic_json iterator result = insert_iterator(pos, ilist.begin(), ilist.end()); for (std::size_t i = 0; i < size; ++i) { - (result + i)->m_parent = this; + (result + static_cast(i))->m_parent = this; } return result; #else @@ -25135,7 +25135,7 @@ class basic_json }; // wrapper for "add" operation; add value at ptr - const auto operation_add = [this, &result](json_pointer & ptr, basic_json val) + const auto operation_add = [&result](json_pointer & ptr, basic_json val) { // adding to the root of the target document means replacing it if (ptr.empty()) @@ -25233,9 +25233,9 @@ class basic_json for (const auto& val : json_patch) { // wrapper to get a value for an operation - const auto get_value = [this, &val](const std::string & op, - const std::string & member, - bool string_type) -> basic_json & + const auto get_value = [&val](const std::string & op, + const std::string & member, + bool string_type) -> basic_json & { // find value auto it = val.m_value.object->find(member); diff --git a/test/src/unit-json_patch.cpp b/test/src/unit-json_patch.cpp index 9d1966f0ad..af44e42326 100644 --- a/test/src/unit-json_patch.cpp +++ b/test/src/unit-json_patch.cpp @@ -691,11 +691,10 @@ TEST_CASE("JSON patch") json j; json patch = {"op", "add", "path", "", "value", 1}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.104] parse error: (/0) JSON patch must be an array of objects"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.104] parse error: (/0) JSON patch must be an array of objects"); #else - "[json.exception.parse_error.104] parse error: JSON patch must be an array of objects"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.104] parse error: JSON patch must be an array of objects"); #endif } @@ -704,11 +703,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"foo", "bar"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation must have member 'op'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation must have member 'op'"); #else - "[json.exception.parse_error.105] parse error: operation must have member 'op'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation must have member 'op'"); #endif } @@ -717,11 +715,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation must have string member 'op'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation must have string member 'op'"); #else - "[json.exception.parse_error.105] parse error: operation must have string member 'op'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation must have string member 'op'"); #endif } @@ -730,11 +727,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "foo"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation value 'foo' is invalid"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation value 'foo' is invalid"); #else - "[json.exception.parse_error.105] parse error: operation value 'foo' is invalid"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation value 'foo' is invalid"); #endif } } @@ -746,11 +742,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "add"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'path'"); #else - "[json.exception.parse_error.105] parse error: operation 'add' must have member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'add' must have member 'path'"); #endif } @@ -759,11 +754,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "add"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have string member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have string member 'path'"); #else - "[json.exception.parse_error.105] parse error: operation 'add' must have string member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'add' must have string member 'path'"); #endif } @@ -772,11 +766,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "add"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'value'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'value'"); #else - "[json.exception.parse_error.105] parse error: operation 'add' must have member 'value'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'add' must have member 'value'"); #endif } @@ -797,11 +790,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "remove"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have member 'path'"); #else - "[json.exception.parse_error.105] parse error: operation 'remove' must have member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'remove' must have member 'path'"); #endif } @@ -810,11 +802,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "remove"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have string member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have string member 'path'"); #else - "[json.exception.parse_error.105] parse error: operation 'remove' must have string member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'remove' must have string member 'path'"); #endif } @@ -853,11 +844,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "replace"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'path'"); #else - "[json.exception.parse_error.105] parse error: operation 'replace' must have member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'replace' must have member 'path'"); #endif } @@ -866,11 +856,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "replace"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have string member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have string member 'path'"); #else - "[json.exception.parse_error.105] parse error: operation 'replace' must have string member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'replace' must have string member 'path'"); #endif } @@ -879,11 +868,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "replace"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'value'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'value'"); #else - "[json.exception.parse_error.105] parse error: operation 'replace' must have member 'value'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'replace' must have member 'value'"); #endif } @@ -913,13 +901,11 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "move"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'path'" + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'path'"); #else - "[json.exception.parse_error.105] parse error: operation 'move' must have member 'path'" + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'move' must have member 'path'"); #endif - ); } SECTION("non-string 'path'") @@ -927,11 +913,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "move"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'path'"); #else - "[json.exception.parse_error.105] parse error: operation 'move' must have string member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'move' must have string member 'path'"); #endif } @@ -940,11 +925,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "move"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'from'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'from'"); #else - "[json.exception.parse_error.105] parse error: operation 'move' must have member 'from'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'move' must have member 'from'"); #endif } @@ -953,11 +937,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "move"}, {"path", ""}, {"from", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'from'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'from'"); #else - "[json.exception.parse_error.105] parse error: operation 'move' must have string member 'from'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'move' must have string member 'from'"); #endif } @@ -987,11 +970,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "copy"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'path'"); #else - "[json.exception.parse_error.105] parse error: operation 'copy' must have member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'copy' must have member 'path'"); #endif } @@ -1000,11 +982,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "copy"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'path'"); #else - "[json.exception.parse_error.105] parse error: operation 'copy' must have string member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'copy' must have string member 'path'"); #endif } @@ -1013,11 +994,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "copy"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'from'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'from'"); #else - "[json.exception.parse_error.105] parse error: operation 'copy' must have member 'from'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'copy' must have member 'from'"); #endif } @@ -1026,11 +1006,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "copy"}, {"path", ""}, {"from", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'from'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'from'"); #else - "[json.exception.parse_error.105] parse error: operation 'copy' must have string member 'from'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'copy' must have string member 'from'"); #endif } @@ -1060,11 +1039,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "test"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'path'"); #else - "[json.exception.parse_error.105] parse error: operation 'test' must have member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'test' must have member 'path'"); #endif } @@ -1073,11 +1051,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "test"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have string member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have string member 'path'"); #else - "[json.exception.parse_error.105] parse error: operation 'test' must have string member 'path'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'test' must have string member 'path'"); #endif } @@ -1086,11 +1063,10 @@ TEST_CASE("JSON patch") json j; json patch = {{{"op", "test"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); - CHECK_THROWS_WITH(j.patch(patch), #if JSON_DIAGNOSTICS - "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'value'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'value'"); #else - "[json.exception.parse_error.105] parse error: operation 'test' must have member 'value'"); + CHECK_THROWS_WITH(j.patch(patch), "[json.exception.parse_error.105] parse error: operation 'test' must have member 'value'"); #endif } } From 1d6ba22f158dae41726636d05cf001b08a371c59 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 10 Jan 2021 14:10:59 +0100 Subject: [PATCH 023/143] :recycle: simplify code --- include/nlohmann/detail/output/binary_writer.hpp | 7 ++++--- include/nlohmann/detail/output/serializer.hpp | 5 +++-- single_include/nlohmann/json.hpp | 12 +++++++----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 10bc34011b..b8314402cc 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -30,6 +30,7 @@ class binary_writer using string_t = typename BasicJsonType::string_t; using binary_t = typename BasicJsonType::binary_t; using number_float_t = typename BasicJsonType::number_float_t; + using diagnostics_t = detail::diagnostics_t; public: /*! @@ -58,7 +59,7 @@ class binary_writer default: { - JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), detail::diagnostics_t(j)));; + JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), diagnostics_t(j)));; } } } @@ -907,7 +908,7 @@ class binary_writer const auto it = name.find(static_cast(0)); if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) { - JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", detail::diagnostics_t(j))); + JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", diagnostics_t(j))); } return /*id*/ 1ul + name.size() + /*zero-terminator*/1u; @@ -1031,7 +1032,7 @@ class binary_writer } else { - JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", detail::diagnostics_t(j))); + JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", diagnostics_t(j))); } } diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 3384141805..7c570f8cb7 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -45,6 +45,7 @@ class serializer using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using binary_char_t = typename BasicJsonType::binary_t::value_type; + using diagnostics_t = detail::diagnostics_t; static constexpr std::uint8_t UTF8_ACCEPT = 0; static constexpr std::uint8_t UTF8_REJECT = 1; @@ -500,7 +501,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, detail::diagnostics_t())); + JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, diagnostics_t())); } case error_handler_t::ignore: @@ -594,7 +595,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, detail::diagnostics_t())); + JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, diagnostics_t())); } case error_handler_t::ignore: diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index c8a4116f93..0aab317974 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -12992,6 +12992,7 @@ class binary_writer using string_t = typename BasicJsonType::string_t; using binary_t = typename BasicJsonType::binary_t; using number_float_t = typename BasicJsonType::number_float_t; + using diagnostics_t = detail::diagnostics_t; public: /*! @@ -13020,7 +13021,7 @@ class binary_writer default: { - JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), detail::diagnostics_t(j)));; + JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), diagnostics_t(j)));; } } } @@ -13869,7 +13870,7 @@ class binary_writer const auto it = name.find(static_cast(0)); if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) { - JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", detail::diagnostics_t(j))); + JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", diagnostics_t(j))); } return /*id*/ 1ul + name.size() + /*zero-terminator*/1u; @@ -13993,7 +13994,7 @@ class binary_writer } else { - JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", detail::diagnostics_t(j))); + JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", diagnostics_t(j))); } } @@ -15720,6 +15721,7 @@ class serializer using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using binary_char_t = typename BasicJsonType::binary_t::value_type; + using diagnostics_t = detail::diagnostics_t; static constexpr std::uint8_t UTF8_ACCEPT = 0; static constexpr std::uint8_t UTF8_REJECT = 1; @@ -16175,7 +16177,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, detail::diagnostics_t())); + JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, diagnostics_t())); } case error_handler_t::ignore: @@ -16269,7 +16271,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, detail::diagnostics_t())); + JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, diagnostics_t())); } case error_handler_t::ignore: From 9d0150c234caa1e6b0ea24f4735153d5f43030e6 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 10 Jan 2021 15:04:14 +0100 Subject: [PATCH 024/143] :recycle: simplify code --- include/nlohmann/json.hpp | 181 ++++++++++--------------------- single_include/nlohmann/json.hpp | 181 ++++++++++--------------------- 2 files changed, 118 insertions(+), 244 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 5a913c9643..cbfde69280 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1242,6 +1242,47 @@ class basic_json JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr); } + reference set_parent(reference j, bool recursive) + { +#if JSON_DIAGNOSTICS + if (recursive) + { + switch (m_type) + { + case value_t::array: + { + for (auto& element : *m_value.array) + { + element.m_parent = this; + } + break; + } + + case value_t::object: + { + for (auto& element : *m_value.object) + { + element.second.m_parent = this; + } + break; + } + + default: + break; + } + } + else + { + j.m_parent = this; + } +#else + static_cast(j); + static_cast(recursive); +#endif + + return j; + } + public: ////////////////////////// // JSON parser callback // @@ -1651,12 +1692,7 @@ class basic_json auto res = m_value.object->emplace( std::move(*((*element.m_value.array)[0].m_value.string)), std::move((*element.m_value.array)[1])); - -#if JSON_DIAGNOSTICS - res.first->second.m_parent = this; -#else - static_cast(res); // unused variable - fix warning -#endif + set_parent(res.first->second, false); } } else @@ -1664,12 +1700,7 @@ class basic_json // the initializer list describes an array -> create array m_type = value_t::array; m_value.array = create(init.begin(), init.end()); -#if JSON_DIAGNOSTICS - for (auto& element : *m_value.array) - { - element.m_parent = this; - } -#endif + set_parent(*this, true); } assert_invariant(); @@ -1881,12 +1912,7 @@ class basic_json : m_type(value_t::array) { m_value.array = create(cnt, val); -#if JSON_DIAGNOSTICS - for (auto& element : *m_value.array) - { - element.m_parent = this; - } -#endif + set_parent(*this, true); assert_invariant(); } @@ -2019,12 +2045,7 @@ class basic_json { m_value.object = create(first.m_it.object_iterator, last.m_it.object_iterator); -#if JSON_DIAGNOSTICS - for (auto& element : *m_value.object) - { - element.second.m_parent = this; - } -#endif + set_parent(*this, true); break; } @@ -2032,12 +2053,7 @@ class basic_json { m_value.array = create(first.m_it.array_iterator, last.m_it.array_iterator); -#if JSON_DIAGNOSTICS - for (auto& element : *m_value.array) - { - element.m_parent = this; - } -#endif + set_parent(*this, true); break; } @@ -2100,24 +2116,14 @@ class basic_json case value_t::object: { m_value = *other.m_value.object; -#if JSON_DIAGNOSTICS - for (auto& element : *m_value.object) - { - element.second.m_parent = this; - } -#endif + set_parent(*this, true); break; } case value_t::array: { m_value = *other.m_value.array; -#if JSON_DIAGNOSTICS - for (auto& element : *m_value.array) - { - element.m_parent = this; - } -#endif + set_parent(*this, true); break; } @@ -2201,32 +2207,7 @@ class basic_json other.m_type = value_t::null; other.m_value = {}; -#if JSON_DIAGNOSTICS - switch (m_type) - { - case value_t::array: - { - for (auto& element : *m_value.array) - { - element.m_parent = this; - } - break; - } - - case value_t::object: - { - for (auto& element : *m_value.object) - { - element.second.m_parent = this; - } - break; - } - - default: - break; - } -#endif - + set_parent(*this, true); assert_invariant(); } @@ -3391,13 +3372,7 @@ class basic_json { JSON_TRY { -#if JSON_DIAGNOSTICS - reference result = m_value.array->at(idx); - result.m_parent = this; - return result; -#else - return m_value.array->at(idx); -#endif + return set_parent(m_value.array->at(idx), false); } JSON_CATCH (std::out_of_range&) { @@ -3495,13 +3470,7 @@ class basic_json { JSON_TRY { -#if JSON_DIAGNOSTICS - reference result = m_value.object->at(key); - result.m_parent = this; - return result; -#else - return m_value.object->at(key); -#endif + return set_parent(m_value.object->at(key), false); } JSON_CATCH (std::out_of_range&) { @@ -3699,13 +3668,7 @@ class basic_json // operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { -#if JSON_DIAGNOSTICS - reference result = m_value.object->operator[](key); - result.m_parent = this; - return result; -#else - return m_value.object->operator[](key); -#endif + return set_parent(m_value.object->operator[](key), false); } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); @@ -3795,13 +3758,7 @@ class basic_json // at only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { -#if JSON_DIAGNOSTICS - reference result = m_value.object->operator[](key); - result.m_parent = this; - return result; -#else - return m_value.object->operator[](key); -#endif + return set_parent(m_value.object->operator[](key), false); } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); @@ -5357,9 +5314,7 @@ class basic_json // add element to array (move semantics) m_value.array->push_back(std::move(val)); -#if JSON_DIAGNOSTICS - m_value.array->back().m_parent = this; -#endif + set_parent(m_value.array->back(), false); // if val is moved from, basic_json move constructor marks it null so we do not call the destructor } @@ -5395,9 +5350,7 @@ class basic_json // add element to array m_value.array->push_back(val); -#if JSON_DIAGNOSTICS - m_value.array->back().m_parent = this; -#endif + set_parent(m_value.array->back(), false); } /*! @@ -5447,12 +5400,8 @@ class basic_json } // add element to object -#if JSON_DIAGNOSTICS auto res = m_value.object->insert(val); - res.first->second.m_parent = this; -#else - m_value.object->insert(val); -#endif + set_parent(res.first->second, false); } /*! @@ -5556,19 +5505,10 @@ class basic_json // add element to array (perfect forwarding) #ifdef JSON_HAS_CPP_17 -#if JSON_DIAGNOSTICS - reference result = m_value.array->emplace_back(std::forward(args)...); - result.m_parent = this; - return result; -#else - return m_value.array->emplace_back(std::forward(args)...); -#endif + return set_parent(m_value.array->emplace_back(std::forward(args)...), false); #else m_value.array->emplace_back(std::forward(args)...); -#if JSON_DIAGNOSTICS - m_value.array->back().m_parent = this; -#endif - return m_value.array->back(); + return set_parent(m_value.array->back(), false); #endif } @@ -5618,10 +5558,7 @@ class basic_json // add element to array (perfect forwarding) auto res = m_value.object->emplace(std::forward(args)...); - -#if JSON_DIAGNOSTICS - res.first->second.m_parent = this; -#endif + set_parent(res.first->second, false); // create result iterator and set iterator to the result of emplace auto it = begin(); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 0aab317974..4d079db097 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -17988,6 +17988,47 @@ class basic_json JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr); } + reference set_parent(reference j, bool recursive) + { +#if JSON_DIAGNOSTICS + if (recursive) + { + switch (m_type) + { + case value_t::array: + { + for (auto& element : *m_value.array) + { + element.m_parent = this; + } + break; + } + + case value_t::object: + { + for (auto& element : *m_value.object) + { + element.second.m_parent = this; + } + break; + } + + default: + break; + } + } + else + { + j.m_parent = this; + } +#else + static_cast(j); + static_cast(recursive); +#endif + + return j; + } + public: ////////////////////////// // JSON parser callback // @@ -18397,12 +18438,7 @@ class basic_json auto res = m_value.object->emplace( std::move(*((*element.m_value.array)[0].m_value.string)), std::move((*element.m_value.array)[1])); - -#if JSON_DIAGNOSTICS - res.first->second.m_parent = this; -#else - static_cast(res); // unused variable - fix warning -#endif + set_parent(res.first->second, false); } } else @@ -18410,12 +18446,7 @@ class basic_json // the initializer list describes an array -> create array m_type = value_t::array; m_value.array = create(init.begin(), init.end()); -#if JSON_DIAGNOSTICS - for (auto& element : *m_value.array) - { - element.m_parent = this; - } -#endif + set_parent(*this, true); } assert_invariant(); @@ -18627,12 +18658,7 @@ class basic_json : m_type(value_t::array) { m_value.array = create(cnt, val); -#if JSON_DIAGNOSTICS - for (auto& element : *m_value.array) - { - element.m_parent = this; - } -#endif + set_parent(*this, true); assert_invariant(); } @@ -18765,12 +18791,7 @@ class basic_json { m_value.object = create(first.m_it.object_iterator, last.m_it.object_iterator); -#if JSON_DIAGNOSTICS - for (auto& element : *m_value.object) - { - element.second.m_parent = this; - } -#endif + set_parent(*this, true); break; } @@ -18778,12 +18799,7 @@ class basic_json { m_value.array = create(first.m_it.array_iterator, last.m_it.array_iterator); -#if JSON_DIAGNOSTICS - for (auto& element : *m_value.array) - { - element.m_parent = this; - } -#endif + set_parent(*this, true); break; } @@ -18846,24 +18862,14 @@ class basic_json case value_t::object: { m_value = *other.m_value.object; -#if JSON_DIAGNOSTICS - for (auto& element : *m_value.object) - { - element.second.m_parent = this; - } -#endif + set_parent(*this, true); break; } case value_t::array: { m_value = *other.m_value.array; -#if JSON_DIAGNOSTICS - for (auto& element : *m_value.array) - { - element.m_parent = this; - } -#endif + set_parent(*this, true); break; } @@ -18947,32 +18953,7 @@ class basic_json other.m_type = value_t::null; other.m_value = {}; -#if JSON_DIAGNOSTICS - switch (m_type) - { - case value_t::array: - { - for (auto& element : *m_value.array) - { - element.m_parent = this; - } - break; - } - - case value_t::object: - { - for (auto& element : *m_value.object) - { - element.second.m_parent = this; - } - break; - } - - default: - break; - } -#endif - + set_parent(*this, true); assert_invariant(); } @@ -20137,13 +20118,7 @@ class basic_json { JSON_TRY { -#if JSON_DIAGNOSTICS - reference result = m_value.array->at(idx); - result.m_parent = this; - return result; -#else - return m_value.array->at(idx); -#endif + return set_parent(m_value.array->at(idx), false); } JSON_CATCH (std::out_of_range&) { @@ -20241,13 +20216,7 @@ class basic_json { JSON_TRY { -#if JSON_DIAGNOSTICS - reference result = m_value.object->at(key); - result.m_parent = this; - return result; -#else - return m_value.object->at(key); -#endif + return set_parent(m_value.object->at(key), false); } JSON_CATCH (std::out_of_range&) { @@ -20445,13 +20414,7 @@ class basic_json // operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { -#if JSON_DIAGNOSTICS - reference result = m_value.object->operator[](key); - result.m_parent = this; - return result; -#else - return m_value.object->operator[](key); -#endif + return set_parent(m_value.object->operator[](key), false); } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); @@ -20541,13 +20504,7 @@ class basic_json // at only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { -#if JSON_DIAGNOSTICS - reference result = m_value.object->operator[](key); - result.m_parent = this; - return result; -#else - return m_value.object->operator[](key); -#endif + return set_parent(m_value.object->operator[](key), false); } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); @@ -22103,9 +22060,7 @@ class basic_json // add element to array (move semantics) m_value.array->push_back(std::move(val)); -#if JSON_DIAGNOSTICS - m_value.array->back().m_parent = this; -#endif + set_parent(m_value.array->back(), false); // if val is moved from, basic_json move constructor marks it null so we do not call the destructor } @@ -22141,9 +22096,7 @@ class basic_json // add element to array m_value.array->push_back(val); -#if JSON_DIAGNOSTICS - m_value.array->back().m_parent = this; -#endif + set_parent(m_value.array->back(), false); } /*! @@ -22193,12 +22146,8 @@ class basic_json } // add element to object -#if JSON_DIAGNOSTICS auto res = m_value.object->insert(val); - res.first->second.m_parent = this; -#else - m_value.object->insert(val); -#endif + set_parent(res.first->second, false); } /*! @@ -22302,19 +22251,10 @@ class basic_json // add element to array (perfect forwarding) #ifdef JSON_HAS_CPP_17 -#if JSON_DIAGNOSTICS - reference result = m_value.array->emplace_back(std::forward(args)...); - result.m_parent = this; - return result; -#else - return m_value.array->emplace_back(std::forward(args)...); -#endif + return set_parent(m_value.array->emplace_back(std::forward(args)...), false); #else m_value.array->emplace_back(std::forward(args)...); -#if JSON_DIAGNOSTICS - m_value.array->back().m_parent = this; -#endif - return m_value.array->back(); + return set_parent(m_value.array->back(), false); #endif } @@ -22364,10 +22304,7 @@ class basic_json // add element to array (perfect forwarding) auto res = m_value.object->emplace(std::forward(args)...); - -#if JSON_DIAGNOSTICS - res.first->second.m_parent = this; -#endif + set_parent(res.first->second, false); // create result iterator and set iterator to the result of emplace auto it = begin(); From 23f462b598b9da7b30e2924af39f807c45deaa63 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Sun, 10 Jan 2021 19:23:32 +0100 Subject: [PATCH 025/143] Reduced code duplication, renamed tag to identity_tag. --- include/nlohmann/adl_serializer.hpp | 7 +- .../nlohmann/detail/conversions/from_json.hpp | 97 +++++++--------- .../detail/meta/{tag.hpp => identity_tag.hpp} | 2 +- single_include/nlohmann/json.hpp | 107 ++++++++---------- 4 files changed, 99 insertions(+), 114 deletions(-) rename include/nlohmann/detail/meta/{tag.hpp => identity_tag.hpp} (75%) diff --git a/include/nlohmann/adl_serializer.hpp b/include/nlohmann/adl_serializer.hpp index 9eb751b705..1dee29eb94 100644 --- a/include/nlohmann/adl_serializer.hpp +++ b/include/nlohmann/adl_serializer.hpp @@ -5,6 +5,7 @@ #include #include +#include #include namespace nlohmann @@ -46,10 +47,10 @@ struct adl_serializer */ template static auto from_json(BasicJsonType && j) noexcept( - noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) - -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) + noexcept(::nlohmann::from_json(std::forward(j), detail::identity_tag {}))) + -> decltype(::nlohmann::from_json(std::forward(j), detail::identity_tag {})) { - return ::nlohmann::from_json(std::forward(j), detail::tag {}); + return ::nlohmann::from_json(std::forward(j), detail::identity_tag {}); } /*! diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index c9b88ce5b9..b29ac05c64 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include @@ -250,16 +250,16 @@ void()) } template < typename BasicJsonType, typename Array, std::size_t... Is > -Array from_json_array_impl(BasicJsonType&& j, tag /*unused*/, index_sequence /*unused*/) +Array from_json_array_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/) { return { std::forward(j).at(Is).template get()... }; } template < typename BasicJsonType, typename T, std::size_t N, enable_if_t < !std::is_default_constructible>::value, int > = 0 > -auto from_json(BasicJsonType && j, tag> t) +auto from_json(BasicJsonType && j, identity_tag> tag) -> decltype(j.template get(), -from_json_array_impl(std::forward(j), t, make_index_sequence {})) +from_json_array_impl(std::forward(j), tag, make_index_sequence {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -267,7 +267,7 @@ from_json_array_impl(std::forward(j), t, make_index_sequence { std::string(j.type_name()))); } - return from_json_array_impl(std::forward(j), t, make_index_sequence {}); + return from_json_array_impl(std::forward(j), tag, make_index_sequence {}); } template @@ -345,24 +345,24 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } } +template < typename BasicJsonType, class A1, class A2 > +std::pair from_json_pair_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) +{ + return {std::forward(j).at(0).template get(), + std::forward(j).at(1).template get()}; +} + + template>::value, int> = 0> -void from_json(BasicJsonType && j, std::pair& p) +void from_json_pair_impl(BasicJsonType && j, std::pair& p, priority_tag<1> /*unused*/) { - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); - } - - p = {std::forward(j).at(0).template get(), - std::forward(j).at(1).template get() - }; + p = from_json_pair_impl(std::forward(j), identity_tag> {}, priority_tag<0> {}); } -template < typename BasicJsonType, class A1, class A2, - enable_if_t < !std::is_default_constructible>::value, int > = 0 > -std::pair from_json(BasicJsonType && j, tag> /*unused*/) +template +auto from_json(BasicJsonType&& j, PairRelatedType&& p) +-> decltype(from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -370,19 +370,25 @@ std::pair from_json(BasicJsonType && j, tag> /*unused* std::string(j.type_name()))); } - return {std::forward(j).at(0).template get(), - std::forward(j).at(1).template get()}; + return from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {}); } template -void from_json_tuple_impl(BasicJsonType&& j, Tuple& t, index_sequence /*unused*/) +Tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/, priority_tag<0> /*unused*/) +{ + return std::make_tuple(std::forward(j).at(Idx).template get::type>()...); +} + +template::value, int> = 0> +void from_json_tuple_impl(BasicJsonType && j, Tuple& t, index_sequence /*unused*/, priority_tag<1> /*unused*/) { - t = std::make_tuple(std::forward(j).at(Idx).template get::type>()...); + t = from_json_tuple_impl(std::forward(j), identity_tag {}, priority_tag<0> {}); } -template>::value, int > = 0 > -void from_json(BasicJsonType && j, std::tuple& t) +template +auto from_json_tuple(BasicJsonType&& j, TupleRelated&& t, index_sequence idx) +-> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -390,26 +396,21 @@ void from_json(BasicJsonType && j, std::tuple& t) std::string(j.type_name()))); } - from_json_tuple_impl(std::forward(j), t, index_sequence_for {}); + return from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {}); } -template -Tuple from_json_tuple_impl(BasicJsonType&& j, tag /*unused*/, index_sequence /*unused*/) +template +auto from_json(BasicJsonType&& j, std::tuple& t) +-> decltype(from_json_tuple(std::forward(j), t, index_sequence_for {})) { - return std::make_tuple(std::forward(j).at(Idx).template get::type>()...); + from_json_tuple(std::forward(j), t, index_sequence_for {}); } -template < typename BasicJsonType, typename... Args, - enable_if_t < !std::is_default_constructible>::value, int > = 0 > -std::tuple from_json(BasicJsonType && j, tag> t) +template +auto from_json(BasicJsonType&& j, identity_tag> tag) +-> decltype(from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {})) { - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); - } - - return from_json_tuple_impl(std::forward(j), t, index_sequence_for {}); + return from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {}); } template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, @@ -455,21 +456,11 @@ void from_json(const BasicJsonType& j, std::unordered_map - auto operator()(const BasicJsonType& j, T& val) const - noexcept(noexcept(from_json(j, val))) - -> decltype(from_json(j, val), void()) - { - return from_json(j, val); - } - - // overload to pass calls to built-in from_json functions for non-default constructible STL - // types (e.g. std::array, where X is not default constructible). - template - auto operator()(const BasicJsonType& j, detail::tag t) const - noexcept(noexcept(from_json(j, t))) - -> decltype(from_json(j, t)) + auto operator()(const BasicJsonType& j, T&& val) const + noexcept(noexcept(from_json(j, std::forward(val)))) + -> decltype(from_json(j, std::forward(val))) { - return from_json(j, t); + return from_json(j, std::forward(val)); } }; } // namespace detail diff --git a/include/nlohmann/detail/meta/tag.hpp b/include/nlohmann/detail/meta/identity_tag.hpp similarity index 75% rename from include/nlohmann/detail/meta/tag.hpp rename to include/nlohmann/detail/meta/identity_tag.hpp index 631887d1d2..73a3e91700 100644 --- a/include/nlohmann/detail/meta/tag.hpp +++ b/include/nlohmann/detail/meta/identity_tag.hpp @@ -5,6 +5,6 @@ namespace nlohmann namespace detail { // dispatching helper struct -template struct tag {}; +template struct identity_tag {}; } // namespace detail } // namespace nlohmann diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 06f7caa378..f3c1b987c1 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2813,7 +2813,7 @@ constexpr T static_const::value; } // namespace detail } // namespace nlohmann -// #include +// #include namespace nlohmann @@ -2821,7 +2821,7 @@ namespace nlohmann namespace detail { // dispatching helper struct -template struct tag {}; +template struct identity_tag {}; } // namespace detail } // namespace nlohmann @@ -3747,16 +3747,16 @@ void()) } template < typename BasicJsonType, typename Array, std::size_t... Is > -Array from_json_array_impl(BasicJsonType&& j, tag /*unused*/, index_sequence /*unused*/) +Array from_json_array_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/) { return { std::forward(j).at(Is).template get()... }; } template < typename BasicJsonType, typename T, std::size_t N, enable_if_t < !std::is_default_constructible>::value, int > = 0 > -auto from_json(BasicJsonType && j, tag> t) +auto from_json(BasicJsonType && j, identity_tag> tag) -> decltype(j.template get(), -from_json_array_impl(std::forward(j), t, make_index_sequence {})) +from_json_array_impl(std::forward(j), tag, make_index_sequence {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -3764,7 +3764,7 @@ from_json_array_impl(std::forward(j), t, make_index_sequence { std::string(j.type_name()))); } - return from_json_array_impl(std::forward(j), t, make_index_sequence {}); + return from_json_array_impl(std::forward(j), tag, make_index_sequence {}); } template @@ -3842,24 +3842,24 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } } +template < typename BasicJsonType, class A1, class A2 > +std::pair from_json_pair_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) +{ + return {std::forward(j).at(0).template get(), + std::forward(j).at(1).template get()}; +} + + template>::value, int> = 0> -void from_json(BasicJsonType && j, std::pair& p) +void from_json_pair_impl(BasicJsonType && j, std::pair& p, priority_tag<1> /*unused*/) { - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); - } - - p = {std::forward(j).at(0).template get(), - std::forward(j).at(1).template get() - }; + p = from_json_pair_impl(std::forward(j), identity_tag> {}, priority_tag<0> {}); } -template < typename BasicJsonType, class A1, class A2, - enable_if_t < !std::is_default_constructible>::value, int > = 0 > -std::pair from_json(BasicJsonType && j, tag> /*unused*/) +template +auto from_json(BasicJsonType&& j, PairRelatedType&& p) +-> decltype(from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -3867,19 +3867,25 @@ std::pair from_json(BasicJsonType && j, tag> /*unused* std::string(j.type_name()))); } - return {std::forward(j).at(0).template get(), - std::forward(j).at(1).template get()}; + return from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {}); } template -void from_json_tuple_impl(BasicJsonType&& j, Tuple& t, index_sequence /*unused*/) +Tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/, priority_tag<0> /*unused*/) +{ + return std::make_tuple(std::forward(j).at(Idx).template get::type>()...); +} + +template::value, int> = 0> +void from_json_tuple_impl(BasicJsonType && j, Tuple& t, index_sequence /*unused*/, priority_tag<1> /*unused*/) { - t = std::make_tuple(std::forward(j).at(Idx).template get::type>()...); + t = from_json_tuple_impl(std::forward(j), identity_tag {}, priority_tag<0> {}); } -template>::value, int > = 0 > -void from_json(BasicJsonType && j, std::tuple& t) +template +auto from_json_tuple(BasicJsonType&& j, TupleRelated&& t, index_sequence idx) +-> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -3887,26 +3893,21 @@ void from_json(BasicJsonType && j, std::tuple& t) std::string(j.type_name()))); } - from_json_tuple_impl(std::forward(j), t, index_sequence_for {}); + return from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {}); } -template -Tuple from_json_tuple_impl(BasicJsonType&& j, tag /*unused*/, index_sequence /*unused*/) +template +auto from_json(BasicJsonType&& j, std::tuple& t) +-> decltype(from_json_tuple(std::forward(j), t, index_sequence_for {})) { - return std::make_tuple(std::forward(j).at(Idx).template get::type>()...); + from_json_tuple(std::forward(j), t, index_sequence_for {}); } -template < typename BasicJsonType, typename... Args, - enable_if_t < !std::is_default_constructible>::value, int > = 0 > -std::tuple from_json(BasicJsonType && j, tag> t) +template +auto from_json(BasicJsonType&& j, identity_tag> tag) +-> decltype(from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {})) { - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); - } - - return from_json_tuple_impl(std::forward(j), t, index_sequence_for {}); + return from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {}); } template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, @@ -3952,21 +3953,11 @@ void from_json(const BasicJsonType& j, std::unordered_map - auto operator()(const BasicJsonType& j, T& val) const - noexcept(noexcept(from_json(j, val))) - -> decltype(from_json(j, val), void()) - { - return from_json(j, val); - } - - // overload to pass calls to built-in from_json functions for non-default constructible STL - // types (e.g. std::array, where X is not default constructible). - template - auto operator()(const BasicJsonType& j, detail::tag t) const - noexcept(noexcept(from_json(j, t))) - -> decltype(from_json(j, t)) + auto operator()(const BasicJsonType& j, T&& val) const + noexcept(noexcept(from_json(j, std::forward(val)))) + -> decltype(from_json(j, std::forward(val))) { - return from_json(j, t); + return from_json(j, std::forward(val)); } }; } // namespace detail @@ -4540,6 +4531,8 @@ constexpr const auto& to_json = detail::static_const::value; } // namespace } // namespace nlohmann +// #include + // #include @@ -4582,10 +4575,10 @@ struct adl_serializer */ template static auto from_json(BasicJsonType && j) noexcept( - noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) - -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) + noexcept(::nlohmann::from_json(std::forward(j), detail::identity_tag {}))) + -> decltype(::nlohmann::from_json(std::forward(j), detail::identity_tag {})) { - return ::nlohmann::from_json(std::forward(j), detail::tag {}); + return ::nlohmann::from_json(std::forward(j), detail::identity_tag {}); } /*! From ff57bdcc8bc55bd1db425573bce3b8b899f4025f Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 10 Jan 2021 22:40:50 +0100 Subject: [PATCH 026/143] :bug: fix invariants --- .../nlohmann/detail/conversions/to_json.hpp | 10 +++ include/nlohmann/detail/input/json_sax.hpp | 33 +++---- include/nlohmann/detail/input/parser.hpp | 4 +- include/nlohmann/json.hpp | 42 ++++++--- single_include/nlohmann/json.hpp | 89 ++++++++++++------- 5 files changed, 118 insertions(+), 60 deletions(-) diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index b45004fd42..eeb7865231 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -132,6 +132,7 @@ struct external_constructor { j.m_type = value_t::array; j.m_value = arr; + j.set_parent(j, true); j.assert_invariant(); } @@ -140,6 +141,7 @@ struct external_constructor { j.m_type = value_t::array; j.m_value = std::move(arr); + j.set_parent(j, true); j.assert_invariant(); } @@ -152,6 +154,7 @@ struct external_constructor using std::end; j.m_type = value_t::array; j.m_value.array = j.template create(begin(arr), end(arr)); + j.set_parent(j, true); j.assert_invariant(); } @@ -164,6 +167,9 @@ struct external_constructor for (const bool x : arr) { j.m_value.array->push_back(x); +#if JSON_DIAGNOSTICS + j.m_value.array->back().m_parent = &j; +#endif } j.assert_invariant(); } @@ -179,6 +185,7 @@ struct external_constructor { std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin()); } + j.set_parent(j, true); j.assert_invariant(); } }; @@ -191,6 +198,7 @@ struct external_constructor { j.m_type = value_t::object; j.m_value = obj; + j.set_parent(j, true); j.assert_invariant(); } @@ -199,6 +207,7 @@ struct external_constructor { j.m_type = value_t::object; j.m_value = std::move(obj); + j.set_parent(j, true); j.assert_invariant(); } @@ -211,6 +220,7 @@ struct external_constructor j.m_type = value_t::object; j.m_value.object = j.template create(begin(obj), end(obj)); + j.set_parent(j, true); j.assert_invariant(); } }; diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index f29aa1a682..3088cbbcd8 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -236,6 +236,7 @@ class json_sax_dom_parser bool end_object() { + ref_stack.back()->set_parent(*ref_stack.back(), true); ref_stack.pop_back(); return true; } @@ -254,6 +255,7 @@ class json_sax_dom_parser bool end_array() { + ref_stack.back()->set_parent(*ref_stack.back(), true); ref_stack.pop_back(); return true; } @@ -298,18 +300,12 @@ class json_sax_dom_parser if (ref_stack.back()->is_array()) { ref_stack.back()->m_value.array->emplace_back(std::forward(v)); -#if JSON_DIAGNOSTICS - ref_stack.back()->m_value.array->back().m_parent = ref_stack.back(); -#endif return &(ref_stack.back()->m_value.array->back()); } JSON_ASSERT(ref_stack.back()->is_object()); JSON_ASSERT(object_element); *object_element = BasicJsonType(std::forward(v)); -#if JSON_DIAGNOSTICS - object_element->m_parent = ref_stack.back(); -#endif return object_element; } @@ -432,10 +428,17 @@ class json_sax_dom_callback_parser bool end_object() { - if (ref_stack.back() && !callback(static_cast(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back())) + if (ref_stack.back()) { - // discard object - *ref_stack.back() = discarded; + if (!callback(static_cast(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back())) + { + // discard object + *ref_stack.back() = discarded; + } + else + { + ref_stack.back()->set_parent(*ref_stack.back(), true); + } } JSON_ASSERT(!ref_stack.empty()); @@ -483,7 +486,11 @@ class json_sax_dom_callback_parser if (ref_stack.back()) { keep = callback(static_cast(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back()); - if (!keep) + if (keep) + { + ref_stack.back()->set_parent(*ref_stack.back(), true); + } + else { // discard array *ref_stack.back() = discarded; @@ -582,9 +589,6 @@ class json_sax_dom_callback_parser if (ref_stack.back()->is_array()) { ref_stack.back()->m_value.array->emplace_back(std::move(value)); -#if JSON_DIAGNOSTICS - ref_stack.back()->m_value.array->back().m_parent = ref_stack.back(); -#endif return {true, &(ref_stack.back()->m_value.array->back())}; } @@ -602,9 +606,6 @@ class json_sax_dom_callback_parser JSON_ASSERT(object_element); *object_element = std::move(value); -#if JSON_DIAGNOSTICS - object_element->m_parent = ref_stack.back(); -#endif return {true, object_element}; } diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index 40ef371eb9..d9ac2b684c 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -90,7 +90,6 @@ class parser { json_sax_dom_callback_parser sdp(result, callback, allow_exceptions); sax_parse_internal(&sdp); - result.assert_invariant(); // in strict mode, input must be completely read if (strict && (get_token() != token_type::end_of_input)) @@ -119,7 +118,6 @@ class parser { json_sax_dom_parser sdp(result, allow_exceptions); sax_parse_internal(&sdp); - result.assert_invariant(); // in strict mode, input must be completely read if (strict && (get_token() != token_type::end_of_input)) @@ -137,6 +135,8 @@ class parser return; } } + + result.assert_invariant(); } /*! diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index cbfde69280..10fff0baeb 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1233,13 +1233,29 @@ class basic_json invariant. Furthermore, it has to be called each time the type of a JSON value is changed, because the invariant expresses a relationship between @a m_type and @a m_value. + + Furthermore, the parent relation is checked for arrays and objects: If + @a check_parents true and the value is an array or object, then the + container's elements must have the current value as parent. + + @param[in] check_parents whether the parent relation should be checked. + The value is true by default and should only be set to true + during destruction of objects when the invariant does not + need to hold. */ - void assert_invariant() const noexcept + void assert_invariant(bool check_parents = true) const noexcept { JSON_ASSERT(m_type != value_t::object || m_value.object != nullptr); JSON_ASSERT(m_type != value_t::array || m_value.array != nullptr); JSON_ASSERT(m_type != value_t::string || m_value.string != nullptr); JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr); + +#if JSON_DIAGNOSTICS + JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j) + { + return j.m_parent == this; + })); +#endif } reference set_parent(reference j, bool recursive) @@ -1497,6 +1513,7 @@ class basic_json std::forward(val)))) { JSONSerializer::to_json(*this, std::forward(val)); + set_parent(*this, true); assert_invariant(); } @@ -1575,6 +1592,7 @@ class basic_json default: // LCOV_EXCL_LINE JSON_ASSERT(false); // LCOV_EXCL_LINE } + set_parent(*this, true); assert_invariant(); } @@ -1689,10 +1707,9 @@ class basic_json for (auto& element_ref : init) { auto element = element_ref.moved_or_copied(); - auto res = m_value.object->emplace( - std::move(*((*element.m_value.array)[0].m_value.string)), - std::move((*element.m_value.array)[1])); - set_parent(res.first->second, false); + m_value.object->emplace( + std::move(*((*element.m_value.array)[0].m_value.string)), + std::move((*element.m_value.array)[1])); } } else @@ -1700,9 +1717,9 @@ class basic_json // the initializer list describes an array -> create array m_type = value_t::array; m_value.array = create(init.begin(), init.end()); - set_parent(*this, true); } + set_parent(*this, true); assert_invariant(); } @@ -2045,7 +2062,6 @@ class basic_json { m_value.object = create(first.m_it.object_iterator, last.m_it.object_iterator); - set_parent(*this, true); break; } @@ -2053,7 +2069,6 @@ class basic_json { m_value.array = create(first.m_it.array_iterator, last.m_it.array_iterator); - set_parent(*this, true); break; } @@ -2067,6 +2082,7 @@ class basic_json JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), diagnostics_t())); } + set_parent(*this, true); assert_invariant(); } @@ -2116,14 +2132,12 @@ class basic_json case value_t::object: { m_value = *other.m_value.object; - set_parent(*this, true); break; } case value_t::array: { m_value = *other.m_value.array; - set_parent(*this, true); break; } @@ -2167,6 +2181,7 @@ class basic_json break; } + set_parent(*this, true); assert_invariant(); } @@ -2201,7 +2216,7 @@ class basic_json m_value(std::move(other.m_value)) { // check that passed value is valid - other.assert_invariant(); + other.assert_invariant(false); // invalidate payload other.m_type = value_t::null; @@ -2248,6 +2263,7 @@ class basic_json swap(m_type, other.m_type); swap(m_value, other.m_value); + set_parent(*this, true); assert_invariant(); return *this; } @@ -2269,7 +2285,7 @@ class basic_json */ ~basic_json() noexcept { - assert_invariant(); + assert_invariant(false); m_value.destroy(m_type); } @@ -5990,6 +6006,8 @@ class basic_json { std::swap(m_type, other.m_type); std::swap(m_value, other.m_value); + + set_parent(*this, true); assert_invariant(); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 4d079db097..2d98dce466 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -4302,6 +4302,7 @@ struct external_constructor { j.m_type = value_t::array; j.m_value = arr; + j.set_parent(j, true); j.assert_invariant(); } @@ -4310,6 +4311,7 @@ struct external_constructor { j.m_type = value_t::array; j.m_value = std::move(arr); + j.set_parent(j, true); j.assert_invariant(); } @@ -4322,6 +4324,7 @@ struct external_constructor using std::end; j.m_type = value_t::array; j.m_value.array = j.template create(begin(arr), end(arr)); + j.set_parent(j, true); j.assert_invariant(); } @@ -4334,6 +4337,9 @@ struct external_constructor for (const bool x : arr) { j.m_value.array->push_back(x); +#if JSON_DIAGNOSTICS + j.m_value.array->back().m_parent = &j; +#endif } j.assert_invariant(); } @@ -4349,6 +4355,7 @@ struct external_constructor { std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin()); } + j.set_parent(j, true); j.assert_invariant(); } }; @@ -4361,6 +4368,7 @@ struct external_constructor { j.m_type = value_t::object; j.m_value = obj; + j.set_parent(j, true); j.assert_invariant(); } @@ -4369,6 +4377,7 @@ struct external_constructor { j.m_type = value_t::object; j.m_value = std::move(obj); + j.set_parent(j, true); j.assert_invariant(); } @@ -4381,6 +4390,7 @@ struct external_constructor j.m_type = value_t::object; j.m_value.object = j.template create(begin(obj), end(obj)); + j.set_parent(j, true); j.assert_invariant(); } }; @@ -5628,6 +5638,7 @@ class json_sax_dom_parser bool end_object() { + ref_stack.back()->set_parent(*ref_stack.back(), true); ref_stack.pop_back(); return true; } @@ -5646,6 +5657,7 @@ class json_sax_dom_parser bool end_array() { + ref_stack.back()->set_parent(*ref_stack.back(), true); ref_stack.pop_back(); return true; } @@ -5690,18 +5702,12 @@ class json_sax_dom_parser if (ref_stack.back()->is_array()) { ref_stack.back()->m_value.array->emplace_back(std::forward(v)); -#if JSON_DIAGNOSTICS - ref_stack.back()->m_value.array->back().m_parent = ref_stack.back(); -#endif return &(ref_stack.back()->m_value.array->back()); } JSON_ASSERT(ref_stack.back()->is_object()); JSON_ASSERT(object_element); *object_element = BasicJsonType(std::forward(v)); -#if JSON_DIAGNOSTICS - object_element->m_parent = ref_stack.back(); -#endif return object_element; } @@ -5824,10 +5830,17 @@ class json_sax_dom_callback_parser bool end_object() { - if (ref_stack.back() && !callback(static_cast(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back())) + if (ref_stack.back()) { - // discard object - *ref_stack.back() = discarded; + if (!callback(static_cast(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back())) + { + // discard object + *ref_stack.back() = discarded; + } + else + { + ref_stack.back()->set_parent(*ref_stack.back(), true); + } } JSON_ASSERT(!ref_stack.empty()); @@ -5875,7 +5888,11 @@ class json_sax_dom_callback_parser if (ref_stack.back()) { keep = callback(static_cast(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back()); - if (!keep) + if (keep) + { + ref_stack.back()->set_parent(*ref_stack.back(), true); + } + else { // discard array *ref_stack.back() = discarded; @@ -5974,9 +5991,6 @@ class json_sax_dom_callback_parser if (ref_stack.back()->is_array()) { ref_stack.back()->m_value.array->emplace_back(std::move(value)); -#if JSON_DIAGNOSTICS - ref_stack.back()->m_value.array->back().m_parent = ref_stack.back(); -#endif return {true, &(ref_stack.back()->m_value.array->back())}; } @@ -5994,9 +6008,6 @@ class json_sax_dom_callback_parser JSON_ASSERT(object_element); *object_element = std::move(value); -#if JSON_DIAGNOSTICS - object_element->m_parent = ref_stack.back(); -#endif return {true, object_element}; } @@ -10430,7 +10441,6 @@ class parser { json_sax_dom_callback_parser sdp(result, callback, allow_exceptions); sax_parse_internal(&sdp); - result.assert_invariant(); // in strict mode, input must be completely read if (strict && (get_token() != token_type::end_of_input)) @@ -10459,7 +10469,6 @@ class parser { json_sax_dom_parser sdp(result, allow_exceptions); sax_parse_internal(&sdp); - result.assert_invariant(); // in strict mode, input must be completely read if (strict && (get_token() != token_type::end_of_input)) @@ -10477,6 +10486,8 @@ class parser return; } } + + result.assert_invariant(); } /*! @@ -17979,13 +17990,29 @@ class basic_json invariant. Furthermore, it has to be called each time the type of a JSON value is changed, because the invariant expresses a relationship between @a m_type and @a m_value. + + Furthermore, the parent relation is checked for arrays and objects: If + @a check_parents true and the value is an array or object, then the + container's elements must have the current value as parent. + + @param[in] check_parents whether the parent relation should be checked. + The value is true by default and should only be set to true + during destruction of objects when the invariant does not + need to hold. */ - void assert_invariant() const noexcept + void assert_invariant(bool check_parents = true) const noexcept { JSON_ASSERT(m_type != value_t::object || m_value.object != nullptr); JSON_ASSERT(m_type != value_t::array || m_value.array != nullptr); JSON_ASSERT(m_type != value_t::string || m_value.string != nullptr); JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr); + +#if JSON_DIAGNOSTICS + JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j) + { + return j.m_parent == this; + })); +#endif } reference set_parent(reference j, bool recursive) @@ -18243,6 +18270,7 @@ class basic_json std::forward(val)))) { JSONSerializer::to_json(*this, std::forward(val)); + set_parent(*this, true); assert_invariant(); } @@ -18321,6 +18349,7 @@ class basic_json default: // LCOV_EXCL_LINE JSON_ASSERT(false); // LCOV_EXCL_LINE } + set_parent(*this, true); assert_invariant(); } @@ -18435,10 +18464,9 @@ class basic_json for (auto& element_ref : init) { auto element = element_ref.moved_or_copied(); - auto res = m_value.object->emplace( - std::move(*((*element.m_value.array)[0].m_value.string)), - std::move((*element.m_value.array)[1])); - set_parent(res.first->second, false); + m_value.object->emplace( + std::move(*((*element.m_value.array)[0].m_value.string)), + std::move((*element.m_value.array)[1])); } } else @@ -18446,9 +18474,9 @@ class basic_json // the initializer list describes an array -> create array m_type = value_t::array; m_value.array = create(init.begin(), init.end()); - set_parent(*this, true); } + set_parent(*this, true); assert_invariant(); } @@ -18791,7 +18819,6 @@ class basic_json { m_value.object = create(first.m_it.object_iterator, last.m_it.object_iterator); - set_parent(*this, true); break; } @@ -18799,7 +18826,6 @@ class basic_json { m_value.array = create(first.m_it.array_iterator, last.m_it.array_iterator); - set_parent(*this, true); break; } @@ -18813,6 +18839,7 @@ class basic_json JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), diagnostics_t())); } + set_parent(*this, true); assert_invariant(); } @@ -18862,14 +18889,12 @@ class basic_json case value_t::object: { m_value = *other.m_value.object; - set_parent(*this, true); break; } case value_t::array: { m_value = *other.m_value.array; - set_parent(*this, true); break; } @@ -18913,6 +18938,7 @@ class basic_json break; } + set_parent(*this, true); assert_invariant(); } @@ -18947,7 +18973,7 @@ class basic_json m_value(std::move(other.m_value)) { // check that passed value is valid - other.assert_invariant(); + other.assert_invariant(false); // invalidate payload other.m_type = value_t::null; @@ -18994,6 +19020,7 @@ class basic_json swap(m_type, other.m_type); swap(m_value, other.m_value); + set_parent(*this, true); assert_invariant(); return *this; } @@ -19015,7 +19042,7 @@ class basic_json */ ~basic_json() noexcept { - assert_invariant(); + assert_invariant(false); m_value.destroy(m_type); } @@ -22736,6 +22763,8 @@ class basic_json { std::swap(m_type, other.m_type); std::swap(m_value, other.m_value); + + set_parent(*this, true); assert_invariant(); } From 672e8bfc1d788384432c612859f7cb080ace8803 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Mon, 11 Jan 2021 18:16:15 +0100 Subject: [PATCH 027/143] Fixed std::pair trying to deserialize via array functions. --- .../nlohmann/detail/conversions/from_json.hpp | 25 +++++++++++-------- single_include/nlohmann/json.hpp | 25 +++++++++++-------- test/src/unit-regression2.cpp | 3 --- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index b29ac05c64..aaf75e2187 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -249,17 +249,23 @@ void()) from_json_array_impl(j, arr, priority_tag<3> {}); } -template < typename BasicJsonType, typename Array, std::size_t... Is > -Array from_json_array_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/) +template < typename T, typename BasicJsonType, typename ArrayType, std::size_t... Idx> +ArrayType from_json_inplace_array_impl_base(BasicJsonType&& j, identity_tag /*unused*/, + index_sequence /*unused*/) { - return { std::forward(j).at(Is).template get()... }; + return { std::forward(j).at(Idx).template get()... }; } -template < typename BasicJsonType, typename T, std::size_t N, - enable_if_t < !std::is_default_constructible>::value, int > = 0 > -auto from_json(BasicJsonType && j, identity_tag> tag) --> decltype(j.template get(), -from_json_array_impl(std::forward(j), tag, make_index_sequence {})) +template < typename BasicJsonType, typename T, std::size_t N > +auto from_json_inplace_array_impl(BasicJsonType&& j, identity_tag> tag, priority_tag<0> /*unused*/) +-> decltype(from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {})) +{ + return from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {}); +} + +template < typename BasicJsonType, typename ArrayType > +auto from_json(BasicJsonType&& j, identity_tag tag) +-> decltype(from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -267,7 +273,7 @@ from_json_array_impl(std::forward(j), tag, make_index_sequence std::string(j.type_name()))); } - return from_json_array_impl(std::forward(j), tag, make_index_sequence {}); + return from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {}); } template @@ -352,7 +358,6 @@ std::pair from_json_pair_impl(BasicJsonType&& j, identity_tag(j).at(1).template get()}; } - template>::value, int> = 0> void from_json_pair_impl(BasicJsonType && j, std::pair& p, priority_tag<1> /*unused*/) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f3c1b987c1..b452ca713f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3746,17 +3746,23 @@ void()) from_json_array_impl(j, arr, priority_tag<3> {}); } -template < typename BasicJsonType, typename Array, std::size_t... Is > -Array from_json_array_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/) +template < typename T, typename BasicJsonType, typename ArrayType, std::size_t... Idx> +ArrayType from_json_inplace_array_impl_base(BasicJsonType&& j, identity_tag /*unused*/, + index_sequence /*unused*/) { - return { std::forward(j).at(Is).template get()... }; + return { std::forward(j).at(Idx).template get()... }; } -template < typename BasicJsonType, typename T, std::size_t N, - enable_if_t < !std::is_default_constructible>::value, int > = 0 > -auto from_json(BasicJsonType && j, identity_tag> tag) --> decltype(j.template get(), -from_json_array_impl(std::forward(j), tag, make_index_sequence {})) +template < typename BasicJsonType, typename T, std::size_t N > +auto from_json_inplace_array_impl(BasicJsonType&& j, identity_tag> tag, priority_tag<0> /*unused*/) +-> decltype(from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {})) +{ + return from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {}); +} + +template < typename BasicJsonType, typename ArrayType > +auto from_json(BasicJsonType&& j, identity_tag tag) +-> decltype(from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -3764,7 +3770,7 @@ from_json_array_impl(std::forward(j), tag, make_index_sequence std::string(j.type_name()))); } - return from_json_array_impl(std::forward(j), tag, make_index_sequence {}); + return from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {}); } template @@ -3849,7 +3855,6 @@ std::pair from_json_pair_impl(BasicJsonType&& j, identity_tag(j).at(1).template get()}; } - template>::value, int> = 0> void from_json_pair_impl(BasicJsonType && j, std::pair& p, priority_tag<1> /*unused*/) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 1046bb2e58..e31296fac6 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -543,9 +543,6 @@ TEST_CASE("regression tests 2") { { json j = { 3, 8 }; - auto x = j.at(0).get(); - CHECK(x.x == 3); - auto p = j.get>(); CHECK(p.first.x == 3); CHECK(p.second.x == 8); From 6ebf274ca093939af7bb5e921a20221b9204d410 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Tue, 12 Jan 2021 18:28:00 +0100 Subject: [PATCH 028/143] Add internal version of is_default_constructible to work around LWG 2367. --- include/nlohmann/detail/meta/type_traits.hpp | 73 +++++++++++++++----- 1 file changed, 56 insertions(+), 17 deletions(-) diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index e30d99e4ce..1fbf7cde7b 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -149,6 +149,52 @@ struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> /////////////////// // is_ functions // /////////////////// +// https://en.cppreference.com/w/cpp/types/conjunction +template struct conjunction : std::true_type { }; +template struct conjunction : B1 { }; +template +struct conjunction +: std::conditional, B1>::type {}; + +// Reimplementation of is_constructible and is_default_constructible, due to them being broken for +// std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367). +// This causes compile errors in e.g. clang 3.5 or gcc 4.9. +// Based on commit fixing this in gcc: https://github.com/gcc-mirror/gcc/commit/d3c64041b32b6962ad6b2d879231537a477631fb +template +struct is_default_constructible : std::is_default_constructible {}; + +template +struct is_default_constructible> + : conjunction, is_default_constructible> {}; + +template +struct is_default_constructible> + : conjunction, is_default_constructible> {}; + +template +struct is_default_constructible> + : conjunction...> {}; + +template +struct is_default_constructible> + : conjunction...> {}; + + +template +struct is_constructible : std::is_constructible {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_constructible> : is_default_constructible> {}; + template struct is_iterator_traits : std::false_type {}; @@ -192,9 +238,9 @@ struct is_compatible_object_type_impl < // macOS's is_constructible does not play well with nonesuch... static constexpr bool value = - std::is_constructible::value && - std::is_constructible::value; }; @@ -215,10 +261,10 @@ struct is_constructible_object_type_impl < using object_t = typename BasicJsonType::object_t; static constexpr bool value = - (std::is_default_constructible::value && + (is_default_constructible::value && (std::is_move_assignable::value || std::is_copy_assignable::value) && - (std::is_constructible::value && std::is_same < typename object_t::mapped_type, @@ -246,7 +292,7 @@ struct is_compatible_string_type_impl < value_type_t, CompatibleStringType>::value >> { static constexpr auto value = - std::is_constructible::value; + is_constructible::value; }; template @@ -264,7 +310,7 @@ struct is_constructible_string_type_impl < value_type_t, ConstructibleStringType>::value >> { static constexpr auto value = - std::is_constructible::value; }; @@ -287,7 +333,7 @@ struct is_compatible_array_type_impl < iterator_traits>::value >> { static constexpr bool value = - std::is_constructible::value; }; @@ -310,7 +356,7 @@ struct is_constructible_array_type_impl < BasicJsonType, ConstructibleArrayType, enable_if_t < !std::is_same::value&& - std::is_default_constructible::value&& + is_default_constructible::value&& (std::is_move_assignable::value || std::is_copy_assignable::value)&& is_detected::value&& @@ -354,7 +400,7 @@ struct is_compatible_integer_type_impl < using CompatibleLimits = std::numeric_limits; static constexpr auto value = - std::is_constructible::value && CompatibleLimits::is_integer && RealLimits::is_signed == CompatibleLimits::is_signed; @@ -381,17 +427,10 @@ template struct is_compatible_type : is_compatible_type_impl {}; -// https://en.cppreference.com/w/cpp/types/conjunction -template struct conjunction : std::true_type { }; -template struct conjunction : B1 { }; -template -struct conjunction -: std::conditional, B1>::type {}; - template struct is_constructible_tuple : std::false_type {}; template -struct is_constructible_tuple> : conjunction...> {}; +struct is_constructible_tuple> : conjunction...> {}; } // namespace detail } // namespace nlohmann From 6278f31d23d77f472618b4cf3b0f9a37fc1e742a Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Tue, 12 Jan 2021 18:28:29 +0100 Subject: [PATCH 029/143] Simplify from_json overloads. --- .../nlohmann/detail/conversions/from_json.hpp | 65 +++++++------------ 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index aaf75e2187..fcb6a5e60e 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -249,23 +249,16 @@ void()) from_json_array_impl(j, arr, priority_tag<3> {}); } -template < typename T, typename BasicJsonType, typename ArrayType, std::size_t... Idx> -ArrayType from_json_inplace_array_impl_base(BasicJsonType&& j, identity_tag /*unused*/, - index_sequence /*unused*/) +template < typename BasicJsonType, typename T, std::size_t... Idx > +std::array from_json_inplace_array_impl(BasicJsonType&& j, + identity_tag> /*unused*/, index_sequence /*unused*/) { return { std::forward(j).at(Idx).template get()... }; } template < typename BasicJsonType, typename T, std::size_t N > -auto from_json_inplace_array_impl(BasicJsonType&& j, identity_tag> tag, priority_tag<0> /*unused*/) --> decltype(from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {})) -{ - return from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {}); -} - -template < typename BasicJsonType, typename ArrayType > -auto from_json(BasicJsonType&& j, identity_tag tag) --> decltype(from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {})) +auto from_json(BasicJsonType&& j, identity_tag> tag) +-> decltype(from_json_inplace_array_impl(std::forward(j), tag, make_index_sequence {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -273,7 +266,7 @@ auto from_json(BasicJsonType&& j, identity_tag tag) std::string(j.type_name()))); } - return from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {}); + return from_json_inplace_array_impl(std::forward(j), tag, make_index_sequence {}); } template @@ -358,9 +351,8 @@ std::pair from_json_pair_impl(BasicJsonType&& j, identity_tag(j).at(1).template get()}; } -template>::value, int> = 0> -void from_json_pair_impl(BasicJsonType && j, std::pair& p, priority_tag<1> /*unused*/) +template +void from_json_pair_impl(BasicJsonType&& j, std::pair& p, priority_tag<1> /*unused*/) { p = from_json_pair_impl(std::forward(j), identity_tag> {}, priority_tag<0> {}); } @@ -378,22 +370,27 @@ auto from_json(BasicJsonType&& j, PairRelatedType&& p) return from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {}); } -template -Tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/, priority_tag<0> /*unused*/) +template +std::tuple from_json_tuple_impl_base(BasicJsonType&& j, index_sequence /*unused*/) +{ + return std::make_tuple(std::forward(j).at(Idx).template get()...); +} + +template +std::tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) { - return std::make_tuple(std::forward(j).at(Idx).template get::type>()...); + return from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); } -template::value, int> = 0> -void from_json_tuple_impl(BasicJsonType && j, Tuple& t, index_sequence /*unused*/, priority_tag<1> /*unused*/) +template +void from_json_tuple_impl(BasicJsonType&& j, std::tuple& t, priority_tag<1> /*unused*/) { - t = from_json_tuple_impl(std::forward(j), identity_tag {}, priority_tag<0> {}); + t = from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); } -template -auto from_json_tuple(BasicJsonType&& j, TupleRelated&& t, index_sequence idx) --> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {})) +template +auto from_json(BasicJsonType&& j, TupleRelated&& t) +-> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<1> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -401,21 +398,7 @@ auto from_json_tuple(BasicJsonType&& j, TupleRelated&& t, index_sequence std::string(j.type_name()))); } - return from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {}); -} - -template -auto from_json(BasicJsonType&& j, std::tuple& t) --> decltype(from_json_tuple(std::forward(j), t, index_sequence_for {})) -{ - from_json_tuple(std::forward(j), t, index_sequence_for {}); -} - -template -auto from_json(BasicJsonType&& j, identity_tag> tag) --> decltype(from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {})) -{ - return from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {}); + return from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<1> {}); } template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, From 6ef1614fa92879aec9b328e95ade3be2a97cc820 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Tue, 12 Jan 2021 18:28:47 +0100 Subject: [PATCH 030/143] Refactor json::get() to use priority_tag. --- include/nlohmann/json.hpp | 312 +++++++++++++++++++++----------------- 1 file changed, 169 insertions(+), 143 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 2ca029dd13..500acb36ec 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -2838,50 +2838,53 @@ class basic_json /// @{ /*! - @brief get special-case overload + @brief get a pointer value (implicit) - This overloads avoids a lot of template boilerplate, it can be seen as the - identity method + Implicit pointer access to the internally stored JSON value. No copies are + made. - @tparam BasicJsonType == @ref basic_json + @warning Writing data to the pointee of the result yields an undefined + state. - @return a copy of *this + @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref + object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, + @ref number_unsigned_t, or @ref number_float_t. Enforced by a static + assertion. + + @return pointer to the internally stored JSON value if the requested + pointer type @a PointerType fits to the JSON value; `nullptr` otherwise @complexity Constant. - @since version 2.1.0 + @liveexample{The example below shows how pointers to internal values of a + JSON value can be requested. Note that no type conversions are made and a + `nullptr` is returned if the value and the requested pointer type does not + match.,get_ptr} + + @since version 1.0.0 */ - template::type, basic_json_t>::value, - int> = 0> - basic_json get() const + template::value, int>::type = 0> + auto get_ptr() noexcept -> decltype(std::declval().get_impl_ptr(std::declval())) { - return *this; + // delegate the call to get_impl_ptr<>() + return get_impl_ptr(static_cast(nullptr)); } /*! - @brief get special-case overload - - This overloads converts the current @ref basic_json in a different - @ref basic_json type - - @tparam BasicJsonType == @ref basic_json - - @return a copy of *this, converted into @tparam BasicJsonType - - @complexity Depending on the implementation of the called `from_json()` - method. - - @since version 3.2.0 + @brief get a pointer value (implicit) + @copydoc get_ptr() */ - template < typename BasicJsonType, detail::enable_if_t < - !std::is_same::value&& - detail::is_basic_json::value, int > = 0 > - BasicJsonType get() const + template < typename PointerType, typename std::enable_if < + std::is_pointer::value&& + std::is_const::type>::value, int >::type = 0 > + constexpr auto get_ptr() const noexcept -> decltype(std::declval().get_impl_ptr(std::declval())) { - return *this; + // delegate the call to get_impl_ptr<>() const + return get_impl_ptr(static_cast(nullptr)); } + private: /*! @brief get a value (explicit) @@ -2923,21 +2926,12 @@ class basic_json */ template < typename ValueTypeCV, typename ValueType = detail::uncvref_t, detail::enable_if_t < - !detail::is_basic_json::value && - detail::has_from_json::value && - !detail::has_non_default_from_json::value, + detail::is_default_constructible::value && + detail::has_from_json::value, int > = 0 > - ValueType get() const noexcept(noexcept( - JSONSerializer::from_json(std::declval(), std::declval()))) + ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept( + JSONSerializer::from_json(std::declval(), std::declval()))) { - // we cannot static_assert on ValueTypeCV being non-const, because - // there is support for get(), which is why we - // still need the uncvref - static_assert(!std::is_reference::value, - "get() cannot be used with reference types, you might want to use get_ref()"); - static_assert(std::is_default_constructible::value, - "types must be DefaultConstructible when used with get()"); - ValueType ret; JSONSerializer::from_json(*this, ret); return ret; @@ -2975,133 +2969,107 @@ class basic_json @since version 2.1.0 */ template < typename ValueTypeCV, typename ValueType = detail::uncvref_t, - detail::enable_if_t < !std::is_same::value && - detail::has_non_default_from_json::value, - int > = 0 > - ValueType get() const noexcept(noexcept( - JSONSerializer::from_json(std::declval()))) + detail::enable_if_t < + detail::has_non_default_from_json::value, + int > = 0 > + ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept( + JSONSerializer::from_json(std::declval()))) { - static_assert(!std::is_reference::value, - "get() cannot be used with reference types, you might want to use get_ref()"); return JSONSerializer::from_json(*this); } /*! - @brief get a value (explicit) - - Explicit type conversion between the JSON value and a compatible value. - The value is filled into the input parameter by calling the @ref json_serializer - `from_json()` method. - - The function is equivalent to executing - @code {.cpp} - ValueType v; - JSONSerializer::from_json(*this, v); - @endcode - - This overloads is chosen if: - - @a ValueType is not @ref basic_json, - - @ref json_serializer has a `from_json()` method of the form - `void from_json(const basic_json&, ValueType&)`, and + @brief get special-case overload - @tparam ValueType the input parameter type. + This overloads converts the current @ref basic_json in a different + @ref basic_json type - @return the input parameter, allowing chaining calls. + @tparam BasicJsonType == @ref basic_json - @throw what @ref json_serializer `from_json()` method throws + @return a copy of *this, converted into @tparam BasicJsonType - @liveexample{The example below shows several conversions from JSON values - to other types. There a few things to note: (1) Floating-point numbers can - be converted to integers\, (2) A JSON array can be converted to a standard - `std::vector`\, (3) A JSON object can be converted to C++ - associative containers such as `std::unordered_map`.,get_to} + @complexity Depending on the implementation of the called `from_json()` + method. - @since version 3.3.0 + @since version 3.2.0 */ - template < typename ValueType, - detail::enable_if_t < - !detail::is_basic_json::value&& - detail::has_from_json::value, + template < typename BasicJsonType, detail::enable_if_t < + detail::is_basic_json::value, int > = 0 > - ValueType & get_to(ValueType& v) const noexcept(noexcept( - JSONSerializer::from_json(std::declval(), v))) + BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const { - JSONSerializer::from_json(*this, v); - return v; + return *this; } - // specialization to allow to call get_to with a basic_json value - // see https://github.com/nlohmann/json/issues/2175 - template::value, + /*! + @brief get special-case overload + + This overloads avoids a lot of template boilerplate, it can be seen as the + identity method + + @tparam BasicJsonType == @ref basic_json + + @return a copy of *this + + @complexity Constant. + + @since version 2.1.0 + */ + template::value, int> = 0> - ValueType & get_to(ValueType& v) const + basic_json get_impl(detail::priority_tag<3> /*unused*/) const { - v = *this; - return v; + return *this; } - template < - typename T, std::size_t N, - typename Array = T (&)[N], - detail::enable_if_t < - detail::has_from_json::value, int > = 0 > - Array get_to(T (&v)[N]) const - noexcept(noexcept(JSONSerializer::from_json( - std::declval(), v))) + /*! + @brief get a pointer value (explicit) + @copydoc get() + */ + template::value, int> = 0> + constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept + -> decltype(std::declval().template get_ptr()) { - JSONSerializer::from_json(*this, v); - return v; + // delegate the call to get_ptr + return get_ptr(); } - + public: /*! - @brief get a pointer value (implicit) + @brief get a (pointer) value (explicit) - Implicit pointer access to the internally stored JSON value. No copies are - made. + Performs explicit type conversion between the JSON value and a compatible value if required. - @warning Writing data to the pointee of the result yields an undefined - state. + - If the requested type is a pointer to the internally stored JSON value that pointer is returned. + No copies are made. - @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref - object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, - @ref number_unsigned_t, or @ref number_float_t. Enforced by a static - assertion. + - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible + from the current @ref basic_json. - @return pointer to the internally stored JSON value if the requested - pointer type @a PointerType fits to the JSON value; `nullptr` otherwise + - Otherwise the value is converted by calling the @ref json_serializer `from_json()` + method. - @complexity Constant. + @tparam ValueTypeCV the provided value type + @tparam ValueType the returned value type - @liveexample{The example below shows how pointers to internal values of a - JSON value can be requested. Note that no type conversions are made and a - `nullptr` is returned if the value and the requested pointer type does not - match.,get_ptr} + @return copy of the JSON value, converted to @tparam ValueType if necessary - @since version 1.0.0 - */ - template::value, int>::type = 0> - auto get_ptr() noexcept -> decltype(std::declval().get_impl_ptr(std::declval())) - { - // delegate the call to get_impl_ptr<>() - return get_impl_ptr(static_cast(nullptr)); - } + @throw what @ref json_serializer `from_json()` method throws if conversion is required - /*! - @brief get a pointer value (implicit) - @copydoc get_ptr() + @since version 2.1.0 */ - template < typename PointerType, typename std::enable_if < - std::is_pointer::value&& - std::is_const::type>::value, int >::type = 0 > - constexpr auto get_ptr() const noexcept -> decltype(std::declval().get_impl_ptr(std::declval())) + template < typename ValueTypeCV, typename ValueType = detail::uncvref_t> + constexpr auto get() const noexcept(noexcept(get_impl(detail::priority_tag<4> {}))) + -> decltype(get_impl(detail::priority_tag<4> {})) { - // delegate the call to get_impl_ptr<>() const - return get_impl_ptr(static_cast(nullptr)); + // we cannot static_assert on ValueTypeCV being non-const, because + // there is support for get(), which is why we + // still need the uncvref + static_assert(!std::is_reference::value, + "get() cannot be used with reference types, you might want to use get_ref()"); + return get_impl(detail::priority_tag<4> {}); } /*! @@ -3140,15 +3108,73 @@ class basic_json } /*! - @brief get a pointer value (explicit) - @copydoc get() + @brief get a value (explicit) + + Explicit type conversion between the JSON value and a compatible value. + The value is filled into the input parameter by calling the @ref json_serializer + `from_json()` method. + + The function is equivalent to executing + @code {.cpp} + ValueType v; + JSONSerializer::from_json(*this, v); + @endcode + + This overloads is chosen if: + - @a ValueType is not @ref basic_json, + - @ref json_serializer has a `from_json()` method of the form + `void from_json(const basic_json&, ValueType&)`, and + + @tparam ValueType the input parameter type. + + @return the input parameter, allowing chaining calls. + + @throw what @ref json_serializer `from_json()` method throws + + @liveexample{The example below shows several conversions from JSON values + to other types. There a few things to note: (1) Floating-point numbers can + be converted to integers\, (2) A JSON array can be converted to a standard + `std::vector`\, (3) A JSON object can be converted to C++ + associative containers such as `std::unordered_map`.,get_to} + + @since version 3.3.0 */ - template::value, int>::type = 0> - constexpr auto get() const noexcept -> decltype(std::declval().template get_ptr()) + template < typename ValueType, + detail::enable_if_t < + !detail::is_basic_json::value&& + detail::has_from_json::value, + int > = 0 > + ValueType & get_to(ValueType& v) const noexcept(noexcept( + JSONSerializer::from_json(std::declval(), v))) { - // delegate the call to get_ptr - return get_ptr(); + JSONSerializer::from_json(*this, v); + return v; + } + + // specialization to allow to call get_to with a basic_json value + // see https://github.com/nlohmann/json/issues/2175 + template::value, + int> = 0> + ValueType & get_to(ValueType& v) const + { + v = *this; + return v; + } + + template < + typename T, std::size_t N, + typename Array = T (&)[N], + detail::enable_if_t < + detail::has_from_json::value, int > = 0 > + Array get_to(T (&v)[N]) const + noexcept(noexcept(JSONSerializer::from_json( + std::declval(), v))) + { + JSONSerializer::from_json(*this, v); + return v; } /*! From fc8c584288162e73a9745ba18eacfc48252a1535 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Tue, 12 Jan 2021 18:29:05 +0100 Subject: [PATCH 031/143] Regenerated single include. --- single_include/nlohmann/json.hpp | 450 +++++++++++++++++-------------- 1 file changed, 249 insertions(+), 201 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index b452ca713f..004850c3aa 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3185,6 +3185,52 @@ struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> /////////////////// // is_ functions // /////////////////// +// https://en.cppreference.com/w/cpp/types/conjunction +template struct conjunction : std::true_type { }; +template struct conjunction : B1 { }; +template +struct conjunction +: std::conditional, B1>::type {}; + +// Reimplementation of is_constructible and is_default_constructible, due to them being broken for +// std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367). +// This causes compile errors in e.g. clang 3.5 or gcc 4.9. +// Based on commit fixing this in gcc: https://github.com/gcc-mirror/gcc/commit/d3c64041b32b6962ad6b2d879231537a477631fb +template +struct is_default_constructible : std::is_default_constructible {}; + +template +struct is_default_constructible> + : conjunction, is_default_constructible> {}; + +template +struct is_default_constructible> + : conjunction, is_default_constructible> {}; + +template +struct is_default_constructible> + : conjunction...> {}; + +template +struct is_default_constructible> + : conjunction...> {}; + + +template +struct is_constructible : std::is_constructible {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_constructible> : is_default_constructible> {}; + template struct is_iterator_traits : std::false_type {}; @@ -3228,9 +3274,9 @@ struct is_compatible_object_type_impl < // macOS's is_constructible does not play well with nonesuch... static constexpr bool value = - std::is_constructible::value && - std::is_constructible::value; }; @@ -3251,10 +3297,10 @@ struct is_constructible_object_type_impl < using object_t = typename BasicJsonType::object_t; static constexpr bool value = - (std::is_default_constructible::value && + (is_default_constructible::value && (std::is_move_assignable::value || std::is_copy_assignable::value) && - (std::is_constructible::value && std::is_same < typename object_t::mapped_type, @@ -3282,7 +3328,7 @@ struct is_compatible_string_type_impl < value_type_t, CompatibleStringType>::value >> { static constexpr auto value = - std::is_constructible::value; + is_constructible::value; }; template @@ -3300,7 +3346,7 @@ struct is_constructible_string_type_impl < value_type_t, ConstructibleStringType>::value >> { static constexpr auto value = - std::is_constructible::value; }; @@ -3323,7 +3369,7 @@ struct is_compatible_array_type_impl < iterator_traits>::value >> { static constexpr bool value = - std::is_constructible::value; }; @@ -3346,7 +3392,7 @@ struct is_constructible_array_type_impl < BasicJsonType, ConstructibleArrayType, enable_if_t < !std::is_same::value&& - std::is_default_constructible::value&& + is_default_constructible::value&& (std::is_move_assignable::value || std::is_copy_assignable::value)&& is_detected::value&& @@ -3390,7 +3436,7 @@ struct is_compatible_integer_type_impl < using CompatibleLimits = std::numeric_limits; static constexpr auto value = - std::is_constructible::value && CompatibleLimits::is_integer && RealLimits::is_signed == CompatibleLimits::is_signed; @@ -3417,18 +3463,11 @@ template struct is_compatible_type : is_compatible_type_impl {}; -// https://en.cppreference.com/w/cpp/types/conjunction -template struct conjunction : std::true_type { }; -template struct conjunction : B1 { }; -template -struct conjunction -: std::conditional, B1>::type {}; - template struct is_constructible_tuple : std::false_type {}; template -struct is_constructible_tuple> : conjunction...> {}; +struct is_constructible_tuple> : conjunction...> {}; } // namespace detail } // namespace nlohmann @@ -3746,23 +3785,16 @@ void()) from_json_array_impl(j, arr, priority_tag<3> {}); } -template < typename T, typename BasicJsonType, typename ArrayType, std::size_t... Idx> -ArrayType from_json_inplace_array_impl_base(BasicJsonType&& j, identity_tag /*unused*/, - index_sequence /*unused*/) +template < typename BasicJsonType, typename T, std::size_t... Idx > +std::array from_json_inplace_array_impl(BasicJsonType&& j, + identity_tag> /*unused*/, index_sequence /*unused*/) { return { std::forward(j).at(Idx).template get()... }; } template < typename BasicJsonType, typename T, std::size_t N > -auto from_json_inplace_array_impl(BasicJsonType&& j, identity_tag> tag, priority_tag<0> /*unused*/) --> decltype(from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {})) -{ - return from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {}); -} - -template < typename BasicJsonType, typename ArrayType > -auto from_json(BasicJsonType&& j, identity_tag tag) --> decltype(from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {})) +auto from_json(BasicJsonType&& j, identity_tag> tag) +-> decltype(from_json_inplace_array_impl(std::forward(j), tag, make_index_sequence {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -3770,7 +3802,7 @@ auto from_json(BasicJsonType&& j, identity_tag tag) std::string(j.type_name()))); } - return from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {}); + return from_json_inplace_array_impl(std::forward(j), tag, make_index_sequence {}); } template @@ -3855,9 +3887,8 @@ std::pair from_json_pair_impl(BasicJsonType&& j, identity_tag(j).at(1).template get()}; } -template>::value, int> = 0> -void from_json_pair_impl(BasicJsonType && j, std::pair& p, priority_tag<1> /*unused*/) +template +void from_json_pair_impl(BasicJsonType&& j, std::pair& p, priority_tag<1> /*unused*/) { p = from_json_pair_impl(std::forward(j), identity_tag> {}, priority_tag<0> {}); } @@ -3875,22 +3906,27 @@ auto from_json(BasicJsonType&& j, PairRelatedType&& p) return from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {}); } -template -Tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/, priority_tag<0> /*unused*/) +template +std::tuple from_json_tuple_impl_base(BasicJsonType&& j, index_sequence /*unused*/) { - return std::make_tuple(std::forward(j).at(Idx).template get::type>()...); + return std::make_tuple(std::forward(j).at(Idx).template get()...); } -template::value, int> = 0> -void from_json_tuple_impl(BasicJsonType && j, Tuple& t, index_sequence /*unused*/, priority_tag<1> /*unused*/) +template +std::tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) { - t = from_json_tuple_impl(std::forward(j), identity_tag {}, priority_tag<0> {}); + return from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); } -template -auto from_json_tuple(BasicJsonType&& j, TupleRelated&& t, index_sequence idx) --> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {})) +template +void from_json_tuple_impl(BasicJsonType&& j, std::tuple& t, priority_tag<1> /*unused*/) +{ + t = from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); +} + +template +auto from_json(BasicJsonType&& j, TupleRelated&& t) +-> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<1> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -3898,21 +3934,7 @@ auto from_json_tuple(BasicJsonType&& j, TupleRelated&& t, index_sequence std::string(j.type_name()))); } - return from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {}); -} - -template -auto from_json(BasicJsonType&& j, std::tuple& t) --> decltype(from_json_tuple(std::forward(j), t, index_sequence_for {})) -{ - from_json_tuple(std::forward(j), t, index_sequence_for {}); -} - -template -auto from_json(BasicJsonType&& j, identity_tag> tag) --> decltype(from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {})) -{ - return from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {}); + return from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<1> {}); } template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, @@ -19575,50 +19597,53 @@ class basic_json /// @{ /*! - @brief get special-case overload + @brief get a pointer value (implicit) - This overloads avoids a lot of template boilerplate, it can be seen as the - identity method + Implicit pointer access to the internally stored JSON value. No copies are + made. - @tparam BasicJsonType == @ref basic_json + @warning Writing data to the pointee of the result yields an undefined + state. - @return a copy of *this + @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref + object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, + @ref number_unsigned_t, or @ref number_float_t. Enforced by a static + assertion. + + @return pointer to the internally stored JSON value if the requested + pointer type @a PointerType fits to the JSON value; `nullptr` otherwise @complexity Constant. - @since version 2.1.0 + @liveexample{The example below shows how pointers to internal values of a + JSON value can be requested. Note that no type conversions are made and a + `nullptr` is returned if the value and the requested pointer type does not + match.,get_ptr} + + @since version 1.0.0 */ - template::type, basic_json_t>::value, - int> = 0> - basic_json get() const + template::value, int>::type = 0> + auto get_ptr() noexcept -> decltype(std::declval().get_impl_ptr(std::declval())) { - return *this; + // delegate the call to get_impl_ptr<>() + return get_impl_ptr(static_cast(nullptr)); } /*! - @brief get special-case overload - - This overloads converts the current @ref basic_json in a different - @ref basic_json type - - @tparam BasicJsonType == @ref basic_json - - @return a copy of *this, converted into @tparam BasicJsonType - - @complexity Depending on the implementation of the called `from_json()` - method. - - @since version 3.2.0 + @brief get a pointer value (implicit) + @copydoc get_ptr() */ - template < typename BasicJsonType, detail::enable_if_t < - !std::is_same::value&& - detail::is_basic_json::value, int > = 0 > - BasicJsonType get() const + template < typename PointerType, typename std::enable_if < + std::is_pointer::value&& + std::is_const::type>::value, int >::type = 0 > + constexpr auto get_ptr() const noexcept -> decltype(std::declval().get_impl_ptr(std::declval())) { - return *this; + // delegate the call to get_impl_ptr<>() const + return get_impl_ptr(static_cast(nullptr)); } + private: /*! @brief get a value (explicit) @@ -19660,21 +19685,12 @@ class basic_json */ template < typename ValueTypeCV, typename ValueType = detail::uncvref_t, detail::enable_if_t < - !detail::is_basic_json::value && - detail::has_from_json::value && - !detail::has_non_default_from_json::value, + detail::is_default_constructible::value && + detail::has_from_json::value, int > = 0 > - ValueType get() const noexcept(noexcept( - JSONSerializer::from_json(std::declval(), std::declval()))) + ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept( + JSONSerializer::from_json(std::declval(), std::declval()))) { - // we cannot static_assert on ValueTypeCV being non-const, because - // there is support for get(), which is why we - // still need the uncvref - static_assert(!std::is_reference::value, - "get() cannot be used with reference types, you might want to use get_ref()"); - static_assert(std::is_default_constructible::value, - "types must be DefaultConstructible when used with get()"); - ValueType ret; JSONSerializer::from_json(*this, ret); return ret; @@ -19712,133 +19728,107 @@ class basic_json @since version 2.1.0 */ template < typename ValueTypeCV, typename ValueType = detail::uncvref_t, - detail::enable_if_t < !std::is_same::value && - detail::has_non_default_from_json::value, - int > = 0 > - ValueType get() const noexcept(noexcept( - JSONSerializer::from_json(std::declval()))) + detail::enable_if_t < + detail::has_non_default_from_json::value, + int > = 0 > + ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept( + JSONSerializer::from_json(std::declval()))) { - static_assert(!std::is_reference::value, - "get() cannot be used with reference types, you might want to use get_ref()"); return JSONSerializer::from_json(*this); } /*! - @brief get a value (explicit) - - Explicit type conversion between the JSON value and a compatible value. - The value is filled into the input parameter by calling the @ref json_serializer - `from_json()` method. - - The function is equivalent to executing - @code {.cpp} - ValueType v; - JSONSerializer::from_json(*this, v); - @endcode - - This overloads is chosen if: - - @a ValueType is not @ref basic_json, - - @ref json_serializer has a `from_json()` method of the form - `void from_json(const basic_json&, ValueType&)`, and + @brief get special-case overload - @tparam ValueType the input parameter type. + This overloads converts the current @ref basic_json in a different + @ref basic_json type - @return the input parameter, allowing chaining calls. + @tparam BasicJsonType == @ref basic_json - @throw what @ref json_serializer `from_json()` method throws + @return a copy of *this, converted into @tparam BasicJsonType - @liveexample{The example below shows several conversions from JSON values - to other types. There a few things to note: (1) Floating-point numbers can - be converted to integers\, (2) A JSON array can be converted to a standard - `std::vector`\, (3) A JSON object can be converted to C++ - associative containers such as `std::unordered_map`.,get_to} + @complexity Depending on the implementation of the called `from_json()` + method. - @since version 3.3.0 + @since version 3.2.0 */ - template < typename ValueType, - detail::enable_if_t < - !detail::is_basic_json::value&& - detail::has_from_json::value, + template < typename BasicJsonType, detail::enable_if_t < + detail::is_basic_json::value, int > = 0 > - ValueType & get_to(ValueType& v) const noexcept(noexcept( - JSONSerializer::from_json(std::declval(), v))) + BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const { - JSONSerializer::from_json(*this, v); - return v; + return *this; } - // specialization to allow to call get_to with a basic_json value - // see https://github.com/nlohmann/json/issues/2175 - template::value, + /*! + @brief get special-case overload + + This overloads avoids a lot of template boilerplate, it can be seen as the + identity method + + @tparam BasicJsonType == @ref basic_json + + @return a copy of *this + + @complexity Constant. + + @since version 2.1.0 + */ + template::value, int> = 0> - ValueType & get_to(ValueType& v) const + basic_json get_impl(detail::priority_tag<3> /*unused*/) const { - v = *this; - return v; + return *this; } - template < - typename T, std::size_t N, - typename Array = T (&)[N], - detail::enable_if_t < - detail::has_from_json::value, int > = 0 > - Array get_to(T (&v)[N]) const - noexcept(noexcept(JSONSerializer::from_json( - std::declval(), v))) + /*! + @brief get a pointer value (explicit) + @copydoc get() + */ + template::value, int> = 0> + constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept + -> decltype(std::declval().template get_ptr()) { - JSONSerializer::from_json(*this, v); - return v; + // delegate the call to get_ptr + return get_ptr(); } - + public: /*! - @brief get a pointer value (implicit) + @brief get a (pointer) value (explicit) - Implicit pointer access to the internally stored JSON value. No copies are - made. + Performs explicit type conversion between the JSON value and a compatible value if required. - @warning Writing data to the pointee of the result yields an undefined - state. + - If the requested type is a pointer to the internally stored JSON value that pointer is returned. + No copies are made. - @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref - object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, - @ref number_unsigned_t, or @ref number_float_t. Enforced by a static - assertion. + - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible + from the current @ref basic_json. - @return pointer to the internally stored JSON value if the requested - pointer type @a PointerType fits to the JSON value; `nullptr` otherwise + - Otherwise the value is converted by calling the @ref json_serializer `from_json()` + method. - @complexity Constant. + @tparam ValueTypeCV the provided value type + @tparam ValueType the returned value type - @liveexample{The example below shows how pointers to internal values of a - JSON value can be requested. Note that no type conversions are made and a - `nullptr` is returned if the value and the requested pointer type does not - match.,get_ptr} + @return copy of the JSON value, converted to @tparam ValueType if necessary - @since version 1.0.0 - */ - template::value, int>::type = 0> - auto get_ptr() noexcept -> decltype(std::declval().get_impl_ptr(std::declval())) - { - // delegate the call to get_impl_ptr<>() - return get_impl_ptr(static_cast(nullptr)); - } + @throw what @ref json_serializer `from_json()` method throws if conversion is required - /*! - @brief get a pointer value (implicit) - @copydoc get_ptr() + @since version 2.1.0 */ - template < typename PointerType, typename std::enable_if < - std::is_pointer::value&& - std::is_const::type>::value, int >::type = 0 > - constexpr auto get_ptr() const noexcept -> decltype(std::declval().get_impl_ptr(std::declval())) + template < typename ValueTypeCV, typename ValueType = detail::uncvref_t> + constexpr auto get() const noexcept(noexcept(get_impl(detail::priority_tag<4> {}))) + -> decltype(get_impl(detail::priority_tag<4> {})) { - // delegate the call to get_impl_ptr<>() const - return get_impl_ptr(static_cast(nullptr)); + // we cannot static_assert on ValueTypeCV being non-const, because + // there is support for get(), which is why we + // still need the uncvref + static_assert(!std::is_reference::value, + "get() cannot be used with reference types, you might want to use get_ref()"); + return get_impl(detail::priority_tag<4> {}); } /*! @@ -19877,15 +19867,73 @@ class basic_json } /*! - @brief get a pointer value (explicit) - @copydoc get() + @brief get a value (explicit) + + Explicit type conversion between the JSON value and a compatible value. + The value is filled into the input parameter by calling the @ref json_serializer + `from_json()` method. + + The function is equivalent to executing + @code {.cpp} + ValueType v; + JSONSerializer::from_json(*this, v); + @endcode + + This overloads is chosen if: + - @a ValueType is not @ref basic_json, + - @ref json_serializer has a `from_json()` method of the form + `void from_json(const basic_json&, ValueType&)`, and + + @tparam ValueType the input parameter type. + + @return the input parameter, allowing chaining calls. + + @throw what @ref json_serializer `from_json()` method throws + + @liveexample{The example below shows several conversions from JSON values + to other types. There a few things to note: (1) Floating-point numbers can + be converted to integers\, (2) A JSON array can be converted to a standard + `std::vector`\, (3) A JSON object can be converted to C++ + associative containers such as `std::unordered_map`.,get_to} + + @since version 3.3.0 */ - template::value, int>::type = 0> - constexpr auto get() const noexcept -> decltype(std::declval().template get_ptr()) + template < typename ValueType, + detail::enable_if_t < + !detail::is_basic_json::value&& + detail::has_from_json::value, + int > = 0 > + ValueType & get_to(ValueType& v) const noexcept(noexcept( + JSONSerializer::from_json(std::declval(), v))) { - // delegate the call to get_ptr - return get_ptr(); + JSONSerializer::from_json(*this, v); + return v; + } + + // specialization to allow to call get_to with a basic_json value + // see https://github.com/nlohmann/json/issues/2175 + template::value, + int> = 0> + ValueType & get_to(ValueType& v) const + { + v = *this; + return v; + } + + template < + typename T, std::size_t N, + typename Array = T (&)[N], + detail::enable_if_t < + detail::has_from_json::value, int > = 0 > + Array get_to(T (&v)[N]) const + noexcept(noexcept(JSONSerializer::from_json( + std::declval(), v))) + { + JSONSerializer::from_json(*this, v); + return v; } /*! From fbf6df63d9e16171abdcf47b6d699a1b19aaf1f9 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Wed, 13 Jan 2021 00:12:51 +0100 Subject: [PATCH 032/143] Enable member function calls in trailing return decltype expressions for older compilers. --- include/nlohmann/json.hpp | 27 +++++++++++++++------------ single_include/nlohmann/json.hpp | 27 +++++++++++++++------------ 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 500acb36ec..a7adb724fd 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -2908,7 +2908,6 @@ class basic_json - @ref json_serializer does not have a `from_json()` method of the form `ValueType from_json(const basic_json&)` - @tparam ValueTypeCV the provided value type @tparam ValueType the returned value type @return copy of the JSON value, converted to @a ValueType @@ -2924,9 +2923,9 @@ class basic_json @since version 2.1.0 */ - template < typename ValueTypeCV, typename ValueType = detail::uncvref_t, + template < typename ValueType, detail::enable_if_t < - detail::is_default_constructible::value && + detail::is_default_constructible::value&& detail::has_from_json::value, int > = 0 > ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept( @@ -2948,7 +2947,7 @@ class basic_json The function is equivalent to executing @code {.cpp} - return JSONSerializer::from_json(*this); + return JSONSerializer::from_json(*this); @endcode This overloads is chosen if: @@ -2959,7 +2958,6 @@ class basic_json @note If @ref json_serializer has both overloads of `from_json()`, this one is chosen. - @tparam ValueTypeCV the provided value type @tparam ValueType the returned value type @return copy of the JSON value, converted to @a ValueType @@ -2968,7 +2966,7 @@ class basic_json @since version 2.1.0 */ - template < typename ValueTypeCV, typename ValueType = detail::uncvref_t, + template < typename ValueType, detail::enable_if_t < detail::has_non_default_from_json::value, int > = 0 > @@ -2993,7 +2991,8 @@ class basic_json @since version 3.2.0 */ - template < typename BasicJsonType, detail::enable_if_t < + template < typename BasicJsonType, + detail::enable_if_t < detail::is_basic_json::value, int > = 0 > BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const @@ -3015,7 +3014,8 @@ class basic_json @since version 2.1.0 */ - template::value, int> = 0> basic_json get_impl(detail::priority_tag<3> /*unused*/) const @@ -3027,8 +3027,10 @@ class basic_json @brief get a pointer value (explicit) @copydoc get() */ - template::value, int> = 0> + template::value, + int> = 0> constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept -> decltype(std::declval().template get_ptr()) { @@ -3061,8 +3063,9 @@ class basic_json @since version 2.1.0 */ template < typename ValueTypeCV, typename ValueType = detail::uncvref_t> - constexpr auto get() const noexcept(noexcept(get_impl(detail::priority_tag<4> {}))) - -> decltype(get_impl(detail::priority_tag<4> {})) + constexpr auto get() const noexcept( + noexcept(std::declval().template get_impl(detail::priority_tag<4> {}))) + -> decltype(std::declval().template get_impl(detail::priority_tag<4> {})) { // we cannot static_assert on ValueTypeCV being non-const, because // there is support for get(), which is why we diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 004850c3aa..fd01826b4f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -19667,7 +19667,6 @@ class basic_json - @ref json_serializer does not have a `from_json()` method of the form `ValueType from_json(const basic_json&)` - @tparam ValueTypeCV the provided value type @tparam ValueType the returned value type @return copy of the JSON value, converted to @a ValueType @@ -19683,9 +19682,9 @@ class basic_json @since version 2.1.0 */ - template < typename ValueTypeCV, typename ValueType = detail::uncvref_t, + template < typename ValueType, detail::enable_if_t < - detail::is_default_constructible::value && + detail::is_default_constructible::value&& detail::has_from_json::value, int > = 0 > ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept( @@ -19707,7 +19706,7 @@ class basic_json The function is equivalent to executing @code {.cpp} - return JSONSerializer::from_json(*this); + return JSONSerializer::from_json(*this); @endcode This overloads is chosen if: @@ -19718,7 +19717,6 @@ class basic_json @note If @ref json_serializer has both overloads of `from_json()`, this one is chosen. - @tparam ValueTypeCV the provided value type @tparam ValueType the returned value type @return copy of the JSON value, converted to @a ValueType @@ -19727,7 +19725,7 @@ class basic_json @since version 2.1.0 */ - template < typename ValueTypeCV, typename ValueType = detail::uncvref_t, + template < typename ValueType, detail::enable_if_t < detail::has_non_default_from_json::value, int > = 0 > @@ -19752,7 +19750,8 @@ class basic_json @since version 3.2.0 */ - template < typename BasicJsonType, detail::enable_if_t < + template < typename BasicJsonType, + detail::enable_if_t < detail::is_basic_json::value, int > = 0 > BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const @@ -19774,7 +19773,8 @@ class basic_json @since version 2.1.0 */ - template::value, int> = 0> basic_json get_impl(detail::priority_tag<3> /*unused*/) const @@ -19786,8 +19786,10 @@ class basic_json @brief get a pointer value (explicit) @copydoc get() */ - template::value, int> = 0> + template::value, + int> = 0> constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept -> decltype(std::declval().template get_ptr()) { @@ -19820,8 +19822,9 @@ class basic_json @since version 2.1.0 */ template < typename ValueTypeCV, typename ValueType = detail::uncvref_t> - constexpr auto get() const noexcept(noexcept(get_impl(detail::priority_tag<4> {}))) - -> decltype(get_impl(detail::priority_tag<4> {})) + constexpr auto get() const noexcept( + noexcept(std::declval().template get_impl(detail::priority_tag<4> {}))) + -> decltype(std::declval().template get_impl(detail::priority_tag<4> {})) { // we cannot static_assert on ValueTypeCV being non-const, because // there is support for get(), which is why we From d7c0f157c5bab277fb65c2412cb9cf75e0f920a0 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Wed, 13 Jan 2021 20:47:03 +0100 Subject: [PATCH 033/143] Merged from_json for pair and tuple to try to fix C2995 error in old MSVC versions. --- .../nlohmann/detail/conversions/from_json.hpp | 49 ++++++++----------- single_include/nlohmann/json.hpp | 49 ++++++++----------- 2 files changed, 42 insertions(+), 56 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index fcb6a5e60e..9523943cab 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -188,7 +188,10 @@ auto from_json_array_impl(const BasicJsonType& j, std::array& arr, } } -template +template::value, + int> = 0> auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/) -> decltype( arr.reserve(std::declval()), @@ -209,7 +212,10 @@ auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, p arr = std::move(ret); } -template +template::value, + int> = 0> void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<0> /*unused*/) { @@ -344,53 +350,40 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } } +template +std::tuple from_json_tuple_impl_base(BasicJsonType&& j, index_sequence /*unused*/) +{ + return std::make_tuple(std::forward(j).at(Idx).template get()...); +} + template < typename BasicJsonType, class A1, class A2 > -std::pair from_json_pair_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) +std::pair from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) { return {std::forward(j).at(0).template get(), std::forward(j).at(1).template get()}; } template -void from_json_pair_impl(BasicJsonType&& j, std::pair& p, priority_tag<1> /*unused*/) +void from_json_tuple_impl(BasicJsonType&& j, std::pair& p, priority_tag<1> /*unused*/) { - p = from_json_pair_impl(std::forward(j), identity_tag> {}, priority_tag<0> {}); -} - -template -auto from_json(BasicJsonType&& j, PairRelatedType&& p) --> decltype(from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {})) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); - } - - return from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {}); -} - -template -std::tuple from_json_tuple_impl_base(BasicJsonType&& j, index_sequence /*unused*/) -{ - return std::make_tuple(std::forward(j).at(Idx).template get()...); + p = from_json_tuple_impl(std::forward(j), identity_tag> {}, priority_tag<0> {}); } template -std::tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) +std::tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<2> /*unused*/) { return from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); } template -void from_json_tuple_impl(BasicJsonType&& j, std::tuple& t, priority_tag<1> /*unused*/) +void from_json_tuple_impl(BasicJsonType&& j, std::tuple& t, priority_tag<3> /*unused*/) { t = from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); } template auto from_json(BasicJsonType&& j, TupleRelated&& t) --> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<1> {})) +-> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<3> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -398,7 +391,7 @@ auto from_json(BasicJsonType&& j, TupleRelated&& t) std::string(j.type_name()))); } - return from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<1> {}); + return from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<3> {}); } template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index fd01826b4f..f7ee9030b8 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3724,7 +3724,10 @@ auto from_json_array_impl(const BasicJsonType& j, std::array& arr, } } -template +template::value, + int> = 0> auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/) -> decltype( arr.reserve(std::declval()), @@ -3745,7 +3748,10 @@ auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, p arr = std::move(ret); } -template +template::value, + int> = 0> void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<0> /*unused*/) { @@ -3880,53 +3886,40 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } } +template +std::tuple from_json_tuple_impl_base(BasicJsonType&& j, index_sequence /*unused*/) +{ + return std::make_tuple(std::forward(j).at(Idx).template get()...); +} + template < typename BasicJsonType, class A1, class A2 > -std::pair from_json_pair_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) +std::pair from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) { return {std::forward(j).at(0).template get(), std::forward(j).at(1).template get()}; } template -void from_json_pair_impl(BasicJsonType&& j, std::pair& p, priority_tag<1> /*unused*/) -{ - p = from_json_pair_impl(std::forward(j), identity_tag> {}, priority_tag<0> {}); -} - -template -auto from_json(BasicJsonType&& j, PairRelatedType&& p) --> decltype(from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {})) +void from_json_tuple_impl(BasicJsonType&& j, std::pair& p, priority_tag<1> /*unused*/) { - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); - } - - return from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {}); -} - -template -std::tuple from_json_tuple_impl_base(BasicJsonType&& j, index_sequence /*unused*/) -{ - return std::make_tuple(std::forward(j).at(Idx).template get()...); + p = from_json_tuple_impl(std::forward(j), identity_tag> {}, priority_tag<0> {}); } template -std::tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) +std::tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<2> /*unused*/) { return from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); } template -void from_json_tuple_impl(BasicJsonType&& j, std::tuple& t, priority_tag<1> /*unused*/) +void from_json_tuple_impl(BasicJsonType&& j, std::tuple& t, priority_tag<3> /*unused*/) { t = from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); } template auto from_json(BasicJsonType&& j, TupleRelated&& t) --> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<1> {})) +-> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<3> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -3934,7 +3927,7 @@ auto from_json(BasicJsonType&& j, TupleRelated&& t) std::string(j.type_name()))); } - return from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<1> {}); + return from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<3> {}); } template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, From 6eb37e9b7881057b6f6fb646a46fd2c1ea4c79a1 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Thu, 14 Jan 2021 12:39:03 +0100 Subject: [PATCH 034/143] Only add conditional constexpr to get() for >= C++14 to work around errors on older compilers. --- include/nlohmann/json.hpp | 5 ++++- single_include/nlohmann/json.hpp | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index a7adb724fd..e5a7917c5e 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3063,7 +3063,10 @@ class basic_json @since version 2.1.0 */ template < typename ValueTypeCV, typename ValueType = detail::uncvref_t> - constexpr auto get() const noexcept( +#if defined(JSON_HAS_CPP_14) + constexpr +#endif + auto get() const noexcept( noexcept(std::declval().template get_impl(detail::priority_tag<4> {}))) -> decltype(std::declval().template get_impl(detail::priority_tag<4> {})) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f7ee9030b8..7ce3138295 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -19815,7 +19815,10 @@ class basic_json @since version 2.1.0 */ template < typename ValueTypeCV, typename ValueType = detail::uncvref_t> - constexpr auto get() const noexcept( +#if defined(JSON_HAS_CPP_14) + constexpr +#endif + auto get() const noexcept( noexcept(std::declval().template get_impl(detail::priority_tag<4> {}))) -> decltype(std::declval().template get_impl(detail::priority_tag<4> {})) { From b9d3aa40670a57cedeb71e52c192a7678f0e23c2 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 14 Jan 2021 21:55:49 +0100 Subject: [PATCH 035/143] :recycle: split set_parent function --- .../nlohmann/detail/conversions/to_json.hpp | 14 +-- include/nlohmann/detail/input/json_sax.hpp | 8 +- include/nlohmann/json.hpp | 84 +++++++------- single_include/nlohmann/json.hpp | 106 +++++++++--------- 4 files changed, 104 insertions(+), 108 deletions(-) diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index eeb7865231..f42735e834 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -132,7 +132,7 @@ struct external_constructor { j.m_type = value_t::array; j.m_value = arr; - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } @@ -141,7 +141,7 @@ struct external_constructor { j.m_type = value_t::array; j.m_value = std::move(arr); - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } @@ -154,7 +154,7 @@ struct external_constructor using std::end; j.m_type = value_t::array; j.m_value.array = j.template create(begin(arr), end(arr)); - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } @@ -185,7 +185,7 @@ struct external_constructor { std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin()); } - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } }; @@ -198,7 +198,7 @@ struct external_constructor { j.m_type = value_t::object; j.m_value = obj; - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } @@ -207,7 +207,7 @@ struct external_constructor { j.m_type = value_t::object; j.m_value = std::move(obj); - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } @@ -220,7 +220,7 @@ struct external_constructor j.m_type = value_t::object; j.m_value.object = j.template create(begin(obj), end(obj)); - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } }; diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 3088cbbcd8..4bd64c80e3 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -236,7 +236,7 @@ class json_sax_dom_parser bool end_object() { - ref_stack.back()->set_parent(*ref_stack.back(), true); + ref_stack.back()->set_parents(); ref_stack.pop_back(); return true; } @@ -255,7 +255,7 @@ class json_sax_dom_parser bool end_array() { - ref_stack.back()->set_parent(*ref_stack.back(), true); + ref_stack.back()->set_parents(); ref_stack.pop_back(); return true; } @@ -437,7 +437,7 @@ class json_sax_dom_callback_parser } else { - ref_stack.back()->set_parent(*ref_stack.back(), true); + ref_stack.back()->set_parents(); } } @@ -488,7 +488,7 @@ class json_sax_dom_callback_parser keep = callback(static_cast(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back()); if (keep) { - ref_stack.back()->set_parent(*ref_stack.back(), true); + ref_stack.back()->set_parents(); } else { diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 10fff0baeb..6eba29e95f 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1258,44 +1258,42 @@ class basic_json #endif } - reference set_parent(reference j, bool recursive) + void set_parents() { #if JSON_DIAGNOSTICS - if (recursive) + switch (m_type) { - switch (m_type) + case value_t::array: { - case value_t::array: + for (auto& element : *m_value.array) { - for (auto& element : *m_value.array) - { - element.m_parent = this; - } - break; + element.m_parent = this; } + break; + } - case value_t::object: + case value_t::object: + { + for (auto& element : *m_value.object) { - for (auto& element : *m_value.object) - { - element.second.m_parent = this; - } - break; + element.second.m_parent = this; } - - default: - break; + break; } + + default: + break; } - else - { - j.m_parent = this; - } +#endif + } + + reference set_parent(reference j) + { +#if JSON_DIAGNOSTICS + j.m_parent = this; #else static_cast(j); - static_cast(recursive); #endif - return j; } @@ -1513,7 +1511,7 @@ class basic_json std::forward(val)))) { JSONSerializer::to_json(*this, std::forward(val)); - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -1592,7 +1590,7 @@ class basic_json default: // LCOV_EXCL_LINE JSON_ASSERT(false); // LCOV_EXCL_LINE } - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -1719,7 +1717,7 @@ class basic_json m_value.array = create(init.begin(), init.end()); } - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -1929,7 +1927,7 @@ class basic_json : m_type(value_t::array) { m_value.array = create(cnt, val); - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -2082,7 +2080,7 @@ class basic_json JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), diagnostics_t())); } - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -2181,7 +2179,7 @@ class basic_json break; } - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -2222,7 +2220,7 @@ class basic_json other.m_type = value_t::null; other.m_value = {}; - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -2263,7 +2261,7 @@ class basic_json swap(m_type, other.m_type); swap(m_value, other.m_value); - set_parent(*this, true); + set_parents(); assert_invariant(); return *this; } @@ -3388,7 +3386,7 @@ class basic_json { JSON_TRY { - return set_parent(m_value.array->at(idx), false); + return set_parent(m_value.array->at(idx)); } JSON_CATCH (std::out_of_range&) { @@ -3486,7 +3484,7 @@ class basic_json { JSON_TRY { - return set_parent(m_value.object->at(key), false); + return set_parent(m_value.object->at(key)); } JSON_CATCH (std::out_of_range&) { @@ -3684,7 +3682,7 @@ class basic_json // operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { - return set_parent(m_value.object->operator[](key), false); + return set_parent(m_value.object->operator[](key)); } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); @@ -3774,7 +3772,7 @@ class basic_json // at only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { - return set_parent(m_value.object->operator[](key), false); + return set_parent(m_value.object->operator[](key)); } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); @@ -5330,7 +5328,7 @@ class basic_json // add element to array (move semantics) m_value.array->push_back(std::move(val)); - set_parent(m_value.array->back(), false); + set_parent(m_value.array->back()); // if val is moved from, basic_json move constructor marks it null so we do not call the destructor } @@ -5366,7 +5364,7 @@ class basic_json // add element to array m_value.array->push_back(val); - set_parent(m_value.array->back(), false); + set_parent(m_value.array->back()); } /*! @@ -5417,7 +5415,7 @@ class basic_json // add element to object auto res = m_value.object->insert(val); - set_parent(res.first->second, false); + set_parent(res.first->second); } /*! @@ -5521,10 +5519,10 @@ class basic_json // add element to array (perfect forwarding) #ifdef JSON_HAS_CPP_17 - return set_parent(m_value.array->emplace_back(std::forward(args)...), false); + return set_parent(m_value.array->emplace_back(std::forward(args)...)); #else m_value.array->emplace_back(std::forward(args)...); - return set_parent(m_value.array->back(), false); + return set_parent(m_value.array->back()); #endif } @@ -5574,7 +5572,7 @@ class basic_json // add element to array (perfect forwarding) auto res = m_value.object->emplace(std::forward(args)...); - set_parent(res.first->second, false); + set_parent(res.first->second); // create result iterator and set iterator to the result of emplace auto it = begin(); @@ -6007,7 +6005,7 @@ class basic_json std::swap(m_type, other.m_type); std::swap(m_value, other.m_value); - set_parent(*this, true); + set_parents(); assert_invariant(); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2d98dce466..98b26f039f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -4302,7 +4302,7 @@ struct external_constructor { j.m_type = value_t::array; j.m_value = arr; - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } @@ -4311,7 +4311,7 @@ struct external_constructor { j.m_type = value_t::array; j.m_value = std::move(arr); - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } @@ -4324,7 +4324,7 @@ struct external_constructor using std::end; j.m_type = value_t::array; j.m_value.array = j.template create(begin(arr), end(arr)); - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } @@ -4355,7 +4355,7 @@ struct external_constructor { std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin()); } - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } }; @@ -4368,7 +4368,7 @@ struct external_constructor { j.m_type = value_t::object; j.m_value = obj; - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } @@ -4377,7 +4377,7 @@ struct external_constructor { j.m_type = value_t::object; j.m_value = std::move(obj); - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } @@ -4390,7 +4390,7 @@ struct external_constructor j.m_type = value_t::object; j.m_value.object = j.template create(begin(obj), end(obj)); - j.set_parent(j, true); + j.set_parents(); j.assert_invariant(); } }; @@ -5638,7 +5638,7 @@ class json_sax_dom_parser bool end_object() { - ref_stack.back()->set_parent(*ref_stack.back(), true); + ref_stack.back()->set_parents(); ref_stack.pop_back(); return true; } @@ -5657,7 +5657,7 @@ class json_sax_dom_parser bool end_array() { - ref_stack.back()->set_parent(*ref_stack.back(), true); + ref_stack.back()->set_parents(); ref_stack.pop_back(); return true; } @@ -5839,7 +5839,7 @@ class json_sax_dom_callback_parser } else { - ref_stack.back()->set_parent(*ref_stack.back(), true); + ref_stack.back()->set_parents(); } } @@ -5890,7 +5890,7 @@ class json_sax_dom_callback_parser keep = callback(static_cast(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back()); if (keep) { - ref_stack.back()->set_parent(*ref_stack.back(), true); + ref_stack.back()->set_parents(); } else { @@ -18015,44 +18015,42 @@ class basic_json #endif } - reference set_parent(reference j, bool recursive) + void set_parents() { #if JSON_DIAGNOSTICS - if (recursive) + switch (m_type) { - switch (m_type) + case value_t::array: { - case value_t::array: + for (auto& element : *m_value.array) { - for (auto& element : *m_value.array) - { - element.m_parent = this; - } - break; + element.m_parent = this; } + break; + } - case value_t::object: + case value_t::object: + { + for (auto& element : *m_value.object) { - for (auto& element : *m_value.object) - { - element.second.m_parent = this; - } - break; + element.second.m_parent = this; } - - default: - break; + break; } + + default: + break; } - else - { - j.m_parent = this; - } +#endif + } + + reference set_parent(reference j) + { +#if JSON_DIAGNOSTICS + j.m_parent = this; #else static_cast(j); - static_cast(recursive); #endif - return j; } @@ -18270,7 +18268,7 @@ class basic_json std::forward(val)))) { JSONSerializer::to_json(*this, std::forward(val)); - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -18349,7 +18347,7 @@ class basic_json default: // LCOV_EXCL_LINE JSON_ASSERT(false); // LCOV_EXCL_LINE } - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -18476,7 +18474,7 @@ class basic_json m_value.array = create(init.begin(), init.end()); } - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -18686,7 +18684,7 @@ class basic_json : m_type(value_t::array) { m_value.array = create(cnt, val); - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -18839,7 +18837,7 @@ class basic_json JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), diagnostics_t())); } - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -18938,7 +18936,7 @@ class basic_json break; } - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -18979,7 +18977,7 @@ class basic_json other.m_type = value_t::null; other.m_value = {}; - set_parent(*this, true); + set_parents(); assert_invariant(); } @@ -19020,7 +19018,7 @@ class basic_json swap(m_type, other.m_type); swap(m_value, other.m_value); - set_parent(*this, true); + set_parents(); assert_invariant(); return *this; } @@ -20145,7 +20143,7 @@ class basic_json { JSON_TRY { - return set_parent(m_value.array->at(idx), false); + return set_parent(m_value.array->at(idx)); } JSON_CATCH (std::out_of_range&) { @@ -20243,7 +20241,7 @@ class basic_json { JSON_TRY { - return set_parent(m_value.object->at(key), false); + return set_parent(m_value.object->at(key)); } JSON_CATCH (std::out_of_range&) { @@ -20441,7 +20439,7 @@ class basic_json // operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { - return set_parent(m_value.object->operator[](key), false); + return set_parent(m_value.object->operator[](key)); } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); @@ -20531,7 +20529,7 @@ class basic_json // at only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { - return set_parent(m_value.object->operator[](key), false); + return set_parent(m_value.object->operator[](key)); } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); @@ -22087,7 +22085,7 @@ class basic_json // add element to array (move semantics) m_value.array->push_back(std::move(val)); - set_parent(m_value.array->back(), false); + set_parent(m_value.array->back()); // if val is moved from, basic_json move constructor marks it null so we do not call the destructor } @@ -22123,7 +22121,7 @@ class basic_json // add element to array m_value.array->push_back(val); - set_parent(m_value.array->back(), false); + set_parent(m_value.array->back()); } /*! @@ -22174,7 +22172,7 @@ class basic_json // add element to object auto res = m_value.object->insert(val); - set_parent(res.first->second, false); + set_parent(res.first->second); } /*! @@ -22278,10 +22276,10 @@ class basic_json // add element to array (perfect forwarding) #ifdef JSON_HAS_CPP_17 - return set_parent(m_value.array->emplace_back(std::forward(args)...), false); + return set_parent(m_value.array->emplace_back(std::forward(args)...)); #else m_value.array->emplace_back(std::forward(args)...); - return set_parent(m_value.array->back(), false); + return set_parent(m_value.array->back()); #endif } @@ -22331,7 +22329,7 @@ class basic_json // add element to array (perfect forwarding) auto res = m_value.object->emplace(std::forward(args)...); - set_parent(res.first->second, false); + set_parent(res.first->second); // create result iterator and set iterator to the result of emplace auto it = begin(); @@ -22764,7 +22762,7 @@ class basic_json std::swap(m_type, other.m_type); std::swap(m_value, other.m_value); - set_parent(*this, true); + set_parents(); assert_invariant(); } From a77621687a826bfb020a3ceeda656f7b45479b44 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 14 Jan 2021 21:56:19 +0100 Subject: [PATCH 036/143] :memo: fix comment --- include/nlohmann/json.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 6eba29e95f..9bbb83b9a4 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1239,7 +1239,7 @@ class basic_json container's elements must have the current value as parent. @param[in] check_parents whether the parent relation should be checked. - The value is true by default and should only be set to true + The value is true by default and should only be set to false during destruction of objects when the invariant does not need to hold. */ diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 98b26f039f..78375c5c0c 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -17996,7 +17996,7 @@ class basic_json container's elements must have the current value as parent. @param[in] check_parents whether the parent relation should be checked. - The value is true by default and should only be set to true + The value is true by default and should only be set to false during destruction of objects when the invariant does not need to hold. */ From 0d1fb383b7f4572a154d7a26a1318e2c617b4de8 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 14 Jan 2021 22:05:08 +0100 Subject: [PATCH 037/143] :ok_hand: address comment --- include/nlohmann/detail/conversions/to_json.hpp | 4 +--- single_include/nlohmann/json.hpp | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index f42735e834..228e81879e 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -167,9 +167,7 @@ struct external_constructor for (const bool x : arr) { j.m_value.array->push_back(x); -#if JSON_DIAGNOSTICS - j.m_value.array->back().m_parent = &j; -#endif + j.set_parent(j.m_value.array->back()); } j.assert_invariant(); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 78375c5c0c..4ed9994e22 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -4337,9 +4337,7 @@ struct external_constructor for (const bool x : arr) { j.m_value.array->push_back(x); -#if JSON_DIAGNOSTICS - j.m_value.array->back().m_parent = &j; -#endif + j.set_parent(j.m_value.array->back()); } j.assert_invariant(); } From f8037660d06a0fbb6b1cde944d436032d04564ae Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 14 Jan 2021 22:43:52 +0100 Subject: [PATCH 038/143] :recycle: add iterator set_parent function --- include/nlohmann/json.hpp | 57 ++++++++++---------------------- single_include/nlohmann/json.hpp | 57 ++++++++++---------------------- 2 files changed, 36 insertions(+), 78 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 9bbb83b9a4..81587ef263 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1287,6 +1287,19 @@ class basic_json #endif } + iterator set_parents(iterator it, typename iterator::difference_type count) + { +#if JSON_DIAGNOSTICS + for (typename iterator::difference_type i = 0; i < count; ++i) + { + (it + i)->m_parent = this; + } +#else + static_cast(count); +#endif + return it; + } + reference set_parent(reference j) { #if JSON_DIAGNOSTICS @@ -3601,7 +3614,7 @@ class basic_json // set parent for values added above for (auto i = previous_size; i <= idx; ++i) { - m_value.array->operator[](i).m_parent = this; + set_parent(m_value.array->operator[](i)); } #endif } @@ -5636,13 +5649,7 @@ class basic_json } // insert to array and return iterator -#if JSON_DIAGNOSTICS - iterator result = insert_iterator(pos, val); - result->m_parent = this; - return result; -#else - return insert_iterator(pos, val); -#endif + return set_parents(insert_iterator(pos, val), static_cast(1)); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); @@ -5693,16 +5700,7 @@ class basic_json } // insert to array and return iterator -#if JSON_DIAGNOSTICS - iterator result = insert_iterator(pos, cnt, val); - for (size_type i = 0; i < cnt; ++i) - { - (result + static_cast(i))->m_parent = this; - } - return result; -#else - return insert_iterator(pos, cnt, val); -#endif + return set_parents(insert_iterator(pos, cnt, val), static_cast(cnt)); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); @@ -5764,16 +5762,7 @@ class basic_json } // insert to array and return iterator -#if JSON_DIAGNOSTICS - iterator result = insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); - for (typename iterator::difference_type i = 0; i < std::distance(first, last); ++i) - { - (result + i)->m_parent = this; - } - return result; -#else - return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); -#endif + return set_parents(insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator), std::distance(first, last)); } /*! @@ -5815,17 +5804,7 @@ class basic_json } // insert to array and return iterator -#if JSON_DIAGNOSTICS - const auto size = ilist.size(); - iterator result = insert_iterator(pos, ilist.begin(), ilist.end()); - for (std::size_t i = 0; i < size; ++i) - { - (result + static_cast(i))->m_parent = this; - } - return result; -#else - return insert_iterator(pos, ilist.begin(), ilist.end()); -#endif + return set_parents(insert_iterator(pos, ilist.begin(), ilist.end()), static_cast(ilist.size())); } /*! diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 4ed9994e22..f388b0d02e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18042,6 +18042,19 @@ class basic_json #endif } + iterator set_parents(iterator it, typename iterator::difference_type count) + { +#if JSON_DIAGNOSTICS + for (typename iterator::difference_type i = 0; i < count; ++i) + { + (it + i)->m_parent = this; + } +#else + static_cast(count); +#endif + return it; + } + reference set_parent(reference j) { #if JSON_DIAGNOSTICS @@ -20356,7 +20369,7 @@ class basic_json // set parent for values added above for (auto i = previous_size; i <= idx; ++i) { - m_value.array->operator[](i).m_parent = this; + set_parent(m_value.array->operator[](i)); } #endif } @@ -22391,13 +22404,7 @@ class basic_json } // insert to array and return iterator -#if JSON_DIAGNOSTICS - iterator result = insert_iterator(pos, val); - result->m_parent = this; - return result; -#else - return insert_iterator(pos, val); -#endif + return set_parents(insert_iterator(pos, val), static_cast(1)); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); @@ -22448,16 +22455,7 @@ class basic_json } // insert to array and return iterator -#if JSON_DIAGNOSTICS - iterator result = insert_iterator(pos, cnt, val); - for (size_type i = 0; i < cnt; ++i) - { - (result + static_cast(i))->m_parent = this; - } - return result; -#else - return insert_iterator(pos, cnt, val); -#endif + return set_parents(insert_iterator(pos, cnt, val), static_cast(cnt)); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); @@ -22519,16 +22517,7 @@ class basic_json } // insert to array and return iterator -#if JSON_DIAGNOSTICS - iterator result = insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); - for (typename iterator::difference_type i = 0; i < std::distance(first, last); ++i) - { - (result + i)->m_parent = this; - } - return result; -#else - return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); -#endif + return set_parents(insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator), std::distance(first, last)); } /*! @@ -22570,17 +22559,7 @@ class basic_json } // insert to array and return iterator -#if JSON_DIAGNOSTICS - const auto size = ilist.size(); - iterator result = insert_iterator(pos, ilist.begin(), ilist.end()); - for (std::size_t i = 0; i < size; ++i) - { - (result + static_cast(i))->m_parent = this; - } - return result; -#else - return insert_iterator(pos, ilist.begin(), ilist.end()); -#endif + return set_parents(insert_iterator(pos, ilist.begin(), ilist.end()), static_cast(ilist.size())); } /*! From b0d8628c498d1a2be3391a1949886843746b9878 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 15 Jan 2021 16:54:00 +0100 Subject: [PATCH 039/143] :ok_hand: address comments --- include/nlohmann/json.hpp | 9 ++------- single_include/nlohmann/json.hpp | 9 ++------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 81587ef263..16bf6af005 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3607,16 +3607,10 @@ class basic_json // remember array size before resizing const auto previous_size = m_value.array->size(); #endif - m_value.array->resize(idx + 1); -#if JSON_DIAGNOSTICS // set parent for values added above - for (auto i = previous_size; i <= idx; ++i) - { - set_parent(m_value.array->operator[](i)); - } -#endif + set_parents(begin() + previous_size, idx + 1 - previous_size); } return m_value.array->operator[](idx); @@ -5985,6 +5979,7 @@ class basic_json std::swap(m_value, other.m_value); set_parents(); + other.set_parents(); assert_invariant(); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f388b0d02e..7617b8edaa 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20362,16 +20362,10 @@ class basic_json // remember array size before resizing const auto previous_size = m_value.array->size(); #endif - m_value.array->resize(idx + 1); -#if JSON_DIAGNOSTICS // set parent for values added above - for (auto i = previous_size; i <= idx; ++i) - { - set_parent(m_value.array->operator[](i)); - } -#endif + set_parents(begin() + previous_size, idx + 1 - previous_size); } return m_value.array->operator[](idx); @@ -22740,6 +22734,7 @@ class basic_json std::swap(m_value, other.m_value); set_parents(); + other.set_parents(); assert_invariant(); } From 7633a21e6c1f2330743a8dc3a824e3144be76e1d Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 15 Jan 2021 16:58:05 +0100 Subject: [PATCH 040/143] :green_heart: fix build --- include/nlohmann/json.hpp | 2 ++ single_include/nlohmann/json.hpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 16bf6af005..af54f1b163 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3609,8 +3609,10 @@ class basic_json #endif m_value.array->resize(idx + 1); +#if JSON_DIAGNOSTICS // set parent for values added above set_parents(begin() + previous_size, idx + 1 - previous_size); +#endif } return m_value.array->operator[](idx); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 7617b8edaa..ab3d857c27 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20364,8 +20364,10 @@ class basic_json #endif m_value.array->resize(idx + 1); +#if JSON_DIAGNOSTICS // set parent for values added above set_parents(begin() + previous_size, idx + 1 - previous_size); +#endif } return m_value.array->operator[](idx); From e9d641130d8e50793c763f1dee978fd8dbdb2708 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 16 Jan 2021 15:33:05 +0100 Subject: [PATCH 041/143] :bug: proper JSON Pointer escape in diagnostic messages --- include/nlohmann/detail/diagnostics_t.hpp | 3 +- include/nlohmann/detail/json_pointer.hpp | 47 +-- include/nlohmann/detail/string_escape.hpp | 63 ++++ include/nlohmann/json.hpp | 4 +- single_include/nlohmann/json.hpp | 334 ++++++++++++---------- 5 files changed, 252 insertions(+), 199 deletions(-) create mode 100644 include/nlohmann/detail/string_escape.hpp diff --git a/include/nlohmann/detail/diagnostics_t.hpp b/include/nlohmann/detail/diagnostics_t.hpp index 727e82350f..a2f7f23a06 100644 --- a/include/nlohmann/detail/diagnostics_t.hpp +++ b/include/nlohmann/detail/diagnostics_t.hpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace nlohmann { @@ -70,7 +71,7 @@ class diagnostics_t return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, [](const std::string & a, const std::string & b) { - return a + "/" + b; + return a + "/" + detail::escape(b); }) + ") "; #else return ""; diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index f3a70bdcee..52cf171345 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include namespace nlohmann @@ -70,7 +71,7 @@ class json_pointer std::string{}, [](const std::string & a, const std::string & b) { - return a + "/" + escape(b); + return a + "/" + detail::escape(b); }); } @@ -791,53 +792,13 @@ class json_pointer } // finally, store the reference token - unescape(reference_token); + detail::unescape(reference_token); result.push_back(reference_token); } return result; } - /*! - @brief replace all occurrences of a substring by another string - - @param[in,out] s the string to manipulate; changed so that all - occurrences of @a f are replaced with @a t - @param[in] f the substring to replace with @a t - @param[in] t the string to replace @a f - - @pre The search string @a f must not be empty. **This precondition is - enforced with an assertion.** - - @since version 2.0.0 - */ - static void replace_substring(std::string& s, const std::string& f, - const std::string& t) - { - JSON_ASSERT(!f.empty()); - for (auto pos = s.find(f); // find first occurrence of f - pos != std::string::npos; // make sure f was found - s.replace(pos, f.size(), t), // replace with t, and - pos = s.find(f, pos + t.size())) // find next occurrence of f - {} - } - - JSON_PRIVATE_UNLESS_TESTED: - /// escape "~" to "~0" and "/" to "~1" - static std::string escape(std::string s) - { - replace_substring(s, "~", "~0"); - replace_substring(s, "/", "~1"); - return s; - } - - /// unescape "~1" to tilde and "~0" to slash (order is important!) - static void unescape(std::string& s) - { - replace_substring(s, "~1", "/"); - replace_substring(s, "~0", "~"); - } - private: /*! @param[in] reference_string the reference string to the current value @@ -883,7 +844,7 @@ class json_pointer // iterate object and use keys as reference string for (const auto& element : *value.m_value.object) { - flatten(reference_string + "/" + escape(element.first), element.second, result); + flatten(reference_string + "/" + detail::escape(element.first), element.second, result); } } break; diff --git a/include/nlohmann/detail/string_escape.hpp b/include/nlohmann/detail/string_escape.hpp new file mode 100644 index 0000000000..84f7da52e0 --- /dev/null +++ b/include/nlohmann/detail/string_escape.hpp @@ -0,0 +1,63 @@ +#pragma once + +#include +#include + +namespace nlohmann +{ +namespace detail +{ + +/*! +@brief replace all occurrences of a substring by another string + +@param[in,out] s the string to manipulate; changed so that all + occurrences of @a f are replaced with @a t +@param[in] f the substring to replace with @a t +@param[in] t the string to replace @a f + +@pre The search string @a f must not be empty. **This precondition is +enforced with an assertion.** + +@since version 2.0.0 +*/ +inline void replace_substring(std::string& s, const std::string& f, + const std::string& t) +{ + JSON_ASSERT(!f.empty()); + for (auto pos = s.find(f); // find first occurrence of f + pos != std::string::npos; // make sure f was found + s.replace(pos, f.size(), t), // replace with t, and + pos = s.find(f, pos + t.size())) // find next occurrence of f + {} +} + +/*! + * @brief string escaping as described in RFC 6901 (Sect. 4) + * @param[in] s string to escape + * @return escaped string + * + * Note the order of escaping "~" to "~0" and "/" to "~1" is important. + */ +inline std::string escape(std::string s) +{ + replace_substring(s, "~", "~0"); + replace_substring(s, "/", "~1"); + return s; +} + +/*! + * @brief string unescaping as described in RFC 6901 (Sect. 4) + * @param[in] s string to unescape + * @return unescaped string + * + * Note the order of escaping "~1" to "/" and "~0" to "~" is important. + */ +static void unescape(std::string& s) +{ + replace_substring(s, "~1", "/"); + replace_substring(s, "~0", "~"); +} + +} // namespace detail +} // namespace nlohmann diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index af54f1b163..833d8d4d27 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -8651,7 +8651,7 @@ class basic_json for (auto it = source.cbegin(); it != source.cend(); ++it) { // escape the key name to be used in a JSON patch - const auto key = json_pointer::escape(it.key()); + const auto key = detail::escape(it.key()); if (target.find(it.key()) != target.end()) { @@ -8675,7 +8675,7 @@ class basic_json if (source.find(it.key()) == source.end()) { // found a key that is not in this -> add it - const auto key = json_pointer::escape(it.key()); + const auto key = detail::escape(it.key()); result.push_back( { {"op", "add"}, {"path", path + "/" + key}, diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ab3d857c27..5f23622f55 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -161,116 +161,10 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept } // namespace detail } // namespace nlohmann +// #include -namespace nlohmann -{ -namespace detail -{ - -template -class diagnostics_t -{ - public: - diagnostics_t() noexcept = default; - diagnostics_t(const BasicJsonType& j) noexcept - : m_j(&j) - {} - - std::string diagnostics() const - { -#if JSON_DIAGNOSTICS - if (m_j == nullptr) - { - return ""; - } - - std::vector tokens; - for (const auto* current = m_j; current->m_parent != nullptr; current = current->m_parent) - { - switch (current->m_parent->type()) - { - case value_t::array: - { - for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) - { - if (current->m_parent->m_value.array->operator[](i) == *current) - { - tokens.emplace_back(std::to_string(i)); - continue; - } - } - break; - } - - case value_t::object: - { - for (const auto& element : *current->m_parent->m_value.object) - { - if (element.second == *current) - { - tokens.emplace_back(element.first.c_str()); - continue; - } - } - break; - } - - default: - break; - } - } - - if (tokens.empty()) - { - return ""; - } - - return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, - [](const std::string & a, const std::string & b) - { - return a + "/" + b; - }) + ") "; -#else - return ""; -#endif - } - - private: - const BasicJsonType* m_j = static_cast(nullptr); -}; - -} // namespace detail -} // namespace nlohmann - -// #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 // #include @@ -2549,6 +2443,178 @@ JSON_HEDLEY_DIAGNOSTIC_POP #endif +namespace nlohmann +{ +namespace detail +{ + +/*! +@brief replace all occurrences of a substring by another string + +@param[in,out] s the string to manipulate; changed so that all + occurrences of @a f are replaced with @a t +@param[in] f the substring to replace with @a t +@param[in] t the string to replace @a f + +@pre The search string @a f must not be empty. **This precondition is +enforced with an assertion.** + +@since version 2.0.0 +*/ +inline void replace_substring(std::string& s, const std::string& f, + const std::string& t) +{ + JSON_ASSERT(!f.empty()); + for (auto pos = s.find(f); // find first occurrence of f + pos != std::string::npos; // make sure f was found + s.replace(pos, f.size(), t), // replace with t, and + pos = s.find(f, pos + t.size())) // find next occurrence of f + {} +} + +/*! + * @brief string escaping as described in RFC 6901 (Sect. 4) + * @param[in] s string to escape + * @return escaped string + * + * Note the order of escaping "~" to "~0" and "/" to "~1" is important. + */ +inline std::string escape(std::string s) +{ + replace_substring(s, "~", "~0"); + replace_substring(s, "/", "~1"); + return s; +} + +/*! + * @brief string unescaping as described in RFC 6901 (Sect. 4) + * @param[in] s string to unescape + * @return unescaped string + * + * Note the order of escaping "~1" to "/" and "~0" to "~" is important. + */ +static void unescape(std::string& s) +{ + replace_substring(s, "~1", "/"); + replace_substring(s, "~0", "~"); +} + +} // namespace detail +} // namespace nlohmann + + +namespace nlohmann +{ +namespace detail +{ + +template +class diagnostics_t +{ + public: + diagnostics_t() noexcept = default; + diagnostics_t(const BasicJsonType& j) noexcept + : m_j(&j) + {} + + std::string diagnostics() const + { +#if JSON_DIAGNOSTICS + if (m_j == nullptr) + { + return ""; + } + + std::vector tokens; + for (const auto* current = m_j; current->m_parent != nullptr; current = current->m_parent) + { + switch (current->m_parent->type()) + { + case value_t::array: + { + for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) + { + if (current->m_parent->m_value.array->operator[](i) == *current) + { + tokens.emplace_back(std::to_string(i)); + continue; + } + } + break; + } + + case value_t::object: + { + for (const auto& element : *current->m_parent->m_value.object) + { + if (element.second == *current) + { + tokens.emplace_back(element.first.c_str()); + continue; + } + } + break; + } + + default: + break; + } + } + + if (tokens.empty()) + { + return ""; + } + + return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, + [](const std::string & a, const std::string & b) + { + return a + "/" + detail::escape(b); + }) + ") "; +#else + return ""; +#endif + } + + private: + const BasicJsonType* m_j = static_cast(nullptr); +}; + +} // namespace detail +} // namespace nlohmann + +// #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 @@ -11798,6 +11864,8 @@ class json_reverse_iterator : public std::reverse_iterator // #include +// #include + // #include @@ -11858,7 +11926,7 @@ class json_pointer std::string{}, [](const std::string & a, const std::string & b) { - return a + "/" + escape(b); + return a + "/" + detail::escape(b); }); } @@ -12579,53 +12647,13 @@ class json_pointer } // finally, store the reference token - unescape(reference_token); + detail::unescape(reference_token); result.push_back(reference_token); } return result; } - /*! - @brief replace all occurrences of a substring by another string - - @param[in,out] s the string to manipulate; changed so that all - occurrences of @a f are replaced with @a t - @param[in] f the substring to replace with @a t - @param[in] t the string to replace @a f - - @pre The search string @a f must not be empty. **This precondition is - enforced with an assertion.** - - @since version 2.0.0 - */ - static void replace_substring(std::string& s, const std::string& f, - const std::string& t) - { - JSON_ASSERT(!f.empty()); - for (auto pos = s.find(f); // find first occurrence of f - pos != std::string::npos; // make sure f was found - s.replace(pos, f.size(), t), // replace with t, and - pos = s.find(f, pos + t.size())) // find next occurrence of f - {} - } - - JSON_PRIVATE_UNLESS_TESTED: - /// escape "~" to "~0" and "/" to "~1" - static std::string escape(std::string s) - { - replace_substring(s, "~", "~0"); - replace_substring(s, "/", "~1"); - return s; - } - - /// unescape "~1" to tilde and "~0" to slash (order is important!) - static void unescape(std::string& s) - { - replace_substring(s, "~1", "/"); - replace_substring(s, "~0", "~"); - } - private: /*! @param[in] reference_string the reference string to the current value @@ -12671,7 +12699,7 @@ class json_pointer // iterate object and use keys as reference string for (const auto& element : *value.m_value.object) { - flatten(reference_string + "/" + escape(element.first), element.second, result); + flatten(reference_string + "/" + detail::escape(element.first), element.second, result); } } break; @@ -25406,7 +25434,7 @@ class basic_json for (auto it = source.cbegin(); it != source.cend(); ++it) { // escape the key name to be used in a JSON patch - const auto key = json_pointer::escape(it.key()); + const auto key = detail::escape(it.key()); if (target.find(it.key()) != target.end()) { @@ -25430,7 +25458,7 @@ class basic_json if (source.find(it.key()) == source.end()) { // found a key that is not in this -> add it - const auto key = json_pointer::escape(it.key()); + const auto key = detail::escape(it.key()); result.push_back( { {"op", "add"}, {"path", path + "/" + key}, From aeecc09ba141e4a91a0005cecad90490921a6bb9 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 16 Jan 2021 15:33:19 +0100 Subject: [PATCH 042/143] :white_check_mark: add tests for diagnostics --- test/CMakeLists.txt | 1 + test/src/unit-diagnostics.cpp | 81 +++++++++++++++++++++++++++++++++++ test/src/unit-unicode.cpp | 4 +- 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 test/src/unit-diagnostics.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3d8bceb70c..e5484fc731 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -110,6 +110,7 @@ set(files src/unit-convenience.cpp src/unit-conversions.cpp src/unit-deserialization.cpp + src/unit-diagnostics.cpp src/unit-element_access1.cpp src/unit-element_access2.cpp src/unit-hash.cpp diff --git a/test/src/unit-diagnostics.cpp b/test/src/unit-diagnostics.cpp new file mode 100644 index 0000000000..d7c8e772b6 --- /dev/null +++ b/test/src/unit-diagnostics.cpp @@ -0,0 +1,81 @@ +/* + __ _____ _____ _____ + __| | __| | | | JSON for Modern C++ (test suite) +| | |__ | | | | | | version 3.9.1 +|_____|_____|_____|_|___| https://github.com/nlohmann/json + +Licensed under the MIT License . +SPDX-License-Identifier: MIT +Copyright (c) 2013-2019 Niels Lohmann . + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "doctest_compatibility.h" + +#define JSON_DIAGNOSTICS 1 +#include +using nlohmann::json; + +TEST_CASE("Better diagnostics") +{ + SECTION("invalid type") + { + json j; + j["a"]["b"]["c"] = 1; + std::string s; + CHECK_THROWS_WITH_AS(s = j["a"]["b"]["c"], "[json.exception.type_error.302] (/a/b/c) type must be string, but is number", json::type_error); + } + + SECTION("missing key") + { + json j; + j["object"]["object"] = true; + CHECK_THROWS_WITH_AS(j["object"].at("not_found"), "[json.exception.out_of_range.403] (/object) key 'not_found' not found", json::out_of_range); + } + + SECTION("array index out of range") + { + json j; + j["array"][4] = true; + CHECK_THROWS_WITH_AS(j["array"].at(5), "[json.exception.out_of_range.401] (/array) array index 5 is out of range", json::out_of_range); + } + + SECTION("array index at wrong type") + { + json j; + j["array"][4] = true; + CHECK_THROWS_WITH_AS(j["array"][4][5], "[json.exception.type_error.305] (/array/4) cannot use operator[] with a numeric argument with boolean", json::type_error); + } + + SECTION("wrong iterator") + { + json j; + j["array"] = json::array(); + CHECK_THROWS_WITH_AS(j["array"].erase(j.begin()), "[json.exception.invalid_iterator.202] (/array) iterator does not fit current value", json::invalid_iterator); + } + + SECTION("JSON Pointer escaping") + { + json j; + j["a/b"]["m~n"] = 1; + std::string s; + CHECK_THROWS_WITH_AS(s = j["a/b"]["m~n"], "[json.exception.type_error.302] (/a~1b/m~0n) type must be string, but is number", json::type_error); + } +} diff --git a/test/src/unit-unicode.cpp b/test/src/unit-unicode.cpp index acaca2888d..654c48c2a3 100644 --- a/test/src/unit-unicode.cpp +++ b/test/src/unit-unicode.cpp @@ -1181,8 +1181,8 @@ TEST_CASE("Unicode" * doctest::skip()) CHECK_NOTHROW(json::json_pointer("/" + ptr)); // check escape/unescape roundtrip - auto escaped = json::json_pointer::escape(ptr); - json::json_pointer::unescape(escaped); + auto escaped = nlohmann::detail::escape(ptr); + nlohmann::detail::unescape(escaped); CHECK(escaped == ptr); } } From e23af7434dad09d966c63459b53cb257d5ce1115 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 17 Jan 2021 13:32:26 +0100 Subject: [PATCH 043/143] :rotating_light: fix warnings --- include/nlohmann/json.hpp | 4 +++- single_include/nlohmann/json.hpp | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 833d8d4d27..b7fcc466a0 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1255,6 +1255,8 @@ class basic_json { return j.m_parent == this; })); +#else + static_cast(check_parents); #endif } @@ -3611,7 +3613,7 @@ class basic_json #if JSON_DIAGNOSTICS // set parent for values added above - set_parents(begin() + previous_size, idx + 1 - previous_size); + set_parents(begin() + static_cast(previous_size), static_cast(idx + 1 - previous_size)); #endif } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 5f23622f55..8d55debbf4 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18038,6 +18038,8 @@ class basic_json { return j.m_parent == this; })); +#else + static_cast(check_parents); #endif } @@ -20394,7 +20396,7 @@ class basic_json #if JSON_DIAGNOSTICS // set parent for values added above - set_parents(begin() + previous_size, idx + 1 - previous_size); + set_parents(begin() + static_cast(previous_size), static_cast(idx + 1 - previous_size)); #endif } From 65107f7c9d61e60a1e26bbbeec59a82954dcc559 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 17 Jan 2021 13:32:36 +0100 Subject: [PATCH 044/143] :green_heart: fix build --- test/src/unit-diagnostics.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/src/unit-diagnostics.cpp b/test/src/unit-diagnostics.cpp index d7c8e772b6..bdae902261 100644 --- a/test/src/unit-diagnostics.cpp +++ b/test/src/unit-diagnostics.cpp @@ -29,7 +29,12 @@ SOFTWARE. #include "doctest_compatibility.h" +#ifdef JSON_DIAGNOSTICS + #undef JSON_DIAGNOSTICS +#endif + #define JSON_DIAGNOSTICS 1 + #include using nlohmann::json; @@ -40,7 +45,7 @@ TEST_CASE("Better diagnostics") json j; j["a"]["b"]["c"] = 1; std::string s; - CHECK_THROWS_WITH_AS(s = j["a"]["b"]["c"], "[json.exception.type_error.302] (/a/b/c) type must be string, but is number", json::type_error); + CHECK_THROWS_WITH_AS(s = j["a"]["b"]["c"].get(), "[json.exception.type_error.302] (/a/b/c) type must be string, but is number", json::type_error); } SECTION("missing key") From 5ec098051443a0aee9d1abe7c8f018ec1e1ab063 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 17 Jan 2021 16:51:14 +0100 Subject: [PATCH 045/143] :green_heart: fix build --- test/src/unit-diagnostics.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/unit-diagnostics.cpp b/test/src/unit-diagnostics.cpp index bdae902261..c77030b669 100644 --- a/test/src/unit-diagnostics.cpp +++ b/test/src/unit-diagnostics.cpp @@ -81,6 +81,6 @@ TEST_CASE("Better diagnostics") json j; j["a/b"]["m~n"] = 1; std::string s; - CHECK_THROWS_WITH_AS(s = j["a/b"]["m~n"], "[json.exception.type_error.302] (/a~1b/m~0n) type must be string, but is number", json::type_error); + CHECK_THROWS_WITH_AS(s = j["a/b"]["m~n"].get(), "[json.exception.type_error.302] (/a~1b/m~0n) type must be string, but is number", json::type_error); } } From 33379684b4d7cdce6eaf243512419e97872bd127 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 17 Jan 2021 22:52:40 +0100 Subject: [PATCH 046/143] :white_check_mark: improve coverage --- include/nlohmann/detail/diagnostics_t.hpp | 6 +++--- single_include/nlohmann/json.hpp | 6 +++--- test/src/unit-diagnostics.cpp | 7 +++++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/diagnostics_t.hpp b/include/nlohmann/detail/diagnostics_t.hpp index a2f7f23a06..c9a1f46d5b 100644 --- a/include/nlohmann/detail/diagnostics_t.hpp +++ b/include/nlohmann/detail/diagnostics_t.hpp @@ -58,14 +58,14 @@ class diagnostics_t break; } - default: - break; + default: // LCOV_EXCL_LINE + break; // LCOV_EXCL_LINE } } if (tokens.empty()) { - return ""; + return ""; // LCOV_EXCL_LINE } return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 8d55debbf4..d434df940c 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2556,14 +2556,14 @@ class diagnostics_t break; } - default: - break; + default: // LCOV_EXCL_LINE + break; // LCOV_EXCL_LINE } } if (tokens.empty()) { - return ""; + return ""; // LCOV_EXCL_LINE } return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, diff --git a/test/src/unit-diagnostics.cpp b/test/src/unit-diagnostics.cpp index c77030b669..a75f3e9722 100644 --- a/test/src/unit-diagnostics.cpp +++ b/test/src/unit-diagnostics.cpp @@ -40,6 +40,13 @@ using nlohmann::json; TEST_CASE("Better diagnostics") { + SECTION("empty JSON Pointer") + { + json j = 1; + std::string s; + CHECK_THROWS_WITH_AS(s = j.get(), "[json.exception.type_error.302] type must be string, but is number", json::type_error); + } + SECTION("invalid type") { json j; From d6ff059a901fc69163d9a42e15fcef87278fb23d Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 20 Jan 2021 15:05:07 +0100 Subject: [PATCH 047/143] :ok_hand: addressed review comments --- include/nlohmann/detail/diagnostics_t.hpp | 6 +++--- single_include/nlohmann/json.hpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/diagnostics_t.hpp b/include/nlohmann/detail/diagnostics_t.hpp index c9a1f46d5b..4e943cad56 100644 --- a/include/nlohmann/detail/diagnostics_t.hpp +++ b/include/nlohmann/detail/diagnostics_t.hpp @@ -39,7 +39,7 @@ class diagnostics_t if (current->m_parent->m_value.array->operator[](i) == *current) { tokens.emplace_back(std::to_string(i)); - continue; + break; } } break; @@ -52,7 +52,7 @@ class diagnostics_t if (element.second == *current) { tokens.emplace_back(element.first.c_str()); - continue; + break; } } break; @@ -79,7 +79,7 @@ class diagnostics_t } private: - const BasicJsonType* m_j = static_cast(nullptr); + const BasicJsonType* m_j = nullptr; }; } // namespace detail diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index d434df940c..6cb7dc1b94 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2537,7 +2537,7 @@ class diagnostics_t if (current->m_parent->m_value.array->operator[](i) == *current) { tokens.emplace_back(std::to_string(i)); - continue; + break; } } break; @@ -2550,7 +2550,7 @@ class diagnostics_t if (element.second == *current) { tokens.emplace_back(element.first.c_str()); - continue; + break; } } break; @@ -2577,7 +2577,7 @@ class diagnostics_t } private: - const BasicJsonType* m_j = static_cast(nullptr); + const BasicJsonType* m_j = nullptr; }; } // namespace detail From 51ac6000d2b1b55c0cf1032677261ed407d72230 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 21 Jan 2021 13:36:23 +0100 Subject: [PATCH 048/143] :white_check_mark: improve coverage --- test/src/unit-diagnostics.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/src/unit-diagnostics.cpp b/test/src/unit-diagnostics.cpp index a75f3e9722..8bd5c41fa4 100644 --- a/test/src/unit-diagnostics.cpp +++ b/test/src/unit-diagnostics.cpp @@ -90,4 +90,9 @@ TEST_CASE("Better diagnostics") std::string s; CHECK_THROWS_WITH_AS(s = j["a/b"]["m~n"].get(), "[json.exception.type_error.302] (/a~1b/m~0n) type must be string, but is number", json::type_error); } + + SECTION("Parse error") + { + CHECK_THROWS_WITH_AS(json::parse(""), "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error); + } } From d00ad33e46fb2fe116467a9ef296cc70c4b6ac79 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 21 Jan 2021 21:47:19 +0100 Subject: [PATCH 049/143] :memo: update documentation --- doc/examples/diagnostics_extended.cpp | 22 +++++++++++++ doc/examples/diagnostics_extended.link | 1 + doc/examples/diagnostics_extended.output | 1 + doc/examples/diagnostics_standard.cpp | 20 ++++++++++++ doc/examples/diagnostics_standard.link | 1 + doc/examples/diagnostics_standard.output | 1 + doc/mkdocs/docs/features/macros.md | 10 ++++++ doc/mkdocs/docs/home/exceptions.md | 39 ++++++++++++++++++++++++ doc/mkdocs/docs/home/license.md | 2 +- 9 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 doc/examples/diagnostics_extended.cpp create mode 100644 doc/examples/diagnostics_extended.link create mode 100644 doc/examples/diagnostics_extended.output create mode 100644 doc/examples/diagnostics_standard.cpp create mode 100644 doc/examples/diagnostics_standard.link create mode 100644 doc/examples/diagnostics_standard.output diff --git a/doc/examples/diagnostics_extended.cpp b/doc/examples/diagnostics_extended.cpp new file mode 100644 index 0000000000..f4c43f05e6 --- /dev/null +++ b/doc/examples/diagnostics_extended.cpp @@ -0,0 +1,22 @@ +#include + +# define JSON_DIAGNOSTICS 1 +#include + +using json = nlohmann::json; + +int main() +{ + json j; + j["address"]["street"] = "Fake Street"; + j["address"]["housenumber"] = "12"; + + try + { + int housenumber = j["address"]["housenumber"]; + } + catch (json::exception& e) + { + std::cout << e.what() << '\n'; + } +} diff --git a/doc/examples/diagnostics_extended.link b/doc/examples/diagnostics_extended.link new file mode 100644 index 0000000000..9f10da9423 --- /dev/null +++ b/doc/examples/diagnostics_extended.link @@ -0,0 +1 @@ +online \ No newline at end of file diff --git a/doc/examples/diagnostics_extended.output b/doc/examples/diagnostics_extended.output new file mode 100644 index 0000000000..f142927a17 --- /dev/null +++ b/doc/examples/diagnostics_extended.output @@ -0,0 +1 @@ +[json.exception.type_error.302] (/address/housenumber) type must be number, but is string diff --git a/doc/examples/diagnostics_standard.cpp b/doc/examples/diagnostics_standard.cpp new file mode 100644 index 0000000000..575c409eb6 --- /dev/null +++ b/doc/examples/diagnostics_standard.cpp @@ -0,0 +1,20 @@ +#include +#include + +using json = nlohmann::json; + +int main() +{ + json j; + j["address"]["street"] = "Fake Street"; + j["address"]["housenumber"] = "12"; + + try + { + int housenumber = j["address"]["housenumber"]; + } + catch (json::exception& e) + { + std::cout << e.what() << '\n'; + } +} diff --git a/doc/examples/diagnostics_standard.link b/doc/examples/diagnostics_standard.link new file mode 100644 index 0000000000..cd0453b5ef --- /dev/null +++ b/doc/examples/diagnostics_standard.link @@ -0,0 +1 @@ +online \ No newline at end of file diff --git a/doc/examples/diagnostics_standard.output b/doc/examples/diagnostics_standard.output new file mode 100644 index 0000000000..79707a0cb9 --- /dev/null +++ b/doc/examples/diagnostics_standard.output @@ -0,0 +1 @@ +[json.exception.type_error.302] type must be number, but is string diff --git a/doc/mkdocs/docs/features/macros.md b/doc/mkdocs/docs/features/macros.md index 696438d2f0..d044bfd26b 100644 --- a/doc/mkdocs/docs/features/macros.md +++ b/doc/mkdocs/docs/features/macros.md @@ -12,6 +12,14 @@ This macro overrides `#!cpp catch` calls inside the library. The argument is the See [Switch off exceptions](../home/exceptions.md#switch-off-exceptions) for an example. +## `JSON_DIAGNOSTICS` + +This macro enables extended diagnostics for exception messages. Possible values are `1` to enable or `0` to disable (default). + +When enabled, exception messages contain a [JSON Pointer](json_pointer.md) to the JSON value that triggered the exception, see [Extended diagnostic messages](../home/exceptions.md#extended-diagnostic-messages) for an example. + +The diagnostics messages can also be controlled with the CMake option `JSON_Diagnostics` (`OFF` by default) which sets `JSON_DIAGNOSTICS` accordingly. + ## `JSON_NOEXCEPTION` Exceptions can be switched off by defining the symbol `JSON_NOEXCEPTION`. @@ -56,6 +64,8 @@ When defined to `0`, implicit conversions are switched off. By default, implicit auto s = j.get(); ``` +Implicit conversions can also be controlled with the CMake option `JSON_ImplicitConversions` (`ON` by default) which sets `JSON_USE_IMPLICIT_CONVERSIONS` accordingly. + ## `NLOHMANN_DEFINE_TYPE_INTRUSIVE(type, member...)` This macro can be used to simplify the serialization/deserialization of types if (1) want to use a JSON object as serialization and (2) want to use the member variable names as object keys in that object. diff --git a/doc/mkdocs/docs/home/exceptions.md b/doc/mkdocs/docs/home/exceptions.md index 0475f53e2b..a3b1e9c763 100644 --- a/doc/mkdocs/docs/home/exceptions.md +++ b/doc/mkdocs/docs/home/exceptions.md @@ -50,6 +50,45 @@ Note that `JSON_THROW_USER` should leave the current scope (e.g., by throwing or #include ``` +### Extended diagnostic messages + +Exceptions in the library are thrown in the local context of the JSON value they are detected. This makes detailed +diagnostics messages, and hence debugging, difficult. + +??? example + + ```cpp + --8<-- "examples/diagnostics_standard.cpp" + ``` + + Output: + + ``` + --8<-- "examples/diagnostics_standard.output" + ``` + + This exception can be hard to debug if storing the value `#!c "12"` and accessing it is further apart. + +To create better diagnostics messages, each JSON value needs a pointer to its parent value such that a global context (i.e., a path from the root value to the value that lead to the exception) can be created. That global context is provided as [JSON Pointer](../features/json_pointer.md). + +As this global context comes at the price of storing one additional pointer per JSON value and runtime overhead to maintain the parent relation, extended diagnostics are disabled by default. They can, however, be enabled by defining the preprocessor symbol [`JSON_DIAGNOSTICS`](../features/macros.md#json_diagnostics) to `1` before including `json.hpp`. + +??? example + + ```cpp + --8<-- "examples/diagnostics_extended.cpp" + ``` + + Output: + + ``` + --8<-- "examples/diagnostics_extended.output" + ``` + + Now the exception message contains a JSON Pointer `/address/housenumber` that indicates which value has the wrong type. + + + ## Parse errors This exception is thrown by the library when a parse error occurs. Parse errors diff --git a/doc/mkdocs/docs/home/license.md b/doc/mkdocs/docs/home/license.md index 4cd6ca2cc3..d359468e08 100644 --- a/doc/mkdocs/docs/home/license.md +++ b/doc/mkdocs/docs/home/license.md @@ -4,7 +4,7 @@ The class is licensed under the [MIT License](https://opensource.org/licenses/MIT): -Copyright © 2013-2020 [Niels Lohmann](https://nlohmann.me) +Copyright © 2013-2021 [Niels Lohmann](https://nlohmann.me) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: From 7b7da08fb643c75dd18516ac8835ce2f2d5509a6 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 23 Jan 2021 10:04:19 +0100 Subject: [PATCH 050/143] :memo: update documentation --- doc/mkdocs/docs/features/macros.md | 2 +- doc/mkdocs/docs/home/exceptions.md | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/doc/mkdocs/docs/features/macros.md b/doc/mkdocs/docs/features/macros.md index d044bfd26b..b468c091af 100644 --- a/doc/mkdocs/docs/features/macros.md +++ b/doc/mkdocs/docs/features/macros.md @@ -16,7 +16,7 @@ See [Switch off exceptions](../home/exceptions.md#switch-off-exceptions) for an This macro enables extended diagnostics for exception messages. Possible values are `1` to enable or `0` to disable (default). -When enabled, exception messages contain a [JSON Pointer](json_pointer.md) to the JSON value that triggered the exception, see [Extended diagnostic messages](../home/exceptions.md#extended-diagnostic-messages) for an example. +When enabled, exception messages contain a [JSON Pointer](json_pointer.md) to the JSON value that triggered the exception, see [Extended diagnostic messages](../home/exceptions.md#extended-diagnostic-messages) for an example. Note that enabling this macro increases the size of every JSON value by one pointer and adds some runtime overhead. The diagnostics messages can also be controlled with the CMake option `JSON_Diagnostics` (`OFF` by default) which sets `JSON_DIAGNOSTICS` accordingly. diff --git a/doc/mkdocs/docs/home/exceptions.md b/doc/mkdocs/docs/home/exceptions.md index a3b1e9c763..da68f21495 100644 --- a/doc/mkdocs/docs/home/exceptions.md +++ b/doc/mkdocs/docs/home/exceptions.md @@ -52,8 +52,7 @@ Note that `JSON_THROW_USER` should leave the current scope (e.g., by throwing or ### Extended diagnostic messages -Exceptions in the library are thrown in the local context of the JSON value they are detected. This makes detailed -diagnostics messages, and hence debugging, difficult. +Exceptions in the library are thrown in the local context of the JSON value they are detected. This makes detailed diagnostics messages, and hence debugging, difficult. ??? example @@ -88,7 +87,6 @@ As this global context comes at the price of storing one additional pointer per Now the exception message contains a JSON Pointer `/address/housenumber` that indicates which value has the wrong type. - ## Parse errors This exception is thrown by the library when a parse error occurs. Parse errors From 848927ae90376d6e1e383bbd40073cf1aa09190a Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Sat, 23 Jan 2021 18:24:47 +0100 Subject: [PATCH 051/143] Updated comments as requested. --- include/nlohmann/adl_serializer.hpp | 4 ++-- include/nlohmann/detail/meta/type_traits.hpp | 1 + single_include/nlohmann/json.hpp | 5 +++-- test/src/unit-regression2.cpp | 1 + 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/adl_serializer.hpp b/include/nlohmann/adl_serializer.hpp index 1dee29eb94..f967612db2 100644 --- a/include/nlohmann/adl_serializer.hpp +++ b/include/nlohmann/adl_serializer.hpp @@ -20,7 +20,7 @@ struct adl_serializer This function is usually called by the `get()` function of the @ref basic_json class (either explicit or via conversion operators). - @note This function is chosen for value types which can be default constructed. + @note This function is chosen for default-constructible value types. @param[in] j JSON value to read from @param[in,out] val value to write to @@ -39,7 +39,7 @@ struct adl_serializer This function is usually called by the `get()` function of the @ref basic_json class (either explicit or via conversion operators). - @note This function is chosen for value types which can not be default constructed. + @note This function is chosen for value types which are not default-constructible. @param[in] j JSON value to read from diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index 1fbf7cde7b..631829d07d 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -149,6 +149,7 @@ struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> /////////////////// // is_ functions // /////////////////// + // https://en.cppreference.com/w/cpp/types/conjunction template struct conjunction : std::true_type { }; template struct conjunction : B1 { }; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 7ce3138295..79c822d552 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3185,6 +3185,7 @@ struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> /////////////////// // is_ functions // /////////////////// + // https://en.cppreference.com/w/cpp/types/conjunction template struct conjunction : std::true_type { }; template struct conjunction : B1 { }; @@ -4568,7 +4569,7 @@ struct adl_serializer This function is usually called by the `get()` function of the @ref basic_json class (either explicit or via conversion operators). - @note This function is chosen for value types which can be default constructed. + @note This function is chosen for default-constructible value types. @param[in] j JSON value to read from @param[in,out] val value to write to @@ -4587,7 +4588,7 @@ struct adl_serializer This function is usually called by the `get()` function of the @ref basic_json class (either explicit or via conversion operators). - @note This function is chosen for value types which can not be default constructed. + @note This function is chosen for value types which are not default-constructible. @param[in] j JSON value to read from diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index e31296fac6..5a3de09d2b 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -136,6 +136,7 @@ struct NotSerializableData ///////////////////////////////////////////////////////////////////// // for #2574 ///////////////////////////////////////////////////////////////////// + struct NonDefaultConstructible { explicit NonDefaultConstructible (int x) : x(x) { } From 380a613f2b5d32425021129cd1f371ddcfd54ddf Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 23 Jan 2021 20:58:59 +0100 Subject: [PATCH 052/143] :bug: fix bug in diagnostics_t --- include/nlohmann/detail/diagnostics_t.hpp | 4 ++-- single_include/nlohmann/json.hpp | 4 ++-- test/src/unit-diagnostics.cpp | 6 ++++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/diagnostics_t.hpp b/include/nlohmann/detail/diagnostics_t.hpp index 4e943cad56..f89b0e3d13 100644 --- a/include/nlohmann/detail/diagnostics_t.hpp +++ b/include/nlohmann/detail/diagnostics_t.hpp @@ -36,7 +36,7 @@ class diagnostics_t { for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) { - if (current->m_parent->m_value.array->operator[](i) == *current) + if (¤t->m_parent->m_value.array->operator[](i) == current) { tokens.emplace_back(std::to_string(i)); break; @@ -49,7 +49,7 @@ class diagnostics_t { for (const auto& element : *current->m_parent->m_value.object) { - if (element.second == *current) + if (&element.second == current) { tokens.emplace_back(element.first.c_str()); break; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 6cb7dc1b94..fc9f6d630a 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2534,7 +2534,7 @@ class diagnostics_t { for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) { - if (current->m_parent->m_value.array->operator[](i) == *current) + if (¤t->m_parent->m_value.array->operator[](i) == current) { tokens.emplace_back(std::to_string(i)); break; @@ -2547,7 +2547,7 @@ class diagnostics_t { for (const auto& element : *current->m_parent->m_value.object) { - if (element.second == *current) + if (&element.second == current) { tokens.emplace_back(element.first.c_str()); break; diff --git a/test/src/unit-diagnostics.cpp b/test/src/unit-diagnostics.cpp index 8bd5c41fa4..5885f6ecb7 100644 --- a/test/src/unit-diagnostics.cpp +++ b/test/src/unit-diagnostics.cpp @@ -95,4 +95,10 @@ TEST_CASE("Better diagnostics") { CHECK_THROWS_WITH_AS(json::parse(""), "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error); } + + SECTION("Regression test for https://github.com/nlohmann/json/pull/2562#pullrequestreview-574858448") + { + CHECK_THROWS_WITH_AS(json({"0", "0"})[1].get(), "[json.exception.type_error.302] (/1) type must be number, but is string", json::type_error); + CHECK_THROWS_WITH_AS(json({"0", "1"})[1].get(), "[json.exception.type_error.302] (/1) type must be number, but is string", json::type_error); + } } From c190a72f3d8ae2a628ba57a9fedf58f91142cc83 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 24 Jan 2021 17:45:08 +0100 Subject: [PATCH 053/143] :ok_hand: apply suggestion Co-authored-by: Alexander Karzhenkov --- include/nlohmann/detail/json_pointer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 52cf171345..9fb7d0d22a 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -884,7 +884,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive())) { - JSON_THROW(detail::type_error::create(315, "values in object must be primitive", diagnostics_t(element))); + JSON_THROW(detail::type_error::create(315, "values in object must be primitive", diagnostics_t(element.second))); } // assign value to reference pointed to by JSON pointer; Note that if From e8dba10f53dd44c11be3eca1c273779cb48a69b0 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 24 Jan 2021 17:45:48 +0100 Subject: [PATCH 054/143] :white_check_mark: add test --- single_include/nlohmann/json.hpp | 2 +- test/src/unit-diagnostics.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index fc9f6d630a..33ca9ad89a 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -12739,7 +12739,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive())) { - JSON_THROW(detail::type_error::create(315, "values in object must be primitive", diagnostics_t(element))); + JSON_THROW(detail::type_error::create(315, "values in object must be primitive", diagnostics_t(element.second))); } // assign value to reference pointed to by JSON pointer; Note that if diff --git a/test/src/unit-diagnostics.cpp b/test/src/unit-diagnostics.cpp index 5885f6ecb7..1cea374a74 100644 --- a/test/src/unit-diagnostics.cpp +++ b/test/src/unit-diagnostics.cpp @@ -101,4 +101,11 @@ TEST_CASE("Better diagnostics") CHECK_THROWS_WITH_AS(json({"0", "0"})[1].get(), "[json.exception.type_error.302] (/1) type must be number, but is string", json::type_error); CHECK_THROWS_WITH_AS(json({"0", "1"})[1].get(), "[json.exception.type_error.302] (/1) type must be number, but is string", json::type_error); } + + SECTION("Regression test for https://github.com/nlohmann/json/pull/2562/files/380a613f2b5d32425021129cd1f371ddcfd54ddf#r563259793") + { + json j; + j["/foo"] = {1, 2, 3}; + CHECK_THROWS_WITH_AS(j.unflatten(), "[json.exception.type_error.315] (/~1foo) values in object must be primitive", json::type_error); + } } From 130382f2a90428d8fdd72e7b156d428764552efb Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Sun, 24 Jan 2021 20:02:24 +0100 Subject: [PATCH 055/143] Remove comment about GCC commit which didn't really relate to the code. --- include/nlohmann/detail/meta/type_traits.hpp | 1 - single_include/nlohmann/json.hpp | 1 - 2 files changed, 2 deletions(-) diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index 631829d07d..22d0bfe044 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -160,7 +160,6 @@ struct conjunction // Reimplementation of is_constructible and is_default_constructible, due to them being broken for // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367). // This causes compile errors in e.g. clang 3.5 or gcc 4.9. -// Based on commit fixing this in gcc: https://github.com/gcc-mirror/gcc/commit/d3c64041b32b6962ad6b2d879231537a477631fb template struct is_default_constructible : std::is_default_constructible {}; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 79c822d552..7abc0f9b71 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3196,7 +3196,6 @@ struct conjunction // Reimplementation of is_constructible and is_default_constructible, due to them being broken for // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367). // This causes compile errors in e.g. clang 3.5 or gcc 4.9. -// Based on commit fixing this in gcc: https://github.com/gcc-mirror/gcc/commit/d3c64041b32b6962ad6b2d879231537a477631fb template struct is_default_constructible : std::is_default_constructible {}; From 74cc0ab47098f0b25c0436564924c437c2254781 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 25 Jan 2021 13:47:50 +0100 Subject: [PATCH 056/143] :recycle: remove diagnostics_t class --- .../nlohmann/detail/conversions/from_json.hpp | 31 ++-- include/nlohmann/detail/diagnostics_t.hpp | 86 ---------- include/nlohmann/detail/exceptions.hpp | 82 ++++++++-- .../nlohmann/detail/input/binary_reader.hpp | 38 +++-- include/nlohmann/detail/input/json_sax.hpp | 11 +- include/nlohmann/detail/input/parser.hpp | 36 ++--- .../nlohmann/detail/iterators/iter_impl.hpp | 26 ++-- include/nlohmann/detail/json_pointer.hpp | 43 +++-- .../nlohmann/detail/output/binary_writer.hpp | 8 +- include/nlohmann/detail/output/serializer.hpp | 6 +- include/nlohmann/json.hpp | 147 +++++++++--------- test/src/unit-json_pointer.cpp | 7 +- 12 files changed, 231 insertions(+), 290 deletions(-) delete mode 100644 include/nlohmann/detail/diagnostics_t.hpp diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 2a8d7543e2..f03c018159 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -13,7 +13,6 @@ #include // valarray #include -#include #include #include #include @@ -28,7 +27,7 @@ void from_json(const BasicJsonType& j, typename std::nullptr_t& n) { if (JSON_HEDLEY_UNLIKELY(!j.is_null())) { - JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), j)); } n = nullptr; } @@ -59,7 +58,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j)); } } @@ -68,7 +67,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { if (JSON_HEDLEY_UNLIKELY(!j.is_boolean())) { - JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), j)); } b = *j.template get_ptr(); } @@ -78,7 +77,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j)); } s = *j.template get_ptr(); } @@ -94,7 +93,7 @@ void from_json(const BasicJsonType& j, ConstructibleStringType& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j)); } s = *j.template get_ptr(); @@ -134,7 +133,7 @@ void from_json(const BasicJsonType& j, std::forward_list& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } l.clear(); std::transform(j.rbegin(), j.rend(), @@ -151,7 +150,7 @@ void from_json(const BasicJsonType& j, std::valarray& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } l.resize(j.size()); std::transform(j.begin(), j.end(), std::begin(l), @@ -242,7 +241,7 @@ void()) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } from_json_array_impl(j, arr, priority_tag<3> {}); @@ -253,7 +252,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin) { if (JSON_HEDLEY_UNLIKELY(!j.is_binary())) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), j)); } bin = *j.template get_ptr(); @@ -265,7 +264,7 @@ void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) { if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), j)); } ConstructibleObjectType ret; @@ -319,7 +318,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j)); } } @@ -348,14 +347,14 @@ void from_json(const BasicJsonType& j, std::map& { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j)); } m.emplace(p.at(0).template get(), p.at(1).template get()); } @@ -368,14 +367,14 @@ void from_json(const BasicJsonType& j, std::unordered_map(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j)); } m.emplace(p.at(0).template get(), p.at(1).template get()); } diff --git a/include/nlohmann/detail/diagnostics_t.hpp b/include/nlohmann/detail/diagnostics_t.hpp deleted file mode 100644 index f89b0e3d13..0000000000 --- a/include/nlohmann/detail/diagnostics_t.hpp +++ /dev/null @@ -1,86 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace nlohmann -{ -namespace detail -{ - -template -class diagnostics_t -{ - public: - diagnostics_t() noexcept = default; - diagnostics_t(const BasicJsonType& j) noexcept - : m_j(&j) - {} - - std::string diagnostics() const - { -#if JSON_DIAGNOSTICS - if (m_j == nullptr) - { - return ""; - } - - std::vector tokens; - for (const auto* current = m_j; current->m_parent != nullptr; current = current->m_parent) - { - switch (current->m_parent->type()) - { - case value_t::array: - { - for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) - { - if (¤t->m_parent->m_value.array->operator[](i) == current) - { - tokens.emplace_back(std::to_string(i)); - break; - } - } - break; - } - - case value_t::object: - { - for (const auto& element : *current->m_parent->m_value.object) - { - if (&element.second == current) - { - tokens.emplace_back(element.first.c_str()); - break; - } - } - break; - } - - default: // LCOV_EXCL_LINE - break; // LCOV_EXCL_LINE - } - } - - if (tokens.empty()) - { - return ""; // LCOV_EXCL_LINE - } - - return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, - [](const std::string & a, const std::string & b) - { - return a + "/" + detail::escape(b); - }) + ") "; -#else - return ""; -#endif - } - - private: - const BasicJsonType* m_j = nullptr; -}; - -} // namespace detail -} // namespace nlohmann diff --git a/include/nlohmann/detail/exceptions.hpp b/include/nlohmann/detail/exceptions.hpp index 56a582f794..31a4a2c354 100644 --- a/include/nlohmann/detail/exceptions.hpp +++ b/include/nlohmann/detail/exceptions.hpp @@ -4,7 +4,8 @@ #include // runtime_error #include // to_string -#include +#include +#include #include #include @@ -66,6 +67,61 @@ class exception : public std::exception return "[json.exception." + ename + "." + std::to_string(id_) + "] "; } + template + static std::string diagnostics(const BasicJsonType& leaf_element) + { +#if JSON_DIAGNOSTICS + std::vector tokens; + for (const auto* current = &leaf_element; current->m_parent != nullptr; current = current->m_parent) + { + switch (current->m_parent->type()) + { + case value_t::array: + { + for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) + { + if (¤t->m_parent->m_value.array->operator[](i) == current) + { + tokens.emplace_back(std::to_string(i)); + break; + } + } + break; + } + + case value_t::object: + { + for (const auto& element : *current->m_parent->m_value.object) + { + if (&element.second == current) + { + tokens.emplace_back(element.first.c_str()); + break; + } + } + break; + } + + default: // LCOV_EXCL_LINE + break; // LCOV_EXCL_LINE + } + } + + if (tokens.empty()) + { + return ""; + } + + return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, + [](const std::string & a, const std::string & b) + { + return a + "/" + detail::escape(b); + }) + ") "; +#else + return ""; +#endif + } + private: /// an exception object as storage for error messages std::runtime_error m; @@ -129,19 +185,19 @@ class parse_error : public exception @return parse_error object */ template - static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const detail::diagnostics_t& diagnostics) + static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("parse_error", id_) + "parse error" + - position_string(pos) + ": " + diagnostics.diagnostics() + what_arg; + position_string(pos) + ": " + exception::diagnostics(context) + what_arg; return parse_error(id_, pos.chars_read_total, w.c_str()); } template - static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) + static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("parse_error", id_) + "parse error" + (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + - ": " + diagnostics.diagnostics() + what_arg; + ": " + exception::diagnostics(context) + what_arg; return parse_error(id_, byte_, w.c_str()); } @@ -208,9 +264,9 @@ class invalid_iterator : public exception { public: template - static invalid_iterator create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) + static invalid_iterator create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("invalid_iterator", id_) + diagnostics.diagnostics() + what_arg; + std::string w = exception::name("invalid_iterator", id_) + exception::diagnostics(context) + what_arg; return invalid_iterator(id_, w.c_str()); } @@ -263,9 +319,9 @@ class type_error : public exception { public: template - static type_error create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) + static type_error create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("type_error", id_) + diagnostics.diagnostics() + what_arg; + std::string w = exception::name("type_error", id_) + exception::diagnostics(context) + what_arg; return type_error(id_, w.c_str()); } @@ -311,9 +367,9 @@ class out_of_range : public exception { public: template - static out_of_range create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) + static out_of_range create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("out_of_range", id_) + diagnostics.diagnostics() + what_arg; + std::string w = exception::name("out_of_range", id_) + exception::diagnostics(context) + what_arg; return out_of_range(id_, w.c_str()); } @@ -350,9 +406,9 @@ class other_error : public exception { public: template - static other_error create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) + static other_error create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("other_error", id_) + diagnostics.diagnostics() + what_arg; + std::string w = exception::name("other_error", id_) + exception::diagnostics(context) + what_arg; return other_error(id_, w.c_str()); } diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index a896f15586..ad7359da3c 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -13,7 +13,6 @@ #include // make_pair, move #include // vector -#include #include #include #include @@ -65,7 +64,6 @@ class binary_reader using json_sax_t = SAX; using char_type = typename InputAdapterType::char_type; using char_int_type = typename std::char_traits::int_type; - using diagnostics_t = detail::diagnostics_t; public: /*! @@ -139,7 +137,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current != std::char_traits::eof())) { return sax->parse_error(chars_read, get_token_string(), - parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"), diagnostics_t())); + parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"), BasicJsonType())); } } @@ -215,7 +213,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 1)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"), BasicJsonType())); } return get_string(input_format_t::bson, len - static_cast(1), result) && get() != std::char_traits::eof(); @@ -236,7 +234,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 0)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"), BasicJsonType())); } // All BSON binary values have a subtype @@ -318,7 +316,7 @@ class binary_reader { std::array cr{{}}; (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type)); - return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), diagnostics_t())); + return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), BasicJsonType())); } } } @@ -718,7 +716,7 @@ class binary_reader case cbor_tag_handler_t::error: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); } case cbor_tag_handler_t::ignore: @@ -833,7 +831,7 @@ class binary_reader default: // anything else (0xFF is handled inside the other types) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); } } } @@ -928,7 +926,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"), BasicJsonType())); } } } @@ -1027,7 +1025,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"), BasicJsonType())); } } } @@ -1494,7 +1492,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); } } } @@ -1576,7 +1574,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"), BasicJsonType())); } } } @@ -1826,7 +1824,7 @@ class binary_reader default: auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), BasicJsonType())); } } @@ -1896,7 +1894,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), BasicJsonType())); } } } @@ -1934,7 +1932,7 @@ class binary_reader return false; } auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"), BasicJsonType())); } return get_ubjson_size_value(result.first); @@ -2024,7 +2022,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current > 127)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"), BasicJsonType())); } string_t s(1, static_cast(current)); return sax->string(s); @@ -2045,7 +2043,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); } } } @@ -2223,7 +2221,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input)) { - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), diagnostics_t())); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), BasicJsonType())); } switch (result_number) @@ -2235,7 +2233,7 @@ class binary_reader case token_type::value_float: return sax->number_float(number_lexer.get_number_float(), std::move(number_string)); default: - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), diagnostics_t())); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), BasicJsonType())); } } @@ -2391,7 +2389,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current == std::char_traits::eof())) { return sax->parse_error(chars_read, "", - parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), diagnostics_t())); + parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), BasicJsonType())); } return true; } diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 4bd64c80e3..a4b7e6d086 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -5,7 +5,6 @@ #include // move #include // vector -#include #include #include @@ -155,7 +154,6 @@ class json_sax_dom_parser using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; using binary_t = typename BasicJsonType::binary_t; - using diagnostics_t = detail::diagnostics_t; /*! @param[in, out] r reference to a JSON value that is manipulated while @@ -221,7 +219,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), *ref_stack.back())); } return true; @@ -247,7 +245,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), *ref_stack.back())); } return true; @@ -332,7 +330,6 @@ class json_sax_dom_callback_parser using binary_t = typename BasicJsonType::binary_t; using parser_callback_t = typename BasicJsonType::parser_callback_t; using parse_event_t = typename BasicJsonType::parse_event_t; - using diagnostics_t = detail::diagnostics_t; json_sax_dom_callback_parser(BasicJsonType& r, const parser_callback_t cb, @@ -403,7 +400,7 @@ class json_sax_dom_callback_parser // check object limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), *ref_stack.back())); } return true; @@ -473,7 +470,7 @@ class json_sax_dom_callback_parser // check array limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), *ref_stack.back())); } return true; diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index d9ac2b684c..3d04f95443 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -8,7 +8,6 @@ #include // vector #include -#include #include #include #include @@ -58,7 +57,6 @@ class parser using string_t = typename BasicJsonType::string_t; using lexer_t = lexer; using token_type = typename lexer_t::token_type; - using diagnostics_t = detail::diagnostics_t; public: /// a parser reading from an input adapter @@ -97,7 +95,7 @@ class parser sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"), diagnostics_t())); + exception_message(token_type::end_of_input, "value"), BasicJsonType())); } // in case of an error, return discarded value @@ -124,8 +122,7 @@ class parser { sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType())); } // in case of an error, return discarded value @@ -163,8 +160,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType())); } return result; @@ -210,8 +206,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) { @@ -223,8 +218,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType())); } // remember we are now inside an object @@ -267,7 +261,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'", diagnostics_t())); + out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'", BasicJsonType())); } if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string()))) @@ -337,16 +331,14 @@ class parser // using "uninitialized" to avoid "expected" message return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::uninitialized, "value"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), BasicJsonType())); } default: // the last token was unexpected { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::literal_or_value, "value"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), BasicJsonType())); } } } @@ -392,8 +384,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_array, "array"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), BasicJsonType())); } else // object { @@ -405,8 +396,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) @@ -419,8 +409,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType())); } // parse values @@ -448,8 +437,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_object, "object"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), BasicJsonType())); } } } diff --git a/include/nlohmann/detail/iterators/iter_impl.hpp b/include/nlohmann/detail/iterators/iter_impl.hpp index 565ac64d1a..118fef3f51 100644 --- a/include/nlohmann/detail/iterators/iter_impl.hpp +++ b/include/nlohmann/detail/iterators/iter_impl.hpp @@ -3,7 +3,6 @@ #include // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next #include // conditional, is_const, remove_const -#include #include #include #include @@ -52,7 +51,6 @@ class iter_impl // make sure BasicJsonType is basic_json or const basic_json static_assert(is_basic_json::type>::value, "iter_impl only accepts (const) basic_json"); - using diagnostics_t = detail::diagnostics_t; public: @@ -259,7 +257,7 @@ class iter_impl } case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); default: { @@ -268,7 +266,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); } } } @@ -302,7 +300,7 @@ class iter_impl return m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); } } } @@ -403,7 +401,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object)); } JSON_ASSERT(m_object != nullptr); @@ -440,7 +438,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object)); } JSON_ASSERT(m_object != nullptr); @@ -448,7 +446,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", *m_object)); case value_t::array: return (m_it.array_iterator < other.m_it.array_iterator); @@ -496,7 +494,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object)); case value_t::array: { @@ -567,7 +565,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object)); case value_t::array: return m_it.array_iterator - other.m_it.array_iterator; @@ -588,13 +586,13 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", *m_object)); case value_t::array: return *std::next(m_it.array_iterator, n); case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); default: { @@ -603,7 +601,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); } } } @@ -621,7 +619,7 @@ class iter_impl return m_it.object_iterator->first; } - JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", *m_object)); } /*! diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 9fb7d0d22a..cb2ec85600 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -8,7 +8,6 @@ #include // move #include // vector -#include #include #include #include @@ -23,8 +22,6 @@ class json_pointer NLOHMANN_BASIC_JSON_TPL_DECLARATION friend class basic_json; - using diagnostics_t = detail::diagnostics_t; - public: /*! @brief create JSON pointer @@ -251,7 +248,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); } reference_tokens.pop_back(); @@ -275,7 +272,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); } return reference_tokens.back(); @@ -341,13 +338,13 @@ class json_pointer // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) { - JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", diagnostics_t())); + JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", BasicJsonType())); } // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9'))) { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", diagnostics_t())); + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", BasicJsonType())); } std::size_t processed_chars = 0; @@ -358,20 +355,20 @@ class json_pointer } JSON_CATCH(std::out_of_range&) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", diagnostics_t())); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType())); } // check if the string was completely read if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size())) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", diagnostics_t())); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType())); } // only triggered on special platforms (like 32bit), see also // https://github.com/nlohmann/json/pull/2203 if (res >= static_cast((std::numeric_limits::max)())) { - JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", diagnostics_t())); // LCOV_EXCL_LINE + JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", BasicJsonType())); // LCOV_EXCL_LINE } return static_cast(res); @@ -382,7 +379,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); } json_pointer result = *this; @@ -445,7 +442,7 @@ class json_pointer single value; that is, with an empty list of reference tokens. */ default: - JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", diagnostics_t(j))); + JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", j)); } } @@ -517,7 +514,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); } } @@ -550,7 +547,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range", diagnostics_t(*ptr))); + ") is out of range", *ptr)); } // note: at performs range check @@ -559,7 +556,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); } } @@ -597,7 +594,7 @@ class json_pointer if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) { // "-" cannot be used for const access - JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", diagnostics_t(*ptr))); + JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", *ptr)); } // use unchecked array access @@ -606,7 +603,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); } } @@ -639,7 +636,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range", diagnostics_t(*ptr))); + ") is out of range", *ptr)); } // note: at performs range check @@ -648,7 +645,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); } } @@ -752,7 +749,7 @@ class json_pointer // check if nonempty reference string begins with slash if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) { - JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", diagnostics_t())); + JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", BasicJsonType())); } // extract the reference tokens: @@ -787,7 +784,7 @@ class json_pointer (reference_token[pos + 1] != '0' && reference_token[pos + 1] != '1'))) { - JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", diagnostics_t())); + JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", BasicJsonType())); } } @@ -874,7 +871,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!value.is_object())) { - JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", diagnostics_t(value))); + JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", value)); } BasicJsonType result; @@ -884,7 +881,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive())) { - JSON_THROW(detail::type_error::create(315, "values in object must be primitive", diagnostics_t(element.second))); + JSON_THROW(detail::type_error::create(315, "values in object must be primitive", element.second)); } // assign value to reference pointed to by JSON pointer; Note that if diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index b8314402cc..27215f193e 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -8,7 +8,6 @@ #include // string #include // isnan, isinf -#include #include #include #include @@ -30,7 +29,6 @@ class binary_writer using string_t = typename BasicJsonType::string_t; using binary_t = typename BasicJsonType::binary_t; using number_float_t = typename BasicJsonType::number_float_t; - using diagnostics_t = detail::diagnostics_t; public: /*! @@ -59,7 +57,7 @@ class binary_writer default: { - JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), diagnostics_t(j)));; + JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), j));; } } } @@ -908,7 +906,7 @@ class binary_writer const auto it = name.find(static_cast(0)); if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) { - JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", diagnostics_t(j))); + JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", j)); } return /*id*/ 1ul + name.size() + /*zero-terminator*/1u; @@ -1032,7 +1030,7 @@ class binary_writer } else { - JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", diagnostics_t(j))); + JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", j)); } } diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 7c570f8cb7..d9c8b22fef 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -13,7 +13,6 @@ #include // move #include -#include #include #include #include @@ -45,7 +44,6 @@ class serializer using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using binary_char_t = typename BasicJsonType::binary_t::value_type; - using diagnostics_t = detail::diagnostics_t; static constexpr std::uint8_t UTF8_ACCEPT = 0; static constexpr std::uint8_t UTF8_REJECT = 1; @@ -501,7 +499,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, diagnostics_t())); + JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, BasicJsonType())); } case error_handler_t::ignore: @@ -595,7 +593,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, diagnostics_t())); + JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, BasicJsonType())); } case error_handler_t::ignore: diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index b7fcc466a0..7b44c0ea34 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -51,7 +51,6 @@ SOFTWARE. #include #include #include -#include #include #include #include @@ -190,15 +189,11 @@ class basic_json friend class ::nlohmann::detail::json_sax_dom_parser; template friend class ::nlohmann::detail::json_sax_dom_callback_parser; - template - friend class ::nlohmann::detail::diagnostics_t; + friend class ::nlohmann::detail::exception; /// workaround type for MSVC using basic_json_t = NLOHMANN_BASIC_JSON_TPL; - /// shortcut - using diagnostics_t = ::nlohmann::detail::diagnostics_t; - JSON_PRIVATE_UNLESS_TESTED: // convenience aliases for types residing in namespace detail; using lexer = ::nlohmann::detail::lexer_base; @@ -1066,7 +1061,7 @@ class basic_json object = nullptr; // silence warning, see #821 if (JSON_HEDLEY_UNLIKELY(t == value_t::null)) { - JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1", diagnostics_t())); // LCOV_EXCL_LINE + JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1", basic_json())); // LCOV_EXCL_LINE } break; } @@ -1707,7 +1702,7 @@ class basic_json // if object is wanted but impossible, throw an exception if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object)) { - JSON_THROW(type_error::create(301, "cannot create object from initializer list", diagnostics_t())); + JSON_THROW(type_error::create(301, "cannot create object from initializer list", basic_json())); } } @@ -2012,7 +2007,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", diagnostics_t())); + JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", basic_json())); } // copy type from first iterator @@ -2030,7 +2025,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t())); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", *first.m_object)); } break; } @@ -2092,7 +2087,7 @@ class basic_json } default: - JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), diagnostics_t())); + JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), *first.m_object)); } set_parents(); @@ -2791,7 +2786,7 @@ class basic_json return m_value.boolean; } - JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()), *this)); } /// get a pointer to the value (object) @@ -2912,7 +2907,7 @@ class basic_json return *ptr; } - JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()), diagnostics_t(obj))); + JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()), obj)); } public: @@ -3340,7 +3335,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), *this)); } return *get_ptr(); @@ -3351,7 +3346,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), *this)); } return *get_ptr(); @@ -3406,12 +3401,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this)); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); } } @@ -3453,12 +3448,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this)); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); } } @@ -3504,12 +3499,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this))); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this)); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); } } @@ -3555,12 +3550,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this))); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this)); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); } } @@ -3620,7 +3615,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), *this)); } /*! @@ -3650,7 +3645,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), *this)); } /*! @@ -3696,7 +3691,7 @@ class basic_json return set_parent(m_value.object->operator[](key)); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } /*! @@ -3738,7 +3733,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } /*! @@ -3786,7 +3781,7 @@ class basic_json return set_parent(m_value.object->operator[](key)); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } /*! @@ -3830,7 +3825,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } /*! @@ -3902,7 +3897,7 @@ class basic_json return default_value; } - JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), *this)); } /*! @@ -3975,7 +3970,7 @@ class basic_json } } - JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), *this)); } /*! @@ -4129,7 +4124,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != pos.m_object)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } IteratorType result = end(); @@ -4145,7 +4140,7 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin())) { - JSON_THROW(invalid_iterator::create(205, "iterator out of range", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(205, "iterator out of range", *this)); } if (is_string()) @@ -4181,7 +4176,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); } return result; @@ -4242,7 +4237,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object)) { - JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", *this)); } IteratorType result = end(); @@ -4259,7 +4254,7 @@ class basic_json if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", *this)); } if (is_string()) @@ -4297,7 +4292,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); } return result; @@ -4340,7 +4335,7 @@ class basic_json return m_value.object->erase(key); } - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); } /*! @@ -4374,14 +4369,14 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(idx >= size())) { - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this)); } m_value.array->erase(m_value.array->begin() + static_cast(idx)); } else { - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); } } @@ -5326,7 +5321,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this)); } // transform null object into an array @@ -5362,7 +5357,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this)); } // transform null object into an array @@ -5413,7 +5408,7 @@ class basic_json // push_back only works for null objects or objects if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this)); } // transform null object into an object @@ -5517,7 +5512,7 @@ class basic_json // emplace_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), *this)); } // transform null object into an array @@ -5570,7 +5565,7 @@ class basic_json // emplace only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), *this)); } // transform null object into an object @@ -5643,14 +5638,14 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } // insert to array and return iterator return set_parents(insert_iterator(pos, val), static_cast(1)); } - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } /*! @@ -5694,14 +5689,14 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } // insert to array and return iterator return set_parents(insert_iterator(pos, cnt, val), static_cast(cnt)); } - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } /*! @@ -5739,24 +5734,24 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this)); } if (JSON_HEDLEY_UNLIKELY(first.m_object == this)) { - JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", *this)); } // insert to array and return iterator @@ -5792,13 +5787,13 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } // insert to array and return iterator @@ -5833,19 +5828,19 @@ class basic_json // insert only works for objects if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this)); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this)); } m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator); @@ -5882,11 +5877,11 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), *this)); } if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()), *this)); } for (auto it = j.cbegin(); it != j.cend(); ++it) @@ -5933,20 +5928,20 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), *this)); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this)); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object() || !last.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this)); } for (auto it = first; it != last; ++it) @@ -6044,7 +6039,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -6077,7 +6072,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -6110,7 +6105,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -6143,7 +6138,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -6157,7 +6152,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -8366,7 +8361,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(idx > parent.size())) { // avoid undefined behavior - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(parent))); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", parent)); } // default case: insert add offset @@ -8400,7 +8395,7 @@ class basic_json } else { - JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found", diagnostics_t(*this))); + JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found", *this)); } } else if (parent.is_array()) @@ -8413,7 +8408,7 @@ class basic_json // type check: top level value must be an array if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array())) { - JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(json_patch))); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", json_patch)); } // iterate and apply the operations @@ -8433,13 +8428,13 @@ class basic_json // check if desired value is present if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end())) { - JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", diagnostics_t(val))); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", val)); } // check if result is of type string if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string())) { - JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", diagnostics_t(val))); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", val)); } // no error: return value @@ -8449,7 +8444,7 @@ class basic_json // type check: every element of the array must be an object if (JSON_HEDLEY_UNLIKELY(!val.is_object())) { - JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(val))); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", val)); } // collect mandatory members @@ -8527,7 +8522,7 @@ class basic_json // throw an exception if test fails if (JSON_HEDLEY_UNLIKELY(!success)) { - JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump(), diagnostics_t(val))); + JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump(), val)); } break; @@ -8537,7 +8532,7 @@ class basic_json { // op must be "add", "remove", "replace", "move", "copy", or // "test" - JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid", diagnostics_t(val))); + JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid", val)); } } } diff --git a/test/src/unit-json_pointer.cpp b/test/src/unit-json_pointer.cpp index 14d8cd1830..52a798fd29 100644 --- a/test/src/unit-json_pointer.cpp +++ b/test/src/unit-json_pointer.cpp @@ -496,8 +496,11 @@ TEST_CASE("JSON pointers") // error for nonprimitve values CHECK_THROWS_AS(json({{"/1", {1, 2, 3}}}).unflatten(), json::type_error&); - CHECK_THROWS_WITH(json({{"/1", {1, 2, 3}}}).unflatten(), - "[json.exception.type_error.315] values in object must be primitive"); +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH(json({{"/1", {1, 2, 3}}}).unflatten(), "[json.exception.type_error.315] (/~11) values in object must be primitive"); +#else + CHECK_THROWS_WITH(json({{"/1", {1, 2, 3}}}).unflatten(), "[json.exception.type_error.315] values in object must be primitive"); +#endif // error for conflicting values json j_error = {{"", 42}, {"/foo", 17}}; From 524eea58871f60968f3e7a3389942ae941b90c47 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 26 Jan 2021 17:49:15 +0100 Subject: [PATCH 057/143] :ok_hand: remove unused template parameter --- include/nlohmann/detail/exceptions.hpp | 16 +- single_include/nlohmann/json.hpp | 520 +++++++++++-------------- 2 files changed, 232 insertions(+), 304 deletions(-) diff --git a/include/nlohmann/detail/exceptions.hpp b/include/nlohmann/detail/exceptions.hpp index 31a4a2c354..5c9dce3c57 100644 --- a/include/nlohmann/detail/exceptions.hpp +++ b/include/nlohmann/detail/exceptions.hpp @@ -114,9 +114,9 @@ class exception : public std::exception return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, [](const std::string & a, const std::string & b) - { - return a + "/" + detail::escape(b); - }) + ") "; + { + return a + "/" + detail::escape(b); + }) + ") "; #else return ""; #endif @@ -188,7 +188,7 @@ class parse_error : public exception static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("parse_error", id_) + "parse error" + - position_string(pos) + ": " + exception::diagnostics(context) + what_arg; + position_string(pos) + ": " + exception::diagnostics(context) + what_arg; return parse_error(id_, pos.chars_read_total, w.c_str()); } @@ -197,7 +197,7 @@ class parse_error : public exception { std::string w = exception::name("parse_error", id_) + "parse error" + (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + - ": " + exception::diagnostics(context) + what_arg; + ": " + exception::diagnostics(context) + what_arg; return parse_error(id_, byte_, w.c_str()); } @@ -321,7 +321,7 @@ class type_error : public exception template static type_error create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("type_error", id_) + exception::diagnostics(context) + what_arg; + std::string w = exception::name("type_error", id_) + exception::diagnostics(context) + what_arg; return type_error(id_, w.c_str()); } @@ -369,7 +369,7 @@ class out_of_range : public exception template static out_of_range create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("out_of_range", id_) + exception::diagnostics(context) + what_arg; + std::string w = exception::name("out_of_range", id_) + exception::diagnostics(context) + what_arg; return out_of_range(id_, w.c_str()); } @@ -408,7 +408,7 @@ class other_error : public exception template static other_error create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("other_error", id_) + exception::diagnostics(context) + what_arg; + std::string w = exception::name("other_error", id_) + exception::diagnostics(context) + what_arg; return other_error(id_, w.c_str()); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 33ca9ad89a..e11cbd34cd 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -73,11 +73,6 @@ SOFTWARE. #include // runtime_error #include // to_string -// #include - - -#include -#include // #include @@ -2502,87 +2497,6 @@ static void unescape(std::string& s) } // namespace detail } // namespace nlohmann - -namespace nlohmann -{ -namespace detail -{ - -template -class diagnostics_t -{ - public: - diagnostics_t() noexcept = default; - diagnostics_t(const BasicJsonType& j) noexcept - : m_j(&j) - {} - - std::string diagnostics() const - { -#if JSON_DIAGNOSTICS - if (m_j == nullptr) - { - return ""; - } - - std::vector tokens; - for (const auto* current = m_j; current->m_parent != nullptr; current = current->m_parent) - { - switch (current->m_parent->type()) - { - case value_t::array: - { - for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) - { - if (¤t->m_parent->m_value.array->operator[](i) == current) - { - tokens.emplace_back(std::to_string(i)); - break; - } - } - break; - } - - case value_t::object: - { - for (const auto& element : *current->m_parent->m_value.object) - { - if (&element.second == current) - { - tokens.emplace_back(element.first.c_str()); - break; - } - } - break; - } - - default: // LCOV_EXCL_LINE - break; // LCOV_EXCL_LINE - } - } - - if (tokens.empty()) - { - return ""; // LCOV_EXCL_LINE - } - - return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, - [](const std::string & a, const std::string & b) - { - return a + "/" + detail::escape(b); - }) + ") "; -#else - return ""; -#endif - } - - private: - const BasicJsonType* m_j = nullptr; -}; - -} // namespace detail -} // namespace nlohmann - // #include @@ -2673,6 +2587,61 @@ class exception : public std::exception return "[json.exception." + ename + "." + std::to_string(id_) + "] "; } + template + static std::string diagnostics(const BasicJsonType& leaf_element) + { +#if JSON_DIAGNOSTICS + std::vector tokens; + for (const auto* current = &leaf_element; current->m_parent != nullptr; current = current->m_parent) + { + switch (current->m_parent->type()) + { + case value_t::array: + { + for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) + { + if (¤t->m_parent->m_value.array->operator[](i) == current) + { + tokens.emplace_back(std::to_string(i)); + break; + } + } + break; + } + + case value_t::object: + { + for (const auto& element : *current->m_parent->m_value.object) + { + if (&element.second == current) + { + tokens.emplace_back(element.first.c_str()); + break; + } + } + break; + } + + default: // LCOV_EXCL_LINE + break; // LCOV_EXCL_LINE + } + } + + if (tokens.empty()) + { + return ""; + } + + return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, + [](const std::string & a, const std::string & b) + { + return a + "/" + detail::escape(b); + }) + ") "; +#else + return ""; +#endif + } + private: /// an exception object as storage for error messages std::runtime_error m; @@ -2736,19 +2705,19 @@ class parse_error : public exception @return parse_error object */ template - static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const detail::diagnostics_t& diagnostics) + static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("parse_error", id_) + "parse error" + - position_string(pos) + ": " + diagnostics.diagnostics() + what_arg; + position_string(pos) + ": " + exception::diagnostics(context) + what_arg; return parse_error(id_, pos.chars_read_total, w.c_str()); } template - static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) + static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("parse_error", id_) + "parse error" + (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + - ": " + diagnostics.diagnostics() + what_arg; + ": " + exception::diagnostics(context) + what_arg; return parse_error(id_, byte_, w.c_str()); } @@ -2815,9 +2784,9 @@ class invalid_iterator : public exception { public: template - static invalid_iterator create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) + static invalid_iterator create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("invalid_iterator", id_) + diagnostics.diagnostics() + what_arg; + std::string w = exception::name("invalid_iterator", id_) + exception::diagnostics(context) + what_arg; return invalid_iterator(id_, w.c_str()); } @@ -2870,9 +2839,9 @@ class type_error : public exception { public: template - static type_error create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) + static type_error create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("type_error", id_) + diagnostics.diagnostics() + what_arg; + std::string w = exception::name("type_error", id_) + exception::diagnostics(context) + what_arg; return type_error(id_, w.c_str()); } @@ -2918,9 +2887,9 @@ class out_of_range : public exception { public: template - static out_of_range create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) + static out_of_range create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("out_of_range", id_) + diagnostics.diagnostics() + what_arg; + std::string w = exception::name("out_of_range", id_) + exception::diagnostics(context) + what_arg; return out_of_range(id_, w.c_str()); } @@ -2957,9 +2926,9 @@ class other_error : public exception { public: template - static other_error create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) + static other_error create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("other_error", id_) + diagnostics.diagnostics() + what_arg; + std::string w = exception::name("other_error", id_) + exception::diagnostics(context) + what_arg; return other_error(id_, w.c_str()); } @@ -2970,8 +2939,6 @@ class other_error : public exception } // namespace detail } // namespace nlohmann -// #include - // #include // #include @@ -3675,7 +3642,7 @@ void from_json(const BasicJsonType& j, typename std::nullptr_t& n) { if (JSON_HEDLEY_UNLIKELY(!j.is_null())) { - JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), j)); } n = nullptr; } @@ -3706,7 +3673,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j)); } } @@ -3715,7 +3682,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { if (JSON_HEDLEY_UNLIKELY(!j.is_boolean())) { - JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), j)); } b = *j.template get_ptr(); } @@ -3725,7 +3692,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j)); } s = *j.template get_ptr(); } @@ -3741,7 +3708,7 @@ void from_json(const BasicJsonType& j, ConstructibleStringType& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j)); } s = *j.template get_ptr(); @@ -3781,7 +3748,7 @@ void from_json(const BasicJsonType& j, std::forward_list& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } l.clear(); std::transform(j.rbegin(), j.rend(), @@ -3798,7 +3765,7 @@ void from_json(const BasicJsonType& j, std::valarray& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } l.resize(j.size()); std::transform(j.begin(), j.end(), std::begin(l), @@ -3889,7 +3856,7 @@ void()) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } from_json_array_impl(j, arr, priority_tag<3> {}); @@ -3900,7 +3867,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin) { if (JSON_HEDLEY_UNLIKELY(!j.is_binary())) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), j)); } bin = *j.template get_ptr(); @@ -3912,7 +3879,7 @@ void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) { if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), j)); } ConstructibleObjectType ret; @@ -3966,7 +3933,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j)); } } @@ -3995,14 +3962,14 @@ void from_json(const BasicJsonType& j, std::map& { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j)); } m.emplace(p.at(0).template get(), p.at(1).template get()); } @@ -4015,14 +3982,14 @@ void from_json(const BasicJsonType& j, std::unordered_map(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t(j))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j)); } m.emplace(p.at(0).template get(), p.at(1).template get()); } @@ -4835,8 +4802,6 @@ class byte_container_with_subtype : public BinaryType // #include -// #include - // #include @@ -4976,8 +4941,6 @@ std::size_t hash(const BasicJsonType& j) #include // make_pair, move #include // vector -// #include - // #include // #include @@ -5468,8 +5431,6 @@ class span_input_adapter #include // move #include // vector -// #include - // #include // #include @@ -5621,7 +5582,6 @@ class json_sax_dom_parser using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; using binary_t = typename BasicJsonType::binary_t; - using diagnostics_t = detail::diagnostics_t; /*! @param[in, out] r reference to a JSON value that is manipulated while @@ -5687,7 +5647,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), *ref_stack.back())); } return true; @@ -5713,7 +5673,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), *ref_stack.back())); } return true; @@ -5798,7 +5758,6 @@ class json_sax_dom_callback_parser using binary_t = typename BasicJsonType::binary_t; using parser_callback_t = typename BasicJsonType::parser_callback_t; using parse_event_t = typename BasicJsonType::parse_event_t; - using diagnostics_t = detail::diagnostics_t; json_sax_dom_callback_parser(BasicJsonType& r, const parser_callback_t cb, @@ -5869,7 +5828,7 @@ class json_sax_dom_callback_parser // check object limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), *ref_stack.back())); } return true; @@ -5939,7 +5898,7 @@ class json_sax_dom_callback_parser // check array limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), *ref_stack.back())); } return true; @@ -8003,7 +7962,6 @@ class binary_reader using json_sax_t = SAX; using char_type = typename InputAdapterType::char_type; using char_int_type = typename std::char_traits::int_type; - using diagnostics_t = detail::diagnostics_t; public: /*! @@ -8077,7 +8035,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current != std::char_traits::eof())) { return sax->parse_error(chars_read, get_token_string(), - parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"), diagnostics_t())); + parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"), BasicJsonType())); } } @@ -8153,7 +8111,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 1)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"), BasicJsonType())); } return get_string(input_format_t::bson, len - static_cast(1), result) && get() != std::char_traits::eof(); @@ -8174,7 +8132,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 0)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"), BasicJsonType())); } // All BSON binary values have a subtype @@ -8256,7 +8214,7 @@ class binary_reader { std::array cr{{}}; (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type)); - return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), diagnostics_t())); + return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), BasicJsonType())); } } } @@ -8656,7 +8614,7 @@ class binary_reader case cbor_tag_handler_t::error: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); } case cbor_tag_handler_t::ignore: @@ -8771,7 +8729,7 @@ class binary_reader default: // anything else (0xFF is handled inside the other types) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); } } } @@ -8866,7 +8824,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"), BasicJsonType())); } } } @@ -8965,7 +8923,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"), BasicJsonType())); } } } @@ -9432,7 +9390,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); } } } @@ -9514,7 +9472,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"), BasicJsonType())); } } } @@ -9764,7 +9722,7 @@ class binary_reader default: auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), BasicJsonType())); } } @@ -9834,7 +9792,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), BasicJsonType())); } } } @@ -9872,7 +9830,7 @@ class binary_reader return false; } auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"), BasicJsonType())); } return get_ubjson_size_value(result.first); @@ -9962,7 +9920,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current > 127)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"), BasicJsonType())); } string_t s(1, static_cast(current)); return sax->string(s); @@ -9983,7 +9941,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); } } } @@ -10161,7 +10119,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input)) { - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), diagnostics_t())); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), BasicJsonType())); } switch (result_number) @@ -10173,7 +10131,7 @@ class binary_reader case token_type::value_float: return sax->number_float(number_lexer.get_number_float(), std::move(number_string)); default: - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), diagnostics_t())); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), BasicJsonType())); } } @@ -10329,7 +10287,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current == std::char_traits::eof())) { return sax->parse_error(chars_read, "", - parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), diagnostics_t())); + parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), BasicJsonType())); } return true; } @@ -10416,8 +10374,6 @@ class binary_reader // #include -// #include - // #include // #include @@ -10473,7 +10429,6 @@ class parser using string_t = typename BasicJsonType::string_t; using lexer_t = lexer; using token_type = typename lexer_t::token_type; - using diagnostics_t = detail::diagnostics_t; public: /// a parser reading from an input adapter @@ -10512,7 +10467,7 @@ class parser sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"), diagnostics_t())); + exception_message(token_type::end_of_input, "value"), BasicJsonType())); } // in case of an error, return discarded value @@ -10539,8 +10494,7 @@ class parser { sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType())); } // in case of an error, return discarded value @@ -10578,8 +10532,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType())); } return result; @@ -10625,8 +10578,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) { @@ -10638,8 +10590,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType())); } // remember we are now inside an object @@ -10682,7 +10633,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'", diagnostics_t())); + out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'", BasicJsonType())); } if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string()))) @@ -10752,16 +10703,14 @@ class parser // using "uninitialized" to avoid "expected" message return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::uninitialized, "value"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), BasicJsonType())); } default: // the last token was unexpected { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::literal_or_value, "value"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), BasicJsonType())); } } } @@ -10807,8 +10756,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_array, "array"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), BasicJsonType())); } else // object { @@ -10820,8 +10768,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) @@ -10834,8 +10781,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType())); } // parse values @@ -10863,8 +10809,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_object, "object"), diagnostics_t())); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), BasicJsonType())); } } } @@ -11075,8 +11020,6 @@ template struct internal_iterator #include // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next #include // conditional, is_const, remove_const -// #include - // #include // #include @@ -11132,7 +11075,6 @@ class iter_impl // make sure BasicJsonType is basic_json or const basic_json static_assert(is_basic_json::type>::value, "iter_impl only accepts (const) basic_json"); - using diagnostics_t = detail::diagnostics_t; public: @@ -11339,7 +11281,7 @@ class iter_impl } case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); default: { @@ -11348,7 +11290,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); } } } @@ -11382,7 +11324,7 @@ class iter_impl return m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); } } } @@ -11483,7 +11425,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object)); } JSON_ASSERT(m_object != nullptr); @@ -11520,7 +11462,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object)); } JSON_ASSERT(m_object != nullptr); @@ -11528,7 +11470,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", *m_object)); case value_t::array: return (m_it.array_iterator < other.m_it.array_iterator); @@ -11576,7 +11518,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object)); case value_t::array: { @@ -11647,7 +11589,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object)); case value_t::array: return m_it.array_iterator - other.m_it.array_iterator; @@ -11668,13 +11610,13 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", *m_object)); case value_t::array: return *std::next(m_it.array_iterator, n); case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); default: { @@ -11683,7 +11625,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); } } } @@ -11701,7 +11643,7 @@ class iter_impl return m_it.object_iterator->first; } - JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", diagnostics_t(*m_object))); + JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", *m_object)); } /*! @@ -11858,8 +11800,6 @@ class json_reverse_iterator : public std::reverse_iterator #include // move #include // vector -// #include - // #include // #include @@ -11878,8 +11818,6 @@ class json_pointer NLOHMANN_BASIC_JSON_TPL_DECLARATION friend class basic_json; - using diagnostics_t = detail::diagnostics_t; - public: /*! @brief create JSON pointer @@ -12106,7 +12044,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); } reference_tokens.pop_back(); @@ -12130,7 +12068,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); } return reference_tokens.back(); @@ -12196,13 +12134,13 @@ class json_pointer // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) { - JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", diagnostics_t())); + JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", BasicJsonType())); } // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9'))) { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", diagnostics_t())); + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", BasicJsonType())); } std::size_t processed_chars = 0; @@ -12213,20 +12151,20 @@ class json_pointer } JSON_CATCH(std::out_of_range&) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", diagnostics_t())); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType())); } // check if the string was completely read if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size())) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", diagnostics_t())); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType())); } // only triggered on special platforms (like 32bit), see also // https://github.com/nlohmann/json/pull/2203 if (res >= static_cast((std::numeric_limits::max)())) { - JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", diagnostics_t())); // LCOV_EXCL_LINE + JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", BasicJsonType())); // LCOV_EXCL_LINE } return static_cast(res); @@ -12237,7 +12175,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); } json_pointer result = *this; @@ -12300,7 +12238,7 @@ class json_pointer single value; that is, with an empty list of reference tokens. */ default: - JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", diagnostics_t(j))); + JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", j)); } } @@ -12372,7 +12310,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); } } @@ -12405,7 +12343,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range", diagnostics_t(*ptr))); + ") is out of range", *ptr)); } // note: at performs range check @@ -12414,7 +12352,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); } } @@ -12452,7 +12390,7 @@ class json_pointer if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) { // "-" cannot be used for const access - JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", diagnostics_t(*ptr))); + JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", *ptr)); } // use unchecked array access @@ -12461,7 +12399,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); } } @@ -12494,7 +12432,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range", diagnostics_t(*ptr))); + ") is out of range", *ptr)); } // note: at performs range check @@ -12503,7 +12441,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); } } @@ -12607,7 +12545,7 @@ class json_pointer // check if nonempty reference string begins with slash if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) { - JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", diagnostics_t())); + JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", BasicJsonType())); } // extract the reference tokens: @@ -12642,7 +12580,7 @@ class json_pointer (reference_token[pos + 1] != '0' && reference_token[pos + 1] != '1'))) { - JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", diagnostics_t())); + JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", BasicJsonType())); } } @@ -12729,7 +12667,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!value.is_object())) { - JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", diagnostics_t(value))); + JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", value)); } BasicJsonType result; @@ -12739,7 +12677,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive())) { - JSON_THROW(detail::type_error::create(315, "values in object must be primitive", diagnostics_t(element.second))); + JSON_THROW(detail::type_error::create(315, "values in object must be primitive", element.second)); } // assign value to reference pointed to by JSON pointer; Note that if @@ -12879,8 +12817,6 @@ class json_ref #include // string #include // isnan, isinf -// #include - // #include // #include @@ -13029,7 +12965,6 @@ class binary_writer using string_t = typename BasicJsonType::string_t; using binary_t = typename BasicJsonType::binary_t; using number_float_t = typename BasicJsonType::number_float_t; - using diagnostics_t = detail::diagnostics_t; public: /*! @@ -13058,7 +12993,7 @@ class binary_writer default: { - JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), diagnostics_t(j)));; + JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), j));; } } } @@ -13907,7 +13842,7 @@ class binary_writer const auto it = name.find(static_cast(0)); if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) { - JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", diagnostics_t(j))); + JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", j)); } return /*id*/ 1ul + name.size() + /*zero-terminator*/1u; @@ -14031,7 +13966,7 @@ class binary_writer } else { - JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", diagnostics_t(j))); + JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", j)); } } @@ -15719,8 +15654,6 @@ char* to_chars(char* first, const char* last, FloatType value) } // namespace detail } // namespace nlohmann -// #include - // #include // #include @@ -15758,7 +15691,6 @@ class serializer using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using binary_char_t = typename BasicJsonType::binary_t::value_type; - using diagnostics_t = detail::diagnostics_t; static constexpr std::uint8_t UTF8_ACCEPT = 0; static constexpr std::uint8_t UTF8_REJECT = 1; @@ -16214,7 +16146,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, diagnostics_t())); + JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, BasicJsonType())); } case error_handler_t::ignore: @@ -16308,7 +16240,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, diagnostics_t())); + JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, BasicJsonType())); } case error_handler_t::ignore: @@ -16973,15 +16905,11 @@ class basic_json friend class ::nlohmann::detail::json_sax_dom_parser; template friend class ::nlohmann::detail::json_sax_dom_callback_parser; - template - friend class ::nlohmann::detail::diagnostics_t; + friend class ::nlohmann::detail::exception; /// workaround type for MSVC using basic_json_t = NLOHMANN_BASIC_JSON_TPL; - /// shortcut - using diagnostics_t = ::nlohmann::detail::diagnostics_t; - JSON_PRIVATE_UNLESS_TESTED: // convenience aliases for types residing in namespace detail; using lexer = ::nlohmann::detail::lexer_base; @@ -17849,7 +17777,7 @@ class basic_json object = nullptr; // silence warning, see #821 if (JSON_HEDLEY_UNLIKELY(t == value_t::null)) { - JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1", diagnostics_t())); // LCOV_EXCL_LINE + JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1", basic_json())); // LCOV_EXCL_LINE } break; } @@ -18490,7 +18418,7 @@ class basic_json // if object is wanted but impossible, throw an exception if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object)) { - JSON_THROW(type_error::create(301, "cannot create object from initializer list", diagnostics_t())); + JSON_THROW(type_error::create(301, "cannot create object from initializer list", basic_json())); } } @@ -18795,7 +18723,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", diagnostics_t())); + JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", basic_json())); } // copy type from first iterator @@ -18813,7 +18741,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t())); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", *first.m_object)); } break; } @@ -18875,7 +18803,7 @@ class basic_json } default: - JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), diagnostics_t())); + JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), *first.m_object)); } set_parents(); @@ -19574,7 +19502,7 @@ class basic_json return m_value.boolean; } - JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()), *this)); } /// get a pointer to the value (object) @@ -19695,7 +19623,7 @@ class basic_json return *ptr; } - JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()), diagnostics_t(obj))); + JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()), obj)); } public: @@ -20123,7 +20051,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), *this)); } return *get_ptr(); @@ -20134,7 +20062,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), *this)); } return *get_ptr(); @@ -20189,12 +20117,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this)); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); } } @@ -20236,12 +20164,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this)); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); } } @@ -20287,12 +20215,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this))); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this)); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); } } @@ -20338,12 +20266,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this))); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this)); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); } } @@ -20403,7 +20331,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), *this)); } /*! @@ -20433,7 +20361,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), *this)); } /*! @@ -20479,7 +20407,7 @@ class basic_json return set_parent(m_value.object->operator[](key)); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } /*! @@ -20521,7 +20449,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } /*! @@ -20569,7 +20497,7 @@ class basic_json return set_parent(m_value.object->operator[](key)); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } /*! @@ -20613,7 +20541,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } /*! @@ -20685,7 +20613,7 @@ class basic_json return default_value; } - JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), *this)); } /*! @@ -20758,7 +20686,7 @@ class basic_json } } - JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), *this)); } /*! @@ -20912,7 +20840,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != pos.m_object)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } IteratorType result = end(); @@ -20928,7 +20856,7 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin())) { - JSON_THROW(invalid_iterator::create(205, "iterator out of range", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(205, "iterator out of range", *this)); } if (is_string()) @@ -20964,7 +20892,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); } return result; @@ -21025,7 +20953,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object)) { - JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", *this)); } IteratorType result = end(); @@ -21042,7 +20970,7 @@ class basic_json if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", *this)); } if (is_string()) @@ -21080,7 +21008,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); } return result; @@ -21123,7 +21051,7 @@ class basic_json return m_value.object->erase(key); } - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); } /*! @@ -21157,14 +21085,14 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(idx >= size())) { - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this)); } m_value.array->erase(m_value.array->begin() + static_cast(idx)); } else { - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); } } @@ -22109,7 +22037,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this)); } // transform null object into an array @@ -22145,7 +22073,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this)); } // transform null object into an array @@ -22196,7 +22124,7 @@ class basic_json // push_back only works for null objects or objects if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this)); } // transform null object into an object @@ -22300,7 +22228,7 @@ class basic_json // emplace_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), *this)); } // transform null object into an array @@ -22353,7 +22281,7 @@ class basic_json // emplace only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), *this)); } // transform null object into an object @@ -22426,14 +22354,14 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } // insert to array and return iterator return set_parents(insert_iterator(pos, val), static_cast(1)); } - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } /*! @@ -22477,14 +22405,14 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } // insert to array and return iterator return set_parents(insert_iterator(pos, cnt, val), static_cast(cnt)); } - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } /*! @@ -22522,24 +22450,24 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this)); } if (JSON_HEDLEY_UNLIKELY(first.m_object == this)) { - JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", *this)); } // insert to array and return iterator @@ -22575,13 +22503,13 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } // insert to array and return iterator @@ -22616,19 +22544,19 @@ class basic_json // insert only works for objects if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this)); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this)); } m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator); @@ -22665,11 +22593,11 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), *this)); } if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()), *this)); } for (auto it = j.cbegin(); it != j.cend(); ++it) @@ -22716,20 +22644,20 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), *this)); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this)); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object() || !last.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this))); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this)); } for (auto it = first; it != last; ++it) @@ -22827,7 +22755,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -22860,7 +22788,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -22893,7 +22821,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -22926,7 +22854,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -22940,7 +22868,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -25149,7 +25077,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(idx > parent.size())) { // avoid undefined behavior - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(parent))); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", parent)); } // default case: insert add offset @@ -25183,7 +25111,7 @@ class basic_json } else { - JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found", diagnostics_t(*this))); + JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found", *this)); } } else if (parent.is_array()) @@ -25196,7 +25124,7 @@ class basic_json // type check: top level value must be an array if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array())) { - JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(json_patch))); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", json_patch)); } // iterate and apply the operations @@ -25216,13 +25144,13 @@ class basic_json // check if desired value is present if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end())) { - JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", diagnostics_t(val))); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", val)); } // check if result is of type string if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string())) { - JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", diagnostics_t(val))); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", val)); } // no error: return value @@ -25232,7 +25160,7 @@ class basic_json // type check: every element of the array must be an object if (JSON_HEDLEY_UNLIKELY(!val.is_object())) { - JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(val))); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", val)); } // collect mandatory members @@ -25310,7 +25238,7 @@ class basic_json // throw an exception if test fails if (JSON_HEDLEY_UNLIKELY(!success)) { - JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump(), diagnostics_t(val))); + JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump(), val)); } break; @@ -25320,7 +25248,7 @@ class basic_json { // op must be "add", "remove", "replace", "move", "copy", or // "test" - JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid", diagnostics_t(val))); + JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid", val)); } } } From ffdeb77468040576f7fcd1a0ba932725295a22ea Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 27 Jan 2021 12:54:46 +0100 Subject: [PATCH 058/143] :rotating_light: fix warnings #2615 --- .../nlohmann/detail/conversions/to_chars.hpp | 24 ++-- .../nlohmann/detail/input/input_adapters.hpp | 6 +- include/nlohmann/detail/input/parser.hpp | 92 ++++++------- .../detail/iterators/iteration_proxy.hpp | 2 +- single_include/nlohmann/json.hpp | 124 +++++++++--------- 5 files changed, 120 insertions(+), 128 deletions(-) diff --git a/include/nlohmann/detail/conversions/to_chars.hpp b/include/nlohmann/detail/conversions/to_chars.hpp index c632ff2bea..49ed0f913e 100644 --- a/include/nlohmann/detail/conversions/to_chars.hpp +++ b/include/nlohmann/detail/conversions/to_chars.hpp @@ -490,51 +490,49 @@ inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10) return 10; } // LCOV_EXCL_STOP - else if (n >= 100000000) + if (n >= 100000000) { pow10 = 100000000; return 9; } - else if (n >= 10000000) + if (n >= 10000000) { pow10 = 10000000; return 8; } - else if (n >= 1000000) + if (n >= 1000000) { pow10 = 1000000; return 7; } - else if (n >= 100000) + if (n >= 100000) { pow10 = 100000; return 6; } - else if (n >= 10000) + if (n >= 10000) { pow10 = 10000; return 5; } - else if (n >= 1000) + if (n >= 1000) { pow10 = 1000; return 4; } - else if (n >= 100) + if (n >= 100) { pow10 = 100; return 3; } - else if (n >= 10) + if (n >= 10) { pow10 = 10; return 2; } - else - { - pow10 = 1; - return 1; - } + + pow10 = 1; + return 1; } inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta, diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index a78a6ec96c..c437564fd0 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -135,10 +135,8 @@ class iterator_input_adapter std::advance(current, 1); return result; } - else - { - return std::char_traits::eof(); - } + + return std::char_traits::eof(); } private: diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index ffe483aa1e..74283cd125 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -393,62 +393,62 @@ class parser parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"))); } - else // object - { - // comma -> next value - if (get_token() == token_type::value_separator) - { - // parse key - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); - } - if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) - { - return false; - } + // states.back() is false -> object - // parse separator (:) - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); - } + // comma -> next value + if (get_token() == token_type::value_separator) + { + // parse key + if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string)) + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::value_string, "object key"))); + } - // parse values - get_token(); - continue; + if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) + { + return false; } - // closing } - if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object)) + // parse separator (:) + if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) { - if (JSON_HEDLEY_UNLIKELY(!sax->end_object())) - { - return false; - } + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::name_separator, "object separator"))); + } - // We are done with this object. Before we can parse a - // new value, we need to evaluate the new state first. - // By setting skip_to_state_evaluation to false, we - // are effectively jumping to the beginning of this if. - JSON_ASSERT(!states.empty()); - states.pop_back(); - skip_to_state_evaluation = true; - continue; + // parse values + get_token(); + continue; + } + + // closing } + if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object)) + { + if (JSON_HEDLEY_UNLIKELY(!sax->end_object())) + { + return false; } - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_object, "object"))); + // We are done with this object. Before we can parse a + // new value, we need to evaluate the new state first. + // By setting skip_to_state_evaluation to false, we + // are effectively jumping to the beginning of this if. + JSON_ASSERT(!states.empty()); + states.pop_back(); + skip_to_state_evaluation = true; + continue; } + + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::end_object, "object"))); } } diff --git a/include/nlohmann/detail/iterators/iteration_proxy.hpp b/include/nlohmann/detail/iterators/iteration_proxy.hpp index 74b4eb347f..3e181d5d92 100644 --- a/include/nlohmann/detail/iterators/iteration_proxy.hpp +++ b/include/nlohmann/detail/iterators/iteration_proxy.hpp @@ -39,7 +39,7 @@ template class iteration_proxy_value /// a string representation of the array index mutable string_type array_index_str = "0"; /// an empty string (to return a reference for primitive values) - const string_type empty_str = ""; + const string_type empty_str; public: explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {} diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 492118a5f8..03579b1897 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3944,7 +3944,7 @@ template class iteration_proxy_value /// a string representation of the array index mutable string_type array_index_str = "0"; /// an empty string (to return a reference for primitive values) - const string_type empty_str = ""; + const string_type empty_str; public: explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {} @@ -4947,10 +4947,8 @@ class iterator_input_adapter std::advance(current, 1); return result; } - else - { - return std::char_traits::eof(); - } + + return std::char_traits::eof(); } private: @@ -10618,62 +10616,62 @@ class parser parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"))); } - else // object - { - // comma -> next value - if (get_token() == token_type::value_separator) - { - // parse key - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); - } - if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) - { - return false; - } + // states.back() is false -> object - // parse separator (:) - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); - } + // comma -> next value + if (get_token() == token_type::value_separator) + { + // parse key + if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string)) + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::value_string, "object key"))); + } - // parse values - get_token(); - continue; + if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) + { + return false; } - // closing } - if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object)) + // parse separator (:) + if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) { - if (JSON_HEDLEY_UNLIKELY(!sax->end_object())) - { - return false; - } + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::name_separator, "object separator"))); + } - // We are done with this object. Before we can parse a - // new value, we need to evaluate the new state first. - // By setting skip_to_state_evaluation to false, we - // are effectively jumping to the beginning of this if. - JSON_ASSERT(!states.empty()); - states.pop_back(); - skip_to_state_evaluation = true; - continue; + // parse values + get_token(); + continue; + } + + // closing } + if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object)) + { + if (JSON_HEDLEY_UNLIKELY(!sax->end_object())) + { + return false; } - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_object, "object"))); + // We are done with this object. Before we can parse a + // new value, we need to evaluate the new state first. + // By setting skip_to_state_evaluation to false, we + // are effectively jumping to the beginning of this if. + JSON_ASSERT(!states.empty()); + states.pop_back(); + skip_to_state_evaluation = true; + continue; } + + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), + exception_message(token_type::end_object, "object"))); } } @@ -14948,51 +14946,49 @@ inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10) return 10; } // LCOV_EXCL_STOP - else if (n >= 100000000) + if (n >= 100000000) { pow10 = 100000000; return 9; } - else if (n >= 10000000) + if (n >= 10000000) { pow10 = 10000000; return 8; } - else if (n >= 1000000) + if (n >= 1000000) { pow10 = 1000000; return 7; } - else if (n >= 100000) + if (n >= 100000) { pow10 = 100000; return 6; } - else if (n >= 10000) + if (n >= 10000) { pow10 = 10000; return 5; } - else if (n >= 1000) + if (n >= 1000) { pow10 = 1000; return 4; } - else if (n >= 100) + if (n >= 100) { pow10 = 100; return 3; } - else if (n >= 10) + if (n >= 10) { pow10 = 10; return 2; } - else - { - pow10 = 1; - return 1; - } + + pow10 = 1; + return 1; } inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta, From 5b0c804630cc6dd0a40dd2b4cbc7dd1026f6326a Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 27 Jan 2021 14:22:58 +0100 Subject: [PATCH 059/143] :bug: properly assign two labels to one test case #2596 --- test/cmake_fetch_content/CMakeLists.txt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/cmake_fetch_content/CMakeLists.txt b/test/cmake_fetch_content/CMakeLists.txt index f850b8bd78..7df00865e6 100644 --- a/test/cmake_fetch_content/CMakeLists.txt +++ b/test/cmake_fetch_content/CMakeLists.txt @@ -11,12 +11,10 @@ if (${CMAKE_VERSION} VERSION_GREATER "3.11.0") ) set_tests_properties(cmake_fetch_content_configure PROPERTIES FIXTURES_SETUP cmake_fetch_content - LABELS git_required - LABELS not_reproducible + LABELS "git_required;not_reproducible" ) set_tests_properties(cmake_fetch_content_build PROPERTIES FIXTURES_REQUIRED cmake_fetch_content - LABELS git_required - LABELS not_reproducible + LABELS "git_required;not_reproducible" ) endif() From 9f6b78ee5a9e4e2e7d55dd65f6f3ba127e33f4d1 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 27 Jan 2021 14:34:10 +0100 Subject: [PATCH 060/143] :memo: add documentation --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index eb56d6c6ff..076737245e 100644 --- a/README.md +++ b/README.md @@ -1638,4 +1638,6 @@ In case you have downloaded the library rather than checked out the code via Git Some tests change the installed files and hence make the whole process not reproducible. Please execute `ctest -LE not_reproducible` to skip these tests. See [issue #2324](https://github.com/nlohmann/json/issues/2324) for more information. +Note you need to call `cmake -LE "not_reproducible|git_required"` to exclude both labels. See [issue #2596](https://github.com/nlohmann/json/issues/2596) for more information. + As Intel compilers use unsafe floating point optimization by default, the unit tests may fail. Use flag [`/fp:precise`](https://software.intel.com/content/www/us/en/develop/documentation/cpp-compiler-developer-guide-and-reference/top/compiler-reference/compiler-options/compiler-option-details/floating-point-options/fp-model-fp.html) then. From 6d4eed5aeb59a3f40831edacf8693e53a543e832 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 30 Jan 2021 12:51:54 +0100 Subject: [PATCH 061/143] :rotating_light: fix warning --- include/nlohmann/detail/iterators/iteration_proxy.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/iterators/iteration_proxy.hpp b/include/nlohmann/detail/iterators/iteration_proxy.hpp index 3e181d5d92..1b47faeb3e 100644 --- a/include/nlohmann/detail/iterators/iteration_proxy.hpp +++ b/include/nlohmann/detail/iterators/iteration_proxy.hpp @@ -39,7 +39,7 @@ template class iteration_proxy_value /// a string representation of the array index mutable string_type array_index_str = "0"; /// an empty string (to return a reference for primitive values) - const string_type empty_str; + const string_type empty_str{}; public: explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {} diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 03579b1897..30dd96f43e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3944,7 +3944,7 @@ template class iteration_proxy_value /// a string representation of the array index mutable string_type array_index_str = "0"; /// an empty string (to return a reference for primitive values) - const string_type empty_str; + const string_type empty_str{}; public: explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {} From 567e2e3412afea14b3fb7535c0c75179235c0832 Mon Sep 17 00:00:00 2001 From: "William A. Wieselquist" Date: Sat, 6 Feb 2021 14:48:12 -0500 Subject: [PATCH 062/143] Fix missing 1.78 in example in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 076737245e..5d354f1ed1 100644 --- a/README.md +++ b/README.md @@ -577,7 +577,7 @@ j[1] = 42; bool foo = j.at(2); // comparison -j == "[\"foo\", 42, true]"_json; // true +j == "[\"foo\", 42, true, 1.78]"_json; // true // other stuff j.size(); // 3 entries From 56a6dec0de4cb555d564ac2937b26bc58404cbdc Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 7 Feb 2021 17:46:11 +0100 Subject: [PATCH 063/143] :twisted_rightwards_arrows: merge develop branch --- include/nlohmann/detail/input/parser.hpp | 34 +- single_include/nlohmann/json.hpp | 926 ++++++++++++++--------- 2 files changed, 575 insertions(+), 385 deletions(-) diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index 6ebc095bfb..7b5d494f85 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -389,15 +389,15 @@ class parser // states.back() is false -> object - // comma -> next value - if (get_token() == token_type::value_separator) + // comma -> next value + if (get_token() == token_type::value_separator) + { + // parse key + if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string)) { - // parse key - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType())); + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) @@ -405,12 +405,12 @@ class parser return false; } - // parse separator (:) - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType())); + // parse separator (:) + if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) + { + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType())); } // parse values @@ -436,9 +436,9 @@ class parser continue; } - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), BasicJsonType())); + return sax->parse_error(m_lexer.get_position(), + m_lexer.get_token_string(), + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), BasicJsonType())); } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 30dd96f43e..605cc699aa 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -73,35 +73,93 @@ SOFTWARE. #include // runtime_error #include // to_string -// #include +// #include +#include // array #include // size_t +#include // uint8_t +#include // string 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; +/////////////////////////// +// JSON type enumeration // +/////////////////////////// - /// conversion to size_t to preserve SAX interface - constexpr operator size_t() const - { - return chars_read_total; - } +/*! +@brief the JSON type enumeration + +This enumeration collects the different JSON types. It is internally used to +distinguish the stored values, and the functions @ref basic_json::is_null(), +@ref basic_json::is_object(), @ref basic_json::is_array(), +@ref basic_json::is_string(), @ref basic_json::is_boolean(), +@ref basic_json::is_number() (with @ref basic_json::is_number_integer(), +@ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()), +@ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and +@ref basic_json::is_structured() rely on it. + +@note There are three enumeration entries (number_integer, number_unsigned, and +number_float), because the library distinguishes these three types for numbers: +@ref basic_json::number_unsigned_t is used for unsigned integers, +@ref basic_json::number_integer_t is used for signed integers, and +@ref basic_json::number_float_t is used for floating-point numbers or to +approximate integers which do not fit in the limits of their respective type. + +@sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON +value with the default value for a given type + +@since version 1.0.0 +*/ +enum class value_t : std::uint8_t +{ + null, ///< null value + object, ///< object (unordered set of name/value pairs) + array, ///< array (ordered collection of values) + string, ///< string value + boolean, ///< boolean value + number_integer, ///< number value (signed integer) + number_unsigned, ///< number value (unsigned integer) + number_float, ///< number value (floating-point) + binary, ///< binary array (ordered collection of bytes) + discarded ///< discarded by the parser callback function }; -} // namespace detail -} // namespace nlohmann +/*! +@brief comparison operator for JSON types +Returns an ordering that is similar to Python: +- order: null < boolean < number < object < array < string < binary +- furthermore, each type is not smaller than itself +- discarded values are not comparable +- binary is represented as a b"" string in python and directly comparable to a + string; however, making a binary array directly comparable with a string would + be surprising behavior in a JSON file. + +@since version 1.0.0 +*/ +inline bool operator<(const value_t lhs, const value_t rhs) noexcept +{ + static constexpr std::array order = {{ + 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */, + 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */, + 6 /* binary */ + } + }; + + const auto l_index = static_cast(lhs); + const auto r_index = static_cast(rhs); + return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index]; +} +} // namespace detail +} // namespace nlohmann + +// #include + + +#include // #include @@ -2380,6 +2438,97 @@ JSON_HEDLEY_DIAGNOSTIC_POP #endif +namespace nlohmann +{ +namespace detail +{ + +/*! +@brief replace all occurrences of a substring by another string + +@param[in,out] s the string to manipulate; changed so that all + occurrences of @a f are replaced with @a t +@param[in] f the substring to replace with @a t +@param[in] t the string to replace @a f + +@pre The search string @a f must not be empty. **This precondition is +enforced with an assertion.** + +@since version 2.0.0 +*/ +inline void replace_substring(std::string& s, const std::string& f, + const std::string& t) +{ + JSON_ASSERT(!f.empty()); + for (auto pos = s.find(f); // find first occurrence of f + pos != std::string::npos; // make sure f was found + s.replace(pos, f.size(), t), // replace with t, and + pos = s.find(f, pos + t.size())) // find next occurrence of f + {} +} + +/*! + * @brief string escaping as described in RFC 6901 (Sect. 4) + * @param[in] s string to escape + * @return escaped string + * + * Note the order of escaping "~" to "~0" and "/" to "~1" is important. + */ +inline std::string escape(std::string s) +{ + replace_substring(s, "~", "~0"); + replace_substring(s, "/", "~1"); + return s; +} + +/*! + * @brief string unescaping as described in RFC 6901 (Sect. 4) + * @param[in] s string to unescape + * @return unescaped string + * + * Note the order of escaping "~1" to "/" and "~0" to "~" is important. + */ +static void unescape(std::string& s) +{ + replace_substring(s, "~1", "/"); + replace_substring(s, "~0", "~"); +} + +} // namespace detail +} // namespace nlohmann + +// #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 @@ -2438,6 +2587,61 @@ class exception : public std::exception return "[json.exception." + ename + "." + std::to_string(id_) + "] "; } + template + static std::string diagnostics(const BasicJsonType& leaf_element) + { +#if JSON_DIAGNOSTICS + std::vector tokens; + for (const auto* current = &leaf_element; current->m_parent != nullptr; current = current->m_parent) + { + switch (current->m_parent->type()) + { + case value_t::array: + { + for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) + { + if (¤t->m_parent->m_value.array->operator[](i) == current) + { + tokens.emplace_back(std::to_string(i)); + break; + } + } + break; + } + + case value_t::object: + { + for (const auto& element : *current->m_parent->m_value.object) + { + if (&element.second == current) + { + tokens.emplace_back(element.first.c_str()); + break; + } + } + break; + } + + default: // LCOV_EXCL_LINE + break; // LCOV_EXCL_LINE + } + } + + if (tokens.empty()) + { + return ""; + } + + return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, + [](const std::string & a, const std::string & b) + { + return a + "/" + detail::escape(b); + }) + ") "; +#else + return ""; +#endif + } + private: /// an exception object as storage for error messages std::runtime_error m; @@ -2500,18 +2704,20 @@ class parse_error : public exception @param[in] what_arg the explanatory string @return parse_error object */ - static parse_error create(int id_, const position_t& pos, const std::string& what_arg) + template + static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("parse_error", id_) + "parse error" + - position_string(pos) + ": " + what_arg; + position_string(pos) + ": " + exception::diagnostics(context) + what_arg; return parse_error(id_, pos.chars_read_total, w.c_str()); } - static parse_error create(int id_, std::size_t byte_, const std::string& what_arg) + template + static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const BasicJsonType& context) { std::string w = exception::name("parse_error", id_) + "parse error" + (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + - ": " + what_arg; + ": " + exception::diagnostics(context) + what_arg; return parse_error(id_, byte_, w.c_str()); } @@ -2577,9 +2783,10 @@ caught.,invalid_iterator} class invalid_iterator : public exception { public: - static invalid_iterator create(int id_, const std::string& what_arg) + template + static invalid_iterator create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("invalid_iterator", id_) + what_arg; + std::string w = exception::name("invalid_iterator", id_) + exception::diagnostics(context) + what_arg; return invalid_iterator(id_, w.c_str()); } @@ -2631,9 +2838,10 @@ caught.,type_error} class type_error : public exception { public: - static type_error create(int id_, const std::string& what_arg) + template + static type_error create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("type_error", id_) + what_arg; + std::string w = exception::name("type_error", id_) + exception::diagnostics(context) + what_arg; return type_error(id_, w.c_str()); } @@ -2678,9 +2886,10 @@ caught.,out_of_range} class out_of_range : public exception { public: - static out_of_range create(int id_, const std::string& what_arg) + template + static out_of_range create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("out_of_range", id_) + what_arg; + std::string w = exception::name("out_of_range", id_) + exception::diagnostics(context) + what_arg; return out_of_range(id_, w.c_str()); } @@ -2716,9 +2925,10 @@ caught.,other_error} class other_error : public exception { public: - static other_error create(int id_, const std::string& what_arg) + template + static other_error create(int id_, const std::string& what_arg, const BasicJsonType& context) { - std::string w = exception::name("other_error", id_) + what_arg; + std::string w = exception::name("other_error", id_) + exception::diagnostics(context) + what_arg; return other_error(id_, w.c_str()); } @@ -3423,87 +3633,6 @@ struct is_constructible_tuple> : conjunction -#include // array -#include // size_t -#include // uint8_t -#include // string - -namespace nlohmann -{ -namespace detail -{ -/////////////////////////// -// JSON type enumeration // -/////////////////////////// - -/*! -@brief the JSON type enumeration - -This enumeration collects the different JSON types. It is internally used to -distinguish the stored values, and the functions @ref basic_json::is_null(), -@ref basic_json::is_object(), @ref basic_json::is_array(), -@ref basic_json::is_string(), @ref basic_json::is_boolean(), -@ref basic_json::is_number() (with @ref basic_json::is_number_integer(), -@ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()), -@ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and -@ref basic_json::is_structured() rely on it. - -@note There are three enumeration entries (number_integer, number_unsigned, and -number_float), because the library distinguishes these three types for numbers: -@ref basic_json::number_unsigned_t is used for unsigned integers, -@ref basic_json::number_integer_t is used for signed integers, and -@ref basic_json::number_float_t is used for floating-point numbers or to -approximate integers which do not fit in the limits of their respective type. - -@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON -value with the default value for a given type - -@since version 1.0.0 -*/ -enum class value_t : std::uint8_t -{ - null, ///< null value - object, ///< object (unordered set of name/value pairs) - array, ///< array (ordered collection of values) - string, ///< string value - boolean, ///< boolean value - number_integer, ///< number value (signed integer) - number_unsigned, ///< number value (unsigned integer) - number_float, ///< number value (floating-point) - binary, ///< binary array (ordered collection of bytes) - discarded ///< discarded by the parser callback function -}; - -/*! -@brief comparison operator for JSON types - -Returns an ordering that is similar to Python: -- order: null < boolean < number < object < array < string < binary -- furthermore, each type is not smaller than itself -- discarded values are not comparable -- binary is represented as a b"" string in python and directly comparable to a - string; however, making a binary array directly comparable with a string would - be surprising behavior in a JSON file. - -@since version 1.0.0 -*/ -inline bool operator<(const value_t lhs, const value_t rhs) noexcept -{ - static constexpr std::array order = {{ - 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */, - 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */, - 6 /* binary */ - } - }; - - const auto l_index = static_cast(lhs); - const auto r_index = static_cast(rhs); - return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index]; -} -} // namespace detail -} // namespace nlohmann - - namespace nlohmann { namespace detail @@ -3513,7 +3642,7 @@ void from_json(const BasicJsonType& j, typename std::nullptr_t& n) { if (JSON_HEDLEY_UNLIKELY(!j.is_null())) { - JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), j)); } n = nullptr; } @@ -3544,7 +3673,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j)); } } @@ -3553,7 +3682,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { if (JSON_HEDLEY_UNLIKELY(!j.is_boolean())) { - JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), j)); } b = *j.template get_ptr(); } @@ -3563,7 +3692,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j)); } s = *j.template get_ptr(); } @@ -3579,7 +3708,7 @@ void from_json(const BasicJsonType& j, ConstructibleStringType& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j)); } s = *j.template get_ptr(); @@ -3619,7 +3748,7 @@ void from_json(const BasicJsonType& j, std::forward_list& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } l.clear(); std::transform(j.rbegin(), j.rend(), @@ -3636,7 +3765,7 @@ void from_json(const BasicJsonType& j, std::valarray& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } l.resize(j.size()); std::transform(j.begin(), j.end(), std::begin(l), @@ -3727,8 +3856,7 @@ void()) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } from_json_array_impl(j, arr, priority_tag<3> {}); @@ -3739,7 +3867,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin) { if (JSON_HEDLEY_UNLIKELY(!j.is_binary())) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), j)); } bin = *j.template get_ptr(); @@ -3751,7 +3879,7 @@ void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) { if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), j)); } ConstructibleObjectType ret; @@ -3805,7 +3933,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j)); } } @@ -3834,14 +3962,14 @@ void from_json(const BasicJsonType& j, std::map& { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j)); } m.emplace(p.at(0).template get(), p.at(1).template get()); } @@ -3854,14 +3982,14 @@ void from_json(const BasicJsonType& j, std::unordered_map(), p.at(1).template get()); } @@ -4207,6 +4335,7 @@ struct external_constructor { j.m_type = value_t::array; j.m_value = arr; + j.set_parents(); j.assert_invariant(); } @@ -4215,6 +4344,7 @@ struct external_constructor { j.m_type = value_t::array; j.m_value = std::move(arr); + j.set_parents(); j.assert_invariant(); } @@ -4227,6 +4357,7 @@ struct external_constructor using std::end; j.m_type = value_t::array; j.m_value.array = j.template create(begin(arr), end(arr)); + j.set_parents(); j.assert_invariant(); } @@ -4239,6 +4370,7 @@ struct external_constructor for (const bool x : arr) { j.m_value.array->push_back(x); + j.set_parent(j.m_value.array->back()); } j.assert_invariant(); } @@ -4254,6 +4386,7 @@ struct external_constructor { std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin()); } + j.set_parents(); j.assert_invariant(); } }; @@ -4266,6 +4399,7 @@ struct external_constructor { j.m_type = value_t::object; j.m_value = obj; + j.set_parents(); j.assert_invariant(); } @@ -4274,6 +4408,7 @@ struct external_constructor { j.m_type = value_t::object; j.m_value = std::move(obj); + j.set_parents(); j.assert_invariant(); } @@ -4286,6 +4421,7 @@ struct external_constructor j.m_type = value_t::object; j.m_value.object = j.template create(begin(obj), end(obj)); + j.set_parents(); j.assert_invariant(); } }; @@ -5509,8 +5645,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, - "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), *ref_stack.back())); } return true; @@ -5525,6 +5660,7 @@ class json_sax_dom_parser bool end_object() { + ref_stack.back()->set_parents(); ref_stack.pop_back(); return true; } @@ -5535,8 +5671,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, - "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), *ref_stack.back())); } return true; @@ -5544,6 +5679,7 @@ class json_sax_dom_parser bool end_array() { + ref_stack.back()->set_parents(); ref_stack.pop_back(); return true; } @@ -5690,7 +5826,7 @@ class json_sax_dom_callback_parser // check object limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), *ref_stack.back())); } return true; @@ -5715,10 +5851,17 @@ class json_sax_dom_callback_parser bool end_object() { - if (ref_stack.back() && !callback(static_cast(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back())) + if (ref_stack.back()) { - // discard object - *ref_stack.back() = discarded; + if (!callback(static_cast(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back())) + { + // discard object + *ref_stack.back() = discarded; + } + else + { + ref_stack.back()->set_parents(); + } } JSON_ASSERT(!ref_stack.empty()); @@ -5753,7 +5896,7 @@ class json_sax_dom_callback_parser // check array limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), *ref_stack.back())); } return true; @@ -5766,7 +5909,11 @@ class json_sax_dom_callback_parser if (ref_stack.back()) { keep = callback(static_cast(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back()); - if (!keep) + if (keep) + { + ref_stack.back()->set_parents(); + } + else { // discard array *ref_stack.back() = discarded; @@ -5864,7 +6011,7 @@ class json_sax_dom_callback_parser // array if (ref_stack.back()->is_array()) { - ref_stack.back()->m_value.array->push_back(std::move(value)); + ref_stack.back()->m_value.array->emplace_back(std::move(value)); return {true, &(ref_stack.back()->m_value.array->back())}; } @@ -7886,7 +8033,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current != std::char_traits::eof())) { return sax->parse_error(chars_read, get_token_string(), - parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"))); + parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"), BasicJsonType())); } } @@ -7962,7 +8109,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 1)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"), BasicJsonType())); } return get_string(input_format_t::bson, len - static_cast(1), result) && get() != std::char_traits::eof(); @@ -7983,7 +8130,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 0)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"), BasicJsonType())); } // All BSON binary values have a subtype @@ -8065,7 +8212,7 @@ class binary_reader { std::array cr{{}}; (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type)); - return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()))); + return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), BasicJsonType())); } } } @@ -8465,7 +8612,7 @@ class binary_reader case cbor_tag_handler_t::error: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); } case cbor_tag_handler_t::ignore: @@ -8580,7 +8727,7 @@ class binary_reader default: // anything else (0xFF is handled inside the other types) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); } } } @@ -8675,7 +8822,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"), BasicJsonType())); } } } @@ -8774,7 +8921,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"), BasicJsonType())); } } } @@ -9241,7 +9388,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); } } } @@ -9323,7 +9470,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"), BasicJsonType())); } } } @@ -9573,7 +9720,7 @@ class binary_reader default: auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), BasicJsonType())); } } @@ -9643,7 +9790,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), BasicJsonType())); } } } @@ -9681,7 +9828,7 @@ class binary_reader return false; } auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"), BasicJsonType())); } return get_ubjson_size_value(result.first); @@ -9771,7 +9918,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current > 127)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"), BasicJsonType())); } string_t s(1, static_cast(current)); return sax->string(s); @@ -9792,7 +9939,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); } } } @@ -9970,7 +10117,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input)) { - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"))); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), BasicJsonType())); } switch (result_number) @@ -9982,7 +10129,7 @@ class binary_reader case token_type::value_float: return sax->number_float(number_lexer.get_number_float(), std::move(number_string)); default: - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"))); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), BasicJsonType())); } } @@ -10138,7 +10285,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current == std::char_traits::eof())) { return sax->parse_error(chars_read, "", - parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context))); + parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), BasicJsonType())); } return true; } @@ -10311,7 +10458,6 @@ class parser { json_sax_dom_callback_parser sdp(result, callback, allow_exceptions); sax_parse_internal(&sdp); - result.assert_invariant(); // in strict mode, input must be completely read if (strict && (get_token() != token_type::end_of_input)) @@ -10319,7 +10465,7 @@ class parser sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), BasicJsonType())); } // in case of an error, return discarded value @@ -10340,15 +10486,13 @@ class parser { json_sax_dom_parser sdp(result, allow_exceptions); sax_parse_internal(&sdp); - result.assert_invariant(); // in strict mode, input must be completely read if (strict && (get_token() != token_type::end_of_input)) { sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType())); } // in case of an error, return discarded value @@ -10358,6 +10502,8 @@ class parser return; } } + + result.assert_invariant(); } /*! @@ -10384,8 +10530,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType())); } return result; @@ -10431,8 +10576,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) { @@ -10444,8 +10588,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType())); } // remember we are now inside an object @@ -10488,7 +10631,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'")); + out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'", BasicJsonType())); } if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string()))) @@ -10558,16 +10701,14 @@ class parser // using "uninitialized" to avoid "expected" message return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::uninitialized, "value"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), BasicJsonType())); } default: // the last token was unexpected { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::literal_or_value, "value"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), BasicJsonType())); } } } @@ -10613,8 +10754,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_array, "array"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), BasicJsonType())); } // states.back() is false -> object @@ -10627,8 +10767,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) @@ -10641,8 +10780,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType())); } // parse values @@ -10670,8 +10808,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_object, "object"))); + parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), BasicJsonType())); } } @@ -11142,7 +11279,7 @@ class iter_impl } case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); default: { @@ -11151,7 +11288,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); } } } @@ -11185,7 +11322,7 @@ class iter_impl return m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); } } } @@ -11286,7 +11423,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object)); } JSON_ASSERT(m_object != nullptr); @@ -11323,7 +11460,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object)); } JSON_ASSERT(m_object != nullptr); @@ -11331,7 +11468,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators")); + JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", *m_object)); case value_t::array: return (m_it.array_iterator < other.m_it.array_iterator); @@ -11379,7 +11516,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object)); case value_t::array: { @@ -11450,7 +11587,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object)); case value_t::array: return m_it.array_iterator - other.m_it.array_iterator; @@ -11471,13 +11608,13 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators")); + JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", *m_object)); case value_t::array: return *std::next(m_it.array_iterator, n); case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); default: { @@ -11486,7 +11623,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); } } } @@ -11504,7 +11641,7 @@ class iter_impl return m_it.object_iterator->first; } - JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators")); + JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", *m_object)); } /*! @@ -11665,6 +11802,8 @@ class json_reverse_iterator : public std::reverse_iterator // #include +// #include + // #include @@ -11723,7 +11862,7 @@ class json_pointer std::string{}, [](const std::string & a, const std::string & b) { - return a + "/" + escape(b); + return a + "/" + detail::escape(b); }); } @@ -11903,7 +12042,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); } reference_tokens.pop_back(); @@ -11927,7 +12066,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); } return reference_tokens.back(); @@ -11993,15 +12132,13 @@ class json_pointer // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) { - JSON_THROW(detail::parse_error::create(106, 0, - "array index '" + s + - "' must not begin with '0'")); + JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", BasicJsonType())); } // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9'))) { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number")); + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", BasicJsonType())); } std::size_t processed_chars = 0; @@ -12012,20 +12149,20 @@ class json_pointer } JSON_CATCH(std::out_of_range&) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType())); } // check if the string was completely read if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size())) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType())); } // only triggered on special platforms (like 32bit), see also // https://github.com/nlohmann/json/pull/2203 if (res >= static_cast((std::numeric_limits::max)())) { - JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type")); // LCOV_EXCL_LINE + JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", BasicJsonType())); // LCOV_EXCL_LINE } return static_cast(res); @@ -12036,7 +12173,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); } json_pointer result = *this; @@ -12099,7 +12236,7 @@ class json_pointer single value; that is, with an empty list of reference tokens. */ default: - JSON_THROW(detail::type_error::create(313, "invalid value to unflatten")); + JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", j)); } } @@ -12171,7 +12308,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); } } @@ -12204,7 +12341,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + ") is out of range", *ptr)); } // note: at performs range check @@ -12213,7 +12350,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); } } @@ -12251,9 +12388,7 @@ class json_pointer if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) { // "-" cannot be used for const access - JSON_THROW(detail::out_of_range::create(402, - "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", *ptr)); } // use unchecked array access @@ -12262,7 +12397,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); } } @@ -12295,7 +12430,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + ") is out of range", *ptr)); } // note: at performs range check @@ -12304,7 +12439,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); } } @@ -12408,9 +12543,7 @@ class json_pointer // check if nonempty reference string begins with slash if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) { - JSON_THROW(detail::parse_error::create(107, 1, - "JSON pointer must be empty or begin with '/' - was: '" + - reference_string + "'")); + JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", BasicJsonType())); } // extract the reference tokens: @@ -12445,58 +12578,18 @@ class json_pointer (reference_token[pos + 1] != '0' && reference_token[pos + 1] != '1'))) { - JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'")); + JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", BasicJsonType())); } } // finally, store the reference token - unescape(reference_token); + detail::unescape(reference_token); result.push_back(reference_token); } return result; } - /*! - @brief replace all occurrences of a substring by another string - - @param[in,out] s the string to manipulate; changed so that all - occurrences of @a f are replaced with @a t - @param[in] f the substring to replace with @a t - @param[in] t the string to replace @a f - - @pre The search string @a f must not be empty. **This precondition is - enforced with an assertion.** - - @since version 2.0.0 - */ - static void replace_substring(std::string& s, const std::string& f, - const std::string& t) - { - JSON_ASSERT(!f.empty()); - for (auto pos = s.find(f); // find first occurrence of f - pos != std::string::npos; // make sure f was found - s.replace(pos, f.size(), t), // replace with t, and - pos = s.find(f, pos + t.size())) // find next occurrence of f - {} - } - - JSON_PRIVATE_UNLESS_TESTED: - /// escape "~" to "~0" and "/" to "~1" - static std::string escape(std::string s) - { - replace_substring(s, "~", "~0"); - replace_substring(s, "/", "~1"); - return s; - } - - /// unescape "~1" to tilde and "~0" to slash (order is important!) - static void unescape(std::string& s) - { - replace_substring(s, "~1", "/"); - replace_substring(s, "~0", "~"); - } - private: /*! @param[in] reference_string the reference string to the current value @@ -12542,7 +12635,7 @@ class json_pointer // iterate object and use keys as reference string for (const auto& element : *value.m_value.object) { - flatten(reference_string + "/" + escape(element.first), element.second, result); + flatten(reference_string + "/" + detail::escape(element.first), element.second, result); } } break; @@ -12572,7 +12665,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!value.is_object())) { - JSON_THROW(detail::type_error::create(314, "only objects can be unflattened")); + JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", value)); } BasicJsonType result; @@ -12582,7 +12675,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive())) { - JSON_THROW(detail::type_error::create(315, "values in object must be primitive")); + JSON_THROW(detail::type_error::create(315, "values in object must be primitive", element.second)); } // assign value to reference pointed to by JSON pointer; Note that if @@ -12898,7 +12991,7 @@ class binary_writer default: { - JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), j));; } } } @@ -13742,13 +13835,12 @@ class binary_writer @return The size of a BSON document entry header, including the id marker and the entry name size (and its null-terminator). */ - static std::size_t calc_bson_entry_header_size(const string_t& name) + static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j) { const auto it = name.find(static_cast(0)); if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) { - JSON_THROW(out_of_range::create(409, - "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")")); + JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", j)); } return /*id*/ 1ul + name.size() + /*zero-terminator*/1u; @@ -13858,21 +13950,21 @@ class binary_writer @brief Writes a BSON element with key @a name and unsigned @a value */ void write_bson_unsigned(const string_t& name, - const std::uint64_t value) + const BasicJsonType& j) { - if (value <= static_cast((std::numeric_limits::max)())) + if (j.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) { write_bson_entry_header(name, 0x10 /* int32 */); - write_number(static_cast(value)); + write_number(static_cast(j.m_value.number_unsigned)); } - else if (value <= static_cast((std::numeric_limits::max)())) + else if (j.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) { write_bson_entry_header(name, 0x12 /* int64 */); - write_number(static_cast(value)); + write_number(static_cast(j.m_value.number_unsigned)); } else { - JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(value) + " cannot be represented by BSON as it does not fit int64")); + JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", j)); } } @@ -13949,7 +14041,7 @@ class binary_writer static std::size_t calc_bson_element_size(const string_t& name, const BasicJsonType& j) { - const auto header_size = calc_bson_entry_header_size(name); + const auto header_size = calc_bson_entry_header_size(name, j); switch (j.type()) { case value_t::object: @@ -14018,7 +14110,7 @@ class binary_writer return write_bson_integer(name, j.m_value.number_integer); case value_t::number_unsigned: - return write_bson_unsigned(name, j.m_value.number_unsigned); + return write_bson_unsigned(name, j); case value_t::string: return write_bson_string(name, *j.m_value.string); @@ -16050,7 +16142,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn)); + JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, BasicJsonType())); } case error_handler_t::ignore: @@ -16144,7 +16236,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn)); + JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, BasicJsonType())); } case error_handler_t::ignore: @@ -16809,6 +16901,7 @@ class basic_json friend class ::nlohmann::detail::json_sax_dom_parser; template friend class ::nlohmann::detail::json_sax_dom_callback_parser; + friend class ::nlohmann::detail::exception; /// workaround type for MSVC using basic_json_t = NLOHMANN_BASIC_JSON_TPL; @@ -17680,7 +17773,7 @@ class basic_json object = nullptr; // silence warning, see #821 if (JSON_HEDLEY_UNLIKELY(t == value_t::null)) { - JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1")); // LCOV_EXCL_LINE + JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1", basic_json())); // LCOV_EXCL_LINE } break; } @@ -17847,13 +17940,83 @@ class basic_json invariant. Furthermore, it has to be called each time the type of a JSON value is changed, because the invariant expresses a relationship between @a m_type and @a m_value. + + Furthermore, the parent relation is checked for arrays and objects: If + @a check_parents true and the value is an array or object, then the + container's elements must have the current value as parent. + + @param[in] check_parents whether the parent relation should be checked. + The value is true by default and should only be set to false + during destruction of objects when the invariant does not + need to hold. */ - void assert_invariant() const noexcept + void assert_invariant(bool check_parents = true) const noexcept { JSON_ASSERT(m_type != value_t::object || m_value.object != nullptr); JSON_ASSERT(m_type != value_t::array || m_value.array != nullptr); JSON_ASSERT(m_type != value_t::string || m_value.string != nullptr); JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr); + +#if JSON_DIAGNOSTICS + JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j) + { + return j.m_parent == this; + })); +#else + static_cast(check_parents); +#endif + } + + void set_parents() + { +#if JSON_DIAGNOSTICS + switch (m_type) + { + case value_t::array: + { + for (auto& element : *m_value.array) + { + element.m_parent = this; + } + break; + } + + case value_t::object: + { + for (auto& element : *m_value.object) + { + element.second.m_parent = this; + } + break; + } + + default: + break; + } +#endif + } + + iterator set_parents(iterator it, typename iterator::difference_type count) + { +#if JSON_DIAGNOSTICS + for (typename iterator::difference_type i = 0; i < count; ++i) + { + (it + i)->m_parent = this; + } +#else + static_cast(count); +#endif + return it; + } + + reference set_parent(reference j) + { +#if JSON_DIAGNOSTICS + j.m_parent = this; +#else + static_cast(j); +#endif + return j; } public: @@ -18070,6 +18233,7 @@ class basic_json std::forward(val)))) { JSONSerializer::to_json(*this, std::forward(val)); + set_parents(); assert_invariant(); } @@ -18148,6 +18312,7 @@ class basic_json default: // LCOV_EXCL_LINE JSON_ASSERT(false); // LCOV_EXCL_LINE } + set_parents(); assert_invariant(); } @@ -18249,7 +18414,7 @@ class basic_json // if object is wanted but impossible, throw an exception if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object)) { - JSON_THROW(type_error::create(301, "cannot create object from initializer list")); + JSON_THROW(type_error::create(301, "cannot create object from initializer list", basic_json())); } } @@ -18259,13 +18424,13 @@ class basic_json m_type = value_t::object; m_value = value_t::object; - std::for_each(init.begin(), init.end(), [this](const detail::json_ref& element_ref) + for (auto& element_ref : init) { auto element = element_ref.moved_or_copied(); m_value.object->emplace( std::move(*((*element.m_value.array)[0].m_value.string)), std::move((*element.m_value.array)[1])); - }); + } } else { @@ -18274,6 +18439,7 @@ class basic_json m_value.array = create(init.begin(), init.end()); } + set_parents(); assert_invariant(); } @@ -18483,6 +18649,7 @@ class basic_json : m_type(value_t::array) { m_value.array = create(cnt, val); + set_parents(); assert_invariant(); } @@ -18552,7 +18719,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(201, "iterators are not compatible")); + JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", basic_json())); } // copy type from first iterator @@ -18570,7 +18737,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, "iterators out of range")); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", *first.m_object)); } break; } @@ -18632,10 +18799,10 @@ class basic_json } default: - JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + - std::string(first.m_object->type_name()))); + JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), *first.m_object)); } + set_parents(); assert_invariant(); } @@ -18734,6 +18901,7 @@ class basic_json break; } + set_parents(); assert_invariant(); } @@ -18768,12 +18936,13 @@ class basic_json m_value(std::move(other.m_value)) { // check that passed value is valid - other.assert_invariant(); + other.assert_invariant(false); // invalidate payload other.m_type = value_t::null; other.m_value = {}; + set_parents(); assert_invariant(); } @@ -18814,6 +18983,7 @@ class basic_json swap(m_type, other.m_type); swap(m_value, other.m_value); + set_parents(); assert_invariant(); return *this; } @@ -18835,7 +19005,7 @@ class basic_json */ ~basic_json() noexcept { - assert_invariant(); + assert_invariant(false); m_value.destroy(m_type); } @@ -19328,7 +19498,7 @@ class basic_json return m_value.boolean; } - JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()), *this)); } /// get a pointer to the value (object) @@ -19449,7 +19619,7 @@ class basic_json return *ptr; } - JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()))); + JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()), obj)); } public: @@ -19877,7 +20047,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), *this)); } return *get_ptr(); @@ -19888,7 +20058,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), *this)); } return *get_ptr(); @@ -19938,17 +20108,17 @@ class basic_json { JSON_TRY { - return m_value.array->at(idx); + return set_parent(m_value.array->at(idx)); } JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this)); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); } } @@ -19990,12 +20160,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this)); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); } } @@ -20036,17 +20206,17 @@ class basic_json { JSON_TRY { - return m_value.object->at(key); + return set_parent(m_value.object->at(key)); } JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this)); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); } } @@ -20092,12 +20262,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this)); } } else { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); } } @@ -20142,15 +20312,22 @@ class basic_json // fill up array with null values if given idx is outside range if (idx >= m_value.array->size()) { - m_value.array->insert(m_value.array->end(), - idx - m_value.array->size() + 1, - basic_json()); +#if JSON_DIAGNOSTICS + // remember array size before resizing + const auto previous_size = m_value.array->size(); +#endif + m_value.array->resize(idx + 1); + +#if JSON_DIAGNOSTICS + // set parent for values added above + set_parents(begin() + static_cast(previous_size), static_cast(idx + 1 - previous_size)); +#endif } return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), *this)); } /*! @@ -20180,7 +20357,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), *this)); } /*! @@ -20223,10 +20400,10 @@ class basic_json // operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { - return m_value.object->operator[](key); + return set_parent(m_value.object->operator[](key)); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } /*! @@ -20268,7 +20445,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } /*! @@ -20313,10 +20490,10 @@ class basic_json // at only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { - return m_value.object->operator[](key); + return set_parent(m_value.object->operator[](key)); } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } /*! @@ -20360,7 +20537,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); } /*! @@ -20432,7 +20609,7 @@ class basic_json return default_value; } - JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), *this)); } /*! @@ -20505,7 +20682,7 @@ class basic_json } } - JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), *this)); } /*! @@ -20659,7 +20836,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != pos.m_object)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } IteratorType result = end(); @@ -20675,7 +20852,7 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin())) { - JSON_THROW(invalid_iterator::create(205, "iterator out of range")); + JSON_THROW(invalid_iterator::create(205, "iterator out of range", *this)); } if (is_string()) @@ -20711,7 +20888,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); } return result; @@ -20772,7 +20949,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object)) { - JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value")); + JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", *this)); } IteratorType result = end(); @@ -20789,7 +20966,7 @@ class basic_json if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, "iterators out of range")); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", *this)); } if (is_string()) @@ -20827,7 +21004,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); } return result; @@ -20870,7 +21047,7 @@ class basic_json return m_value.object->erase(key); } - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); } /*! @@ -20904,14 +21081,14 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(idx >= size())) { - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this)); } m_value.array->erase(m_value.array->begin() + static_cast(idx)); } else { - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); } } @@ -21856,7 +22033,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this)); } // transform null object into an array @@ -21869,6 +22046,7 @@ class basic_json // add element to array (move semantics) m_value.array->push_back(std::move(val)); + set_parent(m_value.array->back()); // if val is moved from, basic_json move constructor marks it null so we do not call the destructor } @@ -21891,7 +22069,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this)); } // transform null object into an array @@ -21904,6 +22082,7 @@ class basic_json // add element to array m_value.array->push_back(val); + set_parent(m_value.array->back()); } /*! @@ -21941,7 +22120,7 @@ class basic_json // push_back only works for null objects or objects if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this)); } // transform null object into an object @@ -21952,8 +22131,9 @@ class basic_json assert_invariant(); } - // add element to array - m_value.object->insert(val); + // add element to object + auto res = m_value.object->insert(val); + set_parent(res.first->second); } /*! @@ -22044,7 +22224,7 @@ class basic_json // emplace_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), *this)); } // transform null object into an array @@ -22057,10 +22237,10 @@ class basic_json // add element to array (perfect forwarding) #ifdef JSON_HAS_CPP_17 - return m_value.array->emplace_back(std::forward(args)...); + return set_parent(m_value.array->emplace_back(std::forward(args)...)); #else m_value.array->emplace_back(std::forward(args)...); - return m_value.array->back(); + return set_parent(m_value.array->back()); #endif } @@ -22097,7 +22277,7 @@ class basic_json // emplace only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), *this)); } // transform null object into an object @@ -22110,6 +22290,8 @@ class basic_json // add element to array (perfect forwarding) auto res = m_value.object->emplace(std::forward(args)...); + set_parent(res.first->second); + // create result iterator and set iterator to the result of emplace auto it = begin(); it.m_it.object_iterator = res.first; @@ -22168,14 +22350,14 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } // insert to array and return iterator - return insert_iterator(pos, val); + return set_parents(insert_iterator(pos, val), static_cast(1)); } - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } /*! @@ -22219,14 +22401,14 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } // insert to array and return iterator - return insert_iterator(pos, cnt, val); + return set_parents(insert_iterator(pos, cnt, val), static_cast(cnt)); } - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } /*! @@ -22264,28 +22446,28 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this)); } if (JSON_HEDLEY_UNLIKELY(first.m_object == this)) { - JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container")); + JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", *this)); } // insert to array and return iterator - return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); + return set_parents(insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator), std::distance(first, last)); } /*! @@ -22317,17 +22499,17 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); } // insert to array and return iterator - return insert_iterator(pos, ilist.begin(), ilist.end()); + return set_parents(insert_iterator(pos, ilist.begin(), ilist.end()), static_cast(ilist.size())); } /*! @@ -22358,19 +22540,19 @@ class basic_json // insert only works for objects if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this)); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this)); } m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator); @@ -22407,11 +22589,11 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), *this)); } if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()), *this)); } for (auto it = j.cbegin(); it != j.cend(); ++it) @@ -22458,20 +22640,20 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), *this)); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this)); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object() || !last.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this)); } for (auto it = first; it != last; ++it) @@ -22506,6 +22688,9 @@ class basic_json { std::swap(m_type, other.m_type); std::swap(m_value, other.m_value); + + set_parents(); + other.set_parents(); assert_invariant(); } @@ -22566,7 +22751,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -22599,7 +22784,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -22632,7 +22817,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -22665,7 +22850,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -22679,7 +22864,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); } } @@ -23587,6 +23772,11 @@ class basic_json /// the value of the current element json_value m_value = {}; +#if JSON_DIAGNOSTICS + /// a pointer to a parent value (for debugging purposes) + basic_json* m_parent = nullptr; +#endif + ////////////////////////////////////////// // binary serialization/deserialization // ////////////////////////////////////////// @@ -24883,7 +25073,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(idx > parent.size())) { // avoid undefined behavior - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", parent)); } // default case: insert add offset @@ -24899,7 +25089,7 @@ class basic_json }; // wrapper for "remove" operation; remove value at ptr - const auto operation_remove = [&result](json_pointer & ptr) + const auto operation_remove = [this, &result](json_pointer & ptr) { // get reference to parent of JSON pointer ptr const auto last_path = ptr.back(); @@ -24917,7 +25107,7 @@ class basic_json } else { - JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found", *this)); } } else if (parent.is_array()) @@ -24930,7 +25120,7 @@ class basic_json // type check: top level value must be an array if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array())) { - JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", json_patch)); } // iterate and apply the operations @@ -24950,13 +25140,13 @@ class basic_json // check if desired value is present if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end())) { - JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", val)); } // check if result is of type string if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string())) { - JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", val)); } // no error: return value @@ -24966,7 +25156,7 @@ class basic_json // type check: every element of the array must be an object if (JSON_HEDLEY_UNLIKELY(!val.is_object())) { - JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", val)); } // collect mandatory members @@ -25044,7 +25234,7 @@ class basic_json // throw an exception if test fails if (JSON_HEDLEY_UNLIKELY(!success)) { - JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump())); + JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump(), val)); } break; @@ -25054,7 +25244,7 @@ class basic_json { // op must be "add", "remove", "replace", "move", "copy", or // "test" - JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid")); + JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid", val)); } } } @@ -25170,7 +25360,7 @@ class basic_json for (auto it = source.cbegin(); it != source.cend(); ++it) { // escape the key name to be used in a JSON patch - const auto key = json_pointer::escape(it.key()); + const auto key = detail::escape(it.key()); if (target.find(it.key()) != target.end()) { @@ -25194,7 +25384,7 @@ class basic_json if (source.find(it.key()) == source.end()) { // found a key that is not in this -> add it - const auto key = json_pointer::escape(it.key()); + const auto key = detail::escape(it.key()); result.push_back( { {"op", "add"}, {"path", path + "/" + key}, From bb90e34d22b59ad3a15e459000fbe5a693b32397 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 7 Feb 2021 17:47:08 +0100 Subject: [PATCH 064/143] :arrow_up: Hedley v15 --- include/nlohmann/thirdparty/hedley/hedley.hpp | 141 ++++++++++++----- .../thirdparty/hedley/hedley_undef.hpp | 5 +- single_include/nlohmann/json.hpp | 146 +++++++++++++----- 3 files changed, 218 insertions(+), 74 deletions(-) diff --git a/include/nlohmann/thirdparty/hedley/hedley.hpp b/include/nlohmann/thirdparty/hedley/hedley.hpp index c1fa16dbb6..36f9fe81a3 100644 --- a/include/nlohmann/thirdparty/hedley/hedley.hpp +++ b/include/nlohmann/thirdparty/hedley/hedley.hpp @@ -10,11 +10,11 @@ * SPDX-License-Identifier: CC0-1.0 */ -#if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 14) +#if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15) #if defined(JSON_HEDLEY_VERSION) #undef JSON_HEDLEY_VERSION #endif -#define JSON_HEDLEY_VERSION 14 +#define JSON_HEDLEY_VERSION 15 #if defined(JSON_HEDLEY_STRINGIFY_EX) #undef JSON_HEDLEY_STRINGIFY_EX @@ -381,7 +381,7 @@ #if __VER__ > 1000 #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000)) #else - #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(VER / 100, __VER__ % 100, 0) + #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0) #endif #endif @@ -458,6 +458,22 @@ #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0) #endif +#if defined(JSON_HEDLEY_MCST_LCC_VERSION) + #undef JSON_HEDLEY_MCST_LCC_VERSION +#endif +#if defined(__LCC__) && defined(__LCC_MINOR__) + #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__) +#endif + +#if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK) + #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_MCST_LCC_VERSION) + #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0) +#endif + #if defined(JSON_HEDLEY_GCC_VERSION) #undef JSON_HEDLEY_GCC_VERSION #endif @@ -467,6 +483,7 @@ !defined(JSON_HEDLEY_INTEL_VERSION) && \ !defined(JSON_HEDLEY_PGI_VERSION) && \ !defined(JSON_HEDLEY_ARM_VERSION) && \ + !defined(JSON_HEDLEY_CRAY_VERSION) && \ !defined(JSON_HEDLEY_TI_VERSION) && \ !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \ !defined(JSON_HEDLEY_TI_CL430_VERSION) && \ @@ -474,7 +491,8 @@ !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \ !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \ !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \ - !defined(__COMPCERT__) + !defined(__COMPCERT__) && \ + !defined(JSON_HEDLEY_MCST_LCC_VERSION) #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION #endif @@ -490,17 +508,21 @@ #if defined(JSON_HEDLEY_HAS_ATTRIBUTE) #undef JSON_HEDLEY_HAS_ATTRIBUTE #endif -#if defined(__has_attribute) - #define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute) +#if \ + defined(__has_attribute) && \ + ( \ + (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \ + ) +# define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute) #else - #define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0) +# define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0) #endif #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE) #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE #endif #if defined(__has_attribute) - #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) __has_attribute(attribute) + #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) #else #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) #endif @@ -509,7 +531,7 @@ #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE #endif #if defined(__has_attribute) - #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) __has_attribute(attribute) + #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) #else #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) #endif @@ -873,6 +895,8 @@ #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996)) +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444") #elif \ JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ @@ -923,6 +947,8 @@ #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163") #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161") +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161") #else #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS #endif @@ -953,6 +979,8 @@ #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173") #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097") +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097") #else #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES #endif @@ -970,6 +998,21 @@ #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL #endif +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wunused-function") + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"") +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505)) +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142") +#else + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION +#endif + #if defined(JSON_HEDLEY_DEPRECATED) #undef JSON_HEDLEY_DEPRECATED #endif @@ -982,7 +1025,7 @@ #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since)) #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement)) #elif \ - JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) || \ + (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \ JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \ @@ -992,7 +1035,8 @@ JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since))) #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement))) #elif defined(__cplusplus) && (__cplusplus >= 201402L) @@ -1012,7 +1056,9 @@ (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__)) #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__)) #elif \ @@ -1035,7 +1081,8 @@ #if \ JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \ JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since))) #else #define JSON_HEDLEY_UNAVAILABLE(available_since) @@ -1063,7 +1110,8 @@ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__)) #elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L) @@ -1087,7 +1135,8 @@ JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \ JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) + JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position))) #else #define JSON_HEDLEY_SENTINEL(position) @@ -1098,7 +1147,9 @@ #endif #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) #define JSON_HEDLEY_NO_RETURN __noreturn -#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) +#elif \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L #define JSON_HEDLEY_NO_RETURN _Noreturn @@ -1120,7 +1171,8 @@ (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return") @@ -1177,7 +1229,9 @@ JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \ JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) + JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable() #elif defined(JSON_HEDLEY_ASSUME) #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0) @@ -1255,7 +1309,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check))) #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0) #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check)) @@ -1292,7 +1347,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP #endif #if \ (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) + JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability)) # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability)) # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability)) @@ -1313,7 +1369,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \ - JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) + JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) # define JSON_HEDLEY_PREDICT(expr, expected, probability) \ (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))) # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \ @@ -1359,7 +1416,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_MALLOC __attribute__((__malloc__)) #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory") @@ -1392,7 +1450,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) # define JSON_HEDLEY_PURE __attribute__((__pure__)) #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data") @@ -1428,7 +1487,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_CONST __attribute__((__const__)) #elif \ JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) @@ -1456,7 +1516,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \ JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \ - defined(__clang__) + defined(__clang__) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_RESTRICT __restrict #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus) #define JSON_HEDLEY_RESTRICT _Restrict @@ -1484,7 +1545,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_INLINE __inline #else #define JSON_HEDLEY_INLINE @@ -1510,7 +1572,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE #elif \ JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \ @@ -1552,7 +1616,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__)) #elif \ JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ @@ -1599,7 +1665,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \ ) \ - ) + ) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden"))) # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default"))) # else @@ -1615,7 +1682,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP #if \ JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \ JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__)) #elif \ JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \ @@ -1631,7 +1699,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP #endif #if \ JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) + JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__)) #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough) #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]]) @@ -1648,7 +1717,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP #endif #if \ JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) + JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__)) #elif defined(_Ret_notnull_) /* SAL */ #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_ @@ -1690,7 +1760,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \ (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \ - JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) + JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr) #endif #if !defined(__cplusplus) @@ -1714,7 +1785,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP !defined(JSON_HEDLEY_SUNPRO_VERSION) && \ !defined(JSON_HEDLEY_PGI_VERSION) && \ !defined(JSON_HEDLEY_IAR_VERSION)) || \ - JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) || \ + (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \ JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \ JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \ @@ -1891,7 +1962,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #if defined(JSON_HEDLEY_FLAGS) #undef JSON_HEDLEY_FLAGS #endif -#if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) +#if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion")) #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__)) #else #define JSON_HEDLEY_FLAGS diff --git a/include/nlohmann/thirdparty/hedley/hedley_undef.hpp b/include/nlohmann/thirdparty/hedley/hedley_undef.hpp index 2f70f13c6d..e74f4dfbfe 100644 --- a/include/nlohmann/thirdparty/hedley/hedley_undef.hpp +++ b/include/nlohmann/thirdparty/hedley/hedley_undef.hpp @@ -1,5 +1,3 @@ -#pragma once - #undef JSON_HEDLEY_ALWAYS_INLINE #undef JSON_HEDLEY_ARM_VERSION #undef JSON_HEDLEY_ARM_VERSION_CHECK @@ -33,6 +31,7 @@ #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS +#undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION #undef JSON_HEDLEY_DIAGNOSTIC_POP #undef JSON_HEDLEY_DIAGNOSTIC_PUSH #undef JSON_HEDLEY_DMC_VERSION @@ -84,6 +83,8 @@ #undef JSON_HEDLEY_IS_CONSTEXPR_ #undef JSON_HEDLEY_LIKELY #undef JSON_HEDLEY_MALLOC +#undef JSON_HEDLEY_MCST_LCC_VERSION +#undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK #undef JSON_HEDLEY_MESSAGE #undef JSON_HEDLEY_MSVC_VERSION #undef JSON_HEDLEY_MSVC_VERSION_CHECK diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 30dd96f43e..93b0a9652d 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -119,11 +119,11 @@ struct position_t * SPDX-License-Identifier: CC0-1.0 */ -#if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 14) +#if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15) #if defined(JSON_HEDLEY_VERSION) #undef JSON_HEDLEY_VERSION #endif -#define JSON_HEDLEY_VERSION 14 +#define JSON_HEDLEY_VERSION 15 #if defined(JSON_HEDLEY_STRINGIFY_EX) #undef JSON_HEDLEY_STRINGIFY_EX @@ -490,7 +490,7 @@ struct position_t #if __VER__ > 1000 #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000)) #else - #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(VER / 100, __VER__ % 100, 0) + #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0) #endif #endif @@ -567,6 +567,22 @@ struct position_t #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0) #endif +#if defined(JSON_HEDLEY_MCST_LCC_VERSION) + #undef JSON_HEDLEY_MCST_LCC_VERSION +#endif +#if defined(__LCC__) && defined(__LCC_MINOR__) + #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__) +#endif + +#if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK) + #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_MCST_LCC_VERSION) + #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0) +#endif + #if defined(JSON_HEDLEY_GCC_VERSION) #undef JSON_HEDLEY_GCC_VERSION #endif @@ -576,6 +592,7 @@ struct position_t !defined(JSON_HEDLEY_INTEL_VERSION) && \ !defined(JSON_HEDLEY_PGI_VERSION) && \ !defined(JSON_HEDLEY_ARM_VERSION) && \ + !defined(JSON_HEDLEY_CRAY_VERSION) && \ !defined(JSON_HEDLEY_TI_VERSION) && \ !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \ !defined(JSON_HEDLEY_TI_CL430_VERSION) && \ @@ -583,7 +600,8 @@ struct position_t !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \ !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \ !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \ - !defined(__COMPCERT__) + !defined(__COMPCERT__) && \ + !defined(JSON_HEDLEY_MCST_LCC_VERSION) #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION #endif @@ -599,17 +617,21 @@ struct position_t #if defined(JSON_HEDLEY_HAS_ATTRIBUTE) #undef JSON_HEDLEY_HAS_ATTRIBUTE #endif -#if defined(__has_attribute) - #define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute) +#if \ + defined(__has_attribute) && \ + ( \ + (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \ + ) +# define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute) #else - #define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0) +# define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0) #endif #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE) #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE #endif #if defined(__has_attribute) - #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) __has_attribute(attribute) + #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) #else #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) #endif @@ -618,7 +640,7 @@ struct position_t #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE #endif #if defined(__has_attribute) - #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) __has_attribute(attribute) + #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) #else #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) #endif @@ -982,6 +1004,8 @@ struct position_t #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996)) +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444") #elif \ JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ @@ -1032,6 +1056,8 @@ struct position_t #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163") #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161") +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161") #else #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS #endif @@ -1062,6 +1088,8 @@ struct position_t #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173") #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097") +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097") #else #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES #endif @@ -1079,6 +1107,21 @@ struct position_t #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL #endif +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wunused-function") + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"") +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505)) +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142") +#else + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION +#endif + #if defined(JSON_HEDLEY_DEPRECATED) #undef JSON_HEDLEY_DEPRECATED #endif @@ -1091,7 +1134,7 @@ struct position_t #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since)) #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement)) #elif \ - JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) || \ + (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \ JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \ @@ -1101,7 +1144,8 @@ struct position_t JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since))) #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement))) #elif defined(__cplusplus) && (__cplusplus >= 201402L) @@ -1121,7 +1165,9 @@ struct position_t (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__)) #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__)) #elif \ @@ -1144,7 +1190,8 @@ struct position_t #if \ JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \ JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since))) #else #define JSON_HEDLEY_UNAVAILABLE(available_since) @@ -1172,7 +1219,8 @@ struct position_t JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__)) #elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L) @@ -1196,7 +1244,8 @@ struct position_t JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \ JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) + JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position))) #else #define JSON_HEDLEY_SENTINEL(position) @@ -1207,7 +1256,9 @@ struct position_t #endif #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) #define JSON_HEDLEY_NO_RETURN __noreturn -#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) +#elif \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L #define JSON_HEDLEY_NO_RETURN _Noreturn @@ -1229,7 +1280,8 @@ struct position_t (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return") @@ -1286,7 +1338,9 @@ struct position_t JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \ JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) + JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable() #elif defined(JSON_HEDLEY_ASSUME) #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0) @@ -1364,7 +1418,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check))) #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0) #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check)) @@ -1401,7 +1456,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP #endif #if \ (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) + JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability)) # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability)) # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability)) @@ -1422,7 +1478,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \ - JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) + JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) # define JSON_HEDLEY_PREDICT(expr, expected, probability) \ (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))) # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \ @@ -1468,7 +1525,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_MALLOC __attribute__((__malloc__)) #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory") @@ -1501,7 +1559,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) # define JSON_HEDLEY_PURE __attribute__((__pure__)) #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data") @@ -1537,7 +1596,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_CONST __attribute__((__const__)) #elif \ JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) @@ -1565,7 +1625,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \ JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \ - defined(__clang__) + defined(__clang__) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_RESTRICT __restrict #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus) #define JSON_HEDLEY_RESTRICT _Restrict @@ -1593,7 +1654,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_INLINE __inline #else #define JSON_HEDLEY_INLINE @@ -1619,7 +1681,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE #elif \ JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \ @@ -1661,7 +1725,9 @@ JSON_HEDLEY_DIAGNOSTIC_POP (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__)) #elif \ JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ @@ -1708,7 +1774,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \ ) \ - ) + ) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden"))) # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default"))) # else @@ -1724,7 +1791,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP #if \ JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \ JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__)) #elif \ JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \ @@ -1740,7 +1808,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP #endif #if \ JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) + JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__)) #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough) #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]]) @@ -1757,7 +1826,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP #endif #if \ JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) + JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__)) #elif defined(_Ret_notnull_) /* SAL */ #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_ @@ -1799,7 +1869,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \ JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \ (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \ - JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) + JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr) #endif #if !defined(__cplusplus) @@ -1823,7 +1894,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP !defined(JSON_HEDLEY_SUNPRO_VERSION) && \ !defined(JSON_HEDLEY_PGI_VERSION) && \ !defined(JSON_HEDLEY_IAR_VERSION)) || \ - JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) || \ + (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \ JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \ JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \ JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \ @@ -2000,7 +2071,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #if defined(JSON_HEDLEY_FLAGS) #undef JSON_HEDLEY_FLAGS #endif -#if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) +#if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion")) #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__)) #else #define JSON_HEDLEY_FLAGS @@ -25440,8 +25511,6 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std #undef JSON_EXPLICIT // #include - - #undef JSON_HEDLEY_ALWAYS_INLINE #undef JSON_HEDLEY_ARM_VERSION #undef JSON_HEDLEY_ARM_VERSION_CHECK @@ -25475,6 +25544,7 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS +#undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION #undef JSON_HEDLEY_DIAGNOSTIC_POP #undef JSON_HEDLEY_DIAGNOSTIC_PUSH #undef JSON_HEDLEY_DMC_VERSION @@ -25526,6 +25596,8 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std #undef JSON_HEDLEY_IS_CONSTEXPR_ #undef JSON_HEDLEY_LIKELY #undef JSON_HEDLEY_MALLOC +#undef JSON_HEDLEY_MCST_LCC_VERSION +#undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK #undef JSON_HEDLEY_MESSAGE #undef JSON_HEDLEY_MSVC_VERSION #undef JSON_HEDLEY_MSVC_VERSION_CHECK From ddbdb65834fe82d648cdd00b9e2965dac7a0377d Mon Sep 17 00:00:00 2001 From: abbaswasim Date: Tue, 2 Mar 2021 01:42:33 +0000 Subject: [PATCH 065/143] Fix amount of entries in the json object After the initial j.push_back() calls there is another j.emplace_back() call that makes the size == 4 not 3. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5d354f1ed1..26f8a60691 100644 --- a/README.md +++ b/README.md @@ -580,7 +580,7 @@ bool foo = j.at(2); j == "[\"foo\", 42, true, 1.78]"_json; // true // other stuff -j.size(); // 3 entries +j.size(); // 4 entries j.empty(); // false j.type(); // json::value_t::array j.clear(); // the array is empty again From 0a9ec38f44c06513ebd396b133b35ee970291d4e Mon Sep 17 00:00:00 2001 From: Remy Jette Date: Mon, 15 Mar 2021 17:30:58 -0700 Subject: [PATCH 066/143] Remove HEDLEY annotation from exception::what() The latest MSVC compiler throws the following warning on nlohmann::detail::exception::what() if /analyze is enabled: ``` vcruntime_exception.h(93) : warning C28204: 'what' has an override at `nlohmann\json\develop\single_include\nlohmann\json.hpp(2644)` and only the override is annotated for return: when an override is annotated, the base (this function) should be similarly annotated. ``` See https://godbolt.org/z/r331h4 --- include/nlohmann/detail/exceptions.hpp | 1 - single_include/nlohmann/json.hpp | 1 - 2 files changed, 2 deletions(-) diff --git a/include/nlohmann/detail/exceptions.hpp b/include/nlohmann/detail/exceptions.hpp index 5c9dce3c57..bd0534c438 100644 --- a/include/nlohmann/detail/exceptions.hpp +++ b/include/nlohmann/detail/exceptions.hpp @@ -49,7 +49,6 @@ class exception : public std::exception { public: /// returns the explanatory string - JSON_HEDLEY_RETURNS_NON_NULL const char* what() const noexcept override { return m.what(); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index a83971da21..d34b790056 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2640,7 +2640,6 @@ class exception : public std::exception { public: /// returns the explanatory string - JSON_HEDLEY_RETURNS_NON_NULL const char* what() const noexcept override { return m.what(); From 90d51b9205131106739ef06d09a75701eb4d406d Mon Sep 17 00:00:00 2001 From: Fraser Date: Thu, 18 Mar 2021 12:25:12 -0400 Subject: [PATCH 067/143] Update parse_exceptions.md Referring to https://github.com/nlohmann/json/blob/develop/doc/examples/parse_error.cpp and https://json.nlohmann.me/home/exceptions/ (and also based on testing), the catch command should reference `catch (json::parse_error& ex)` and not `catch (json::exception::parse_error& ex)` --- doc/mkdocs/docs/features/parsing/parse_exceptions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/mkdocs/docs/features/parsing/parse_exceptions.md b/doc/mkdocs/docs/features/parsing/parse_exceptions.md index b882e0b5ca..f0569b8e21 100644 --- a/doc/mkdocs/docs/features/parsing/parse_exceptions.md +++ b/doc/mkdocs/docs/features/parsing/parse_exceptions.md @@ -8,7 +8,7 @@ try { j = json::parse(my_input); } -catch (json::exception::parse_error& ex) +catch (json::parse_error& ex) { std::cerr << "parse error at byte " << ex.byte << std::endl; } From 6f551930e5c7ef397056de121c0da82f77573cca Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 24 Mar 2021 07:15:18 +0100 Subject: [PATCH 068/143] :rotating_light: add new CI and fix warnings (#2561) * :alembic: move CI targets to CMake * :recycle: add target for cpplint * :recycle: add target for self-contained binaries * :recycle: add targets for iwyu and infer * :loud_sound: add version output * :recycle: add target for oclint * :rotating_light: fix warnings * :recycle: rename targets * :recycle: use iwyu properly * :rotating_light: fix warnings * :recycle: use iwyu properly * :recycle: add target for benchmarks * :recycle: add target for CMake flags * :construction_worker: use GitHub Actions * :alembic: try to install Clang 11 * :alembic: try to install GCC 11 * :alembic: try to install Clang 11 * :alembic: try to install GCC 11 * :alembic: add clang analyze target * :fire: remove Google Benchmark * :arrow_up: Google Benchmark 1.5.2 * :fire: use fetchcontent * :penguin: add target to download a Linux version of CMake * :hammer: fix dependency * :rotating_light: fix includes * :rotating_light: fix comment * :wrench: adjust flags for GCC 11.0.0 20210110 (experimental) * :whale: user Docker image to run CI * :wrench: add target for Valgrind * :construction_worker: add target for Valgrind tests * :alembic: add Dart * :rewind: remove Dart * :alembic: do not call ctest in test subdirectory * :alembic: download test data explicitly * :alembic: only execute Valgrind tests * :alembic: fix labels * :fire: remove unneeded jobs * :hammer: cleanup * :bug: fix OCLint call * :white_check_mark: add targets for offline and git-independent tests * :white_check_mark: add targets for C++ language versions and reproducible tests * :hammer: clean up * :construction_worker: add CI steps for cppcheck and cpplint * :rotating_light: fix warnings from Clang-Tidy * :construction_worker: add CI steps for Clang-Tidy * :rotating_light: fix warnings * :wrench: select proper binary * :rotating_light: fix warnings * :rotating_light: suppress some unhelpful warnings * :rotating_light: fix warnings * :art: fix format * :rotating_light: fix warnings * :construction_worker: add CI steps for Sanitizers * :rotating_light: fix warnings * :zap: add optimization to sanitizer build * :rotating_light: fix warnings * :rotating_light: add missing header * :rotating_light: fix warnings * :construction_worker: add CI step for coverage * :construction_worker: add CI steps for disabled exceptions and implicit conversions * :rotating_light: fix warnings * :construction_worker: add CI steps for checking indentation * :bug: fix variable use * :green_heart: fix build * :heavy_minus_sign: remove CircleCI * :construction_worker: add CI step for diagnostics * :rotating_light: fix warning * :fire: clean Travis --- .circleci/config.yml | 56 - .clang-tidy | 21 + .github/workflows/macos.yml | 2 +- .github/workflows/ubuntu.yml | 205 ++- .github/workflows/windows.yml | 8 +- .travis.yml | 102 +- CMakeLists.txt | 5 + Makefile | 2 + README.md | 10 +- benchmarks/CMakeLists.txt | 23 +- benchmarks/thirdparty/benchmark/AUTHORS | 46 - benchmarks/thirdparty/benchmark/BUILD.bazel | 42 - .../thirdparty/benchmark/CMakeLists.txt | 251 --- .../thirdparty/benchmark/CONTRIBUTING.md | 58 - benchmarks/thirdparty/benchmark/CONTRIBUTORS | 65 - benchmarks/thirdparty/benchmark/LICENSE | 202 --- benchmarks/thirdparty/benchmark/README.md | 950 ----------- benchmarks/thirdparty/benchmark/WORKSPACE | 7 - benchmarks/thirdparty/benchmark/appveyor.yml | 56 - .../benchmark/cmake/AddCXXCompilerFlag.cmake | 74 - .../benchmark/cmake/CXXFeatureCheck.cmake | 64 - .../benchmark/cmake/Config.cmake.in | 1 - .../benchmark/cmake/GetGitVersion.cmake | 54 - .../benchmark/cmake/HandleGTest.cmake | 113 -- .../benchmark/cmake/Modules/FindLLVMAr.cmake | 16 - .../benchmark/cmake/Modules/FindLLVMNm.cmake | 16 - .../cmake/Modules/FindLLVMRanLib.cmake | 15 - .../benchmark/cmake/benchmark.pc.in | 11 - .../benchmark/cmake/gnu_posix_regex.cpp | 12 - .../benchmark/cmake/llvm-toolchain.cmake | 8 - .../benchmark/cmake/posix_regex.cpp | 14 - .../benchmark/cmake/split_list.cmake | 3 - .../thirdparty/benchmark/cmake/std_regex.cpp | 10 - .../benchmark/cmake/steady_clock.cpp | 7 - .../cmake/thread_safety_attributes.cpp | 4 - .../benchmark/docs/AssemblyTests.md | 147 -- benchmarks/thirdparty/benchmark/docs/tools.md | 242 --- .../benchmark/include/benchmark/benchmark.h | 1456 ----------------- benchmarks/thirdparty/benchmark/mingw.py | 320 ---- benchmarks/thirdparty/benchmark/releasing.md | 16 - .../thirdparty/benchmark/src/CMakeLists.txt | 105 -- .../thirdparty/benchmark/src/arraysize.h | 33 - .../thirdparty/benchmark/src/benchmark.cc | 630 ------- .../benchmark/src/benchmark_api_internal.h | 47 - .../benchmark/src/benchmark_main.cc | 17 - .../benchmark/src/benchmark_register.cc | 461 ------ .../benchmark/src/benchmark_register.h | 33 - benchmarks/thirdparty/benchmark/src/check.h | 79 - .../thirdparty/benchmark/src/colorprint.cc | 188 --- .../thirdparty/benchmark/src/colorprint.h | 33 - .../benchmark/src/commandlineflags.cc | 218 --- .../benchmark/src/commandlineflags.h | 79 - .../thirdparty/benchmark/src/complexity.cc | 220 --- .../thirdparty/benchmark/src/complexity.h | 55 - .../benchmark/src/console_reporter.cc | 182 --- .../thirdparty/benchmark/src/counter.cc | 68 - benchmarks/thirdparty/benchmark/src/counter.h | 26 - .../thirdparty/benchmark/src/csv_reporter.cc | 149 -- .../thirdparty/benchmark/src/cycleclock.h | 177 -- .../benchmark/src/internal_macros.h | 89 - .../thirdparty/benchmark/src/json_reporter.cc | 205 --- benchmarks/thirdparty/benchmark/src/log.h | 73 - benchmarks/thirdparty/benchmark/src/mutex.h | 155 -- benchmarks/thirdparty/benchmark/src/re.h | 152 -- .../thirdparty/benchmark/src/reporter.cc | 87 - benchmarks/thirdparty/benchmark/src/sleep.cc | 51 - benchmarks/thirdparty/benchmark/src/sleep.h | 15 - .../thirdparty/benchmark/src/statistics.cc | 178 -- .../thirdparty/benchmark/src/statistics.h | 37 - .../thirdparty/benchmark/src/string_util.cc | 172 -- .../thirdparty/benchmark/src/string_util.h | 40 - .../thirdparty/benchmark/src/sysinfo.cc | 587 ------- .../thirdparty/benchmark/src/thread_manager.h | 66 - .../thirdparty/benchmark/src/thread_timer.h | 69 - benchmarks/thirdparty/benchmark/src/timers.cc | 217 --- benchmarks/thirdparty/benchmark/src/timers.h | 48 - .../thirdparty/benchmark/tools/compare.py | 316 ---- .../benchmark/tools/compare_bench.py | 67 - .../tools/gbench/Inputs/test1_run1.json | 102 -- .../tools/gbench/Inputs/test1_run2.json | 102 -- .../tools/gbench/Inputs/test2_run.json | 81 - .../benchmark/tools/gbench/__init__.py | 8 - .../benchmark/tools/gbench/report.py | 208 --- .../thirdparty/benchmark/tools/gbench/util.py | 159 -- .../thirdparty/benchmark/tools/strip_asm.py | 151 -- cmake/ci.cmake | 794 +++++++++ .../nlohmann/byte_container_with_subtype.hpp | 24 +- .../nlohmann/detail/conversions/from_json.hpp | 8 +- .../nlohmann/detail/conversions/to_chars.hpp | 4 +- .../nlohmann/detail/conversions/to_json.hpp | 16 +- include/nlohmann/detail/exceptions.hpp | 4 +- include/nlohmann/detail/hash.hpp | 5 +- .../nlohmann/detail/input/binary_reader.hpp | 32 +- .../nlohmann/detail/input/input_adapters.hpp | 18 +- include/nlohmann/detail/input/json_sax.hpp | 15 +- include/nlohmann/detail/input/lexer.hpp | 12 +- include/nlohmann/detail/input/parser.hpp | 3 +- .../nlohmann/detail/iterators/iter_impl.hpp | 17 +- .../detail/iterators/iteration_proxy.hpp | 5 +- .../iterators/json_reverse_iterator.hpp | 4 +- .../detail/iterators/primitive_iterator.hpp | 4 +- include/nlohmann/detail/json_pointer.hpp | 32 +- include/nlohmann/detail/json_ref.hpp | 2 +- include/nlohmann/detail/macro_scope.hpp | 6 - include/nlohmann/detail/macro_unscope.hpp | 3 - .../nlohmann/detail/output/binary_writer.hpp | 20 +- .../detail/output/output_adapters.hpp | 6 + include/nlohmann/detail/output/serializer.hpp | 31 +- include/nlohmann/json.hpp | 387 ++--- include/nlohmann/ordered_map.hpp | 4 + include/nlohmann/thirdparty/hedley/hedley.hpp | 2 + .../thirdparty/hedley/hedley_undef.hpp | 2 + single_include/nlohmann/json.hpp | 667 ++++---- test/CMakeLists.txt | 54 +- test/src/UBSAN.supp | 1 - test/src/unit-algorithms.cpp | 4 +- test/src/unit-allocator.cpp | 30 +- test/src/unit-alt-string.cpp | 20 +- test/src/unit-bson.cpp | 22 +- test/src/unit-capacity.cpp | 20 +- test/src/unit-cbor.cpp | 78 +- test/src/unit-class_lexer.cpp | 10 +- test/src/unit-class_parser.cpp | 211 +-- test/src/unit-comparison.cpp | 8 +- test/src/unit-constructor1.cpp | 84 +- test/src/unit-constructor2.cpp | 32 +- test/src/unit-convenience.cpp | 4 +- test/src/unit-conversions.cpp | 86 +- test/src/unit-deserialization.cpp | 127 +- test/src/unit-diagnostics.cpp | 3 +- test/src/unit-element_access2.cpp | 68 +- test/src/unit-items.cpp | 48 +- test/src/unit-json_patch.cpp | 2 +- test/src/unit-json_pointer.cpp | 2 +- test/src/unit-msgpack.cpp | 76 +- test/src/unit-noexcept.cpp | 20 +- test/src/unit-ordered_map.cpp | 1 + test/src/unit-readme.cpp | 12 +- test/src/unit-reference_access.cpp | 30 +- test/src/unit-regression1.cpp | 60 +- test/src/unit-regression2.cpp | 71 +- test/src/unit-serialization.cpp | 6 +- test/src/unit-testsuites.cpp | 56 +- test/src/unit-to_chars.cpp | 26 +- test/src/unit-ubjson.cpp | 66 +- test/src/unit-udt.cpp | 99 +- test/src/unit-udt_macro.cpp | 6 +- test/src/unit-unicode.cpp | 8 +- test/src/unit-user_defined_input.cpp | 15 +- test/src/unit-wstring.cpp | 2 +- test/thirdparty/doctest/doctest.h | 86 +- test/thirdparty/fifo_map/fifo_map.hpp | 8 +- 152 files changed, 2492 insertions(+), 12146 deletions(-) delete mode 100644 .circleci/config.yml delete mode 100755 benchmarks/thirdparty/benchmark/AUTHORS delete mode 100755 benchmarks/thirdparty/benchmark/BUILD.bazel delete mode 100755 benchmarks/thirdparty/benchmark/CMakeLists.txt delete mode 100755 benchmarks/thirdparty/benchmark/CONTRIBUTING.md delete mode 100755 benchmarks/thirdparty/benchmark/CONTRIBUTORS delete mode 100755 benchmarks/thirdparty/benchmark/LICENSE delete mode 100755 benchmarks/thirdparty/benchmark/README.md delete mode 100755 benchmarks/thirdparty/benchmark/WORKSPACE delete mode 100755 benchmarks/thirdparty/benchmark/appveyor.yml delete mode 100755 benchmarks/thirdparty/benchmark/cmake/AddCXXCompilerFlag.cmake delete mode 100755 benchmarks/thirdparty/benchmark/cmake/CXXFeatureCheck.cmake delete mode 100755 benchmarks/thirdparty/benchmark/cmake/Config.cmake.in delete mode 100755 benchmarks/thirdparty/benchmark/cmake/GetGitVersion.cmake delete mode 100755 benchmarks/thirdparty/benchmark/cmake/HandleGTest.cmake delete mode 100755 benchmarks/thirdparty/benchmark/cmake/Modules/FindLLVMAr.cmake delete mode 100755 benchmarks/thirdparty/benchmark/cmake/Modules/FindLLVMNm.cmake delete mode 100755 benchmarks/thirdparty/benchmark/cmake/Modules/FindLLVMRanLib.cmake delete mode 100755 benchmarks/thirdparty/benchmark/cmake/benchmark.pc.in delete mode 100755 benchmarks/thirdparty/benchmark/cmake/gnu_posix_regex.cpp delete mode 100755 benchmarks/thirdparty/benchmark/cmake/llvm-toolchain.cmake delete mode 100755 benchmarks/thirdparty/benchmark/cmake/posix_regex.cpp delete mode 100755 benchmarks/thirdparty/benchmark/cmake/split_list.cmake delete mode 100755 benchmarks/thirdparty/benchmark/cmake/std_regex.cpp delete mode 100755 benchmarks/thirdparty/benchmark/cmake/steady_clock.cpp delete mode 100755 benchmarks/thirdparty/benchmark/cmake/thread_safety_attributes.cpp delete mode 100755 benchmarks/thirdparty/benchmark/docs/AssemblyTests.md delete mode 100755 benchmarks/thirdparty/benchmark/docs/tools.md delete mode 100755 benchmarks/thirdparty/benchmark/include/benchmark/benchmark.h delete mode 100755 benchmarks/thirdparty/benchmark/mingw.py delete mode 100755 benchmarks/thirdparty/benchmark/releasing.md delete mode 100755 benchmarks/thirdparty/benchmark/src/CMakeLists.txt delete mode 100755 benchmarks/thirdparty/benchmark/src/arraysize.h delete mode 100755 benchmarks/thirdparty/benchmark/src/benchmark.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/benchmark_api_internal.h delete mode 100755 benchmarks/thirdparty/benchmark/src/benchmark_main.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/benchmark_register.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/benchmark_register.h delete mode 100755 benchmarks/thirdparty/benchmark/src/check.h delete mode 100755 benchmarks/thirdparty/benchmark/src/colorprint.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/colorprint.h delete mode 100755 benchmarks/thirdparty/benchmark/src/commandlineflags.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/commandlineflags.h delete mode 100755 benchmarks/thirdparty/benchmark/src/complexity.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/complexity.h delete mode 100755 benchmarks/thirdparty/benchmark/src/console_reporter.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/counter.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/counter.h delete mode 100755 benchmarks/thirdparty/benchmark/src/csv_reporter.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/cycleclock.h delete mode 100755 benchmarks/thirdparty/benchmark/src/internal_macros.h delete mode 100755 benchmarks/thirdparty/benchmark/src/json_reporter.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/log.h delete mode 100755 benchmarks/thirdparty/benchmark/src/mutex.h delete mode 100755 benchmarks/thirdparty/benchmark/src/re.h delete mode 100755 benchmarks/thirdparty/benchmark/src/reporter.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/sleep.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/sleep.h delete mode 100755 benchmarks/thirdparty/benchmark/src/statistics.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/statistics.h delete mode 100755 benchmarks/thirdparty/benchmark/src/string_util.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/string_util.h delete mode 100755 benchmarks/thirdparty/benchmark/src/sysinfo.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/thread_manager.h delete mode 100755 benchmarks/thirdparty/benchmark/src/thread_timer.h delete mode 100755 benchmarks/thirdparty/benchmark/src/timers.cc delete mode 100755 benchmarks/thirdparty/benchmark/src/timers.h delete mode 100755 benchmarks/thirdparty/benchmark/tools/compare.py delete mode 100755 benchmarks/thirdparty/benchmark/tools/compare_bench.py delete mode 100755 benchmarks/thirdparty/benchmark/tools/gbench/Inputs/test1_run1.json delete mode 100755 benchmarks/thirdparty/benchmark/tools/gbench/Inputs/test1_run2.json delete mode 100755 benchmarks/thirdparty/benchmark/tools/gbench/Inputs/test2_run.json delete mode 100755 benchmarks/thirdparty/benchmark/tools/gbench/__init__.py delete mode 100755 benchmarks/thirdparty/benchmark/tools/gbench/report.py delete mode 100755 benchmarks/thirdparty/benchmark/tools/gbench/util.py delete mode 100755 benchmarks/thirdparty/benchmark/tools/strip_asm.py create mode 100644 cmake/ci.cmake delete mode 100644 test/src/UBSAN.supp diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 82e5098408..0000000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,56 +0,0 @@ -version: 2 -jobs: - build_stable: - docker: - - image: debian:stretch - - steps: - - checkout - - - run: - name: Install required tools - command: 'apt-get update && apt-get install -y gcc g++ git cmake' - - run: - name: Run CMake - command: 'mkdir build ; cd build ; cmake .. -DJSON_BuildTests=On' - - run: - name: Compile - command: 'cmake --build build' - - run: - name: Execute test suite - command: 'cd build ; ctest --output-on-failure -j 2' - - build_bleeding_edge: - docker: - - image: archlinux - - steps: - - checkout - - - run: - name: Install required tools - command: 'pacman -Sy --noconfirm base base-devel gcc git cmake' - - run: - name: Run CMake - command: 'mkdir build ; cd build ; cmake .. -DJSON_BuildTests=On' - - run: - name: Compile - command: 'cmake --build build' - - run: - name: Execute test suite - command: 'cd build ; ctest --output-on-failure -j 2' - -workflows: - version: 2 - build_and_test_all: - jobs: - - build_stable: - filters: - branches: - ignore: - gh-pages - - build_bleeding_edge: - filters: - branches: - ignore: - gh-pages diff --git a/.clang-tidy b/.clang-tidy index 046d84f870..a28335936d 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,23 +1,44 @@ Checks: '*, + -android-cloexec-fopen, -cppcoreguidelines-avoid-goto, -cppcoreguidelines-avoid-magic-numbers, + -cppcoreguidelines-avoid-non-const-global-variables, -cppcoreguidelines-macro-usage, + -cppcoreguidelines-pro-bounds-array-to-pointer-decay, + -cppcoreguidelines-pro-bounds-constant-array-index, + -cppcoreguidelines-pro-bounds-pointer-arithmetic, + -cppcoreguidelines-pro-type-reinterpret-cast, + -cppcoreguidelines-pro-type-union-access, -fuchsia-default-arguments-calls, -fuchsia-default-arguments-declarations, -fuchsia-overloaded-operator, -google-explicit-constructor, + -google-readability-function-size, + -google-runtime-int, -google-runtime-references, -hicpp-avoid-goto, -hicpp-explicit-conversions, + -hicpp-function-size, -hicpp-no-array-decay, + -hicpp-no-assembler, + -hicpp-signed-bitwise, -hicpp-uppercase-literal-suffix, -llvm-header-guard, -llvm-include-order, + -llvmlibc-*, + -misc-no-recursion, -misc-non-private-member-variables-in-classes, -modernize-use-trailing-return-type, + -readability-function-size, -readability-magic-numbers, + -readability-redundant-access-specifiers, -readability-uppercase-literal-suffix' CheckOptions: - key: hicpp-special-member-functions.AllowSoleDefaultDtor value: 1 + +WarningsAsErrors: '*' + +#HeaderFilterRegex: '.*nlohmann.*' +HeaderFilterRegex: '.*hpp$' diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 5b178ad055..aad3a787de 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -8,7 +8,7 @@ jobs: runs-on: macos-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: cmake run: cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On - name: build diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index d6b6540779..d09b640fe4 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -3,62 +3,177 @@ name: Ubuntu on: [push, pull_request] jobs: - gcc_build: + ci_test_clang_cxx20: runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 - - name: install_gcc + - uses: actions/checkout@v2 + - name: install_clang run: | - sudo apt update - sudo apt install gcc-10 g++-10 + sudo apt update + sudo apt install clang-10 ninja-build shell: bash - name: cmake - run: cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On - env: - CC: gcc-10 - CXX: g++-10 + run: cmake -S . -B build -DJSON_CI=On - name: build - run: cmake --build build --parallel 10 - - name: test - run: cd build ; ctest -j 10 --output-on-failure + run: cmake --build build --target ci_test_clang_cxx20 - clang_build: + ci_clang_analyze: runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: install_ninja + run: | + sudo apt update + sudo apt install ninja-build + shell: bash + - name: install_clang + run: | + wget https://apt.llvm.org/llvm.sh + chmod +x llvm.sh + sudo ./llvm.sh 11 + sudo apt-get install clang-tools-11 + shell: bash + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_clang_analyze + ci_test_clang: + runs-on: ubuntu-latest steps: - - uses: actions/checkout@v1 - - name: install_gcc - run: | - sudo apt update - sudo apt install clang-10 - shell: bash - - name: cmake - run: cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On - env: - CC: clang-10 - CXX: clang++-10 - - name: build - run: cmake --build build --parallel 10 - - name: test - run: cd build ; ctest -j 10 --output-on-failure + - uses: actions/checkout@v2 + - name: install_ninja + run: | + sudo apt update + sudo apt install ninja-build + shell: bash + - name: install_clang + run: | + wget https://apt.llvm.org/llvm.sh + chmod +x llvm.sh + sudo ./llvm.sh 11 + sudo apt-get install clang-tools-11 + shell: bash + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_test_clang - clang_build_cxx20: + ci_test_gcc: runs-on: ubuntu-latest + container: nlohmann/json-ci:latest + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_test_gcc + ci_test_valgrind: + runs-on: ubuntu-latest + container: nlohmann/json-ci:latest steps: - - uses: actions/checkout@v1 - - name: install_gcc - run: | - sudo apt update - sudo apt install clang-10 - shell: bash - - name: cmake - run: cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On -DCMAKE_CXX_STANDARD=20 -DCMAKE_CXX_STANDARD_REQUIRED=ON - env: - CC: clang-10 - CXX: clang++-10 - - name: build - run: cmake --build build --parallel 10 - - name: test - run: cd build ; ctest -j 10 --output-on-failure + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_test_valgrind + + ci_cppcheck: + runs-on: ubuntu-latest + container: nlohmann/json-ci:latest + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_cppcheck + + ci_cpplint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_cpplint + + ci_clang_tidy: + runs-on: ubuntu-latest + container: nlohmann/json-ci:latest + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_clang_tidy + + ci_test_amalgamation: + runs-on: ubuntu-latest + container: nlohmann/json-ci:latest + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_test_amalgamation + + ci_test_diagnostics: + runs-on: ubuntu-latest + container: nlohmann/json-ci:latest + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_test_diagnostics + + ci_test_noexceptions: + runs-on: ubuntu-latest + container: nlohmann/json-ci:latest + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_test_noexceptions + + ci_test_noimplicitconversions: + runs-on: ubuntu-latest + container: nlohmann/json-ci:latest + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_test_noimplicitconversions + + ci_test_clang_sanitizer: + runs-on: ubuntu-latest + container: nlohmann/json-ci:latest + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_test_clang_sanitizer + + ci_test_coverage: + runs-on: ubuntu-latest + container: nlohmann/json-ci:latest + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_test_coverage + - name: archive coverage report + uses: actions/upload-artifact@v2 + with: + name: code-coverage-report + path: /__w/json/json/build/html + - name: Coveralls + uses: coverallsapp/github-action@master + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + path-to-lcov: /__w/json/json/build/json.info.filtered.noexcept diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 1778c94182..ae243762fe 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -7,7 +7,7 @@ jobs: runs-on: windows-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: cmake run: cmake -S . -B build -G "Visual Studio 16 2019" -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On - name: build @@ -19,7 +19,7 @@ jobs: runs-on: windows-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: install Clang run: curl -fsSL -o LLVM10.exe https://github.com/llvm/llvm-project/releases/download/llvmorg-10.0.0/LLVM-10.0.0-win64.exe ; 7z x LLVM10.exe -y -o"C:/Program Files/LLVM" - name: cmake @@ -33,7 +33,7 @@ jobs: runs-on: windows-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: cmake run: cmake -S . -B build -G "Visual Studio 16 2019" -A x64 -T ClangCL -DJSON_BuildTests=On - name: build @@ -45,7 +45,7 @@ jobs: runs-on: windows-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: cmake run: cmake -S . -B build -G "Visual Studio 16 2019" -A Win32 -T ClangCL -DJSON_BuildTests=On - name: build diff --git a/.travis.yml b/.travis.yml index f48ee1fd6d..cfa68c5233 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,87 +17,6 @@ group: edge matrix: include: - # Valgrind - - os: linux - compiler: gcc - env: - - COMPILER=g++-4.9 - - CMAKE_OPTIONS=-DJSON_Valgrind=ON - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-4.9', 'valgrind', 'ninja-build'] - - # clang sanitizer - - os: linux - compiler: clang - env: - - COMPILER=clang++-7 - - CMAKE_OPTIONS=-DJSON_Sanitizer=ON - - UBSAN_OPTIONS=print_stacktrace=1,suppressions=$(pwd)/test/src/UBSAN.supp - addons: - apt: - sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-7'] - packages: ['g++-6', 'clang-7', 'ninja-build'] - before_script: - - export PATH=$PATH:/usr/lib/llvm-7/bin - - # cppcheck - - os: linux - compiler: gcc - env: - - COMPILER=g++-4.9 - - SPECIAL=cppcheck - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-4.9', 'cppcheck', 'ninja-build'] - after_success: - - make cppcheck - - # no exceptions - - os: linux - compiler: gcc - env: - - COMPILER=g++-4.9 - - CMAKE_OPTIONS=-DJSON_NoExceptions=ON - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-4.9', 'ninja-build'] - - # check amalgamation - - os: linux - compiler: gcc - env: - - COMPILER=g++-4.9 - - SPECIAL=amalgamation - - MULTIPLE_HEADERS=ON - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-4.9', 'astyle', 'ninja-build'] - after_success: - - make check-amalgamation - - # Coveralls (http://gronlier.fr/blog/2015/01/adding-code-coverage-to-your-c-project/) - - - os: linux - compiler: gcc - dist: bionic - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-7', 'ninja-build'] - before_script: - - pip install --user cpp-coveralls - after_success: - - coveralls --build-root test --include include/nlohmann --gcov 'gcov-7' --gcov-options '\-lp' - env: - - COMPILER=g++-7 - - CMAKE_OPTIONS=-DJSON_Coverage=ON - - MULTIPLE_HEADERS=ON - # Coverity (only for branch coverity_scan) - os: linux @@ -133,11 +52,6 @@ matrix: - os: osx osx_image: xcode12 - - os: osx - osx_image: xcode12 - env: - - IMPLICIT_CONVERSIONS=OFF - # Linux / GCC - os: linux @@ -196,16 +110,6 @@ matrix: sources: ['ubuntu-toolchain-r-test'] packages: ['g++-9', 'ninja-build'] - - os: linux - compiler: gcc - env: - - COMPILER=g++-9 - - IMPLICIT_CONVERSIONS=OFF - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-9', 'ninja-build'] - - os: linux compiler: gcc env: @@ -316,17 +220,13 @@ script: # make sure CXX is correctly set - if [[ "${COMPILER}" != "" ]]; then export CXX=${COMPILER}; fi - # by default, use the single-header version - - if [[ "${MULTIPLE_HEADERS}" == "" ]]; then export MULTIPLE_HEADERS=OFF; fi - # by default, use implicit conversions - - if [[ "${IMPLICIT_CONVERSIONS}" == "" ]]; then export IMPLICIT_CONVERSIONS=ON; fi # append CXX_STANDARD to CMAKE_OPTIONS if required - CMAKE_OPTIONS+=${CXX_STANDARD:+ -DCMAKE_CXX_STANDARD=$CXX_STANDARD -DCMAKE_CXX_STANDARD_REQUIRED=ON} # compile and execute unit tests - mkdir -p build && cd build - - cmake .. ${CMAKE_OPTIONS} -DJSON_MultipleHeaders=${MULTIPLE_HEADERS} -DJSON_ImplicitConversions=${IMPLICIT_CONVERSIONS} -DJSON_BuildTests=On -GNinja && cmake --build . --config Release + - cmake .. ${CMAKE_OPTIONS} -DJSON_BuildTests=On -GNinja && cmake --build . --config Release - ctest -C Release --timeout 2700 -V -j - cd .. diff --git a/CMakeLists.txt b/CMakeLists.txt index 36f1cf7056..1636575594 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,11 @@ option(JSON_Install "Install CMake targets during install step." ${MAIN_PROJECT} option(JSON_MultipleHeaders "Use non-amalgamated version of the library." OFF) option(JSON_ImplicitConversions "Enable implicit conversions." ON) option(JSON_Diagnostics "Enable better diagnostic messages." OFF) +option(JSON_CI "Enable CI build targets." OFF) + +if (JSON_CI) + include(cmake/ci.cmake) +endif () ## ## CONFIGURATION diff --git a/Makefile b/Makefile index d3963f503e..655bb6c4ad 100644 --- a/Makefile +++ b/Makefile @@ -641,4 +641,6 @@ update_hedley: curl https://raw.githubusercontent.com/nemequ/hedley/master/hedley.h -o include/nlohmann/thirdparty/hedley/hedley.hpp $(SED) -i 's/HEDLEY_/JSON_HEDLEY_/g' include/nlohmann/thirdparty/hedley/hedley.hpp grep "[[:blank:]]*#[[:blank:]]*undef" include/nlohmann/thirdparty/hedley/hedley.hpp | grep -v "__" | sort | uniq | $(SED) 's/ //g' | $(SED) 's/undef/undef /g' > include/nlohmann/thirdparty/hedley/hedley_undef.hpp + $(SED) -i '1s/^/#pragma once\n\n/' include/nlohmann/thirdparty/hedley/hedley.hpp + $(SED) -i '1s/^/#pragma once\n\n/' include/nlohmann/thirdparty/hedley/hedley_undef.hpp $(MAKE) amalgamate diff --git a/README.md b/README.md index 26f8a60691..d428e4c473 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ [![Ubuntu](https://github.com/nlohmann/json/workflows/Ubuntu/badge.svg)](https://github.com/nlohmann/json/actions?query=workflow%3AUbuntu) [![macOS](https://github.com/nlohmann/json/workflows/macOS/badge.svg)](https://github.com/nlohmann/json/actions?query=workflow%3AmacOS) [![Windows](https://github.com/nlohmann/json/workflows/Windows/badge.svg)](https://github.com/nlohmann/json/actions?query=workflow%3AWindows) -[![Build Status](https://circleci.com/gh/nlohmann/json.svg?style=svg)](https://circleci.com/gh/nlohmann/json) [![Coverage Status](https://coveralls.io/repos/github/nlohmann/json/badge.svg?branch=develop)](https://coveralls.io/github/nlohmann/json?branch=develop) [![Coverity Scan Build Status](https://scan.coverity.com/projects/5550/badge.svg)](https://scan.coverity.com/projects/nlohmann-json) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/f3732b3327e34358a0e9d1fe9f661f08)](https://www.codacy.com/app/nlohmann/json?utm_source=github.com&utm_medium=referral&utm_content=nlohmann/json&utm_campaign=Badge_Grade) @@ -365,7 +364,7 @@ The above example can also be expressed explicitly using [`json::parse()`](https ```cpp // parse explicitly -auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }"); +auto j3 = json::parse(R"({"happy": true, "pi": 3.141})"); ``` You can also get a string representation of a JSON value (serialize): @@ -577,7 +576,7 @@ j[1] = 42; bool foo = j.at(2); // comparison -j == "[\"foo\", 42, true, 1.78]"_json; // true +j == R"(["foo", 1, true, 1.78])"_json; // true // other stuff j.size(); // 4 entries @@ -1228,7 +1227,7 @@ Please note: - Unsupported versions of GCC and Clang are rejected by `#error` directives. This can be switched off by defining `JSON_SKIP_UNSUPPORTED_COMPILER_CHECK`. Note that you can expect no support in this case. -The following compilers are currently used in continuous integration at [Travis](https://travis-ci.org/nlohmann/json), [AppVeyor](https://ci.appveyor.com/project/nlohmann/json), [GitHub Actions](https://github.com/nlohmann/json/actions), and [CircleCI](https://circleci.com/gh/nlohmann/json): +The following compilers are currently used in continuous integration at [Travis](https://travis-ci.org/nlohmann/json), [AppVeyor](https://ci.appveyor.com/project/nlohmann/json), and [GitHub Actions](https://github.com/nlohmann/json/actions): | Compiler | Operating System | CI Provider | |-------------------------------------------------------------------|--------------------|----------------| @@ -1251,14 +1250,12 @@ The following compilers are currently used in continuous integration at [Travis] | GCC 4.8.5 (Ubuntu 4.8.5-4ubuntu8\~14.04.2) | Ubuntu 14.04.5 LTS | Travis | | GCC 4.9.4 (Ubuntu 4.9.4-2ubuntu1\~14.04.1) | Ubuntu 14.04.5 LTS | Travis | | GCC 5.5.0 (Ubuntu 5.5.0-12ubuntu1\~14.04) | Ubuntu 14.04.5 LTS | Travis | -| GCC 6.3.0 (Debian 6.3.0-18+deb9u1) | Debian 9 | Circle CI | | GCC 6.5.0 (Ubuntu 6.5.0-2ubuntu1\~14.04.1) | Ubuntu 14.04.5 LTS | Travis | | GCC 7.3.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project) | Windows-6.3.9600 | AppVeyor | | GCC 7.5.0 (Ubuntu 7.5.0-3ubuntu1\~14.04.1) | Ubuntu 14.04.5 LTS | Travis | | GCC 7.5.0 (Ubuntu 7.5.0-3ubuntu1\~18.04) | Ubuntu 18.04.4 LTS | GitHub Actions | | GCC 8.4.0 (Ubuntu 8.4.0-1ubuntu1\~14.04) | Ubuntu 14.04.5 LTS | Travis | | GCC 9.3.0 (Ubuntu 9.3.0-11ubuntu0\~14.04) | Ubuntu 14.04.5 LTS | Travis | -| GCC 10.1.0 (Arch Linux latest) | Arch Linux | Circle CI | | MSVC 19.0.24241.7 (Build Engine version 14.0.25420.1) | Windows-6.3.9600 | AppVeyor | | MSVC 19.16.27035.0 (15.9.21+g9802d43bc3 for .NET Framework) | Windows-10.0.14393 | AppVeyor | | MSVC 19.25.28614.0 (Build Engine version 16.5.0+d4cbfca49 for .NET Framework) | Windows-10.0.17763 | AppVeyor | @@ -1541,7 +1538,6 @@ The library itself consists of a single header file licensed under the MIT licen - [**American fuzzy lop**](https://lcamtuf.coredump.cx/afl/) for fuzz testing - [**AppVeyor**](https://www.appveyor.com) for [continuous integration](https://ci.appveyor.com/project/nlohmann/json) on Windows - [**Artistic Style**](http://astyle.sourceforge.net) for automatic source code indentation -- [**CircleCI**](https://circleci.com) for [continuous integration](https://circleci.com/gh/nlohmann/json). - [**Clang**](https://clang.llvm.org) for compilation with code sanitizers - [**CMake**](https://cmake.org) for build automation - [**Codacity**](https://www.codacy.com) for further [code analysis](https://www.codacy.com/app/nlohmann/json) diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index 86063dbada..ee4db1912d 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -1,18 +1,25 @@ -cmake_minimum_required(VERSION 3.8) +cmake_minimum_required(VERSION 3.11) project(JSON_Benchmarks LANGUAGES CXX) # set compiler flags if((CMAKE_CXX_COMPILER_ID MATCHES GNU) OR (CMAKE_CXX_COMPILER_ID MATCHES Clang)) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto -DNDEBUG -O3") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto -DNDEBUG -O3") endif() # configure Google Benchmarks -set(BENCHMARK_ENABLE_TESTING OFF CACHE INTERNAL "" FORCE) -add_subdirectory(thirdparty/benchmark) +include(FetchContent) +FetchContent_Declare( + benchmark + GIT_REPOSITORY https://github.com/google/benchmark.git + GIT_SHALLOW TRUE +) -# header directories -include_directories(thirdparty) -include_directories(${CMAKE_SOURCE_DIR}/../single_include) +FetchContent_GetProperties(benchmark) +if(NOT benchmark_POPULATED) + FetchContent_Populate(benchmark) + set(BENCHMARK_ENABLE_TESTING OFF CACHE INTERNAL "" FORCE) + add_subdirectory(${benchmark_SOURCE_DIR} ${benchmark_BINARY_DIR}) +endif() # download test data include(${CMAKE_SOURCE_DIR}/../cmake/download_test_data.cmake) @@ -22,4 +29,4 @@ add_executable(json_benchmarks src/benchmarks.cpp) target_compile_features(json_benchmarks PRIVATE cxx_std_11) target_link_libraries(json_benchmarks benchmark ${CMAKE_THREAD_LIBS_INIT}) add_dependencies(json_benchmarks download_test_data) -target_include_directories(json_benchmarks PRIVATE ${CMAKE_BINARY_DIR}/include) +target_include_directories(json_benchmarks PRIVATE ${CMAKE_SOURCE_DIR}/../single_include ${CMAKE_BINARY_DIR}/include) diff --git a/benchmarks/thirdparty/benchmark/AUTHORS b/benchmarks/thirdparty/benchmark/AUTHORS deleted file mode 100755 index f8219036d2..0000000000 --- a/benchmarks/thirdparty/benchmark/AUTHORS +++ /dev/null @@ -1,46 +0,0 @@ -# This is the official list of benchmark authors for copyright purposes. -# This file is distinct from the CONTRIBUTORS files. -# See the latter for an explanation. -# -# Names should be added to this file as: -# Name or Organization -# The email address is not required for organizations. -# -# Please keep the list sorted. - -Albert Pretorius -Arne Beer -Carto -Christopher Seymour -David Coeurjolly -Deniz Evrenci -Dirac Research -Dominik Czarnota -Eric Fiselier -Eugene Zhuk -Evgeny Safronov -Felix Homann -Google Inc. -International Business Machines Corporation -Ismael Jimenez Martinez -Jern-Kuan Leong -JianXiong Zhou -Joao Paulo Magalhaes -Jussi Knuuttila -Kaito Udagawa -Kishan Kumar -Lei Xu -Matt Clarkson -Maxim Vafin -MongoDB Inc. -Nick Hutchinson -Oleksandr Sochka -Paul Redmond -Radoslav Yovchev -Roman Lebedev -Shuo Chen -Steinar H. Gunderson -Stripe, Inc. -Yixuan Qiu -Yusuke Suzuki -Zbigniew Skowron diff --git a/benchmarks/thirdparty/benchmark/BUILD.bazel b/benchmarks/thirdparty/benchmark/BUILD.bazel deleted file mode 100755 index 6ee69f2907..0000000000 --- a/benchmarks/thirdparty/benchmark/BUILD.bazel +++ /dev/null @@ -1,42 +0,0 @@ -licenses(["notice"]) - -config_setting( - name = "windows", - values = { - "cpu": "x64_windows", - }, - visibility = [":__subpackages__"], -) - -cc_library( - name = "benchmark", - srcs = glob( - [ - "src/*.cc", - "src/*.h", - ], - exclude = ["src/benchmark_main.cc"], - ), - hdrs = ["include/benchmark/benchmark.h"], - linkopts = select({ - ":windows": ["-DEFAULTLIB:shlwapi.lib"], - "//conditions:default": ["-pthread"], - }), - strip_include_prefix = "include", - visibility = ["//visibility:public"], -) - -cc_library( - name = "benchmark_main", - srcs = ["src/benchmark_main.cc"], - hdrs = ["include/benchmark/benchmark.h"], - strip_include_prefix = "include", - visibility = ["//visibility:public"], - deps = [":benchmark"], -) - -cc_library( - name = "benchmark_internal_headers", - hdrs = glob(["src/*.h"]), - visibility = ["//test:__pkg__"], -) diff --git a/benchmarks/thirdparty/benchmark/CMakeLists.txt b/benchmarks/thirdparty/benchmark/CMakeLists.txt deleted file mode 100755 index b1c1d3d5a9..0000000000 --- a/benchmarks/thirdparty/benchmark/CMakeLists.txt +++ /dev/null @@ -1,251 +0,0 @@ -cmake_minimum_required (VERSION 2.8.12) - -project (benchmark) - -foreach(p - CMP0054 # CMake 3.1 - CMP0056 # export EXE_LINKER_FLAGS to try_run - CMP0057 # Support no if() IN_LIST operator - ) - if(POLICY ${p}) - cmake_policy(SET ${p} NEW) - endif() -endforeach() - -option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON) -option(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON) -option(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF) -option(BENCHMARK_USE_LIBCXX "Build and test using libc++ as the standard library." OFF) -option(BENCHMARK_BUILD_32_BITS "Build a 32 bit version of the library." OFF) -option(BENCHMARK_ENABLE_INSTALL "Enable installation of benchmark. (Projects embedding benchmark may want to turn this OFF.)" ON) - -# Allow unmet dependencies to be met using CMake's ExternalProject mechanics, which -# may require downloading the source code. -option(BENCHMARK_DOWNLOAD_DEPENDENCIES "Allow the downloading and in-tree building of unmet dependencies" OFF) - -# This option can be used to disable building and running unit tests which depend on gtest -# in cases where it is not possible to build or find a valid version of gtest. -option(BENCHMARK_ENABLE_GTEST_TESTS "Enable building the unit tests which depend on gtest" ON) - -set(ENABLE_ASSEMBLY_TESTS_DEFAULT OFF) -function(should_enable_assembly_tests) - if(CMAKE_BUILD_TYPE) - string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER) - if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage") - # FIXME: The --coverage flag needs to be removed when building assembly - # tests for this to work. - return() - endif() - endif() - if (MSVC) - return() - elseif(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64") - return() - elseif(NOT CMAKE_SIZEOF_VOID_P EQUAL 8) - # FIXME: Make these work on 32 bit builds - return() - elseif(BENCHMARK_BUILD_32_BITS) - # FIXME: Make these work on 32 bit builds - return() - endif() - find_program(LLVM_FILECHECK_EXE FileCheck) - if (LLVM_FILECHECK_EXE) - set(LLVM_FILECHECK_EXE "${LLVM_FILECHECK_EXE}" CACHE PATH "llvm filecheck" FORCE) - message(STATUS "LLVM FileCheck Found: ${LLVM_FILECHECK_EXE}") - else() - message(STATUS "Failed to find LLVM FileCheck") - return() - endif() - set(ENABLE_ASSEMBLY_TESTS_DEFAULT ON PARENT_SCOPE) -endfunction() -should_enable_assembly_tests() - -# This option disables the building and running of the assembly verification tests -option(BENCHMARK_ENABLE_ASSEMBLY_TESTS "Enable building and running the assembly tests" - ${ENABLE_ASSEMBLY_TESTS_DEFAULT}) - -# Make sure we can import out CMake functions -list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules") -list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") - - -# Read the git tags to determine the project version -include(GetGitVersion) -get_git_version(GIT_VERSION) - -# Tell the user what versions we are using -string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" VERSION ${GIT_VERSION}) -message("-- Version: ${VERSION}") - -# The version of the libraries -set(GENERIC_LIB_VERSION ${VERSION}) -string(SUBSTRING ${VERSION} 0 1 GENERIC_LIB_SOVERSION) - -# Import our CMake modules -include(CheckCXXCompilerFlag) -include(AddCXXCompilerFlag) -include(CXXFeatureCheck) - -if (BENCHMARK_BUILD_32_BITS) - add_required_cxx_compiler_flag(-m32) -endif() - -if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") - # Turn compiler warnings up to 11 - string(REGEX REPLACE "[-/]W[1-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") - add_definitions(-D_CRT_SECURE_NO_WARNINGS) - - if (NOT BENCHMARK_ENABLE_EXCEPTIONS) - add_cxx_compiler_flag(-EHs-) - add_cxx_compiler_flag(-EHa-) - endif() - # Link time optimisation - if (BENCHMARK_ENABLE_LTO) - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL") - set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG") - set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG") - set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG") - - set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /GL") - string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO}") - set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") - string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}") - set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") - string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}") - set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") - - set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /GL") - set(CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL "${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL} /LTCG") - set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL} /LTCG") - set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} /LTCG") - endif() -else() - # Try and enable C++11. Don't use C++14 because it doesn't work in some - # configurations. - add_cxx_compiler_flag(-std=c++11) - if (NOT HAVE_CXX_FLAG_STD_CXX11) - add_cxx_compiler_flag(-std=c++0x) - endif() - - # Turn compiler warnings up to 11 - add_cxx_compiler_flag(-Wall) - - add_cxx_compiler_flag(-Wextra) - add_cxx_compiler_flag(-Wshadow) - add_cxx_compiler_flag(-Werror RELEASE) - add_cxx_compiler_flag(-Werror RELWITHDEBINFO) - add_cxx_compiler_flag(-Werror MINSIZEREL) - add_cxx_compiler_flag(-pedantic) - add_cxx_compiler_flag(-pedantic-errors) - add_cxx_compiler_flag(-Wshorten-64-to-32) - add_cxx_compiler_flag(-Wfloat-equal) - add_cxx_compiler_flag(-fstrict-aliasing) - if (NOT BENCHMARK_ENABLE_EXCEPTIONS) - add_cxx_compiler_flag(-fno-exceptions) - endif() - - if (HAVE_CXX_FLAG_FSTRICT_ALIASING) - if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel") #ICC17u2: Many false positives for Wstrict-aliasing - add_cxx_compiler_flag(-Wstrict-aliasing) - endif() - endif() - # ICC17u2: overloaded virtual function "benchmark::Fixture::SetUp" is only partially overridden - # (because of deprecated overload) - add_cxx_compiler_flag(-wd654) - add_cxx_compiler_flag(-Wthread-safety) - if (HAVE_CXX_FLAG_WTHREAD_SAFETY) - cxx_feature_check(THREAD_SAFETY_ATTRIBUTES) - endif() - - # On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a - # predefined macro, which turns on all of the wonderful libc extensions. - # However g++ doesn't do this in Cygwin so we have to define it ourselfs - # since we depend on GNU/POSIX/BSD extensions. - if (CYGWIN) - add_definitions(-D_GNU_SOURCE=1) - endif() - - # Link time optimisation - if (BENCHMARK_ENABLE_LTO) - add_cxx_compiler_flag(-flto) - if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") - find_program(GCC_AR gcc-ar) - if (GCC_AR) - set(CMAKE_AR ${GCC_AR}) - endif() - find_program(GCC_RANLIB gcc-ranlib) - if (GCC_RANLIB) - set(CMAKE_RANLIB ${GCC_RANLIB}) - endif() - elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") - include(llvm-toolchain) - endif() - endif() - - # Coverage build type - set(BENCHMARK_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}" - CACHE STRING "Flags used by the C++ compiler during coverage builds." - FORCE) - set(BENCHMARK_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG}" - CACHE STRING "Flags used for linking binaries during coverage builds." - FORCE) - set(BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" - CACHE STRING "Flags used by the shared libraries linker during coverage builds." - FORCE) - mark_as_advanced( - BENCHMARK_CXX_FLAGS_COVERAGE - BENCHMARK_EXE_LINKER_FLAGS_COVERAGE - BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE) - set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING - "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.") - add_cxx_compiler_flag(--coverage COVERAGE) -endif() - -if (BENCHMARK_USE_LIBCXX) - if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - add_cxx_compiler_flag(-stdlib=libc++) - elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR - "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") - add_cxx_compiler_flag(-nostdinc++) - message("libc++ header path must be manually specified using CMAKE_CXX_FLAGS") - # Adding -nodefaultlibs directly to CMAKE__LINKER_FLAGS will break - # configuration checks such as 'find_package(Threads)' - list(APPEND BENCHMARK_CXX_LINKER_FLAGS -nodefaultlibs) - # -lc++ cannot be added directly to CMAKE__LINKER_FLAGS because - # linker flags appear before all linker inputs and -lc++ must appear after. - list(APPEND BENCHMARK_CXX_LIBRARIES c++) - else() - message(FATAL_ERROR "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler") - endif() -endif(BENCHMARK_USE_LIBCXX) - -# C++ feature checks -# Determine the correct regular expression engine to use -cxx_feature_check(STD_REGEX) -cxx_feature_check(GNU_POSIX_REGEX) -cxx_feature_check(POSIX_REGEX) -if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX) - message(FATAL_ERROR "Failed to determine the source files for the regular expression backend") -endif() -if (NOT BENCHMARK_ENABLE_EXCEPTIONS AND HAVE_STD_REGEX - AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX) - message(WARNING "Using std::regex with exceptions disabled is not fully supported") -endif() -cxx_feature_check(STEADY_CLOCK) -# Ensure we have pthreads -find_package(Threads REQUIRED) - -# Set up directories -include_directories(${PROJECT_SOURCE_DIR}/include) - -# Build the targets -add_subdirectory(src) - -if (BENCHMARK_ENABLE_TESTING) - enable_testing() - if (BENCHMARK_ENABLE_GTEST_TESTS) - include(HandleGTest) - endif() - add_subdirectory(test) -endif() diff --git a/benchmarks/thirdparty/benchmark/CONTRIBUTING.md b/benchmarks/thirdparty/benchmark/CONTRIBUTING.md deleted file mode 100755 index 43de4c9d47..0000000000 --- a/benchmarks/thirdparty/benchmark/CONTRIBUTING.md +++ /dev/null @@ -1,58 +0,0 @@ -# How to contribute # - -We'd love to accept your patches and contributions to this project. There are -a just a few small guidelines you need to follow. - - -## Contributor License Agreement ## - -Contributions to any Google project must be accompanied by a Contributor -License Agreement. This is not a copyright **assignment**, it simply gives -Google permission to use and redistribute your contributions as part of the -project. - - * If you are an individual writing original source code and you're sure you - own the intellectual property, then you'll need to sign an [individual - CLA][]. - - * If you work for a company that wants to allow you to contribute your work, - then you'll need to sign a [corporate CLA][]. - -You generally only need to submit a CLA once, so if you've already submitted -one (even if it was for a different project), you probably don't need to do it -again. - -[individual CLA]: https://developers.google.com/open-source/cla/individual -[corporate CLA]: https://developers.google.com/open-source/cla/corporate - -Once your CLA is submitted (or if you already submitted one for -another Google project), make a commit adding yourself to the -[AUTHORS][] and [CONTRIBUTORS][] files. This commit can be part -of your first [pull request][]. - -[AUTHORS]: AUTHORS -[CONTRIBUTORS]: CONTRIBUTORS - - -## Submitting a patch ## - - 1. It's generally best to start by opening a new issue describing the bug or - feature you're intending to fix. Even if you think it's relatively minor, - it's helpful to know what people are working on. Mention in the initial - issue that you are planning to work on that bug or feature so that it can - be assigned to you. - - 1. Follow the normal process of [forking][] the project, and setup a new - branch to work in. It's important that each group of changes be done in - separate branches in order to ensure that a pull request only includes the - commits related to that bug or feature. - - 1. Do your best to have [well-formed commit messages][] for each change. - This provides consistency throughout the project, and ensures that commit - messages are able to be formatted properly by various git tools. - - 1. Finally, push the commits to your fork and submit a [pull request][]. - -[forking]: https://help.github.com/articles/fork-a-repo -[well-formed commit messages]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html -[pull request]: https://help.github.com/articles/creating-a-pull-request diff --git a/benchmarks/thirdparty/benchmark/CONTRIBUTORS b/benchmarks/thirdparty/benchmark/CONTRIBUTORS deleted file mode 100755 index 1cf04db17e..0000000000 --- a/benchmarks/thirdparty/benchmark/CONTRIBUTORS +++ /dev/null @@ -1,65 +0,0 @@ -# People who have agreed to one of the CLAs and can contribute patches. -# The AUTHORS file lists the copyright holders; this file -# lists people. For example, Google employees are listed here -# but not in AUTHORS, because Google holds the copyright. -# -# Names should be added to this file only after verifying that -# the individual or the individual's organization has agreed to -# the appropriate Contributor License Agreement, found here: -# -# https://developers.google.com/open-source/cla/individual -# https://developers.google.com/open-source/cla/corporate -# -# The agreement for individuals can be filled out on the web. -# -# When adding J Random Contributor's name to this file, -# either J's name or J's organization's name should be -# added to the AUTHORS file, depending on whether the -# individual or corporate CLA was used. -# -# Names should be added to this file as: -# Name -# -# Please keep the list sorted. - -Albert Pretorius -Arne Beer -Billy Robert O'Neal III -Chris Kennelly -Christopher Seymour -David Coeurjolly -Deniz Evrenci -Dominic Hamon -Dominik Czarnota -Eric Fiselier -Eugene Zhuk -Evgeny Safronov -Felix Homann -Ismael Jimenez Martinez -Jern-Kuan Leong -JianXiong Zhou -Joao Paulo Magalhaes -John Millikin -Jussi Knuuttila -Kai Wolf -Kishan Kumar -Kaito Udagawa -Lei Xu -Matt Clarkson -Maxim Vafin -Nick Hutchinson -Oleksandr Sochka -Pascal Leroy -Paul Redmond -Pierre Phaneuf -Radoslav Yovchev -Raul Marin -Ray Glover -Robert Guo -Roman Lebedev -Shuo Chen -Tobias Ulvgård -Tom Madams -Yixuan Qiu -Yusuke Suzuki -Zbigniew Skowron diff --git a/benchmarks/thirdparty/benchmark/LICENSE b/benchmarks/thirdparty/benchmark/LICENSE deleted file mode 100755 index d645695673..0000000000 --- a/benchmarks/thirdparty/benchmark/LICENSE +++ /dev/null @@ -1,202 +0,0 @@ - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/benchmarks/thirdparty/benchmark/README.md b/benchmarks/thirdparty/benchmark/README.md deleted file mode 100755 index 0341c31bd7..0000000000 --- a/benchmarks/thirdparty/benchmark/README.md +++ /dev/null @@ -1,950 +0,0 @@ -# benchmark -[![Build Status](https://travis-ci.org/google/benchmark.svg?branch=master)](https://travis-ci.org/google/benchmark) -[![Build status](https://ci.appveyor.com/api/projects/status/u0qsyp7t1tk7cpxs/branch/master?svg=true)](https://ci.appveyor.com/project/google/benchmark/branch/master) -[![Coverage Status](https://coveralls.io/repos/google/benchmark/badge.svg)](https://coveralls.io/r/google/benchmark) -[![slackin](https://slackin-iqtfqnpzxd.now.sh/badge.svg)](https://slackin-iqtfqnpzxd.now.sh/) - -A library to support the benchmarking of functions, similar to unit-tests. - -Discussion group: https://groups.google.com/d/forum/benchmark-discuss - -IRC channel: https://freenode.net #googlebenchmark - -[Known issues and common problems](#known-issues) - -[Additional Tooling Documentation](docs/tools.md) - -[Assembly Testing Documentation](docs/AssemblyTests.md) - - -## Building - -The basic steps for configuring and building the library look like this: - -```bash -$ git clone https://github.com/google/benchmark.git -# Benchmark requires Google Test as a dependency. Add the source tree as a subdirectory. -$ git clone https://github.com/google/googletest.git benchmark/googletest -$ mkdir build && cd build -$ cmake -G [options] ../benchmark -# Assuming a makefile generator was used -$ make -``` - -Note that Google Benchmark requires Google Test to build and run the tests. This -dependency can be provided two ways: - -* Checkout the Google Test sources into `benchmark/googletest` as above. -* Otherwise, if `-DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON` is specified during - configuration, the library will automatically download and build any required - dependencies. - -If you do not wish to build and run the tests, add `-DBENCHMARK_ENABLE_GTEST_TESTS=OFF` -to `CMAKE_ARGS`. - - -## Installation Guide - -For Ubuntu and Debian Based System - -First make sure you have git and cmake installed (If not please install it) - -``` -sudo apt-get install git -sudo apt-get install cmake -``` - -Now, let's clone the repository and build it - -``` -git clone https://github.com/google/benchmark.git -cd benchmark -git clone https://github.com/google/googletest.git -mkdir build -cd build -cmake .. -DCMAKE_BUILD_TYPE=RELEASE -make -``` - -We need to install the library globally now - -``` -sudo make install -``` - -Now you have google/benchmark installed in your machine -Note: Don't forget to link to pthread library while building - -## Stable and Experimental Library Versions - -The main branch contains the latest stable version of the benchmarking library; -the API of which can be considered largely stable, with source breaking changes -being made only upon the release of a new major version. - -Newer, experimental, features are implemented and tested on the -[`v2` branch](https://github.com/google/benchmark/tree/v2). Users who wish -to use, test, and provide feedback on the new features are encouraged to try -this branch. However, this branch provides no stability guarantees and reserves -the right to change and break the API at any time. - -##Prerequisite knowledge - -Before attempting to understand this framework one should ideally have some familiarity with the structure and format of the Google Test framework, upon which it is based. Documentation for Google Test, including a "Getting Started" (primer) guide, is available here: -https://github.com/google/googletest/blob/master/googletest/docs/Documentation.md - - -## Example usage -### Basic usage -Define a function that executes the code to be measured. - -```c++ -#include - -static void BM_StringCreation(benchmark::State& state) { - for (auto _ : state) - std::string empty_string; -} -// Register the function as a benchmark -BENCHMARK(BM_StringCreation); - -// Define another benchmark -static void BM_StringCopy(benchmark::State& state) { - std::string x = "hello"; - for (auto _ : state) - std::string copy(x); -} -BENCHMARK(BM_StringCopy); - -BENCHMARK_MAIN(); -``` - -Don't forget to inform your linker to add benchmark library e.g. through -`-lbenchmark` compilation flag. Alternatively, you may leave out the -`BENCHMARK_MAIN();` at the end of the source file and link against -`-lbenchmark_main` to get the same default behavior. - -The benchmark library will reporting the timing for the code within the `for(...)` loop. - -### Passing arguments -Sometimes a family of benchmarks can be implemented with just one routine that -takes an extra argument to specify which one of the family of benchmarks to -run. For example, the following code defines a family of benchmarks for -measuring the speed of `memcpy()` calls of different lengths: - -```c++ -static void BM_memcpy(benchmark::State& state) { - char* src = new char[state.range(0)]; - char* dst = new char[state.range(0)]; - memset(src, 'x', state.range(0)); - for (auto _ : state) - memcpy(dst, src, state.range(0)); - state.SetBytesProcessed(int64_t(state.iterations()) * - int64_t(state.range(0))); - delete[] src; - delete[] dst; -} -BENCHMARK(BM_memcpy)->Arg(8)->Arg(64)->Arg(512)->Arg(1<<10)->Arg(8<<10); -``` - -The preceding code is quite repetitive, and can be replaced with the following -short-hand. The following invocation will pick a few appropriate arguments in -the specified range and will generate a benchmark for each such argument. - -```c++ -BENCHMARK(BM_memcpy)->Range(8, 8<<10); -``` - -By default the arguments in the range are generated in multiples of eight and -the command above selects [ 8, 64, 512, 4k, 8k ]. In the following code the -range multiplier is changed to multiples of two. - -```c++ -BENCHMARK(BM_memcpy)->RangeMultiplier(2)->Range(8, 8<<10); -``` -Now arguments generated are [ 8, 16, 32, 64, 128, 256, 512, 1024, 2k, 4k, 8k ]. - -You might have a benchmark that depends on two or more inputs. For example, the -following code defines a family of benchmarks for measuring the speed of set -insertion. - -```c++ -static void BM_SetInsert(benchmark::State& state) { - std::set data; - for (auto _ : state) { - state.PauseTiming(); - data = ConstructRandomSet(state.range(0)); - state.ResumeTiming(); - for (int j = 0; j < state.range(1); ++j) - data.insert(RandomNumber()); - } -} -BENCHMARK(BM_SetInsert) - ->Args({1<<10, 128}) - ->Args({2<<10, 128}) - ->Args({4<<10, 128}) - ->Args({8<<10, 128}) - ->Args({1<<10, 512}) - ->Args({2<<10, 512}) - ->Args({4<<10, 512}) - ->Args({8<<10, 512}); -``` - -The preceding code is quite repetitive, and can be replaced with the following -short-hand. The following macro will pick a few appropriate arguments in the -product of the two specified ranges and will generate a benchmark for each such -pair. - -```c++ -BENCHMARK(BM_SetInsert)->Ranges({{1<<10, 8<<10}, {128, 512}}); -``` - -For more complex patterns of inputs, passing a custom function to `Apply` allows -programmatic specification of an arbitrary set of arguments on which to run the -benchmark. The following example enumerates a dense range on one parameter, -and a sparse range on the second. - -```c++ -static void CustomArguments(benchmark::internal::Benchmark* b) { - for (int i = 0; i <= 10; ++i) - for (int j = 32; j <= 1024*1024; j *= 8) - b->Args({i, j}); -} -BENCHMARK(BM_SetInsert)->Apply(CustomArguments); -``` - -### Calculate asymptotic complexity (Big O) -Asymptotic complexity might be calculated for a family of benchmarks. The -following code will calculate the coefficient for the high-order term in the -running time and the normalized root-mean square error of string comparison. - -```c++ -static void BM_StringCompare(benchmark::State& state) { - std::string s1(state.range(0), '-'); - std::string s2(state.range(0), '-'); - for (auto _ : state) { - benchmark::DoNotOptimize(s1.compare(s2)); - } - state.SetComplexityN(state.range(0)); -} -BENCHMARK(BM_StringCompare) - ->RangeMultiplier(2)->Range(1<<10, 1<<18)->Complexity(benchmark::oN); -``` - -As shown in the following invocation, asymptotic complexity might also be -calculated automatically. - -```c++ -BENCHMARK(BM_StringCompare) - ->RangeMultiplier(2)->Range(1<<10, 1<<18)->Complexity(); -``` - -The following code will specify asymptotic complexity with a lambda function, -that might be used to customize high-order term calculation. - -```c++ -BENCHMARK(BM_StringCompare)->RangeMultiplier(2) - ->Range(1<<10, 1<<18)->Complexity([](int n)->double{return n; }); -``` - -### Templated benchmarks -Templated benchmarks work the same way: This example produces and consumes -messages of size `sizeof(v)` `range_x` times. It also outputs throughput in the -absence of multiprogramming. - -```c++ -template int BM_Sequential(benchmark::State& state) { - Q q; - typename Q::value_type v; - for (auto _ : state) { - for (int i = state.range(0); i--; ) - q.push(v); - for (int e = state.range(0); e--; ) - q.Wait(&v); - } - // actually messages, not bytes: - state.SetBytesProcessed( - static_cast(state.iterations())*state.range(0)); -} -BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue)->Range(1<<0, 1<<10); -``` - -Three macros are provided for adding benchmark templates. - -```c++ -#ifdef BENCHMARK_HAS_CXX11 -#define BENCHMARK_TEMPLATE(func, ...) // Takes any number of parameters. -#else // C++ < C++11 -#define BENCHMARK_TEMPLATE(func, arg1) -#endif -#define BENCHMARK_TEMPLATE1(func, arg1) -#define BENCHMARK_TEMPLATE2(func, arg1, arg2) -``` - -### A Faster KeepRunning loop - -In C++11 mode, a ranged-based for loop should be used in preference to -the `KeepRunning` loop for running the benchmarks. For example: - -```c++ -static void BM_Fast(benchmark::State &state) { - for (auto _ : state) { - FastOperation(); - } -} -BENCHMARK(BM_Fast); -``` - -The reason the ranged-for loop is faster than using `KeepRunning`, is -because `KeepRunning` requires a memory load and store of the iteration count -ever iteration, whereas the ranged-for variant is able to keep the iteration count -in a register. - -For example, an empty inner loop of using the ranged-based for method looks like: - -```asm -# Loop Init - mov rbx, qword ptr [r14 + 104] - call benchmark::State::StartKeepRunning() - test rbx, rbx - je .LoopEnd -.LoopHeader: # =>This Inner Loop Header: Depth=1 - add rbx, -1 - jne .LoopHeader -.LoopEnd: -``` - -Compared to an empty `KeepRunning` loop, which looks like: - -```asm -.LoopHeader: # in Loop: Header=BB0_3 Depth=1 - cmp byte ptr [rbx], 1 - jne .LoopInit -.LoopBody: # =>This Inner Loop Header: Depth=1 - mov rax, qword ptr [rbx + 8] - lea rcx, [rax + 1] - mov qword ptr [rbx + 8], rcx - cmp rax, qword ptr [rbx + 104] - jb .LoopHeader - jmp .LoopEnd -.LoopInit: - mov rdi, rbx - call benchmark::State::StartKeepRunning() - jmp .LoopBody -.LoopEnd: -``` - -Unless C++03 compatibility is required, the ranged-for variant of writing -the benchmark loop should be preferred. - -## Passing arbitrary arguments to a benchmark -In C++11 it is possible to define a benchmark that takes an arbitrary number -of extra arguments. The `BENCHMARK_CAPTURE(func, test_case_name, ...args)` -macro creates a benchmark that invokes `func` with the `benchmark::State` as -the first argument followed by the specified `args...`. -The `test_case_name` is appended to the name of the benchmark and -should describe the values passed. - -```c++ -template -void BM_takes_args(benchmark::State& state, ExtraArgs&&... extra_args) { - [...] -} -// Registers a benchmark named "BM_takes_args/int_string_test" that passes -// the specified values to `extra_args`. -BENCHMARK_CAPTURE(BM_takes_args, int_string_test, 42, std::string("abc")); -``` -Note that elements of `...args` may refer to global variables. Users should -avoid modifying global state inside of a benchmark. - -## Using RegisterBenchmark(name, fn, args...) - -The `RegisterBenchmark(name, func, args...)` function provides an alternative -way to create and register benchmarks. -`RegisterBenchmark(name, func, args...)` creates, registers, and returns a -pointer to a new benchmark with the specified `name` that invokes -`func(st, args...)` where `st` is a `benchmark::State` object. - -Unlike the `BENCHMARK` registration macros, which can only be used at the global -scope, the `RegisterBenchmark` can be called anywhere. This allows for -benchmark tests to be registered programmatically. - -Additionally `RegisterBenchmark` allows any callable object to be registered -as a benchmark. Including capturing lambdas and function objects. - -For Example: -```c++ -auto BM_test = [](benchmark::State& st, auto Inputs) { /* ... */ }; - -int main(int argc, char** argv) { - for (auto& test_input : { /* ... */ }) - benchmark::RegisterBenchmark(test_input.name(), BM_test, test_input); - benchmark::Initialize(&argc, argv); - benchmark::RunSpecifiedBenchmarks(); -} -``` - -### Multithreaded benchmarks -In a multithreaded test (benchmark invoked by multiple threads simultaneously), -it is guaranteed that none of the threads will start until all have reached -the start of the benchmark loop, and all will have finished before any thread -exits the benchmark loop. (This behavior is also provided by the `KeepRunning()` -API) As such, any global setup or teardown can be wrapped in a check against the thread -index: - -```c++ -static void BM_MultiThreaded(benchmark::State& state) { - if (state.thread_index == 0) { - // Setup code here. - } - for (auto _ : state) { - // Run the test as normal. - } - if (state.thread_index == 0) { - // Teardown code here. - } -} -BENCHMARK(BM_MultiThreaded)->Threads(2); -``` - -If the benchmarked code itself uses threads and you want to compare it to -single-threaded code, you may want to use real-time ("wallclock") measurements -for latency comparisons: - -```c++ -BENCHMARK(BM_test)->Range(8, 8<<10)->UseRealTime(); -``` - -Without `UseRealTime`, CPU time is used by default. - - -## Manual timing -For benchmarking something for which neither CPU time nor real-time are -correct or accurate enough, completely manual timing is supported using -the `UseManualTime` function. - -When `UseManualTime` is used, the benchmarked code must call -`SetIterationTime` once per iteration of the benchmark loop to -report the manually measured time. - -An example use case for this is benchmarking GPU execution (e.g. OpenCL -or CUDA kernels, OpenGL or Vulkan or Direct3D draw calls), which cannot -be accurately measured using CPU time or real-time. Instead, they can be -measured accurately using a dedicated API, and these measurement results -can be reported back with `SetIterationTime`. - -```c++ -static void BM_ManualTiming(benchmark::State& state) { - int microseconds = state.range(0); - std::chrono::duration sleep_duration { - static_cast(microseconds) - }; - - for (auto _ : state) { - auto start = std::chrono::high_resolution_clock::now(); - // Simulate some useful workload with a sleep - std::this_thread::sleep_for(sleep_duration); - auto end = std::chrono::high_resolution_clock::now(); - - auto elapsed_seconds = - std::chrono::duration_cast>( - end - start); - - state.SetIterationTime(elapsed_seconds.count()); - } -} -BENCHMARK(BM_ManualTiming)->Range(1, 1<<17)->UseManualTime(); -``` - -### Preventing optimisation -To prevent a value or expression from being optimized away by the compiler -the `benchmark::DoNotOptimize(...)` and `benchmark::ClobberMemory()` -functions can be used. - -```c++ -static void BM_test(benchmark::State& state) { - for (auto _ : state) { - int x = 0; - for (int i=0; i < 64; ++i) { - benchmark::DoNotOptimize(x += i); - } - } -} -``` - -`DoNotOptimize()` forces the *result* of `` to be stored in either -memory or a register. For GNU based compilers it acts as read/write barrier -for global memory. More specifically it forces the compiler to flush pending -writes to memory and reload any other values as necessary. - -Note that `DoNotOptimize()` does not prevent optimizations on `` -in any way. `` may even be removed entirely when the result is already -known. For example: - -```c++ - /* Example 1: `` is removed entirely. */ - int foo(int x) { return x + 42; } - while (...) DoNotOptimize(foo(0)); // Optimized to DoNotOptimize(42); - - /* Example 2: Result of '' is only reused */ - int bar(int) __attribute__((const)); - while (...) DoNotOptimize(bar(0)); // Optimized to: - // int __result__ = bar(0); - // while (...) DoNotOptimize(__result__); -``` - -The second tool for preventing optimizations is `ClobberMemory()`. In essence -`ClobberMemory()` forces the compiler to perform all pending writes to global -memory. Memory managed by block scope objects must be "escaped" using -`DoNotOptimize(...)` before it can be clobbered. In the below example -`ClobberMemory()` prevents the call to `v.push_back(42)` from being optimized -away. - -```c++ -static void BM_vector_push_back(benchmark::State& state) { - for (auto _ : state) { - std::vector v; - v.reserve(1); - benchmark::DoNotOptimize(v.data()); // Allow v.data() to be clobbered. - v.push_back(42); - benchmark::ClobberMemory(); // Force 42 to be written to memory. - } -} -``` - -Note that `ClobberMemory()` is only available for GNU or MSVC based compilers. - -### Set time unit manually -If a benchmark runs a few milliseconds it may be hard to visually compare the -measured times, since the output data is given in nanoseconds per default. In -order to manually set the time unit, you can specify it manually: - -```c++ -BENCHMARK(BM_test)->Unit(benchmark::kMillisecond); -``` - -## Controlling number of iterations -In all cases, the number of iterations for which the benchmark is run is -governed by the amount of time the benchmark takes. Concretely, the number of -iterations is at least one, not more than 1e9, until CPU time is greater than -the minimum time, or the wallclock time is 5x minimum time. The minimum time is -set as a flag `--benchmark_min_time` or per benchmark by calling `MinTime` on -the registered benchmark object. - -## Reporting the mean, median and standard deviation by repeated benchmarks -By default each benchmark is run once and that single result is reported. -However benchmarks are often noisy and a single result may not be representative -of the overall behavior. For this reason it's possible to repeatedly rerun the -benchmark. - -The number of runs of each benchmark is specified globally by the -`--benchmark_repetitions` flag or on a per benchmark basis by calling -`Repetitions` on the registered benchmark object. When a benchmark is run more -than once the mean, median and standard deviation of the runs will be reported. - -Additionally the `--benchmark_report_aggregates_only={true|false}` flag or -`ReportAggregatesOnly(bool)` function can be used to change how repeated tests -are reported. By default the result of each repeated run is reported. When this -option is `true` only the mean, median and standard deviation of the runs is reported. -Calling `ReportAggregatesOnly(bool)` on a registered benchmark object overrides -the value of the flag for that benchmark. - -## User-defined statistics for repeated benchmarks -While having mean, median and standard deviation is nice, this may not be -enough for everyone. For example you may want to know what is the largest -observation, e.g. because you have some real-time constraints. This is easy. -The following code will specify a custom statistic to be calculated, defined -by a lambda function. - -```c++ -void BM_spin_empty(benchmark::State& state) { - for (auto _ : state) { - for (int x = 0; x < state.range(0); ++x) { - benchmark::DoNotOptimize(x); - } - } -} - -BENCHMARK(BM_spin_empty) - ->ComputeStatistics("max", [](const std::vector& v) -> double { - return *(std::max_element(std::begin(v), std::end(v))); - }) - ->Arg(512); -``` - -## Fixtures -Fixture tests are created by -first defining a type that derives from `::benchmark::Fixture` and then -creating/registering the tests using the following macros: - -* `BENCHMARK_F(ClassName, Method)` -* `BENCHMARK_DEFINE_F(ClassName, Method)` -* `BENCHMARK_REGISTER_F(ClassName, Method)` - -For Example: - -```c++ -class MyFixture : public benchmark::Fixture {}; - -BENCHMARK_F(MyFixture, FooTest)(benchmark::State& st) { - for (auto _ : st) { - ... - } -} - -BENCHMARK_DEFINE_F(MyFixture, BarTest)(benchmark::State& st) { - for (auto _ : st) { - ... - } -} -/* BarTest is NOT registered */ -BENCHMARK_REGISTER_F(MyFixture, BarTest)->Threads(2); -/* BarTest is now registered */ -``` - -### Templated fixtures -Also you can create templated fixture by using the following macros: - -* `BENCHMARK_TEMPLATE_F(ClassName, Method, ...)` -* `BENCHMARK_TEMPLATE_DEFINE_F(ClassName, Method, ...)` - -For example: -```c++ -template -class MyFixture : public benchmark::Fixture {}; - -BENCHMARK_TEMPLATE_F(MyFixture, IntTest, int)(benchmark::State& st) { - for (auto _ : st) { - ... - } -} - -BENCHMARK_TEMPLATE_DEFINE_F(MyFixture, DoubleTest, double)(benchmark::State& st) { - for (auto _ : st) { - ... - } -} - -BENCHMARK_REGISTER_F(MyFixture, DoubleTest)->Threads(2); -``` - -## User-defined counters - -You can add your own counters with user-defined names. The example below -will add columns "Foo", "Bar" and "Baz" in its output: - -```c++ -static void UserCountersExample1(benchmark::State& state) { - double numFoos = 0, numBars = 0, numBazs = 0; - for (auto _ : state) { - // ... count Foo,Bar,Baz events - } - state.counters["Foo"] = numFoos; - state.counters["Bar"] = numBars; - state.counters["Baz"] = numBazs; -} -``` - -The `state.counters` object is a `std::map` with `std::string` keys -and `Counter` values. The latter is a `double`-like class, via an implicit -conversion to `double&`. Thus you can use all of the standard arithmetic -assignment operators (`=,+=,-=,*=,/=`) to change the value of each counter. - -In multithreaded benchmarks, each counter is set on the calling thread only. -When the benchmark finishes, the counters from each thread will be summed; -the resulting sum is the value which will be shown for the benchmark. - -The `Counter` constructor accepts two parameters: the value as a `double` -and a bit flag which allows you to show counters as rates and/or as -per-thread averages: - -```c++ - // sets a simple counter - state.counters["Foo"] = numFoos; - - // Set the counter as a rate. It will be presented divided - // by the duration of the benchmark. - state.counters["FooRate"] = Counter(numFoos, benchmark::Counter::kIsRate); - - // Set the counter as a thread-average quantity. It will - // be presented divided by the number of threads. - state.counters["FooAvg"] = Counter(numFoos, benchmark::Counter::kAvgThreads); - - // There's also a combined flag: - state.counters["FooAvgRate"] = Counter(numFoos,benchmark::Counter::kAvgThreadsRate); -``` - -When you're compiling in C++11 mode or later you can use `insert()` with -`std::initializer_list`: - -```c++ - // With C++11, this can be done: - state.counters.insert({{"Foo", numFoos}, {"Bar", numBars}, {"Baz", numBazs}}); - // ... instead of: - state.counters["Foo"] = numFoos; - state.counters["Bar"] = numBars; - state.counters["Baz"] = numBazs; -``` - -### Counter reporting - -When using the console reporter, by default, user counters are are printed at -the end after the table, the same way as ``bytes_processed`` and -``items_processed``. This is best for cases in which there are few counters, -or where there are only a couple of lines per benchmark. Here's an example of -the default output: - -``` ------------------------------------------------------------------------------- -Benchmark Time CPU Iterations UserCounters... ------------------------------------------------------------------------------- -BM_UserCounter/threads:8 2248 ns 10277 ns 68808 Bar=16 Bat=40 Baz=24 Foo=8 -BM_UserCounter/threads:1 9797 ns 9788 ns 71523 Bar=2 Bat=5 Baz=3 Foo=1024m -BM_UserCounter/threads:2 4924 ns 9842 ns 71036 Bar=4 Bat=10 Baz=6 Foo=2 -BM_UserCounter/threads:4 2589 ns 10284 ns 68012 Bar=8 Bat=20 Baz=12 Foo=4 -BM_UserCounter/threads:8 2212 ns 10287 ns 68040 Bar=16 Bat=40 Baz=24 Foo=8 -BM_UserCounter/threads:16 1782 ns 10278 ns 68144 Bar=32 Bat=80 Baz=48 Foo=16 -BM_UserCounter/threads:32 1291 ns 10296 ns 68256 Bar=64 Bat=160 Baz=96 Foo=32 -BM_UserCounter/threads:4 2615 ns 10307 ns 68040 Bar=8 Bat=20 Baz=12 Foo=4 -BM_Factorial 26 ns 26 ns 26608979 40320 -BM_Factorial/real_time 26 ns 26 ns 26587936 40320 -BM_CalculatePiRange/1 16 ns 16 ns 45704255 0 -BM_CalculatePiRange/8 73 ns 73 ns 9520927 3.28374 -BM_CalculatePiRange/64 609 ns 609 ns 1140647 3.15746 -BM_CalculatePiRange/512 4900 ns 4901 ns 142696 3.14355 -``` - -If this doesn't suit you, you can print each counter as a table column by -passing the flag `--benchmark_counters_tabular=true` to the benchmark -application. This is best for cases in which there are a lot of counters, or -a lot of lines per individual benchmark. Note that this will trigger a -reprinting of the table header any time the counter set changes between -individual benchmarks. Here's an example of corresponding output when -`--benchmark_counters_tabular=true` is passed: - -``` ---------------------------------------------------------------------------------------- -Benchmark Time CPU Iterations Bar Bat Baz Foo ---------------------------------------------------------------------------------------- -BM_UserCounter/threads:8 2198 ns 9953 ns 70688 16 40 24 8 -BM_UserCounter/threads:1 9504 ns 9504 ns 73787 2 5 3 1 -BM_UserCounter/threads:2 4775 ns 9550 ns 72606 4 10 6 2 -BM_UserCounter/threads:4 2508 ns 9951 ns 70332 8 20 12 4 -BM_UserCounter/threads:8 2055 ns 9933 ns 70344 16 40 24 8 -BM_UserCounter/threads:16 1610 ns 9946 ns 70720 32 80 48 16 -BM_UserCounter/threads:32 1192 ns 9948 ns 70496 64 160 96 32 -BM_UserCounter/threads:4 2506 ns 9949 ns 70332 8 20 12 4 --------------------------------------------------------------- -Benchmark Time CPU Iterations --------------------------------------------------------------- -BM_Factorial 26 ns 26 ns 26392245 40320 -BM_Factorial/real_time 26 ns 26 ns 26494107 40320 -BM_CalculatePiRange/1 15 ns 15 ns 45571597 0 -BM_CalculatePiRange/8 74 ns 74 ns 9450212 3.28374 -BM_CalculatePiRange/64 595 ns 595 ns 1173901 3.15746 -BM_CalculatePiRange/512 4752 ns 4752 ns 147380 3.14355 -BM_CalculatePiRange/4k 37970 ns 37972 ns 18453 3.14184 -BM_CalculatePiRange/32k 303733 ns 303744 ns 2305 3.14162 -BM_CalculatePiRange/256k 2434095 ns 2434186 ns 288 3.1416 -BM_CalculatePiRange/1024k 9721140 ns 9721413 ns 71 3.14159 -BM_CalculatePi/threads:8 2255 ns 9943 ns 70936 -``` -Note above the additional header printed when the benchmark changes from -``BM_UserCounter`` to ``BM_Factorial``. This is because ``BM_Factorial`` does -not have the same counter set as ``BM_UserCounter``. - -## Exiting Benchmarks in Error - -When errors caused by external influences, such as file I/O and network -communication, occur within a benchmark the -`State::SkipWithError(const char* msg)` function can be used to skip that run -of benchmark and report the error. Note that only future iterations of the -`KeepRunning()` are skipped. For the ranged-for version of the benchmark loop -Users must explicitly exit the loop, otherwise all iterations will be performed. -Users may explicitly return to exit the benchmark immediately. - -The `SkipWithError(...)` function may be used at any point within the benchmark, -including before and after the benchmark loop. - -For example: - -```c++ -static void BM_test(benchmark::State& state) { - auto resource = GetResource(); - if (!resource.good()) { - state.SkipWithError("Resource is not good!"); - // KeepRunning() loop will not be entered. - } - for (state.KeepRunning()) { - auto data = resource.read_data(); - if (!resource.good()) { - state.SkipWithError("Failed to read data!"); - break; // Needed to skip the rest of the iteration. - } - do_stuff(data); - } -} - -static void BM_test_ranged_fo(benchmark::State & state) { - state.SkipWithError("test will not be entered"); - for (auto _ : state) { - state.SkipWithError("Failed!"); - break; // REQUIRED to prevent all further iterations. - } -} -``` - -## Running a subset of the benchmarks - -The `--benchmark_filter=` option can be used to only run the benchmarks -which match the specified ``. For example: - -```bash -$ ./run_benchmarks.x --benchmark_filter=BM_memcpy/32 -Run on (1 X 2300 MHz CPU ) -2016-06-25 19:34:24 -Benchmark Time CPU Iterations ----------------------------------------------------- -BM_memcpy/32 11 ns 11 ns 79545455 -BM_memcpy/32k 2181 ns 2185 ns 324074 -BM_memcpy/32 12 ns 12 ns 54687500 -BM_memcpy/32k 1834 ns 1837 ns 357143 -``` - - -## Output Formats -The library supports multiple output formats. Use the -`--benchmark_format=` flag to set the format type. `console` -is the default format. - -The Console format is intended to be a human readable format. By default -the format generates color output. Context is output on stderr and the -tabular data on stdout. Example tabular output looks like: -``` -Benchmark Time(ns) CPU(ns) Iterations ----------------------------------------------------------------------- -BM_SetInsert/1024/1 28928 29349 23853 133.097kB/s 33.2742k items/s -BM_SetInsert/1024/8 32065 32913 21375 949.487kB/s 237.372k items/s -BM_SetInsert/1024/10 33157 33648 21431 1.13369MB/s 290.225k items/s -``` - -The JSON format outputs human readable json split into two top level attributes. -The `context` attribute contains information about the run in general, including -information about the CPU and the date. -The `benchmarks` attribute contains a list of every benchmark run. Example json -output looks like: -```json -{ - "context": { - "date": "2015/03/17-18:40:25", - "num_cpus": 40, - "mhz_per_cpu": 2801, - "cpu_scaling_enabled": false, - "build_type": "debug" - }, - "benchmarks": [ - { - "name": "BM_SetInsert/1024/1", - "iterations": 94877, - "real_time": 29275, - "cpu_time": 29836, - "bytes_per_second": 134066, - "items_per_second": 33516 - }, - { - "name": "BM_SetInsert/1024/8", - "iterations": 21609, - "real_time": 32317, - "cpu_time": 32429, - "bytes_per_second": 986770, - "items_per_second": 246693 - }, - { - "name": "BM_SetInsert/1024/10", - "iterations": 21393, - "real_time": 32724, - "cpu_time": 33355, - "bytes_per_second": 1199226, - "items_per_second": 299807 - } - ] -} -``` - -The CSV format outputs comma-separated values. The `context` is output on stderr -and the CSV itself on stdout. Example CSV output looks like: -``` -name,iterations,real_time,cpu_time,bytes_per_second,items_per_second,label -"BM_SetInsert/1024/1",65465,17890.7,8407.45,475768,118942, -"BM_SetInsert/1024/8",116606,18810.1,9766.64,3.27646e+06,819115, -"BM_SetInsert/1024/10",106365,17238.4,8421.53,4.74973e+06,1.18743e+06, -``` - -## Output Files -The library supports writing the output of the benchmark to a file specified -by `--benchmark_out=`. The format of the output can be specified -using `--benchmark_out_format={json|console|csv}`. Specifying -`--benchmark_out` does not suppress the console output. - -## Debug vs Release -By default, benchmark builds as a debug library. You will see a warning in the output when this is the case. To build it as a release library instead, use: - -``` -cmake -DCMAKE_BUILD_TYPE=Release -``` - -To enable link-time optimisation, use - -``` -cmake -DCMAKE_BUILD_TYPE=Release -DBENCHMARK_ENABLE_LTO=true -``` - -If you are using gcc, you might need to set `GCC_AR` and `GCC_RANLIB` cmake cache variables, if autodetection fails. -If you are using clang, you may need to set `LLVMAR_EXECUTABLE`, `LLVMNM_EXECUTABLE` and `LLVMRANLIB_EXECUTABLE` cmake cache variables. - -## Linking against the library - -When the library is built using GCC it is necessary to link with `-pthread`, -due to how GCC implements `std::thread`. - -For GCC 4.x failing to link to pthreads will lead to runtime exceptions, not linker errors. -See [issue #67](https://github.com/google/benchmark/issues/67) for more details. - -## Compiler Support - -Google Benchmark uses C++11 when building the library. As such we require -a modern C++ toolchain, both compiler and standard library. - -The following minimum versions are strongly recommended build the library: - -* GCC 4.8 -* Clang 3.4 -* Visual Studio 2013 -* Intel 2015 Update 1 - -Anything older *may* work. - -Note: Using the library and its headers in C++03 is supported. C++11 is only -required to build the library. - -## Disable CPU frequency scaling -If you see this error: -``` -***WARNING*** CPU scaling is enabled, the benchmark real time measurements may be noisy and will incur extra overhead. -``` -you might want to disable the CPU frequency scaling while running the benchmark: -```bash -sudo cpupower frequency-set --governor performance -./mybench -sudo cpupower frequency-set --governor powersave -``` - -# Known Issues - -### Windows with CMake - -* Users must manually link `shlwapi.lib`. Failure to do so may result -in unresolved symbols. - -### Solaris - -* Users must explicitly link with kstat library (-lkstat compilation flag). diff --git a/benchmarks/thirdparty/benchmark/WORKSPACE b/benchmarks/thirdparty/benchmark/WORKSPACE deleted file mode 100755 index 54734f1ea5..0000000000 --- a/benchmarks/thirdparty/benchmark/WORKSPACE +++ /dev/null @@ -1,7 +0,0 @@ -workspace(name = "com_github_google_benchmark") - -http_archive( - name = "com_google_googletest", - urls = ["https://github.com/google/googletest/archive/3f0cf6b62ad1eb50d8736538363d3580dd640c3e.zip"], - strip_prefix = "googletest-3f0cf6b62ad1eb50d8736538363d3580dd640c3e", -) diff --git a/benchmarks/thirdparty/benchmark/appveyor.yml b/benchmarks/thirdparty/benchmark/appveyor.yml deleted file mode 100755 index e99c6e77f0..0000000000 --- a/benchmarks/thirdparty/benchmark/appveyor.yml +++ /dev/null @@ -1,56 +0,0 @@ -version: '{build}' - -image: Visual Studio 2017 - -configuration: - - Debug - - Release - -environment: - matrix: - - compiler: msvc-15-seh - generator: "Visual Studio 15 2017" - - - compiler: msvc-15-seh - generator: "Visual Studio 15 2017 Win64" - - - compiler: msvc-14-seh - generator: "Visual Studio 14 2015" - - - compiler: msvc-14-seh - generator: "Visual Studio 14 2015 Win64" - - - compiler: msvc-12-seh - generator: "Visual Studio 12 2013" - - - compiler: msvc-12-seh - generator: "Visual Studio 12 2013 Win64" - - - compiler: gcc-5.3.0-posix - generator: "MinGW Makefiles" - cxx_path: 'C:\mingw-w64\i686-5.3.0-posix-dwarf-rt_v4-rev0\mingw32\bin' - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 - -matrix: - fast_finish: true - -install: - # git bash conflicts with MinGW makefiles - - if "%generator%"=="MinGW Makefiles" (set "PATH=%PATH:C:\Program Files\Git\usr\bin;=%") - - if not "%cxx_path%"=="" (set "PATH=%PATH%;%cxx_path%") - -build_script: - - md _build -Force - - cd _build - - echo %configuration% - - cmake -G "%generator%" "-DCMAKE_BUILD_TYPE=%configuration%" -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON .. - - cmake --build . --config %configuration% - -test_script: - - ctest -c %configuration% --timeout 300 --output-on-failure - -artifacts: - - path: '_build/CMakeFiles/*.log' - name: logs - - path: '_build/Testing/**/*.xml' - name: test_results diff --git a/benchmarks/thirdparty/benchmark/cmake/AddCXXCompilerFlag.cmake b/benchmarks/thirdparty/benchmark/cmake/AddCXXCompilerFlag.cmake deleted file mode 100755 index d0d2099814..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/AddCXXCompilerFlag.cmake +++ /dev/null @@ -1,74 +0,0 @@ -# - Adds a compiler flag if it is supported by the compiler -# -# This function checks that the supplied compiler flag is supported and then -# adds it to the corresponding compiler flags -# -# add_cxx_compiler_flag( []) -# -# - Example -# -# include(AddCXXCompilerFlag) -# add_cxx_compiler_flag(-Wall) -# add_cxx_compiler_flag(-no-strict-aliasing RELEASE) -# Requires CMake 2.6+ - -if(__add_cxx_compiler_flag) - return() -endif() -set(__add_cxx_compiler_flag INCLUDED) - -include(CheckCXXCompilerFlag) - -function(mangle_compiler_flag FLAG OUTPUT) - string(TOUPPER "HAVE_CXX_FLAG_${FLAG}" SANITIZED_FLAG) - string(REPLACE "+" "X" SANITIZED_FLAG ${SANITIZED_FLAG}) - string(REGEX REPLACE "[^A-Za-z_0-9]" "_" SANITIZED_FLAG ${SANITIZED_FLAG}) - string(REGEX REPLACE "_+" "_" SANITIZED_FLAG ${SANITIZED_FLAG}) - set(${OUTPUT} "${SANITIZED_FLAG}" PARENT_SCOPE) -endfunction(mangle_compiler_flag) - -function(add_cxx_compiler_flag FLAG) - mangle_compiler_flag("${FLAG}" MANGLED_FLAG) - set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") - set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}") - check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG}) - set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}") - if(${MANGLED_FLAG}) - set(VARIANT ${ARGV1}) - if(ARGV1) - string(TOUPPER "_${VARIANT}" VARIANT) - endif() - set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${BENCHMARK_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE) - endif() -endfunction() - -function(add_required_cxx_compiler_flag FLAG) - mangle_compiler_flag("${FLAG}" MANGLED_FLAG) - set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") - set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}") - check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG}) - set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}") - if(${MANGLED_FLAG}) - set(VARIANT ${ARGV1}) - if(ARGV1) - string(TOUPPER "_${VARIANT}" VARIANT) - endif() - set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE) - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE) - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE) - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE) - set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}" PARENT_SCOPE) - else() - message(FATAL_ERROR "Required flag '${FLAG}' is not supported by the compiler") - endif() -endfunction() - -function(check_cxx_warning_flag FLAG) - mangle_compiler_flag("${FLAG}" MANGLED_FLAG) - set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") - # Add -Werror to ensure the compiler generates an error if the warning flag - # doesn't exist. - set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror ${FLAG}") - check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG}) - set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}") -endfunction() diff --git a/benchmarks/thirdparty/benchmark/cmake/CXXFeatureCheck.cmake b/benchmarks/thirdparty/benchmark/cmake/CXXFeatureCheck.cmake deleted file mode 100755 index c4c4d660f1..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/CXXFeatureCheck.cmake +++ /dev/null @@ -1,64 +0,0 @@ -# - Compile and run code to check for C++ features -# -# This functions compiles a source file under the `cmake` folder -# and adds the corresponding `HAVE_[FILENAME]` flag to the CMake -# environment -# -# cxx_feature_check( []) -# -# - Example -# -# include(CXXFeatureCheck) -# cxx_feature_check(STD_REGEX) -# Requires CMake 2.8.12+ - -if(__cxx_feature_check) - return() -endif() -set(__cxx_feature_check INCLUDED) - -function(cxx_feature_check FILE) - string(TOLOWER ${FILE} FILE) - string(TOUPPER ${FILE} VAR) - string(TOUPPER "HAVE_${VAR}" FEATURE) - if (DEFINED HAVE_${VAR}) - set(HAVE_${VAR} 1 PARENT_SCOPE) - add_definitions(-DHAVE_${VAR}) - return() - endif() - - if (NOT DEFINED COMPILE_${FEATURE}) - message("-- Performing Test ${FEATURE}") - if(CMAKE_CROSSCOMPILING) - try_compile(COMPILE_${FEATURE} - ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp - CMAKE_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS} - LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES}) - if(COMPILE_${FEATURE}) - message(WARNING - "If you see build failures due to cross compilation, try setting HAVE_${VAR} to 0") - set(RUN_${FEATURE} 0) - else() - set(RUN_${FEATURE} 1) - endif() - else() - message("-- Performing Test ${FEATURE}") - try_run(RUN_${FEATURE} COMPILE_${FEATURE} - ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp - CMAKE_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS} - LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES}) - endif() - endif() - - if(RUN_${FEATURE} EQUAL 0) - message("-- Performing Test ${FEATURE} -- success") - set(HAVE_${VAR} 1 PARENT_SCOPE) - add_definitions(-DHAVE_${VAR}) - else() - if(NOT COMPILE_${FEATURE}) - message("-- Performing Test ${FEATURE} -- failed to compile") - else() - message("-- Performing Test ${FEATURE} -- compiled but failed to run") - endif() - endif() -endfunction() diff --git a/benchmarks/thirdparty/benchmark/cmake/Config.cmake.in b/benchmarks/thirdparty/benchmark/cmake/Config.cmake.in deleted file mode 100755 index 6e9256eea8..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/Config.cmake.in +++ /dev/null @@ -1 +0,0 @@ -include("${CMAKE_CURRENT_LIST_DIR}/@targets_export_name@.cmake") diff --git a/benchmarks/thirdparty/benchmark/cmake/GetGitVersion.cmake b/benchmarks/thirdparty/benchmark/cmake/GetGitVersion.cmake deleted file mode 100755 index 88cebe3a1c..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/GetGitVersion.cmake +++ /dev/null @@ -1,54 +0,0 @@ -# - Returns a version string from Git tags -# -# This function inspects the annotated git tags for the project and returns a string -# into a CMake variable -# -# get_git_version() -# -# - Example -# -# include(GetGitVersion) -# get_git_version(GIT_VERSION) -# -# Requires CMake 2.8.11+ -find_package(Git) - -if(__get_git_version) - return() -endif() -set(__get_git_version INCLUDED) - -function(get_git_version var) - if(GIT_EXECUTABLE) - execute_process(COMMAND ${GIT_EXECUTABLE} describe --match "v[0-9]*.[0-9]*.[0-9]*" --abbrev=8 - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - RESULT_VARIABLE status - OUTPUT_VARIABLE GIT_VERSION - ERROR_QUIET) - if(${status}) - set(GIT_VERSION "v0.0.0") - else() - string(STRIP ${GIT_VERSION} GIT_VERSION) - string(REGEX REPLACE "-[0-9]+-g" "-" GIT_VERSION ${GIT_VERSION}) - endif() - - # Work out if the repository is dirty - execute_process(COMMAND ${GIT_EXECUTABLE} update-index -q --refresh - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - OUTPUT_QUIET - ERROR_QUIET) - execute_process(COMMAND ${GIT_EXECUTABLE} diff-index --name-only HEAD -- - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - OUTPUT_VARIABLE GIT_DIFF_INDEX - ERROR_QUIET) - string(COMPARE NOTEQUAL "${GIT_DIFF_INDEX}" "" GIT_DIRTY) - if (${GIT_DIRTY}) - set(GIT_VERSION "${GIT_VERSION}-dirty") - endif() - else() - set(GIT_VERSION "v0.0.0") - endif() - - message("-- git Version: ${GIT_VERSION}") - set(${var} ${GIT_VERSION} PARENT_SCOPE) -endfunction() diff --git a/benchmarks/thirdparty/benchmark/cmake/HandleGTest.cmake b/benchmarks/thirdparty/benchmark/cmake/HandleGTest.cmake deleted file mode 100755 index 7ce1a633d6..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/HandleGTest.cmake +++ /dev/null @@ -1,113 +0,0 @@ - -include(split_list) - -macro(build_external_gtest) - include(ExternalProject) - set(GTEST_FLAGS "") - if (BENCHMARK_USE_LIBCXX) - if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - list(APPEND GTEST_FLAGS -stdlib=libc++) - else() - message(WARNING "Unsupported compiler (${CMAKE_CXX_COMPILER}) when using libc++") - endif() - endif() - if (BENCHMARK_BUILD_32_BITS) - list(APPEND GTEST_FLAGS -m32) - endif() - if (NOT "${CMAKE_CXX_FLAGS}" STREQUAL "") - list(APPEND GTEST_FLAGS ${CMAKE_CXX_FLAGS}) - endif() - string(TOUPPER "${CMAKE_BUILD_TYPE}" GTEST_BUILD_TYPE) - if ("${GTEST_BUILD_TYPE}" STREQUAL "COVERAGE") - set(GTEST_BUILD_TYPE "DEBUG") - endif() - # FIXME: Since 10/Feb/2017 the googletest trunk has had a bug where - # -Werror=unused-function fires during the build on OS X. This is a temporary - # workaround to keep our travis bots from failing. It should be removed - # once gtest is fixed. - if (NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") - list(APPEND GTEST_FLAGS "-Wno-unused-function") - endif() - split_list(GTEST_FLAGS) - set(EXCLUDE_FROM_ALL_OPT "") - set(EXCLUDE_FROM_ALL_VALUE "") - if (${CMAKE_VERSION} VERSION_GREATER "3.0.99") - set(EXCLUDE_FROM_ALL_OPT "EXCLUDE_FROM_ALL") - set(EXCLUDE_FROM_ALL_VALUE "ON") - endif() - ExternalProject_Add(googletest - ${EXCLUDE_FROM_ALL_OPT} ${EXCLUDE_FROM_ALL_VALUE} - GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG master - PREFIX "${CMAKE_BINARY_DIR}/googletest" - INSTALL_DIR "${CMAKE_BINARY_DIR}/googletest" - CMAKE_CACHE_ARGS - -DCMAKE_BUILD_TYPE:STRING=${GTEST_BUILD_TYPE} - -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER} - -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER} - -DCMAKE_INSTALL_PREFIX:PATH= - -DCMAKE_INSTALL_LIBDIR:PATH=/lib - -DCMAKE_CXX_FLAGS:STRING=${GTEST_FLAGS} - -Dgtest_force_shared_crt:BOOL=ON - ) - - ExternalProject_Get_Property(googletest install_dir) - set(GTEST_INCLUDE_DIRS ${install_dir}/include) - file(MAKE_DIRECTORY ${GTEST_INCLUDE_DIRS}) - - set(LIB_SUFFIX "${CMAKE_STATIC_LIBRARY_SUFFIX}") - set(LIB_PREFIX "${CMAKE_STATIC_LIBRARY_PREFIX}") - if("${GTEST_BUILD_TYPE}" STREQUAL "DEBUG") - set(LIB_SUFFIX "d${CMAKE_STATIC_LIBRARY_SUFFIX}") - endif() - - # Use gmock_main instead of gtest_main because it initializes gtest as well. - # Note: The libraries are listed in reverse order of their dependancies. - foreach(LIB gtest gmock gmock_main) - add_library(${LIB} UNKNOWN IMPORTED) - set_target_properties(${LIB} PROPERTIES - IMPORTED_LOCATION ${install_dir}/lib/${LIB_PREFIX}${LIB}${LIB_SUFFIX} - INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIRS} - INTERFACE_LINK_LIBRARIES "${GTEST_BOTH_LIBRARIES}" - ) - add_dependencies(${LIB} googletest) - list(APPEND GTEST_BOTH_LIBRARIES ${LIB}) - endforeach() -endmacro(build_external_gtest) - -if (BENCHMARK_ENABLE_GTEST_TESTS) - if (IS_DIRECTORY ${CMAKE_SOURCE_DIR}/googletest) - set(GTEST_ROOT "${CMAKE_SOURCE_DIR}/googletest") - set(INSTALL_GTEST OFF CACHE INTERNAL "") - set(INSTALL_GMOCK OFF CACHE INTERNAL "") - add_subdirectory(${CMAKE_SOURCE_DIR}/googletest) - set(GTEST_BOTH_LIBRARIES gtest gmock gmock_main) - foreach(HEADER test mock) - # CMake 2.8 and older don't respect INTERFACE_INCLUDE_DIRECTORIES, so we - # have to add the paths ourselves. - set(HFILE g${HEADER}/g${HEADER}.h) - set(HPATH ${GTEST_ROOT}/google${HEADER}/include) - find_path(HEADER_PATH_${HEADER} ${HFILE} - NO_DEFAULT_PATHS - HINTS ${HPATH} - ) - if (NOT HEADER_PATH_${HEADER}) - message(FATAL_ERROR "Failed to find header ${HFILE} in ${HPATH}") - endif() - list(APPEND GTEST_INCLUDE_DIRS ${HEADER_PATH_${HEADER}}) - endforeach() - elseif(BENCHMARK_DOWNLOAD_DEPENDENCIES) - build_external_gtest() - else() - find_package(GTest REQUIRED) - find_path(GMOCK_INCLUDE_DIRS gmock/gmock.h - HINTS ${GTEST_INCLUDE_DIRS}) - if (NOT GMOCK_INCLUDE_DIRS) - message(FATAL_ERROR "Failed to find header gmock/gmock.h with hint ${GTEST_INCLUDE_DIRS}") - endif() - set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS}) - # FIXME: We don't currently require the gmock library to build the tests, - # and it's likely we won't find it, so we don't try. As long as we've - # found the gmock/gmock.h header and gtest_main that should be good enough. - endif() -endif() diff --git a/benchmarks/thirdparty/benchmark/cmake/Modules/FindLLVMAr.cmake b/benchmarks/thirdparty/benchmark/cmake/Modules/FindLLVMAr.cmake deleted file mode 100755 index 23469813cf..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/Modules/FindLLVMAr.cmake +++ /dev/null @@ -1,16 +0,0 @@ -include(FeatureSummary) - -find_program(LLVMAR_EXECUTABLE - NAMES llvm-ar - DOC "The llvm-ar executable" - ) - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(LLVMAr - DEFAULT_MSG - LLVMAR_EXECUTABLE) - -SET_PACKAGE_PROPERTIES(LLVMAr PROPERTIES - URL https://llvm.org/docs/CommandGuide/llvm-ar.html - DESCRIPTION "create, modify, and extract from archives" -) diff --git a/benchmarks/thirdparty/benchmark/cmake/Modules/FindLLVMNm.cmake b/benchmarks/thirdparty/benchmark/cmake/Modules/FindLLVMNm.cmake deleted file mode 100755 index e56430a04f..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/Modules/FindLLVMNm.cmake +++ /dev/null @@ -1,16 +0,0 @@ -include(FeatureSummary) - -find_program(LLVMNM_EXECUTABLE - NAMES llvm-nm - DOC "The llvm-nm executable" - ) - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(LLVMNm - DEFAULT_MSG - LLVMNM_EXECUTABLE) - -SET_PACKAGE_PROPERTIES(LLVMNm PROPERTIES - URL https://llvm.org/docs/CommandGuide/llvm-nm.html - DESCRIPTION "list LLVM bitcode and object file’s symbol table" -) diff --git a/benchmarks/thirdparty/benchmark/cmake/Modules/FindLLVMRanLib.cmake b/benchmarks/thirdparty/benchmark/cmake/Modules/FindLLVMRanLib.cmake deleted file mode 100755 index 7b53e1a790..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/Modules/FindLLVMRanLib.cmake +++ /dev/null @@ -1,15 +0,0 @@ -include(FeatureSummary) - -find_program(LLVMRANLIB_EXECUTABLE - NAMES llvm-ranlib - DOC "The llvm-ranlib executable" - ) - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(LLVMRanLib - DEFAULT_MSG - LLVMRANLIB_EXECUTABLE) - -SET_PACKAGE_PROPERTIES(LLVMRanLib PROPERTIES - DESCRIPTION "generate index for LLVM archive" -) diff --git a/benchmarks/thirdparty/benchmark/cmake/benchmark.pc.in b/benchmarks/thirdparty/benchmark/cmake/benchmark.pc.in deleted file mode 100755 index 1e84bff68d..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/benchmark.pc.in +++ /dev/null @@ -1,11 +0,0 @@ -prefix=@CMAKE_INSTALL_PREFIX@ -exec_prefix=${prefix} -libdir=${prefix}/lib -includedir=${prefix}/include - -Name: @PROJECT_NAME@ -Description: Google microbenchmark framework -Version: @VERSION@ - -Libs: -L${libdir} -lbenchmark -Cflags: -I${includedir} diff --git a/benchmarks/thirdparty/benchmark/cmake/gnu_posix_regex.cpp b/benchmarks/thirdparty/benchmark/cmake/gnu_posix_regex.cpp deleted file mode 100755 index b5b91cdab7..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/gnu_posix_regex.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include -int main() { - std::string str = "test0159"; - regex_t re; - int ec = regcomp(&re, "^[a-z]+[0-9]+$", REG_EXTENDED | REG_NOSUB); - if (ec != 0) { - return ec; - } - return regexec(&re, str.c_str(), 0, nullptr, 0) ? -1 : 0; -} - diff --git a/benchmarks/thirdparty/benchmark/cmake/llvm-toolchain.cmake b/benchmarks/thirdparty/benchmark/cmake/llvm-toolchain.cmake deleted file mode 100755 index fc119e52fd..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/llvm-toolchain.cmake +++ /dev/null @@ -1,8 +0,0 @@ -find_package(LLVMAr REQUIRED) -set(CMAKE_AR "${LLVMAR_EXECUTABLE}" CACHE FILEPATH "" FORCE) - -find_package(LLVMNm REQUIRED) -set(CMAKE_NM "${LLVMNM_EXECUTABLE}" CACHE FILEPATH "" FORCE) - -find_package(LLVMRanLib REQUIRED) -set(CMAKE_RANLIB "${LLVMRANLIB_EXECUTABLE}" CACHE FILEPATH "" FORCE) diff --git a/benchmarks/thirdparty/benchmark/cmake/posix_regex.cpp b/benchmarks/thirdparty/benchmark/cmake/posix_regex.cpp deleted file mode 100755 index 466dc62560..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/posix_regex.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -int main() { - std::string str = "test0159"; - regex_t re; - int ec = regcomp(&re, "^[a-z]+[0-9]+$", REG_EXTENDED | REG_NOSUB); - if (ec != 0) { - return ec; - } - int ret = regexec(&re, str.c_str(), 0, nullptr, 0) ? -1 : 0; - regfree(&re); - return ret; -} - diff --git a/benchmarks/thirdparty/benchmark/cmake/split_list.cmake b/benchmarks/thirdparty/benchmark/cmake/split_list.cmake deleted file mode 100755 index 67aed3fdc8..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/split_list.cmake +++ /dev/null @@ -1,3 +0,0 @@ -macro(split_list listname) - string(REPLACE ";" " " ${listname} "${${listname}}") -endmacro() diff --git a/benchmarks/thirdparty/benchmark/cmake/std_regex.cpp b/benchmarks/thirdparty/benchmark/cmake/std_regex.cpp deleted file mode 100755 index 696f2a26bc..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/std_regex.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include -int main() { - const std::string str = "test0159"; - std::regex re; - re = std::regex("^[a-z]+[0-9]+$", - std::regex_constants::extended | std::regex_constants::nosubs); - return std::regex_search(str, re) ? 0 : -1; -} - diff --git a/benchmarks/thirdparty/benchmark/cmake/steady_clock.cpp b/benchmarks/thirdparty/benchmark/cmake/steady_clock.cpp deleted file mode 100755 index 66d50d17e9..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/steady_clock.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include - -int main() { - typedef std::chrono::steady_clock Clock; - Clock::time_point tp = Clock::now(); - ((void)tp); -} diff --git a/benchmarks/thirdparty/benchmark/cmake/thread_safety_attributes.cpp b/benchmarks/thirdparty/benchmark/cmake/thread_safety_attributes.cpp deleted file mode 100755 index 46161babdb..0000000000 --- a/benchmarks/thirdparty/benchmark/cmake/thread_safety_attributes.cpp +++ /dev/null @@ -1,4 +0,0 @@ -#define HAVE_THREAD_SAFETY_ATTRIBUTES -#include "../src/mutex.h" - -int main() {} diff --git a/benchmarks/thirdparty/benchmark/docs/AssemblyTests.md b/benchmarks/thirdparty/benchmark/docs/AssemblyTests.md deleted file mode 100755 index 1fbdc269b5..0000000000 --- a/benchmarks/thirdparty/benchmark/docs/AssemblyTests.md +++ /dev/null @@ -1,147 +0,0 @@ -# Assembly Tests - -The Benchmark library provides a number of functions whose primary -purpose in to affect assembly generation, including `DoNotOptimize` -and `ClobberMemory`. In addition there are other functions, -such as `KeepRunning`, for which generating good assembly is paramount. - -For these functions it's important to have tests that verify the -correctness and quality of the implementation. This requires testing -the code generated by the compiler. - -This document describes how the Benchmark library tests compiler output, -as well as how to properly write new tests. - - -## Anatomy of a Test - -Writing a test has two steps: - -* Write the code you want to generate assembly for. -* Add `// CHECK` lines to match against the verified assembly. - -Example: -```c++ - -// CHECK-LABEL: test_add: -extern "C" int test_add() { - extern int ExternInt; - return ExternInt + 1; - - // CHECK: movl ExternInt(%rip), %eax - // CHECK: addl %eax - // CHECK: ret -} - -``` - -#### LLVM Filecheck - -[LLVM's Filecheck](https://llvm.org/docs/CommandGuide/FileCheck.html) -is used to test the generated assembly against the `// CHECK` lines -specified in the tests source file. Please see the documentation -linked above for information on how to write `CHECK` directives. - -#### Tips and Tricks: - -* Tests should match the minimal amount of output required to establish -correctness. `CHECK` directives don't have to match on the exact next line -after the previous match, so tests should omit checks for unimportant -bits of assembly. ([`CHECK-NEXT`](https://llvm.org/docs/CommandGuide/FileCheck.html#the-check-next-directive) -can be used to ensure a match occurs exactly after the previous match). - -* The tests are compiled with `-O3 -g0`. So we're only testing the -optimized output. - -* The assembly output is further cleaned up using `tools/strip_asm.py`. -This removes comments, assembler directives, and unused labels before -the test is run. - -* The generated and stripped assembly file for a test is output under -`/test/.s` - -* Filecheck supports using [`CHECK` prefixes](https://llvm.org/docs/CommandGuide/FileCheck.html#cmdoption-check-prefixes) -to specify lines that should only match in certain situations. -The Benchmark tests use `CHECK-CLANG` and `CHECK-GNU` for lines that -are only expected to match Clang or GCC's output respectively. Normal -`CHECK` lines match against all compilers. (Note: `CHECK-NOT` and -`CHECK-LABEL` are NOT prefixes. They are versions of non-prefixed -`CHECK` lines) - -* Use `extern "C"` to disable name mangling for specific functions. This -makes them easier to name in the `CHECK` lines. - - -## Problems Writing Portable Tests - -Writing tests which check the code generated by a compiler are -inherently non-portable. Different compilers and even different compiler -versions may generate entirely different code. The Benchmark tests -must tolerate this. - -LLVM Filecheck provides a number of mechanisms to help write -"more portable" tests; including [matching using regular expressions](https://llvm.org/docs/CommandGuide/FileCheck.html#filecheck-pattern-matching-syntax), -allowing the creation of [named variables](https://llvm.org/docs/CommandGuide/FileCheck.html#filecheck-variables) -for later matching, and [checking non-sequential matches](https://llvm.org/docs/CommandGuide/FileCheck.html#the-check-dag-directive). - -#### Capturing Variables - -For example, say GCC stores a variable in a register but Clang stores -it in memory. To write a test that tolerates both cases we "capture" -the destination of the store, and then use the captured expression -to write the remainder of the test. - -```c++ -// CHECK-LABEL: test_div_no_op_into_shr: -extern "C" void test_div_no_op_into_shr(int value) { - int divisor = 2; - benchmark::DoNotOptimize(divisor); // hide the value from the optimizer - return value / divisor; - - // CHECK: movl $2, [[DEST:.*]] - // CHECK: idivl [[DEST]] - // CHECK: ret -} -``` - -#### Using Regular Expressions to Match Differing Output - -Often tests require testing assembly lines which may subtly differ -between compilers or compiler versions. A common example of this -is matching stack frame addresses. In this case regular expressions -can be used to match the differing bits of output. For example: - -```c++ -int ExternInt; -struct Point { int x, y, z; }; - -// CHECK-LABEL: test_store_point: -extern "C" void test_store_point() { - Point p{ExternInt, ExternInt, ExternInt}; - benchmark::DoNotOptimize(p); - - // CHECK: movl ExternInt(%rip), %eax - // CHECK: movl %eax, -{{[0-9]+}}(%rsp) - // CHECK: movl %eax, -{{[0-9]+}}(%rsp) - // CHECK: movl %eax, -{{[0-9]+}}(%rsp) - // CHECK: ret -} -``` - -## Current Requirements and Limitations - -The tests require Filecheck to be installed along the `PATH` of the -build machine. Otherwise the tests will be disabled. - -Additionally, as mentioned in the previous section, codegen tests are -inherently non-portable. Currently the tests are limited to: - -* x86_64 targets. -* Compiled with GCC or Clang - -Further work could be done, at least on a limited basis, to extend the -tests to other architectures and compilers (using `CHECK` prefixes). - -Furthermore, the tests fail for builds which specify additional flags -that modify code generation, including `--coverage` or `-fsanitize=`. - diff --git a/benchmarks/thirdparty/benchmark/docs/tools.md b/benchmarks/thirdparty/benchmark/docs/tools.md deleted file mode 100755 index 70500bd322..0000000000 --- a/benchmarks/thirdparty/benchmark/docs/tools.md +++ /dev/null @@ -1,242 +0,0 @@ -# Benchmark Tools - -## compare_bench.py - -The `compare_bench.py` utility which can be used to compare the result of benchmarks. -The program is invoked like: - -``` bash -$ compare_bench.py [benchmark options]... -``` - -Where `` and `` either specify a benchmark executable file, or a JSON output file. The type of the input file is automatically detected. If a benchmark executable is specified then the benchmark is run to obtain the results. Otherwise the results are simply loaded from the output file. - -`[benchmark options]` will be passed to the benchmarks invocations. They can be anything that binary accepts, be it either normal `--benchmark_*` parameters, or some custom parameters your binary takes. - -The sample output using the JSON test files under `Inputs/` gives: - -``` bash -$ ./compare_bench.py ./gbench/Inputs/test1_run1.json ./gbench/Inputs/test1_run2.json -Comparing ./gbench/Inputs/test1_run1.json to ./gbench/Inputs/test1_run2.json -Benchmark Time CPU Time Old Time New CPU Old CPU New -------------------------------------------------------------------------------------------------------------- -BM_SameTimes +0.0000 +0.0000 10 10 10 10 -BM_2xFaster -0.5000 -0.5000 50 25 50 25 -BM_2xSlower +1.0000 +1.0000 50 100 50 100 -BM_1PercentFaster -0.0100 -0.0100 100 99 100 99 -BM_1PercentSlower +0.0100 +0.0100 100 101 100 101 -BM_10PercentFaster -0.1000 -0.1000 100 90 100 90 -BM_10PercentSlower +0.1000 +0.1000 100 110 100 110 -BM_100xSlower +99.0000 +99.0000 100 10000 100 10000 -BM_100xFaster -0.9900 -0.9900 10000 100 10000 100 -BM_10PercentCPUToTime +0.1000 -0.1000 100 110 100 90 -BM_ThirdFaster -0.3333 -0.3334 100 67 100 67 -BM_BadTimeUnit -0.9000 +0.2000 0 0 0 1 -``` - -As you can note, the values in `Time` and `CPU` columns are calculated as `(new - old) / |old|`. - -When a benchmark executable is run, the raw output from the benchmark is printed in real time to stdout. The sample output using `benchmark/basic_test` for both arguments looks like: - -``` -./compare_bench.py test/basic_test test/basic_test --benchmark_filter=BM_empty.* -RUNNING: test/basic_test --benchmark_filter=BM_empty.* --benchmark_out=/tmp/tmpN7LF3a -Run on (8 X 4000 MHz CPU s) -2017-11-07 23:28:36 ---------------------------------------------------------------------- -Benchmark Time CPU Iterations ---------------------------------------------------------------------- -BM_empty 4 ns 4 ns 170178757 -BM_empty/threads:8 1 ns 7 ns 103868920 -BM_empty_stop_start 0 ns 0 ns 1000000000 -BM_empty_stop_start/threads:8 0 ns 0 ns 1403031720 -RUNNING: /test/basic_test --benchmark_filter=BM_empty.* --benchmark_out=/tmp/tmplvrIp8 -Run on (8 X 4000 MHz CPU s) -2017-11-07 23:28:38 ---------------------------------------------------------------------- -Benchmark Time CPU Iterations ---------------------------------------------------------------------- -BM_empty 4 ns 4 ns 169534855 -BM_empty/threads:8 1 ns 7 ns 104188776 -BM_empty_stop_start 0 ns 0 ns 1000000000 -BM_empty_stop_start/threads:8 0 ns 0 ns 1404159424 -Comparing ../build/test/basic_test to ../build/test/basic_test -Benchmark Time CPU Time Old Time New CPU Old CPU New ---------------------------------------------------------------------------------------------------------------------- -BM_empty -0.0048 -0.0049 4 4 4 4 -BM_empty/threads:8 -0.0123 -0.0054 1 1 7 7 -BM_empty_stop_start -0.0000 -0.0000 0 0 0 0 -BM_empty_stop_start/threads:8 -0.0029 +0.0001 0 0 0 0 - -``` - -As you can note, the values in `Time` and `CPU` columns are calculated as `(new - old) / |old|`. -Obviously this example doesn't give any useful output, but it's intended to show the output format when 'compare_bench.py' needs to run benchmarks. - -## compare.py - -The `compare.py` can be used to compare the result of benchmarks. -There are three modes of operation: - -1. Just compare two benchmarks, what `compare_bench.py` did. -The program is invoked like: - -``` bash -$ compare.py benchmarks [benchmark options]... -``` -Where `` and `` either specify a benchmark executable file, or a JSON output file. The type of the input file is automatically detected. If a benchmark executable is specified then the benchmark is run to obtain the results. Otherwise the results are simply loaded from the output file. - -`[benchmark options]` will be passed to the benchmarks invocations. They can be anything that binary accepts, be it either normal `--benchmark_*` parameters, or some custom parameters your binary takes. - -Example output: -``` -$ ./compare.py benchmarks ./a.out ./a.out -RUNNING: ./a.out --benchmark_out=/tmp/tmprBT5nW -Run on (8 X 4000 MHz CPU s) -2017-11-07 21:16:44 ------------------------------------------------------- -Benchmark Time CPU Iterations ------------------------------------------------------- -BM_memcpy/8 36 ns 36 ns 19101577 211.669MB/s -BM_memcpy/64 76 ns 76 ns 9412571 800.199MB/s -BM_memcpy/512 84 ns 84 ns 8249070 5.64771GB/s -BM_memcpy/1024 116 ns 116 ns 6181763 8.19505GB/s -BM_memcpy/8192 643 ns 643 ns 1062855 11.8636GB/s -BM_copy/8 222 ns 222 ns 3137987 34.3772MB/s -BM_copy/64 1608 ns 1608 ns 432758 37.9501MB/s -BM_copy/512 12589 ns 12589 ns 54806 38.7867MB/s -BM_copy/1024 25169 ns 25169 ns 27713 38.8003MB/s -BM_copy/8192 201165 ns 201112 ns 3486 38.8466MB/s -RUNNING: ./a.out --benchmark_out=/tmp/tmpt1wwG_ -Run on (8 X 4000 MHz CPU s) -2017-11-07 21:16:53 ------------------------------------------------------- -Benchmark Time CPU Iterations ------------------------------------------------------- -BM_memcpy/8 36 ns 36 ns 19397903 211.255MB/s -BM_memcpy/64 73 ns 73 ns 9691174 839.635MB/s -BM_memcpy/512 85 ns 85 ns 8312329 5.60101GB/s -BM_memcpy/1024 118 ns 118 ns 6438774 8.11608GB/s -BM_memcpy/8192 656 ns 656 ns 1068644 11.6277GB/s -BM_copy/8 223 ns 223 ns 3146977 34.2338MB/s -BM_copy/64 1611 ns 1611 ns 435340 37.8751MB/s -BM_copy/512 12622 ns 12622 ns 54818 38.6844MB/s -BM_copy/1024 25257 ns 25239 ns 27779 38.6927MB/s -BM_copy/8192 205013 ns 205010 ns 3479 38.108MB/s -Comparing ./a.out to ./a.out -Benchmark Time CPU Time Old Time New CPU Old CPU New ------------------------------------------------------------------------------------------------------- -BM_memcpy/8 +0.0020 +0.0020 36 36 36 36 -BM_memcpy/64 -0.0468 -0.0470 76 73 76 73 -BM_memcpy/512 +0.0081 +0.0083 84 85 84 85 -BM_memcpy/1024 +0.0098 +0.0097 116 118 116 118 -BM_memcpy/8192 +0.0200 +0.0203 643 656 643 656 -BM_copy/8 +0.0046 +0.0042 222 223 222 223 -BM_copy/64 +0.0020 +0.0020 1608 1611 1608 1611 -BM_copy/512 +0.0027 +0.0026 12589 12622 12589 12622 -BM_copy/1024 +0.0035 +0.0028 25169 25257 25169 25239 -BM_copy/8192 +0.0191 +0.0194 201165 205013 201112 205010 -``` - -What it does is for the every benchmark from the first run it looks for the benchmark with exactly the same name in the second run, and then compares the results. If the names differ, the benchmark is omitted from the diff. -As you can note, the values in `Time` and `CPU` columns are calculated as `(new - old) / |old|`. - -2. Compare two different filters of one benchmark -The program is invoked like: - -``` bash -$ compare.py filters [benchmark options]... -``` -Where `` either specify a benchmark executable file, or a JSON output file. The type of the input file is automatically detected. If a benchmark executable is specified then the benchmark is run to obtain the results. Otherwise the results are simply loaded from the output file. - -Where `` and `` are the same regex filters that you would pass to the `[--benchmark_filter=]` parameter of the benchmark binary. - -`[benchmark options]` will be passed to the benchmarks invocations. They can be anything that binary accepts, be it either normal `--benchmark_*` parameters, or some custom parameters your binary takes. - -Example output: -``` -$ ./compare.py filters ./a.out BM_memcpy BM_copy -RUNNING: ./a.out --benchmark_filter=BM_memcpy --benchmark_out=/tmp/tmpBWKk0k -Run on (8 X 4000 MHz CPU s) -2017-11-07 21:37:28 ------------------------------------------------------- -Benchmark Time CPU Iterations ------------------------------------------------------- -BM_memcpy/8 36 ns 36 ns 17891491 211.215MB/s -BM_memcpy/64 74 ns 74 ns 9400999 825.646MB/s -BM_memcpy/512 87 ns 87 ns 8027453 5.46126GB/s -BM_memcpy/1024 111 ns 111 ns 6116853 8.5648GB/s -BM_memcpy/8192 657 ns 656 ns 1064679 11.6247GB/s -RUNNING: ./a.out --benchmark_filter=BM_copy --benchmark_out=/tmp/tmpAvWcOM -Run on (8 X 4000 MHz CPU s) -2017-11-07 21:37:33 ----------------------------------------------------- -Benchmark Time CPU Iterations ----------------------------------------------------- -BM_copy/8 227 ns 227 ns 3038700 33.6264MB/s -BM_copy/64 1640 ns 1640 ns 426893 37.2154MB/s -BM_copy/512 12804 ns 12801 ns 55417 38.1444MB/s -BM_copy/1024 25409 ns 25407 ns 27516 38.4365MB/s -BM_copy/8192 202986 ns 202990 ns 3454 38.4871MB/s -Comparing BM_memcpy to BM_copy (from ./a.out) -Benchmark Time CPU Time Old Time New CPU Old CPU New --------------------------------------------------------------------------------------------------------------------- -[BM_memcpy vs. BM_copy]/8 +5.2829 +5.2812 36 227 36 227 -[BM_memcpy vs. BM_copy]/64 +21.1719 +21.1856 74 1640 74 1640 -[BM_memcpy vs. BM_copy]/512 +145.6487 +145.6097 87 12804 87 12801 -[BM_memcpy vs. BM_copy]/1024 +227.1860 +227.1776 111 25409 111 25407 -[BM_memcpy vs. BM_copy]/8192 +308.1664 +308.2898 657 202986 656 202990 -``` - -As you can see, it applies filter to the benchmarks, both when running the benchmark, and before doing the diff. And to make the diff work, the matches are replaced with some common string. Thus, you can compare two different benchmark families within one benchmark binary. -As you can note, the values in `Time` and `CPU` columns are calculated as `(new - old) / |old|`. - -3. Compare filter one from benchmark one to filter two from benchmark two: -The program is invoked like: - -``` bash -$ compare.py filters [benchmark options]... -``` - -Where `` and `` either specify a benchmark executable file, or a JSON output file. The type of the input file is automatically detected. If a benchmark executable is specified then the benchmark is run to obtain the results. Otherwise the results are simply loaded from the output file. - -Where `` and `` are the same regex filters that you would pass to the `[--benchmark_filter=]` parameter of the benchmark binary. - -`[benchmark options]` will be passed to the benchmarks invocations. They can be anything that binary accepts, be it either normal `--benchmark_*` parameters, or some custom parameters your binary takes. - -Example output: -``` -$ ./compare.py benchmarksfiltered ./a.out BM_memcpy ./a.out BM_copy -RUNNING: ./a.out --benchmark_filter=BM_memcpy --benchmark_out=/tmp/tmp_FvbYg -Run on (8 X 4000 MHz CPU s) -2017-11-07 21:38:27 ------------------------------------------------------- -Benchmark Time CPU Iterations ------------------------------------------------------- -BM_memcpy/8 37 ns 37 ns 18953482 204.118MB/s -BM_memcpy/64 74 ns 74 ns 9206578 828.245MB/s -BM_memcpy/512 91 ns 91 ns 8086195 5.25476GB/s -BM_memcpy/1024 120 ns 120 ns 5804513 7.95662GB/s -BM_memcpy/8192 664 ns 664 ns 1028363 11.4948GB/s -RUNNING: ./a.out --benchmark_filter=BM_copy --benchmark_out=/tmp/tmpDfL5iE -Run on (8 X 4000 MHz CPU s) -2017-11-07 21:38:32 ----------------------------------------------------- -Benchmark Time CPU Iterations ----------------------------------------------------- -BM_copy/8 230 ns 230 ns 2985909 33.1161MB/s -BM_copy/64 1654 ns 1653 ns 419408 36.9137MB/s -BM_copy/512 13122 ns 13120 ns 53403 37.2156MB/s -BM_copy/1024 26679 ns 26666 ns 26575 36.6218MB/s -BM_copy/8192 215068 ns 215053 ns 3221 36.3283MB/s -Comparing BM_memcpy (from ./a.out) to BM_copy (from ./a.out) -Benchmark Time CPU Time Old Time New CPU Old CPU New --------------------------------------------------------------------------------------------------------------------- -[BM_memcpy vs. BM_copy]/8 +5.1649 +5.1637 37 230 37 230 -[BM_memcpy vs. BM_copy]/64 +21.4352 +21.4374 74 1654 74 1653 -[BM_memcpy vs. BM_copy]/512 +143.6022 +143.5865 91 13122 91 13120 -[BM_memcpy vs. BM_copy]/1024 +221.5903 +221.4790 120 26679 120 26666 -[BM_memcpy vs. BM_copy]/8192 +322.9059 +323.0096 664 215068 664 215053 -``` -This is a mix of the previous two modes, two (potentially different) benchmark binaries are run, and a different filter is applied to each one. -As you can note, the values in `Time` and `CPU` columns are calculated as `(new - old) / |old|`. diff --git a/benchmarks/thirdparty/benchmark/include/benchmark/benchmark.h b/benchmarks/thirdparty/benchmark/include/benchmark/benchmark.h deleted file mode 100755 index 23dd3d09b1..0000000000 --- a/benchmarks/thirdparty/benchmark/include/benchmark/benchmark.h +++ /dev/null @@ -1,1456 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Support for registering benchmarks for functions. - -/* Example usage: -// Define a function that executes the code to be measured a -// specified number of times: -static void BM_StringCreation(benchmark::State& state) { - for (auto _ : state) - std::string empty_string; -} - -// Register the function as a benchmark -BENCHMARK(BM_StringCreation); - -// Define another benchmark -static void BM_StringCopy(benchmark::State& state) { - std::string x = "hello"; - for (auto _ : state) - std::string copy(x); -} -BENCHMARK(BM_StringCopy); - -// Augment the main() program to invoke benchmarks if specified -// via the --benchmarks command line flag. E.g., -// my_unittest --benchmark_filter=all -// my_unittest --benchmark_filter=BM_StringCreation -// my_unittest --benchmark_filter=String -// my_unittest --benchmark_filter='Copy|Creation' -int main(int argc, char** argv) { - benchmark::Initialize(&argc, argv); - benchmark::RunSpecifiedBenchmarks(); - return 0; -} - -// Sometimes a family of microbenchmarks can be implemented with -// just one routine that takes an extra argument to specify which -// one of the family of benchmarks to run. For example, the following -// code defines a family of microbenchmarks for measuring the speed -// of memcpy() calls of different lengths: - -static void BM_memcpy(benchmark::State& state) { - char* src = new char[state.range(0)]; char* dst = new char[state.range(0)]; - memset(src, 'x', state.range(0)); - for (auto _ : state) - memcpy(dst, src, state.range(0)); - state.SetBytesProcessed(int64_t(state.iterations()) * - int64_t(state.range(0))); - delete[] src; delete[] dst; -} -BENCHMARK(BM_memcpy)->Arg(8)->Arg(64)->Arg(512)->Arg(1<<10)->Arg(8<<10); - -// The preceding code is quite repetitive, and can be replaced with the -// following short-hand. The following invocation will pick a few -// appropriate arguments in the specified range and will generate a -// microbenchmark for each such argument. -BENCHMARK(BM_memcpy)->Range(8, 8<<10); - -// You might have a microbenchmark that depends on two inputs. For -// example, the following code defines a family of microbenchmarks for -// measuring the speed of set insertion. -static void BM_SetInsert(benchmark::State& state) { - set data; - for (auto _ : state) { - state.PauseTiming(); - data = ConstructRandomSet(state.range(0)); - state.ResumeTiming(); - for (int j = 0; j < state.range(1); ++j) - data.insert(RandomNumber()); - } -} -BENCHMARK(BM_SetInsert) - ->Args({1<<10, 128}) - ->Args({2<<10, 128}) - ->Args({4<<10, 128}) - ->Args({8<<10, 128}) - ->Args({1<<10, 512}) - ->Args({2<<10, 512}) - ->Args({4<<10, 512}) - ->Args({8<<10, 512}); - -// The preceding code is quite repetitive, and can be replaced with -// the following short-hand. The following macro will pick a few -// appropriate arguments in the product of the two specified ranges -// and will generate a microbenchmark for each such pair. -BENCHMARK(BM_SetInsert)->Ranges({{1<<10, 8<<10}, {128, 512}}); - -// For more complex patterns of inputs, passing a custom function -// to Apply allows programmatic specification of an -// arbitrary set of arguments to run the microbenchmark on. -// The following example enumerates a dense range on -// one parameter, and a sparse range on the second. -static void CustomArguments(benchmark::internal::Benchmark* b) { - for (int i = 0; i <= 10; ++i) - for (int j = 32; j <= 1024*1024; j *= 8) - b->Args({i, j}); -} -BENCHMARK(BM_SetInsert)->Apply(CustomArguments); - -// Templated microbenchmarks work the same way: -// Produce then consume 'size' messages 'iters' times -// Measures throughput in the absence of multiprogramming. -template int BM_Sequential(benchmark::State& state) { - Q q; - typename Q::value_type v; - for (auto _ : state) { - for (int i = state.range(0); i--; ) - q.push(v); - for (int e = state.range(0); e--; ) - q.Wait(&v); - } - // actually messages, not bytes: - state.SetBytesProcessed( - static_cast(state.iterations())*state.range(0)); -} -BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue)->Range(1<<0, 1<<10); - -Use `Benchmark::MinTime(double t)` to set the minimum time used to run the -benchmark. This option overrides the `benchmark_min_time` flag. - -void BM_test(benchmark::State& state) { - ... body ... -} -BENCHMARK(BM_test)->MinTime(2.0); // Run for at least 2 seconds. - -In a multithreaded test, it is guaranteed that none of the threads will start -until all have reached the loop start, and all will have finished before any -thread exits the loop body. As such, any global setup or teardown you want to -do can be wrapped in a check against the thread index: - -static void BM_MultiThreaded(benchmark::State& state) { - if (state.thread_index == 0) { - // Setup code here. - } - for (auto _ : state) { - // Run the test as normal. - } - if (state.thread_index == 0) { - // Teardown code here. - } -} -BENCHMARK(BM_MultiThreaded)->Threads(4); - - -If a benchmark runs a few milliseconds it may be hard to visually compare the -measured times, since the output data is given in nanoseconds per default. In -order to manually set the time unit, you can specify it manually: - -BENCHMARK(BM_test)->Unit(benchmark::kMillisecond); -*/ - -#ifndef BENCHMARK_BENCHMARK_H_ -#define BENCHMARK_BENCHMARK_H_ - - -// The _MSVC_LANG check should detect Visual Studio 2015 Update 3 and newer. -#if __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L) -#define BENCHMARK_HAS_CXX11 -#endif - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#if defined(BENCHMARK_HAS_CXX11) -#include -#include -#include -#endif - -#if defined(_MSC_VER) -#include // for _ReadWriteBarrier -#endif - -#ifndef BENCHMARK_HAS_CXX11 -#define BENCHMARK_DISALLOW_COPY_AND_ASSIGN(TypeName) \ - TypeName(const TypeName&); \ - TypeName& operator=(const TypeName&) -#else -#define BENCHMARK_DISALLOW_COPY_AND_ASSIGN(TypeName) \ - TypeName(const TypeName&) = delete; \ - TypeName& operator=(const TypeName&) = delete -#endif - -#if defined(__GNUC__) -#define BENCHMARK_UNUSED __attribute__((unused)) -#define BENCHMARK_ALWAYS_INLINE __attribute__((always_inline)) -#define BENCHMARK_NOEXCEPT noexcept -#define BENCHMARK_NOEXCEPT_OP(x) noexcept(x) -#elif defined(_MSC_VER) && !defined(__clang__) -#define BENCHMARK_UNUSED -#define BENCHMARK_ALWAYS_INLINE __forceinline -#if _MSC_VER >= 1900 -#define BENCHMARK_NOEXCEPT noexcept -#define BENCHMARK_NOEXCEPT_OP(x) noexcept(x) -#else -#define BENCHMARK_NOEXCEPT -#define BENCHMARK_NOEXCEPT_OP(x) -#endif -#define __func__ __FUNCTION__ -#else -#define BENCHMARK_UNUSED -#define BENCHMARK_ALWAYS_INLINE -#define BENCHMARK_NOEXCEPT -#define BENCHMARK_NOEXCEPT_OP(x) -#endif - -#define BENCHMARK_INTERNAL_TOSTRING2(x) #x -#define BENCHMARK_INTERNAL_TOSTRING(x) BENCHMARK_INTERNAL_TOSTRING2(x) - -#if defined(__GNUC__) -#define BENCHMARK_BUILTIN_EXPECT(x, y) __builtin_expect(x, y) -#define BENCHMARK_DEPRECATED_MSG(msg) __attribute__((deprecated(msg))) -#else -#define BENCHMARK_BUILTIN_EXPECT(x, y) x -#define BENCHMARK_DEPRECATED_MSG(msg) -#define BENCHMARK_WARNING_MSG(msg) __pragma(message(__FILE__ "(" BENCHMARK_INTERNAL_TOSTRING(__LINE__) ") : warning note: " msg)) -#endif - -#if defined(__GNUC__) && !defined(__clang__) -#define BENCHMARK_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) -#endif - -namespace benchmark { -class BenchmarkReporter; - -void Initialize(int* argc, char** argv); - -// Report to stdout all arguments in 'argv' as unrecognized except the first. -// Returns true there is at least on unrecognized argument (i.e. 'argc' > 1). -bool ReportUnrecognizedArguments(int argc, char** argv); - -// Generate a list of benchmarks matching the specified --benchmark_filter flag -// and if --benchmark_list_tests is specified return after printing the name -// of each matching benchmark. Otherwise run each matching benchmark and -// report the results. -// -// The second and third overload use the specified 'console_reporter' and -// 'file_reporter' respectively. 'file_reporter' will write to the file -// specified -// by '--benchmark_output'. If '--benchmark_output' is not given the -// 'file_reporter' is ignored. -// -// RETURNS: The number of matching benchmarks. -size_t RunSpecifiedBenchmarks(); -size_t RunSpecifiedBenchmarks(BenchmarkReporter* console_reporter); -size_t RunSpecifiedBenchmarks(BenchmarkReporter* console_reporter, - BenchmarkReporter* file_reporter); - -// If this routine is called, peak memory allocation past this point in the -// benchmark is reported at the end of the benchmark report line. (It is -// computed by running the benchmark once with a single iteration and a memory -// tracer.) -// TODO(dominic) -// void MemoryUsage(); - -namespace internal { -class Benchmark; -class BenchmarkImp; -class BenchmarkFamilies; - -void UseCharPointer(char const volatile*); - -// Take ownership of the pointer and register the benchmark. Return the -// registered benchmark. -Benchmark* RegisterBenchmarkInternal(Benchmark*); - -// Ensure that the standard streams are properly initialized in every TU. -int InitializeStreams(); -BENCHMARK_UNUSED static int stream_init_anchor = InitializeStreams(); - -} // namespace internal - - -#if (!defined(__GNUC__) && !defined(__clang__)) || defined(__pnacl__) || \ - defined(__EMSCRIPTEN__) -# define BENCHMARK_HAS_NO_INLINE_ASSEMBLY -#endif - - -// The DoNotOptimize(...) function can be used to prevent a value or -// expression from being optimized away by the compiler. This function is -// intended to add little to no overhead. -// See: https://youtu.be/nXaxk27zwlk?t=2441 -#ifndef BENCHMARK_HAS_NO_INLINE_ASSEMBLY -template -inline BENCHMARK_ALWAYS_INLINE -void DoNotOptimize(Tp const& value) { - asm volatile("" : : "r,m"(value) : "memory"); -} - -template -inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp& value) { -#if defined(__clang__) - asm volatile("" : "+r,m"(value) : : "memory"); -#else - asm volatile("" : "+m,r"(value) : : "memory"); -#endif -} - -// Force the compiler to flush pending writes to global memory. Acts as an -// effective read/write barrier -inline BENCHMARK_ALWAYS_INLINE void ClobberMemory() { - asm volatile("" : : : "memory"); -} -#elif defined(_MSC_VER) -template -inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) { - internal::UseCharPointer(&reinterpret_cast(value)); - _ReadWriteBarrier(); -} - -inline BENCHMARK_ALWAYS_INLINE void ClobberMemory() { - _ReadWriteBarrier(); -} -#else -template -inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) { - internal::UseCharPointer(&reinterpret_cast(value)); -} -// FIXME Add ClobberMemory() for non-gnu and non-msvc compilers -#endif - - - -// This class is used for user-defined counters. -class Counter { -public: - - enum Flags { - kDefaults = 0, - // Mark the counter as a rate. It will be presented divided - // by the duration of the benchmark. - kIsRate = 1, - // Mark the counter as a thread-average quantity. It will be - // presented divided by the number of threads. - kAvgThreads = 2, - // Mark the counter as a thread-average rate. See above. - kAvgThreadsRate = kIsRate|kAvgThreads - }; - - double value; - Flags flags; - - BENCHMARK_ALWAYS_INLINE - Counter(double v = 0., Flags f = kDefaults) : value(v), flags(f) {} - - BENCHMARK_ALWAYS_INLINE operator double const& () const { return value; } - BENCHMARK_ALWAYS_INLINE operator double & () { return value; } - -}; - -// This is the container for the user-defined counters. -typedef std::map UserCounters; - - -// TimeUnit is passed to a benchmark in order to specify the order of magnitude -// for the measured time. -enum TimeUnit { kNanosecond, kMicrosecond, kMillisecond }; - -// BigO is passed to a benchmark in order to specify the asymptotic -// computational -// complexity for the benchmark. In case oAuto is selected, complexity will be -// calculated automatically to the best fit. -enum BigO { oNone, o1, oN, oNSquared, oNCubed, oLogN, oNLogN, oAuto, oLambda }; - -// BigOFunc is passed to a benchmark in order to specify the asymptotic -// computational complexity for the benchmark. -typedef double(BigOFunc)(int64_t); - -// StatisticsFunc is passed to a benchmark in order to compute some descriptive -// statistics over all the measurements of some type -typedef double(StatisticsFunc)(const std::vector&); - -struct Statistics { - std::string name_; - StatisticsFunc* compute_; - - Statistics(std::string name, StatisticsFunc* compute) - : name_(name), compute_(compute) {} -}; - -namespace internal { -class ThreadTimer; -class ThreadManager; - -enum ReportMode -#if defined(BENCHMARK_HAS_CXX11) - : unsigned -#else -#endif - { - RM_Unspecified, // The mode has not been manually specified - RM_Default, // The mode is user-specified as default. - RM_ReportAggregatesOnly -}; -} // namespace internal - -// State is passed to a running Benchmark and contains state for the -// benchmark to use. -class State { - public: - struct StateIterator; - friend struct StateIterator; - - // Returns iterators used to run each iteration of a benchmark using a - // C++11 ranged-based for loop. These functions should not be called directly. - // - // REQUIRES: The benchmark has not started running yet. Neither begin nor end - // have been called previously. - // - // NOTE: KeepRunning may not be used after calling either of these functions. - BENCHMARK_ALWAYS_INLINE StateIterator begin(); - BENCHMARK_ALWAYS_INLINE StateIterator end(); - - // Returns true if the benchmark should continue through another iteration. - // NOTE: A benchmark may not return from the test until KeepRunning() has - // returned false. - bool KeepRunning(); - - // Returns true iff the benchmark should run n more iterations. - // REQUIRES: 'n' > 0. - // NOTE: A benchmark must not return from the test until KeepRunningBatch() - // has returned false. - // NOTE: KeepRunningBatch() may overshoot by up to 'n' iterations. - // - // Intended usage: - // while (state.KeepRunningBatch(1000)) { - // // process 1000 elements - // } - bool KeepRunningBatch(size_t n); - - // REQUIRES: timer is running and 'SkipWithError(...)' has not been called - // by the current thread. - // Stop the benchmark timer. If not called, the timer will be - // automatically stopped after the last iteration of the benchmark loop. - // - // For threaded benchmarks the PauseTiming() function only pauses the timing - // for the current thread. - // - // NOTE: The "real time" measurement is per-thread. If different threads - // report different measurements the largest one is reported. - // - // NOTE: PauseTiming()/ResumeTiming() are relatively - // heavyweight, and so their use should generally be avoided - // within each benchmark iteration, if possible. - void PauseTiming(); - - // REQUIRES: timer is not running and 'SkipWithError(...)' has not been called - // by the current thread. - // Start the benchmark timer. The timer is NOT running on entrance to the - // benchmark function. It begins running after control flow enters the - // benchmark loop. - // - // NOTE: PauseTiming()/ResumeTiming() are relatively - // heavyweight, and so their use should generally be avoided - // within each benchmark iteration, if possible. - void ResumeTiming(); - - // REQUIRES: 'SkipWithError(...)' has not been called previously by the - // current thread. - // Report the benchmark as resulting in an error with the specified 'msg'. - // After this call the user may explicitly 'return' from the benchmark. - // - // If the ranged-for style of benchmark loop is used, the user must explicitly - // break from the loop, otherwise all future iterations will be run. - // If the 'KeepRunning()' loop is used the current thread will automatically - // exit the loop at the end of the current iteration. - // - // For threaded benchmarks only the current thread stops executing and future - // calls to `KeepRunning()` will block until all threads have completed - // the `KeepRunning()` loop. If multiple threads report an error only the - // first error message is used. - // - // NOTE: Calling 'SkipWithError(...)' does not cause the benchmark to exit - // the current scope immediately. If the function is called from within - // the 'KeepRunning()' loop the current iteration will finish. It is the users - // responsibility to exit the scope as needed. - void SkipWithError(const char* msg); - - // REQUIRES: called exactly once per iteration of the benchmarking loop. - // Set the manually measured time for this benchmark iteration, which - // is used instead of automatically measured time if UseManualTime() was - // specified. - // - // For threaded benchmarks the final value will be set to the largest - // reported values. - void SetIterationTime(double seconds); - - // Set the number of bytes processed by the current benchmark - // execution. This routine is typically called once at the end of a - // throughput oriented benchmark. If this routine is called with a - // value > 0, the report is printed in MB/sec instead of nanoseconds - // per iteration. - // - // REQUIRES: a benchmark has exited its benchmarking loop. - BENCHMARK_ALWAYS_INLINE - void SetBytesProcessed(int64_t bytes) { bytes_processed_ = bytes; } - - BENCHMARK_ALWAYS_INLINE - int64_t bytes_processed() const { return bytes_processed_; } - - // If this routine is called with complexity_n > 0 and complexity report is - // requested for the - // family benchmark, then current benchmark will be part of the computation - // and complexity_n will - // represent the length of N. - BENCHMARK_ALWAYS_INLINE - void SetComplexityN(int64_t complexity_n) { complexity_n_ = complexity_n; } - - BENCHMARK_ALWAYS_INLINE - int64_t complexity_length_n() { return complexity_n_; } - - // If this routine is called with items > 0, then an items/s - // label is printed on the benchmark report line for the currently - // executing benchmark. It is typically called at the end of a processing - // benchmark where a processing items/second output is desired. - // - // REQUIRES: a benchmark has exited its benchmarking loop. - BENCHMARK_ALWAYS_INLINE - void SetItemsProcessed(int64_t items) { items_processed_ = items; } - - BENCHMARK_ALWAYS_INLINE - int64_t items_processed() const { return items_processed_; } - - // If this routine is called, the specified label is printed at the - // end of the benchmark report line for the currently executing - // benchmark. Example: - // static void BM_Compress(benchmark::State& state) { - // ... - // double compress = input_size / output_size; - // state.SetLabel(StrFormat("compress:%.1f%%", 100.0*compression)); - // } - // Produces output that looks like: - // BM_Compress 50 50 14115038 compress:27.3% - // - // REQUIRES: a benchmark has exited its benchmarking loop. - void SetLabel(const char* label); - - void BENCHMARK_ALWAYS_INLINE SetLabel(const std::string& str) { - this->SetLabel(str.c_str()); - } - - // Range arguments for this run. CHECKs if the argument has been set. - BENCHMARK_ALWAYS_INLINE - int64_t range(std::size_t pos = 0) const { - assert(range_.size() > pos); - return range_[pos]; - } - - BENCHMARK_DEPRECATED_MSG("use 'range(0)' instead") - int64_t range_x() const { return range(0); } - - BENCHMARK_DEPRECATED_MSG("use 'range(1)' instead") - int64_t range_y() const { return range(1); } - - BENCHMARK_ALWAYS_INLINE - size_t iterations() const { - if (BENCHMARK_BUILTIN_EXPECT(!started_, false)) { - return 0; - } - return max_iterations - total_iterations_ + batch_leftover_; - } - -private: // items we expect on the first cache line (ie 64 bytes of the struct) - - // When total_iterations_ is 0, KeepRunning() and friends will return false. - // May be larger than max_iterations. - size_t total_iterations_; - - // When using KeepRunningBatch(), batch_leftover_ holds the number of - // iterations beyond max_iters that were run. Used to track - // completed_iterations_ accurately. - size_t batch_leftover_; - -public: - const size_t max_iterations; - -private: - bool started_; - bool finished_; - bool error_occurred_; - -private: // items we don't need on the first cache line - std::vector range_; - - int64_t bytes_processed_; - int64_t items_processed_; - - int64_t complexity_n_; - - public: - // Container for user-defined counters. - UserCounters counters; - // Index of the executing thread. Values from [0, threads). - const int thread_index; - // Number of threads concurrently executing the benchmark. - const int threads; - - - // TODO(EricWF) make me private - State(size_t max_iters, const std::vector& ranges, int thread_i, - int n_threads, internal::ThreadTimer* timer, - internal::ThreadManager* manager); - - private: - void StartKeepRunning(); - // Implementation of KeepRunning() and KeepRunningBatch(). - // is_batch must be true unless n is 1. - bool KeepRunningInternal(size_t n, bool is_batch); - void FinishKeepRunning(); - internal::ThreadTimer* timer_; - internal::ThreadManager* manager_; - BENCHMARK_DISALLOW_COPY_AND_ASSIGN(State); -}; - -inline BENCHMARK_ALWAYS_INLINE -bool State::KeepRunning() { - return KeepRunningInternal(1, /*is_batch=*/ false); -} - -inline BENCHMARK_ALWAYS_INLINE -bool State::KeepRunningBatch(size_t n) { - return KeepRunningInternal(n, /*is_batch=*/ true); -} - -inline BENCHMARK_ALWAYS_INLINE -bool State::KeepRunningInternal(size_t n, bool is_batch) { - // total_iterations_ is set to 0 by the constructor, and always set to a - // nonzero value by StartKepRunning(). - assert(n > 0); - // n must be 1 unless is_batch is true. - assert(is_batch || n == 1); - if (BENCHMARK_BUILTIN_EXPECT(total_iterations_ >= n, true)) { - total_iterations_ -= n; - return true; - } - if (!started_) { - StartKeepRunning(); - if (!error_occurred_ && total_iterations_ >= n) { - total_iterations_-= n; - return true; - } - } - // For non-batch runs, total_iterations_ must be 0 by now. - if (is_batch && total_iterations_ != 0) { - batch_leftover_ = n - total_iterations_; - total_iterations_ = 0; - return true; - } - FinishKeepRunning(); - return false; -} - -struct State::StateIterator { - struct BENCHMARK_UNUSED Value {}; - typedef std::forward_iterator_tag iterator_category; - typedef Value value_type; - typedef Value reference; - typedef Value pointer; - typedef std::ptrdiff_t difference_type; - - private: - friend class State; - BENCHMARK_ALWAYS_INLINE - StateIterator() : cached_(0), parent_() {} - - BENCHMARK_ALWAYS_INLINE - explicit StateIterator(State* st) - : cached_(st->error_occurred_ ? 0 : st->max_iterations), parent_(st) {} - - public: - BENCHMARK_ALWAYS_INLINE - Value operator*() const { return Value(); } - - BENCHMARK_ALWAYS_INLINE - StateIterator& operator++() { - assert(cached_ > 0); - --cached_; - return *this; - } - - BENCHMARK_ALWAYS_INLINE - bool operator!=(StateIterator const&) const { - if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) return true; - parent_->FinishKeepRunning(); - return false; - } - - private: - size_t cached_; - State* const parent_; -}; - -inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::begin() { - return StateIterator(this); -} -inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::end() { - StartKeepRunning(); - return StateIterator(); -} - -namespace internal { - -typedef void(Function)(State&); - -// ------------------------------------------------------ -// Benchmark registration object. The BENCHMARK() macro expands -// into an internal::Benchmark* object. Various methods can -// be called on this object to change the properties of the benchmark. -// Each method returns "this" so that multiple method calls can -// chained into one expression. -class Benchmark { - public: - virtual ~Benchmark(); - - // Note: the following methods all return "this" so that multiple - // method calls can be chained together in one expression. - - // Run this benchmark once with "x" as the extra argument passed - // to the function. - // REQUIRES: The function passed to the constructor must accept an arg1. - Benchmark* Arg(int64_t x); - - // Run this benchmark with the given time unit for the generated output report - Benchmark* Unit(TimeUnit unit); - - // Run this benchmark once for a number of values picked from the - // range [start..limit]. (start and limit are always picked.) - // REQUIRES: The function passed to the constructor must accept an arg1. - Benchmark* Range(int64_t start, int64_t limit); - - // Run this benchmark once for all values in the range [start..limit] with - // specific step - // REQUIRES: The function passed to the constructor must accept an arg1. - Benchmark* DenseRange(int64_t start, int64_t limit, int step = 1); - - // Run this benchmark once with "args" as the extra arguments passed - // to the function. - // REQUIRES: The function passed to the constructor must accept arg1, arg2 ... - Benchmark* Args(const std::vector& args); - - // Equivalent to Args({x, y}) - // NOTE: This is a legacy C++03 interface provided for compatibility only. - // New code should use 'Args'. - Benchmark* ArgPair(int64_t x, int64_t y) { - std::vector args; - args.push_back(x); - args.push_back(y); - return Args(args); - } - - // Run this benchmark once for a number of values picked from the - // ranges [start..limit]. (starts and limits are always picked.) - // REQUIRES: The function passed to the constructor must accept arg1, arg2 ... - Benchmark* Ranges(const std::vector >& ranges); - - // Equivalent to ArgNames({name}) - Benchmark* ArgName(const std::string& name); - - // Set the argument names to display in the benchmark name. If not called, - // only argument values will be shown. - Benchmark* ArgNames(const std::vector& names); - - // Equivalent to Ranges({{lo1, hi1}, {lo2, hi2}}). - // NOTE: This is a legacy C++03 interface provided for compatibility only. - // New code should use 'Ranges'. - Benchmark* RangePair(int64_t lo1, int64_t hi1, int64_t lo2, int64_t hi2) { - std::vector > ranges; - ranges.push_back(std::make_pair(lo1, hi1)); - ranges.push_back(std::make_pair(lo2, hi2)); - return Ranges(ranges); - } - - // Pass this benchmark object to *func, which can customize - // the benchmark by calling various methods like Arg, Args, - // Threads, etc. - Benchmark* Apply(void (*func)(Benchmark* benchmark)); - - // Set the range multiplier for non-dense range. If not called, the range - // multiplier kRangeMultiplier will be used. - Benchmark* RangeMultiplier(int multiplier); - - // Set the minimum amount of time to use when running this benchmark. This - // option overrides the `benchmark_min_time` flag. - // REQUIRES: `t > 0` and `Iterations` has not been called on this benchmark. - Benchmark* MinTime(double t); - - // Specify the amount of iterations that should be run by this benchmark. - // REQUIRES: 'n > 0' and `MinTime` has not been called on this benchmark. - // - // NOTE: This function should only be used when *exact* iteration control is - // needed and never to control or limit how long a benchmark runs, where - // `--benchmark_min_time=N` or `MinTime(...)` should be used instead. - Benchmark* Iterations(size_t n); - - // Specify the amount of times to repeat this benchmark. This option overrides - // the `benchmark_repetitions` flag. - // REQUIRES: `n > 0` - Benchmark* Repetitions(int n); - - // Specify if each repetition of the benchmark should be reported separately - // or if only the final statistics should be reported. If the benchmark - // is not repeated then the single result is always reported. - Benchmark* ReportAggregatesOnly(bool value = true); - - // If a particular benchmark is I/O bound, runs multiple threads internally or - // if for some reason CPU timings are not representative, call this method. If - // called, the elapsed time will be used to control how many iterations are - // run, and in the printing of items/second or MB/seconds values. If not - // called, the cpu time used by the benchmark will be used. - Benchmark* UseRealTime(); - - // If a benchmark must measure time manually (e.g. if GPU execution time is - // being - // measured), call this method. If called, each benchmark iteration should - // call - // SetIterationTime(seconds) to report the measured time, which will be used - // to control how many iterations are run, and in the printing of items/second - // or MB/second values. - Benchmark* UseManualTime(); - - // Set the asymptotic computational complexity for the benchmark. If called - // the asymptotic computational complexity will be shown on the output. - Benchmark* Complexity(BigO complexity = benchmark::oAuto); - - // Set the asymptotic computational complexity for the benchmark. If called - // the asymptotic computational complexity will be shown on the output. - Benchmark* Complexity(BigOFunc* complexity); - - // Add this statistics to be computed over all the values of benchmark run - Benchmark* ComputeStatistics(std::string name, StatisticsFunc* statistics); - - // Support for running multiple copies of the same benchmark concurrently - // in multiple threads. This may be useful when measuring the scaling - // of some piece of code. - - // Run one instance of this benchmark concurrently in t threads. - Benchmark* Threads(int t); - - // Pick a set of values T from [min_threads,max_threads]. - // min_threads and max_threads are always included in T. Run this - // benchmark once for each value in T. The benchmark run for a - // particular value t consists of t threads running the benchmark - // function concurrently. For example, consider: - // BENCHMARK(Foo)->ThreadRange(1,16); - // This will run the following benchmarks: - // Foo in 1 thread - // Foo in 2 threads - // Foo in 4 threads - // Foo in 8 threads - // Foo in 16 threads - Benchmark* ThreadRange(int min_threads, int max_threads); - - // For each value n in the range, run this benchmark once using n threads. - // min_threads and max_threads are always included in the range. - // stride specifies the increment. E.g. DenseThreadRange(1, 8, 3) starts - // a benchmark with 1, 4, 7 and 8 threads. - Benchmark* DenseThreadRange(int min_threads, int max_threads, int stride = 1); - - // Equivalent to ThreadRange(NumCPUs(), NumCPUs()) - Benchmark* ThreadPerCpu(); - - virtual void Run(State& state) = 0; - - // Used inside the benchmark implementation - struct Instance; - - protected: - explicit Benchmark(const char* name); - Benchmark(Benchmark const&); - void SetName(const char* name); - - int ArgsCnt() const; - - private: - friend class BenchmarkFamilies; - - std::string name_; - ReportMode report_mode_; - std::vector arg_names_; // Args for all benchmark runs - std::vector > args_; // Args for all benchmark runs - TimeUnit time_unit_; - int range_multiplier_; - double min_time_; - size_t iterations_; - int repetitions_; - bool use_real_time_; - bool use_manual_time_; - BigO complexity_; - BigOFunc* complexity_lambda_; - std::vector statistics_; - std::vector thread_counts_; - - Benchmark& operator=(Benchmark const&); -}; - -} // namespace internal - -// Create and register a benchmark with the specified 'name' that invokes -// the specified functor 'fn'. -// -// RETURNS: A pointer to the registered benchmark. -internal::Benchmark* RegisterBenchmark(const char* name, - internal::Function* fn); - -#if defined(BENCHMARK_HAS_CXX11) -template -internal::Benchmark* RegisterBenchmark(const char* name, Lambda&& fn); -#endif - -// Remove all registered benchmarks. All pointers to previously registered -// benchmarks are invalidated. -void ClearRegisteredBenchmarks(); - -namespace internal { -// The class used to hold all Benchmarks created from static function. -// (ie those created using the BENCHMARK(...) macros. -class FunctionBenchmark : public Benchmark { - public: - FunctionBenchmark(const char* name, Function* func) - : Benchmark(name), func_(func) {} - - virtual void Run(State& st); - - private: - Function* func_; -}; - -#ifdef BENCHMARK_HAS_CXX11 -template -class LambdaBenchmark : public Benchmark { - public: - virtual void Run(State& st) { lambda_(st); } - - private: - template - LambdaBenchmark(const char* name, OLambda&& lam) - : Benchmark(name), lambda_(std::forward(lam)) {} - - LambdaBenchmark(LambdaBenchmark const&) = delete; - - private: - template - friend Benchmark* ::benchmark::RegisterBenchmark(const char*, Lam&&); - - Lambda lambda_; -}; -#endif - -} // namespace internal - -inline internal::Benchmark* RegisterBenchmark(const char* name, - internal::Function* fn) { - return internal::RegisterBenchmarkInternal( - ::new internal::FunctionBenchmark(name, fn)); -} - -#ifdef BENCHMARK_HAS_CXX11 -template -internal::Benchmark* RegisterBenchmark(const char* name, Lambda&& fn) { - using BenchType = - internal::LambdaBenchmark::type>; - return internal::RegisterBenchmarkInternal( - ::new BenchType(name, std::forward(fn))); -} -#endif - -#if defined(BENCHMARK_HAS_CXX11) && \ - (!defined(BENCHMARK_GCC_VERSION) || BENCHMARK_GCC_VERSION >= 409) -template -internal::Benchmark* RegisterBenchmark(const char* name, Lambda&& fn, - Args&&... args) { - return benchmark::RegisterBenchmark( - name, [=](benchmark::State& st) { fn(st, args...); }); -} -#else -#define BENCHMARK_HAS_NO_VARIADIC_REGISTER_BENCHMARK -#endif - -// The base class for all fixture tests. -class Fixture : public internal::Benchmark { - public: - Fixture() : internal::Benchmark("") {} - - virtual void Run(State& st) { - this->SetUp(st); - this->BenchmarkCase(st); - this->TearDown(st); - } - - // These will be deprecated ... - virtual void SetUp(const State&) {} - virtual void TearDown(const State&) {} - // ... In favor of these. - virtual void SetUp(State& st) { SetUp(const_cast(st)); } - virtual void TearDown(State& st) { TearDown(const_cast(st)); } - - protected: - virtual void BenchmarkCase(State&) = 0; -}; - -} // namespace benchmark - -// ------------------------------------------------------ -// Macro to register benchmarks - -// Check that __COUNTER__ is defined and that __COUNTER__ increases by 1 -// every time it is expanded. X + 1 == X + 0 is used in case X is defined to be -// empty. If X is empty the expression becomes (+1 == +0). -#if defined(__COUNTER__) && (__COUNTER__ + 1 == __COUNTER__ + 0) -#define BENCHMARK_PRIVATE_UNIQUE_ID __COUNTER__ -#else -#define BENCHMARK_PRIVATE_UNIQUE_ID __LINE__ -#endif - -// Helpers for generating unique variable names -#define BENCHMARK_PRIVATE_NAME(n) \ - BENCHMARK_PRIVATE_CONCAT(_benchmark_, BENCHMARK_PRIVATE_UNIQUE_ID, n) -#define BENCHMARK_PRIVATE_CONCAT(a, b, c) BENCHMARK_PRIVATE_CONCAT2(a, b, c) -#define BENCHMARK_PRIVATE_CONCAT2(a, b, c) a##b##c - -#define BENCHMARK_PRIVATE_DECLARE(n) \ - static ::benchmark::internal::Benchmark* BENCHMARK_PRIVATE_NAME(n) \ - BENCHMARK_UNUSED - -#define BENCHMARK(n) \ - BENCHMARK_PRIVATE_DECLARE(n) = \ - (::benchmark::internal::RegisterBenchmarkInternal( \ - new ::benchmark::internal::FunctionBenchmark(#n, n))) - -// Old-style macros -#define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a)) -#define BENCHMARK_WITH_ARG2(n, a1, a2) BENCHMARK(n)->Args({(a1), (a2)}) -#define BENCHMARK_WITH_UNIT(n, t) BENCHMARK(n)->Unit((t)) -#define BENCHMARK_RANGE(n, lo, hi) BENCHMARK(n)->Range((lo), (hi)) -#define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \ - BENCHMARK(n)->RangePair({{(l1), (h1)}, {(l2), (h2)}}) - -#ifdef BENCHMARK_HAS_CXX11 - -// Register a benchmark which invokes the function specified by `func` -// with the additional arguments specified by `...`. -// -// For example: -// -// template ` -// void BM_takes_args(benchmark::State& state, ExtraArgs&&... extra_args) { -// [...] -//} -// /* Registers a benchmark named "BM_takes_args/int_string_test` */ -// BENCHMARK_CAPTURE(BM_takes_args, int_string_test, 42, std::string("abc")); -#define BENCHMARK_CAPTURE(func, test_case_name, ...) \ - BENCHMARK_PRIVATE_DECLARE(func) = \ - (::benchmark::internal::RegisterBenchmarkInternal( \ - new ::benchmark::internal::FunctionBenchmark( \ - #func "/" #test_case_name, \ - [](::benchmark::State& st) { func(st, __VA_ARGS__); }))) - -#endif // BENCHMARK_HAS_CXX11 - -// This will register a benchmark for a templatized function. For example: -// -// template -// void BM_Foo(int iters); -// -// BENCHMARK_TEMPLATE(BM_Foo, 1); -// -// will register BM_Foo<1> as a benchmark. -#define BENCHMARK_TEMPLATE1(n, a) \ - BENCHMARK_PRIVATE_DECLARE(n) = \ - (::benchmark::internal::RegisterBenchmarkInternal( \ - new ::benchmark::internal::FunctionBenchmark(#n "<" #a ">", n))) - -#define BENCHMARK_TEMPLATE2(n, a, b) \ - BENCHMARK_PRIVATE_DECLARE(n) = \ - (::benchmark::internal::RegisterBenchmarkInternal( \ - new ::benchmark::internal::FunctionBenchmark(#n "<" #a "," #b ">", \ - n))) - -#ifdef BENCHMARK_HAS_CXX11 -#define BENCHMARK_TEMPLATE(n, ...) \ - BENCHMARK_PRIVATE_DECLARE(n) = \ - (::benchmark::internal::RegisterBenchmarkInternal( \ - new ::benchmark::internal::FunctionBenchmark( \ - #n "<" #__VA_ARGS__ ">", n<__VA_ARGS__>))) -#else -#define BENCHMARK_TEMPLATE(n, a) BENCHMARK_TEMPLATE1(n, a) -#endif - -#define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \ - class BaseClass##_##Method##_Benchmark : public BaseClass { \ - public: \ - BaseClass##_##Method##_Benchmark() : BaseClass() { \ - this->SetName(#BaseClass "/" #Method); \ - } \ - \ - protected: \ - virtual void BenchmarkCase(::benchmark::State&); \ - }; - -#define BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \ - class BaseClass##_##Method##_Benchmark : public BaseClass { \ - public: \ - BaseClass##_##Method##_Benchmark() : BaseClass() { \ - this->SetName(#BaseClass"<" #a ">/" #Method); \ - } \ - \ - protected: \ - virtual void BenchmarkCase(::benchmark::State&); \ - }; - -#define BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \ - class BaseClass##_##Method##_Benchmark : public BaseClass { \ - public: \ - BaseClass##_##Method##_Benchmark() : BaseClass() { \ - this->SetName(#BaseClass"<" #a "," #b ">/" #Method); \ - } \ - \ - protected: \ - virtual void BenchmarkCase(::benchmark::State&); \ - }; - -#ifdef BENCHMARK_HAS_CXX11 -#define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, ...) \ - class BaseClass##_##Method##_Benchmark : public BaseClass<__VA_ARGS__> { \ - public: \ - BaseClass##_##Method##_Benchmark() : BaseClass<__VA_ARGS__>() { \ - this->SetName(#BaseClass"<" #__VA_ARGS__ ">/" #Method); \ - } \ - \ - protected: \ - virtual void BenchmarkCase(::benchmark::State&); \ - }; -#else -#define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(n, a) BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(n, a) -#endif - -#define BENCHMARK_DEFINE_F(BaseClass, Method) \ - BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \ - void BaseClass##_##Method##_Benchmark::BenchmarkCase - -#define BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a) \ - BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \ - void BaseClass##_##Method##_Benchmark::BenchmarkCase - -#define BENCHMARK_TEMPLATE2_DEFINE_F(BaseClass, Method, a, b) \ - BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \ - void BaseClass##_##Method##_Benchmark::BenchmarkCase - -#ifdef BENCHMARK_HAS_CXX11 -#define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, ...) \ - BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \ - void BaseClass##_##Method##_Benchmark::BenchmarkCase -#else -#define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, a) BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a) -#endif - -#define BENCHMARK_REGISTER_F(BaseClass, Method) \ - BENCHMARK_PRIVATE_REGISTER_F(BaseClass##_##Method##_Benchmark) - -#define BENCHMARK_PRIVATE_REGISTER_F(TestName) \ - BENCHMARK_PRIVATE_DECLARE(TestName) = \ - (::benchmark::internal::RegisterBenchmarkInternal(new TestName())) - -// This macro will define and register a benchmark within a fixture class. -#define BENCHMARK_F(BaseClass, Method) \ - BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \ - BENCHMARK_REGISTER_F(BaseClass, Method); \ - void BaseClass##_##Method##_Benchmark::BenchmarkCase - -#define BENCHMARK_TEMPLATE1_F(BaseClass, Method, a) \ - BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \ - BENCHMARK_REGISTER_F(BaseClass, Method); \ - void BaseClass##_##Method##_Benchmark::BenchmarkCase - -#define BENCHMARK_TEMPLATE2_F(BaseClass, Method, a, b) \ - BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \ - BENCHMARK_REGISTER_F(BaseClass, Method); \ - void BaseClass##_##Method##_Benchmark::BenchmarkCase - -#ifdef BENCHMARK_HAS_CXX11 -#define BENCHMARK_TEMPLATE_F(BaseClass, Method, ...) \ - BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \ - BENCHMARK_REGISTER_F(BaseClass, Method); \ - void BaseClass##_##Method##_Benchmark::BenchmarkCase -#else -#define BENCHMARK_TEMPLATE_F(BaseClass, Method, a) BENCHMARK_TEMPLATE1_F(BaseClass, Method, a) -#endif - -// Helper macro to create a main routine in a test that runs the benchmarks -#define BENCHMARK_MAIN() \ - int main(int argc, char** argv) { \ - ::benchmark::Initialize(&argc, argv); \ - if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; \ - ::benchmark::RunSpecifiedBenchmarks(); \ - } \ - int main(int, char**) - - -// ------------------------------------------------------ -// Benchmark Reporters - -namespace benchmark { - -struct CPUInfo { - struct CacheInfo { - std::string type; - int level; - int size; - int num_sharing; - }; - - int num_cpus; - double cycles_per_second; - std::vector caches; - bool scaling_enabled; - - static const CPUInfo& Get(); - - private: - CPUInfo(); - BENCHMARK_DISALLOW_COPY_AND_ASSIGN(CPUInfo); -}; - -// Interface for custom benchmark result printers. -// By default, benchmark reports are printed to stdout. However an application -// can control the destination of the reports by calling -// RunSpecifiedBenchmarks and passing it a custom reporter object. -// The reporter object must implement the following interface. -class BenchmarkReporter { - public: - struct Context { - CPUInfo const& cpu_info; - // The number of chars in the longest benchmark name. - size_t name_field_width; - static const char *executable_name; - Context(); - }; - - struct Run { - Run() - : error_occurred(false), - iterations(1), - time_unit(kNanosecond), - real_accumulated_time(0), - cpu_accumulated_time(0), - bytes_per_second(0), - items_per_second(0), - max_heapbytes_used(0), - complexity(oNone), - complexity_lambda(), - complexity_n(0), - report_big_o(false), - report_rms(false), - counters() {} - - std::string benchmark_name; - std::string report_label; // Empty if not set by benchmark. - bool error_occurred; - std::string error_message; - - int64_t iterations; - TimeUnit time_unit; - double real_accumulated_time; - double cpu_accumulated_time; - - // Return a value representing the real time per iteration in the unit - // specified by 'time_unit'. - // NOTE: If 'iterations' is zero the returned value represents the - // accumulated time. - double GetAdjustedRealTime() const; - - // Return a value representing the cpu time per iteration in the unit - // specified by 'time_unit'. - // NOTE: If 'iterations' is zero the returned value represents the - // accumulated time. - double GetAdjustedCPUTime() const; - - // Zero if not set by benchmark. - double bytes_per_second; - double items_per_second; - - // This is set to 0.0 if memory tracing is not enabled. - double max_heapbytes_used; - - // Keep track of arguments to compute asymptotic complexity - BigO complexity; - BigOFunc* complexity_lambda; - int64_t complexity_n; - - // what statistics to compute from the measurements - const std::vector* statistics; - - // Inform print function whether the current run is a complexity report - bool report_big_o; - bool report_rms; - - UserCounters counters; - }; - - // Construct a BenchmarkReporter with the output stream set to 'std::cout' - // and the error stream set to 'std::cerr' - BenchmarkReporter(); - - // Called once for every suite of benchmarks run. - // The parameter "context" contains information that the - // reporter may wish to use when generating its report, for example the - // platform under which the benchmarks are running. The benchmark run is - // never started if this function returns false, allowing the reporter - // to skip runs based on the context information. - virtual bool ReportContext(const Context& context) = 0; - - // Called once for each group of benchmark runs, gives information about - // cpu-time and heap memory usage during the benchmark run. If the group - // of runs contained more than two entries then 'report' contains additional - // elements representing the mean and standard deviation of those runs. - // Additionally if this group of runs was the last in a family of benchmarks - // 'reports' contains additional entries representing the asymptotic - // complexity and RMS of that benchmark family. - virtual void ReportRuns(const std::vector& report) = 0; - - // Called once and only once after ever group of benchmarks is run and - // reported. - virtual void Finalize() {} - - // REQUIRES: The object referenced by 'out' is valid for the lifetime - // of the reporter. - void SetOutputStream(std::ostream* out) { - assert(out); - output_stream_ = out; - } - - // REQUIRES: The object referenced by 'err' is valid for the lifetime - // of the reporter. - void SetErrorStream(std::ostream* err) { - assert(err); - error_stream_ = err; - } - - std::ostream& GetOutputStream() const { return *output_stream_; } - - std::ostream& GetErrorStream() const { return *error_stream_; } - - virtual ~BenchmarkReporter(); - - // Write a human readable string to 'out' representing the specified - // 'context'. - // REQUIRES: 'out' is non-null. - static void PrintBasicContext(std::ostream* out, Context const& context); - - private: - std::ostream* output_stream_; - std::ostream* error_stream_; -}; - -// Simple reporter that outputs benchmark data to the console. This is the -// default reporter used by RunSpecifiedBenchmarks(). -class ConsoleReporter : public BenchmarkReporter { -public: - enum OutputOptions { - OO_None = 0, - OO_Color = 1, - OO_Tabular = 2, - OO_ColorTabular = OO_Color|OO_Tabular, - OO_Defaults = OO_ColorTabular - }; - explicit ConsoleReporter(OutputOptions opts_ = OO_Defaults) - : output_options_(opts_), name_field_width_(0), - prev_counters_(), printed_header_(false) {} - - virtual bool ReportContext(const Context& context); - virtual void ReportRuns(const std::vector& reports); - - protected: - virtual void PrintRunData(const Run& report); - virtual void PrintHeader(const Run& report); - - OutputOptions output_options_; - size_t name_field_width_; - UserCounters prev_counters_; - bool printed_header_; -}; - -class JSONReporter : public BenchmarkReporter { - public: - JSONReporter() : first_report_(true) {} - virtual bool ReportContext(const Context& context); - virtual void ReportRuns(const std::vector& reports); - virtual void Finalize(); - - private: - void PrintRunData(const Run& report); - - bool first_report_; -}; - -class CSVReporter : public BenchmarkReporter { - public: - CSVReporter() : printed_header_(false) {} - virtual bool ReportContext(const Context& context); - virtual void ReportRuns(const std::vector& reports); - - private: - void PrintRunData(const Run& report); - - bool printed_header_; - std::set< std::string > user_counter_names_; -}; - -inline const char* GetTimeUnitString(TimeUnit unit) { - switch (unit) { - case kMillisecond: - return "ms"; - case kMicrosecond: - return "us"; - case kNanosecond: - default: - return "ns"; - } -} - -inline double GetTimeUnitMultiplier(TimeUnit unit) { - switch (unit) { - case kMillisecond: - return 1e3; - case kMicrosecond: - return 1e6; - case kNanosecond: - default: - return 1e9; - } -} - -} // namespace benchmark - -#endif // BENCHMARK_BENCHMARK_H_ diff --git a/benchmarks/thirdparty/benchmark/mingw.py b/benchmarks/thirdparty/benchmark/mingw.py deleted file mode 100755 index 706ad559db..0000000000 --- a/benchmarks/thirdparty/benchmark/mingw.py +++ /dev/null @@ -1,320 +0,0 @@ -#! /usr/bin/env python -# encoding: utf-8 - -import argparse -import errno -import logging -import os -import platform -import re -import sys -import subprocess -import tempfile - -try: - import winreg -except ImportError: - import _winreg as winreg -try: - import urllib.request as request -except ImportError: - import urllib as request -try: - import urllib.parse as parse -except ImportError: - import urlparse as parse - -class EmptyLogger(object): - ''' - Provides an implementation that performs no logging - ''' - def debug(self, *k, **kw): - pass - def info(self, *k, **kw): - pass - def warn(self, *k, **kw): - pass - def error(self, *k, **kw): - pass - def critical(self, *k, **kw): - pass - def setLevel(self, *k, **kw): - pass - -urls = ( - 'http://downloads.sourceforge.net/project/mingw-w64/Toolchains%20' - 'targetting%20Win32/Personal%20Builds/mingw-builds/installer/' - 'repository.txt', - 'http://downloads.sourceforge.net/project/mingwbuilds/host-windows/' - 'repository.txt' -) -''' -A list of mingw-build repositories -''' - -def repository(urls = urls, log = EmptyLogger()): - ''' - Downloads and parse mingw-build repository files and parses them - ''' - log.info('getting mingw-builds repository') - versions = {} - re_sourceforge = re.compile(r'http://sourceforge.net/projects/([^/]+)/files') - re_sub = r'http://downloads.sourceforge.net/project/\1' - for url in urls: - log.debug(' - requesting: %s', url) - socket = request.urlopen(url) - repo = socket.read() - if not isinstance(repo, str): - repo = repo.decode(); - socket.close() - for entry in repo.split('\n')[:-1]: - value = entry.split('|') - version = tuple([int(n) for n in value[0].strip().split('.')]) - version = versions.setdefault(version, {}) - arch = value[1].strip() - if arch == 'x32': - arch = 'i686' - elif arch == 'x64': - arch = 'x86_64' - arch = version.setdefault(arch, {}) - threading = arch.setdefault(value[2].strip(), {}) - exceptions = threading.setdefault(value[3].strip(), {}) - revision = exceptions.setdefault(int(value[4].strip()[3:]), - re_sourceforge.sub(re_sub, value[5].strip())) - return versions - -def find_in_path(file, path=None): - ''' - Attempts to find an executable in the path - ''' - if platform.system() == 'Windows': - file += '.exe' - if path is None: - path = os.environ.get('PATH', '') - if type(path) is type(''): - path = path.split(os.pathsep) - return list(filter(os.path.exists, - map(lambda dir, file=file: os.path.join(dir, file), path))) - -def find_7zip(log = EmptyLogger()): - ''' - Attempts to find 7zip for unpacking the mingw-build archives - ''' - log.info('finding 7zip') - path = find_in_path('7z') - if not path: - key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\7-Zip') - path, _ = winreg.QueryValueEx(key, 'Path') - path = [os.path.join(path, '7z.exe')] - log.debug('found \'%s\'', path[0]) - return path[0] - -find_7zip() - -def unpack(archive, location, log = EmptyLogger()): - ''' - Unpacks a mingw-builds archive - ''' - sevenzip = find_7zip(log) - log.info('unpacking %s', os.path.basename(archive)) - cmd = [sevenzip, 'x', archive, '-o' + location, '-y'] - log.debug(' - %r', cmd) - with open(os.devnull, 'w') as devnull: - subprocess.check_call(cmd, stdout = devnull) - -def download(url, location, log = EmptyLogger()): - ''' - Downloads and unpacks a mingw-builds archive - ''' - log.info('downloading MinGW') - log.debug(' - url: %s', url) - log.debug(' - location: %s', location) - - re_content = re.compile(r'attachment;[ \t]*filename=(")?([^"]*)(")?[\r\n]*') - - stream = request.urlopen(url) - try: - content = stream.getheader('Content-Disposition') or '' - except AttributeError: - content = stream.headers.getheader('Content-Disposition') or '' - matches = re_content.match(content) - if matches: - filename = matches.group(2) - else: - parsed = parse.urlparse(stream.geturl()) - filename = os.path.basename(parsed.path) - - try: - os.makedirs(location) - except OSError as e: - if e.errno == errno.EEXIST and os.path.isdir(location): - pass - else: - raise - - archive = os.path.join(location, filename) - with open(archive, 'wb') as out: - while True: - buf = stream.read(1024) - if not buf: - break - out.write(buf) - unpack(archive, location, log = log) - os.remove(archive) - - possible = os.path.join(location, 'mingw64') - if not os.path.exists(possible): - possible = os.path.join(location, 'mingw32') - if not os.path.exists(possible): - raise ValueError('Failed to find unpacked MinGW: ' + possible) - return possible - -def root(location = None, arch = None, version = None, threading = None, - exceptions = None, revision = None, log = EmptyLogger()): - ''' - Returns the root folder of a specific version of the mingw-builds variant - of gcc. Will download the compiler if needed - ''' - - # Get the repository if we don't have all the information - if not (arch and version and threading and exceptions and revision): - versions = repository(log = log) - - # Determine some defaults - version = version or max(versions.keys()) - if not arch: - arch = platform.machine().lower() - if arch == 'x86': - arch = 'i686' - elif arch == 'amd64': - arch = 'x86_64' - if not threading: - keys = versions[version][arch].keys() - if 'posix' in keys: - threading = 'posix' - elif 'win32' in keys: - threading = 'win32' - else: - threading = keys[0] - if not exceptions: - keys = versions[version][arch][threading].keys() - if 'seh' in keys: - exceptions = 'seh' - elif 'sjlj' in keys: - exceptions = 'sjlj' - else: - exceptions = keys[0] - if revision == None: - revision = max(versions[version][arch][threading][exceptions].keys()) - if not location: - location = os.path.join(tempfile.gettempdir(), 'mingw-builds') - - # Get the download url - url = versions[version][arch][threading][exceptions][revision] - - # Tell the user whatzzup - log.info('finding MinGW %s', '.'.join(str(v) for v in version)) - log.debug(' - arch: %s', arch) - log.debug(' - threading: %s', threading) - log.debug(' - exceptions: %s', exceptions) - log.debug(' - revision: %s', revision) - log.debug(' - url: %s', url) - - # Store each specific revision differently - slug = '{version}-{arch}-{threading}-{exceptions}-rev{revision}' - slug = slug.format( - version = '.'.join(str(v) for v in version), - arch = arch, - threading = threading, - exceptions = exceptions, - revision = revision - ) - if arch == 'x86_64': - root_dir = os.path.join(location, slug, 'mingw64') - elif arch == 'i686': - root_dir = os.path.join(location, slug, 'mingw32') - else: - raise ValueError('Unknown MinGW arch: ' + arch) - - # Download if needed - if not os.path.exists(root_dir): - downloaded = download(url, os.path.join(location, slug), log = log) - if downloaded != root_dir: - raise ValueError('The location of mingw did not match\n%s\n%s' - % (downloaded, root_dir)) - - return root_dir - -def str2ver(string): - ''' - Converts a version string into a tuple - ''' - try: - version = tuple(int(v) for v in string.split('.')) - if len(version) is not 3: - raise ValueError() - except ValueError: - raise argparse.ArgumentTypeError( - 'please provide a three digit version string') - return version - -def main(): - ''' - Invoked when the script is run directly by the python interpreter - ''' - parser = argparse.ArgumentParser( - description = 'Downloads a specific version of MinGW', - formatter_class = argparse.ArgumentDefaultsHelpFormatter - ) - parser.add_argument('--location', - help = 'the location to download the compiler to', - default = os.path.join(tempfile.gettempdir(), 'mingw-builds')) - parser.add_argument('--arch', required = True, choices = ['i686', 'x86_64'], - help = 'the target MinGW architecture string') - parser.add_argument('--version', type = str2ver, - help = 'the version of GCC to download') - parser.add_argument('--threading', choices = ['posix', 'win32'], - help = 'the threading type of the compiler') - parser.add_argument('--exceptions', choices = ['sjlj', 'seh', 'dwarf'], - help = 'the method to throw exceptions') - parser.add_argument('--revision', type=int, - help = 'the revision of the MinGW release') - group = parser.add_mutually_exclusive_group() - group.add_argument('-v', '--verbose', action='store_true', - help='increase the script output verbosity') - group.add_argument('-q', '--quiet', action='store_true', - help='only print errors and warning') - args = parser.parse_args() - - # Create the logger - logger = logging.getLogger('mingw') - handler = logging.StreamHandler() - formatter = logging.Formatter('%(message)s') - handler.setFormatter(formatter) - logger.addHandler(handler) - logger.setLevel(logging.INFO) - if args.quiet: - logger.setLevel(logging.WARN) - if args.verbose: - logger.setLevel(logging.DEBUG) - - # Get MinGW - root_dir = root(location = args.location, arch = args.arch, - version = args.version, threading = args.threading, - exceptions = args.exceptions, revision = args.revision, - log = logger) - - sys.stdout.write('%s\n' % os.path.join(root_dir, 'bin')) - -if __name__ == '__main__': - try: - main() - except IOError as e: - sys.stderr.write('IO error: %s\n' % e) - sys.exit(1) - except OSError as e: - sys.stderr.write('OS error: %s\n' % e) - sys.exit(1) - except KeyboardInterrupt as e: - sys.stderr.write('Killed\n') - sys.exit(1) diff --git a/benchmarks/thirdparty/benchmark/releasing.md b/benchmarks/thirdparty/benchmark/releasing.md deleted file mode 100755 index f0cd7010e3..0000000000 --- a/benchmarks/thirdparty/benchmark/releasing.md +++ /dev/null @@ -1,16 +0,0 @@ -# How to release - -* Make sure you're on master and synced to HEAD -* Ensure the project builds and tests run (sanity check only, obviously) - * `parallel -j0 exec ::: test/*_test` can help ensure everything at least - passes -* Prepare release notes - * `git log $(git describe --abbrev=0 --tags)..HEAD` gives you the list of - commits between the last annotated tag and HEAD - * Pick the most interesting. -* Create a release through github's interface - * Note this will create a lightweight tag. - * Update this to an annotated tag: - * `git pull --tags` - * `git tag -a -f ` - * `git push --force origin` diff --git a/benchmarks/thirdparty/benchmark/src/CMakeLists.txt b/benchmarks/thirdparty/benchmark/src/CMakeLists.txt deleted file mode 100755 index 701804ba0e..0000000000 --- a/benchmarks/thirdparty/benchmark/src/CMakeLists.txt +++ /dev/null @@ -1,105 +0,0 @@ -# Allow the source files to find headers in src/ -include_directories(${PROJECT_SOURCE_DIR}/src) - -if (DEFINED BENCHMARK_CXX_LINKER_FLAGS) - list(APPEND CMAKE_SHARED_LINKER_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS}) - list(APPEND CMAKE_MODULE_LINKER_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS}) -endif() - -file(GLOB - SOURCE_FILES - *.cc - ${PROJECT_SOURCE_DIR}/include/benchmark/*.h - ${CMAKE_CURRENT_SOURCE_DIR}/*.h) -list(FILTER SOURCE_FILES EXCLUDE REGEX "benchmark_main\\.cc") - -add_library(benchmark ${SOURCE_FILES}) -set_target_properties(benchmark PROPERTIES - OUTPUT_NAME "benchmark" - VERSION ${GENERIC_LIB_VERSION} - SOVERSION ${GENERIC_LIB_SOVERSION} -) -target_include_directories(benchmark PUBLIC - $ - ) - -# Link threads. -target_link_libraries(benchmark ${BENCHMARK_CXX_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) -find_library(LIBRT rt) -if(LIBRT) - target_link_libraries(benchmark ${LIBRT}) -endif() - -# We need extra libraries on Windows -if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") - target_link_libraries(benchmark Shlwapi) -endif() - -# We need extra libraries on Solaris -if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS") - target_link_libraries(benchmark kstat) -endif() - -# Benchmark main library -add_library(benchmark_main "benchmark_main.cc") -set_target_properties(benchmark_main PROPERTIES - OUTPUT_NAME "benchmark_main" - VERSION ${GENERIC_LIB_VERSION} - SOVERSION ${GENERIC_LIB_SOVERSION} -) -target_include_directories(benchmark PUBLIC - $ - ) -target_link_libraries(benchmark_main benchmark) - -set(include_install_dir "include") -set(lib_install_dir "lib/") -set(bin_install_dir "bin/") -set(config_install_dir "lib/cmake/${PROJECT_NAME}") -set(pkgconfig_install_dir "lib/pkgconfig") - -set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated") - -set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake") -set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake") -set(pkg_config "${generated_dir}/${PROJECT_NAME}.pc") -set(targets_export_name "${PROJECT_NAME}Targets") - -set(namespace "${PROJECT_NAME}::") - -include(CMakePackageConfigHelpers) -write_basic_package_version_file( - "${version_config}" VERSION ${GIT_VERSION} COMPATIBILITY SameMajorVersion -) - -configure_file("${PROJECT_SOURCE_DIR}/cmake/Config.cmake.in" "${project_config}" @ONLY) -configure_file("${PROJECT_SOURCE_DIR}/cmake/benchmark.pc.in" "${pkg_config}" @ONLY) - -if (BENCHMARK_ENABLE_INSTALL) - # Install target (will install the library to specified CMAKE_INSTALL_PREFIX variable) - install( - TARGETS benchmark benchmark_main - EXPORT ${targets_export_name} - ARCHIVE DESTINATION ${lib_install_dir} - LIBRARY DESTINATION ${lib_install_dir} - RUNTIME DESTINATION ${bin_install_dir} - INCLUDES DESTINATION ${include_install_dir}) - - install( - DIRECTORY "${PROJECT_SOURCE_DIR}/include/benchmark" - DESTINATION ${include_install_dir} - FILES_MATCHING PATTERN "*.*h") - - install( - FILES "${project_config}" "${version_config}" - DESTINATION "${config_install_dir}") - - install( - FILES "${pkg_config}" - DESTINATION "${pkgconfig_install_dir}") - - install( - EXPORT "${targets_export_name}" - NAMESPACE "${namespace}" - DESTINATION "${config_install_dir}") -endif() diff --git a/benchmarks/thirdparty/benchmark/src/arraysize.h b/benchmarks/thirdparty/benchmark/src/arraysize.h deleted file mode 100755 index 51a50f2dff..0000000000 --- a/benchmarks/thirdparty/benchmark/src/arraysize.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef BENCHMARK_ARRAYSIZE_H_ -#define BENCHMARK_ARRAYSIZE_H_ - -#include "internal_macros.h" - -namespace benchmark { -namespace internal { -// The arraysize(arr) macro returns the # of elements in an array arr. -// The expression is a compile-time constant, and therefore can be -// used in defining new arrays, for example. If you use arraysize on -// a pointer by mistake, you will get a compile-time error. -// - -// This template function declaration is used in defining arraysize. -// Note that the function doesn't need an implementation, as we only -// use its type. -template -char (&ArraySizeHelper(T (&array)[N]))[N]; - -// That gcc wants both of these prototypes seems mysterious. VC, for -// its part, can't decide which to use (another mystery). Matching of -// template overloads: the final frontier. -#ifndef COMPILER_MSVC -template -char (&ArraySizeHelper(const T (&array)[N]))[N]; -#endif - -#define arraysize(array) (sizeof(::benchmark::internal::ArraySizeHelper(array))) - -} // end namespace internal -} // end namespace benchmark - -#endif // BENCHMARK_ARRAYSIZE_H_ diff --git a/benchmarks/thirdparty/benchmark/src/benchmark.cc b/benchmarks/thirdparty/benchmark/src/benchmark.cc deleted file mode 100755 index 82b15ac709..0000000000 --- a/benchmarks/thirdparty/benchmark/src/benchmark.cc +++ /dev/null @@ -1,630 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "benchmark/benchmark.h" -#include "benchmark_api_internal.h" -#include "internal_macros.h" - -#ifndef BENCHMARK_OS_WINDOWS -#ifndef BENCHMARK_OS_FUCHSIA -#include -#endif -#include -#include -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "check.h" -#include "colorprint.h" -#include "commandlineflags.h" -#include "complexity.h" -#include "counter.h" -#include "internal_macros.h" -#include "log.h" -#include "mutex.h" -#include "re.h" -#include "statistics.h" -#include "string_util.h" -#include "thread_manager.h" -#include "thread_timer.h" - -DEFINE_bool(benchmark_list_tests, false, - "Print a list of benchmarks. This option overrides all other " - "options."); - -DEFINE_string(benchmark_filter, ".", - "A regular expression that specifies the set of benchmarks " - "to execute. If this flag is empty, no benchmarks are run. " - "If this flag is the string \"all\", all benchmarks linked " - "into the process are run."); - -DEFINE_double(benchmark_min_time, 0.5, - "Minimum number of seconds we should run benchmark before " - "results are considered significant. For cpu-time based " - "tests, this is the lower bound on the total cpu time " - "used by all threads that make up the test. For real-time " - "based tests, this is the lower bound on the elapsed time " - "of the benchmark execution, regardless of number of " - "threads."); - -DEFINE_int32(benchmark_repetitions, 1, - "The number of runs of each benchmark. If greater than 1, the " - "mean and standard deviation of the runs will be reported."); - -DEFINE_bool(benchmark_report_aggregates_only, false, - "Report the result of each benchmark repetitions. When 'true' is " - "specified only the mean, standard deviation, and other statistics " - "are reported for repeated benchmarks."); - -DEFINE_string(benchmark_format, "console", - "The format to use for console output. Valid values are " - "'console', 'json', or 'csv'."); - -DEFINE_string(benchmark_out_format, "json", - "The format to use for file output. Valid values are " - "'console', 'json', or 'csv'."); - -DEFINE_string(benchmark_out, "", "The file to write additional output to"); - -DEFINE_string(benchmark_color, "auto", - "Whether to use colors in the output. Valid values: " - "'true'/'yes'/1, 'false'/'no'/0, and 'auto'. 'auto' means to use " - "colors if the output is being sent to a terminal and the TERM " - "environment variable is set to a terminal type that supports " - "colors."); - -DEFINE_bool(benchmark_counters_tabular, false, - "Whether to use tabular format when printing user counters to " - "the console. Valid values: 'true'/'yes'/1, 'false'/'no'/0." - "Defaults to false."); - -DEFINE_int32(v, 0, "The level of verbose logging to output"); - -namespace benchmark { - -namespace { -static const size_t kMaxIterations = 1000000000; -} // end namespace - -namespace internal { - -void UseCharPointer(char const volatile*) {} - -namespace { - -BenchmarkReporter::Run CreateRunReport( - const benchmark::internal::Benchmark::Instance& b, - const internal::ThreadManager::Result& results, - double seconds) { - // Create report about this benchmark run. - BenchmarkReporter::Run report; - - report.benchmark_name = b.name; - report.error_occurred = results.has_error_; - report.error_message = results.error_message_; - report.report_label = results.report_label_; - // This is the total iterations across all threads. - report.iterations = results.iterations; - report.time_unit = b.time_unit; - - if (!report.error_occurred) { - double bytes_per_second = 0; - if (results.bytes_processed > 0 && seconds > 0.0) { - bytes_per_second = (results.bytes_processed / seconds); - } - double items_per_second = 0; - if (results.items_processed > 0 && seconds > 0.0) { - items_per_second = (results.items_processed / seconds); - } - - if (b.use_manual_time) { - report.real_accumulated_time = results.manual_time_used; - } else { - report.real_accumulated_time = results.real_time_used; - } - report.cpu_accumulated_time = results.cpu_time_used; - report.bytes_per_second = bytes_per_second; - report.items_per_second = items_per_second; - report.complexity_n = results.complexity_n; - report.complexity = b.complexity; - report.complexity_lambda = b.complexity_lambda; - report.statistics = b.statistics; - report.counters = results.counters; - internal::Finish(&report.counters, seconds, b.threads); - } - return report; -} - -// Execute one thread of benchmark b for the specified number of iterations. -// Adds the stats collected for the thread into *total. -void RunInThread(const benchmark::internal::Benchmark::Instance* b, - size_t iters, int thread_id, - internal::ThreadManager* manager) { - internal::ThreadTimer timer; - State st(iters, b->arg, thread_id, b->threads, &timer, manager); - b->benchmark->Run(st); - CHECK(st.iterations() >= st.max_iterations) - << "Benchmark returned before State::KeepRunning() returned false!"; - { - MutexLock l(manager->GetBenchmarkMutex()); - internal::ThreadManager::Result& results = manager->results; - results.iterations += st.iterations(); - results.cpu_time_used += timer.cpu_time_used(); - results.real_time_used += timer.real_time_used(); - results.manual_time_used += timer.manual_time_used(); - results.bytes_processed += st.bytes_processed(); - results.items_processed += st.items_processed(); - results.complexity_n += st.complexity_length_n(); - internal::Increment(&results.counters, st.counters); - } - manager->NotifyThreadComplete(); -} - -std::vector RunBenchmark( - const benchmark::internal::Benchmark::Instance& b, - std::vector* complexity_reports) { - std::vector reports; // return value - - const bool has_explicit_iteration_count = b.iterations != 0; - size_t iters = has_explicit_iteration_count ? b.iterations : 1; - std::unique_ptr manager; - std::vector pool(b.threads - 1); - const int repeats = - b.repetitions != 0 ? b.repetitions : FLAGS_benchmark_repetitions; - const bool report_aggregates_only = - repeats != 1 && - (b.report_mode == internal::RM_Unspecified - ? FLAGS_benchmark_report_aggregates_only - : b.report_mode == internal::RM_ReportAggregatesOnly); - for (int repetition_num = 0; repetition_num < repeats; repetition_num++) { - for (;;) { - // Try benchmark - VLOG(2) << "Running " << b.name << " for " << iters << "\n"; - - manager.reset(new internal::ThreadManager(b.threads)); - for (std::size_t ti = 0; ti < pool.size(); ++ti) { - pool[ti] = std::thread(&RunInThread, &b, iters, - static_cast(ti + 1), manager.get()); - } - RunInThread(&b, iters, 0, manager.get()); - manager->WaitForAllThreads(); - for (std::thread& thread : pool) thread.join(); - internal::ThreadManager::Result results; - { - MutexLock l(manager->GetBenchmarkMutex()); - results = manager->results; - } - manager.reset(); - // Adjust real/manual time stats since they were reported per thread. - results.real_time_used /= b.threads; - results.manual_time_used /= b.threads; - - VLOG(2) << "Ran in " << results.cpu_time_used << "/" - << results.real_time_used << "\n"; - - // Base decisions off of real time if requested by this benchmark. - double seconds = results.cpu_time_used; - if (b.use_manual_time) { - seconds = results.manual_time_used; - } else if (b.use_real_time) { - seconds = results.real_time_used; - } - - const double min_time = - !IsZero(b.min_time) ? b.min_time : FLAGS_benchmark_min_time; - - // Determine if this run should be reported; Either it has - // run for a sufficient amount of time or because an error was reported. - const bool should_report = repetition_num > 0 - || has_explicit_iteration_count // An exact iteration count was requested - || results.has_error_ - || iters >= kMaxIterations // No chance to try again, we hit the limit. - || seconds >= min_time // the elapsed time is large enough - // CPU time is specified but the elapsed real time greatly exceeds the - // minimum time. Note that user provided timers are except from this - // sanity check. - || ((results.real_time_used >= 5 * min_time) && !b.use_manual_time); - - if (should_report) { - BenchmarkReporter::Run report = CreateRunReport(b, results, seconds); - if (!report.error_occurred && b.complexity != oNone) - complexity_reports->push_back(report); - reports.push_back(report); - break; - } - - // See how much iterations should be increased by - // Note: Avoid division by zero with max(seconds, 1ns). - double multiplier = min_time * 1.4 / std::max(seconds, 1e-9); - // If our last run was at least 10% of FLAGS_benchmark_min_time then we - // use the multiplier directly. Otherwise we use at most 10 times - // expansion. - // NOTE: When the last run was at least 10% of the min time the max - // expansion should be 14x. - bool is_significant = (seconds / min_time) > 0.1; - multiplier = is_significant ? multiplier : std::min(10.0, multiplier); - if (multiplier <= 1.0) multiplier = 2.0; - double next_iters = std::max(multiplier * iters, iters + 1.0); - if (next_iters > kMaxIterations) { - next_iters = kMaxIterations; - } - VLOG(3) << "Next iters: " << next_iters << ", " << multiplier << "\n"; - iters = static_cast(next_iters + 0.5); - } - } - // Calculate additional statistics - auto stat_reports = ComputeStats(reports); - if ((b.complexity != oNone) && b.last_benchmark_instance) { - auto additional_run_stats = ComputeBigO(*complexity_reports); - stat_reports.insert(stat_reports.end(), additional_run_stats.begin(), - additional_run_stats.end()); - complexity_reports->clear(); - } - - if (report_aggregates_only) reports.clear(); - reports.insert(reports.end(), stat_reports.begin(), stat_reports.end()); - return reports; -} - -} // namespace -} // namespace internal - -State::State(size_t max_iters, const std::vector& ranges, int thread_i, - int n_threads, internal::ThreadTimer* timer, - internal::ThreadManager* manager) - : total_iterations_(0), - batch_leftover_(0), - max_iterations(max_iters), - started_(false), - finished_(false), - error_occurred_(false), - range_(ranges), - bytes_processed_(0), - items_processed_(0), - complexity_n_(0), - counters(), - thread_index(thread_i), - threads(n_threads), - timer_(timer), - manager_(manager) { - CHECK(max_iterations != 0) << "At least one iteration must be run"; - CHECK_LT(thread_index, threads) << "thread_index must be less than threads"; - - // Note: The use of offsetof below is technically undefined until C++17 - // because State is not a standard layout type. However, all compilers - // currently provide well-defined behavior as an extension (which is - // demonstrated since constexpr evaluation must diagnose all undefined - // behavior). However, GCC and Clang also warn about this use of offsetof, - // which must be suppressed. -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Winvalid-offsetof" -#endif - // Offset tests to ensure commonly accessed data is on the first cache line. - const int cache_line_size = 64; - static_assert(offsetof(State, error_occurred_) <= - (cache_line_size - sizeof(error_occurred_)), ""); -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif -} - -void State::PauseTiming() { - // Add in time accumulated so far - CHECK(started_ && !finished_ && !error_occurred_); - timer_->StopTimer(); -} - -void State::ResumeTiming() { - CHECK(started_ && !finished_ && !error_occurred_); - timer_->StartTimer(); -} - -void State::SkipWithError(const char* msg) { - CHECK(msg); - error_occurred_ = true; - { - MutexLock l(manager_->GetBenchmarkMutex()); - if (manager_->results.has_error_ == false) { - manager_->results.error_message_ = msg; - manager_->results.has_error_ = true; - } - } - total_iterations_ = 0; - if (timer_->running()) timer_->StopTimer(); -} - -void State::SetIterationTime(double seconds) { - timer_->SetIterationTime(seconds); -} - -void State::SetLabel(const char* label) { - MutexLock l(manager_->GetBenchmarkMutex()); - manager_->results.report_label_ = label; -} - -void State::StartKeepRunning() { - CHECK(!started_ && !finished_); - started_ = true; - total_iterations_ = error_occurred_ ? 0 : max_iterations; - manager_->StartStopBarrier(); - if (!error_occurred_) ResumeTiming(); -} - -void State::FinishKeepRunning() { - CHECK(started_ && (!finished_ || error_occurred_)); - if (!error_occurred_) { - PauseTiming(); - } - // Total iterations has now wrapped around past 0. Fix this. - total_iterations_ = 0; - finished_ = true; - manager_->StartStopBarrier(); -} - -namespace internal { -namespace { - -void RunBenchmarks(const std::vector& benchmarks, - BenchmarkReporter* console_reporter, - BenchmarkReporter* file_reporter) { - // Note the file_reporter can be null. - CHECK(console_reporter != nullptr); - - // Determine the width of the name field using a minimum width of 10. - bool has_repetitions = FLAGS_benchmark_repetitions > 1; - size_t name_field_width = 10; - size_t stat_field_width = 0; - for (const Benchmark::Instance& benchmark : benchmarks) { - name_field_width = - std::max(name_field_width, benchmark.name.size()); - has_repetitions |= benchmark.repetitions > 1; - - for(const auto& Stat : *benchmark.statistics) - stat_field_width = std::max(stat_field_width, Stat.name_.size()); - } - if (has_repetitions) name_field_width += 1 + stat_field_width; - - // Print header here - BenchmarkReporter::Context context; - context.name_field_width = name_field_width; - - // Keep track of running times of all instances of current benchmark - std::vector complexity_reports; - - // We flush streams after invoking reporter methods that write to them. This - // ensures users get timely updates even when streams are not line-buffered. - auto flushStreams = [](BenchmarkReporter* reporter) { - if (!reporter) return; - std::flush(reporter->GetOutputStream()); - std::flush(reporter->GetErrorStream()); - }; - - if (console_reporter->ReportContext(context) && - (!file_reporter || file_reporter->ReportContext(context))) { - flushStreams(console_reporter); - flushStreams(file_reporter); - for (const auto& benchmark : benchmarks) { - std::vector reports = - RunBenchmark(benchmark, &complexity_reports); - console_reporter->ReportRuns(reports); - if (file_reporter) file_reporter->ReportRuns(reports); - flushStreams(console_reporter); - flushStreams(file_reporter); - } - } - console_reporter->Finalize(); - if (file_reporter) file_reporter->Finalize(); - flushStreams(console_reporter); - flushStreams(file_reporter); -} - -std::unique_ptr CreateReporter( - std::string const& name, ConsoleReporter::OutputOptions output_opts) { - typedef std::unique_ptr PtrType; - if (name == "console") { - return PtrType(new ConsoleReporter(output_opts)); - } else if (name == "json") { - return PtrType(new JSONReporter); - } else if (name == "csv") { - return PtrType(new CSVReporter); - } else { - std::cerr << "Unexpected format: '" << name << "'\n"; - std::exit(1); - } -} - -} // end namespace - -bool IsZero(double n) { - return std::abs(n) < std::numeric_limits::epsilon(); -} - -ConsoleReporter::OutputOptions GetOutputOptions(bool force_no_color) { - int output_opts = ConsoleReporter::OO_Defaults; - if ((FLAGS_benchmark_color == "auto" && IsColorTerminal()) || - IsTruthyFlagValue(FLAGS_benchmark_color)) { - output_opts |= ConsoleReporter::OO_Color; - } else { - output_opts &= ~ConsoleReporter::OO_Color; - } - if(force_no_color) { - output_opts &= ~ConsoleReporter::OO_Color; - } - if(FLAGS_benchmark_counters_tabular) { - output_opts |= ConsoleReporter::OO_Tabular; - } else { - output_opts &= ~ConsoleReporter::OO_Tabular; - } - return static_cast< ConsoleReporter::OutputOptions >(output_opts); -} - -} // end namespace internal - -size_t RunSpecifiedBenchmarks() { - return RunSpecifiedBenchmarks(nullptr, nullptr); -} - -size_t RunSpecifiedBenchmarks(BenchmarkReporter* console_reporter) { - return RunSpecifiedBenchmarks(console_reporter, nullptr); -} - -size_t RunSpecifiedBenchmarks(BenchmarkReporter* console_reporter, - BenchmarkReporter* file_reporter) { - std::string spec = FLAGS_benchmark_filter; - if (spec.empty() || spec == "all") - spec = "."; // Regexp that matches all benchmarks - - // Setup the reporters - std::ofstream output_file; - std::unique_ptr default_console_reporter; - std::unique_ptr default_file_reporter; - if (!console_reporter) { - default_console_reporter = internal::CreateReporter( - FLAGS_benchmark_format, internal::GetOutputOptions()); - console_reporter = default_console_reporter.get(); - } - auto& Out = console_reporter->GetOutputStream(); - auto& Err = console_reporter->GetErrorStream(); - - std::string const& fname = FLAGS_benchmark_out; - if (fname.empty() && file_reporter) { - Err << "A custom file reporter was provided but " - "--benchmark_out= was not specified." - << std::endl; - std::exit(1); - } - if (!fname.empty()) { - output_file.open(fname); - if (!output_file.is_open()) { - Err << "invalid file name: '" << fname << std::endl; - std::exit(1); - } - if (!file_reporter) { - default_file_reporter = internal::CreateReporter( - FLAGS_benchmark_out_format, ConsoleReporter::OO_None); - file_reporter = default_file_reporter.get(); - } - file_reporter->SetOutputStream(&output_file); - file_reporter->SetErrorStream(&output_file); - } - - std::vector benchmarks; - if (!FindBenchmarksInternal(spec, &benchmarks, &Err)) return 0; - - if (benchmarks.empty()) { - Err << "Failed to match any benchmarks against regex: " << spec << "\n"; - return 0; - } - - if (FLAGS_benchmark_list_tests) { - for (auto const& benchmark : benchmarks) Out << benchmark.name << "\n"; - } else { - internal::RunBenchmarks(benchmarks, console_reporter, file_reporter); - } - - return benchmarks.size(); -} - -namespace internal { - -void PrintUsageAndExit() { - fprintf(stdout, - "benchmark" - " [--benchmark_list_tests={true|false}]\n" - " [--benchmark_filter=]\n" - " [--benchmark_min_time=]\n" - " [--benchmark_repetitions=]\n" - " [--benchmark_report_aggregates_only={true|false}\n" - " [--benchmark_format=]\n" - " [--benchmark_out=]\n" - " [--benchmark_out_format=]\n" - " [--benchmark_color={auto|true|false}]\n" - " [--benchmark_counters_tabular={true|false}]\n" - " [--v=]\n"); - exit(0); -} - -void ParseCommandLineFlags(int* argc, char** argv) { - using namespace benchmark; - BenchmarkReporter::Context::executable_name = argv[0]; - for (int i = 1; i < *argc; ++i) { - if (ParseBoolFlag(argv[i], "benchmark_list_tests", - &FLAGS_benchmark_list_tests) || - ParseStringFlag(argv[i], "benchmark_filter", &FLAGS_benchmark_filter) || - ParseDoubleFlag(argv[i], "benchmark_min_time", - &FLAGS_benchmark_min_time) || - ParseInt32Flag(argv[i], "benchmark_repetitions", - &FLAGS_benchmark_repetitions) || - ParseBoolFlag(argv[i], "benchmark_report_aggregates_only", - &FLAGS_benchmark_report_aggregates_only) || - ParseStringFlag(argv[i], "benchmark_format", &FLAGS_benchmark_format) || - ParseStringFlag(argv[i], "benchmark_out", &FLAGS_benchmark_out) || - ParseStringFlag(argv[i], "benchmark_out_format", - &FLAGS_benchmark_out_format) || - ParseStringFlag(argv[i], "benchmark_color", &FLAGS_benchmark_color) || - // "color_print" is the deprecated name for "benchmark_color". - // TODO: Remove this. - ParseStringFlag(argv[i], "color_print", &FLAGS_benchmark_color) || - ParseBoolFlag(argv[i], "benchmark_counters_tabular", - &FLAGS_benchmark_counters_tabular) || - ParseInt32Flag(argv[i], "v", &FLAGS_v)) { - for (int j = i; j != *argc - 1; ++j) argv[j] = argv[j + 1]; - - --(*argc); - --i; - } else if (IsFlag(argv[i], "help")) { - PrintUsageAndExit(); - } - } - for (auto const* flag : - {&FLAGS_benchmark_format, &FLAGS_benchmark_out_format}) - if (*flag != "console" && *flag != "json" && *flag != "csv") { - PrintUsageAndExit(); - } - if (FLAGS_benchmark_color.empty()) { - PrintUsageAndExit(); - } -} - -int InitializeStreams() { - static std::ios_base::Init init; - return 0; -} - -} // end namespace internal - -void Initialize(int* argc, char** argv) { - internal::ParseCommandLineFlags(argc, argv); - internal::LogLevel() = FLAGS_v; -} - -bool ReportUnrecognizedArguments(int argc, char** argv) { - for (int i = 1; i < argc; ++i) { - fprintf(stderr, "%s: error: unrecognized command-line flag: %s\n", argv[0], argv[i]); - } - return argc > 1; -} - -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/benchmark_api_internal.h b/benchmarks/thirdparty/benchmark/src/benchmark_api_internal.h deleted file mode 100755 index dd7a3ffe8c..0000000000 --- a/benchmarks/thirdparty/benchmark/src/benchmark_api_internal.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef BENCHMARK_API_INTERNAL_H -#define BENCHMARK_API_INTERNAL_H - -#include "benchmark/benchmark.h" - -#include -#include -#include -#include -#include - -namespace benchmark { -namespace internal { - -// Information kept per benchmark we may want to run -struct Benchmark::Instance { - std::string name; - Benchmark* benchmark; - ReportMode report_mode; - std::vector arg; - TimeUnit time_unit; - int range_multiplier; - bool use_real_time; - bool use_manual_time; - BigO complexity; - BigOFunc* complexity_lambda; - UserCounters counters; - const std::vector* statistics; - bool last_benchmark_instance; - int repetitions; - double min_time; - size_t iterations; - int threads; // Number of concurrent threads to us -}; - -bool FindBenchmarksInternal(const std::string& re, - std::vector* benchmarks, - std::ostream* Err); - -bool IsZero(double n); - -ConsoleReporter::OutputOptions GetOutputOptions(bool force_no_color = false); - -} // end namespace internal -} // end namespace benchmark - -#endif // BENCHMARK_API_INTERNAL_H diff --git a/benchmarks/thirdparty/benchmark/src/benchmark_main.cc b/benchmarks/thirdparty/benchmark/src/benchmark_main.cc deleted file mode 100755 index b3b2478314..0000000000 --- a/benchmarks/thirdparty/benchmark/src/benchmark_main.cc +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2018 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "benchmark/benchmark.h" - -BENCHMARK_MAIN(); diff --git a/benchmarks/thirdparty/benchmark/src/benchmark_register.cc b/benchmarks/thirdparty/benchmark/src/benchmark_register.cc deleted file mode 100755 index dc6f935685..0000000000 --- a/benchmarks/thirdparty/benchmark/src/benchmark_register.cc +++ /dev/null @@ -1,461 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "benchmark_register.h" - -#ifndef BENCHMARK_OS_WINDOWS -#ifndef BENCHMARK_OS_FUCHSIA -#include -#endif -#include -#include -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "benchmark/benchmark.h" -#include "benchmark_api_internal.h" -#include "check.h" -#include "commandlineflags.h" -#include "complexity.h" -#include "internal_macros.h" -#include "log.h" -#include "mutex.h" -#include "re.h" -#include "statistics.h" -#include "string_util.h" -#include "timers.h" - -namespace benchmark { - -namespace { -// For non-dense Range, intermediate values are powers of kRangeMultiplier. -static const int kRangeMultiplier = 8; -// The size of a benchmark family determines is the number of inputs to repeat -// the benchmark on. If this is "large" then warn the user during configuration. -static const size_t kMaxFamilySize = 100; -} // end namespace - -namespace internal { - -//=============================================================================// -// BenchmarkFamilies -//=============================================================================// - -// Class for managing registered benchmarks. Note that each registered -// benchmark identifies a family of related benchmarks to run. -class BenchmarkFamilies { - public: - static BenchmarkFamilies* GetInstance(); - - // Registers a benchmark family and returns the index assigned to it. - size_t AddBenchmark(std::unique_ptr family); - - // Clear all registered benchmark families. - void ClearBenchmarks(); - - // Extract the list of benchmark instances that match the specified - // regular expression. - bool FindBenchmarks(std::string re, - std::vector* benchmarks, - std::ostream* Err); - - private: - BenchmarkFamilies() {} - - std::vector> families_; - Mutex mutex_; -}; - -BenchmarkFamilies* BenchmarkFamilies::GetInstance() { - static BenchmarkFamilies instance; - return &instance; -} - -size_t BenchmarkFamilies::AddBenchmark(std::unique_ptr family) { - MutexLock l(mutex_); - size_t index = families_.size(); - families_.push_back(std::move(family)); - return index; -} - -void BenchmarkFamilies::ClearBenchmarks() { - MutexLock l(mutex_); - families_.clear(); - families_.shrink_to_fit(); -} - -bool BenchmarkFamilies::FindBenchmarks( - std::string spec, std::vector* benchmarks, - std::ostream* ErrStream) { - CHECK(ErrStream); - auto& Err = *ErrStream; - // Make regular expression out of command-line flag - std::string error_msg; - Regex re; - bool isNegativeFilter = false; - if(spec[0] == '-') { - spec.replace(0, 1, ""); - isNegativeFilter = true; - } - if (!re.Init(spec, &error_msg)) { - Err << "Could not compile benchmark re: " << error_msg << std::endl; - return false; - } - - // Special list of thread counts to use when none are specified - const std::vector one_thread = {1}; - - MutexLock l(mutex_); - for (std::unique_ptr& family : families_) { - // Family was deleted or benchmark doesn't match - if (!family) continue; - - if (family->ArgsCnt() == -1) { - family->Args({}); - } - const std::vector* thread_counts = - (family->thread_counts_.empty() - ? &one_thread - : &static_cast&>(family->thread_counts_)); - const size_t family_size = family->args_.size() * thread_counts->size(); - // The benchmark will be run at least 'family_size' different inputs. - // If 'family_size' is very large warn the user. - if (family_size > kMaxFamilySize) { - Err << "The number of inputs is very large. " << family->name_ - << " will be repeated at least " << family_size << " times.\n"; - } - // reserve in the special case the regex ".", since we know the final - // family size. - if (spec == ".") benchmarks->reserve(family_size); - - for (auto const& args : family->args_) { - for (int num_threads : *thread_counts) { - Benchmark::Instance instance; - instance.name = family->name_; - instance.benchmark = family.get(); - instance.report_mode = family->report_mode_; - instance.arg = args; - instance.time_unit = family->time_unit_; - instance.range_multiplier = family->range_multiplier_; - instance.min_time = family->min_time_; - instance.iterations = family->iterations_; - instance.repetitions = family->repetitions_; - instance.use_real_time = family->use_real_time_; - instance.use_manual_time = family->use_manual_time_; - instance.complexity = family->complexity_; - instance.complexity_lambda = family->complexity_lambda_; - instance.statistics = &family->statistics_; - instance.threads = num_threads; - - // Add arguments to instance name - size_t arg_i = 0; - for (auto const& arg : args) { - instance.name += "/"; - - if (arg_i < family->arg_names_.size()) { - const auto& arg_name = family->arg_names_[arg_i]; - if (!arg_name.empty()) { - instance.name += - StrFormat("%s:", family->arg_names_[arg_i].c_str()); - } - } - - instance.name += StrFormat("%d", arg); - ++arg_i; - } - - if (!IsZero(family->min_time_)) - instance.name += StrFormat("/min_time:%0.3f", family->min_time_); - if (family->iterations_ != 0) - instance.name += StrFormat("/iterations:%d", family->iterations_); - if (family->repetitions_ != 0) - instance.name += StrFormat("/repeats:%d", family->repetitions_); - - if (family->use_manual_time_) { - instance.name += "/manual_time"; - } else if (family->use_real_time_) { - instance.name += "/real_time"; - } - - // Add the number of threads used to the name - if (!family->thread_counts_.empty()) { - instance.name += StrFormat("/threads:%d", instance.threads); - } - - if ((re.Match(instance.name) && !isNegativeFilter) || - (!re.Match(instance.name) && isNegativeFilter)) { - instance.last_benchmark_instance = (&args == &family->args_.back()); - benchmarks->push_back(std::move(instance)); - } - } - } - } - return true; -} - -Benchmark* RegisterBenchmarkInternal(Benchmark* bench) { - std::unique_ptr bench_ptr(bench); - BenchmarkFamilies* families = BenchmarkFamilies::GetInstance(); - families->AddBenchmark(std::move(bench_ptr)); - return bench; -} - -// FIXME: This function is a hack so that benchmark.cc can access -// `BenchmarkFamilies` -bool FindBenchmarksInternal(const std::string& re, - std::vector* benchmarks, - std::ostream* Err) { - return BenchmarkFamilies::GetInstance()->FindBenchmarks(re, benchmarks, Err); -} - -//=============================================================================// -// Benchmark -//=============================================================================// - -Benchmark::Benchmark(const char* name) - : name_(name), - report_mode_(RM_Unspecified), - time_unit_(kNanosecond), - range_multiplier_(kRangeMultiplier), - min_time_(0), - iterations_(0), - repetitions_(0), - use_real_time_(false), - use_manual_time_(false), - complexity_(oNone), - complexity_lambda_(nullptr) { - ComputeStatistics("mean", StatisticsMean); - ComputeStatistics("median", StatisticsMedian); - ComputeStatistics("stddev", StatisticsStdDev); -} - -Benchmark::~Benchmark() {} - -Benchmark* Benchmark::Arg(int64_t x) { - CHECK(ArgsCnt() == -1 || ArgsCnt() == 1); - args_.push_back({x}); - return this; -} - -Benchmark* Benchmark::Unit(TimeUnit unit) { - time_unit_ = unit; - return this; -} - -Benchmark* Benchmark::Range(int64_t start, int64_t limit) { - CHECK(ArgsCnt() == -1 || ArgsCnt() == 1); - std::vector arglist; - AddRange(&arglist, start, limit, range_multiplier_); - - for (int64_t i : arglist) { - args_.push_back({i}); - } - return this; -} - -Benchmark* Benchmark::Ranges( - const std::vector>& ranges) { - CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast(ranges.size())); - std::vector> arglists(ranges.size()); - std::size_t total = 1; - for (std::size_t i = 0; i < ranges.size(); i++) { - AddRange(&arglists[i], ranges[i].first, ranges[i].second, - range_multiplier_); - total *= arglists[i].size(); - } - - std::vector ctr(arglists.size(), 0); - - for (std::size_t i = 0; i < total; i++) { - std::vector tmp; - tmp.reserve(arglists.size()); - - for (std::size_t j = 0; j < arglists.size(); j++) { - tmp.push_back(arglists[j].at(ctr[j])); - } - - args_.push_back(std::move(tmp)); - - for (std::size_t j = 0; j < arglists.size(); j++) { - if (ctr[j] + 1 < arglists[j].size()) { - ++ctr[j]; - break; - } - ctr[j] = 0; - } - } - return this; -} - -Benchmark* Benchmark::ArgName(const std::string& name) { - CHECK(ArgsCnt() == -1 || ArgsCnt() == 1); - arg_names_ = {name}; - return this; -} - -Benchmark* Benchmark::ArgNames(const std::vector& names) { - CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast(names.size())); - arg_names_ = names; - return this; -} - -Benchmark* Benchmark::DenseRange(int64_t start, int64_t limit, int step) { - CHECK(ArgsCnt() == -1 || ArgsCnt() == 1); - CHECK_GE(start, 0); - CHECK_LE(start, limit); - for (int64_t arg = start; arg <= limit; arg += step) { - args_.push_back({arg}); - } - return this; -} - -Benchmark* Benchmark::Args(const std::vector& args) { - CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast(args.size())); - args_.push_back(args); - return this; -} - -Benchmark* Benchmark::Apply(void (*custom_arguments)(Benchmark* benchmark)) { - custom_arguments(this); - return this; -} - -Benchmark* Benchmark::RangeMultiplier(int multiplier) { - CHECK(multiplier > 1); - range_multiplier_ = multiplier; - return this; -} - -Benchmark* Benchmark::MinTime(double t) { - CHECK(t > 0.0); - CHECK(iterations_ == 0); - min_time_ = t; - return this; -} - -Benchmark* Benchmark::Iterations(size_t n) { - CHECK(n > 0); - CHECK(IsZero(min_time_)); - iterations_ = n; - return this; -} - -Benchmark* Benchmark::Repetitions(int n) { - CHECK(n > 0); - repetitions_ = n; - return this; -} - -Benchmark* Benchmark::ReportAggregatesOnly(bool value) { - report_mode_ = value ? RM_ReportAggregatesOnly : RM_Default; - return this; -} - -Benchmark* Benchmark::UseRealTime() { - CHECK(!use_manual_time_) - << "Cannot set UseRealTime and UseManualTime simultaneously."; - use_real_time_ = true; - return this; -} - -Benchmark* Benchmark::UseManualTime() { - CHECK(!use_real_time_) - << "Cannot set UseRealTime and UseManualTime simultaneously."; - use_manual_time_ = true; - return this; -} - -Benchmark* Benchmark::Complexity(BigO complexity) { - complexity_ = complexity; - return this; -} - -Benchmark* Benchmark::Complexity(BigOFunc* complexity) { - complexity_lambda_ = complexity; - complexity_ = oLambda; - return this; -} - -Benchmark* Benchmark::ComputeStatistics(std::string name, - StatisticsFunc* statistics) { - statistics_.emplace_back(name, statistics); - return this; -} - -Benchmark* Benchmark::Threads(int t) { - CHECK_GT(t, 0); - thread_counts_.push_back(t); - return this; -} - -Benchmark* Benchmark::ThreadRange(int min_threads, int max_threads) { - CHECK_GT(min_threads, 0); - CHECK_GE(max_threads, min_threads); - - AddRange(&thread_counts_, min_threads, max_threads, 2); - return this; -} - -Benchmark* Benchmark::DenseThreadRange(int min_threads, int max_threads, - int stride) { - CHECK_GT(min_threads, 0); - CHECK_GE(max_threads, min_threads); - CHECK_GE(stride, 1); - - for (auto i = min_threads; i < max_threads; i += stride) { - thread_counts_.push_back(i); - } - thread_counts_.push_back(max_threads); - return this; -} - -Benchmark* Benchmark::ThreadPerCpu() { - thread_counts_.push_back(CPUInfo::Get().num_cpus); - return this; -} - -void Benchmark::SetName(const char* name) { name_ = name; } - -int Benchmark::ArgsCnt() const { - if (args_.empty()) { - if (arg_names_.empty()) return -1; - return static_cast(arg_names_.size()); - } - return static_cast(args_.front().size()); -} - -//=============================================================================// -// FunctionBenchmark -//=============================================================================// - -void FunctionBenchmark::Run(State& st) { func_(st); } - -} // end namespace internal - -void ClearRegisteredBenchmarks() { - internal::BenchmarkFamilies::GetInstance()->ClearBenchmarks(); -} - -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/benchmark_register.h b/benchmarks/thirdparty/benchmark/src/benchmark_register.h deleted file mode 100755 index 0705e219f2..0000000000 --- a/benchmarks/thirdparty/benchmark/src/benchmark_register.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef BENCHMARK_REGISTER_H -#define BENCHMARK_REGISTER_H - -#include - -#include "check.h" - -template -void AddRange(std::vector* dst, T lo, T hi, int mult) { - CHECK_GE(lo, 0); - CHECK_GE(hi, lo); - CHECK_GE(mult, 2); - - // Add "lo" - dst->push_back(lo); - - static const T kmax = std::numeric_limits::max(); - - // Now space out the benchmarks in multiples of "mult" - for (T i = 1; i < kmax / mult; i *= mult) { - if (i >= hi) break; - if (i > lo) { - dst->push_back(i); - } - } - - // Add "hi" (if different from "lo") - if (hi != lo) { - dst->push_back(hi); - } -} - -#endif // BENCHMARK_REGISTER_H diff --git a/benchmarks/thirdparty/benchmark/src/check.h b/benchmarks/thirdparty/benchmark/src/check.h deleted file mode 100755 index 73bead2fb5..0000000000 --- a/benchmarks/thirdparty/benchmark/src/check.h +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef CHECK_H_ -#define CHECK_H_ - -#include -#include -#include - -#include "internal_macros.h" -#include "log.h" - -namespace benchmark { -namespace internal { - -typedef void(AbortHandlerT)(); - -inline AbortHandlerT*& GetAbortHandler() { - static AbortHandlerT* handler = &std::abort; - return handler; -} - -BENCHMARK_NORETURN inline void CallAbortHandler() { - GetAbortHandler()(); - std::abort(); // fallback to enforce noreturn -} - -// CheckHandler is the class constructed by failing CHECK macros. CheckHandler -// will log information about the failures and abort when it is destructed. -class CheckHandler { - public: - CheckHandler(const char* check, const char* file, const char* func, int line) - : log_(GetErrorLogInstance()) { - log_ << file << ":" << line << ": " << func << ": Check `" << check - << "' failed. "; - } - - LogType& GetLog() { return log_; } - - BENCHMARK_NORETURN ~CheckHandler() BENCHMARK_NOEXCEPT_OP(false) { - log_ << std::endl; - CallAbortHandler(); - } - - CheckHandler& operator=(const CheckHandler&) = delete; - CheckHandler(const CheckHandler&) = delete; - CheckHandler() = delete; - - private: - LogType& log_; -}; - -} // end namespace internal -} // end namespace benchmark - -// The CHECK macro returns a std::ostream object that can have extra information -// written to it. -#ifndef NDEBUG -#define CHECK(b) \ - (b ? ::benchmark::internal::GetNullLogInstance() \ - : ::benchmark::internal::CheckHandler(#b, __FILE__, __func__, __LINE__) \ - .GetLog()) -#else -#define CHECK(b) ::benchmark::internal::GetNullLogInstance() -#endif - -#define CHECK_EQ(a, b) CHECK((a) == (b)) -#define CHECK_NE(a, b) CHECK((a) != (b)) -#define CHECK_GE(a, b) CHECK((a) >= (b)) -#define CHECK_LE(a, b) CHECK((a) <= (b)) -#define CHECK_GT(a, b) CHECK((a) > (b)) -#define CHECK_LT(a, b) CHECK((a) < (b)) - -#define CHECK_FLOAT_EQ(a, b, eps) CHECK(std::fabs((a) - (b)) < (eps)) -#define CHECK_FLOAT_NE(a, b, eps) CHECK(std::fabs((a) - (b)) >= (eps)) -#define CHECK_FLOAT_GE(a, b, eps) CHECK((a) - (b) > -(eps)) -#define CHECK_FLOAT_LE(a, b, eps) CHECK((b) - (a) > -(eps)) -#define CHECK_FLOAT_GT(a, b, eps) CHECK((a) - (b) > (eps)) -#define CHECK_FLOAT_LT(a, b, eps) CHECK((b) - (a) > (eps)) - -#endif // CHECK_H_ diff --git a/benchmarks/thirdparty/benchmark/src/colorprint.cc b/benchmarks/thirdparty/benchmark/src/colorprint.cc deleted file mode 100755 index 2dec4a8b28..0000000000 --- a/benchmarks/thirdparty/benchmark/src/colorprint.cc +++ /dev/null @@ -1,188 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "colorprint.h" - -#include -#include -#include -#include -#include -#include - -#include "check.h" -#include "internal_macros.h" - -#ifdef BENCHMARK_OS_WINDOWS -#include -#include -#else -#include -#endif // BENCHMARK_OS_WINDOWS - -namespace benchmark { -namespace { -#ifdef BENCHMARK_OS_WINDOWS -typedef WORD PlatformColorCode; -#else -typedef const char* PlatformColorCode; -#endif - -PlatformColorCode GetPlatformColorCode(LogColor color) { -#ifdef BENCHMARK_OS_WINDOWS - switch (color) { - case COLOR_RED: - return FOREGROUND_RED; - case COLOR_GREEN: - return FOREGROUND_GREEN; - case COLOR_YELLOW: - return FOREGROUND_RED | FOREGROUND_GREEN; - case COLOR_BLUE: - return FOREGROUND_BLUE; - case COLOR_MAGENTA: - return FOREGROUND_BLUE | FOREGROUND_RED; - case COLOR_CYAN: - return FOREGROUND_BLUE | FOREGROUND_GREEN; - case COLOR_WHITE: // fall through to default - default: - return 0; - } -#else - switch (color) { - case COLOR_RED: - return "1"; - case COLOR_GREEN: - return "2"; - case COLOR_YELLOW: - return "3"; - case COLOR_BLUE: - return "4"; - case COLOR_MAGENTA: - return "5"; - case COLOR_CYAN: - return "6"; - case COLOR_WHITE: - return "7"; - default: - return nullptr; - }; -#endif -} - -} // end namespace - -std::string FormatString(const char* msg, va_list args) { - // we might need a second shot at this, so pre-emptivly make a copy - va_list args_cp; - va_copy(args_cp, args); - - std::size_t size = 256; - char local_buff[256]; - auto ret = vsnprintf(local_buff, size, msg, args_cp); - - va_end(args_cp); - - // currently there is no error handling for failure, so this is hack. - CHECK(ret >= 0); - - if (ret == 0) // handle empty expansion - return {}; - else if (static_cast(ret) < size) - return local_buff; - else { - // we did not provide a long enough buffer on our first attempt. - size = (size_t)ret + 1; // + 1 for the null byte - std::unique_ptr buff(new char[size]); - ret = vsnprintf(buff.get(), size, msg, args); - CHECK(ret > 0 && ((size_t)ret) < size); - return buff.get(); - } -} - -std::string FormatString(const char* msg, ...) { - va_list args; - va_start(args, msg); - auto tmp = FormatString(msg, args); - va_end(args); - return tmp; -} - -void ColorPrintf(std::ostream& out, LogColor color, const char* fmt, ...) { - va_list args; - va_start(args, fmt); - ColorPrintf(out, color, fmt, args); - va_end(args); -} - -void ColorPrintf(std::ostream& out, LogColor color, const char* fmt, - va_list args) { -#ifdef BENCHMARK_OS_WINDOWS - ((void)out); // suppress unused warning - - const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); - - // Gets the current text color. - CONSOLE_SCREEN_BUFFER_INFO buffer_info; - GetConsoleScreenBufferInfo(stdout_handle, &buffer_info); - const WORD old_color_attrs = buffer_info.wAttributes; - - // We need to flush the stream buffers into the console before each - // SetConsoleTextAttribute call lest it affect the text that is already - // printed but has not yet reached the console. - fflush(stdout); - SetConsoleTextAttribute(stdout_handle, - GetPlatformColorCode(color) | FOREGROUND_INTENSITY); - vprintf(fmt, args); - - fflush(stdout); - // Restores the text color. - SetConsoleTextAttribute(stdout_handle, old_color_attrs); -#else - const char* color_code = GetPlatformColorCode(color); - if (color_code) out << FormatString("\033[0;3%sm", color_code); - out << FormatString(fmt, args) << "\033[m"; -#endif -} - -bool IsColorTerminal() { -#if BENCHMARK_OS_WINDOWS - // On Windows the TERM variable is usually not set, but the - // console there does support colors. - return 0 != _isatty(_fileno(stdout)); -#else - // On non-Windows platforms, we rely on the TERM variable. This list of - // supported TERM values is copied from Google Test: - // . - const char* const SUPPORTED_TERM_VALUES[] = { - "xterm", "xterm-color", "xterm-256color", - "screen", "screen-256color", "tmux", - "tmux-256color", "rxvt-unicode", "rxvt-unicode-256color", - "linux", "cygwin", - }; - - const char* const term = getenv("TERM"); - - bool term_supports_color = false; - for (const char* candidate : SUPPORTED_TERM_VALUES) { - if (term && 0 == strcmp(term, candidate)) { - term_supports_color = true; - break; - } - } - - return 0 != isatty(fileno(stdout)) && term_supports_color; -#endif // BENCHMARK_OS_WINDOWS -} - -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/colorprint.h b/benchmarks/thirdparty/benchmark/src/colorprint.h deleted file mode 100755 index 9f6fab9b34..0000000000 --- a/benchmarks/thirdparty/benchmark/src/colorprint.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef BENCHMARK_COLORPRINT_H_ -#define BENCHMARK_COLORPRINT_H_ - -#include -#include -#include - -namespace benchmark { -enum LogColor { - COLOR_DEFAULT, - COLOR_RED, - COLOR_GREEN, - COLOR_YELLOW, - COLOR_BLUE, - COLOR_MAGENTA, - COLOR_CYAN, - COLOR_WHITE -}; - -std::string FormatString(const char* msg, va_list args); -std::string FormatString(const char* msg, ...); - -void ColorPrintf(std::ostream& out, LogColor color, const char* fmt, - va_list args); -void ColorPrintf(std::ostream& out, LogColor color, const char* fmt, ...); - -// Returns true if stdout appears to be a terminal that supports colored -// output, false otherwise. -bool IsColorTerminal(); - -} // end namespace benchmark - -#endif // BENCHMARK_COLORPRINT_H_ diff --git a/benchmarks/thirdparty/benchmark/src/commandlineflags.cc b/benchmarks/thirdparty/benchmark/src/commandlineflags.cc deleted file mode 100755 index 2fc92517a3..0000000000 --- a/benchmarks/thirdparty/benchmark/src/commandlineflags.cc +++ /dev/null @@ -1,218 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "commandlineflags.h" - -#include -#include -#include -#include -#include - -namespace benchmark { -// Parses 'str' for a 32-bit signed integer. If successful, writes -// the result to *value and returns true; otherwise leaves *value -// unchanged and returns false. -bool ParseInt32(const std::string& src_text, const char* str, int32_t* value) { - // Parses the environment variable as a decimal integer. - char* end = nullptr; - const long long_value = strtol(str, &end, 10); // NOLINT - - // Has strtol() consumed all characters in the string? - if (*end != '\0') { - // No - an invalid character was encountered. - std::cerr << src_text << " is expected to be a 32-bit integer, " - << "but actually has value \"" << str << "\".\n"; - return false; - } - - // Is the parsed value in the range of an Int32? - const int32_t result = static_cast(long_value); - if (long_value == std::numeric_limits::max() || - long_value == std::numeric_limits::min() || - // The parsed value overflows as a long. (strtol() returns - // LONG_MAX or LONG_MIN when the input overflows.) - result != long_value - // The parsed value overflows as an Int32. - ) { - std::cerr << src_text << " is expected to be a 32-bit integer, " - << "but actually has value \"" << str << "\", " - << "which overflows.\n"; - return false; - } - - *value = result; - return true; -} - -// Parses 'str' for a double. If successful, writes the result to *value and -// returns true; otherwise leaves *value unchanged and returns false. -bool ParseDouble(const std::string& src_text, const char* str, double* value) { - // Parses the environment variable as a decimal integer. - char* end = nullptr; - const double double_value = strtod(str, &end); // NOLINT - - // Has strtol() consumed all characters in the string? - if (*end != '\0') { - // No - an invalid character was encountered. - std::cerr << src_text << " is expected to be a double, " - << "but actually has value \"" << str << "\".\n"; - return false; - } - - *value = double_value; - return true; -} - -// Returns the name of the environment variable corresponding to the -// given flag. For example, FlagToEnvVar("foo") will return -// "BENCHMARK_FOO" in the open-source version. -static std::string FlagToEnvVar(const char* flag) { - const std::string flag_str(flag); - - std::string env_var; - for (size_t i = 0; i != flag_str.length(); ++i) - env_var += static_cast(::toupper(flag_str.c_str()[i])); - - return "BENCHMARK_" + env_var; -} - -// Reads and returns the Boolean environment variable corresponding to -// the given flag; if it's not set, returns default_value. -// -// The value is considered true iff it's not "0". -bool BoolFromEnv(const char* flag, bool default_value) { - const std::string env_var = FlagToEnvVar(flag); - const char* const string_value = getenv(env_var.c_str()); - return string_value == nullptr ? default_value - : strcmp(string_value, "0") != 0; -} - -// Reads and returns a 32-bit integer stored in the environment -// variable corresponding to the given flag; if it isn't set or -// doesn't represent a valid 32-bit integer, returns default_value. -int32_t Int32FromEnv(const char* flag, int32_t default_value) { - const std::string env_var = FlagToEnvVar(flag); - const char* const string_value = getenv(env_var.c_str()); - if (string_value == nullptr) { - // The environment variable is not set. - return default_value; - } - - int32_t result = default_value; - if (!ParseInt32(std::string("Environment variable ") + env_var, string_value, - &result)) { - std::cout << "The default value " << default_value << " is used.\n"; - return default_value; - } - - return result; -} - -// Reads and returns the string environment variable corresponding to -// the given flag; if it's not set, returns default_value. -const char* StringFromEnv(const char* flag, const char* default_value) { - const std::string env_var = FlagToEnvVar(flag); - const char* const value = getenv(env_var.c_str()); - return value == nullptr ? default_value : value; -} - -// Parses a string as a command line flag. The string should have -// the format "--flag=value". When def_optional is true, the "=value" -// part can be omitted. -// -// Returns the value of the flag, or nullptr if the parsing failed. -const char* ParseFlagValue(const char* str, const char* flag, - bool def_optional) { - // str and flag must not be nullptr. - if (str == nullptr || flag == nullptr) return nullptr; - - // The flag must start with "--". - const std::string flag_str = std::string("--") + std::string(flag); - const size_t flag_len = flag_str.length(); - if (strncmp(str, flag_str.c_str(), flag_len) != 0) return nullptr; - - // Skips the flag name. - const char* flag_end = str + flag_len; - - // When def_optional is true, it's OK to not have a "=value" part. - if (def_optional && (flag_end[0] == '\0')) return flag_end; - - // If def_optional is true and there are more characters after the - // flag name, or if def_optional is false, there must be a '=' after - // the flag name. - if (flag_end[0] != '=') return nullptr; - - // Returns the string after "=". - return flag_end + 1; -} - -bool ParseBoolFlag(const char* str, const char* flag, bool* value) { - // Gets the value of the flag as a string. - const char* const value_str = ParseFlagValue(str, flag, true); - - // Aborts if the parsing failed. - if (value_str == nullptr) return false; - - // Converts the string value to a bool. - *value = IsTruthyFlagValue(value_str); - return true; -} - -bool ParseInt32Flag(const char* str, const char* flag, int32_t* value) { - // Gets the value of the flag as a string. - const char* const value_str = ParseFlagValue(str, flag, false); - - // Aborts if the parsing failed. - if (value_str == nullptr) return false; - - // Sets *value to the value of the flag. - return ParseInt32(std::string("The value of flag --") + flag, value_str, - value); -} - -bool ParseDoubleFlag(const char* str, const char* flag, double* value) { - // Gets the value of the flag as a string. - const char* const value_str = ParseFlagValue(str, flag, false); - - // Aborts if the parsing failed. - if (value_str == nullptr) return false; - - // Sets *value to the value of the flag. - return ParseDouble(std::string("The value of flag --") + flag, value_str, - value); -} - -bool ParseStringFlag(const char* str, const char* flag, std::string* value) { - // Gets the value of the flag as a string. - const char* const value_str = ParseFlagValue(str, flag, false); - - // Aborts if the parsing failed. - if (value_str == nullptr) return false; - - *value = value_str; - return true; -} - -bool IsFlag(const char* str, const char* flag) { - return (ParseFlagValue(str, flag, true) != nullptr); -} - -bool IsTruthyFlagValue(const std::string& value) { - if (value.empty()) return true; - char ch = value[0]; - return isalnum(ch) && - !(ch == '0' || ch == 'f' || ch == 'F' || ch == 'n' || ch == 'N'); -} -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/commandlineflags.h b/benchmarks/thirdparty/benchmark/src/commandlineflags.h deleted file mode 100755 index 945c9a9fc4..0000000000 --- a/benchmarks/thirdparty/benchmark/src/commandlineflags.h +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef BENCHMARK_COMMANDLINEFLAGS_H_ -#define BENCHMARK_COMMANDLINEFLAGS_H_ - -#include -#include - -// Macro for referencing flags. -#define FLAG(name) FLAGS_##name - -// Macros for declaring flags. -#define DECLARE_bool(name) extern bool FLAG(name) -#define DECLARE_int32(name) extern int32_t FLAG(name) -#define DECLARE_int64(name) extern int64_t FLAG(name) -#define DECLARE_double(name) extern double FLAG(name) -#define DECLARE_string(name) extern std::string FLAG(name) - -// Macros for defining flags. -#define DEFINE_bool(name, default_val, doc) bool FLAG(name) = (default_val) -#define DEFINE_int32(name, default_val, doc) int32_t FLAG(name) = (default_val) -#define DEFINE_int64(name, default_val, doc) int64_t FLAG(name) = (default_val) -#define DEFINE_double(name, default_val, doc) double FLAG(name) = (default_val) -#define DEFINE_string(name, default_val, doc) \ - std::string FLAG(name) = (default_val) - -namespace benchmark { -// Parses 'str' for a 32-bit signed integer. If successful, writes the result -// to *value and returns true; otherwise leaves *value unchanged and returns -// false. -bool ParseInt32(const std::string& src_text, const char* str, int32_t* value); - -// Parses a bool/Int32/string from the environment variable -// corresponding to the given Google Test flag. -bool BoolFromEnv(const char* flag, bool default_val); -int32_t Int32FromEnv(const char* flag, int32_t default_val); -double DoubleFromEnv(const char* flag, double default_val); -const char* StringFromEnv(const char* flag, const char* default_val); - -// Parses a string for a bool flag, in the form of either -// "--flag=value" or "--flag". -// -// In the former case, the value is taken as true if it passes IsTruthyValue(). -// -// In the latter case, the value is taken as true. -// -// On success, stores the value of the flag in *value, and returns -// true. On failure, returns false without changing *value. -bool ParseBoolFlag(const char* str, const char* flag, bool* value); - -// Parses a string for an Int32 flag, in the form of -// "--flag=value". -// -// On success, stores the value of the flag in *value, and returns -// true. On failure, returns false without changing *value. -bool ParseInt32Flag(const char* str, const char* flag, int32_t* value); - -// Parses a string for a Double flag, in the form of -// "--flag=value". -// -// On success, stores the value of the flag in *value, and returns -// true. On failure, returns false without changing *value. -bool ParseDoubleFlag(const char* str, const char* flag, double* value); - -// Parses a string for a string flag, in the form of -// "--flag=value". -// -// On success, stores the value of the flag in *value, and returns -// true. On failure, returns false without changing *value. -bool ParseStringFlag(const char* str, const char* flag, std::string* value); - -// Returns true if the string matches the flag. -bool IsFlag(const char* str, const char* flag); - -// Returns true unless value starts with one of: '0', 'f', 'F', 'n' or 'N', or -// some non-alphanumeric character. As a special case, also returns true if -// value is the empty string. -bool IsTruthyFlagValue(const std::string& value); -} // end namespace benchmark - -#endif // BENCHMARK_COMMANDLINEFLAGS_H_ diff --git a/benchmarks/thirdparty/benchmark/src/complexity.cc b/benchmarks/thirdparty/benchmark/src/complexity.cc deleted file mode 100755 index 97bf6e09b3..0000000000 --- a/benchmarks/thirdparty/benchmark/src/complexity.cc +++ /dev/null @@ -1,220 +0,0 @@ -// Copyright 2016 Ismael Jimenez Martinez. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Source project : https://github.com/ismaelJimenez/cpp.leastsq -// Adapted to be used with google benchmark - -#include "benchmark/benchmark.h" - -#include -#include -#include "check.h" -#include "complexity.h" - -namespace benchmark { - -// Internal function to calculate the different scalability forms -BigOFunc* FittingCurve(BigO complexity) { - switch (complexity) { - case oN: - return [](int64_t n) -> double { return static_cast(n); }; - case oNSquared: - return [](int64_t n) -> double { return std::pow(n, 2); }; - case oNCubed: - return [](int64_t n) -> double { return std::pow(n, 3); }; - case oLogN: - return [](int64_t n) { return log2(n); }; - case oNLogN: - return [](int64_t n) { return n * log2(n); }; - case o1: - default: - return [](int64_t) { return 1.0; }; - } -} - -// Function to return an string for the calculated complexity -std::string GetBigOString(BigO complexity) { - switch (complexity) { - case oN: - return "N"; - case oNSquared: - return "N^2"; - case oNCubed: - return "N^3"; - case oLogN: - return "lgN"; - case oNLogN: - return "NlgN"; - case o1: - return "(1)"; - default: - return "f(N)"; - } -} - -// Find the coefficient for the high-order term in the running time, by -// minimizing the sum of squares of relative error, for the fitting curve -// given by the lambda expression. -// - n : Vector containing the size of the benchmark tests. -// - time : Vector containing the times for the benchmark tests. -// - fitting_curve : lambda expression (e.g. [](int64_t n) {return n; };). - -// For a deeper explanation on the algorithm logic, look the README file at -// http://github.com/ismaelJimenez/Minimal-Cpp-Least-Squared-Fit - -LeastSq MinimalLeastSq(const std::vector& n, - const std::vector& time, - BigOFunc* fitting_curve) { - double sigma_gn = 0.0; - double sigma_gn_squared = 0.0; - double sigma_time = 0.0; - double sigma_time_gn = 0.0; - - // Calculate least square fitting parameter - for (size_t i = 0; i < n.size(); ++i) { - double gn_i = fitting_curve(n[i]); - sigma_gn += gn_i; - sigma_gn_squared += gn_i * gn_i; - sigma_time += time[i]; - sigma_time_gn += time[i] * gn_i; - } - - LeastSq result; - result.complexity = oLambda; - - // Calculate complexity. - result.coef = sigma_time_gn / sigma_gn_squared; - - // Calculate RMS - double rms = 0.0; - for (size_t i = 0; i < n.size(); ++i) { - double fit = result.coef * fitting_curve(n[i]); - rms += pow((time[i] - fit), 2); - } - - // Normalized RMS by the mean of the observed values - double mean = sigma_time / n.size(); - result.rms = sqrt(rms / n.size()) / mean; - - return result; -} - -// Find the coefficient for the high-order term in the running time, by -// minimizing the sum of squares of relative error. -// - n : Vector containing the size of the benchmark tests. -// - time : Vector containing the times for the benchmark tests. -// - complexity : If different than oAuto, the fitting curve will stick to -// this one. If it is oAuto, it will be calculated the best -// fitting curve. -LeastSq MinimalLeastSq(const std::vector& n, - const std::vector& time, const BigO complexity) { - CHECK_EQ(n.size(), time.size()); - CHECK_GE(n.size(), 2); // Do not compute fitting curve is less than two - // benchmark runs are given - CHECK_NE(complexity, oNone); - - LeastSq best_fit; - - if (complexity == oAuto) { - std::vector fit_curves = {oLogN, oN, oNLogN, oNSquared, oNCubed}; - - // Take o1 as default best fitting curve - best_fit = MinimalLeastSq(n, time, FittingCurve(o1)); - best_fit.complexity = o1; - - // Compute all possible fitting curves and stick to the best one - for (const auto& fit : fit_curves) { - LeastSq current_fit = MinimalLeastSq(n, time, FittingCurve(fit)); - if (current_fit.rms < best_fit.rms) { - best_fit = current_fit; - best_fit.complexity = fit; - } - } - } else { - best_fit = MinimalLeastSq(n, time, FittingCurve(complexity)); - best_fit.complexity = complexity; - } - - return best_fit; -} - -std::vector ComputeBigO( - const std::vector& reports) { - typedef BenchmarkReporter::Run Run; - std::vector results; - - if (reports.size() < 2) return results; - - // Accumulators. - std::vector n; - std::vector real_time; - std::vector cpu_time; - - // Populate the accumulators. - for (const Run& run : reports) { - CHECK_GT(run.complexity_n, 0) << "Did you forget to call SetComplexityN?"; - n.push_back(run.complexity_n); - real_time.push_back(run.real_accumulated_time / run.iterations); - cpu_time.push_back(run.cpu_accumulated_time / run.iterations); - } - - LeastSq result_cpu; - LeastSq result_real; - - if (reports[0].complexity == oLambda) { - result_cpu = MinimalLeastSq(n, cpu_time, reports[0].complexity_lambda); - result_real = MinimalLeastSq(n, real_time, reports[0].complexity_lambda); - } else { - result_cpu = MinimalLeastSq(n, cpu_time, reports[0].complexity); - result_real = MinimalLeastSq(n, real_time, result_cpu.complexity); - } - std::string benchmark_name = - reports[0].benchmark_name.substr(0, reports[0].benchmark_name.find('/')); - - // Get the data from the accumulator to BenchmarkReporter::Run's. - Run big_o; - big_o.benchmark_name = benchmark_name + "_BigO"; - big_o.iterations = 0; - big_o.real_accumulated_time = result_real.coef; - big_o.cpu_accumulated_time = result_cpu.coef; - big_o.report_big_o = true; - big_o.complexity = result_cpu.complexity; - - // All the time results are reported after being multiplied by the - // time unit multiplier. But since RMS is a relative quantity it - // should not be multiplied at all. So, here, we _divide_ it by the - // multiplier so that when it is multiplied later the result is the - // correct one. - double multiplier = GetTimeUnitMultiplier(reports[0].time_unit); - - // Only add label to mean/stddev if it is same for all runs - Run rms; - big_o.report_label = reports[0].report_label; - rms.benchmark_name = benchmark_name + "_RMS"; - rms.report_label = big_o.report_label; - rms.iterations = 0; - rms.real_accumulated_time = result_real.rms / multiplier; - rms.cpu_accumulated_time = result_cpu.rms / multiplier; - rms.report_rms = true; - rms.complexity = result_cpu.complexity; - // don't forget to keep the time unit, or we won't be able to - // recover the correct value. - rms.time_unit = reports[0].time_unit; - - results.push_back(big_o); - results.push_back(rms); - return results; -} - -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/complexity.h b/benchmarks/thirdparty/benchmark/src/complexity.h deleted file mode 100755 index df29b48d29..0000000000 --- a/benchmarks/thirdparty/benchmark/src/complexity.h +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2016 Ismael Jimenez Martinez. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Source project : https://github.com/ismaelJimenez/cpp.leastsq -// Adapted to be used with google benchmark - -#ifndef COMPLEXITY_H_ -#define COMPLEXITY_H_ - -#include -#include - -#include "benchmark/benchmark.h" - -namespace benchmark { - -// Return a vector containing the bigO and RMS information for the specified -// list of reports. If 'reports.size() < 2' an empty vector is returned. -std::vector ComputeBigO( - const std::vector& reports); - -// This data structure will contain the result returned by MinimalLeastSq -// - coef : Estimated coeficient for the high-order term as -// interpolated from data. -// - rms : Normalized Root Mean Squared Error. -// - complexity : Scalability form (e.g. oN, oNLogN). In case a scalability -// form has been provided to MinimalLeastSq this will return -// the same value. In case BigO::oAuto has been selected, this -// parameter will return the best fitting curve detected. - -struct LeastSq { - LeastSq() : coef(0.0), rms(0.0), complexity(oNone) {} - - double coef; - double rms; - BigO complexity; -}; - -// Function to return an string for the calculated complexity -std::string GetBigOString(BigO complexity); - -} // end namespace benchmark - -#endif // COMPLEXITY_H_ diff --git a/benchmarks/thirdparty/benchmark/src/console_reporter.cc b/benchmarks/thirdparty/benchmark/src/console_reporter.cc deleted file mode 100755 index 48920ca782..0000000000 --- a/benchmarks/thirdparty/benchmark/src/console_reporter.cc +++ /dev/null @@ -1,182 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "benchmark/benchmark.h" -#include "complexity.h" -#include "counter.h" - -#include -#include -#include -#include -#include -#include -#include - -#include "check.h" -#include "colorprint.h" -#include "commandlineflags.h" -#include "internal_macros.h" -#include "string_util.h" -#include "timers.h" - -namespace benchmark { - -bool ConsoleReporter::ReportContext(const Context& context) { - name_field_width_ = context.name_field_width; - printed_header_ = false; - prev_counters_.clear(); - - PrintBasicContext(&GetErrorStream(), context); - -#ifdef BENCHMARK_OS_WINDOWS - if ((output_options_ & OO_Color) && &std::cout != &GetOutputStream()) { - GetErrorStream() - << "Color printing is only supported for stdout on windows." - " Disabling color printing\n"; - output_options_ = static_cast< OutputOptions >(output_options_ & ~OO_Color); - } -#endif - - return true; -} - -void ConsoleReporter::PrintHeader(const Run& run) { - std::string str = FormatString("%-*s %13s %13s %10s", static_cast(name_field_width_), - "Benchmark", "Time", "CPU", "Iterations"); - if(!run.counters.empty()) { - if(output_options_ & OO_Tabular) { - for(auto const& c : run.counters) { - str += FormatString(" %10s", c.first.c_str()); - } - } else { - str += " UserCounters..."; - } - } - str += "\n"; - std::string line = std::string(str.length(), '-'); - GetOutputStream() << line << "\n" << str << line << "\n"; -} - -void ConsoleReporter::ReportRuns(const std::vector& reports) { - for (const auto& run : reports) { - // print the header: - // --- if none was printed yet - bool print_header = !printed_header_; - // --- or if the format is tabular and this run - // has different fields from the prev header - print_header |= (output_options_ & OO_Tabular) && - (!internal::SameNames(run.counters, prev_counters_)); - if (print_header) { - printed_header_ = true; - prev_counters_ = run.counters; - PrintHeader(run); - } - // As an alternative to printing the headers like this, we could sort - // the benchmarks by header and then print. But this would require - // waiting for the full results before printing, or printing twice. - PrintRunData(run); - } -} - -static void IgnoreColorPrint(std::ostream& out, LogColor, const char* fmt, - ...) { - va_list args; - va_start(args, fmt); - out << FormatString(fmt, args); - va_end(args); -} - -void ConsoleReporter::PrintRunData(const Run& result) { - typedef void(PrinterFn)(std::ostream&, LogColor, const char*, ...); - auto& Out = GetOutputStream(); - PrinterFn* printer = (output_options_ & OO_Color) ? - (PrinterFn*)ColorPrintf : IgnoreColorPrint; - auto name_color = - (result.report_big_o || result.report_rms) ? COLOR_BLUE : COLOR_GREEN; - printer(Out, name_color, "%-*s ", name_field_width_, - result.benchmark_name.c_str()); - - if (result.error_occurred) { - printer(Out, COLOR_RED, "ERROR OCCURRED: \'%s\'", - result.error_message.c_str()); - printer(Out, COLOR_DEFAULT, "\n"); - return; - } - // Format bytes per second - std::string rate; - if (result.bytes_per_second > 0) { - rate = StrCat(" ", HumanReadableNumber(result.bytes_per_second), "B/s"); - } - - // Format items per second - std::string items; - if (result.items_per_second > 0) { - items = - StrCat(" ", HumanReadableNumber(result.items_per_second), " items/s"); - } - - const double real_time = result.GetAdjustedRealTime(); - const double cpu_time = result.GetAdjustedCPUTime(); - - if (result.report_big_o) { - std::string big_o = GetBigOString(result.complexity); - printer(Out, COLOR_YELLOW, "%10.2f %s %10.2f %s ", real_time, big_o.c_str(), - cpu_time, big_o.c_str()); - } else if (result.report_rms) { - printer(Out, COLOR_YELLOW, "%10.0f %% %10.0f %% ", real_time * 100, - cpu_time * 100); - } else { - const char* timeLabel = GetTimeUnitString(result.time_unit); - printer(Out, COLOR_YELLOW, "%10.0f %s %10.0f %s ", real_time, timeLabel, - cpu_time, timeLabel); - } - - if (!result.report_big_o && !result.report_rms) { - printer(Out, COLOR_CYAN, "%10lld", result.iterations); - } - - for (auto& c : result.counters) { - const std::size_t cNameLen = std::max(std::string::size_type(10), - c.first.length()); - auto const& s = HumanReadableNumber(c.second.value, 1000); - if (output_options_ & OO_Tabular) { - if (c.second.flags & Counter::kIsRate) { - printer(Out, COLOR_DEFAULT, " %*s/s", cNameLen - 2, s.c_str()); - } else { - printer(Out, COLOR_DEFAULT, " %*s", cNameLen, s.c_str()); - } - } else { - const char* unit = (c.second.flags & Counter::kIsRate) ? "/s" : ""; - printer(Out, COLOR_DEFAULT, " %s=%s%s", c.first.c_str(), s.c_str(), - unit); - } - } - - if (!rate.empty()) { - printer(Out, COLOR_DEFAULT, " %*s", 13, rate.c_str()); - } - - if (!items.empty()) { - printer(Out, COLOR_DEFAULT, " %*s", 18, items.c_str()); - } - - if (!result.report_label.empty()) { - printer(Out, COLOR_DEFAULT, " %s", result.report_label.c_str()); - } - - printer(Out, COLOR_DEFAULT, "\n"); -} - -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/counter.cc b/benchmarks/thirdparty/benchmark/src/counter.cc deleted file mode 100755 index ed1aa044ee..0000000000 --- a/benchmarks/thirdparty/benchmark/src/counter.cc +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "counter.h" - -namespace benchmark { -namespace internal { - -double Finish(Counter const& c, double cpu_time, double num_threads) { - double v = c.value; - if (c.flags & Counter::kIsRate) { - v /= cpu_time; - } - if (c.flags & Counter::kAvgThreads) { - v /= num_threads; - } - return v; -} - -void Finish(UserCounters *l, double cpu_time, double num_threads) { - for (auto &c : *l) { - c.second.value = Finish(c.second, cpu_time, num_threads); - } -} - -void Increment(UserCounters *l, UserCounters const& r) { - // add counters present in both or just in *l - for (auto &c : *l) { - auto it = r.find(c.first); - if (it != r.end()) { - c.second.value = c.second + it->second; - } - } - // add counters present in r, but not in *l - for (auto const &tc : r) { - auto it = l->find(tc.first); - if (it == l->end()) { - (*l)[tc.first] = tc.second; - } - } -} - -bool SameNames(UserCounters const& l, UserCounters const& r) { - if (&l == &r) return true; - if (l.size() != r.size()) { - return false; - } - for (auto const& c : l) { - if (r.find(c.first) == r.end()) { - return false; - } - } - return true; -} - -} // end namespace internal -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/counter.h b/benchmarks/thirdparty/benchmark/src/counter.h deleted file mode 100755 index dd6865a31d..0000000000 --- a/benchmarks/thirdparty/benchmark/src/counter.h +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "benchmark/benchmark.h" - -namespace benchmark { - -// these counter-related functions are hidden to reduce API surface. -namespace internal { -void Finish(UserCounters *l, double time, double num_threads); -void Increment(UserCounters *l, UserCounters const& r); -bool SameNames(UserCounters const& l, UserCounters const& r); -} // end namespace internal - -} //end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/csv_reporter.cc b/benchmarks/thirdparty/benchmark/src/csv_reporter.cc deleted file mode 100755 index 35510645b0..0000000000 --- a/benchmarks/thirdparty/benchmark/src/csv_reporter.cc +++ /dev/null @@ -1,149 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "benchmark/benchmark.h" -#include "complexity.h" - -#include -#include -#include -#include -#include -#include - -#include "string_util.h" -#include "timers.h" -#include "check.h" - -// File format reference: http://edoceo.com/utilitas/csv-file-format. - -namespace benchmark { - -namespace { -std::vector elements = { - "name", "iterations", "real_time", "cpu_time", - "time_unit", "bytes_per_second", "items_per_second", "label", - "error_occurred", "error_message"}; -} // namespace - -bool CSVReporter::ReportContext(const Context& context) { - PrintBasicContext(&GetErrorStream(), context); - return true; -} - -void CSVReporter::ReportRuns(const std::vector & reports) { - std::ostream& Out = GetOutputStream(); - - if (!printed_header_) { - // save the names of all the user counters - for (const auto& run : reports) { - for (const auto& cnt : run.counters) { - user_counter_names_.insert(cnt.first); - } - } - - // print the header - for (auto B = elements.begin(); B != elements.end();) { - Out << *B++; - if (B != elements.end()) Out << ","; - } - for (auto B = user_counter_names_.begin(); B != user_counter_names_.end();) { - Out << ",\"" << *B++ << "\""; - } - Out << "\n"; - - printed_header_ = true; - } else { - // check that all the current counters are saved in the name set - for (const auto& run : reports) { - for (const auto& cnt : run.counters) { - CHECK(user_counter_names_.find(cnt.first) != user_counter_names_.end()) - << "All counters must be present in each run. " - << "Counter named \"" << cnt.first - << "\" was not in a run after being added to the header"; - } - } - } - - // print results for each run - for (const auto& run : reports) { - PrintRunData(run); - } - -} - -void CSVReporter::PrintRunData(const Run & run) { - std::ostream& Out = GetOutputStream(); - - // Field with embedded double-quote characters must be doubled and the field - // delimited with double-quotes. - std::string name = run.benchmark_name; - ReplaceAll(&name, "\"", "\"\""); - Out << '"' << name << "\","; - if (run.error_occurred) { - Out << std::string(elements.size() - 3, ','); - Out << "true,"; - std::string msg = run.error_message; - ReplaceAll(&msg, "\"", "\"\""); - Out << '"' << msg << "\"\n"; - return; - } - - // Do not print iteration on bigO and RMS report - if (!run.report_big_o && !run.report_rms) { - Out << run.iterations; - } - Out << ","; - - Out << run.GetAdjustedRealTime() << ","; - Out << run.GetAdjustedCPUTime() << ","; - - // Do not print timeLabel on bigO and RMS report - if (run.report_big_o) { - Out << GetBigOString(run.complexity); - } else if (!run.report_rms) { - Out << GetTimeUnitString(run.time_unit); - } - Out << ","; - - if (run.bytes_per_second > 0.0) { - Out << run.bytes_per_second; - } - Out << ","; - if (run.items_per_second > 0.0) { - Out << run.items_per_second; - } - Out << ","; - if (!run.report_label.empty()) { - // Field with embedded double-quote characters must be doubled and the field - // delimited with double-quotes. - std::string label = run.report_label; - ReplaceAll(&label, "\"", "\"\""); - Out << "\"" << label << "\""; - } - Out << ",,"; // for error_occurred and error_message - - // Print user counters - for (const auto &ucn : user_counter_names_) { - auto it = run.counters.find(ucn); - if(it == run.counters.end()) { - Out << ","; - } else { - Out << "," << it->second; - } - } - Out << '\n'; -} - -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/cycleclock.h b/benchmarks/thirdparty/benchmark/src/cycleclock.h deleted file mode 100755 index 3b376ac57d..0000000000 --- a/benchmarks/thirdparty/benchmark/src/cycleclock.h +++ /dev/null @@ -1,177 +0,0 @@ -// ---------------------------------------------------------------------- -// CycleClock -// A CycleClock tells you the current time in Cycles. The "time" -// is actually time since power-on. This is like time() but doesn't -// involve a system call and is much more precise. -// -// NOTE: Not all cpu/platform/kernel combinations guarantee that this -// clock increments at a constant rate or is synchronized across all logical -// cpus in a system. -// -// If you need the above guarantees, please consider using a different -// API. There are efforts to provide an interface which provides a millisecond -// granularity and implemented as a memory read. A memory read is generally -// cheaper than the CycleClock for many architectures. -// -// Also, in some out of order CPU implementations, the CycleClock is not -// serializing. So if you're trying to count at cycles granularity, your -// data might be inaccurate due to out of order instruction execution. -// ---------------------------------------------------------------------- - -#ifndef BENCHMARK_CYCLECLOCK_H_ -#define BENCHMARK_CYCLECLOCK_H_ - -#include - -#include "benchmark/benchmark.h" -#include "internal_macros.h" - -#if defined(BENCHMARK_OS_MACOSX) -#include -#endif -// For MSVC, we want to use '_asm rdtsc' when possible (since it works -// with even ancient MSVC compilers), and when not possible the -// __rdtsc intrinsic, declared in . Unfortunately, in some -// environments, and have conflicting -// declarations of some other intrinsics, breaking compilation. -// Therefore, we simply declare __rdtsc ourselves. See also -// http://connect.microsoft.com/VisualStudio/feedback/details/262047 -#if defined(COMPILER_MSVC) && !defined(_M_IX86) -extern "C" uint64_t __rdtsc(); -#pragma intrinsic(__rdtsc) -#endif - -#ifndef BENCHMARK_OS_WINDOWS -#include -#include -#endif - -#ifdef BENCHMARK_OS_EMSCRIPTEN -#include -#endif - -namespace benchmark { -// NOTE: only i386 and x86_64 have been well tested. -// PPC, sparc, alpha, and ia64 are based on -// http://peter.kuscsik.com/wordpress/?p=14 -// with modifications by m3b. See also -// https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h -namespace cycleclock { -// This should return the number of cycles since power-on. Thread-safe. -inline BENCHMARK_ALWAYS_INLINE int64_t Now() { -#if defined(BENCHMARK_OS_MACOSX) - // this goes at the top because we need ALL Macs, regardless of - // architecture, to return the number of "mach time units" that - // have passed since startup. See sysinfo.cc where - // InitializeSystemInfo() sets the supposed cpu clock frequency of - // macs to the number of mach time units per second, not actual - // CPU clock frequency (which can change in the face of CPU - // frequency scaling). Also note that when the Mac sleeps, this - // counter pauses; it does not continue counting, nor does it - // reset to zero. - return mach_absolute_time(); -#elif defined(BENCHMARK_OS_EMSCRIPTEN) - // this goes above x86-specific code because old versions of Emscripten - // define __x86_64__, although they have nothing to do with it. - return static_cast(emscripten_get_now() * 1e+6); -#elif defined(__i386__) - int64_t ret; - __asm__ volatile("rdtsc" : "=A"(ret)); - return ret; -#elif defined(__x86_64__) || defined(__amd64__) - uint64_t low, high; - __asm__ volatile("rdtsc" : "=a"(low), "=d"(high)); - return (high << 32) | low; -#elif defined(__powerpc__) || defined(__ppc__) - // This returns a time-base, which is not always precisely a cycle-count. - int64_t tbl, tbu0, tbu1; - asm("mftbu %0" : "=r"(tbu0)); - asm("mftb %0" : "=r"(tbl)); - asm("mftbu %0" : "=r"(tbu1)); - tbl &= -static_cast(tbu0 == tbu1); - // high 32 bits in tbu1; low 32 bits in tbl (tbu0 is garbage) - return (tbu1 << 32) | tbl; -#elif defined(__sparc__) - int64_t tick; - asm(".byte 0x83, 0x41, 0x00, 0x00"); - asm("mov %%g1, %0" : "=r"(tick)); - return tick; -#elif defined(__ia64__) - int64_t itc; - asm("mov %0 = ar.itc" : "=r"(itc)); - return itc; -#elif defined(COMPILER_MSVC) && defined(_M_IX86) - // Older MSVC compilers (like 7.x) don't seem to support the - // __rdtsc intrinsic properly, so I prefer to use _asm instead - // when I know it will work. Otherwise, I'll use __rdtsc and hope - // the code is being compiled with a non-ancient compiler. - _asm rdtsc -#elif defined(COMPILER_MSVC) - return __rdtsc(); -#elif defined(BENCHMARK_OS_NACL) - // Native Client validator on x86/x86-64 allows RDTSC instructions, - // and this case is handled above. Native Client validator on ARM - // rejects MRC instructions (used in the ARM-specific sequence below), - // so we handle it here. Portable Native Client compiles to - // architecture-agnostic bytecode, which doesn't provide any - // cycle counter access mnemonics. - - // Native Client does not provide any API to access cycle counter. - // Use clock_gettime(CLOCK_MONOTONIC, ...) instead of gettimeofday - // because is provides nanosecond resolution (which is noticable at - // least for PNaCl modules running on x86 Mac & Linux). - // Initialize to always return 0 if clock_gettime fails. - struct timespec ts = { 0, 0 }; - clock_gettime(CLOCK_MONOTONIC, &ts); - return static_cast(ts.tv_sec) * 1000000000 + ts.tv_nsec; -#elif defined(__aarch64__) - // System timer of ARMv8 runs at a different frequency than the CPU's. - // The frequency is fixed, typically in the range 1-50MHz. It can be - // read at CNTFRQ special register. We assume the OS has set up - // the virtual timer properly. - int64_t virtual_timer_value; - asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value)); - return virtual_timer_value; -#elif defined(__ARM_ARCH) - // V6 is the earliest arch that has a standard cyclecount - // Native Client validator doesn't allow MRC instructions. -#if (__ARM_ARCH >= 6) - uint32_t pmccntr; - uint32_t pmuseren; - uint32_t pmcntenset; - // Read the user mode perf monitor counter access permissions. - asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r"(pmuseren)); - if (pmuseren & 1) { // Allows reading perfmon counters for user mode code. - asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r"(pmcntenset)); - if (pmcntenset & 0x80000000ul) { // Is it counting? - asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(pmccntr)); - // The counter is set up to count every 64th cycle - return static_cast(pmccntr) * 64; // Should optimize to << 6 - } - } -#endif - struct timeval tv; - gettimeofday(&tv, nullptr); - return static_cast(tv.tv_sec) * 1000000 + tv.tv_usec; -#elif defined(__mips__) - // mips apparently only allows rdtsc for superusers, so we fall - // back to gettimeofday. It's possible clock_gettime would be better. - struct timeval tv; - gettimeofday(&tv, nullptr); - return static_cast(tv.tv_sec) * 1000000 + tv.tv_usec; -#elif defined(__s390__) // Covers both s390 and s390x. - // Return the CPU clock. - uint64_t tsc; - asm("stck %0" : "=Q" (tsc) : : "cc"); - return tsc; -#else -// The soft failover to a generic implementation is automatic only for ARM. -// For other platforms the developer is expected to make an attempt to create -// a fast implementation and use generic version if nothing better is available. -#error You need to define CycleTimer for your OS and CPU -#endif -} -} // end namespace cycleclock -} // end namespace benchmark - -#endif // BENCHMARK_CYCLECLOCK_H_ diff --git a/benchmarks/thirdparty/benchmark/src/internal_macros.h b/benchmarks/thirdparty/benchmark/src/internal_macros.h deleted file mode 100755 index edb8a5c0a3..0000000000 --- a/benchmarks/thirdparty/benchmark/src/internal_macros.h +++ /dev/null @@ -1,89 +0,0 @@ -#ifndef BENCHMARK_INTERNAL_MACROS_H_ -#define BENCHMARK_INTERNAL_MACROS_H_ - -#include "benchmark/benchmark.h" - -#ifndef __has_feature -#define __has_feature(x) 0 -#endif -#ifndef __has_builtin -#define __has_builtin(x) 0 -#endif - -#if defined(__clang__) - #if !defined(COMPILER_CLANG) - #define COMPILER_CLANG - #endif -#elif defined(_MSC_VER) - #if !defined(COMPILER_MSVC) - #define COMPILER_MSVC - #endif -#elif defined(__GNUC__) - #if !defined(COMPILER_GCC) - #define COMPILER_GCC - #endif -#endif - -#if __has_feature(cxx_attributes) - #define BENCHMARK_NORETURN [[noreturn]] -#elif defined(__GNUC__) - #define BENCHMARK_NORETURN __attribute__((noreturn)) -#elif defined(COMPILER_MSVC) - #define BENCHMARK_NORETURN __declspec(noreturn) -#else - #define BENCHMARK_NORETURN -#endif - -#if defined(__CYGWIN__) - #define BENCHMARK_OS_CYGWIN 1 -#elif defined(_WIN32) - #define BENCHMARK_OS_WINDOWS 1 -#elif defined(__APPLE__) - #define BENCHMARK_OS_APPLE 1 - #include "TargetConditionals.h" - #if defined(TARGET_OS_MAC) - #define BENCHMARK_OS_MACOSX 1 - #if defined(TARGET_OS_IPHONE) - #define BENCHMARK_OS_IOS 1 - #endif - #endif -#elif defined(__FreeBSD__) - #define BENCHMARK_OS_FREEBSD 1 -#elif defined(__NetBSD__) - #define BENCHMARK_OS_NETBSD 1 -#elif defined(__OpenBSD__) - #define BENCHMARK_OS_OPENBSD 1 -#elif defined(__linux__) - #define BENCHMARK_OS_LINUX 1 -#elif defined(__native_client__) - #define BENCHMARK_OS_NACL 1 -#elif defined(__EMSCRIPTEN__) - #define BENCHMARK_OS_EMSCRIPTEN 1 -#elif defined(__rtems__) - #define BENCHMARK_OS_RTEMS 1 -#elif defined(__Fuchsia__) -#define BENCHMARK_OS_FUCHSIA 1 -#elif defined (__SVR4) && defined (__sun) -#define BENCHMARK_OS_SOLARIS 1 -#endif - -#if !__has_feature(cxx_exceptions) && !defined(__cpp_exceptions) \ - && !defined(__EXCEPTIONS) - #define BENCHMARK_HAS_NO_EXCEPTIONS -#endif - -#if defined(COMPILER_CLANG) || defined(COMPILER_GCC) - #define BENCHMARK_MAYBE_UNUSED __attribute__((unused)) -#else - #define BENCHMARK_MAYBE_UNUSED -#endif - -#if defined(COMPILER_GCC) || __has_builtin(__builtin_unreachable) - #define BENCHMARK_UNREACHABLE() __builtin_unreachable() -#elif defined(COMPILER_MSVC) - #define BENCHMARK_UNREACHABLE() __assume(false) -#else - #define BENCHMARK_UNREACHABLE() ((void)0) -#endif - -#endif // BENCHMARK_INTERNAL_MACROS_H_ diff --git a/benchmarks/thirdparty/benchmark/src/json_reporter.cc b/benchmarks/thirdparty/benchmark/src/json_reporter.cc deleted file mode 100755 index 685d6b097d..0000000000 --- a/benchmarks/thirdparty/benchmark/src/json_reporter.cc +++ /dev/null @@ -1,205 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "benchmark/benchmark.h" -#include "complexity.h" - -#include -#include -#include -#include -#include -#include -#include // for setprecision -#include - -#include "string_util.h" -#include "timers.h" - -namespace benchmark { - -namespace { - -std::string FormatKV(std::string const& key, std::string const& value) { - return StrFormat("\"%s\": \"%s\"", key.c_str(), value.c_str()); -} - -std::string FormatKV(std::string const& key, const char* value) { - return StrFormat("\"%s\": \"%s\"", key.c_str(), value); -} - -std::string FormatKV(std::string const& key, bool value) { - return StrFormat("\"%s\": %s", key.c_str(), value ? "true" : "false"); -} - -std::string FormatKV(std::string const& key, int64_t value) { - std::stringstream ss; - ss << '"' << key << "\": " << value; - return ss.str(); -} - -std::string FormatKV(std::string const& key, double value) { - std::stringstream ss; - ss << '"' << key << "\": "; - - const auto max_digits10 = std::numeric_limits::max_digits10; - const auto max_fractional_digits10 = max_digits10 - 1; - - ss << std::scientific << std::setprecision(max_fractional_digits10) << value; - return ss.str(); -} - -int64_t RoundDouble(double v) { return static_cast(v + 0.5); } - -} // end namespace - -bool JSONReporter::ReportContext(const Context& context) { - std::ostream& out = GetOutputStream(); - - out << "{\n"; - std::string inner_indent(2, ' '); - - // Open context block and print context information. - out << inner_indent << "\"context\": {\n"; - std::string indent(4, ' '); - - std::string walltime_value = LocalDateTimeString(); - out << indent << FormatKV("date", walltime_value) << ",\n"; - - if (Context::executable_name) { - out << indent << FormatKV("executable", Context::executable_name) << ",\n"; - } - - CPUInfo const& info = context.cpu_info; - out << indent << FormatKV("num_cpus", static_cast(info.num_cpus)) - << ",\n"; - out << indent - << FormatKV("mhz_per_cpu", - RoundDouble(info.cycles_per_second / 1000000.0)) - << ",\n"; - out << indent << FormatKV("cpu_scaling_enabled", info.scaling_enabled) - << ",\n"; - - out << indent << "\"caches\": [\n"; - indent = std::string(6, ' '); - std::string cache_indent(8, ' '); - for (size_t i = 0; i < info.caches.size(); ++i) { - auto& CI = info.caches[i]; - out << indent << "{\n"; - out << cache_indent << FormatKV("type", CI.type) << ",\n"; - out << cache_indent << FormatKV("level", static_cast(CI.level)) - << ",\n"; - out << cache_indent - << FormatKV("size", static_cast(CI.size) * 1000u) << ",\n"; - out << cache_indent - << FormatKV("num_sharing", static_cast(CI.num_sharing)) - << "\n"; - out << indent << "}"; - if (i != info.caches.size() - 1) out << ","; - out << "\n"; - } - indent = std::string(4, ' '); - out << indent << "],\n"; - -#if defined(NDEBUG) - const char build_type[] = "release"; -#else - const char build_type[] = "debug"; -#endif - out << indent << FormatKV("library_build_type", build_type) << "\n"; - // Close context block and open the list of benchmarks. - out << inner_indent << "},\n"; - out << inner_indent << "\"benchmarks\": [\n"; - return true; -} - -void JSONReporter::ReportRuns(std::vector const& reports) { - if (reports.empty()) { - return; - } - std::string indent(4, ' '); - std::ostream& out = GetOutputStream(); - if (!first_report_) { - out << ",\n"; - } - first_report_ = false; - - for (auto it = reports.begin(); it != reports.end(); ++it) { - out << indent << "{\n"; - PrintRunData(*it); - out << indent << '}'; - auto it_cp = it; - if (++it_cp != reports.end()) { - out << ",\n"; - } - } -} - -void JSONReporter::Finalize() { - // Close the list of benchmarks and the top level object. - GetOutputStream() << "\n ]\n}\n"; -} - -void JSONReporter::PrintRunData(Run const& run) { - std::string indent(6, ' '); - std::ostream& out = GetOutputStream(); - out << indent << FormatKV("name", run.benchmark_name) << ",\n"; - if (run.error_occurred) { - out << indent << FormatKV("error_occurred", run.error_occurred) << ",\n"; - out << indent << FormatKV("error_message", run.error_message) << ",\n"; - } - if (!run.report_big_o && !run.report_rms) { - out << indent << FormatKV("iterations", run.iterations) << ",\n"; - out << indent - << FormatKV("real_time", run.GetAdjustedRealTime()) - << ",\n"; - out << indent - << FormatKV("cpu_time", run.GetAdjustedCPUTime()); - out << ",\n" - << indent << FormatKV("time_unit", GetTimeUnitString(run.time_unit)); - } else if (run.report_big_o) { - out << indent - << FormatKV("cpu_coefficient", run.GetAdjustedCPUTime()) - << ",\n"; - out << indent - << FormatKV("real_coefficient", run.GetAdjustedRealTime()) - << ",\n"; - out << indent << FormatKV("big_o", GetBigOString(run.complexity)) << ",\n"; - out << indent << FormatKV("time_unit", GetTimeUnitString(run.time_unit)); - } else if (run.report_rms) { - out << indent - << FormatKV("rms", run.GetAdjustedCPUTime()); - } - if (run.bytes_per_second > 0.0) { - out << ",\n" - << indent - << FormatKV("bytes_per_second", run.bytes_per_second); - } - if (run.items_per_second > 0.0) { - out << ",\n" - << indent - << FormatKV("items_per_second", run.items_per_second); - } - for(auto &c : run.counters) { - out << ",\n" - << indent - << FormatKV(c.first, c.second); - } - if (!run.report_label.empty()) { - out << ",\n" << indent << FormatKV("label", run.report_label); - } - out << '\n'; -} - -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/log.h b/benchmarks/thirdparty/benchmark/src/log.h deleted file mode 100755 index d06e1031db..0000000000 --- a/benchmarks/thirdparty/benchmark/src/log.h +++ /dev/null @@ -1,73 +0,0 @@ -#ifndef BENCHMARK_LOG_H_ -#define BENCHMARK_LOG_H_ - -#include -#include - -#include "benchmark/benchmark.h" - -namespace benchmark { -namespace internal { - -typedef std::basic_ostream&(EndLType)(std::basic_ostream&); - -class LogType { - friend LogType& GetNullLogInstance(); - friend LogType& GetErrorLogInstance(); - - // FIXME: Add locking to output. - template - friend LogType& operator<<(LogType&, Tp const&); - friend LogType& operator<<(LogType&, EndLType*); - - private: - LogType(std::ostream* out) : out_(out) {} - std::ostream* out_; - BENCHMARK_DISALLOW_COPY_AND_ASSIGN(LogType); -}; - -template -LogType& operator<<(LogType& log, Tp const& value) { - if (log.out_) { - *log.out_ << value; - } - return log; -} - -inline LogType& operator<<(LogType& log, EndLType* m) { - if (log.out_) { - *log.out_ << m; - } - return log; -} - -inline int& LogLevel() { - static int log_level = 0; - return log_level; -} - -inline LogType& GetNullLogInstance() { - static LogType log(nullptr); - return log; -} - -inline LogType& GetErrorLogInstance() { - static LogType log(&std::clog); - return log; -} - -inline LogType& GetLogInstanceForLevel(int level) { - if (level <= LogLevel()) { - return GetErrorLogInstance(); - } - return GetNullLogInstance(); -} - -} // end namespace internal -} // end namespace benchmark - -#define VLOG(x) \ - (::benchmark::internal::GetLogInstanceForLevel(x) << "-- LOG(" << x << "):" \ - " ") - -#endif diff --git a/benchmarks/thirdparty/benchmark/src/mutex.h b/benchmarks/thirdparty/benchmark/src/mutex.h deleted file mode 100755 index 5f461d05a0..0000000000 --- a/benchmarks/thirdparty/benchmark/src/mutex.h +++ /dev/null @@ -1,155 +0,0 @@ -#ifndef BENCHMARK_MUTEX_H_ -#define BENCHMARK_MUTEX_H_ - -#include -#include - -#include "check.h" - -// Enable thread safety attributes only with clang. -// The attributes can be safely erased when compiling with other compilers. -#if defined(HAVE_THREAD_SAFETY_ATTRIBUTES) -#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) -#else -#define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op -#endif - -#define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x)) - -#define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) - -#define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) - -#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) - -#define ACQUIRED_BEFORE(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) - -#define ACQUIRED_AFTER(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) - -#define REQUIRES(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) - -#define REQUIRES_SHARED(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) - -#define ACQUIRE(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) - -#define ACQUIRE_SHARED(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) - -#define RELEASE(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__)) - -#define RELEASE_SHARED(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) - -#define TRY_ACQUIRE(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) - -#define TRY_ACQUIRE_SHARED(...) \ - THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) - -#define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) - -#define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x)) - -#define ASSERT_SHARED_CAPABILITY(x) \ - THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x)) - -#define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) - -#define NO_THREAD_SAFETY_ANALYSIS \ - THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) - -namespace benchmark { - -typedef std::condition_variable Condition; - -// NOTE: Wrappers for std::mutex and std::unique_lock are provided so that -// we can annotate them with thread safety attributes and use the -// -Wthread-safety warning with clang. The standard library types cannot be -// used directly because they do not provided the required annotations. -class CAPABILITY("mutex") Mutex { - public: - Mutex() {} - - void lock() ACQUIRE() { mut_.lock(); } - void unlock() RELEASE() { mut_.unlock(); } - std::mutex& native_handle() { return mut_; } - - private: - std::mutex mut_; -}; - -class SCOPED_CAPABILITY MutexLock { - typedef std::unique_lock MutexLockImp; - - public: - MutexLock(Mutex& m) ACQUIRE(m) : ml_(m.native_handle()) {} - ~MutexLock() RELEASE() {} - MutexLockImp& native_handle() { return ml_; } - - private: - MutexLockImp ml_; -}; - -class Barrier { - public: - Barrier(int num_threads) : running_threads_(num_threads) {} - - // Called by each thread - bool wait() EXCLUDES(lock_) { - bool last_thread = false; - { - MutexLock ml(lock_); - last_thread = createBarrier(ml); - } - if (last_thread) phase_condition_.notify_all(); - return last_thread; - } - - void removeThread() EXCLUDES(lock_) { - MutexLock ml(lock_); - --running_threads_; - if (entered_ != 0) phase_condition_.notify_all(); - } - - private: - Mutex lock_; - Condition phase_condition_; - int running_threads_; - - // State for barrier management - int phase_number_ = 0; - int entered_ = 0; // Number of threads that have entered this barrier - - // Enter the barrier and wait until all other threads have also - // entered the barrier. Returns iff this is the last thread to - // enter the barrier. - bool createBarrier(MutexLock& ml) REQUIRES(lock_) { - CHECK_LT(entered_, running_threads_); - entered_++; - if (entered_ < running_threads_) { - // Wait for all threads to enter - int phase_number_cp = phase_number_; - auto cb = [this, phase_number_cp]() { - return this->phase_number_ > phase_number_cp || - entered_ == running_threads_; // A thread has aborted in error - }; - phase_condition_.wait(ml.native_handle(), cb); - if (phase_number_ > phase_number_cp) return false; - // else (running_threads_ == entered_) and we are the last thread. - } - // Last thread has reached the barrier - phase_number_++; - entered_ = 0; - return true; - } -}; - -} // end namespace benchmark - -#endif // BENCHMARK_MUTEX_H_ diff --git a/benchmarks/thirdparty/benchmark/src/re.h b/benchmarks/thirdparty/benchmark/src/re.h deleted file mode 100755 index 924d2f0ba7..0000000000 --- a/benchmarks/thirdparty/benchmark/src/re.h +++ /dev/null @@ -1,152 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef BENCHMARK_RE_H_ -#define BENCHMARK_RE_H_ - -#include "internal_macros.h" - -#if !defined(HAVE_STD_REGEX) && \ - !defined(HAVE_GNU_POSIX_REGEX) && \ - !defined(HAVE_POSIX_REGEX) - // No explicit regex selection; detect based on builtin hints. - #if defined(BENCHMARK_OS_LINUX) || defined(BENCHMARK_OS_APPLE) - #define HAVE_POSIX_REGEX 1 - #elif __cplusplus >= 199711L - #define HAVE_STD_REGEX 1 - #endif -#endif - -// Prefer C regex libraries when compiling w/o exceptions so that we can -// correctly report errors. -#if defined(BENCHMARK_HAS_NO_EXCEPTIONS) && \ - defined(BENCHMARK_HAVE_STD_REGEX) && \ - (defined(HAVE_GNU_POSIX_REGEX) || defined(HAVE_POSIX_REGEX)) - #undef HAVE_STD_REGEX -#endif - -#if defined(HAVE_STD_REGEX) - #include -#elif defined(HAVE_GNU_POSIX_REGEX) - #include -#elif defined(HAVE_POSIX_REGEX) - #include -#else -#error No regular expression backend was found! -#endif -#include - -#include "check.h" - -namespace benchmark { - -// A wrapper around the POSIX regular expression API that provides automatic -// cleanup -class Regex { - public: - Regex() : init_(false) {} - - ~Regex(); - - // Compile a regular expression matcher from spec. Returns true on success. - // - // On failure (and if error is not nullptr), error is populated with a human - // readable error message if an error occurs. - bool Init(const std::string& spec, std::string* error); - - // Returns whether str matches the compiled regular expression. - bool Match(const std::string& str); - - private: - bool init_; -// Underlying regular expression object -#if defined(HAVE_STD_REGEX) - std::regex re_; -#elif defined(HAVE_POSIX_REGEX) || defined(HAVE_GNU_POSIX_REGEX) - regex_t re_; -#else - #error No regular expression backend implementation available -#endif -}; - -#if defined(HAVE_STD_REGEX) - -inline bool Regex::Init(const std::string& spec, std::string* error) { -#ifdef BENCHMARK_HAS_NO_EXCEPTIONS - ((void)error); // suppress unused warning -#else - try { -#endif - re_ = std::regex(spec, std::regex_constants::extended); - init_ = true; -#ifndef BENCHMARK_HAS_NO_EXCEPTIONS - } catch (const std::regex_error& e) { - if (error) { - *error = e.what(); - } - } -#endif - return init_; -} - -inline Regex::~Regex() {} - -inline bool Regex::Match(const std::string& str) { - if (!init_) { - return false; - } - return std::regex_search(str, re_); -} - -#else -inline bool Regex::Init(const std::string& spec, std::string* error) { - int ec = regcomp(&re_, spec.c_str(), REG_EXTENDED | REG_NOSUB); - if (ec != 0) { - if (error) { - size_t needed = regerror(ec, &re_, nullptr, 0); - char* errbuf = new char[needed]; - regerror(ec, &re_, errbuf, needed); - - // regerror returns the number of bytes necessary to null terminate - // the string, so we move that when assigning to error. - CHECK_NE(needed, 0); - error->assign(errbuf, needed - 1); - - delete[] errbuf; - } - - return false; - } - - init_ = true; - return true; -} - -inline Regex::~Regex() { - if (init_) { - regfree(&re_); - } -} - -inline bool Regex::Match(const std::string& str) { - if (!init_) { - return false; - } - return regexec(&re_, str.c_str(), 0, nullptr, 0) == 0; -} -#endif - -} // end namespace benchmark - -#endif // BENCHMARK_RE_H_ diff --git a/benchmarks/thirdparty/benchmark/src/reporter.cc b/benchmarks/thirdparty/benchmark/src/reporter.cc deleted file mode 100755 index 4b40aaec8b..0000000000 --- a/benchmarks/thirdparty/benchmark/src/reporter.cc +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "benchmark/benchmark.h" -#include "timers.h" - -#include - -#include -#include -#include - -#include "check.h" - -namespace benchmark { - -BenchmarkReporter::BenchmarkReporter() - : output_stream_(&std::cout), error_stream_(&std::cerr) {} - -BenchmarkReporter::~BenchmarkReporter() {} - -void BenchmarkReporter::PrintBasicContext(std::ostream *out, - Context const &context) { - CHECK(out) << "cannot be null"; - auto &Out = *out; - - Out << LocalDateTimeString() << "\n"; - - if (context.executable_name) - Out << "Running " << context.executable_name << "\n"; - - const CPUInfo &info = context.cpu_info; - Out << "Run on (" << info.num_cpus << " X " - << (info.cycles_per_second / 1000000.0) << " MHz CPU " - << ((info.num_cpus > 1) ? "s" : "") << ")\n"; - if (info.caches.size() != 0) { - Out << "CPU Caches:\n"; - for (auto &CInfo : info.caches) { - Out << " L" << CInfo.level << " " << CInfo.type << " " - << (CInfo.size / 1000) << "K"; - if (CInfo.num_sharing != 0) - Out << " (x" << (info.num_cpus / CInfo.num_sharing) << ")"; - Out << "\n"; - } - } - - if (info.scaling_enabled) { - Out << "***WARNING*** CPU scaling is enabled, the benchmark " - "real time measurements may be noisy and will incur extra " - "overhead.\n"; - } - -#ifndef NDEBUG - Out << "***WARNING*** Library was built as DEBUG. Timings may be " - "affected.\n"; -#endif -} - -// No initializer because it's already initialized to NULL. -const char* BenchmarkReporter::Context::executable_name; - -BenchmarkReporter::Context::Context() : cpu_info(CPUInfo::Get()) {} - -double BenchmarkReporter::Run::GetAdjustedRealTime() const { - double new_time = real_accumulated_time * GetTimeUnitMultiplier(time_unit); - if (iterations != 0) new_time /= static_cast(iterations); - return new_time; -} - -double BenchmarkReporter::Run::GetAdjustedCPUTime() const { - double new_time = cpu_accumulated_time * GetTimeUnitMultiplier(time_unit); - if (iterations != 0) new_time /= static_cast(iterations); - return new_time; -} - -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/sleep.cc b/benchmarks/thirdparty/benchmark/src/sleep.cc deleted file mode 100755 index 54aa04a422..0000000000 --- a/benchmarks/thirdparty/benchmark/src/sleep.cc +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "sleep.h" - -#include -#include -#include - -#include "internal_macros.h" - -#ifdef BENCHMARK_OS_WINDOWS -#include -#endif - -namespace benchmark { -#ifdef BENCHMARK_OS_WINDOWS -// Window's Sleep takes milliseconds argument. -void SleepForMilliseconds(int milliseconds) { Sleep(milliseconds); } -void SleepForSeconds(double seconds) { - SleepForMilliseconds(static_cast(kNumMillisPerSecond * seconds)); -} -#else // BENCHMARK_OS_WINDOWS -void SleepForMicroseconds(int microseconds) { - struct timespec sleep_time; - sleep_time.tv_sec = microseconds / kNumMicrosPerSecond; - sleep_time.tv_nsec = (microseconds % kNumMicrosPerSecond) * kNumNanosPerMicro; - while (nanosleep(&sleep_time, &sleep_time) != 0 && errno == EINTR) - ; // Ignore signals and wait for the full interval to elapse. -} - -void SleepForMilliseconds(int milliseconds) { - SleepForMicroseconds(milliseconds * kNumMicrosPerMilli); -} - -void SleepForSeconds(double seconds) { - SleepForMicroseconds(static_cast(seconds * kNumMicrosPerSecond)); -} -#endif // BENCHMARK_OS_WINDOWS -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/sleep.h b/benchmarks/thirdparty/benchmark/src/sleep.h deleted file mode 100755 index f98551afe2..0000000000 --- a/benchmarks/thirdparty/benchmark/src/sleep.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef BENCHMARK_SLEEP_H_ -#define BENCHMARK_SLEEP_H_ - -namespace benchmark { -const int kNumMillisPerSecond = 1000; -const int kNumMicrosPerMilli = 1000; -const int kNumMicrosPerSecond = kNumMillisPerSecond * 1000; -const int kNumNanosPerMicro = 1000; -const int kNumNanosPerSecond = kNumNanosPerMicro * kNumMicrosPerSecond; - -void SleepForMilliseconds(int milliseconds); -void SleepForSeconds(double seconds); -} // end namespace benchmark - -#endif // BENCHMARK_SLEEP_H_ diff --git a/benchmarks/thirdparty/benchmark/src/statistics.cc b/benchmarks/thirdparty/benchmark/src/statistics.cc deleted file mode 100755 index 1c91e1015a..0000000000 --- a/benchmarks/thirdparty/benchmark/src/statistics.cc +++ /dev/null @@ -1,178 +0,0 @@ -// Copyright 2016 Ismael Jimenez Martinez. All rights reserved. -// Copyright 2017 Roman Lebedev. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "benchmark/benchmark.h" - -#include -#include -#include -#include -#include -#include "check.h" -#include "statistics.h" - -namespace benchmark { - -auto StatisticsSum = [](const std::vector& v) { - return std::accumulate(v.begin(), v.end(), 0.0); -}; - -double StatisticsMean(const std::vector& v) { - if (v.empty()) return 0.0; - return StatisticsSum(v) * (1.0 / v.size()); -} - -double StatisticsMedian(const std::vector& v) { - if (v.size() < 3) return StatisticsMean(v); - std::vector copy(v); - - auto center = copy.begin() + v.size() / 2; - std::nth_element(copy.begin(), center, copy.end()); - - // did we have an odd number of samples? - // if yes, then center is the median - // it no, then we are looking for the average between center and the value before - if(v.size() % 2 == 1) - return *center; - auto center2 = copy.begin() + v.size() / 2 - 1; - std::nth_element(copy.begin(), center2, copy.end()); - return (*center + *center2) / 2.0; -} - -// Return the sum of the squares of this sample set -auto SumSquares = [](const std::vector& v) { - return std::inner_product(v.begin(), v.end(), v.begin(), 0.0); -}; - -auto Sqr = [](const double dat) { return dat * dat; }; -auto Sqrt = [](const double dat) { - // Avoid NaN due to imprecision in the calculations - if (dat < 0.0) return 0.0; - return std::sqrt(dat); -}; - -double StatisticsStdDev(const std::vector& v) { - const auto mean = StatisticsMean(v); - if (v.empty()) return mean; - - // Sample standard deviation is undefined for n = 1 - if (v.size() == 1) - return 0.0; - - const double avg_squares = SumSquares(v) * (1.0 / v.size()); - return Sqrt(v.size() / (v.size() - 1.0) * (avg_squares - Sqr(mean))); -} - -std::vector ComputeStats( - const std::vector& reports) { - typedef BenchmarkReporter::Run Run; - std::vector results; - - auto error_count = - std::count_if(reports.begin(), reports.end(), - [](Run const& run) { return run.error_occurred; }); - - if (reports.size() - error_count < 2) { - // We don't report aggregated data if there was a single run. - return results; - } - - // Accumulators. - std::vector real_accumulated_time_stat; - std::vector cpu_accumulated_time_stat; - std::vector bytes_per_second_stat; - std::vector items_per_second_stat; - - real_accumulated_time_stat.reserve(reports.size()); - cpu_accumulated_time_stat.reserve(reports.size()); - bytes_per_second_stat.reserve(reports.size()); - items_per_second_stat.reserve(reports.size()); - - // All repetitions should be run with the same number of iterations so we - // can take this information from the first benchmark. - int64_t const run_iterations = reports.front().iterations; - // create stats for user counters - struct CounterStat { - Counter c; - std::vector s; - }; - std::map< std::string, CounterStat > counter_stats; - for(Run const& r : reports) { - for(auto const& cnt : r.counters) { - auto it = counter_stats.find(cnt.first); - if(it == counter_stats.end()) { - counter_stats.insert({cnt.first, {cnt.second, std::vector{}}}); - it = counter_stats.find(cnt.first); - it->second.s.reserve(reports.size()); - } else { - CHECK_EQ(counter_stats[cnt.first].c.flags, cnt.second.flags); - } - } - } - - // Populate the accumulators. - for (Run const& run : reports) { - CHECK_EQ(reports[0].benchmark_name, run.benchmark_name); - CHECK_EQ(run_iterations, run.iterations); - if (run.error_occurred) continue; - real_accumulated_time_stat.emplace_back(run.real_accumulated_time); - cpu_accumulated_time_stat.emplace_back(run.cpu_accumulated_time); - items_per_second_stat.emplace_back(run.items_per_second); - bytes_per_second_stat.emplace_back(run.bytes_per_second); - // user counters - for(auto const& cnt : run.counters) { - auto it = counter_stats.find(cnt.first); - CHECK_NE(it, counter_stats.end()); - it->second.s.emplace_back(cnt.second); - } - } - - // Only add label if it is same for all runs - std::string report_label = reports[0].report_label; - for (std::size_t i = 1; i < reports.size(); i++) { - if (reports[i].report_label != report_label) { - report_label = ""; - break; - } - } - - for(const auto& Stat : *reports[0].statistics) { - // Get the data from the accumulator to BenchmarkReporter::Run's. - Run data; - data.benchmark_name = reports[0].benchmark_name + "_" + Stat.name_; - data.report_label = report_label; - data.iterations = run_iterations; - - data.real_accumulated_time = Stat.compute_(real_accumulated_time_stat); - data.cpu_accumulated_time = Stat.compute_(cpu_accumulated_time_stat); - data.bytes_per_second = Stat.compute_(bytes_per_second_stat); - data.items_per_second = Stat.compute_(items_per_second_stat); - - data.time_unit = reports[0].time_unit; - - // user counters - for(auto const& kv : counter_stats) { - const auto uc_stat = Stat.compute_(kv.second.s); - auto c = Counter(uc_stat, counter_stats[kv.first].c.flags); - data.counters[kv.first] = c; - } - - results.push_back(data); - } - - return results; -} - -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/statistics.h b/benchmarks/thirdparty/benchmark/src/statistics.h deleted file mode 100755 index 7eccc85536..0000000000 --- a/benchmarks/thirdparty/benchmark/src/statistics.h +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2016 Ismael Jimenez Martinez. All rights reserved. -// Copyright 2017 Roman Lebedev. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef STATISTICS_H_ -#define STATISTICS_H_ - -#include - -#include "benchmark/benchmark.h" - -namespace benchmark { - -// Return a vector containing the mean, median and standard devation information -// (and any user-specified info) for the specified list of reports. If 'reports' -// contains less than two non-errored runs an empty vector is returned -std::vector ComputeStats( - const std::vector& reports); - -double StatisticsMean(const std::vector& v); -double StatisticsMedian(const std::vector& v); -double StatisticsStdDev(const std::vector& v); - -} // end namespace benchmark - -#endif // STATISTICS_H_ diff --git a/benchmarks/thirdparty/benchmark/src/string_util.cc b/benchmarks/thirdparty/benchmark/src/string_util.cc deleted file mode 100755 index ebc3acebd2..0000000000 --- a/benchmarks/thirdparty/benchmark/src/string_util.cc +++ /dev/null @@ -1,172 +0,0 @@ -#include "string_util.h" - -#include -#include -#include -#include -#include -#include - -#include "arraysize.h" - -namespace benchmark { -namespace { - -// kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yotta. -const char kBigSIUnits[] = "kMGTPEZY"; -// Kibi, Mebi, Gibi, Tebi, Pebi, Exbi, Zebi, Yobi. -const char kBigIECUnits[] = "KMGTPEZY"; -// milli, micro, nano, pico, femto, atto, zepto, yocto. -const char kSmallSIUnits[] = "munpfazy"; - -// We require that all three arrays have the same size. -static_assert(arraysize(kBigSIUnits) == arraysize(kBigIECUnits), - "SI and IEC unit arrays must be the same size"); -static_assert(arraysize(kSmallSIUnits) == arraysize(kBigSIUnits), - "Small SI and Big SI unit arrays must be the same size"); - -static const int64_t kUnitsSize = arraysize(kBigSIUnits); - -void ToExponentAndMantissa(double val, double thresh, int precision, - double one_k, std::string* mantissa, - int64_t* exponent) { - std::stringstream mantissa_stream; - - if (val < 0) { - mantissa_stream << "-"; - val = -val; - } - - // Adjust threshold so that it never excludes things which can't be rendered - // in 'precision' digits. - const double adjusted_threshold = - std::max(thresh, 1.0 / std::pow(10.0, precision)); - const double big_threshold = adjusted_threshold * one_k; - const double small_threshold = adjusted_threshold; - // Values in ]simple_threshold,small_threshold[ will be printed as-is - const double simple_threshold = 0.01; - - if (val > big_threshold) { - // Positive powers - double scaled = val; - for (size_t i = 0; i < arraysize(kBigSIUnits); ++i) { - scaled /= one_k; - if (scaled <= big_threshold) { - mantissa_stream << scaled; - *exponent = i + 1; - *mantissa = mantissa_stream.str(); - return; - } - } - mantissa_stream << val; - *exponent = 0; - } else if (val < small_threshold) { - // Negative powers - if (val < simple_threshold) { - double scaled = val; - for (size_t i = 0; i < arraysize(kSmallSIUnits); ++i) { - scaled *= one_k; - if (scaled >= small_threshold) { - mantissa_stream << scaled; - *exponent = -static_cast(i + 1); - *mantissa = mantissa_stream.str(); - return; - } - } - } - mantissa_stream << val; - *exponent = 0; - } else { - mantissa_stream << val; - *exponent = 0; - } - *mantissa = mantissa_stream.str(); -} - -std::string ExponentToPrefix(int64_t exponent, bool iec) { - if (exponent == 0) return ""; - - const int64_t index = (exponent > 0 ? exponent - 1 : -exponent - 1); - if (index >= kUnitsSize) return ""; - - const char* array = - (exponent > 0 ? (iec ? kBigIECUnits : kBigSIUnits) : kSmallSIUnits); - if (iec) - return array[index] + std::string("i"); - else - return std::string(1, array[index]); -} - -std::string ToBinaryStringFullySpecified(double value, double threshold, - int precision, double one_k = 1024.0) { - std::string mantissa; - int64_t exponent; - ToExponentAndMantissa(value, threshold, precision, one_k, &mantissa, - &exponent); - return mantissa + ExponentToPrefix(exponent, false); -} - -} // end namespace - -void AppendHumanReadable(int n, std::string* str) { - std::stringstream ss; - // Round down to the nearest SI prefix. - ss << ToBinaryStringFullySpecified(n, 1.0, 0); - *str += ss.str(); -} - -std::string HumanReadableNumber(double n, double one_k) { - // 1.1 means that figures up to 1.1k should be shown with the next unit down; - // this softens edge effects. - // 1 means that we should show one decimal place of precision. - return ToBinaryStringFullySpecified(n, 1.1, 1, one_k); -} - -std::string StrFormatImp(const char* msg, va_list args) { - // we might need a second shot at this, so pre-emptivly make a copy - va_list args_cp; - va_copy(args_cp, args); - - // TODO(ericwf): use std::array for first attempt to avoid one memory - // allocation guess what the size might be - std::array local_buff; - std::size_t size = local_buff.size(); - // 2015-10-08: vsnprintf is used instead of snd::vsnprintf due to a limitation - // in the android-ndk - auto ret = vsnprintf(local_buff.data(), size, msg, args_cp); - - va_end(args_cp); - - // handle empty expansion - if (ret == 0) return std::string{}; - if (static_cast(ret) < size) - return std::string(local_buff.data()); - - // we did not provide a long enough buffer on our first attempt. - // add 1 to size to account for null-byte in size cast to prevent overflow - size = static_cast(ret) + 1; - auto buff_ptr = std::unique_ptr(new char[size]); - // 2015-10-08: vsnprintf is used instead of snd::vsnprintf due to a limitation - // in the android-ndk - ret = vsnprintf(buff_ptr.get(), size, msg, args); - return std::string(buff_ptr.get()); -} - -std::string StrFormat(const char* format, ...) { - va_list args; - va_start(args, format); - std::string tmp = StrFormatImp(format, args); - va_end(args); - return tmp; -} - -void ReplaceAll(std::string* str, const std::string& from, - const std::string& to) { - std::size_t start = 0; - while ((start = str->find(from, start)) != std::string::npos) { - str->replace(start, from.length(), to); - start += to.length(); - } -} - -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/string_util.h b/benchmarks/thirdparty/benchmark/src/string_util.h deleted file mode 100755 index e70e769872..0000000000 --- a/benchmarks/thirdparty/benchmark/src/string_util.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef BENCHMARK_STRING_UTIL_H_ -#define BENCHMARK_STRING_UTIL_H_ - -#include -#include -#include -#include "internal_macros.h" - -namespace benchmark { - -void AppendHumanReadable(int n, std::string* str); - -std::string HumanReadableNumber(double n, double one_k = 1024.0); - -std::string StrFormat(const char* format, ...); - -inline std::ostream& StrCatImp(std::ostream& out) BENCHMARK_NOEXCEPT { - return out; -} - -template -inline std::ostream& StrCatImp(std::ostream& out, First&& f, - Rest&&... rest) { - out << std::forward(f); - return StrCatImp(out, std::forward(rest)...); -} - -template -inline std::string StrCat(Args&&... args) { - std::ostringstream ss; - StrCatImp(ss, std::forward(args)...); - return ss.str(); -} - -void ReplaceAll(std::string* str, const std::string& from, - const std::string& to); - -} // end namespace benchmark - -#endif // BENCHMARK_STRING_UTIL_H_ diff --git a/benchmarks/thirdparty/benchmark/src/sysinfo.cc b/benchmarks/thirdparty/benchmark/src/sysinfo.cc deleted file mode 100755 index d19d0ef4c1..0000000000 --- a/benchmarks/thirdparty/benchmark/src/sysinfo.cc +++ /dev/null @@ -1,587 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "internal_macros.h" - -#ifdef BENCHMARK_OS_WINDOWS -#include -#undef StrCat // Don't let StrCat in string_util.h be renamed to lstrcatA -#include -#include -#else -#include -#ifndef BENCHMARK_OS_FUCHSIA -#include -#endif -#include -#include // this header must be included before 'sys/sysctl.h' to avoid compilation error on FreeBSD -#include -#if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_MACOSX || \ - defined BENCHMARK_OS_NETBSD || defined BENCHMARK_OS_OPENBSD -#define BENCHMARK_HAS_SYSCTL -#include -#endif -#endif -#if defined(BENCHMARK_OS_SOLARIS) -#include -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "check.h" -#include "cycleclock.h" -#include "internal_macros.h" -#include "log.h" -#include "sleep.h" -#include "string_util.h" - -namespace benchmark { -namespace { - -void PrintImp(std::ostream& out) { out << std::endl; } - -template -void PrintImp(std::ostream& out, First&& f, Rest&&... rest) { - out << std::forward(f); - PrintImp(out, std::forward(rest)...); -} - -template -BENCHMARK_NORETURN void PrintErrorAndDie(Args&&... args) { - PrintImp(std::cerr, std::forward(args)...); - std::exit(EXIT_FAILURE); -} - -#ifdef BENCHMARK_HAS_SYSCTL - -/// ValueUnion - A type used to correctly alias the byte-for-byte output of -/// `sysctl` with the result type it's to be interpreted as. -struct ValueUnion { - union DataT { - uint32_t uint32_value; - uint64_t uint64_value; - // For correct aliasing of union members from bytes. - char bytes[8]; - }; - using DataPtr = std::unique_ptr; - - // The size of the data union member + its trailing array size. - size_t Size; - DataPtr Buff; - - public: - ValueUnion() : Size(0), Buff(nullptr, &std::free) {} - - explicit ValueUnion(size_t BuffSize) - : Size(sizeof(DataT) + BuffSize), - Buff(::new (std::malloc(Size)) DataT(), &std::free) {} - - ValueUnion(ValueUnion&& other) = default; - - explicit operator bool() const { return bool(Buff); } - - char* data() const { return Buff->bytes; } - - std::string GetAsString() const { return std::string(data()); } - - int64_t GetAsInteger() const { - if (Size == sizeof(Buff->uint32_value)) - return static_cast(Buff->uint32_value); - else if (Size == sizeof(Buff->uint64_value)) - return static_cast(Buff->uint64_value); - BENCHMARK_UNREACHABLE(); - } - - uint64_t GetAsUnsigned() const { - if (Size == sizeof(Buff->uint32_value)) - return Buff->uint32_value; - else if (Size == sizeof(Buff->uint64_value)) - return Buff->uint64_value; - BENCHMARK_UNREACHABLE(); - } - - template - std::array GetAsArray() { - const int ArrSize = sizeof(T) * N; - CHECK_LE(ArrSize, Size); - std::array Arr; - std::memcpy(Arr.data(), data(), ArrSize); - return Arr; - } -}; - -ValueUnion GetSysctlImp(std::string const& Name) { -#if defined BENCHMARK_OS_OPENBSD - int mib[2]; - - mib[0] = CTL_HW; - if ((Name == "hw.ncpu") || (Name == "hw.cpuspeed")){ - ValueUnion buff(sizeof(int)); - - if (Name == "hw.ncpu") { - mib[1] = HW_NCPU; - } else { - mib[1] = HW_CPUSPEED; - } - - if (sysctl(mib, 2, buff.data(), &buff.Size, nullptr, 0) == -1) { - return ValueUnion(); - } - return buff; - } - return ValueUnion(); -#else - size_t CurBuffSize = 0; - if (sysctlbyname(Name.c_str(), nullptr, &CurBuffSize, nullptr, 0) == -1) - return ValueUnion(); - - ValueUnion buff(CurBuffSize); - if (sysctlbyname(Name.c_str(), buff.data(), &buff.Size, nullptr, 0) == 0) - return buff; - return ValueUnion(); -#endif -} - -BENCHMARK_MAYBE_UNUSED -bool GetSysctl(std::string const& Name, std::string* Out) { - Out->clear(); - auto Buff = GetSysctlImp(Name); - if (!Buff) return false; - Out->assign(Buff.data()); - return true; -} - -template ::value>::type> -bool GetSysctl(std::string const& Name, Tp* Out) { - *Out = 0; - auto Buff = GetSysctlImp(Name); - if (!Buff) return false; - *Out = static_cast(Buff.GetAsUnsigned()); - return true; -} - -template -bool GetSysctl(std::string const& Name, std::array* Out) { - auto Buff = GetSysctlImp(Name); - if (!Buff) return false; - *Out = Buff.GetAsArray(); - return true; -} -#endif - -template -bool ReadFromFile(std::string const& fname, ArgT* arg) { - *arg = ArgT(); - std::ifstream f(fname.c_str()); - if (!f.is_open()) return false; - f >> *arg; - return f.good(); -} - -bool CpuScalingEnabled(int num_cpus) { - // We don't have a valid CPU count, so don't even bother. - if (num_cpus <= 0) return false; -#ifndef BENCHMARK_OS_WINDOWS - // On Linux, the CPUfreq subsystem exposes CPU information as files on the - // local file system. If reading the exported files fails, then we may not be - // running on Linux, so we silently ignore all the read errors. - std::string res; - for (int cpu = 0; cpu < num_cpus; ++cpu) { - std::string governor_file = - StrCat("/sys/devices/system/cpu/cpu", cpu, "/cpufreq/scaling_governor"); - if (ReadFromFile(governor_file, &res) && res != "performance") return true; - } -#endif - return false; -} - -int CountSetBitsInCPUMap(std::string Val) { - auto CountBits = [](std::string Part) { - using CPUMask = std::bitset; - Part = "0x" + Part; - CPUMask Mask(std::stoul(Part, nullptr, 16)); - return static_cast(Mask.count()); - }; - size_t Pos; - int total = 0; - while ((Pos = Val.find(',')) != std::string::npos) { - total += CountBits(Val.substr(0, Pos)); - Val = Val.substr(Pos + 1); - } - if (!Val.empty()) { - total += CountBits(Val); - } - return total; -} - -BENCHMARK_MAYBE_UNUSED -std::vector GetCacheSizesFromKVFS() { - std::vector res; - std::string dir = "/sys/devices/system/cpu/cpu0/cache/"; - int Idx = 0; - while (true) { - CPUInfo::CacheInfo info; - std::string FPath = StrCat(dir, "index", Idx++, "/"); - std::ifstream f(StrCat(FPath, "size").c_str()); - if (!f.is_open()) break; - std::string suffix; - f >> info.size; - if (f.fail()) - PrintErrorAndDie("Failed while reading file '", FPath, "size'"); - if (f.good()) { - f >> suffix; - if (f.bad()) - PrintErrorAndDie( - "Invalid cache size format: failed to read size suffix"); - else if (f && suffix != "K") - PrintErrorAndDie("Invalid cache size format: Expected bytes ", suffix); - else if (suffix == "K") - info.size *= 1000; - } - if (!ReadFromFile(StrCat(FPath, "type"), &info.type)) - PrintErrorAndDie("Failed to read from file ", FPath, "type"); - if (!ReadFromFile(StrCat(FPath, "level"), &info.level)) - PrintErrorAndDie("Failed to read from file ", FPath, "level"); - std::string map_str; - if (!ReadFromFile(StrCat(FPath, "shared_cpu_map"), &map_str)) - PrintErrorAndDie("Failed to read from file ", FPath, "shared_cpu_map"); - info.num_sharing = CountSetBitsInCPUMap(map_str); - res.push_back(info); - } - - return res; -} - -#ifdef BENCHMARK_OS_MACOSX -std::vector GetCacheSizesMacOSX() { - std::vector res; - std::array CacheCounts{{0, 0, 0, 0}}; - GetSysctl("hw.cacheconfig", &CacheCounts); - - struct { - std::string name; - std::string type; - int level; - size_t num_sharing; - } Cases[] = {{"hw.l1dcachesize", "Data", 1, CacheCounts[1]}, - {"hw.l1icachesize", "Instruction", 1, CacheCounts[1]}, - {"hw.l2cachesize", "Unified", 2, CacheCounts[2]}, - {"hw.l3cachesize", "Unified", 3, CacheCounts[3]}}; - for (auto& C : Cases) { - int val; - if (!GetSysctl(C.name, &val)) continue; - CPUInfo::CacheInfo info; - info.type = C.type; - info.level = C.level; - info.size = val; - info.num_sharing = static_cast(C.num_sharing); - res.push_back(std::move(info)); - } - return res; -} -#elif defined(BENCHMARK_OS_WINDOWS) -std::vector GetCacheSizesWindows() { - std::vector res; - DWORD buffer_size = 0; - using PInfo = SYSTEM_LOGICAL_PROCESSOR_INFORMATION; - using CInfo = CACHE_DESCRIPTOR; - - using UPtr = std::unique_ptr; - GetLogicalProcessorInformation(nullptr, &buffer_size); - UPtr buff((PInfo*)malloc(buffer_size), &std::free); - if (!GetLogicalProcessorInformation(buff.get(), &buffer_size)) - PrintErrorAndDie("Failed during call to GetLogicalProcessorInformation: ", - GetLastError()); - - PInfo* it = buff.get(); - PInfo* end = buff.get() + (buffer_size / sizeof(PInfo)); - - for (; it != end; ++it) { - if (it->Relationship != RelationCache) continue; - using BitSet = std::bitset; - BitSet B(it->ProcessorMask); - // To prevent duplicates, only consider caches where CPU 0 is specified - if (!B.test(0)) continue; - CInfo* Cache = &it->Cache; - CPUInfo::CacheInfo C; - C.num_sharing = static_cast(B.count()); - C.level = Cache->Level; - C.size = Cache->Size; - switch (Cache->Type) { - case CacheUnified: - C.type = "Unified"; - break; - case CacheInstruction: - C.type = "Instruction"; - break; - case CacheData: - C.type = "Data"; - break; - case CacheTrace: - C.type = "Trace"; - break; - default: - C.type = "Unknown"; - break; - } - res.push_back(C); - } - return res; -} -#endif - -std::vector GetCacheSizes() { -#ifdef BENCHMARK_OS_MACOSX - return GetCacheSizesMacOSX(); -#elif defined(BENCHMARK_OS_WINDOWS) - return GetCacheSizesWindows(); -#else - return GetCacheSizesFromKVFS(); -#endif -} - -int GetNumCPUs() { -#ifdef BENCHMARK_HAS_SYSCTL - int NumCPU = -1; - if (GetSysctl("hw.ncpu", &NumCPU)) return NumCPU; - fprintf(stderr, "Err: %s\n", strerror(errno)); - std::exit(EXIT_FAILURE); -#elif defined(BENCHMARK_OS_WINDOWS) - SYSTEM_INFO sysinfo; - // Use memset as opposed to = {} to avoid GCC missing initializer false - // positives. - std::memset(&sysinfo, 0, sizeof(SYSTEM_INFO)); - GetSystemInfo(&sysinfo); - return sysinfo.dwNumberOfProcessors; // number of logical - // processors in the current - // group -#elif defined(BENCHMARK_OS_SOLARIS) - // Returns -1 in case of a failure. - int NumCPU = sysconf(_SC_NPROCESSORS_ONLN); - if (NumCPU < 0) { - fprintf(stderr, - "sysconf(_SC_NPROCESSORS_ONLN) failed with error: %s\n", - strerror(errno)); - } - return NumCPU; -#else - int NumCPUs = 0; - int MaxID = -1; - std::ifstream f("/proc/cpuinfo"); - if (!f.is_open()) { - std::cerr << "failed to open /proc/cpuinfo\n"; - return -1; - } - const std::string Key = "processor"; - std::string ln; - while (std::getline(f, ln)) { - if (ln.empty()) continue; - size_t SplitIdx = ln.find(':'); - std::string value; - if (SplitIdx != std::string::npos) value = ln.substr(SplitIdx + 1); - if (ln.size() >= Key.size() && ln.compare(0, Key.size(), Key) == 0) { - NumCPUs++; - if (!value.empty()) { - int CurID = std::stoi(value); - MaxID = std::max(CurID, MaxID); - } - } - } - if (f.bad()) { - std::cerr << "Failure reading /proc/cpuinfo\n"; - return -1; - } - if (!f.eof()) { - std::cerr << "Failed to read to end of /proc/cpuinfo\n"; - return -1; - } - f.close(); - - if ((MaxID + 1) != NumCPUs) { - fprintf(stderr, - "CPU ID assignments in /proc/cpuinfo seem messed up." - " This is usually caused by a bad BIOS.\n"); - } - return NumCPUs; -#endif - BENCHMARK_UNREACHABLE(); -} - -double GetCPUCyclesPerSecond() { -#if defined BENCHMARK_OS_LINUX || defined BENCHMARK_OS_CYGWIN - long freq; - - // If the kernel is exporting the tsc frequency use that. There are issues - // where cpuinfo_max_freq cannot be relied on because the BIOS may be - // exporintg an invalid p-state (on x86) or p-states may be used to put the - // processor in a new mode (turbo mode). Essentially, those frequencies - // cannot always be relied upon. The same reasons apply to /proc/cpuinfo as - // well. - if (ReadFromFile("/sys/devices/system/cpu/cpu0/tsc_freq_khz", &freq) - // If CPU scaling is in effect, we want to use the *maximum* frequency, - // not whatever CPU speed some random processor happens to be using now. - || ReadFromFile("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", - &freq)) { - // The value is in kHz (as the file name suggests). For example, on a - // 2GHz warpstation, the file contains the value "2000000". - return freq * 1000.0; - } - - const double error_value = -1; - double bogo_clock = error_value; - - std::ifstream f("/proc/cpuinfo"); - if (!f.is_open()) { - std::cerr << "failed to open /proc/cpuinfo\n"; - return error_value; - } - - auto startsWithKey = [](std::string const& Value, std::string const& Key) { - if (Key.size() > Value.size()) return false; - auto Cmp = [&](char X, char Y) { - return std::tolower(X) == std::tolower(Y); - }; - return std::equal(Key.begin(), Key.end(), Value.begin(), Cmp); - }; - - std::string ln; - while (std::getline(f, ln)) { - if (ln.empty()) continue; - size_t SplitIdx = ln.find(':'); - std::string value; - if (SplitIdx != std::string::npos) value = ln.substr(SplitIdx + 1); - // When parsing the "cpu MHz" and "bogomips" (fallback) entries, we only - // accept positive values. Some environments (virtual machines) report zero, - // which would cause infinite looping in WallTime_Init. - if (startsWithKey(ln, "cpu MHz")) { - if (!value.empty()) { - double cycles_per_second = std::stod(value) * 1000000.0; - if (cycles_per_second > 0) return cycles_per_second; - } - } else if (startsWithKey(ln, "bogomips")) { - if (!value.empty()) { - bogo_clock = std::stod(value) * 1000000.0; - if (bogo_clock < 0.0) bogo_clock = error_value; - } - } - } - if (f.bad()) { - std::cerr << "Failure reading /proc/cpuinfo\n"; - return error_value; - } - if (!f.eof()) { - std::cerr << "Failed to read to end of /proc/cpuinfo\n"; - return error_value; - } - f.close(); - // If we found the bogomips clock, but nothing better, we'll use it (but - // we're not happy about it); otherwise, fallback to the rough estimation - // below. - if (bogo_clock >= 0.0) return bogo_clock; - -#elif defined BENCHMARK_HAS_SYSCTL - constexpr auto* FreqStr = -#if defined(BENCHMARK_OS_FREEBSD) || defined(BENCHMARK_OS_NETBSD) - "machdep.tsc_freq"; -#elif defined BENCHMARK_OS_OPENBSD - "hw.cpuspeed"; -#else - "hw.cpufrequency"; -#endif - unsigned long long hz = 0; -#if defined BENCHMARK_OS_OPENBSD - if (GetSysctl(FreqStr, &hz)) return hz * 1000000; -#else - if (GetSysctl(FreqStr, &hz)) return hz; -#endif - fprintf(stderr, "Unable to determine clock rate from sysctl: %s: %s\n", - FreqStr, strerror(errno)); - -#elif defined BENCHMARK_OS_WINDOWS - // In NT, read MHz from the registry. If we fail to do so or we're in win9x - // then make a crude estimate. - DWORD data, data_size = sizeof(data); - if (IsWindowsXPOrGreater() && - SUCCEEDED( - SHGetValueA(HKEY_LOCAL_MACHINE, - "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", - "~MHz", nullptr, &data, &data_size))) - return static_cast((int64_t)data * - (int64_t)(1000 * 1000)); // was mhz -#elif defined (BENCHMARK_OS_SOLARIS) - kstat_ctl_t *kc = kstat_open(); - if (!kc) { - std::cerr << "failed to open /dev/kstat\n"; - return -1; - } - kstat_t *ksp = kstat_lookup(kc, (char*)"cpu_info", -1, (char*)"cpu_info0"); - if (!ksp) { - std::cerr << "failed to lookup in /dev/kstat\n"; - return -1; - } - if (kstat_read(kc, ksp, NULL) < 0) { - std::cerr << "failed to read from /dev/kstat\n"; - return -1; - } - kstat_named_t *knp = - (kstat_named_t*)kstat_data_lookup(ksp, (char*)"current_clock_Hz"); - if (!knp) { - std::cerr << "failed to lookup data in /dev/kstat\n"; - return -1; - } - if (knp->data_type != KSTAT_DATA_UINT64) { - std::cerr << "current_clock_Hz is of unexpected data type: " - << knp->data_type << "\n"; - return -1; - } - double clock_hz = knp->value.ui64; - kstat_close(kc); - return clock_hz; -#endif - // If we've fallen through, attempt to roughly estimate the CPU clock rate. - const int estimate_time_ms = 1000; - const auto start_ticks = cycleclock::Now(); - SleepForMilliseconds(estimate_time_ms); - return static_cast(cycleclock::Now() - start_ticks); -} - -} // end namespace - -const CPUInfo& CPUInfo::Get() { - static const CPUInfo* info = new CPUInfo(); - return *info; -} - -CPUInfo::CPUInfo() - : num_cpus(GetNumCPUs()), - cycles_per_second(GetCPUCyclesPerSecond()), - caches(GetCacheSizes()), - scaling_enabled(CpuScalingEnabled(num_cpus)) {} - -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/thread_manager.h b/benchmarks/thirdparty/benchmark/src/thread_manager.h deleted file mode 100755 index 82b4d72b62..0000000000 --- a/benchmarks/thirdparty/benchmark/src/thread_manager.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef BENCHMARK_THREAD_MANAGER_H -#define BENCHMARK_THREAD_MANAGER_H - -#include - -#include "benchmark/benchmark.h" -#include "mutex.h" - -namespace benchmark { -namespace internal { - -class ThreadManager { - public: - ThreadManager(int num_threads) - : alive_threads_(num_threads), start_stop_barrier_(num_threads) {} - - Mutex& GetBenchmarkMutex() const RETURN_CAPABILITY(benchmark_mutex_) { - return benchmark_mutex_; - } - - bool StartStopBarrier() EXCLUDES(end_cond_mutex_) { - return start_stop_barrier_.wait(); - } - - void NotifyThreadComplete() EXCLUDES(end_cond_mutex_) { - start_stop_barrier_.removeThread(); - if (--alive_threads_ == 0) { - MutexLock lock(end_cond_mutex_); - end_condition_.notify_all(); - } - } - - void WaitForAllThreads() EXCLUDES(end_cond_mutex_) { - MutexLock lock(end_cond_mutex_); - end_condition_.wait(lock.native_handle(), - [this]() { return alive_threads_ == 0; }); - } - - public: - struct Result { - int64_t iterations = 0; - double real_time_used = 0; - double cpu_time_used = 0; - double manual_time_used = 0; - int64_t bytes_processed = 0; - int64_t items_processed = 0; - int64_t complexity_n = 0; - std::string report_label_; - std::string error_message_; - bool has_error_ = false; - UserCounters counters; - }; - GUARDED_BY(GetBenchmarkMutex()) Result results; - - private: - mutable Mutex benchmark_mutex_; - std::atomic alive_threads_; - Barrier start_stop_barrier_; - Mutex end_cond_mutex_; - Condition end_condition_; -}; - -} // namespace internal -} // namespace benchmark - -#endif // BENCHMARK_THREAD_MANAGER_H diff --git a/benchmarks/thirdparty/benchmark/src/thread_timer.h b/benchmarks/thirdparty/benchmark/src/thread_timer.h deleted file mode 100755 index eaf108e017..0000000000 --- a/benchmarks/thirdparty/benchmark/src/thread_timer.h +++ /dev/null @@ -1,69 +0,0 @@ -#ifndef BENCHMARK_THREAD_TIMER_H -#define BENCHMARK_THREAD_TIMER_H - -#include "check.h" -#include "timers.h" - -namespace benchmark { -namespace internal { - -class ThreadTimer { - public: - ThreadTimer() = default; - - // Called by each thread - void StartTimer() { - running_ = true; - start_real_time_ = ChronoClockNow(); - start_cpu_time_ = ThreadCPUUsage(); - } - - // Called by each thread - void StopTimer() { - CHECK(running_); - running_ = false; - real_time_used_ += ChronoClockNow() - start_real_time_; - // Floating point error can result in the subtraction producing a negative - // time. Guard against that. - cpu_time_used_ += std::max(ThreadCPUUsage() - start_cpu_time_, 0); - } - - // Called by each thread - void SetIterationTime(double seconds) { manual_time_used_ += seconds; } - - bool running() const { return running_; } - - // REQUIRES: timer is not running - double real_time_used() { - CHECK(!running_); - return real_time_used_; - } - - // REQUIRES: timer is not running - double cpu_time_used() { - CHECK(!running_); - return cpu_time_used_; - } - - // REQUIRES: timer is not running - double manual_time_used() { - CHECK(!running_); - return manual_time_used_; - } - - private: - bool running_ = false; // Is the timer running - double start_real_time_ = 0; // If running_ - double start_cpu_time_ = 0; // If running_ - - // Accumulated time so far (does not contain current slice if running_) - double real_time_used_ = 0; - double cpu_time_used_ = 0; - // Manually set iteration time. User sets this with SetIterationTime(seconds). - double manual_time_used_ = 0; -}; - -} // namespace internal -} // namespace benchmark - -#endif // BENCHMARK_THREAD_TIMER_H diff --git a/benchmarks/thirdparty/benchmark/src/timers.cc b/benchmarks/thirdparty/benchmark/src/timers.cc deleted file mode 100755 index 2010e2450b..0000000000 --- a/benchmarks/thirdparty/benchmark/src/timers.cc +++ /dev/null @@ -1,217 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "timers.h" -#include "internal_macros.h" - -#ifdef BENCHMARK_OS_WINDOWS -#include -#undef StrCat // Don't let StrCat in string_util.h be renamed to lstrcatA -#include -#include -#else -#include -#ifndef BENCHMARK_OS_FUCHSIA -#include -#endif -#include -#include // this header must be included before 'sys/sysctl.h' to avoid compilation error on FreeBSD -#include -#if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_MACOSX -#include -#endif -#if defined(BENCHMARK_OS_MACOSX) -#include -#include -#include -#endif -#endif - -#ifdef BENCHMARK_OS_EMSCRIPTEN -#include -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "check.h" -#include "log.h" -#include "sleep.h" -#include "string_util.h" - -namespace benchmark { - -// Suppress unused warnings on helper functions. -#if defined(__GNUC__) -#pragma GCC diagnostic ignored "-Wunused-function" -#endif - -namespace { -#if defined(BENCHMARK_OS_WINDOWS) -double MakeTime(FILETIME const& kernel_time, FILETIME const& user_time) { - ULARGE_INTEGER kernel; - ULARGE_INTEGER user; - kernel.HighPart = kernel_time.dwHighDateTime; - kernel.LowPart = kernel_time.dwLowDateTime; - user.HighPart = user_time.dwHighDateTime; - user.LowPart = user_time.dwLowDateTime; - return (static_cast(kernel.QuadPart) + - static_cast(user.QuadPart)) * - 1e-7; -} -#elif !defined(BENCHMARK_OS_FUCHSIA) -double MakeTime(struct rusage const& ru) { - return (static_cast(ru.ru_utime.tv_sec) + - static_cast(ru.ru_utime.tv_usec) * 1e-6 + - static_cast(ru.ru_stime.tv_sec) + - static_cast(ru.ru_stime.tv_usec) * 1e-6); -} -#endif -#if defined(BENCHMARK_OS_MACOSX) -double MakeTime(thread_basic_info_data_t const& info) { - return (static_cast(info.user_time.seconds) + - static_cast(info.user_time.microseconds) * 1e-6 + - static_cast(info.system_time.seconds) + - static_cast(info.system_time.microseconds) * 1e-6); -} -#endif -#if defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_THREAD_CPUTIME_ID) -double MakeTime(struct timespec const& ts) { - return ts.tv_sec + (static_cast(ts.tv_nsec) * 1e-9); -} -#endif - -BENCHMARK_NORETURN static void DiagnoseAndExit(const char* msg) { - std::cerr << "ERROR: " << msg << std::endl; - std::exit(EXIT_FAILURE); -} - -} // end namespace - -double ProcessCPUUsage() { -#if defined(BENCHMARK_OS_WINDOWS) - HANDLE proc = GetCurrentProcess(); - FILETIME creation_time; - FILETIME exit_time; - FILETIME kernel_time; - FILETIME user_time; - if (GetProcessTimes(proc, &creation_time, &exit_time, &kernel_time, - &user_time)) - return MakeTime(kernel_time, user_time); - DiagnoseAndExit("GetProccessTimes() failed"); -#elif defined(BENCHMARK_OS_EMSCRIPTEN) - // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) returns 0 on Emscripten. - // Use Emscripten-specific API. Reported CPU time would be exactly the - // same as total time, but this is ok because there aren't long-latency - // syncronous system calls in Emscripten. - return emscripten_get_now() * 1e-3; -#elif defined(CLOCK_PROCESS_CPUTIME_ID) && !defined(BENCHMARK_OS_MACOSX) - // FIXME We want to use clock_gettime, but its not available in MacOS 10.11. See - // https://github.com/google/benchmark/pull/292 - struct timespec spec; - if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &spec) == 0) - return MakeTime(spec); - DiagnoseAndExit("clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) failed"); -#else - struct rusage ru; - if (getrusage(RUSAGE_SELF, &ru) == 0) return MakeTime(ru); - DiagnoseAndExit("getrusage(RUSAGE_SELF, ...) failed"); -#endif -} - -double ThreadCPUUsage() { -#if defined(BENCHMARK_OS_WINDOWS) - HANDLE this_thread = GetCurrentThread(); - FILETIME creation_time; - FILETIME exit_time; - FILETIME kernel_time; - FILETIME user_time; - GetThreadTimes(this_thread, &creation_time, &exit_time, &kernel_time, - &user_time); - return MakeTime(kernel_time, user_time); -#elif defined(BENCHMARK_OS_MACOSX) - // FIXME We want to use clock_gettime, but its not available in MacOS 10.11. See - // https://github.com/google/benchmark/pull/292 - mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; - thread_basic_info_data_t info; - mach_port_t thread = pthread_mach_thread_np(pthread_self()); - if (thread_info(thread, THREAD_BASIC_INFO, (thread_info_t)&info, &count) == - KERN_SUCCESS) { - return MakeTime(info); - } - DiagnoseAndExit("ThreadCPUUsage() failed when evaluating thread_info"); -#elif defined(BENCHMARK_OS_EMSCRIPTEN) - // Emscripten doesn't support traditional threads - return ProcessCPUUsage(); -#elif defined(BENCHMARK_OS_RTEMS) - // RTEMS doesn't support CLOCK_THREAD_CPUTIME_ID. See - // https://github.com/RTEMS/rtems/blob/master/cpukit/posix/src/clockgettime.c - return ProcessCPUUsage(); -#elif defined(BENCHMARK_OS_SOLARIS) - struct rusage ru; - if (getrusage(RUSAGE_LWP, &ru) == 0) return MakeTime(ru); - DiagnoseAndExit("getrusage(RUSAGE_LWP, ...) failed"); -#elif defined(CLOCK_THREAD_CPUTIME_ID) - struct timespec ts; - if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) return MakeTime(ts); - DiagnoseAndExit("clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...) failed"); -#else -#error Per-thread timing is not available on your system. -#endif -} - -namespace { - -std::string DateTimeString(bool local) { - typedef std::chrono::system_clock Clock; - std::time_t now = Clock::to_time_t(Clock::now()); - const std::size_t kStorageSize = 128; - char storage[kStorageSize]; - std::size_t written; - - if (local) { -#if defined(BENCHMARK_OS_WINDOWS) - written = - std::strftime(storage, sizeof(storage), "%x %X", ::localtime(&now)); -#else - std::tm timeinfo; - ::localtime_r(&now, &timeinfo); - written = std::strftime(storage, sizeof(storage), "%F %T", &timeinfo); -#endif - } else { -#if defined(BENCHMARK_OS_WINDOWS) - written = std::strftime(storage, sizeof(storage), "%x %X", ::gmtime(&now)); -#else - std::tm timeinfo; - ::gmtime_r(&now, &timeinfo); - written = std::strftime(storage, sizeof(storage), "%F %T", &timeinfo); -#endif - } - CHECK(written < kStorageSize); - ((void)written); // prevent unused variable in optimized mode. - return std::string(storage); -} - -} // end namespace - -std::string LocalDateTimeString() { return DateTimeString(true); } - -} // end namespace benchmark diff --git a/benchmarks/thirdparty/benchmark/src/timers.h b/benchmarks/thirdparty/benchmark/src/timers.h deleted file mode 100755 index 65606ccd93..0000000000 --- a/benchmarks/thirdparty/benchmark/src/timers.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef BENCHMARK_TIMERS_H -#define BENCHMARK_TIMERS_H - -#include -#include - -namespace benchmark { - -// Return the CPU usage of the current process -double ProcessCPUUsage(); - -// Return the CPU usage of the children of the current process -double ChildrenCPUUsage(); - -// Return the CPU usage of the current thread -double ThreadCPUUsage(); - -#if defined(HAVE_STEADY_CLOCK) -template -struct ChooseSteadyClock { - typedef std::chrono::high_resolution_clock type; -}; - -template <> -struct ChooseSteadyClock { - typedef std::chrono::steady_clock type; -}; -#endif - -struct ChooseClockType { -#if defined(HAVE_STEADY_CLOCK) - typedef ChooseSteadyClock<>::type type; -#else - typedef std::chrono::high_resolution_clock type; -#endif -}; - -inline double ChronoClockNow() { - typedef ChooseClockType::type ClockType; - using FpSeconds = std::chrono::duration; - return FpSeconds(ClockType::now().time_since_epoch()).count(); -} - -std::string LocalDateTimeString(); - -} // end namespace benchmark - -#endif // BENCHMARK_TIMERS_H diff --git a/benchmarks/thirdparty/benchmark/tools/compare.py b/benchmarks/thirdparty/benchmark/tools/compare.py deleted file mode 100755 index f0a4455f5f..0000000000 --- a/benchmarks/thirdparty/benchmark/tools/compare.py +++ /dev/null @@ -1,316 +0,0 @@ -#!/usr/bin/env python - -""" -compare.py - versatile benchmark output compare tool -""" - -import argparse -from argparse import ArgumentParser -import sys -import gbench -from gbench import util, report -from gbench.util import * - - -def check_inputs(in1, in2, flags): - """ - Perform checking on the user provided inputs and diagnose any abnormalities - """ - in1_kind, in1_err = classify_input_file(in1) - in2_kind, in2_err = classify_input_file(in2) - output_file = find_benchmark_flag('--benchmark_out=', flags) - output_type = find_benchmark_flag('--benchmark_out_format=', flags) - if in1_kind == IT_Executable and in2_kind == IT_Executable and output_file: - print(("WARNING: '--benchmark_out=%s' will be passed to both " - "benchmarks causing it to be overwritten") % output_file) - if in1_kind == IT_JSON and in2_kind == IT_JSON and len(flags) > 0: - print("WARNING: passing optional flags has no effect since both " - "inputs are JSON") - if output_type is not None and output_type != 'json': - print(("ERROR: passing '--benchmark_out_format=%s' to 'compare.py`" - " is not supported.") % output_type) - sys.exit(1) - - -def create_parser(): - parser = ArgumentParser( - description='versatile benchmark output compare tool') - subparsers = parser.add_subparsers( - help='This tool has multiple modes of operation:', - dest='mode') - - parser_a = subparsers.add_parser( - 'benchmarks', - help='The most simple use-case, compare all the output of these two benchmarks') - baseline = parser_a.add_argument_group( - 'baseline', 'The benchmark baseline') - baseline.add_argument( - 'test_baseline', - metavar='test_baseline', - type=argparse.FileType('r'), - nargs=1, - help='A benchmark executable or JSON output file') - contender = parser_a.add_argument_group( - 'contender', 'The benchmark that will be compared against the baseline') - contender.add_argument( - 'test_contender', - metavar='test_contender', - type=argparse.FileType('r'), - nargs=1, - help='A benchmark executable or JSON output file') - parser_a.add_argument( - 'benchmark_options', - metavar='benchmark_options', - nargs=argparse.REMAINDER, - help='Arguments to pass when running benchmark executables') - - parser_b = subparsers.add_parser( - 'filters', help='Compare filter one with the filter two of benchmark') - baseline = parser_b.add_argument_group( - 'baseline', 'The benchmark baseline') - baseline.add_argument( - 'test', - metavar='test', - type=argparse.FileType('r'), - nargs=1, - help='A benchmark executable or JSON output file') - baseline.add_argument( - 'filter_baseline', - metavar='filter_baseline', - type=str, - nargs=1, - help='The first filter, that will be used as baseline') - contender = parser_b.add_argument_group( - 'contender', 'The benchmark that will be compared against the baseline') - contender.add_argument( - 'filter_contender', - metavar='filter_contender', - type=str, - nargs=1, - help='The second filter, that will be compared against the baseline') - parser_b.add_argument( - 'benchmark_options', - metavar='benchmark_options', - nargs=argparse.REMAINDER, - help='Arguments to pass when running benchmark executables') - - parser_c = subparsers.add_parser( - 'benchmarksfiltered', - help='Compare filter one of first benchmark with filter two of the second benchmark') - baseline = parser_c.add_argument_group( - 'baseline', 'The benchmark baseline') - baseline.add_argument( - 'test_baseline', - metavar='test_baseline', - type=argparse.FileType('r'), - nargs=1, - help='A benchmark executable or JSON output file') - baseline.add_argument( - 'filter_baseline', - metavar='filter_baseline', - type=str, - nargs=1, - help='The first filter, that will be used as baseline') - contender = parser_c.add_argument_group( - 'contender', 'The benchmark that will be compared against the baseline') - contender.add_argument( - 'test_contender', - metavar='test_contender', - type=argparse.FileType('r'), - nargs=1, - help='The second benchmark executable or JSON output file, that will be compared against the baseline') - contender.add_argument( - 'filter_contender', - metavar='filter_contender', - type=str, - nargs=1, - help='The second filter, that will be compared against the baseline') - parser_c.add_argument( - 'benchmark_options', - metavar='benchmark_options', - nargs=argparse.REMAINDER, - help='Arguments to pass when running benchmark executables') - - return parser - - -def main(): - # Parse the command line flags - parser = create_parser() - args, unknown_args = parser.parse_known_args() - if args.mode is None: - parser.print_help() - exit(1) - assert not unknown_args - benchmark_options = args.benchmark_options - - if args.mode == 'benchmarks': - test_baseline = args.test_baseline[0].name - test_contender = args.test_contender[0].name - filter_baseline = '' - filter_contender = '' - - # NOTE: if test_baseline == test_contender, you are analyzing the stdev - - description = 'Comparing %s to %s' % (test_baseline, test_contender) - elif args.mode == 'filters': - test_baseline = args.test[0].name - test_contender = args.test[0].name - filter_baseline = args.filter_baseline[0] - filter_contender = args.filter_contender[0] - - # NOTE: if filter_baseline == filter_contender, you are analyzing the - # stdev - - description = 'Comparing %s to %s (from %s)' % ( - filter_baseline, filter_contender, args.test[0].name) - elif args.mode == 'benchmarksfiltered': - test_baseline = args.test_baseline[0].name - test_contender = args.test_contender[0].name - filter_baseline = args.filter_baseline[0] - filter_contender = args.filter_contender[0] - - # NOTE: if test_baseline == test_contender and - # filter_baseline == filter_contender, you are analyzing the stdev - - description = 'Comparing %s (from %s) to %s (from %s)' % ( - filter_baseline, test_baseline, filter_contender, test_contender) - else: - # should never happen - print("Unrecognized mode of operation: '%s'" % args.mode) - parser.print_help() - exit(1) - - check_inputs(test_baseline, test_contender, benchmark_options) - - options_baseline = [] - options_contender = [] - - if filter_baseline and filter_contender: - options_baseline = ['--benchmark_filter=%s' % filter_baseline] - options_contender = ['--benchmark_filter=%s' % filter_contender] - - # Run the benchmarks and report the results - json1 = json1_orig = gbench.util.run_or_load_benchmark( - test_baseline, benchmark_options + options_baseline) - json2 = json2_orig = gbench.util.run_or_load_benchmark( - test_contender, benchmark_options + options_contender) - - # Now, filter the benchmarks so that the difference report can work - if filter_baseline and filter_contender: - replacement = '[%s vs. %s]' % (filter_baseline, filter_contender) - json1 = gbench.report.filter_benchmark( - json1_orig, filter_baseline, replacement) - json2 = gbench.report.filter_benchmark( - json2_orig, filter_contender, replacement) - - # Diff and output - output_lines = gbench.report.generate_difference_report(json1, json2) - print(description) - for ln in output_lines: - print(ln) - - -import unittest - - -class TestParser(unittest.TestCase): - def setUp(self): - self.parser = create_parser() - testInputs = os.path.join( - os.path.dirname( - os.path.realpath(__file__)), - 'gbench', - 'Inputs') - self.testInput0 = os.path.join(testInputs, 'test1_run1.json') - self.testInput1 = os.path.join(testInputs, 'test1_run2.json') - - def test_benchmarks_basic(self): - parsed = self.parser.parse_args( - ['benchmarks', self.testInput0, self.testInput1]) - self.assertEqual(parsed.mode, 'benchmarks') - self.assertEqual(parsed.test_baseline[0].name, self.testInput0) - self.assertEqual(parsed.test_contender[0].name, self.testInput1) - self.assertFalse(parsed.benchmark_options) - - def test_benchmarks_with_remainder(self): - parsed = self.parser.parse_args( - ['benchmarks', self.testInput0, self.testInput1, 'd']) - self.assertEqual(parsed.mode, 'benchmarks') - self.assertEqual(parsed.test_baseline[0].name, self.testInput0) - self.assertEqual(parsed.test_contender[0].name, self.testInput1) - self.assertEqual(parsed.benchmark_options, ['d']) - - def test_benchmarks_with_remainder_after_doubleminus(self): - parsed = self.parser.parse_args( - ['benchmarks', self.testInput0, self.testInput1, '--', 'e']) - self.assertEqual(parsed.mode, 'benchmarks') - self.assertEqual(parsed.test_baseline[0].name, self.testInput0) - self.assertEqual(parsed.test_contender[0].name, self.testInput1) - self.assertEqual(parsed.benchmark_options, ['e']) - - def test_filters_basic(self): - parsed = self.parser.parse_args( - ['filters', self.testInput0, 'c', 'd']) - self.assertEqual(parsed.mode, 'filters') - self.assertEqual(parsed.test[0].name, self.testInput0) - self.assertEqual(parsed.filter_baseline[0], 'c') - self.assertEqual(parsed.filter_contender[0], 'd') - self.assertFalse(parsed.benchmark_options) - - def test_filters_with_remainder(self): - parsed = self.parser.parse_args( - ['filters', self.testInput0, 'c', 'd', 'e']) - self.assertEqual(parsed.mode, 'filters') - self.assertEqual(parsed.test[0].name, self.testInput0) - self.assertEqual(parsed.filter_baseline[0], 'c') - self.assertEqual(parsed.filter_contender[0], 'd') - self.assertEqual(parsed.benchmark_options, ['e']) - - def test_filters_with_remainder_after_doubleminus(self): - parsed = self.parser.parse_args( - ['filters', self.testInput0, 'c', 'd', '--', 'f']) - self.assertEqual(parsed.mode, 'filters') - self.assertEqual(parsed.test[0].name, self.testInput0) - self.assertEqual(parsed.filter_baseline[0], 'c') - self.assertEqual(parsed.filter_contender[0], 'd') - self.assertEqual(parsed.benchmark_options, ['f']) - - def test_benchmarksfiltered_basic(self): - parsed = self.parser.parse_args( - ['benchmarksfiltered', self.testInput0, 'c', self.testInput1, 'e']) - self.assertEqual(parsed.mode, 'benchmarksfiltered') - self.assertEqual(parsed.test_baseline[0].name, self.testInput0) - self.assertEqual(parsed.filter_baseline[0], 'c') - self.assertEqual(parsed.test_contender[0].name, self.testInput1) - self.assertEqual(parsed.filter_contender[0], 'e') - self.assertFalse(parsed.benchmark_options) - - def test_benchmarksfiltered_with_remainder(self): - parsed = self.parser.parse_args( - ['benchmarksfiltered', self.testInput0, 'c', self.testInput1, 'e', 'f']) - self.assertEqual(parsed.mode, 'benchmarksfiltered') - self.assertEqual(parsed.test_baseline[0].name, self.testInput0) - self.assertEqual(parsed.filter_baseline[0], 'c') - self.assertEqual(parsed.test_contender[0].name, self.testInput1) - self.assertEqual(parsed.filter_contender[0], 'e') - self.assertEqual(parsed.benchmark_options[0], 'f') - - def test_benchmarksfiltered_with_remainder_after_doubleminus(self): - parsed = self.parser.parse_args( - ['benchmarksfiltered', self.testInput0, 'c', self.testInput1, 'e', '--', 'g']) - self.assertEqual(parsed.mode, 'benchmarksfiltered') - self.assertEqual(parsed.test_baseline[0].name, self.testInput0) - self.assertEqual(parsed.filter_baseline[0], 'c') - self.assertEqual(parsed.test_contender[0].name, self.testInput1) - self.assertEqual(parsed.filter_contender[0], 'e') - self.assertEqual(parsed.benchmark_options[0], 'g') - - -if __name__ == '__main__': - # unittest.main() - main() - -# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 -# kate: tab-width: 4; replace-tabs on; indent-width 4; tab-indents: off; -# kate: indent-mode python; remove-trailing-spaces modified; diff --git a/benchmarks/thirdparty/benchmark/tools/compare_bench.py b/benchmarks/thirdparty/benchmark/tools/compare_bench.py deleted file mode 100755 index 7bbf0d0157..0000000000 --- a/benchmarks/thirdparty/benchmark/tools/compare_bench.py +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env python -""" -compare_bench.py - Compare two benchmarks or their results and report the - difference. -""" -import argparse -from argparse import ArgumentParser -import sys -import gbench -from gbench import util, report -from gbench.util import * - -def check_inputs(in1, in2, flags): - """ - Perform checking on the user provided inputs and diagnose any abnormalities - """ - in1_kind, in1_err = classify_input_file(in1) - in2_kind, in2_err = classify_input_file(in2) - output_file = find_benchmark_flag('--benchmark_out=', flags) - output_type = find_benchmark_flag('--benchmark_out_format=', flags) - if in1_kind == IT_Executable and in2_kind == IT_Executable and output_file: - print(("WARNING: '--benchmark_out=%s' will be passed to both " - "benchmarks causing it to be overwritten") % output_file) - if in1_kind == IT_JSON and in2_kind == IT_JSON and len(flags) > 0: - print("WARNING: passing --benchmark flags has no effect since both " - "inputs are JSON") - if output_type is not None and output_type != 'json': - print(("ERROR: passing '--benchmark_out_format=%s' to 'compare_bench.py`" - " is not supported.") % output_type) - sys.exit(1) - - -def main(): - parser = ArgumentParser( - description='compare the results of two benchmarks') - parser.add_argument( - 'test1', metavar='test1', type=str, nargs=1, - help='A benchmark executable or JSON output file') - parser.add_argument( - 'test2', metavar='test2', type=str, nargs=1, - help='A benchmark executable or JSON output file') - parser.add_argument( - 'benchmark_options', metavar='benchmark_options', nargs=argparse.REMAINDER, - help='Arguments to pass when running benchmark executables' - ) - args, unknown_args = parser.parse_known_args() - # Parse the command line flags - test1 = args.test1[0] - test2 = args.test2[0] - if unknown_args: - # should never happen - print("Unrecognized positional argument arguments: '%s'" - % unknown_args) - exit(1) - benchmark_options = args.benchmark_options - check_inputs(test1, test2, benchmark_options) - # Run the benchmarks and report the results - json1 = gbench.util.run_or_load_benchmark(test1, benchmark_options) - json2 = gbench.util.run_or_load_benchmark(test2, benchmark_options) - output_lines = gbench.report.generate_difference_report(json1, json2) - print('Comparing %s to %s' % (test1, test2)) - for ln in output_lines: - print(ln) - - -if __name__ == '__main__': - main() diff --git a/benchmarks/thirdparty/benchmark/tools/gbench/Inputs/test1_run1.json b/benchmarks/thirdparty/benchmark/tools/gbench/Inputs/test1_run1.json deleted file mode 100755 index d7ec6a9c8f..0000000000 --- a/benchmarks/thirdparty/benchmark/tools/gbench/Inputs/test1_run1.json +++ /dev/null @@ -1,102 +0,0 @@ -{ - "context": { - "date": "2016-08-02 17:44:46", - "num_cpus": 4, - "mhz_per_cpu": 4228, - "cpu_scaling_enabled": false, - "library_build_type": "release" - }, - "benchmarks": [ - { - "name": "BM_SameTimes", - "iterations": 1000, - "real_time": 10, - "cpu_time": 10, - "time_unit": "ns" - }, - { - "name": "BM_2xFaster", - "iterations": 1000, - "real_time": 50, - "cpu_time": 50, - "time_unit": "ns" - }, - { - "name": "BM_2xSlower", - "iterations": 1000, - "real_time": 50, - "cpu_time": 50, - "time_unit": "ns" - }, - { - "name": "BM_1PercentFaster", - "iterations": 1000, - "real_time": 100, - "cpu_time": 100, - "time_unit": "ns" - }, - { - "name": "BM_1PercentSlower", - "iterations": 1000, - "real_time": 100, - "cpu_time": 100, - "time_unit": "ns" - }, - { - "name": "BM_10PercentFaster", - "iterations": 1000, - "real_time": 100, - "cpu_time": 100, - "time_unit": "ns" - }, - { - "name": "BM_10PercentSlower", - "iterations": 1000, - "real_time": 100, - "cpu_time": 100, - "time_unit": "ns" - }, - { - "name": "BM_100xSlower", - "iterations": 1000, - "real_time": 100, - "cpu_time": 100, - "time_unit": "ns" - }, - { - "name": "BM_100xFaster", - "iterations": 1000, - "real_time": 10000, - "cpu_time": 10000, - "time_unit": "ns" - }, - { - "name": "BM_10PercentCPUToTime", - "iterations": 1000, - "real_time": 100, - "cpu_time": 100, - "time_unit": "ns" - }, - { - "name": "BM_ThirdFaster", - "iterations": 1000, - "real_time": 100, - "cpu_time": 100, - "time_unit": "ns" - }, - { - "name": "BM_BadTimeUnit", - "iterations": 1000, - "real_time": 0.4, - "cpu_time": 0.5, - "time_unit": "s" - }, - { - "name": "BM_DifferentTimeUnit", - "iterations": 1, - "real_time": 1, - "cpu_time": 1, - "time_unit": "s" - } - ] -} diff --git a/benchmarks/thirdparty/benchmark/tools/gbench/Inputs/test1_run2.json b/benchmarks/thirdparty/benchmark/tools/gbench/Inputs/test1_run2.json deleted file mode 100755 index 59a5ffaca4..0000000000 --- a/benchmarks/thirdparty/benchmark/tools/gbench/Inputs/test1_run2.json +++ /dev/null @@ -1,102 +0,0 @@ -{ - "context": { - "date": "2016-08-02 17:44:46", - "num_cpus": 4, - "mhz_per_cpu": 4228, - "cpu_scaling_enabled": false, - "library_build_type": "release" - }, - "benchmarks": [ - { - "name": "BM_SameTimes", - "iterations": 1000, - "real_time": 10, - "cpu_time": 10, - "time_unit": "ns" - }, - { - "name": "BM_2xFaster", - "iterations": 1000, - "real_time": 25, - "cpu_time": 25, - "time_unit": "ns" - }, - { - "name": "BM_2xSlower", - "iterations": 20833333, - "real_time": 100, - "cpu_time": 100, - "time_unit": "ns" - }, - { - "name": "BM_1PercentFaster", - "iterations": 1000, - "real_time": 98.9999999, - "cpu_time": 98.9999999, - "time_unit": "ns" - }, - { - "name": "BM_1PercentSlower", - "iterations": 1000, - "real_time": 100.9999999, - "cpu_time": 100.9999999, - "time_unit": "ns" - }, - { - "name": "BM_10PercentFaster", - "iterations": 1000, - "real_time": 90, - "cpu_time": 90, - "time_unit": "ns" - }, - { - "name": "BM_10PercentSlower", - "iterations": 1000, - "real_time": 110, - "cpu_time": 110, - "time_unit": "ns" - }, - { - "name": "BM_100xSlower", - "iterations": 1000, - "real_time": 1.0000e+04, - "cpu_time": 1.0000e+04, - "time_unit": "ns" - }, - { - "name": "BM_100xFaster", - "iterations": 1000, - "real_time": 100, - "cpu_time": 100, - "time_unit": "ns" - }, - { - "name": "BM_10PercentCPUToTime", - "iterations": 1000, - "real_time": 110, - "cpu_time": 90, - "time_unit": "ns" - }, - { - "name": "BM_ThirdFaster", - "iterations": 1000, - "real_time": 66.665, - "cpu_time": 66.664, - "time_unit": "ns" - }, - { - "name": "BM_BadTimeUnit", - "iterations": 1000, - "real_time": 0.04, - "cpu_time": 0.6, - "time_unit": "s" - }, - { - "name": "BM_DifferentTimeUnit", - "iterations": 1, - "real_time": 1, - "cpu_time": 1, - "time_unit": "ns" - } - ] -} diff --git a/benchmarks/thirdparty/benchmark/tools/gbench/Inputs/test2_run.json b/benchmarks/thirdparty/benchmark/tools/gbench/Inputs/test2_run.json deleted file mode 100755 index 15bc698030..0000000000 --- a/benchmarks/thirdparty/benchmark/tools/gbench/Inputs/test2_run.json +++ /dev/null @@ -1,81 +0,0 @@ -{ - "context": { - "date": "2016-08-02 17:44:46", - "num_cpus": 4, - "mhz_per_cpu": 4228, - "cpu_scaling_enabled": false, - "library_build_type": "release" - }, - "benchmarks": [ - { - "name": "BM_Hi", - "iterations": 1234, - "real_time": 42, - "cpu_time": 24, - "time_unit": "ms" - }, - { - "name": "BM_Zero", - "iterations": 1000, - "real_time": 10, - "cpu_time": 10, - "time_unit": "ns" - }, - { - "name": "BM_Zero/4", - "iterations": 4000, - "real_time": 40, - "cpu_time": 40, - "time_unit": "ns" - }, - { - "name": "Prefix/BM_Zero", - "iterations": 2000, - "real_time": 20, - "cpu_time": 20, - "time_unit": "ns" - }, - { - "name": "Prefix/BM_Zero/3", - "iterations": 3000, - "real_time": 30, - "cpu_time": 30, - "time_unit": "ns" - }, - { - "name": "BM_One", - "iterations": 5000, - "real_time": 5, - "cpu_time": 5, - "time_unit": "ns" - }, - { - "name": "BM_One/4", - "iterations": 2000, - "real_time": 20, - "cpu_time": 20, - "time_unit": "ns" - }, - { - "name": "Prefix/BM_One", - "iterations": 1000, - "real_time": 10, - "cpu_time": 10, - "time_unit": "ns" - }, - { - "name": "Prefix/BM_One/3", - "iterations": 1500, - "real_time": 15, - "cpu_time": 15, - "time_unit": "ns" - }, - { - "name": "BM_Bye", - "iterations": 5321, - "real_time": 11, - "cpu_time": 63, - "time_unit": "ns" - } - ] -} diff --git a/benchmarks/thirdparty/benchmark/tools/gbench/__init__.py b/benchmarks/thirdparty/benchmark/tools/gbench/__init__.py deleted file mode 100755 index fce1a1acfb..0000000000 --- a/benchmarks/thirdparty/benchmark/tools/gbench/__init__.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Google Benchmark tooling""" - -__author__ = 'Eric Fiselier' -__email__ = 'eric@efcs.ca' -__versioninfo__ = (0, 5, 0) -__version__ = '.'.join(str(v) for v in __versioninfo__) + 'dev' - -__all__ = [] diff --git a/benchmarks/thirdparty/benchmark/tools/gbench/report.py b/benchmarks/thirdparty/benchmark/tools/gbench/report.py deleted file mode 100755 index 0c090981a8..0000000000 --- a/benchmarks/thirdparty/benchmark/tools/gbench/report.py +++ /dev/null @@ -1,208 +0,0 @@ -"""report.py - Utilities for reporting statistics about benchmark results -""" -import os -import re -import copy - -class BenchmarkColor(object): - def __init__(self, name, code): - self.name = name - self.code = code - - def __repr__(self): - return '%s%r' % (self.__class__.__name__, - (self.name, self.code)) - - def __format__(self, format): - return self.code - -# Benchmark Colors Enumeration -BC_NONE = BenchmarkColor('NONE', '') -BC_MAGENTA = BenchmarkColor('MAGENTA', '\033[95m') -BC_CYAN = BenchmarkColor('CYAN', '\033[96m') -BC_OKBLUE = BenchmarkColor('OKBLUE', '\033[94m') -BC_HEADER = BenchmarkColor('HEADER', '\033[92m') -BC_WARNING = BenchmarkColor('WARNING', '\033[93m') -BC_WHITE = BenchmarkColor('WHITE', '\033[97m') -BC_FAIL = BenchmarkColor('FAIL', '\033[91m') -BC_ENDC = BenchmarkColor('ENDC', '\033[0m') -BC_BOLD = BenchmarkColor('BOLD', '\033[1m') -BC_UNDERLINE = BenchmarkColor('UNDERLINE', '\033[4m') - -def color_format(use_color, fmt_str, *args, **kwargs): - """ - Return the result of 'fmt_str.format(*args, **kwargs)' after transforming - 'args' and 'kwargs' according to the value of 'use_color'. If 'use_color' - is False then all color codes in 'args' and 'kwargs' are replaced with - the empty string. - """ - assert use_color is True or use_color is False - if not use_color: - args = [arg if not isinstance(arg, BenchmarkColor) else BC_NONE - for arg in args] - kwargs = {key: arg if not isinstance(arg, BenchmarkColor) else BC_NONE - for key, arg in kwargs.items()} - return fmt_str.format(*args, **kwargs) - - -def find_longest_name(benchmark_list): - """ - Return the length of the longest benchmark name in a given list of - benchmark JSON objects - """ - longest_name = 1 - for bc in benchmark_list: - if len(bc['name']) > longest_name: - longest_name = len(bc['name']) - return longest_name - - -def calculate_change(old_val, new_val): - """ - Return a float representing the decimal change between old_val and new_val. - """ - if old_val == 0 and new_val == 0: - return 0.0 - if old_val == 0: - return float(new_val - old_val) / (float(old_val + new_val) / 2) - return float(new_val - old_val) / abs(old_val) - - -def filter_benchmark(json_orig, family, replacement=""): - """ - Apply a filter to the json, and only leave the 'family' of benchmarks. - """ - regex = re.compile(family) - filtered = {} - filtered['benchmarks'] = [] - for be in json_orig['benchmarks']: - if not regex.search(be['name']): - continue - filteredbench = copy.deepcopy(be) # Do NOT modify the old name! - filteredbench['name'] = regex.sub(replacement, filteredbench['name']) - filtered['benchmarks'].append(filteredbench) - return filtered - - -def generate_difference_report(json1, json2, use_color=True): - """ - Calculate and report the difference between each test of two benchmarks - runs specified as 'json1' and 'json2'. - """ - first_col_width = find_longest_name(json1['benchmarks']) - def find_test(name): - for b in json2['benchmarks']: - if b['name'] == name: - return b - return None - first_col_width = max(first_col_width, len('Benchmark')) - first_line = "{:<{}s}Time CPU Time Old Time New CPU Old CPU New".format( - 'Benchmark', 12 + first_col_width) - output_strs = [first_line, '-' * len(first_line)] - - gen = (bn for bn in json1['benchmarks'] if 'real_time' in bn and 'cpu_time' in bn) - for bn in gen: - other_bench = find_test(bn['name']) - if not other_bench: - continue - - if bn['time_unit'] != other_bench['time_unit']: - continue - - def get_color(res): - if res > 0.05: - return BC_FAIL - elif res > -0.07: - return BC_WHITE - else: - return BC_CYAN - fmt_str = "{}{:<{}s}{endc}{}{:+16.4f}{endc}{}{:+16.4f}{endc}{:14.0f}{:14.0f}{endc}{:14.0f}{:14.0f}" - tres = calculate_change(bn['real_time'], other_bench['real_time']) - cpures = calculate_change(bn['cpu_time'], other_bench['cpu_time']) - output_strs += [color_format(use_color, fmt_str, - BC_HEADER, bn['name'], first_col_width, - get_color(tres), tres, get_color(cpures), cpures, - bn['real_time'], other_bench['real_time'], - bn['cpu_time'], other_bench['cpu_time'], - endc=BC_ENDC)] - return output_strs - -############################################################################### -# Unit tests - -import unittest - -class TestReportDifference(unittest.TestCase): - def load_results(self): - import json - testInputs = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'Inputs') - testOutput1 = os.path.join(testInputs, 'test1_run1.json') - testOutput2 = os.path.join(testInputs, 'test1_run2.json') - with open(testOutput1, 'r') as f: - json1 = json.load(f) - with open(testOutput2, 'r') as f: - json2 = json.load(f) - return json1, json2 - - def test_basic(self): - expect_lines = [ - ['BM_SameTimes', '+0.0000', '+0.0000', '10', '10', '10', '10'], - ['BM_2xFaster', '-0.5000', '-0.5000', '50', '25', '50', '25'], - ['BM_2xSlower', '+1.0000', '+1.0000', '50', '100', '50', '100'], - ['BM_1PercentFaster', '-0.0100', '-0.0100', '100', '99', '100', '99'], - ['BM_1PercentSlower', '+0.0100', '+0.0100', '100', '101', '100', '101'], - ['BM_10PercentFaster', '-0.1000', '-0.1000', '100', '90', '100', '90'], - ['BM_10PercentSlower', '+0.1000', '+0.1000', '100', '110', '100', '110'], - ['BM_100xSlower', '+99.0000', '+99.0000', '100', '10000', '100', '10000'], - ['BM_100xFaster', '-0.9900', '-0.9900', '10000', '100', '10000', '100'], - ['BM_10PercentCPUToTime', '+0.1000', '-0.1000', '100', '110', '100', '90'], - ['BM_ThirdFaster', '-0.3333', '-0.3334', '100', '67', '100', '67'], - ['BM_BadTimeUnit', '-0.9000', '+0.2000', '0', '0', '0', '1'], - ] - json1, json2 = self.load_results() - output_lines_with_header = generate_difference_report(json1, json2, use_color=False) - output_lines = output_lines_with_header[2:] - print("\n".join(output_lines_with_header)) - self.assertEqual(len(output_lines), len(expect_lines)) - for i in range(0, len(output_lines)): - parts = [x for x in output_lines[i].split(' ') if x] - self.assertEqual(len(parts), 7) - self.assertEqual(parts, expect_lines[i]) - - -class TestReportDifferenceBetweenFamilies(unittest.TestCase): - def load_result(self): - import json - testInputs = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'Inputs') - testOutput = os.path.join(testInputs, 'test2_run.json') - with open(testOutput, 'r') as f: - json = json.load(f) - return json - - def test_basic(self): - expect_lines = [ - ['.', '-0.5000', '-0.5000', '10', '5', '10', '5'], - ['./4', '-0.5000', '-0.5000', '40', '20', '40', '20'], - ['Prefix/.', '-0.5000', '-0.5000', '20', '10', '20', '10'], - ['Prefix/./3', '-0.5000', '-0.5000', '30', '15', '30', '15'], - ] - json = self.load_result() - json1 = filter_benchmark(json, "BM_Z.ro", ".") - json2 = filter_benchmark(json, "BM_O.e", ".") - output_lines_with_header = generate_difference_report(json1, json2, use_color=False) - output_lines = output_lines_with_header[2:] - print("\n") - print("\n".join(output_lines_with_header)) - self.assertEqual(len(output_lines), len(expect_lines)) - for i in range(0, len(output_lines)): - parts = [x for x in output_lines[i].split(' ') if x] - self.assertEqual(len(parts), 7) - self.assertEqual(parts, expect_lines[i]) - - -if __name__ == '__main__': - unittest.main() - -# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 -# kate: tab-width: 4; replace-tabs on; indent-width 4; tab-indents: off; -# kate: indent-mode python; remove-trailing-spaces modified; diff --git a/benchmarks/thirdparty/benchmark/tools/gbench/util.py b/benchmarks/thirdparty/benchmark/tools/gbench/util.py deleted file mode 100755 index 07c2377275..0000000000 --- a/benchmarks/thirdparty/benchmark/tools/gbench/util.py +++ /dev/null @@ -1,159 +0,0 @@ -"""util.py - General utilities for running, loading, and processing benchmarks -""" -import json -import os -import tempfile -import subprocess -import sys - -# Input file type enumeration -IT_Invalid = 0 -IT_JSON = 1 -IT_Executable = 2 - -_num_magic_bytes = 2 if sys.platform.startswith('win') else 4 -def is_executable_file(filename): - """ - Return 'True' if 'filename' names a valid file which is likely - an executable. A file is considered an executable if it starts with the - magic bytes for a EXE, Mach O, or ELF file. - """ - if not os.path.isfile(filename): - return False - with open(filename, mode='rb') as f: - magic_bytes = f.read(_num_magic_bytes) - if sys.platform == 'darwin': - return magic_bytes in [ - b'\xfe\xed\xfa\xce', # MH_MAGIC - b'\xce\xfa\xed\xfe', # MH_CIGAM - b'\xfe\xed\xfa\xcf', # MH_MAGIC_64 - b'\xcf\xfa\xed\xfe', # MH_CIGAM_64 - b'\xca\xfe\xba\xbe', # FAT_MAGIC - b'\xbe\xba\xfe\xca' # FAT_CIGAM - ] - elif sys.platform.startswith('win'): - return magic_bytes == b'MZ' - else: - return magic_bytes == b'\x7FELF' - - -def is_json_file(filename): - """ - Returns 'True' if 'filename' names a valid JSON output file. - 'False' otherwise. - """ - try: - with open(filename, 'r') as f: - json.load(f) - return True - except: - pass - return False - - -def classify_input_file(filename): - """ - Return a tuple (type, msg) where 'type' specifies the classified type - of 'filename'. If 'type' is 'IT_Invalid' then 'msg' is a human readable - string represeting the error. - """ - ftype = IT_Invalid - err_msg = None - if not os.path.exists(filename): - err_msg = "'%s' does not exist" % filename - elif not os.path.isfile(filename): - err_msg = "'%s' does not name a file" % filename - elif is_executable_file(filename): - ftype = IT_Executable - elif is_json_file(filename): - ftype = IT_JSON - else: - err_msg = "'%s' does not name a valid benchmark executable or JSON file" % filename - return ftype, err_msg - - -def check_input_file(filename): - """ - Classify the file named by 'filename' and return the classification. - If the file is classified as 'IT_Invalid' print an error message and exit - the program. - """ - ftype, msg = classify_input_file(filename) - if ftype == IT_Invalid: - print("Invalid input file: %s" % msg) - sys.exit(1) - return ftype - -def find_benchmark_flag(prefix, benchmark_flags): - """ - Search the specified list of flags for a flag matching `` and - if it is found return the arg it specifies. If specified more than once the - last value is returned. If the flag is not found None is returned. - """ - assert prefix.startswith('--') and prefix.endswith('=') - result = None - for f in benchmark_flags: - if f.startswith(prefix): - result = f[len(prefix):] - return result - -def remove_benchmark_flags(prefix, benchmark_flags): - """ - Return a new list containing the specified benchmark_flags except those - with the specified prefix. - """ - assert prefix.startswith('--') and prefix.endswith('=') - return [f for f in benchmark_flags if not f.startswith(prefix)] - -def load_benchmark_results(fname): - """ - Read benchmark output from a file and return the JSON object. - REQUIRES: 'fname' names a file containing JSON benchmark output. - """ - with open(fname, 'r') as f: - return json.load(f) - - -def run_benchmark(exe_name, benchmark_flags): - """ - Run a benchmark specified by 'exe_name' with the specified - 'benchmark_flags'. The benchmark is run directly as a subprocess to preserve - real time console output. - RETURNS: A JSON object representing the benchmark output - """ - output_name = find_benchmark_flag('--benchmark_out=', - benchmark_flags) - is_temp_output = False - if output_name is None: - is_temp_output = True - thandle, output_name = tempfile.mkstemp() - os.close(thandle) - benchmark_flags = list(benchmark_flags) + \ - ['--benchmark_out=%s' % output_name] - - cmd = [exe_name] + benchmark_flags - print("RUNNING: %s" % ' '.join(cmd)) - exitCode = subprocess.call(cmd) - if exitCode != 0: - print('TEST FAILED...') - sys.exit(exitCode) - json_res = load_benchmark_results(output_name) - if is_temp_output: - os.unlink(output_name) - return json_res - - -def run_or_load_benchmark(filename, benchmark_flags): - """ - Get the results for a specified benchmark. If 'filename' specifies - an executable benchmark then the results are generated by running the - benchmark. Otherwise 'filename' must name a valid JSON output file, - which is loaded and the result returned. - """ - ftype = check_input_file(filename) - if ftype == IT_JSON: - return load_benchmark_results(filename) - elif ftype == IT_Executable: - return run_benchmark(filename, benchmark_flags) - else: - assert False # This branch is unreachable \ No newline at end of file diff --git a/benchmarks/thirdparty/benchmark/tools/strip_asm.py b/benchmarks/thirdparty/benchmark/tools/strip_asm.py deleted file mode 100755 index 9030550b43..0000000000 --- a/benchmarks/thirdparty/benchmark/tools/strip_asm.py +++ /dev/null @@ -1,151 +0,0 @@ -#!/usr/bin/env python - -""" -strip_asm.py - Cleanup ASM output for the specified file -""" - -from argparse import ArgumentParser -import sys -import os -import re - -def find_used_labels(asm): - found = set() - label_re = re.compile("\s*j[a-z]+\s+\.L([a-zA-Z0-9][a-zA-Z0-9_]*)") - for l in asm.splitlines(): - m = label_re.match(l) - if m: - found.add('.L%s' % m.group(1)) - return found - - -def normalize_labels(asm): - decls = set() - label_decl = re.compile("^[.]{0,1}L([a-zA-Z0-9][a-zA-Z0-9_]*)(?=:)") - for l in asm.splitlines(): - m = label_decl.match(l) - if m: - decls.add(m.group(0)) - if len(decls) == 0: - return asm - needs_dot = next(iter(decls))[0] != '.' - if not needs_dot: - return asm - for ld in decls: - asm = re.sub("(^|\s+)" + ld + "(?=:|\s)", '\\1.' + ld, asm) - return asm - - -def transform_labels(asm): - asm = normalize_labels(asm) - used_decls = find_used_labels(asm) - new_asm = '' - label_decl = re.compile("^\.L([a-zA-Z0-9][a-zA-Z0-9_]*)(?=:)") - for l in asm.splitlines(): - m = label_decl.match(l) - if not m or m.group(0) in used_decls: - new_asm += l - new_asm += '\n' - return new_asm - - -def is_identifier(tk): - if len(tk) == 0: - return False - first = tk[0] - if not first.isalpha() and first != '_': - return False - for i in range(1, len(tk)): - c = tk[i] - if not c.isalnum() and c != '_': - return False - return True - -def process_identifiers(l): - """ - process_identifiers - process all identifiers and modify them to have - consistent names across all platforms; specifically across ELF and MachO. - For example, MachO inserts an additional understore at the beginning of - names. This function removes that. - """ - parts = re.split(r'([a-zA-Z0-9_]+)', l) - new_line = '' - for tk in parts: - if is_identifier(tk): - if tk.startswith('__Z'): - tk = tk[1:] - elif tk.startswith('_') and len(tk) > 1 and \ - tk[1].isalpha() and tk[1] != 'Z': - tk = tk[1:] - new_line += tk - return new_line - - -def process_asm(asm): - """ - Strip the ASM of unwanted directives and lines - """ - new_contents = '' - asm = transform_labels(asm) - - # TODO: Add more things we want to remove - discard_regexes = [ - re.compile("\s+\..*$"), # directive - re.compile("\s*#(NO_APP|APP)$"), #inline ASM - re.compile("\s*#.*$"), # comment line - re.compile("\s*\.globa?l\s*([.a-zA-Z_][a-zA-Z0-9$_.]*)"), #global directive - re.compile("\s*\.(string|asciz|ascii|[1248]?byte|short|word|long|quad|value|zero)"), - ] - keep_regexes = [ - - ] - fn_label_def = re.compile("^[a-zA-Z_][a-zA-Z0-9_.]*:") - for l in asm.splitlines(): - # Remove Mach-O attribute - l = l.replace('@GOTPCREL', '') - add_line = True - for reg in discard_regexes: - if reg.match(l) is not None: - add_line = False - break - for reg in keep_regexes: - if reg.match(l) is not None: - add_line = True - break - if add_line: - if fn_label_def.match(l) and len(new_contents) != 0: - new_contents += '\n' - l = process_identifiers(l) - new_contents += l - new_contents += '\n' - return new_contents - -def main(): - parser = ArgumentParser( - description='generate a stripped assembly file') - parser.add_argument( - 'input', metavar='input', type=str, nargs=1, - help='An input assembly file') - parser.add_argument( - 'out', metavar='output', type=str, nargs=1, - help='The output file') - args, unknown_args = parser.parse_known_args() - input = args.input[0] - output = args.out[0] - if not os.path.isfile(input): - print(("ERROR: input file '%s' does not exist") % input) - sys.exit(1) - contents = None - with open(input, 'r') as f: - contents = f.read() - new_contents = process_asm(contents) - with open(output, 'w') as f: - f.write(new_contents) - - -if __name__ == '__main__': - main() - -# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 -# kate: tab-width: 4; replace-tabs on; indent-width 4; tab-indents: off; -# kate: indent-mode python; remove-trailing-spaces modified; diff --git a/cmake/ci.cmake b/cmake/ci.cmake new file mode 100644 index 0000000000..5085c369dd --- /dev/null +++ b/cmake/ci.cmake @@ -0,0 +1,794 @@ +# number of parallel jobs for CTest +set(N 10) + +############################################################################### +# Needed tools. +############################################################################### + +include(FindPython3) +find_package(Python3 COMPONENTS Interpreter) + +find_program(ASTYLE_TOOL NAMES astyle) +execute_process(COMMAND ${ASTYLE_TOOL} --version OUTPUT_VARIABLE ASTYLE_TOOL_VERSION ERROR_VARIABLE ASTYLE_TOOL_VERSION) +string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" ASTYLE_TOOL_VERSION "${ASTYLE_TOOL_VERSION}") +message(STATUS "🔖 Artistic Style ${ASTYLE_TOOL_VERSION} (${ASTYLE_TOOL})") + +find_program(CLANG_TOOL NAMES clang++-HEAD clang++-11 clang++) +execute_process(COMMAND ${CLANG_TOOL} --version OUTPUT_VARIABLE CLANG_TOOL_VERSION ERROR_VARIABLE CLANG_TOOL_VERSION) +string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" CLANG_TOOL_VERSION "${CLANG_TOOL_VERSION}") +message(STATUS "🔖 Clang ${CLANG_TOOL_VERSION} (${CLANG_TOOL})") + +find_program(CLANG_TIDY_TOOL NAMES clang-tidy-11 clang-tidy) +execute_process(COMMAND ${CLANG_TIDY_TOOL} --version OUTPUT_VARIABLE CLANG_TIDY_TOOL_VERSION ERROR_VARIABLE CLANG_TIDY_TOOL_VERSION) +string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" CLANG_TIDY_TOOL_VERSION "${CLANG_TIDY_TOOL_VERSION}") +message(STATUS "🔖 Clang-Tidy ${CLANG_TIDY_TOOL_VERSION} (${CLANG_TIDY_TOOL})") + +message(STATUS "🔖 CMake ${CMAKE_VERSION} (${CMAKE_COMMAND})") + +find_program(CPPCHECK_TOOL NAMES cppcheck) +execute_process(COMMAND ${CPPCHECK_TOOL} --version OUTPUT_VARIABLE CPPCHECK_TOOL_VERSION ERROR_VARIABLE CPPCHECK_TOOL_VERSION) +string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" CPPCHECK_TOOL_VERSION "${CPPCHECK_TOOL_VERSION}") +message(STATUS "🔖 Cppcheck ${CPPCHECK_TOOL_VERSION} (${CPPCHECK_TOOL})") + +find_program(GCC_TOOL NAMES g++-HEAD g++-11 g++-10 g++) +execute_process(COMMAND ${GCC_TOOL} --version OUTPUT_VARIABLE GCC_TOOL_VERSION ERROR_VARIABLE GCC_TOOL_VERSION) +string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" GCC_TOOL_VERSION "${GCC_TOOL_VERSION}") +message(STATUS "🔖 GCC ${GCC_TOOL_VERSION} (${GCC_TOOL})") + +find_program(GCOV_TOOL NAMES gcov-HEAD gcov-11 gcov-10 gcov) +execute_process(COMMAND ${GCOV_TOOL} --version OUTPUT_VARIABLE GCOV_TOOL_VERSION ERROR_VARIABLE GCOV_TOOL_VERSION) +string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" GCOV_TOOL_VERSION "${GCOV_TOOL_VERSION}") +message(STATUS "🔖 GCOV ${GCOV_TOOL_VERSION} (${GCOV_TOOL})") + +find_program(GIT_TOOL NAMES git) +execute_process(COMMAND ${GIT_TOOL} --version OUTPUT_VARIABLE GIT_TOOL_VERSION ERROR_VARIABLE GIT_TOOL_VERSION) +string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" GIT_TOOL_VERSION "${GIT_TOOL_VERSION}") +message(STATUS "🔖 Git ${GIT_TOOL_VERSION} (${GIT_TOOL})") + +find_program(IWYU_TOOL NAMES include-what-you-use iwyu) +execute_process(COMMAND ${IWYU_TOOL} --version OUTPUT_VARIABLE IWYU_TOOL_VERSION ERROR_VARIABLE IWYU_TOOL_VERSION) +string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" IWYU_TOOL_VERSION "${IWYU_TOOL_VERSION}") +message(STATUS "🔖 include-what-you-use ${IWYU_TOOL_VERSION} (${IWYU_TOOL})") + +find_program(INFER_TOOL NAMES infer) +execute_process(COMMAND ${INFER_TOOL} --version OUTPUT_VARIABLE INFER_TOOL_VERSION ERROR_VARIABLE INFER_TOOL_VERSION) +string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" INFER_TOOL_VERSION "${INFER_TOOL_VERSION}") +message(STATUS "🔖 Infer ${INFER_TOOL_VERSION} (${INFER_TOOL})") + +find_program(LCOV_TOOL NAMES lcov) +execute_process(COMMAND ${LCOV_TOOL} --version OUTPUT_VARIABLE LCOV_TOOL_VERSION ERROR_VARIABLE LCOV_TOOL_VERSION) +string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" LCOV_TOOL_VERSION "${LCOV_TOOL_VERSION}") +message(STATUS "🔖 LCOV ${LCOV_TOOL_VERSION} (${LCOV_TOOL})") + +find_program(NINJA_TOOL NAMES ninja) +execute_process(COMMAND ${NINJA_TOOL} --version OUTPUT_VARIABLE NINJA_TOOL_VERSION ERROR_VARIABLE NINJA_TOOL_VERSION) +string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" NINJA_TOOL_VERSION "${NINJA_TOOL_VERSION}") +message(STATUS "🔖 Ninja ${NINJA_TOOL_VERSION} (${NINJA_TOOL})") + +find_program(OCLINT_TOOL NAMES oclint-json-compilation-database) +find_program(OCLINT_VERSION_TOOL NAMES oclint) +execute_process(COMMAND ${OCLINT_VERSION_TOOL} --version OUTPUT_VARIABLE OCLINT_TOOL_VERSION ERROR_VARIABLE OCLINT_TOOL_VERSION) +string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" OCLINT_TOOL_VERSION "${OCLINT_TOOL_VERSION}") +message(STATUS "🔖 OCLint ${OCLINT_TOOL_VERSION} (${OCLINT_TOOL})") + +find_program(VALGRIND_TOOL NAMES valgrind) +execute_process(COMMAND ${VALGRIND_TOOL} --version OUTPUT_VARIABLE VALGRIND_TOOL_VERSION ERROR_VARIABLE VALGRIND_TOOL_VERSION) +string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" VALGRIND_TOOL_VERSION "${VALGRIND_TOOL_VERSION}") +message(STATUS "🔖 Valgrind ${VALGRIND_TOOL_VERSION} (${VALGRIND_TOOL})") + +find_program(GENHTML_TOOL NAMES genhtml) +find_program(PLOG_CONVERTER_TOOL NAMES plog-converter) +find_program(PVS_STUDIO_ANALYZER_TOOL NAMES pvs-studio-analyzer) +find_program(SCAN_BUILD_TOOL NAMES scan-build-11 scan-build) + +# the individual source files +file(GLOB_RECURSE SRC_FILES ${PROJECT_SOURCE_DIR}/include/nlohmann/*.hpp) + +############################################################################### +# Different C++ Standards. +############################################################################### + +set(CLANG_CXXFLAGS "-std=c++11 \ + -Werror \ + -Weverything \ + -Wno-c++98-compat \ + -Wno-c++98-compat-pedantic \ + -Wno-deprecated-declarations \ + -Wno-documentation-unknown-command \ + -Wno-exit-time-destructors \ + -Wno-extra-semi-stmt \ + -Wno-padded \ + -Wno-range-loop-analysis \ + -Wno-switch-enum -Wno-covered-switch-default \ + -Wno-weak-vtables \ +") + +set(GCC_CXXFLAGS "-std=c++11 \ + -pedantic \ + -Werror \ + --all-warnings \ + --extra-warnings \ + -W \ + -WNSObject-attribute \ + -Wno-abi-tag \ + -Waddress \ + -Waddress-of-packed-member \ + -Wno-aggregate-return \ + -Waggressive-loop-optimizations \ + -Waligned-new=all \ + -Wall \ + -Walloc-zero \ + -Walloca \ + -Wanalyzer-double-fclose \ + -Wanalyzer-double-free \ + -Wanalyzer-exposure-through-output-file \ + -Wanalyzer-file-leak \ + -Wanalyzer-free-of-non-heap \ + -Wanalyzer-malloc-leak \ + -Wanalyzer-mismatching-deallocation \ + -Wanalyzer-null-argument \ + -Wanalyzer-null-dereference \ + -Wanalyzer-possible-null-argument \ + -Wanalyzer-possible-null-dereference \ + -Wanalyzer-shift-count-negative \ + -Wanalyzer-shift-count-overflow \ + -Wanalyzer-stale-setjmp-buffer \ + -Wanalyzer-tainted-array-index \ + -Wanalyzer-too-complex \ + -Wanalyzer-unsafe-call-within-signal-handler \ + -Wanalyzer-use-after-free \ + -Wanalyzer-use-of-pointer-in-stale-stack-frame \ + -Wanalyzer-write-to-const \ + -Wanalyzer-write-to-string-literal \ + -Warith-conversion \ + -Warray-bounds \ + -Warray-bounds=2 \ + -Warray-parameter=2 \ + -Wattribute-alias=2 \ + -Wattribute-warning \ + -Wattributes \ + -Wbool-compare \ + -Wbool-operation \ + -Wbuiltin-declaration-mismatch \ + -Wbuiltin-macro-redefined \ + -Wc++0x-compat \ + -Wc++11-compat \ + -Wc++14-compat \ + -Wc++17-compat \ + -Wc++1z-compat \ + -Wc++20-compat \ + -Wc++2a-compat \ + -Wcannot-profile \ + -Wcast-align \ + -Wcast-align=strict \ + -Wcast-function-type \ + -Wcast-qual \ + -Wcatch-value=3 \ + -Wchar-subscripts \ + -Wclass-conversion \ + -Wclass-memaccess \ + -Wclobbered \ + -Wcomma-subscript \ + -Wcomment \ + -Wcomments \ + -Wconditionally-supported \ + -Wconversion \ + -Wconversion-null \ + -Wcoverage-mismatch \ + -Wcpp \ + -Wctad-maybe-unsupported \ + -Wctor-dtor-privacy \ + -Wdangling-else \ + -Wdate-time \ + -Wdelete-incomplete \ + -Wdelete-non-virtual-dtor \ + -Wdeprecated \ + -Wdeprecated-copy \ + -Wdeprecated-copy-dtor \ + -Wdeprecated-declarations \ + -Wdeprecated-enum-enum-conversion \ + -Wdeprecated-enum-float-conversion \ + -Wdisabled-optimization \ + -Wdiv-by-zero \ + -Wdouble-promotion \ + -Wduplicated-branches \ + -Wduplicated-cond \ + -Weffc++ \ + -Wempty-body \ + -Wendif-labels \ + -Wenum-compare \ + -Wenum-conversion \ + -Wexpansion-to-defined \ + -Wextra \ + -Wextra-semi \ + -Wfloat-conversion \ + -Wfloat-equal \ + -Wformat-diag \ + -Wformat-overflow=2 \ + -Wformat-signedness \ + -Wformat-truncation=2 \ + -Wformat=2 \ + -Wframe-address \ + -Wfree-nonheap-object \ + -Whsa \ + -Wif-not-aligned \ + -Wignored-attributes \ + -Wignored-qualifiers \ + -Wimplicit-fallthrough=5 \ + -Winaccessible-base \ + -Winherited-variadic-ctor \ + -Winit-list-lifetime \ + -Winit-self \ + -Winline \ + -Wint-in-bool-context \ + -Wint-to-pointer-cast \ + -Winvalid-memory-model \ + -Winvalid-offsetof \ + -Winvalid-pch \ + -Wliteral-suffix \ + -Wlogical-not-parentheses \ + -Wlogical-op \ + -Wno-long-long \ + -Wlto-type-mismatch \ + -Wmain \ + -Wmaybe-uninitialized \ + -Wmemset-elt-size \ + -Wmemset-transposed-args \ + -Wmisleading-indentation \ + -Wmismatched-dealloc \ + -Wmismatched-new-delete \ + -Wmismatched-tags \ + -Wmissing-attributes \ + -Wmissing-braces \ + -Wmissing-declarations \ + -Wmissing-field-initializers \ + -Wmissing-include-dirs \ + -Wmissing-profile \ + -Wmultichar \ + -Wmultiple-inheritance \ + -Wmultistatement-macros \ + -Wno-namespaces \ + -Wnarrowing \ + -Wno-noexcept \ + -Wnoexcept-type \ + -Wnon-template-friend \ + -Wnon-virtual-dtor \ + -Wnonnull \ + -Wnonnull-compare \ + -Wnormalized=nfkc \ + -Wnull-dereference \ + -Wodr \ + -Wold-style-cast \ + -Wopenmp-simd \ + -Woverflow \ + -Woverlength-strings \ + -Woverloaded-virtual \ + -Wpacked \ + -Wpacked-bitfield-compat \ + -Wpacked-not-aligned \ + -Wno-padded \ + -Wparentheses \ + -Wpedantic \ + -Wpessimizing-move \ + -Wplacement-new=2 \ + -Wpmf-conversions \ + -Wpointer-arith \ + -Wpointer-compare \ + -Wpragmas \ + -Wprio-ctor-dtor \ + -Wpsabi \ + -Wno-range-loop-construct \ + -Wredundant-decls \ + -Wredundant-move \ + -Wredundant-tags \ + -Wregister \ + -Wreorder \ + -Wrestrict \ + -Wreturn-local-addr \ + -Wreturn-type \ + -Wscalar-storage-order \ + -Wsequence-point \ + -Wshadow=compatible-local \ + -Wshadow=global \ + -Wshadow=local \ + -Wshift-count-negative \ + -Wshift-count-overflow \ + -Wshift-negative-value \ + -Wshift-overflow=2 \ + -Wsign-compare \ + -Wsign-conversion \ + -Wsign-promo \ + -Wsized-deallocation \ + -Wsizeof-array-argument \ + -Wsizeof-array-div \ + -Wsizeof-pointer-div \ + -Wsizeof-pointer-memaccess \ + -Wstack-protector \ + -Wstrict-aliasing \ + -Wstrict-aliasing=3 \ + -Wstrict-null-sentinel \ + -Wstrict-overflow \ + -Wstrict-overflow=5 \ + -Wstring-compare \ + -Wstringop-overflow=4 \ + -Wstringop-overread \ + -Wstringop-truncation \ + -Wsubobject-linkage \ + -Wsuggest-attribute=cold \ + -Wsuggest-attribute=const \ + -Wsuggest-attribute=format \ + -Wsuggest-attribute=malloc \ + -Wsuggest-attribute=noreturn \ + -Wsuggest-attribute=pure \ + -Wsuggest-final-methods \ + -Wsuggest-final-types \ + -Wsuggest-override \ + -Wswitch \ + -Wswitch-bool \ + -Wswitch-default \ + -Wno-switch-enum \ + -Wswitch-outside-range \ + -Wswitch-unreachable \ + -Wsync-nand \ + -Wsynth \ + -Wno-system-headers \ + -Wtautological-compare \ + -Wno-templates \ + -Wterminate \ + -Wtrampolines \ + -Wtrigraphs \ + -Wtsan \ + -Wtype-limits \ + -Wundef \ + -Wuninitialized \ + -Wunknown-pragmas \ + -Wunreachable-code \ + -Wunsafe-loop-optimizations \ + -Wunused \ + -Wunused-but-set-parameter \ + -Wunused-but-set-variable \ + -Wunused-const-variable=2 \ + -Wunused-function \ + -Wunused-label \ + -Wunused-local-typedefs \ + -Wunused-macros \ + -Wunused-parameter \ + -Wunused-result \ + -Wunused-value \ + -Wunused-variable \ + -Wno-useless-cast \ + -Wvarargs \ + -Wvariadic-macros \ + -Wvector-operation-performance \ + -Wvexing-parse \ + -Wvirtual-inheritance \ + -Wvirtual-move-assign \ + -Wvla \ + -Wvla-parameter \ + -Wvolatile \ + -Wvolatile-register-var \ + -Wwrite-strings \ + -Wzero-as-null-pointer-constant \ + -Wzero-length-bounds \ +") + +add_custom_target(ci_test_gcc + COMMAND CXX=${GCC_TOOL} CXXFLAGS=${GCC_CXXFLAGS} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DJSON_BuildTests=ON -DJSON_MultipleHeaders=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_gcc + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_gcc + COMMAND cd ${PROJECT_BINARY_DIR}/build_gcc && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure + COMMENT "Compile and test with GCC using maximal warning flags" +) + +add_custom_target(ci_test_clang + COMMAND CXX=${CLANG_TOOL} CXXFLAGS=${CLANG_CXXFLAGS} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DJSON_BuildTests=ON -DJSON_MultipleHeaders=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_clang + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_clang + COMMAND cd ${PROJECT_BINARY_DIR}/build_clang && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure + COMMENT "Compile and test with Clang using maximal warning flags" +) + +############################################################################### +# Different C++ Standards. +############################################################################### + +foreach(CXX_STANDARD 11 14 17 20) + add_custom_target(ci_test_gcc_cxx${CXX_STANDARD} + COMMAND CXX=${GCC_TOOL} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DCMAKE_CXX_STANDARD=${CXX_STANDARD} -DCMAKE_CXX_STANDARD_REQUIRED=ON + -DJSON_BuildTests=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_gcc_cxx${CXX_STANDARD} + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_gcc_cxx${CXX_STANDARD} + COMMAND cd ${PROJECT_BINARY_DIR}/build_gcc_cxx${CXX_STANDARD} && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure + COMMENT "Compile and test with GCC for C++${CXX_STANDARD}" + ) + + add_custom_target(ci_test_clang_cxx${CXX_STANDARD} + COMMAND CXX=${CLANG_TOOL} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DCMAKE_CXX_STANDARD=${CXX_STANDARD} -DCMAKE_CXX_STANDARD_REQUIRED=ON + -DJSON_BuildTests=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_clang_cxx${CXX_STANDARD} + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_clang_cxx${CXX_STANDARD} + COMMAND cd ${PROJECT_BINARY_DIR}/build_clang_cxx${CXX_STANDARD} && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure + COMMENT "Compile and test with Clang for C++${CXX_STANDARD}" + ) +endforeach() + +############################################################################### +# Disable exceptions. +############################################################################### + +add_custom_target(ci_test_noexceptions + COMMAND CXX=${CLANG_TOOL} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DJSON_BuildTests=ON -DJSON_MultipleHeaders=ON -DCMAKE_CXX_FLAGS=-DJSON_NOEXCEPTION -DDOCTEST_TEST_FILTER=--no-throw + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_noexceptions + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_noexceptions + COMMAND cd ${PROJECT_BINARY_DIR}/build_noexceptions && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure + COMMENT "Compile and test with exceptions switched off" +) + +############################################################################### +# Disable implicit conversions. +############################################################################### + +add_custom_target(ci_test_noimplicitconversions + COMMAND CXX=${CLANG_TOOL} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DJSON_BuildTests=ON -DJSON_MultipleHeaders=ON -DJSON_ImplicitConversions=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_noimplicitconversions + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_noimplicitconversions + COMMAND cd ${PROJECT_BINARY_DIR}/build_noimplicitconversions && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure + COMMENT "Compile and test with implicit conversions switched off" +) + +############################################################################### +# Enable improved diagnostics. +############################################################################### + +add_custom_target(ci_test_diagnostics + COMMAND CXX=${CLANG_TOOL} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DJSON_BuildTests=ON -DJSON_MultipleHeaders=ON -DJSON_Diagnostics=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_diagnostics + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_diagnostics + COMMAND cd ${PROJECT_BINARY_DIR}/build_diagnostics && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure + COMMENT "Compile and test with improved diagnostics enabled" +) + +############################################################################### +# Coverage. +############################################################################### + +add_custom_target(ci_test_coverage + COMMAND CXX=${GCC_TOOL} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja -DCMAKE_CXX_FLAGS="--coverage;-fprofile-arcs;-ftest-coverage" + -DJSON_BuildTests=ON -DJSON_MultipleHeaders=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_coverage + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_coverage + COMMAND cd ${PROJECT_BINARY_DIR}/build_coverage && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure + + COMMAND ${LCOV_TOOL} --directory . --capture --output-file json.info --rc lcov_branch_coverage=1 + COMMAND ${LCOV_TOOL} -e json.info ${SRC_FILES} --output-file json.info.filtered --gcov-tool ${GCOV_TOOL} --rc lcov_branch_coverage=1 + COMMAND ${CMAKE_SOURCE_DIR}/test/thirdparty/imapdl/filterbr.py json.info.filtered > json.info.filtered.noexcept + COMMAND genhtml --title "JSON for Modern C++" --legend --demangle-cpp --output-directory html --show-details --branch-coverage json.info.filtered.noexcept + + COMMENT "Compile and test with coverage" +) + +############################################################################### +# Sanitizers. +############################################################################### + +set(CLANG_CXX_FLAGS_SANITIZER "-g -O1 -fsanitize=address -fsanitize=undefined -fsanitize=integer -fsanitize=nullability -fno-omit-frame-pointer -fno-sanitize-recover=all -fsanitize-recover=unsigned-integer-overflow") + +add_custom_target(ci_test_clang_sanitizer + COMMAND CXX=${CLANG_TOOL} CXXFLAGS=${CLANG_CXX_FLAGS_SANITIZER} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DJSON_BuildTests=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_clang_sanitizer + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_clang_sanitizer + COMMAND cd ${PROJECT_BINARY_DIR}/build_clang_sanitizer && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure + COMMENT "Compile and test with sanitizers" +) + +############################################################################### +# Check if header is amalgamated and sources are properly indented. +############################################################################### + +set(ASTYLE_FLAGS --style=allman --indent=spaces=4 --indent-modifiers --indent-switches --indent-preproc-block --indent-preproc-define --indent-col1-comments --pad-oper --pad-header --align-pointer=type --align-reference=type --add-brackets --convert-tabs --close-templates --lineend=linux --preserve-date --formatted) + +file(GLOB_RECURSE INDENT_FILES + ${PROJECT_SOURCE_DIR}/include/nlohmann/*.hpp + ${PROJECT_SOURCE_DIR}/test/src/*.cpp + ${PROJECT_SOURCE_DIR}/test/src/*.hpp + ${PROJECT_SOURCE_DIR}/benchmarks/src/benchmarks.cpp + ${PROJECT_SOURCE_DIR}/doc/examples/*.cpp +) + +add_custom_target(ci_test_amalgamation + COMMAND rm -fr ${PROJECT_SOURCE_DIR}/single_include/nlohmann/json.hpp~ + COMMAND cp ${PROJECT_SOURCE_DIR}/single_include/nlohmann/json.hpp ${PROJECT_SOURCE_DIR}/single_include/nlohmann/json.hpp~ + COMMAND ${Python3_EXECUTABLE} ${PROJECT_SOURCE_DIR}/third_party/amalgamate/amalgamate.py -c ${PROJECT_SOURCE_DIR}/third_party/amalgamate/config.json -s . + COMMAND ${ASTYLE_TOOL} ${ASTYLE_FLAGS} --suffix=none --quiet ${PROJECT_SOURCE_DIR}/single_include/nlohmann/json.hpp + COMMAND diff ${PROJECT_SOURCE_DIR}/single_include/nlohmann/json.hpp~ ${PROJECT_SOURCE_DIR}/single_include/nlohmann/json.hpp + + COMMAND ${ASTYLE_TOOL} ${ASTYLE_FLAGS} ${INDENT_FILES} + COMMAND cd ${PROJECT_SOURCE_DIR} && for FILE in `find . -name '*.orig'`\; do false \; done + + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMENT "Check amalagamation and indentation" +) + +############################################################################### +# Valgrind. +############################################################################### + +add_custom_target(ci_test_valgrind + COMMAND CXX=${GCC_TOOL} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DJSON_BuildTests=ON -DJSON_Valgrind=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_valgrind + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_valgrind + COMMAND cd ${PROJECT_BINARY_DIR}/build_valgrind && ${CMAKE_CTEST_COMMAND} -L valgrind --parallel ${N} --output-on-failure + COMMENT "Compile and test with Valgrind" +) + +############################################################################### +# Check code with Clang Static Analyzer. +############################################################################### + +set(CLANG_ANALYZER_CHECKS "fuchsia.HandleChecker,nullability.NullableDereferenced,nullability.NullablePassedToNonnull,nullability.NullableReturnedFromNonnull,optin.cplusplus.UninitializedObject,optin.cplusplus.VirtualCall,optin.mpi.MPI-Checker,optin.osx.OSObjectCStyleCast,optin.osx.cocoa.localizability.EmptyLocalizationContextChecker,optin.osx.cocoa.localizability.NonLocalizedStringChecker,optin.performance.GCDAntipattern,optin.performance.Padding,optin.portability.UnixAPI,security.FloatLoopCounter,security.insecureAPI.DeprecatedOrUnsafeBufferHandling,security.insecureAPI.bcmp,security.insecureAPI.bcopy,security.insecureAPI.bzero,security.insecureAPI.rand,security.insecureAPI.strcpy,valist.CopyToSelf,valist.Uninitialized,valist.Unterminated,webkit.NoUncountedMemberChecker,webkit.RefCntblBaseVirtualDtor,core.CallAndMessage,core.DivideZero,core.NonNullParamChecker,core.NullDereference,core.StackAddressEscape,core.UndefinedBinaryOperatorResult,core.VLASize,core.uninitialized.ArraySubscript,core.uninitialized.Assign,core.uninitialized.Branch,core.uninitialized.CapturedBlockVariable,core.uninitialized.UndefReturn,cplusplus.InnerPointer,cplusplus.Move,cplusplus.NewDelete,cplusplus.NewDeleteLeaks,cplusplus.PlacementNew,cplusplus.PureVirtualCall,deadcode.DeadStores,nullability.NullPassedToNonnull,nullability.NullReturnedFromNonnull,osx.API,osx.MIG,osx.NumberObjectConversion,osx.OSObjectRetainCount,osx.ObjCProperty,osx.SecKeychainAPI,osx.cocoa.AtSync,osx.cocoa.AutoreleaseWrite,osx.cocoa.ClassRelease,osx.cocoa.Dealloc,osx.cocoa.IncompatibleMethodTypes,osx.cocoa.Loops,osx.cocoa.MissingSuperCall,osx.cocoa.NSAutoreleasePool,osx.cocoa.NSError,osx.cocoa.NilArg,osx.cocoa.NonNilReturnValue,osx.cocoa.ObjCGenerics,osx.cocoa.RetainCount,osx.cocoa.RunLoopAutoreleaseLeak,osx.cocoa.SelfInit,osx.cocoa.SuperDealloc,osx.cocoa.UnusedIvars,osx.cocoa.VariadicMethodTypes,osx.coreFoundation.CFError,osx.coreFoundation.CFNumber,osx.coreFoundation.CFRetainRelease,osx.coreFoundation.containers.OutOfBounds,osx.coreFoundation.containers.PointerSizedValues,security.insecureAPI.UncheckedReturn,security.insecureAPI.decodeValueOfObjCType,security.insecureAPI.getpw,security.insecureAPI.gets,security.insecureAPI.mkstemp,security.insecureAPI.mktemp,security.insecureAPI.vfork,unix.API,unix.Malloc,unix.MallocSizeof,unix.MismatchedDeallocator,unix.Vfork,unix.cstring.BadSizeArg,unix.cstring.NullArg") + +add_custom_target(ci_clang_analyze + COMMAND CXX=${CLANG_TOOL} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DJSON_BuildTests=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_clang_analyze + COMMAND cd ${PROJECT_BINARY_DIR}/build_clang_analyze && ${SCAN_BUILD_TOOL} -enable-checker ${CLANG_ANALYZER_CHECKS} --use-c++=${CLANG_TOOL} -analyze-headers -o ${PROJECT_BINARY_DIR}/report ninja + COMMENT "Check code with Clang Analyzer" +) + +############################################################################### +# Check code with Cppcheck. +############################################################################### + +add_custom_target(ci_cppcheck + COMMAND ${CPPCHECK_TOOL} --enable=warning --inline-suppr --inconclusive --force --std=c++11 ${PROJECT_SOURCE_DIR}/single_include/nlohmann/json.hpp --error-exitcode=1 + COMMENT "Check code with Cppcheck" +) + +############################################################################### +# Check code with cpplint. +############################################################################### + +add_custom_target(ci_cpplint + COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/third_party/cpplint/cpplint.py --filter=-whitespace,-legal,-runtime/references,-runtime/explicit,-runtime/indentation_namespace,-readability/casting,-readability/nolint --quiet --recursive ${SRC_FILES} + COMMENT "Check code with cpplint" +) + +############################################################################### +# Check code with OCLint. +############################################################################### + +file(COPY ${PROJECT_SOURCE_DIR}/single_include/nlohmann/json.hpp DESTINATION ${PROJECT_BINARY_DIR}/src_single) +file(RENAME ${PROJECT_BINARY_DIR}/src_single/json.hpp ${PROJECT_BINARY_DIR}/src_single/all.cpp) +file(APPEND "${PROJECT_BINARY_DIR}/src_single/all.cpp" "\n\nint main()\n{}\n") + +add_executable(single_all ${PROJECT_BINARY_DIR}/src_single/all.cpp) +target_compile_features(single_all PRIVATE cxx_std_11) + +add_custom_target(ci_oclint + COMMAND ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + -DJSON_BuildTests=OFF -DJSON_CI=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_oclint + COMMAND ${OCLINT_TOOL} -i ${PROJECT_BINARY_DIR}/build_oclint/src_single/all.cpp -p ${PROJECT_BINARY_DIR}/build_oclint -- + -report-type html -enable-global-analysis --max-priority-1=0 --max-priority-2=1000 --max-priority-3=2000 + --disable-rule=MultipleUnaryOperator + --disable-rule=DoubleNegative + --disable-rule=ShortVariableName + --disable-rule=GotoStatement + --disable-rule=LongLine + -o ${PROJECT_BINARY_DIR}/build_oclint/oclint_report.html + COMMENT "Check code with OCLint" +) + +############################################################################### +# Check code with Clang-Tidy. +############################################################################### + +add_custom_target(ci_clang_tidy + COMMAND CXX=${CLANG_TOOL} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_CXX_CLANG_TIDY=${CLANG_TIDY_TOOL} + -DJSON_BuildTests=ON -DJSON_MultipleHeaders=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_clang_tidy + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_clang_tidy + COMMENT "Check code with Clang-Tidy" +) + +############################################################################### +# Check code with PVS-Studio Analyzer . +############################################################################### + +add_custom_target(ci_pvs_studio + COMMAND CXX=${CLANG_TOOL} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + -DJSON_BuildTests=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_pvs_studio + COMMAND cd ${PROJECT_BINARY_DIR}/build_pvs_studio && ${PVS_STUDIO_ANALYZER_TOOL} analyze -j 10 + COMMAND cd ${PROJECT_BINARY_DIR}/build_pvs_studio && ${PLOG_CONVERTER_TOOL} -a'GA:1,2;64:1;CS' -t fullhtml PVS-Studio.log -o pvs + COMMENT "Check code with PVS Studio" +) + +############################################################################### +# Check code with Infer static analyzer. +############################################################################### + +add_custom_target(ci_infer + COMMAND mkdir -p ${PROJECT_BINARY_DIR}/build_infer + COMMAND cd ${PROJECT_BINARY_DIR}/build_infer && ${INFER_TOOL} compile -- ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${PROJECT_SOURCE_DIR} -DJSON_BuildTests=ON -DJSON_MultipleHeaders=ON + COMMAND cd ${PROJECT_BINARY_DIR}/build_infer && ${INFER_TOOL} run -- make --parallel ${N} + COMMENT "Check code with Infer" +) + +############################################################################### +# Run test suite with previously downloaded test data. +############################################################################### + +add_custom_target(ci_offline_testdata + COMMAND mkdir -p ${PROJECT_BINARY_DIR}/build_offline_testdata/test_data + COMMAND cd ${PROJECT_BINARY_DIR}/build_offline_testdata/test_data && ${GIT_TOOL} clone -c advice.detachedHead=false --branch v3.0.0 https://github.com/nlohmann/json_test_data.git --quiet --depth 1 + COMMAND ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DJSON_BuildTests=ON -DJSON_TestDataDirectory=${PROJECT_BINARY_DIR}/build_offline_testdata/test_data/json_test_data + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_offline_testdata + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_offline_testdata + COMMAND cd ${PROJECT_BINARY_DIR}/build_offline_testdata && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure + COMMENT "Check code with previously downloaded test data" +) + +############################################################################### +# Run test suite when project was not checked out from Git +############################################################################### + +add_custom_target(ci_non_git_tests + COMMAND mkdir -p ${PROJECT_BINARY_DIR}/build_non_git_tests/sources + COMMAND cd ${PROJECT_SOURCE_DIR} && for FILE in `${GIT_TOOL} ls-tree --name-only HEAD`\; do cp -r $$FILE ${PROJECT_BINARY_DIR}/build_non_git_tests/sources \; done + COMMAND ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DJSON_BuildTests=ON + -S${PROJECT_BINARY_DIR}/build_non_git_tests/sources -B${PROJECT_BINARY_DIR}/build_non_git_tests + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_non_git_tests + COMMAND cd ${PROJECT_BINARY_DIR}/build_non_git_tests && ${CMAKE_CTEST_COMMAND} --parallel ${N} -LE git_required --output-on-failure + COMMENT "Check code when project was not checked out from Git" +) + +############################################################################### +# Run test suite and exclude tests that change installed files +############################################################################### + +add_custom_target(ci_reproducible_tests + COMMAND ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DJSON_BuildTests=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_reproducible_tests + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_reproducible_tests + COMMAND cd ${PROJECT_BINARY_DIR}/build_reproducible_tests && ${CMAKE_CTEST_COMMAND} --parallel ${N} -LE not_reproducible --output-on-failure + COMMENT "Check code and exclude tests that change installed files" +) + +############################################################################### +# Check if every header in the include folder includes sufficient headers to +# be compiled individually. +############################################################################### + +set(iwyu_path_and_options ${IWYU_TOOL} -Xiwyu --max_line_length=300) + +foreach(SRC_FILE ${SRC_FILES}) + # get relative path of the header file + file(RELATIVE_PATH RELATIVE_SRC_FILE "${PROJECT_SOURCE_DIR}/include/nlohmann" "${SRC_FILE}") + # replace slashes and strip suffix + string(REPLACE "/" "_" RELATIVE_SRC_FILE "${RELATIVE_SRC_FILE}") + string(REPLACE ".hpp" "" RELATIVE_SRC_FILE "${RELATIVE_SRC_FILE}") + # create code file + file(WRITE "${PROJECT_BINARY_DIR}/src_single/${RELATIVE_SRC_FILE}.cpp" "#include \"${SRC_FILE}\" // IWYU pragma: keep\n\nint main()\n{}\n") + # create executable + add_executable(single_${RELATIVE_SRC_FILE} EXCLUDE_FROM_ALL ${PROJECT_BINARY_DIR}/src_single/${RELATIVE_SRC_FILE}.cpp) + target_include_directories(single_${RELATIVE_SRC_FILE} PRIVATE ${PROJECT_SOURCE_DIR}/include) + target_compile_features(single_${RELATIVE_SRC_FILE} PRIVATE cxx_std_11) + set_property(TARGET single_${RELATIVE_SRC_FILE} PROPERTY CXX_INCLUDE_WHAT_YOU_USE "${iwyu_path_and_options}") + # remember binary for ci_single_binaries target + list(APPEND single_binaries single_${RELATIVE_SRC_FILE}) +endforeach() + +add_custom_target(ci_single_binaries + DEPENDS ${single_binaries} + COMMENT "Check if headers are self-contained" +) + +############################################################################### +# Benchmarks +############################################################################### + +add_custom_target(ci_benchmarks + COMMAND ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Release -GNinja + -S${PROJECT_SOURCE_DIR}/benchmarks -B${PROJECT_BINARY_DIR}/build_benchmarks + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_benchmarks --target json_benchmarks + COMMAND cd ${PROJECT_BINARY_DIR}/build_benchmarks && ./json_benchmarks + COMMENT "Run benchmarks" +) + +############################################################################### +# CMake flags +############################################################################### + +if (APPLE) + set(CMAKE_310_BINARY ${PROJECT_BINARY_DIR}/cmake-3.1.0-Darwin64/CMake.app/Contents/bin/cmake) + add_custom_command( + OUTPUT ${CMAKE_310_BINARY} + COMMAND wget https://github.com/Kitware/CMake/releases/download/v3.1.0/cmake-3.1.0-Darwin64.tar.gz + COMMAND tar xfz cmake-3.1.0-Darwin64.tar.gz + COMMAND rm cmake-3.1.0-Darwin64.tar.gz + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} + COMMENT "Download CMake 3.1.0" + ) +else() + set(CMAKE_310_BINARY ${PROJECT_BINARY_DIR}/cmake-3.1.0-Linux-x86_64/bin/cmake) + add_custom_command( + OUTPUT ${CMAKE_310_BINARY} + COMMAND wget https://github.com/Kitware/CMake/releases/download/v3.1.0/cmake-3.1.0-Linux-x86_64.tar.gz + COMMAND tar xfz cmake-3.1.0-Linux-x86_64.tar.gz + COMMAND rm cmake-3.1.0-Linux-x86_64.tar.gz + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} + COMMENT "Download CMake 3.1.0" + ) +endif() + +set(JSON_CMAKE_FLAGS "JSON_BuildTests;JSON_Install;JSON_MultipleHeaders;JSON_Sanitizer;JSON_Valgrind;JSON_NoExceptions;JSON_Coverage;JSON_Diagnostics") + +foreach(JSON_CMAKE_FLAG ${JSON_CMAKE_FLAGS}) + string(TOLOWER "ci_cmake_flag_${JSON_CMAKE_FLAG}" JSON_CMAKE_FLAG_TARGET) + add_custom_target("${JSON_CMAKE_FLAG_TARGET}" + COMMENT "Check CMake flag ${JSON_CMAKE_FLAG} (CMake ${CMAKE_VERSION})" + COMMAND ${CMAKE_COMMAND} + -Werror=dev + -D${JSON_CMAKE_FLAG}=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_${JSON_CMAKE_FLAG_TARGET} + ) + add_custom_target("${JSON_CMAKE_FLAG_TARGET}_31" + COMMENT "Check CMake flag ${JSON_CMAKE_FLAG} (CMake 3.1)" + COMMAND mkdir ${PROJECT_BINARY_DIR}/build_${JSON_CMAKE_FLAG_TARGET}_31 + COMMAND cd ${PROJECT_BINARY_DIR}/build_${JSON_CMAKE_FLAG_TARGET}_31 && ${CMAKE_310_BINARY} + -Werror=dev ${PROJECT_SOURCE_DIR} + -D${JSON_CMAKE_FLAG}=ON + -DCMAKE_CXX_COMPILE_FEATURES="cxx_range_for" -DCMAKE_CXX_FLAGS="-std=gnu++11" + DEPENDS ${CMAKE_310_BINARY} + ) + list(APPEND JSON_CMAKE_FLAG_TARGETS ${JSON_CMAKE_FLAG_TARGET} ${JSON_CMAKE_FLAG_TARGET}_31) + list(APPEND JSON_CMAKE_FLAG_BUILD_DIRS ${PROJECT_BINARY_DIR}/build_${JSON_CMAKE_FLAG_TARGET} ${PROJECT_BINARY_DIR}/build_${JSON_CMAKE_FLAG_TARGET}_31) +endforeach() + +add_custom_target(ci_cmake_flags + DEPENDS ${JSON_CMAKE_FLAG_TARGETS} + COMMENT "Check CMake flags" +) + +############################################################################### +# Clean up all generated files. +############################################################################### + +add_custom_target(ci_clean + COMMAND rm -fr ${PROJECT_BINARY_DIR}/build_* cmake-3.1.0-Darwin64 ${JSON_CMAKE_FLAG_BUILD_DIRS} ${single_binaries} + COMMENT "Clean generated directories" +) diff --git a/include/nlohmann/byte_container_with_subtype.hpp b/include/nlohmann/byte_container_with_subtype.hpp index ee3ab4011b..df68395a25 100644 --- a/include/nlohmann/byte_container_with_subtype.hpp +++ b/include/nlohmann/byte_container_with_subtype.hpp @@ -73,9 +73,9 @@ class byte_container_with_subtype : public BinaryType @exceptionsafety No-throw guarantee: this member function never throws exceptions. - @sa @ref subtype() -- return the binary subtype - @sa @ref clear_subtype() -- clears the binary subtype - @sa @ref has_subtype() -- returns whether or not the binary value has a + @sa see @ref subtype() -- return the binary subtype + @sa see @ref clear_subtype() -- clears the binary subtype + @sa see @ref has_subtype() -- returns whether or not the binary value has a subtype @since version 3.8.0 @@ -100,9 +100,9 @@ class byte_container_with_subtype : public BinaryType @exceptionsafety No-throw guarantee: this member function never throws exceptions. - @sa @ref set_subtype() -- sets the binary subtype - @sa @ref clear_subtype() -- clears the binary subtype - @sa @ref has_subtype() -- returns whether or not the binary value has a + @sa see @ref set_subtype() -- sets the binary subtype + @sa see @ref clear_subtype() -- clears the binary subtype + @sa see @ref has_subtype() -- returns whether or not the binary value has a subtype @since version 3.8.0 @@ -122,9 +122,9 @@ class byte_container_with_subtype : public BinaryType @exceptionsafety No-throw guarantee: this member function never throws exceptions. - @sa @ref subtype() -- return the binary subtype - @sa @ref set_subtype() -- sets the binary subtype - @sa @ref clear_subtype() -- clears the binary subtype + @sa see @ref subtype() -- return the binary subtype + @sa see @ref set_subtype() -- sets the binary subtype + @sa see @ref clear_subtype() -- clears the binary subtype @since version 3.8.0 */ @@ -145,9 +145,9 @@ class byte_container_with_subtype : public BinaryType @exceptionsafety No-throw guarantee: this member function never throws exceptions. - @sa @ref subtype() -- return the binary subtype - @sa @ref set_subtype() -- sets the binary subtype - @sa @ref has_subtype() -- returns whether or not the binary value has a + @sa see @ref subtype() -- return the binary subtype + @sa see @ref set_subtype() -- sets the binary subtype + @sa see @ref has_subtype() -- returns whether or not the binary value has a subtype @since version 3.8.0 diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index f03c018159..6f8bd647b7 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -161,7 +161,7 @@ void from_json(const BasicJsonType& j, std::valarray& l) } template -auto from_json(const BasicJsonType& j, T (&arr)[N]) +auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) -> decltype(j.template get(), void()) { for (std::size_t i = 0; i < N; ++i) @@ -268,7 +268,7 @@ void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) } ConstructibleObjectType ret; - auto inner_object = j.template get_ptr(); + const auto* inner_object = j.template get_ptr(); using value_type = typename ConstructibleObjectType::value_type; std::transform( inner_object->begin(), inner_object->end(), @@ -395,8 +395,8 @@ struct from_json_fn /// namespace to hold default `from_json` function /// to see why this is required: /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html -namespace +namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces) { -constexpr const auto& from_json = detail::static_const::value; +constexpr const auto& from_json = detail::static_const::value; // NOLINT(misc-definitions-in-headers) } // namespace } // namespace nlohmann diff --git a/include/nlohmann/detail/conversions/to_chars.hpp b/include/nlohmann/detail/conversions/to_chars.hpp index 49ed0f913e..e904d10fac 100644 --- a/include/nlohmann/detail/conversions/to_chars.hpp +++ b/include/nlohmann/detail/conversions/to_chars.hpp @@ -200,7 +200,7 @@ boundaries compute_boundaries(FloatType value) using bits_type = typename std::conditional::type; - const std::uint64_t bits = reinterpret_bits(value); + const auto bits = static_cast(reinterpret_bits(value)); const std::uint64_t E = bits >> (kPrecision - 1); const std::uint64_t F = bits & (kHiddenBit - 1); @@ -618,7 +618,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, JSON_ASSERT(p1 > 0); - std::uint32_t pow10; + std::uint32_t pow10{}; const int k = find_largest_pow10(p1, pow10); // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1) diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index 228e81879e..9d7f55fc94 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -73,8 +73,7 @@ struct external_constructor static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b) { j.m_type = value_t::binary; - typename BasicJsonType::binary_t value{b}; - j.m_value = value; + j.m_value = typename BasicJsonType::binary_t(b); j.assert_invariant(); } @@ -82,8 +81,7 @@ struct external_constructor static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b) { j.m_type = value_t::binary; - typename BasicJsonType::binary_t value{std::move(b)}; - j.m_value = value; + j.m_value = typename BasicJsonType::binary_t(std::move(b));; j.assert_invariant(); } }; @@ -330,9 +328,9 @@ void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj) template < typename BasicJsonType, typename T, std::size_t N, enable_if_t < !std::is_constructible::value, + const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) int > = 0 > -void to_json(BasicJsonType& j, const T(&arr)[N]) +void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) { external_constructor::construct(j, arr); } @@ -375,8 +373,10 @@ struct to_json_fn } // namespace detail /// namespace to hold default `to_json` function -namespace +/// to see why this is required: +/// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html +namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces) { -constexpr const auto& to_json = detail::static_const::value; +constexpr const auto& to_json = detail::static_const::value; // NOLINT(misc-definitions-in-headers) } // namespace } // namespace nlohmann diff --git a/include/nlohmann/detail/exceptions.hpp b/include/nlohmann/detail/exceptions.hpp index bd0534c438..fc157a904b 100644 --- a/include/nlohmann/detail/exceptions.hpp +++ b/include/nlohmann/detail/exceptions.hpp @@ -3,6 +3,7 @@ #include // exception #include // runtime_error #include // to_string +#include // vector #include #include @@ -55,7 +56,7 @@ class exception : public std::exception } /// the id of the exception - const int id; + const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes) protected: JSON_HEDLEY_NON_NULL(3) @@ -117,6 +118,7 @@ class exception : public std::exception return a + "/" + detail::escape(b); }) + ") "; #else + static_cast(leaf_element); return ""; #endif } diff --git a/include/nlohmann/detail/hash.hpp b/include/nlohmann/detail/hash.hpp index c32d5535c5..70c5daf338 100644 --- a/include/nlohmann/detail/hash.hpp +++ b/include/nlohmann/detail/hash.hpp @@ -1,6 +1,7 @@ #pragma once -#include // size_t, uint8_t +#include // uint8_t +#include // size_t #include // hash #include @@ -111,7 +112,7 @@ std::size_t hash(const BasicJsonType& j) } default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE return 0; // LCOV_EXCL_LINE } } diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index ad7359da3c..9b9fedfdfc 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -71,16 +71,16 @@ class binary_reader @param[in] adapter input adapter to read from */ - explicit binary_reader(InputAdapterType&& adapter) : ia(std::move(adapter)) + explicit binary_reader(InputAdapterType&& adapter) noexcept : ia(std::move(adapter)) { (void)detail::is_sax_static_asserts {}; } // make class move-only binary_reader(const binary_reader&) = delete; - binary_reader(binary_reader&&) = default; + binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) binary_reader& operator=(const binary_reader&) = delete; - binary_reader& operator=(binary_reader&&) = default; + binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) ~binary_reader() = default; /*! @@ -89,7 +89,7 @@ class binary_reader @param[in] strict whether to expect the input to be consumed completed @param[in] tag_handler how to treat CBOR tags - @return + @return whether parsing was successful */ JSON_HEDLEY_NON_NULL(3) bool sax_parse(const input_format_t format, @@ -119,7 +119,7 @@ class binary_reader break; default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } // strict mode: next byte must be EOF @@ -173,7 +173,7 @@ class binary_reader /*! @brief Parses a C-style string from the BSON input. - @param[in, out] result A reference to the string variable where the read + @param[in,out] result A reference to the string variable where the read string is to be stored. @return `true` if the \x00-byte indicating the end of the string was encountered before the EOF; false` indicates an unexpected EOF. @@ -201,7 +201,7 @@ class binary_reader input. @param[in] len The length (including the zero-byte at the end) of the string to be read. - @param[in, out] result A reference to the string variable where the read + @param[in,out] result A reference to the string variable where the read string is to be stored. @tparam NumberType The type of the length @a len @pre len >= 1 @@ -222,7 +222,7 @@ class binary_reader /*! @brief Parses a byte array input of length @a len from the BSON input. @param[in] len The length of the byte array to be read. - @param[in, out] result A reference to the binary variable where the read + @param[in,out] result A reference to the binary variable where the read array is to be stored. @tparam NumberType The type of the length @a len @pre len >= 0 @@ -315,7 +315,7 @@ class binary_reader default: // anything else not supported (yet) { std::array cr{{}}; - (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type)); + (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type)); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), BasicJsonType())); } } @@ -754,7 +754,7 @@ class binary_reader } default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE return false; // LCOV_EXCL_LINE } } @@ -1854,7 +1854,7 @@ class binary_reader { return false; } - result = static_cast(number); + result = static_cast(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char return true; } @@ -2211,8 +2211,8 @@ class binary_reader } // parse number string - auto number_ia = detail::input_adapter(std::forward(number_vector)); - auto number_lexer = detail::lexer(std::move(number_ia), false); + using ia_type = decltype(detail::input_adapter(number_vector)); + auto number_lexer = detail::lexer(detail::input_adapter(number_vector), false); const auto result_number = number_lexer.scan(); const auto number_string = number_lexer.get_token_string(); const auto result_remainder = number_lexer.scan(); @@ -2287,7 +2287,7 @@ class binary_reader bool get_number(const input_format_t format, NumberType& result) { // step 1: read input into array with system's byte order - std::array vec; + std::array vec{}; for (std::size_t i = 0; i < sizeof(NumberType); ++i) { get(); @@ -2400,7 +2400,7 @@ class binary_reader std::string get_token_string() const { std::array cr{{}}; - (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(current)); + (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(current)); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) return std::string{cr.data()}; } @@ -2435,7 +2435,7 @@ class binary_reader break; default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } return error_msg + " " + context + ": " + detail; diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index c437564fd0..5d0b59b57a 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -42,9 +42,10 @@ class file_input_adapter // make class move-only file_input_adapter(const file_input_adapter&) = delete; - file_input_adapter(file_input_adapter&&) = default; + file_input_adapter(file_input_adapter&&) noexcept = default; file_input_adapter& operator=(const file_input_adapter&) = delete; file_input_adapter& operator=(file_input_adapter&&) = delete; + ~file_input_adapter() = default; std::char_traits::int_type get_character() noexcept { @@ -88,9 +89,10 @@ class input_stream_adapter // delete because of pointer members input_stream_adapter(const input_stream_adapter&) = delete; input_stream_adapter& operator=(input_stream_adapter&) = delete; - input_stream_adapter& operator=(input_stream_adapter&& rhs) = delete; + input_stream_adapter& operator=(input_stream_adapter&&) = delete; - input_stream_adapter(input_stream_adapter&& rhs) noexcept : is(rhs.is), sb(rhs.sb) + input_stream_adapter(input_stream_adapter&& rhs) noexcept + : is(rhs.is), sb(rhs.sb) { rhs.is = nullptr; rhs.sb = nullptr; @@ -125,7 +127,8 @@ class iterator_input_adapter using char_type = typename std::iterator_traits::value_type; iterator_input_adapter(IteratorType first, IteratorType last) - : current(std::move(first)), end(std::move(last)) {} + : current(std::move(first)), end(std::move(last)) + {} typename std::char_traits::int_type get_character() { @@ -150,7 +153,6 @@ class iterator_input_adapter { return current == end; } - }; @@ -393,7 +395,7 @@ struct container_input_adapter_factory< ContainerType, } }; -} +} // namespace container_input_adapter_factory_impl template typename container_input_adapter_factory_impl::container_input_adapter_factory::adapter_type input_adapter(const ContainerType& container) @@ -435,7 +437,7 @@ contiguous_bytes_input_adapter input_adapter(CharT b) } template -auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + N)) +auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + N)) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) { return input_adapter(array, array + N); } @@ -464,7 +466,7 @@ class span_input_adapter contiguous_bytes_input_adapter&& get() { - return std::move(ia); + return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg) } private: diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index a4b7e6d086..67278f8566 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -126,6 +126,11 @@ struct json_sax const std::string& last_token, const detail::exception& ex) = 0; + json_sax() = default; + json_sax(const json_sax&) = default; + json_sax(json_sax&&) noexcept = default; + json_sax& operator=(const json_sax&) = default; + json_sax& operator=(json_sax&&) noexcept = default; virtual ~json_sax() = default; }; @@ -156,7 +161,7 @@ class json_sax_dom_parser using binary_t = typename BasicJsonType::binary_t; /*! - @param[in, out] r reference to a JSON value that is manipulated while + @param[in,out] r reference to a JSON value that is manipulated while parsing @param[in] allow_exceptions_ whether parse errors yield exceptions */ @@ -166,9 +171,9 @@ class json_sax_dom_parser // make class move-only json_sax_dom_parser(const json_sax_dom_parser&) = delete; - json_sax_dom_parser(json_sax_dom_parser&&) = default; + json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete; - json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; + json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) ~json_sax_dom_parser() = default; bool null() @@ -341,9 +346,9 @@ class json_sax_dom_callback_parser // make class move-only json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete; - json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; + json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete; - json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; + json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) ~json_sax_dom_callback_parser() = default; bool null() diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index eae82eaa32..7db2722619 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -112,7 +112,7 @@ class lexer : public lexer_base public: using token_type = typename lexer_base::token_type; - explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) + explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept : ia(std::move(adapter)) , ignore_comments(ignore_comments_) , decimal_point_char(static_cast(get_decimal_point())) @@ -120,9 +120,9 @@ class lexer : public lexer_base // delete because of pointer members lexer(const lexer&) = delete; - lexer(lexer&&) = default; + lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) lexer& operator=(lexer&) = delete; - lexer& operator=(lexer&&) = default; + lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) ~lexer() = default; private: @@ -998,7 +998,7 @@ class lexer : public lexer_base // all other characters are rejected outside scan_number() default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } scan_number_minus: @@ -1236,7 +1236,7 @@ class lexer : public lexer_base // we are done scanning a number) unget(); - char* endptr = nullptr; + char* endptr = nullptr; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) errno = 0; // try to parse integers first and fall back to floats @@ -1447,7 +1447,7 @@ class lexer : public lexer_base { // escape control characters std::array cs{{}}; - (std::snprintf)(cs.data(), cs.size(), "", static_cast(c)); + (std::snprintf)(cs.data(), cs.size(), "", static_cast(c)); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) result += cs.data(); } else diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index 7b5d494f85..90f2327656 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -41,7 +41,7 @@ enum class parse_event_t : uint8_t template using parser_callback_t = - std::function; + std::function; /*! @brief syntax analysis @@ -487,5 +487,6 @@ class parser /// whether to throw exceptions in case of errors const bool allow_exceptions = true; }; + } // namespace detail } // namespace nlohmann diff --git a/include/nlohmann/detail/iterators/iter_impl.hpp b/include/nlohmann/detail/iterators/iter_impl.hpp index 118fef3f51..1747a88af0 100644 --- a/include/nlohmann/detail/iterators/iter_impl.hpp +++ b/include/nlohmann/detail/iterators/iter_impl.hpp @@ -75,8 +75,10 @@ class iter_impl typename BasicJsonType::const_reference, typename BasicJsonType::reference>::type; - /// default constructor iter_impl() = default; + ~iter_impl() = default; + iter_impl(iter_impl&&) noexcept = default; + iter_impl& operator=(iter_impl&&) noexcept = default; /*! @brief constructor for a given JSON instance @@ -138,8 +140,11 @@ class iter_impl */ iter_impl& operator=(const iter_impl& other) noexcept { - m_object = other.m_object; - m_it = other.m_it; + if (&other != this) + { + m_object = other.m_object; + m_it = other.m_it; + } return *this; } @@ -158,7 +163,7 @@ class iter_impl @return const/non-const iterator @note It is not checked whether @a other is initialized. */ - iter_impl& operator=(const iter_impl::type>& other) noexcept + iter_impl& operator=(const iter_impl::type>& other) noexcept // NOLINT(cert-oop54-cpp) { m_object = other.m_object; m_it = other.m_it; @@ -309,7 +314,7 @@ class iter_impl @brief post-increment (it++) @pre The iterator is initialized; i.e. `m_object != nullptr`. */ - iter_impl const operator++(int) + iter_impl const operator++(int) // NOLINT(readability-const-return-type) { auto result = *this; ++(*this); @@ -352,7 +357,7 @@ class iter_impl @brief post-decrement (it--) @pre The iterator is initialized; i.e. `m_object != nullptr`. */ - iter_impl const operator--(int) + iter_impl const operator--(int) // NOLINT(readability-const-return-type) { auto result = *this; --(*this); diff --git a/include/nlohmann/detail/iterators/iteration_proxy.hpp b/include/nlohmann/detail/iterators/iteration_proxy.hpp index 1b47faeb3e..d59098d95d 100644 --- a/include/nlohmann/detail/iterators/iteration_proxy.hpp +++ b/include/nlohmann/detail/iterators/iteration_proxy.hpp @@ -4,6 +4,7 @@ #include // input_iterator_tag #include // string, to_string #include // tuple_size, get, tuple_element +#include // move #include #include @@ -42,7 +43,9 @@ template class iteration_proxy_value const string_type empty_str{}; public: - explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {} + explicit iteration_proxy_value(IteratorType it) noexcept + : anchor(std::move(it)) + {} /// dereference operator (needed for range-based for) iteration_proxy_value& operator*() diff --git a/include/nlohmann/detail/iterators/json_reverse_iterator.hpp b/include/nlohmann/detail/iterators/json_reverse_iterator.hpp index f3b5b5db6b..e787fdbcd7 100644 --- a/include/nlohmann/detail/iterators/json_reverse_iterator.hpp +++ b/include/nlohmann/detail/iterators/json_reverse_iterator.hpp @@ -48,7 +48,7 @@ class json_reverse_iterator : public std::reverse_iterator explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {} /// post-increment (it++) - json_reverse_iterator const operator++(int) + json_reverse_iterator const operator++(int) // NOLINT(readability-const-return-type) { return static_cast(base_iterator::operator++(1)); } @@ -60,7 +60,7 @@ class json_reverse_iterator : public std::reverse_iterator } /// post-decrement (it--) - json_reverse_iterator const operator--(int) + json_reverse_iterator const operator--(int) // NOLINT(readability-const-return-type) { return static_cast(base_iterator::operator--(1)); } diff --git a/include/nlohmann/detail/iterators/primitive_iterator.hpp b/include/nlohmann/detail/iterators/primitive_iterator.hpp index ae7471ef59..15aa2f08aa 100644 --- a/include/nlohmann/detail/iterators/primitive_iterator.hpp +++ b/include/nlohmann/detail/iterators/primitive_iterator.hpp @@ -87,7 +87,7 @@ class primitive_iterator_t return *this; } - primitive_iterator_t const operator++(int) noexcept + primitive_iterator_t const operator++(int) noexcept // NOLINT(readability-const-return-type) { auto result = *this; ++m_it; @@ -100,7 +100,7 @@ class primitive_iterator_t return *this; } - primitive_iterator_t const operator--(int) noexcept + primitive_iterator_t const operator--(int) noexcept // NOLINT(readability-const-return-type) { auto result = *this; --m_it; diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index cb2ec85600..72e14f0144 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -88,9 +88,9 @@ class json_pointer @complexity Linear in the length of @a ptr. - @sa @ref operator/=(std::string) to append a reference token - @sa @ref operator/=(std::size_t) to append an array index - @sa @ref operator/(const json_pointer&, const json_pointer&) for a binary operator + @sa see @ref operator/=(std::string) to append a reference token + @sa see @ref operator/=(std::size_t) to append an array index + @sa see @ref operator/(const json_pointer&, const json_pointer&) for a binary operator @since version 3.6.0 */ @@ -112,9 +112,9 @@ class json_pointer @complexity Amortized constant. - @sa @ref operator/=(const json_pointer&) to append a JSON pointer - @sa @ref operator/=(std::size_t) to append an array index - @sa @ref operator/(const json_pointer&, std::size_t) for a binary operator + @sa see @ref operator/=(const json_pointer&) to append a JSON pointer + @sa see @ref operator/=(std::size_t) to append an array index + @sa see @ref operator/(const json_pointer&, std::size_t) for a binary operator @since version 3.6.0 */ @@ -134,9 +134,9 @@ class json_pointer @complexity Amortized constant. - @sa @ref operator/=(const json_pointer&) to append a JSON pointer - @sa @ref operator/=(std::string) to append a reference token - @sa @ref operator/(const json_pointer&, std::string) for a binary operator + @sa see @ref operator/=(const json_pointer&) to append a JSON pointer + @sa see @ref operator/=(std::string) to append a reference token + @sa see @ref operator/(const json_pointer&, std::string) for a binary operator @since version 3.6.0 */ @@ -156,7 +156,7 @@ class json_pointer @complexity Linear in the length of @a lhs and @a rhs. - @sa @ref operator/=(const json_pointer&) to append a JSON pointer + @sa see @ref operator/=(const json_pointer&) to append a JSON pointer @since version 3.6.0 */ @@ -177,11 +177,11 @@ class json_pointer @complexity Linear in the length of @a ptr. - @sa @ref operator/=(std::string) to append a reference token + @sa see @ref operator/=(std::string) to append a reference token @since version 3.6.0 */ - friend json_pointer operator/(const json_pointer& ptr, std::string token) + friend json_pointer operator/(const json_pointer& ptr, std::string token) // NOLINT(performance-unnecessary-value-param) { return json_pointer(ptr) /= std::move(token); } @@ -197,7 +197,7 @@ class json_pointer @complexity Linear in the length of @a ptr. - @sa @ref operator/=(std::size_t) to append an array index + @sa see @ref operator/=(std::size_t) to append an array index @since version 3.6.0 */ @@ -348,7 +348,7 @@ class json_pointer } std::size_t processed_chars = 0; - unsigned long long res = 0; + unsigned long long res = 0; // NOLINT(runtime/int) JSON_TRY { res = std::stoull(s, &processed_chars); @@ -366,7 +366,7 @@ class json_pointer // only triggered on special platforms (like 32bit), see also // https://github.com/nlohmann/json/pull/2203 - if (res >= static_cast((std::numeric_limits::max)())) + if (res >= static_cast((std::numeric_limits::max)())) // NOLINT(runtime/int) { JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", BasicJsonType())); // LCOV_EXCL_LINE } @@ -398,7 +398,7 @@ class json_pointer */ BasicJsonType& get_and_create(BasicJsonType& j) const { - auto result = &j; + auto* result = &j; // in case no reference tokens exist, return a reference to the JSON value // j which will be overwritten by a primitive value diff --git a/include/nlohmann/detail/json_ref.hpp b/include/nlohmann/detail/json_ref.hpp index 26a4903828..b4e5dabaf7 100644 --- a/include/nlohmann/detail/json_ref.hpp +++ b/include/nlohmann/detail/json_ref.hpp @@ -35,7 +35,7 @@ class json_ref {} // class should be movable only - json_ref(json_ref&&) = default; + json_ref(json_ref&&) noexcept = default; json_ref(const json_ref&) = delete; json_ref& operator=(const json_ref&) = delete; json_ref& operator=(json_ref&&) = delete; diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 77acf04c76..bd36048f1f 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -31,12 +31,6 @@ #define JSON_HAS_CPP_14 #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 - // disable documentation warnings on clang #if defined(__clang__) #pragma GCC diagnostic push diff --git a/include/nlohmann/detail/macro_unscope.hpp b/include/nlohmann/detail/macro_unscope.hpp index 5ac66f5af8..21c091daf9 100644 --- a/include/nlohmann/detail/macro_unscope.hpp +++ b/include/nlohmann/detail/macro_unscope.hpp @@ -1,9 +1,6 @@ #pragma once // restore GCC/clang diagnostic settings -#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) - #pragma GCC diagnostic pop -#endif #if defined(__clang__) #pragma GCC diagnostic pop #endif diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 27215f193e..24e7c10943 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -2,11 +2,12 @@ #include // reverse #include // array +#include // isnan, isinf #include // uint8_t, uint16_t, uint32_t, uint64_t #include // memcpy #include // numeric_limits #include // string -#include // isnan, isinf +#include // move #include #include @@ -36,7 +37,7 @@ class binary_writer @param[in] adapter output adapter to write to */ - explicit binary_writer(output_adapter_t adapter) : oa(adapter) + explicit binary_writer(output_adapter_t adapter) : oa(std::move(adapter)) { JSON_ASSERT(oa); } @@ -1139,7 +1140,7 @@ class binary_writer // LCOV_EXCL_START default: - JSON_ASSERT(false); + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) return 0ul; // LCOV_EXCL_STOP } @@ -1150,7 +1151,6 @@ class binary_writer key @a name. @param name The name to associate with the JSON entity @a j within the current BSON document - @return The size of the BSON entry */ void write_bson_element(const string_t& name, const BasicJsonType& j) @@ -1186,7 +1186,7 @@ class binary_writer // LCOV_EXCL_START default: - JSON_ASSERT(false); + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) return; // LCOV_EXCL_STOP } @@ -1195,8 +1195,8 @@ class binary_writer /*! @brief Calculates the size of the BSON serialization of the given JSON-object @a j. - @param[in] j JSON value to serialize - @pre j.type() == value_t::object + @param[in] value JSON value to serialize + @pre value.type() == value_t::object */ static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value) { @@ -1210,8 +1210,8 @@ class binary_writer } /*! - @param[in] j JSON value to serialize - @pre j.type() == value_t::object + @param[in] value JSON value to serialize + @pre value.type() == value_t::object */ void write_bson_object(const typename BasicJsonType::object_t& value) { @@ -1509,7 +1509,7 @@ class binary_writer void write_number(const NumberType n) { // step 1: write number to array of length NumberType - std::array vec; + std::array vec{}; std::memcpy(vec.data(), &n, sizeof(NumberType)); // step 2: write array to output (with possible reordering) diff --git a/include/nlohmann/detail/output/output_adapters.hpp b/include/nlohmann/detail/output/output_adapters.hpp index 71ca65b92d..25886ad1ad 100644 --- a/include/nlohmann/detail/output/output_adapters.hpp +++ b/include/nlohmann/detail/output/output_adapters.hpp @@ -20,6 +20,12 @@ template struct output_adapter_protocol virtual void write_character(CharType c) = 0; virtual void write_characters(const CharType* s, std::size_t length) = 0; virtual ~output_adapter_protocol() = default; + + output_adapter_protocol() = default; + output_adapter_protocol(const output_adapter_protocol&) = default; + output_adapter_protocol(output_adapter_protocol&&) noexcept = default; + output_adapter_protocol& operator=(const output_adapter_protocol&) = default; + output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default; }; /// a type to simplify interfaces diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index d9c8b22fef..dfbe54329a 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -358,7 +358,7 @@ class serializer } default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } } @@ -379,7 +379,7 @@ class serializer */ void dump_escaped(const string_t& s, const bool ensure_ascii) { - std::uint32_t codepoint; + std::uint32_t codepoint{}; std::uint8_t state = UTF8_ACCEPT; std::size_t bytes = 0; // number of bytes written to string_buffer @@ -454,12 +454,14 @@ class serializer { if (codepoint <= 0xFFFF) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) (std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x", static_cast(codepoint)); bytes += 6; } else { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) (std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x", static_cast(0xD7C0u + (codepoint >> 10u)), static_cast(0xDC00u + (codepoint & 0x3FFu))); @@ -498,6 +500,7 @@ class serializer case error_handler_t::strict: { std::string sn(3, '\0'); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, BasicJsonType())); } @@ -557,7 +560,7 @@ class serializer } default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } break; } @@ -592,6 +595,7 @@ class serializer case error_handler_t::strict: { std::string sn(3, '\0'); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, BasicJsonType())); } @@ -620,7 +624,7 @@ class serializer } default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } } } @@ -700,12 +704,12 @@ class serializer } // use a pointer to fill the buffer - auto buffer_ptr = number_buffer.begin(); + auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg) const bool is_negative = std::is_same::value && !(x >= 0); // see issue #755 number_unsigned_t abs_value; - unsigned int n_chars; + unsigned int n_chars{}; if (is_negative) { @@ -783,8 +787,8 @@ class serializer void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/) { - char* begin = number_buffer.data(); - char* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x); + auto* begin = number_buffer.data(); + auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x); o->write_characters(begin, static_cast(end - begin)); } @@ -795,6 +799,7 @@ class serializer static constexpr auto d = std::numeric_limits::max_digits10; // the actual conversion + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x); // negative value indicates an error @@ -805,8 +810,8 @@ class serializer // erase thousands separator if (thousands_sep != '\0') { - const auto end = std::remove(number_buffer.begin(), - number_buffer.begin() + len, thousands_sep); + auto* const end = std::remove(number_buffer.begin(), + number_buffer.begin() + len, thousands_sep); std::fill(end, number_buffer.end(), '\0'); JSON_ASSERT((end - number_buffer.begin()) <= len); len = (end - number_buffer.begin()); @@ -815,7 +820,7 @@ class serializer // convert decimal point to '.' if (decimal_point != '\0' && decimal_point != '.') { - const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point); + auto* const dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point); if (dec_pos != number_buffer.end()) { *dec_pos = '.'; @@ -901,7 +906,7 @@ class serializer */ number_unsigned_t remove_sign(number_unsigned_t x) { - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE return x; // LCOV_EXCL_LINE } @@ -916,7 +921,7 @@ class serializer */ inline number_unsigned_t remove_sign(number_integer_t x) noexcept { - JSON_ASSERT(x < 0 && x < (std::numeric_limits::max)()); + JSON_ASSERT(x < 0 && x < (std::numeric_limits::max)()); // NOLINT(misc-redundant-expression) return static_cast(-(x + 1)) + 1; } diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 7b44c0ea34..3c9fa9199a 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -64,6 +64,7 @@ SOFTWARE. #include #include #include +#include #include #include #include @@ -170,7 +171,7 @@ Format](http://rfc7159.net/rfc7159) @nosubgrouping */ NLOHMANN_BASIC_JSON_TPL_DECLARATION -class basic_json +class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions) { private: template friend struct detail::external_constructor; @@ -492,7 +493,7 @@ class basic_json access to object values, a pointer of type `object_t*` must be dereferenced. - @sa @ref array_t -- type for an array value + @sa see @ref array_t -- type for an array value @since version 1.0.0 @@ -550,7 +551,7 @@ class basic_json Arrays are stored as pointers in a @ref basic_json type. That is, for any access to array values, a pointer of type `array_t*` must be dereferenced. - @sa @ref object_t -- type for an object value + @sa see @ref object_t -- type for an object value @since version 1.0.0 */ @@ -699,9 +700,9 @@ class basic_json Integer number values are stored directly inside a @ref basic_json type. - @sa @ref number_float_t -- type for number values (floating-point) + @sa see @ref number_float_t -- type for number values (floating-point) - @sa @ref number_unsigned_t -- type for number values (unsigned integer) + @sa see @ref number_unsigned_t -- type for number values (unsigned integer) @since version 1.0.0 */ @@ -771,8 +772,8 @@ class basic_json Integer number values are stored directly inside a @ref basic_json type. - @sa @ref number_float_t -- type for number values (floating-point) - @sa @ref number_integer_t -- type for number values (integer) + @sa see @ref number_float_t -- type for number values (floating-point) + @sa see @ref number_integer_t -- type for number values (integer) @since version 2.0.0 */ @@ -838,9 +839,9 @@ class basic_json Floating-point number values are stored directly inside a @ref basic_json type. - @sa @ref number_integer_t -- type for number values (integer) + @sa see @ref number_integer_t -- type for number values (integer) - @sa @ref number_unsigned_t -- type for number values (unsigned integer) + @sa see @ref number_unsigned_t -- type for number values (unsigned integer) @since version 1.0.0 */ @@ -911,7 +912,7 @@ class basic_json - If a subtype is given, it is used and added as unsigned 8-bit integer. - If no subtype is given, the generic binary subtype 0x00 is used. - @sa @ref binary -- create a binary array + @sa see @ref binary -- create a binary array @since version 3.8.0 */ @@ -1246,10 +1247,15 @@ class basic_json JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr); #if JSON_DIAGNOSTICS - JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j) + JSON_TRY { - return j.m_parent == this; - })); + // cppcheck-suppress assertWithSideEffect + JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j) + { + return j.m_parent == this; + })); + } + JSON_CATCH(...) {} // LCOV_EXCL_LINE #else static_cast(check_parents); #endif @@ -1325,7 +1331,7 @@ class basic_json @image html callback_events.png "Example when certain parse events are triggered" - @sa @ref parser_callback_t for more information and examples + @sa see @ref parser_callback_t for more information and examples */ using parse_event_t = detail::parse_event_t; @@ -1374,7 +1380,7 @@ class basic_json should be kept (`true`) or not (`false`). In the latter case, it is either skipped completely or replaced by an empty discarded object. - @sa @ref parse for examples + @sa see @ref parse for examples @since version 1.0.0 */ @@ -1415,7 +1421,7 @@ class basic_json @liveexample{The following code shows the constructor for different @ref value_t values,basic_json__value_t} - @sa @ref clear() -- restores the postcondition of this constructor + @sa see @ref clear() -- restores the postcondition of this constructor @since version 1.0.0 */ @@ -1491,8 +1497,7 @@ class basic_json - @a CompatibleType is not a different @ref basic_json type (i.e. with different template arguments) - @a CompatibleType is not a @ref basic_json nested type (e.g., @ref json_pointer, @ref iterator, etc ...) - - @ref @ref json_serializer has a - `to_json(basic_json_t&, CompatibleType&&)` method + - `json_serializer` has a `to_json(basic_json_t&, CompatibleType&&)` method @tparam U = `uncvref_t` @@ -1516,7 +1521,7 @@ class basic_json typename U = detail::uncvref_t, detail::enable_if_t < !detail::is_basic_json::value && detail::is_compatible_type::value, int > = 0 > - basic_json(CompatibleType && val) noexcept(noexcept( + basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape) JSONSerializer::to_json(std::declval(), std::forward(val)))) { @@ -1598,7 +1603,7 @@ class basic_json m_type = value_t::discarded; break; default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } set_parents(); assert_invariant(); @@ -1671,9 +1676,9 @@ class basic_json @liveexample{The example below shows how JSON values are created from initializer lists.,basic_json__list_init_t} - @sa @ref array(initializer_list_t) -- create a JSON array + @sa see @ref array(initializer_list_t) -- create a JSON array value from an initializer list - @sa @ref object(initializer_list_t) -- create a JSON object + @sa see @ref object(initializer_list_t) -- create a JSON object value from an initializer list @since version 1.0.0 @@ -1854,9 +1859,9 @@ class basic_json @liveexample{The following code shows an example for the `array` function.,array} - @sa @ref basic_json(initializer_list_t, bool, value_t) -- + @sa see @ref basic_json(initializer_list_t, bool, value_t) -- create a JSON value from an initializer list - @sa @ref object(initializer_list_t) -- create a JSON object + @sa see @ref object(initializer_list_t) -- create a JSON object value from an initializer list @since version 1.0.0 @@ -1898,9 +1903,9 @@ class basic_json @liveexample{The following code shows an example for the `object` function.,object} - @sa @ref basic_json(initializer_list_t, bool, value_t) -- + @sa see @ref basic_json(initializer_list_t, bool, value_t) -- create a JSON value from an initializer list - @sa @ref array(initializer_list_t) -- create a JSON array + @sa see @ref array(initializer_list_t) -- create a JSON array value from an initializer list @since version 1.0.0 @@ -2403,8 +2408,8 @@ class basic_json @liveexample{The following code exemplifies `type()` for all JSON types.,type} - @sa @ref operator value_t() -- return the type of the JSON value (implicit) - @sa @ref type_name() -- return the type as string + @sa see @ref operator value_t() -- return the type of the JSON value (implicit) + @sa see @ref type_name() -- return the type as string @since version 1.0.0 */ @@ -2430,12 +2435,12 @@ class basic_json @liveexample{The following code exemplifies `is_primitive()` for all JSON types.,is_primitive} - @sa @ref is_structured() -- returns whether JSON value is structured - @sa @ref is_null() -- returns whether JSON value is `null` - @sa @ref is_string() -- returns whether JSON value is a string - @sa @ref is_boolean() -- returns whether JSON value is a boolean - @sa @ref is_number() -- returns whether JSON value is a number - @sa @ref is_binary() -- returns whether JSON value is a binary array + @sa see @ref is_structured() -- returns whether JSON value is structured + @sa see @ref is_null() -- returns whether JSON value is `null` + @sa see @ref is_string() -- returns whether JSON value is a string + @sa see @ref is_boolean() -- returns whether JSON value is a boolean + @sa see @ref is_number() -- returns whether JSON value is a number + @sa see @ref is_binary() -- returns whether JSON value is a binary array @since version 1.0.0 */ @@ -2460,9 +2465,9 @@ class basic_json @liveexample{The following code exemplifies `is_structured()` for all JSON types.,is_structured} - @sa @ref is_primitive() -- returns whether value is primitive - @sa @ref is_array() -- returns whether value is an array - @sa @ref is_object() -- returns whether value is an object + @sa see @ref is_primitive() -- returns whether value is primitive + @sa see @ref is_array() -- returns whether value is an array + @sa see @ref is_object() -- returns whether value is an object @since version 1.0.0 */ @@ -2532,11 +2537,11 @@ class basic_json @liveexample{The following code exemplifies `is_number()` for all JSON types.,is_number} - @sa @ref is_number_integer() -- check if value is an integer or unsigned + @sa see @ref is_number_integer() -- check if value is an integer or unsigned integer number - @sa @ref is_number_unsigned() -- check if value is an unsigned integer + @sa see @ref is_number_unsigned() -- check if value is an unsigned integer number - @sa @ref is_number_float() -- check if value is a floating-point number + @sa see @ref is_number_float() -- check if value is a floating-point number @since version 1.0.0 */ @@ -2562,10 +2567,10 @@ class basic_json @liveexample{The following code exemplifies `is_number_integer()` for all JSON types.,is_number_integer} - @sa @ref is_number() -- check if value is a number - @sa @ref is_number_unsigned() -- check if value is an unsigned integer + @sa see @ref is_number() -- check if value is a number + @sa see @ref is_number_unsigned() -- check if value is an unsigned integer number - @sa @ref is_number_float() -- check if value is a floating-point number + @sa see @ref is_number_float() -- check if value is a floating-point number @since version 1.0.0 */ @@ -2590,10 +2595,10 @@ class basic_json @liveexample{The following code exemplifies `is_number_unsigned()` for all JSON types.,is_number_unsigned} - @sa @ref is_number() -- check if value is a number - @sa @ref is_number_integer() -- check if value is an integer or unsigned + @sa see @ref is_number() -- check if value is a number + @sa see @ref is_number_integer() -- check if value is an integer or unsigned integer number - @sa @ref is_number_float() -- check if value is a floating-point number + @sa see @ref is_number_float() -- check if value is a floating-point number @since version 2.0.0 */ @@ -2618,9 +2623,9 @@ class basic_json @liveexample{The following code exemplifies `is_number_float()` for all JSON types.,is_number_float} - @sa @ref is_number() -- check if value is number - @sa @ref is_number_integer() -- check if value is an integer number - @sa @ref is_number_unsigned() -- check if value is an unsigned integer + @sa see @ref is_number() -- check if value is number + @sa see @ref is_number_integer() -- check if value is an integer number + @sa see @ref is_number_unsigned() -- check if value is an unsigned integer number @since version 1.0.0 @@ -2761,8 +2766,8 @@ class basic_json @liveexample{The following code exemplifies the @ref value_t operator for all JSON types.,operator__value_t} - @sa @ref type() -- return the type of the JSON value (explicit) - @sa @ref type_name() -- return the type as string + @sa see @ref type() -- return the type of the JSON value (explicit) + @sa see @ref type_name() -- return the type as string @since version 1.0.0 */ @@ -2900,7 +2905,7 @@ class basic_json static ReferenceType get_ref_impl(ThisType& obj) { // delegate the call to get_ptr<>() - auto ptr = obj.template get_ptr::type>(); + auto* ptr = obj.template get_ptr::type>(); if (JSON_HEDLEY_LIKELY(ptr != nullptr)) { @@ -2945,7 +2950,7 @@ class basic_json @tparam BasicJsonType == @ref basic_json - @return a copy of *this, converted into @tparam BasicJsonType + @return a copy of *this, converted into @a BasicJsonType @complexity Depending on the implementation of the called `from_json()` method. @@ -3016,7 +3021,7 @@ class basic_json static_assert(std::is_default_constructible::value, "types must be DefaultConstructible when used with get()"); - ValueType ret; + ValueType ret{}; JSONSerializer::from_json(*this, ret); return ret; } @@ -3123,10 +3128,10 @@ class basic_json template < typename T, std::size_t N, - typename Array = T (&)[N], + typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) detail::enable_if_t < detail::has_from_json::value, int > = 0 > - Array get_to(T (&v)[N]) const + Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) noexcept(noexcept(JSONSerializer::from_json( std::declval(), v))) { @@ -3205,7 +3210,7 @@ class basic_json `nullptr` is returned if the value and the requested pointer type does not match.,get__PointerType} - @sa @ref get_ptr() for explicit pointer-member access + @sa see @ref get_ptr() for explicit pointer-member access @since version 1.0.0 */ @@ -3327,7 +3332,7 @@ class basic_json @throw type_error.302 if the value is not binary - @sa @ref is_binary() to check if the value is binary + @sa see @ref is_binary() to check if the value is binary @since version 3.8.0 */ @@ -3477,9 +3482,9 @@ class basic_json @complexity Logarithmic in the size of the container. - @sa @ref operator[](const typename object_t::key_type&) for unchecked + @sa see @ref operator[](const typename object_t::key_type&) for unchecked access by reference - @sa @ref value() for access by value with a default value + @sa see @ref value() for access by value with a default value @since version 1.0.0 @@ -3528,9 +3533,9 @@ class basic_json @complexity Logarithmic in the size of the container. - @sa @ref operator[](const typename object_t::key_type&) for unchecked + @sa see @ref operator[](const typename object_t::key_type&) for unchecked access by reference - @sa @ref value() for access by value with a default value + @sa see @ref value() for access by value with a default value @since version 1.0.0 @@ -3669,9 +3674,9 @@ class basic_json @liveexample{The example below shows how object elements can be read and written using the `[]` operator.,operatorarray__key_type} - @sa @ref at(const typename object_t::key_type&) for access by reference + @sa see @ref at(const typename object_t::key_type&) for access by reference with range checking - @sa @ref value() for access by value with a default value + @sa see @ref value() for access by value with a default value @since version 1.0.0 */ @@ -3718,9 +3723,9 @@ class basic_json @liveexample{The example below shows how object elements can be read using the `[]` operator.,operatorarray__key_type_const} - @sa @ref at(const typename object_t::key_type&) for access by reference + @sa see @ref at(const typename object_t::key_type&) for access by reference with range checking - @sa @ref value() for access by value with a default value + @sa see @ref value() for access by value with a default value @since version 1.0.0 */ @@ -3757,9 +3762,9 @@ class basic_json @liveexample{The example below shows how object elements can be read and written using the `[]` operator.,operatorarray__key_type} - @sa @ref at(const typename object_t::key_type&) for access by reference + @sa see @ref at(const typename object_t::key_type&) for access by reference with range checking - @sa @ref value() for access by value with a default value + @sa see @ref value() for access by value with a default value @since version 1.1.0 */ @@ -3808,9 +3813,9 @@ class basic_json @liveexample{The example below shows how object elements can be read using the `[]` operator.,operatorarray__key_type_const} - @sa @ref at(const typename object_t::key_type&) for access by reference + @sa see @ref at(const typename object_t::key_type&) for access by reference with range checking - @sa @ref value() for access by value with a default value + @sa see @ref value() for access by value with a default value @since version 1.1.0 */ @@ -3871,9 +3876,9 @@ class basic_json @liveexample{The example below shows how object elements can be queried with a default value.,basic_json__value} - @sa @ref at(const typename object_t::key_type&) for access by reference + @sa see @ref at(const typename object_t::key_type&) for access by reference with range checking - @sa @ref operator[](const typename object_t::key_type&) for unchecked + @sa see @ref operator[](const typename object_t::key_type&) for unchecked access by reference @since version 1.0.0 @@ -3948,7 +3953,7 @@ class basic_json @liveexample{The example below shows how object elements can be queried with a default value.,basic_json__value_ptr} - @sa @ref operator[](const json_pointer&) for unchecked access by reference + @sa see @ref operator[](const json_pointer&) for unchecked access by reference @since version 2.0.2 */ @@ -4004,7 +4009,7 @@ class basic_json @liveexample{The following code shows an example for `front()`.,front} - @sa @ref back() -- access the last element + @sa see @ref back() -- access the last element @since version 1.0.0 */ @@ -4048,7 +4053,7 @@ class basic_json @liveexample{The following code shows an example for `back()`.,back} - @sa @ref front() -- access the first element + @sa see @ref front() -- access the first element @since version 1.0.0 */ @@ -4106,11 +4111,11 @@ class basic_json @liveexample{The example shows the result of `erase()` for different JSON types.,erase__IteratorType} - @sa @ref erase(IteratorType, IteratorType) -- removes the elements in + @sa see @ref erase(IteratorType, IteratorType) -- removes the elements in the given range - @sa @ref erase(const typename object_t::key_type&) -- removes the element + @sa see @ref erase(const typename object_t::key_type&) -- removes the element from an object at the given key - @sa @ref erase(const size_type) -- removes the element from an array at + @sa see @ref erase(const size_type) -- removes the element from an array at the given index @since version 1.0.0 @@ -4220,10 +4225,10 @@ class basic_json @liveexample{The example shows the result of `erase()` for different JSON types.,erase__IteratorType_IteratorType} - @sa @ref erase(IteratorType) -- removes the element at a given position - @sa @ref erase(const typename object_t::key_type&) -- removes the element + @sa see @ref erase(IteratorType) -- removes the element at a given position + @sa see @ref erase(const typename object_t::key_type&) -- removes the element from an object at the given key - @sa @ref erase(const size_type) -- removes the element from an array at + @sa see @ref erase(const size_type) -- removes the element from an array at the given index @since version 1.0.0 @@ -4319,10 +4324,10 @@ class basic_json @liveexample{The example shows the effect of `erase()`.,erase__key_type} - @sa @ref erase(IteratorType) -- removes the element at a given position - @sa @ref erase(IteratorType, IteratorType) -- removes the elements in + @sa see @ref erase(IteratorType) -- removes the element at a given position + @sa see @ref erase(IteratorType, IteratorType) -- removes the elements in the given range - @sa @ref erase(const size_type) -- removes the element from an array at + @sa see @ref erase(const size_type) -- removes the element from an array at the given index @since version 1.0.0 @@ -4354,10 +4359,10 @@ class basic_json @liveexample{The example shows the effect of `erase()`.,erase__size_type} - @sa @ref erase(IteratorType) -- removes the element at a given position - @sa @ref erase(IteratorType, IteratorType) -- removes the elements in + @sa see @ref erase(IteratorType) -- removes the element at a given position + @sa see @ref erase(IteratorType, IteratorType) -- removes the elements in the given range - @sa @ref erase(const typename object_t::key_type&) -- removes the element + @sa see @ref erase(const typename object_t::key_type&) -- removes the element from an object at the given key @since version 1.0.0 @@ -4410,7 +4415,7 @@ class basic_json @liveexample{The example shows how `find()` is used.,find__key_type} - @sa @ref contains(KeyT&&) const -- checks whether a key exists + @sa see @ref contains(KeyT&&) const -- checks whether a key exists @since version 1.0.0 */ @@ -4492,8 +4497,8 @@ class basic_json @liveexample{The following code shows an example for `contains()`.,contains} - @sa @ref find(KeyT&&) -- returns an iterator to an object element - @sa @ref contains(const json_pointer&) const -- checks the existence for a JSON pointer + @sa see @ref find(KeyT&&) -- returns an iterator to an object element + @sa see @ref contains(const json_pointer&) const -- checks the existence for a JSON pointer @since version 3.6.0 */ @@ -4526,7 +4531,7 @@ class basic_json @liveexample{The following code shows an example for `contains()`.,contains_json_pointer} - @sa @ref contains(KeyT &&) const -- checks the existence of a key + @sa see @ref contains(KeyT &&) const -- checks the existence of a key @since version 3.7.0 */ @@ -4563,9 +4568,9 @@ class basic_json @liveexample{The following code shows an example for `begin()`.,begin} - @sa @ref cbegin() -- returns a const iterator to the beginning - @sa @ref end() -- returns an iterator to the end - @sa @ref cend() -- returns a const iterator to the end + @sa see @ref cbegin() -- returns a const iterator to the beginning + @sa see @ref end() -- returns an iterator to the end + @sa see @ref cend() -- returns a const iterator to the end @since version 1.0.0 */ @@ -4603,9 +4608,9 @@ class basic_json @liveexample{The following code shows an example for `cbegin()`.,cbegin} - @sa @ref begin() -- returns an iterator to the beginning - @sa @ref end() -- returns an iterator to the end - @sa @ref cend() -- returns a const iterator to the end + @sa see @ref begin() -- returns an iterator to the beginning + @sa see @ref end() -- returns an iterator to the end + @sa see @ref cend() -- returns a const iterator to the end @since version 1.0.0 */ @@ -4634,9 +4639,9 @@ class basic_json @liveexample{The following code shows an example for `end()`.,end} - @sa @ref cend() -- returns a const iterator to the end - @sa @ref begin() -- returns an iterator to the beginning - @sa @ref cbegin() -- returns a const iterator to the beginning + @sa see @ref cend() -- returns a const iterator to the end + @sa see @ref begin() -- returns an iterator to the beginning + @sa see @ref cbegin() -- returns a const iterator to the beginning @since version 1.0.0 */ @@ -4674,9 +4679,9 @@ class basic_json @liveexample{The following code shows an example for `cend()`.,cend} - @sa @ref end() -- returns an iterator to the end - @sa @ref begin() -- returns an iterator to the beginning - @sa @ref cbegin() -- returns a const iterator to the beginning + @sa see @ref end() -- returns an iterator to the end + @sa see @ref begin() -- returns an iterator to the beginning + @sa see @ref cbegin() -- returns a const iterator to the beginning @since version 1.0.0 */ @@ -4704,9 +4709,9 @@ class basic_json @liveexample{The following code shows an example for `rbegin()`.,rbegin} - @sa @ref crbegin() -- returns a const reverse iterator to the beginning - @sa @ref rend() -- returns a reverse iterator to the end - @sa @ref crend() -- returns a const reverse iterator to the end + @sa see @ref crbegin() -- returns a const reverse iterator to the beginning + @sa see @ref rend() -- returns a reverse iterator to the end + @sa see @ref crend() -- returns a const reverse iterator to the end @since version 1.0.0 */ @@ -4741,9 +4746,9 @@ class basic_json @liveexample{The following code shows an example for `rend()`.,rend} - @sa @ref crend() -- returns a const reverse iterator to the end - @sa @ref rbegin() -- returns a reverse iterator to the beginning - @sa @ref crbegin() -- returns a const reverse iterator to the beginning + @sa see @ref crend() -- returns a const reverse iterator to the end + @sa see @ref rbegin() -- returns a reverse iterator to the beginning + @sa see @ref crbegin() -- returns a const reverse iterator to the beginning @since version 1.0.0 */ @@ -4778,9 +4783,9 @@ class basic_json @liveexample{The following code shows an example for `crbegin()`.,crbegin} - @sa @ref rbegin() -- returns a reverse iterator to the beginning - @sa @ref rend() -- returns a reverse iterator to the end - @sa @ref crend() -- returns a const reverse iterator to the end + @sa see @ref rbegin() -- returns a reverse iterator to the beginning + @sa see @ref rend() -- returns a reverse iterator to the end + @sa see @ref crend() -- returns a const reverse iterator to the end @since version 1.0.0 */ @@ -4807,9 +4812,9 @@ class basic_json @liveexample{The following code shows an example for `crend()`.,crend} - @sa @ref rend() -- returns a reverse iterator to the end - @sa @ref rbegin() -- returns a reverse iterator to the beginning - @sa @ref crbegin() -- returns a const reverse iterator to the beginning + @sa see @ref rend() -- returns a reverse iterator to the end + @sa see @ref rbegin() -- returns a reverse iterator to the beginning + @sa see @ref crbegin() -- returns a const reverse iterator to the beginning @since version 1.0.0 */ @@ -5020,7 +5025,7 @@ class basic_json - The complexity is constant. - Has the semantics of `begin() == end()`. - @sa @ref size() -- returns the number of elements + @sa see @ref size() -- returns the number of elements @since version 1.0.0 */ @@ -5092,8 +5097,8 @@ class basic_json - The complexity is constant. - Has the semantics of `std::distance(begin(), end())`. - @sa @ref empty() -- checks whether the container is empty - @sa @ref max_size() -- returns the maximal number of elements + @sa see @ref empty() -- checks whether the container is empty + @sa see @ref max_size() -- returns the maximal number of elements @since version 1.0.0 */ @@ -5164,7 +5169,7 @@ class basic_json - Has the semantics of returning `b.size()` where `b` is the largest possible JSON value. - @sa @ref size() -- returns the number of elements + @sa see @ref size() -- returns the number of elements @since version 1.0.0 */ @@ -5234,7 +5239,7 @@ class basic_json @exceptionsafety No-throw guarantee: this function never throws exceptions. - @sa @ref basic_json(value_t) -- constructor that creates an object with the + @sa see @ref basic_json(value_t) -- constructor that creates an object with the same value than calling `clear()` @since version 1.0.0 @@ -6030,7 +6035,7 @@ class basic_json @since version 1.0.0 */ - void swap(array_t& other) + void swap(array_t& other) // NOLINT(bugprone-exception-escape) { // swap only works for arrays if (JSON_HEDLEY_LIKELY(is_array())) @@ -6063,7 +6068,7 @@ class basic_json @since version 1.0.0 */ - void swap(object_t& other) + void swap(object_t& other) // NOLINT(bugprone-exception-escape) { // swap only works for objects if (JSON_HEDLEY_LIKELY(is_object())) @@ -6096,7 +6101,7 @@ class basic_json @since version 1.0.0 */ - void swap(string_t& other) + void swap(string_t& other) // NOLINT(bugprone-exception-escape) { // swap only works for strings if (JSON_HEDLEY_LIKELY(is_string())) @@ -6129,7 +6134,7 @@ class basic_json @since version 3.8.0 */ - void swap(binary_t& other) + void swap(binary_t& other) // NOLINT(bugprone-exception-escape) { // swap only works for strings if (JSON_HEDLEY_LIKELY(is_binary())) @@ -6142,8 +6147,8 @@ class basic_json } } - /// @copydoc swap(binary_t) - void swap(typename binary_t::container_type& other) + /// @copydoc swap(binary_t&) + void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape) { // swap only works for strings if (JSON_HEDLEY_LIKELY(is_binary())) @@ -6295,7 +6300,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept + friend bool operator==(const_reference lhs, ScalarType rhs) noexcept { return lhs == basic_json(rhs); } @@ -6306,7 +6311,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept + friend bool operator==(ScalarType lhs, const_reference rhs) noexcept { return basic_json(lhs) == rhs; } @@ -6340,7 +6345,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator!=(const_reference lhs, const ScalarType rhs) noexcept + friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept { return lhs != basic_json(rhs); } @@ -6351,7 +6356,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator!=(const ScalarType lhs, const_reference rhs) noexcept + friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept { return basic_json(lhs) != rhs; } @@ -6461,7 +6466,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator<(const_reference lhs, const ScalarType rhs) noexcept + friend bool operator<(const_reference lhs, ScalarType rhs) noexcept { return lhs < basic_json(rhs); } @@ -6472,7 +6477,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator<(const ScalarType lhs, const_reference rhs) noexcept + friend bool operator<(ScalarType lhs, const_reference rhs) noexcept { return basic_json(lhs) < rhs; } @@ -6507,7 +6512,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator<=(const_reference lhs, const ScalarType rhs) noexcept + friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept { return lhs <= basic_json(rhs); } @@ -6518,7 +6523,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator<=(const ScalarType lhs, const_reference rhs) noexcept + friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept { return basic_json(lhs) <= rhs; } @@ -6553,7 +6558,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator>(const_reference lhs, const ScalarType rhs) noexcept + friend bool operator>(const_reference lhs, ScalarType rhs) noexcept { return lhs > basic_json(rhs); } @@ -6564,7 +6569,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator>(const ScalarType lhs, const_reference rhs) noexcept + friend bool operator>(ScalarType lhs, const_reference rhs) noexcept { return basic_json(lhs) > rhs; } @@ -6599,7 +6604,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator>=(const_reference lhs, const ScalarType rhs) noexcept + friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept { return lhs >= basic_json(rhs); } @@ -6610,7 +6615,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator>=(const ScalarType lhs, const_reference rhs) noexcept + friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept { return basic_json(lhs) >= rhs; } @@ -6936,7 +6941,9 @@ class basic_json { auto ia = i.get(); return format == input_format_t::json + // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) + // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) : detail::binary_reader(std::move(ia)).sax_parse(format, sax, strict); } @@ -7016,8 +7023,8 @@ class basic_json @liveexample{The following code exemplifies `type_name()` for all JSON types.,type_name} - @sa @ref type() -- return the type of the JSON value - @sa @ref operator value_t() -- return the type of the JSON value (implicit) + @sa see @ref type() -- return the type of the JSON value + @sa see @ref operator value_t() -- return the type of the JSON value (implicit) @since version 1.0.0, public since 2.1.0, `const char*` and `noexcept` since 3.0.0 @@ -7158,10 +7165,10 @@ class basic_json vector in CBOR format.,to_cbor} @sa http://cbor.io - @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool, const cbor_tag_handler_t) for the + @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the analogous deserialization - @sa @ref to_msgpack(const basic_json&) for the related MessagePack format - @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the + @sa see @ref to_msgpack(const basic_json&) for the related MessagePack format + @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the related UBJSON format @since version 2.0.9; compact representation of floating-point numbers @@ -7255,9 +7262,9 @@ class basic_json vector in MessagePack format.,to_msgpack} @sa http://msgpack.org - @sa @ref from_msgpack for the analogous deserialization - @sa @ref to_cbor(const basic_json& for the related CBOR format - @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the + @sa see @ref from_msgpack for the analogous deserialization + @sa see @ref to_cbor(const basic_json& for the related CBOR format + @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the related UBJSON format @since version 2.0.9 @@ -7358,10 +7365,10 @@ class basic_json vector in UBJSON format.,to_ubjson} @sa http://ubjson.org - @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the + @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the analogous deserialization - @sa @ref to_cbor(const basic_json& for the related CBOR format - @sa @ref to_msgpack(const basic_json&) for the related MessagePack format + @sa see @ref to_cbor(const basic_json& for the related CBOR format + @sa see @ref to_msgpack(const basic_json&) for the related MessagePack format @since version 3.1.0 */ @@ -7436,12 +7443,12 @@ class basic_json vector in BSON format.,to_bson} @sa http://bsonspec.org/spec.html - @sa @ref from_bson(detail::input_adapter&&, const bool strict) for the + @sa see @ref from_bson(detail::input_adapter&&, const bool strict) for the analogous deserialization - @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the + @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the related UBJSON format - @sa @ref to_cbor(const basic_json&) for the related CBOR format - @sa @ref to_msgpack(const basic_json&) for the related MessagePack format + @sa see @ref to_cbor(const basic_json&) for the related CBOR format + @sa see @ref to_msgpack(const basic_json&) for the related MessagePack format */ static std::vector to_bson(const basic_json& j) { @@ -7456,7 +7463,7 @@ class basic_json @param j The JSON object to convert to BSON. @param o The output adapter that receives the binary BSON representation. @pre The input `j` shall be an object: `j.is_object() == true` - @sa @ref to_bson(const basic_json&) + @sa see @ref to_bson(const basic_json&) */ static void to_bson(const basic_json& j, detail::output_adapter o) { @@ -7563,10 +7570,10 @@ class basic_json format to a JSON value.,from_cbor} @sa http://cbor.io - @sa @ref to_cbor(const basic_json&) for the analogous serialization - @sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for the + @sa see @ref to_cbor(const basic_json&) for the analogous serialization + @sa see @ref from_msgpack(InputType&&, const bool, const bool) for the related MessagePack format - @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the + @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the related UBJSON format @since version 2.0.9; parameter @a start_index since 2.1.1; changed to @@ -7589,7 +7596,7 @@ class basic_json } /*! - @copydoc from_cbor(detail::input_adapter&&, const bool, const bool, const cbor_tag_handler_t) + @copydoc from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) */ template JSON_HEDLEY_WARN_UNUSED_RESULT @@ -7627,6 +7634,7 @@ class basic_json basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); + // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); return res ? result : basic_json(value_t::discarded); } @@ -7704,12 +7712,12 @@ class basic_json MessagePack format to a JSON value.,from_msgpack} @sa http://msgpack.org - @sa @ref to_msgpack(const basic_json&) for the analogous serialization - @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool, const cbor_tag_handler_t) for the + @sa see @ref to_msgpack(const basic_json&) for the analogous serialization + @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the related CBOR format - @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for + @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the related UBJSON format - @sa @ref from_bson(detail::input_adapter&&, const bool, const bool) for + @sa see @ref from_bson(InputType&&, const bool, const bool) for the related BSON format @since version 2.0.9; parameter @a start_index since 2.1.1; changed to @@ -7731,7 +7739,7 @@ class basic_json } /*! - @copydoc from_msgpack(detail::input_adapter&&, const bool, const bool) + @copydoc from_msgpack(InputType&&, const bool, const bool) */ template JSON_HEDLEY_WARN_UNUSED_RESULT @@ -7766,6 +7774,7 @@ class basic_json basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); + // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7822,13 +7831,13 @@ class basic_json UBJSON format to a JSON value.,from_ubjson} @sa http://ubjson.org - @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the + @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the analogous serialization - @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool, const cbor_tag_handler_t) for the + @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the related CBOR format - @sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for + @sa see @ref from_msgpack(InputType&&, const bool, const bool) for the related MessagePack format - @sa @ref from_bson(detail::input_adapter&&, const bool, const bool) for + @sa see @ref from_bson(InputType&&, const bool, const bool) for the related BSON format @since version 3.1.0; added @a allow_exceptions parameter since 3.2.0 @@ -7847,7 +7856,7 @@ class basic_json } /*! - @copydoc from_ubjson(detail::input_adapter&&, const bool, const bool) + @copydoc from_ubjson(InputType&&, const bool, const bool) */ template JSON_HEDLEY_WARN_UNUSED_RESULT @@ -7881,6 +7890,7 @@ class basic_json basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); + // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7938,12 +7948,12 @@ class basic_json BSON format to a JSON value.,from_bson} @sa http://bsonspec.org/spec.html - @sa @ref to_bson(const basic_json&) for the analogous serialization - @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool, const cbor_tag_handler_t) for the + @sa see @ref to_bson(const basic_json&) for the analogous serialization + @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the related CBOR format - @sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for + @sa see @ref from_msgpack(InputType&&, const bool, const bool) for the related MessagePack format - @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the + @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the related UBJSON format */ template @@ -7960,7 +7970,7 @@ class basic_json } /*! - @copydoc from_bson(detail::input_adapter&&, const bool, const bool) + @copydoc from_bson(InputType&&, const bool, const bool) */ template JSON_HEDLEY_WARN_UNUSED_RESULT @@ -7994,6 +8004,7 @@ class basic_json basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); + // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -8176,7 +8187,7 @@ class basic_json @liveexample{The following code shows how a JSON object is flattened to an object whose keys consist of JSON pointers.,flatten} - @sa @ref unflatten() for the reverse function + @sa see @ref unflatten() for the reverse function @since version 2.0.0 */ @@ -8213,7 +8224,7 @@ class basic_json @liveexample{The following code shows how a flattened JSON object is unflattened into the original nested JSON object.,unflatten} - @sa @ref flatten() for the reverse function + @sa see @ref flatten() for the reverse function @since version 2.0.0 */ @@ -8271,7 +8282,7 @@ class basic_json @liveexample{The following code shows how a JSON patch is applied to a value.,patch} - @sa @ref diff -- create a JSON patch by comparing two JSON values + @sa see @ref diff -- create a JSON patch by comparing two JSON values @sa [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902) @sa [RFC 6901 (JSON Pointer)](https://tools.ietf.org/html/rfc6901) @@ -8372,7 +8383,7 @@ class basic_json // if there exists a parent it cannot be primitive default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } }; @@ -8428,12 +8439,14 @@ class basic_json // check if desired value is present if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end())) { + // NOLINTNEXTLINE(performance-inefficient-string-concatenation) JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", val)); } // check if result is of type string if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string())) { + // NOLINTNEXTLINE(performance-inefficient-string-concatenation) JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", val)); } @@ -8566,8 +8579,8 @@ class basic_json @liveexample{The following code shows how a JSON patch is created as a diff for two JSON values.,diff} - @sa @ref patch -- apply a JSON patch - @sa @ref merge_patch -- apply a JSON Merge Patch + @sa see @ref patch -- apply a JSON patch + @sa see @ref merge_patch -- apply a JSON Merge Patch @sa [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902) @@ -8648,12 +8661,12 @@ class basic_json for (auto it = source.cbegin(); it != source.cend(); ++it) { // escape the key name to be used in a JSON patch - const auto key = detail::escape(it.key()); + const auto path_key = path + "/" + detail::escape(it.key()); if (target.find(it.key()) != target.end()) { // recursive call to compare object values at key it - auto temp_diff = diff(it.value(), target[it.key()], path + "/" + key); + auto temp_diff = diff(it.value(), target[it.key()], path_key); result.insert(result.end(), temp_diff.begin(), temp_diff.end()); } else @@ -8661,7 +8674,7 @@ class basic_json // found a key that is not in o -> remove it result.push_back(object( { - {"op", "remove"}, {"path", path + "/" + key} + {"op", "remove"}, {"path", path_key} })); } } @@ -8672,10 +8685,10 @@ class basic_json if (source.find(it.key()) == source.end()) { // found a key that is not in this -> add it - const auto key = detail::escape(it.key()); + const auto path_key = path + "/" + detail::escape(it.key()); result.push_back( { - {"op", "add"}, {"path", path + "/" + key}, + {"op", "add"}, {"path", path_key}, {"value", it.value()} }); } @@ -8744,7 +8757,7 @@ class basic_json @liveexample{The following code shows how a JSON Merge Patch is applied to a JSON document.,merge_patch} - @sa @ref patch -- apply a JSON patch + @sa see @ref patch -- apply a JSON patch @sa [RFC 7396 (JSON Merge Patch)](https://tools.ietf.org/html/rfc7396) @since version 3.0.0 @@ -8843,8 +8856,8 @@ struct less<::nlohmann::detail::value_t> @since version 1.0.0 */ template<> -inline void swap(nlohmann::json& j1, nlohmann::json& j2) noexcept( - is_nothrow_move_constructible::value&& +inline void swap(nlohmann::json& j1, nlohmann::json& j2) noexcept( // NOLINT(readability-inconsistent-declaration-parameter-name) + is_nothrow_move_constructible::value&& // NOLINT(misc-redundant-expression) is_nothrow_move_assignable::value ) { diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index 330677c4d6..cf5f133e70 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -1,7 +1,11 @@ #pragma once #include // less +#include // initializer_list +#include // input_iterator_tag, iterator_traits #include // allocator +#include // for out_of_range +#include // enable_if, is_convertible #include // pair #include // vector diff --git a/include/nlohmann/thirdparty/hedley/hedley.hpp b/include/nlohmann/thirdparty/hedley/hedley.hpp index 36f9fe81a3..b309e989fc 100644 --- a/include/nlohmann/thirdparty/hedley/hedley.hpp +++ b/include/nlohmann/thirdparty/hedley/hedley.hpp @@ -1,3 +1,5 @@ +#pragma once + /* Hedley - https://nemequ.github.io/hedley * Created by Evan Nemerson * diff --git a/include/nlohmann/thirdparty/hedley/hedley_undef.hpp b/include/nlohmann/thirdparty/hedley/hedley_undef.hpp index e74f4dfbfe..d2b37a16d6 100644 --- a/include/nlohmann/thirdparty/hedley/hedley_undef.hpp +++ b/include/nlohmann/thirdparty/hedley/hedley_undef.hpp @@ -1,3 +1,5 @@ +#pragma once + #undef JSON_HEDLEY_ALWAYS_INLINE #undef JSON_HEDLEY_ARM_VERSION #undef JSON_HEDLEY_ARM_VERSION_CHECK diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index d34b790056..282f33291f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -72,6 +72,7 @@ SOFTWARE. #include // exception #include // runtime_error #include // to_string +#include // vector // #include @@ -165,6 +166,8 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept #include // pair // #include + + /* Hedley - https://nemequ.github.io/hedley * Created by Evan Nemerson * @@ -2237,12 +2240,6 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_HAS_CPP_14 #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 - // disable documentation warnings on clang #if defined(__clang__) #pragma GCC diagnostic push @@ -2646,7 +2643,7 @@ class exception : public std::exception } /// the id of the exception - const int id; + const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes) protected: JSON_HEDLEY_NON_NULL(3) @@ -2708,6 +2705,7 @@ class exception : public std::exception return a + "/" + detail::escape(b); }) + ") "; #else + static_cast(leaf_element); return ""; #endif } @@ -3846,7 +3844,7 @@ void from_json(const BasicJsonType& j, std::valarray& l) } template -auto from_json(const BasicJsonType& j, T (&arr)[N]) +auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) -> decltype(j.template get(), void()) { for (std::size_t i = 0; i < N; ++i) @@ -3953,7 +3951,7 @@ void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) } ConstructibleObjectType ret; - auto inner_object = j.template get_ptr(); + const auto* inner_object = j.template get_ptr(); using value_type = typename ConstructibleObjectType::value_type; std::transform( inner_object->begin(), inner_object->end(), @@ -4080,9 +4078,9 @@ struct from_json_fn /// namespace to hold default `from_json` function /// to see why this is required: /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html -namespace +namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces) { -constexpr const auto& from_json = detail::static_const::value; +constexpr const auto& from_json = detail::static_const::value; // NOLINT(misc-definitions-in-headers) } // namespace } // namespace nlohmann @@ -4105,6 +4103,7 @@ constexpr const auto& from_json = detail::static_const::va #include // input_iterator_tag #include // string, to_string #include // tuple_size, get, tuple_element +#include // move // #include @@ -4145,7 +4144,9 @@ template class iteration_proxy_value const string_type empty_str{}; public: - explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {} + explicit iteration_proxy_value(IteratorType it) noexcept + : anchor(std::move(it)) + {} /// dereference operator (needed for range-based for) iteration_proxy_value& operator*() @@ -4346,8 +4347,7 @@ struct external_constructor static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b) { j.m_type = value_t::binary; - typename BasicJsonType::binary_t value{b}; - j.m_value = value; + j.m_value = typename BasicJsonType::binary_t(b); j.assert_invariant(); } @@ -4355,8 +4355,7 @@ struct external_constructor static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b) { j.m_type = value_t::binary; - typename BasicJsonType::binary_t value{std::move(b)}; - j.m_value = value; + j.m_value = typename BasicJsonType::binary_t(std::move(b));; j.assert_invariant(); } }; @@ -4603,9 +4602,9 @@ void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj) template < typename BasicJsonType, typename T, std::size_t N, enable_if_t < !std::is_constructible::value, + const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) int > = 0 > -void to_json(BasicJsonType& j, const T(&arr)[N]) +void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) { external_constructor::construct(j, arr); } @@ -4648,9 +4647,11 @@ struct to_json_fn } // namespace detail /// namespace to hold default `to_json` function -namespace +/// to see why this is required: +/// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html +namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces) { -constexpr const auto& to_json = detail::static_const::value; +constexpr const auto& to_json = detail::static_const::value; // NOLINT(misc-definitions-in-headers) } // namespace } // namespace nlohmann @@ -4774,9 +4775,9 @@ class byte_container_with_subtype : public BinaryType @exceptionsafety No-throw guarantee: this member function never throws exceptions. - @sa @ref subtype() -- return the binary subtype - @sa @ref clear_subtype() -- clears the binary subtype - @sa @ref has_subtype() -- returns whether or not the binary value has a + @sa see @ref subtype() -- return the binary subtype + @sa see @ref clear_subtype() -- clears the binary subtype + @sa see @ref has_subtype() -- returns whether or not the binary value has a subtype @since version 3.8.0 @@ -4801,9 +4802,9 @@ class byte_container_with_subtype : public BinaryType @exceptionsafety No-throw guarantee: this member function never throws exceptions. - @sa @ref set_subtype() -- sets the binary subtype - @sa @ref clear_subtype() -- clears the binary subtype - @sa @ref has_subtype() -- returns whether or not the binary value has a + @sa see @ref set_subtype() -- sets the binary subtype + @sa see @ref clear_subtype() -- clears the binary subtype + @sa see @ref has_subtype() -- returns whether or not the binary value has a subtype @since version 3.8.0 @@ -4823,9 +4824,9 @@ class byte_container_with_subtype : public BinaryType @exceptionsafety No-throw guarantee: this member function never throws exceptions. - @sa @ref subtype() -- return the binary subtype - @sa @ref set_subtype() -- sets the binary subtype - @sa @ref clear_subtype() -- clears the binary subtype + @sa see @ref subtype() -- return the binary subtype + @sa see @ref set_subtype() -- sets the binary subtype + @sa see @ref clear_subtype() -- clears the binary subtype @since version 3.8.0 */ @@ -4846,9 +4847,9 @@ class byte_container_with_subtype : public BinaryType @exceptionsafety No-throw guarantee: this member function never throws exceptions. - @sa @ref subtype() -- return the binary subtype - @sa @ref set_subtype() -- sets the binary subtype - @sa @ref has_subtype() -- returns whether or not the binary value has a + @sa see @ref subtype() -- return the binary subtype + @sa see @ref set_subtype() -- sets the binary subtype + @sa see @ref has_subtype() -- returns whether or not the binary value has a subtype @since version 3.8.0 @@ -4875,7 +4876,8 @@ class byte_container_with_subtype : public BinaryType // #include -#include // size_t, uint8_t +#include // uint8_t +#include // size_t #include // hash // #include @@ -4987,7 +4989,7 @@ std::size_t hash(const BasicJsonType& j) } default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE return 0; // LCOV_EXCL_LINE } } @@ -5060,9 +5062,10 @@ class file_input_adapter // make class move-only file_input_adapter(const file_input_adapter&) = delete; - file_input_adapter(file_input_adapter&&) = default; + file_input_adapter(file_input_adapter&&) noexcept = default; file_input_adapter& operator=(const file_input_adapter&) = delete; file_input_adapter& operator=(file_input_adapter&&) = delete; + ~file_input_adapter() = default; std::char_traits::int_type get_character() noexcept { @@ -5106,9 +5109,10 @@ class input_stream_adapter // delete because of pointer members input_stream_adapter(const input_stream_adapter&) = delete; input_stream_adapter& operator=(input_stream_adapter&) = delete; - input_stream_adapter& operator=(input_stream_adapter&& rhs) = delete; + input_stream_adapter& operator=(input_stream_adapter&&) = delete; - input_stream_adapter(input_stream_adapter&& rhs) noexcept : is(rhs.is), sb(rhs.sb) + input_stream_adapter(input_stream_adapter&& rhs) noexcept + : is(rhs.is), sb(rhs.sb) { rhs.is = nullptr; rhs.sb = nullptr; @@ -5143,7 +5147,8 @@ class iterator_input_adapter using char_type = typename std::iterator_traits::value_type; iterator_input_adapter(IteratorType first, IteratorType last) - : current(std::move(first)), end(std::move(last)) {} + : current(std::move(first)), end(std::move(last)) + {} typename std::char_traits::int_type get_character() { @@ -5168,7 +5173,6 @@ class iterator_input_adapter { return current == end; } - }; @@ -5411,7 +5415,7 @@ struct container_input_adapter_factory< ContainerType, } }; -} +} // namespace container_input_adapter_factory_impl template typename container_input_adapter_factory_impl::container_input_adapter_factory::adapter_type input_adapter(const ContainerType& container) @@ -5453,7 +5457,7 @@ contiguous_bytes_input_adapter input_adapter(CharT b) } template -auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + N)) +auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + N)) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) { return input_adapter(array, array + N); } @@ -5482,7 +5486,7 @@ class span_input_adapter contiguous_bytes_input_adapter&& get() { - return std::move(ia); + return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg) } private: @@ -5622,6 +5626,11 @@ struct json_sax const std::string& last_token, const detail::exception& ex) = 0; + json_sax() = default; + json_sax(const json_sax&) = default; + json_sax(json_sax&&) noexcept = default; + json_sax& operator=(const json_sax&) = default; + json_sax& operator=(json_sax&&) noexcept = default; virtual ~json_sax() = default; }; @@ -5652,7 +5661,7 @@ class json_sax_dom_parser using binary_t = typename BasicJsonType::binary_t; /*! - @param[in, out] r reference to a JSON value that is manipulated while + @param[in,out] r reference to a JSON value that is manipulated while parsing @param[in] allow_exceptions_ whether parse errors yield exceptions */ @@ -5662,9 +5671,9 @@ class json_sax_dom_parser // make class move-only json_sax_dom_parser(const json_sax_dom_parser&) = delete; - json_sax_dom_parser(json_sax_dom_parser&&) = default; + json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete; - json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; + json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) ~json_sax_dom_parser() = default; bool null() @@ -5837,9 +5846,9 @@ class json_sax_dom_callback_parser // make class move-only json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete; - json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; + json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete; - json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; + json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) ~json_sax_dom_callback_parser() = default; bool null() @@ -6319,7 +6328,7 @@ class lexer : public lexer_base public: using token_type = typename lexer_base::token_type; - explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) + explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept : ia(std::move(adapter)) , ignore_comments(ignore_comments_) , decimal_point_char(static_cast(get_decimal_point())) @@ -6327,9 +6336,9 @@ class lexer : public lexer_base // delete because of pointer members lexer(const lexer&) = delete; - lexer(lexer&&) = default; + lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) lexer& operator=(lexer&) = delete; - lexer& operator=(lexer&&) = default; + lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) ~lexer() = default; private: @@ -7205,7 +7214,7 @@ class lexer : public lexer_base // all other characters are rejected outside scan_number() default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } scan_number_minus: @@ -7443,7 +7452,7 @@ class lexer : public lexer_base // we are done scanning a number) unget(); - char* endptr = nullptr; + char* endptr = nullptr; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) errno = 0; // try to parse integers first and fall back to floats @@ -7654,7 +7663,7 @@ class lexer : public lexer_base { // escape control characters std::array cs{{}}; - (std::snprintf)(cs.data(), cs.size(), "", static_cast(c)); + (std::snprintf)(cs.data(), cs.size(), "", static_cast(c)); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) result += cs.data(); } else @@ -8037,16 +8046,16 @@ class binary_reader @param[in] adapter input adapter to read from */ - explicit binary_reader(InputAdapterType&& adapter) : ia(std::move(adapter)) + explicit binary_reader(InputAdapterType&& adapter) noexcept : ia(std::move(adapter)) { (void)detail::is_sax_static_asserts {}; } // make class move-only binary_reader(const binary_reader&) = delete; - binary_reader(binary_reader&&) = default; + binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) binary_reader& operator=(const binary_reader&) = delete; - binary_reader& operator=(binary_reader&&) = default; + binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) ~binary_reader() = default; /*! @@ -8055,7 +8064,7 @@ class binary_reader @param[in] strict whether to expect the input to be consumed completed @param[in] tag_handler how to treat CBOR tags - @return + @return whether parsing was successful */ JSON_HEDLEY_NON_NULL(3) bool sax_parse(const input_format_t format, @@ -8085,7 +8094,7 @@ class binary_reader break; default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } // strict mode: next byte must be EOF @@ -8139,7 +8148,7 @@ class binary_reader /*! @brief Parses a C-style string from the BSON input. - @param[in, out] result A reference to the string variable where the read + @param[in,out] result A reference to the string variable where the read string is to be stored. @return `true` if the \x00-byte indicating the end of the string was encountered before the EOF; false` indicates an unexpected EOF. @@ -8167,7 +8176,7 @@ class binary_reader input. @param[in] len The length (including the zero-byte at the end) of the string to be read. - @param[in, out] result A reference to the string variable where the read + @param[in,out] result A reference to the string variable where the read string is to be stored. @tparam NumberType The type of the length @a len @pre len >= 1 @@ -8188,7 +8197,7 @@ class binary_reader /*! @brief Parses a byte array input of length @a len from the BSON input. @param[in] len The length of the byte array to be read. - @param[in, out] result A reference to the binary variable where the read + @param[in,out] result A reference to the binary variable where the read array is to be stored. @tparam NumberType The type of the length @a len @pre len >= 0 @@ -8281,7 +8290,7 @@ class binary_reader default: // anything else not supported (yet) { std::array cr{{}}; - (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type)); + (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type)); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), BasicJsonType())); } } @@ -8720,7 +8729,7 @@ class binary_reader } default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE return false; // LCOV_EXCL_LINE } } @@ -9820,7 +9829,7 @@ class binary_reader { return false; } - result = static_cast(number); + result = static_cast(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char return true; } @@ -10177,8 +10186,8 @@ class binary_reader } // parse number string - auto number_ia = detail::input_adapter(std::forward(number_vector)); - auto number_lexer = detail::lexer(std::move(number_ia), false); + using ia_type = decltype(detail::input_adapter(number_vector)); + auto number_lexer = detail::lexer(detail::input_adapter(number_vector), false); const auto result_number = number_lexer.scan(); const auto number_string = number_lexer.get_token_string(); const auto result_remainder = number_lexer.scan(); @@ -10253,7 +10262,7 @@ class binary_reader bool get_number(const input_format_t format, NumberType& result) { // step 1: read input into array with system's byte order - std::array vec; + std::array vec{}; for (std::size_t i = 0; i < sizeof(NumberType); ++i) { get(); @@ -10366,7 +10375,7 @@ class binary_reader std::string get_token_string() const { std::array cr{{}}; - (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(current)); + (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(current)); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) return std::string{cr.data()}; } @@ -10401,7 +10410,7 @@ class binary_reader break; default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } return error_msg + " " + context + ": " + detail; @@ -10481,7 +10490,7 @@ enum class parse_event_t : uint8_t template using parser_callback_t = - std::function; + std::function; /*! @brief syntax analysis @@ -10927,6 +10936,7 @@ class parser /// whether to throw exceptions in case of errors const bool allow_exceptions = true; }; + } // namespace detail } // namespace nlohmann @@ -11024,7 +11034,7 @@ class primitive_iterator_t return *this; } - primitive_iterator_t const operator++(int) noexcept + primitive_iterator_t const operator++(int) noexcept // NOLINT(readability-const-return-type) { auto result = *this; ++m_it; @@ -11037,7 +11047,7 @@ class primitive_iterator_t return *this; } - primitive_iterator_t const operator--(int) noexcept + primitive_iterator_t const operator--(int) noexcept // NOLINT(readability-const-return-type) { auto result = *this; --m_it; @@ -11167,8 +11177,10 @@ class iter_impl typename BasicJsonType::const_reference, typename BasicJsonType::reference>::type; - /// default constructor iter_impl() = default; + ~iter_impl() = default; + iter_impl(iter_impl&&) noexcept = default; + iter_impl& operator=(iter_impl&&) noexcept = default; /*! @brief constructor for a given JSON instance @@ -11230,8 +11242,11 @@ class iter_impl */ iter_impl& operator=(const iter_impl& other) noexcept { - m_object = other.m_object; - m_it = other.m_it; + if (&other != this) + { + m_object = other.m_object; + m_it = other.m_it; + } return *this; } @@ -11250,7 +11265,7 @@ class iter_impl @return const/non-const iterator @note It is not checked whether @a other is initialized. */ - iter_impl& operator=(const iter_impl::type>& other) noexcept + iter_impl& operator=(const iter_impl::type>& other) noexcept // NOLINT(cert-oop54-cpp) { m_object = other.m_object; m_it = other.m_it; @@ -11401,7 +11416,7 @@ class iter_impl @brief post-increment (it++) @pre The iterator is initialized; i.e. `m_object != nullptr`. */ - iter_impl const operator++(int) + iter_impl const operator++(int) // NOLINT(readability-const-return-type) { auto result = *this; ++(*this); @@ -11444,7 +11459,7 @@ class iter_impl @brief post-decrement (it--) @pre The iterator is initialized; i.e. `m_object != nullptr`. */ - iter_impl const operator--(int) + iter_impl const operator--(int) // NOLINT(readability-const-return-type) { auto result = *this; --(*this); @@ -11785,7 +11800,7 @@ class json_reverse_iterator : public std::reverse_iterator explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {} /// post-increment (it++) - json_reverse_iterator const operator++(int) + json_reverse_iterator const operator++(int) // NOLINT(readability-const-return-type) { return static_cast(base_iterator::operator++(1)); } @@ -11797,7 +11812,7 @@ class json_reverse_iterator : public std::reverse_iterator } /// post-decrement (it--) - json_reverse_iterator const operator--(int) + json_reverse_iterator const operator--(int) // NOLINT(readability-const-return-type) { return static_cast(base_iterator::operator--(1)); } @@ -11952,9 +11967,9 @@ class json_pointer @complexity Linear in the length of @a ptr. - @sa @ref operator/=(std::string) to append a reference token - @sa @ref operator/=(std::size_t) to append an array index - @sa @ref operator/(const json_pointer&, const json_pointer&) for a binary operator + @sa see @ref operator/=(std::string) to append a reference token + @sa see @ref operator/=(std::size_t) to append an array index + @sa see @ref operator/(const json_pointer&, const json_pointer&) for a binary operator @since version 3.6.0 */ @@ -11976,9 +11991,9 @@ class json_pointer @complexity Amortized constant. - @sa @ref operator/=(const json_pointer&) to append a JSON pointer - @sa @ref operator/=(std::size_t) to append an array index - @sa @ref operator/(const json_pointer&, std::size_t) for a binary operator + @sa see @ref operator/=(const json_pointer&) to append a JSON pointer + @sa see @ref operator/=(std::size_t) to append an array index + @sa see @ref operator/(const json_pointer&, std::size_t) for a binary operator @since version 3.6.0 */ @@ -11998,9 +12013,9 @@ class json_pointer @complexity Amortized constant. - @sa @ref operator/=(const json_pointer&) to append a JSON pointer - @sa @ref operator/=(std::string) to append a reference token - @sa @ref operator/(const json_pointer&, std::string) for a binary operator + @sa see @ref operator/=(const json_pointer&) to append a JSON pointer + @sa see @ref operator/=(std::string) to append a reference token + @sa see @ref operator/(const json_pointer&, std::string) for a binary operator @since version 3.6.0 */ @@ -12020,7 +12035,7 @@ class json_pointer @complexity Linear in the length of @a lhs and @a rhs. - @sa @ref operator/=(const json_pointer&) to append a JSON pointer + @sa see @ref operator/=(const json_pointer&) to append a JSON pointer @since version 3.6.0 */ @@ -12041,11 +12056,11 @@ class json_pointer @complexity Linear in the length of @a ptr. - @sa @ref operator/=(std::string) to append a reference token + @sa see @ref operator/=(std::string) to append a reference token @since version 3.6.0 */ - friend json_pointer operator/(const json_pointer& ptr, std::string token) + friend json_pointer operator/(const json_pointer& ptr, std::string token) // NOLINT(performance-unnecessary-value-param) { return json_pointer(ptr) /= std::move(token); } @@ -12061,7 +12076,7 @@ class json_pointer @complexity Linear in the length of @a ptr. - @sa @ref operator/=(std::size_t) to append an array index + @sa see @ref operator/=(std::size_t) to append an array index @since version 3.6.0 */ @@ -12212,7 +12227,7 @@ class json_pointer } std::size_t processed_chars = 0; - unsigned long long res = 0; + unsigned long long res = 0; // NOLINT(runtime/int) JSON_TRY { res = std::stoull(s, &processed_chars); @@ -12230,7 +12245,7 @@ class json_pointer // only triggered on special platforms (like 32bit), see also // https://github.com/nlohmann/json/pull/2203 - if (res >= static_cast((std::numeric_limits::max)())) + if (res >= static_cast((std::numeric_limits::max)())) // NOLINT(runtime/int) { JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", BasicJsonType())); // LCOV_EXCL_LINE } @@ -12262,7 +12277,7 @@ class json_pointer */ BasicJsonType& get_and_create(BasicJsonType& j) const { - auto result = &j; + auto* result = &j; // in case no reference tokens exist, return a reference to the JSON value // j which will be overwritten by a primitive value @@ -12836,7 +12851,7 @@ class json_ref {} // class should be movable only - json_ref(json_ref&&) = default; + json_ref(json_ref&&) noexcept = default; json_ref(const json_ref&) = delete; json_ref& operator=(const json_ref&) = delete; json_ref& operator=(json_ref&&) = delete; @@ -12870,6 +12885,8 @@ class json_ref // #include +// #include + // #include // #include @@ -12879,11 +12896,12 @@ class json_ref #include // reverse #include // array +#include // isnan, isinf #include // uint8_t, uint16_t, uint32_t, uint64_t #include // memcpy #include // numeric_limits #include // string -#include // isnan, isinf +#include // move // #include @@ -12913,6 +12931,12 @@ template struct output_adapter_protocol virtual void write_character(CharType c) = 0; virtual void write_characters(const CharType* s, std::size_t length) = 0; virtual ~output_adapter_protocol() = default; + + output_adapter_protocol() = default; + output_adapter_protocol(const output_adapter_protocol&) = default; + output_adapter_protocol(output_adapter_protocol&&) noexcept = default; + output_adapter_protocol& operator=(const output_adapter_protocol&) = default; + output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default; }; /// a type to simplify interfaces @@ -13040,7 +13064,7 @@ class binary_writer @param[in] adapter output adapter to write to */ - explicit binary_writer(output_adapter_t adapter) : oa(adapter) + explicit binary_writer(output_adapter_t adapter) : oa(std::move(adapter)) { JSON_ASSERT(oa); } @@ -14143,7 +14167,7 @@ class binary_writer // LCOV_EXCL_START default: - JSON_ASSERT(false); + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) return 0ul; // LCOV_EXCL_STOP } @@ -14154,7 +14178,6 @@ class binary_writer key @a name. @param name The name to associate with the JSON entity @a j within the current BSON document - @return The size of the BSON entry */ void write_bson_element(const string_t& name, const BasicJsonType& j) @@ -14190,7 +14213,7 @@ class binary_writer // LCOV_EXCL_START default: - JSON_ASSERT(false); + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) return; // LCOV_EXCL_STOP } @@ -14199,8 +14222,8 @@ class binary_writer /*! @brief Calculates the size of the BSON serialization of the given JSON-object @a j. - @param[in] j JSON value to serialize - @pre j.type() == value_t::object + @param[in] value JSON value to serialize + @pre value.type() == value_t::object */ static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value) { @@ -14214,8 +14237,8 @@ class binary_writer } /*! - @param[in] j JSON value to serialize - @pre j.type() == value_t::object + @param[in] value JSON value to serialize + @pre value.type() == value_t::object */ void write_bson_object(const typename BasicJsonType::object_t& value) { @@ -14513,7 +14536,7 @@ class binary_writer void write_number(const NumberType n) { // step 1: write number to array of length NumberType - std::array vec; + std::array vec{}; std::memcpy(vec.data(), &n, sizeof(NumberType)); // step 2: write array to output (with possible reordering) @@ -14818,7 +14841,7 @@ boundaries compute_boundaries(FloatType value) using bits_type = typename std::conditional::type; - const std::uint64_t bits = reinterpret_bits(value); + const auto bits = static_cast(reinterpret_bits(value)); const std::uint64_t E = bits >> (kPrecision - 1); const std::uint64_t F = bits & (kHiddenBit - 1); @@ -15236,7 +15259,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, JSON_ASSERT(p1 > 0); - std::uint32_t pow10; + std::uint32_t pow10{}; const int k = find_largest_pow10(p1, pow10); // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1) @@ -16071,7 +16094,7 @@ class serializer } default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } } @@ -16092,7 +16115,7 @@ class serializer */ void dump_escaped(const string_t& s, const bool ensure_ascii) { - std::uint32_t codepoint; + std::uint32_t codepoint{}; std::uint8_t state = UTF8_ACCEPT; std::size_t bytes = 0; // number of bytes written to string_buffer @@ -16167,12 +16190,14 @@ class serializer { if (codepoint <= 0xFFFF) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) (std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x", static_cast(codepoint)); bytes += 6; } else { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) (std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x", static_cast(0xD7C0u + (codepoint >> 10u)), static_cast(0xDC00u + (codepoint & 0x3FFu))); @@ -16211,6 +16236,7 @@ class serializer case error_handler_t::strict: { std::string sn(3, '\0'); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, BasicJsonType())); } @@ -16270,7 +16296,7 @@ class serializer } default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } break; } @@ -16305,6 +16331,7 @@ class serializer case error_handler_t::strict: { std::string sn(3, '\0'); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, BasicJsonType())); } @@ -16333,7 +16360,7 @@ class serializer } default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } } } @@ -16413,12 +16440,12 @@ class serializer } // use a pointer to fill the buffer - auto buffer_ptr = number_buffer.begin(); + auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg) const bool is_negative = std::is_same::value && !(x >= 0); // see issue #755 number_unsigned_t abs_value; - unsigned int n_chars; + unsigned int n_chars{}; if (is_negative) { @@ -16496,8 +16523,8 @@ class serializer void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/) { - char* begin = number_buffer.data(); - char* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x); + auto* begin = number_buffer.data(); + auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x); o->write_characters(begin, static_cast(end - begin)); } @@ -16508,6 +16535,7 @@ class serializer static constexpr auto d = std::numeric_limits::max_digits10; // the actual conversion + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x); // negative value indicates an error @@ -16518,8 +16546,8 @@ class serializer // erase thousands separator if (thousands_sep != '\0') { - const auto end = std::remove(number_buffer.begin(), - number_buffer.begin() + len, thousands_sep); + auto* const end = std::remove(number_buffer.begin(), + number_buffer.begin() + len, thousands_sep); std::fill(end, number_buffer.end(), '\0'); JSON_ASSERT((end - number_buffer.begin()) <= len); len = (end - number_buffer.begin()); @@ -16528,7 +16556,7 @@ class serializer // convert decimal point to '.' if (decimal_point != '\0' && decimal_point != '.') { - const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point); + auto* const dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point); if (dec_pos != number_buffer.end()) { *dec_pos = '.'; @@ -16614,7 +16642,7 @@ class serializer */ number_unsigned_t remove_sign(number_unsigned_t x) { - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE return x; // LCOV_EXCL_LINE } @@ -16629,7 +16657,7 @@ class serializer */ inline number_unsigned_t remove_sign(number_integer_t x) noexcept { - JSON_ASSERT(x < 0 && x < (std::numeric_limits::max)()); + JSON_ASSERT(x < 0 && x < (std::numeric_limits::max)()); // NOLINT(misc-redundant-expression) return static_cast(-(x + 1)) + 1; } @@ -16669,7 +16697,11 @@ class serializer #include // less +#include // initializer_list +#include // input_iterator_tag, iterator_traits #include // allocator +#include // for out_of_range +#include // enable_if, is_convertible #include // pair #include // vector @@ -16952,7 +16984,7 @@ Format](http://rfc7159.net/rfc7159) @nosubgrouping */ NLOHMANN_BASIC_JSON_TPL_DECLARATION -class basic_json +class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions) { private: template friend struct detail::external_constructor; @@ -17274,7 +17306,7 @@ class basic_json access to object values, a pointer of type `object_t*` must be dereferenced. - @sa @ref array_t -- type for an array value + @sa see @ref array_t -- type for an array value @since version 1.0.0 @@ -17332,7 +17364,7 @@ class basic_json Arrays are stored as pointers in a @ref basic_json type. That is, for any access to array values, a pointer of type `array_t*` must be dereferenced. - @sa @ref object_t -- type for an object value + @sa see @ref object_t -- type for an object value @since version 1.0.0 */ @@ -17481,9 +17513,9 @@ class basic_json Integer number values are stored directly inside a @ref basic_json type. - @sa @ref number_float_t -- type for number values (floating-point) + @sa see @ref number_float_t -- type for number values (floating-point) - @sa @ref number_unsigned_t -- type for number values (unsigned integer) + @sa see @ref number_unsigned_t -- type for number values (unsigned integer) @since version 1.0.0 */ @@ -17553,8 +17585,8 @@ class basic_json Integer number values are stored directly inside a @ref basic_json type. - @sa @ref number_float_t -- type for number values (floating-point) - @sa @ref number_integer_t -- type for number values (integer) + @sa see @ref number_float_t -- type for number values (floating-point) + @sa see @ref number_integer_t -- type for number values (integer) @since version 2.0.0 */ @@ -17620,9 +17652,9 @@ class basic_json Floating-point number values are stored directly inside a @ref basic_json type. - @sa @ref number_integer_t -- type for number values (integer) + @sa see @ref number_integer_t -- type for number values (integer) - @sa @ref number_unsigned_t -- type for number values (unsigned integer) + @sa see @ref number_unsigned_t -- type for number values (unsigned integer) @since version 1.0.0 */ @@ -17693,7 +17725,7 @@ class basic_json - If a subtype is given, it is used and added as unsigned 8-bit integer. - If no subtype is given, the generic binary subtype 0x00 is used. - @sa @ref binary -- create a binary array + @sa see @ref binary -- create a binary array @since version 3.8.0 */ @@ -18028,10 +18060,15 @@ class basic_json JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr); #if JSON_DIAGNOSTICS - JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j) + JSON_TRY { - return j.m_parent == this; - })); + // cppcheck-suppress assertWithSideEffect + JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j) + { + return j.m_parent == this; + })); + } + JSON_CATCH(...) {} // LCOV_EXCL_LINE #else static_cast(check_parents); #endif @@ -18107,7 +18144,7 @@ class basic_json @image html callback_events.png "Example when certain parse events are triggered" - @sa @ref parser_callback_t for more information and examples + @sa see @ref parser_callback_t for more information and examples */ using parse_event_t = detail::parse_event_t; @@ -18156,7 +18193,7 @@ class basic_json should be kept (`true`) or not (`false`). In the latter case, it is either skipped completely or replaced by an empty discarded object. - @sa @ref parse for examples + @sa see @ref parse for examples @since version 1.0.0 */ @@ -18197,7 +18234,7 @@ class basic_json @liveexample{The following code shows the constructor for different @ref value_t values,basic_json__value_t} - @sa @ref clear() -- restores the postcondition of this constructor + @sa see @ref clear() -- restores the postcondition of this constructor @since version 1.0.0 */ @@ -18273,8 +18310,7 @@ class basic_json - @a CompatibleType is not a different @ref basic_json type (i.e. with different template arguments) - @a CompatibleType is not a @ref basic_json nested type (e.g., @ref json_pointer, @ref iterator, etc ...) - - @ref @ref json_serializer has a - `to_json(basic_json_t&, CompatibleType&&)` method + - `json_serializer` has a `to_json(basic_json_t&, CompatibleType&&)` method @tparam U = `uncvref_t` @@ -18298,7 +18334,7 @@ class basic_json typename U = detail::uncvref_t, detail::enable_if_t < !detail::is_basic_json::value && detail::is_compatible_type::value, int > = 0 > - basic_json(CompatibleType && val) noexcept(noexcept( + basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape) JSONSerializer::to_json(std::declval(), std::forward(val)))) { @@ -18380,7 +18416,7 @@ class basic_json m_type = value_t::discarded; break; default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } set_parents(); assert_invariant(); @@ -18453,9 +18489,9 @@ class basic_json @liveexample{The example below shows how JSON values are created from initializer lists.,basic_json__list_init_t} - @sa @ref array(initializer_list_t) -- create a JSON array + @sa see @ref array(initializer_list_t) -- create a JSON array value from an initializer list - @sa @ref object(initializer_list_t) -- create a JSON object + @sa see @ref object(initializer_list_t) -- create a JSON object value from an initializer list @since version 1.0.0 @@ -18636,9 +18672,9 @@ class basic_json @liveexample{The following code shows an example for the `array` function.,array} - @sa @ref basic_json(initializer_list_t, bool, value_t) -- + @sa see @ref basic_json(initializer_list_t, bool, value_t) -- create a JSON value from an initializer list - @sa @ref object(initializer_list_t) -- create a JSON object + @sa see @ref object(initializer_list_t) -- create a JSON object value from an initializer list @since version 1.0.0 @@ -18680,9 +18716,9 @@ class basic_json @liveexample{The following code shows an example for the `object` function.,object} - @sa @ref basic_json(initializer_list_t, bool, value_t) -- + @sa see @ref basic_json(initializer_list_t, bool, value_t) -- create a JSON value from an initializer list - @sa @ref array(initializer_list_t) -- create a JSON array + @sa see @ref array(initializer_list_t) -- create a JSON array value from an initializer list @since version 1.0.0 @@ -19185,8 +19221,8 @@ class basic_json @liveexample{The following code exemplifies `type()` for all JSON types.,type} - @sa @ref operator value_t() -- return the type of the JSON value (implicit) - @sa @ref type_name() -- return the type as string + @sa see @ref operator value_t() -- return the type of the JSON value (implicit) + @sa see @ref type_name() -- return the type as string @since version 1.0.0 */ @@ -19212,12 +19248,12 @@ class basic_json @liveexample{The following code exemplifies `is_primitive()` for all JSON types.,is_primitive} - @sa @ref is_structured() -- returns whether JSON value is structured - @sa @ref is_null() -- returns whether JSON value is `null` - @sa @ref is_string() -- returns whether JSON value is a string - @sa @ref is_boolean() -- returns whether JSON value is a boolean - @sa @ref is_number() -- returns whether JSON value is a number - @sa @ref is_binary() -- returns whether JSON value is a binary array + @sa see @ref is_structured() -- returns whether JSON value is structured + @sa see @ref is_null() -- returns whether JSON value is `null` + @sa see @ref is_string() -- returns whether JSON value is a string + @sa see @ref is_boolean() -- returns whether JSON value is a boolean + @sa see @ref is_number() -- returns whether JSON value is a number + @sa see @ref is_binary() -- returns whether JSON value is a binary array @since version 1.0.0 */ @@ -19242,9 +19278,9 @@ class basic_json @liveexample{The following code exemplifies `is_structured()` for all JSON types.,is_structured} - @sa @ref is_primitive() -- returns whether value is primitive - @sa @ref is_array() -- returns whether value is an array - @sa @ref is_object() -- returns whether value is an object + @sa see @ref is_primitive() -- returns whether value is primitive + @sa see @ref is_array() -- returns whether value is an array + @sa see @ref is_object() -- returns whether value is an object @since version 1.0.0 */ @@ -19314,11 +19350,11 @@ class basic_json @liveexample{The following code exemplifies `is_number()` for all JSON types.,is_number} - @sa @ref is_number_integer() -- check if value is an integer or unsigned + @sa see @ref is_number_integer() -- check if value is an integer or unsigned integer number - @sa @ref is_number_unsigned() -- check if value is an unsigned integer + @sa see @ref is_number_unsigned() -- check if value is an unsigned integer number - @sa @ref is_number_float() -- check if value is a floating-point number + @sa see @ref is_number_float() -- check if value is a floating-point number @since version 1.0.0 */ @@ -19344,10 +19380,10 @@ class basic_json @liveexample{The following code exemplifies `is_number_integer()` for all JSON types.,is_number_integer} - @sa @ref is_number() -- check if value is a number - @sa @ref is_number_unsigned() -- check if value is an unsigned integer + @sa see @ref is_number() -- check if value is a number + @sa see @ref is_number_unsigned() -- check if value is an unsigned integer number - @sa @ref is_number_float() -- check if value is a floating-point number + @sa see @ref is_number_float() -- check if value is a floating-point number @since version 1.0.0 */ @@ -19372,10 +19408,10 @@ class basic_json @liveexample{The following code exemplifies `is_number_unsigned()` for all JSON types.,is_number_unsigned} - @sa @ref is_number() -- check if value is a number - @sa @ref is_number_integer() -- check if value is an integer or unsigned + @sa see @ref is_number() -- check if value is a number + @sa see @ref is_number_integer() -- check if value is an integer or unsigned integer number - @sa @ref is_number_float() -- check if value is a floating-point number + @sa see @ref is_number_float() -- check if value is a floating-point number @since version 2.0.0 */ @@ -19400,9 +19436,9 @@ class basic_json @liveexample{The following code exemplifies `is_number_float()` for all JSON types.,is_number_float} - @sa @ref is_number() -- check if value is number - @sa @ref is_number_integer() -- check if value is an integer number - @sa @ref is_number_unsigned() -- check if value is an unsigned integer + @sa see @ref is_number() -- check if value is number + @sa see @ref is_number_integer() -- check if value is an integer number + @sa see @ref is_number_unsigned() -- check if value is an unsigned integer number @since version 1.0.0 @@ -19543,8 +19579,8 @@ class basic_json @liveexample{The following code exemplifies the @ref value_t operator for all JSON types.,operator__value_t} - @sa @ref type() -- return the type of the JSON value (explicit) - @sa @ref type_name() -- return the type as string + @sa see @ref type() -- return the type of the JSON value (explicit) + @sa see @ref type_name() -- return the type as string @since version 1.0.0 */ @@ -19682,7 +19718,7 @@ class basic_json static ReferenceType get_ref_impl(ThisType& obj) { // delegate the call to get_ptr<>() - auto ptr = obj.template get_ptr::type>(); + auto* ptr = obj.template get_ptr::type>(); if (JSON_HEDLEY_LIKELY(ptr != nullptr)) { @@ -19727,7 +19763,7 @@ class basic_json @tparam BasicJsonType == @ref basic_json - @return a copy of *this, converted into @tparam BasicJsonType + @return a copy of *this, converted into @a BasicJsonType @complexity Depending on the implementation of the called `from_json()` method. @@ -19798,7 +19834,7 @@ class basic_json static_assert(std::is_default_constructible::value, "types must be DefaultConstructible when used with get()"); - ValueType ret; + ValueType ret{}; JSONSerializer::from_json(*this, ret); return ret; } @@ -19905,10 +19941,10 @@ class basic_json template < typename T, std::size_t N, - typename Array = T (&)[N], + typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) detail::enable_if_t < detail::has_from_json::value, int > = 0 > - Array get_to(T (&v)[N]) const + Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) noexcept(noexcept(JSONSerializer::from_json( std::declval(), v))) { @@ -19987,7 +20023,7 @@ class basic_json `nullptr` is returned if the value and the requested pointer type does not match.,get__PointerType} - @sa @ref get_ptr() for explicit pointer-member access + @sa see @ref get_ptr() for explicit pointer-member access @since version 1.0.0 */ @@ -20109,7 +20145,7 @@ class basic_json @throw type_error.302 if the value is not binary - @sa @ref is_binary() to check if the value is binary + @sa see @ref is_binary() to check if the value is binary @since version 3.8.0 */ @@ -20259,9 +20295,9 @@ class basic_json @complexity Logarithmic in the size of the container. - @sa @ref operator[](const typename object_t::key_type&) for unchecked + @sa see @ref operator[](const typename object_t::key_type&) for unchecked access by reference - @sa @ref value() for access by value with a default value + @sa see @ref value() for access by value with a default value @since version 1.0.0 @@ -20310,9 +20346,9 @@ class basic_json @complexity Logarithmic in the size of the container. - @sa @ref operator[](const typename object_t::key_type&) for unchecked + @sa see @ref operator[](const typename object_t::key_type&) for unchecked access by reference - @sa @ref value() for access by value with a default value + @sa see @ref value() for access by value with a default value @since version 1.0.0 @@ -20451,9 +20487,9 @@ class basic_json @liveexample{The example below shows how object elements can be read and written using the `[]` operator.,operatorarray__key_type} - @sa @ref at(const typename object_t::key_type&) for access by reference + @sa see @ref at(const typename object_t::key_type&) for access by reference with range checking - @sa @ref value() for access by value with a default value + @sa see @ref value() for access by value with a default value @since version 1.0.0 */ @@ -20500,9 +20536,9 @@ class basic_json @liveexample{The example below shows how object elements can be read using the `[]` operator.,operatorarray__key_type_const} - @sa @ref at(const typename object_t::key_type&) for access by reference + @sa see @ref at(const typename object_t::key_type&) for access by reference with range checking - @sa @ref value() for access by value with a default value + @sa see @ref value() for access by value with a default value @since version 1.0.0 */ @@ -20539,9 +20575,9 @@ class basic_json @liveexample{The example below shows how object elements can be read and written using the `[]` operator.,operatorarray__key_type} - @sa @ref at(const typename object_t::key_type&) for access by reference + @sa see @ref at(const typename object_t::key_type&) for access by reference with range checking - @sa @ref value() for access by value with a default value + @sa see @ref value() for access by value with a default value @since version 1.1.0 */ @@ -20590,9 +20626,9 @@ class basic_json @liveexample{The example below shows how object elements can be read using the `[]` operator.,operatorarray__key_type_const} - @sa @ref at(const typename object_t::key_type&) for access by reference + @sa see @ref at(const typename object_t::key_type&) for access by reference with range checking - @sa @ref value() for access by value with a default value + @sa see @ref value() for access by value with a default value @since version 1.1.0 */ @@ -20653,9 +20689,9 @@ class basic_json @liveexample{The example below shows how object elements can be queried with a default value.,basic_json__value} - @sa @ref at(const typename object_t::key_type&) for access by reference + @sa see @ref at(const typename object_t::key_type&) for access by reference with range checking - @sa @ref operator[](const typename object_t::key_type&) for unchecked + @sa see @ref operator[](const typename object_t::key_type&) for unchecked access by reference @since version 1.0.0 @@ -20730,7 +20766,7 @@ class basic_json @liveexample{The example below shows how object elements can be queried with a default value.,basic_json__value_ptr} - @sa @ref operator[](const json_pointer&) for unchecked access by reference + @sa see @ref operator[](const json_pointer&) for unchecked access by reference @since version 2.0.2 */ @@ -20786,7 +20822,7 @@ class basic_json @liveexample{The following code shows an example for `front()`.,front} - @sa @ref back() -- access the last element + @sa see @ref back() -- access the last element @since version 1.0.0 */ @@ -20830,7 +20866,7 @@ class basic_json @liveexample{The following code shows an example for `back()`.,back} - @sa @ref front() -- access the first element + @sa see @ref front() -- access the first element @since version 1.0.0 */ @@ -20888,11 +20924,11 @@ class basic_json @liveexample{The example shows the result of `erase()` for different JSON types.,erase__IteratorType} - @sa @ref erase(IteratorType, IteratorType) -- removes the elements in + @sa see @ref erase(IteratorType, IteratorType) -- removes the elements in the given range - @sa @ref erase(const typename object_t::key_type&) -- removes the element + @sa see @ref erase(const typename object_t::key_type&) -- removes the element from an object at the given key - @sa @ref erase(const size_type) -- removes the element from an array at + @sa see @ref erase(const size_type) -- removes the element from an array at the given index @since version 1.0.0 @@ -21002,10 +21038,10 @@ class basic_json @liveexample{The example shows the result of `erase()` for different JSON types.,erase__IteratorType_IteratorType} - @sa @ref erase(IteratorType) -- removes the element at a given position - @sa @ref erase(const typename object_t::key_type&) -- removes the element + @sa see @ref erase(IteratorType) -- removes the element at a given position + @sa see @ref erase(const typename object_t::key_type&) -- removes the element from an object at the given key - @sa @ref erase(const size_type) -- removes the element from an array at + @sa see @ref erase(const size_type) -- removes the element from an array at the given index @since version 1.0.0 @@ -21101,10 +21137,10 @@ class basic_json @liveexample{The example shows the effect of `erase()`.,erase__key_type} - @sa @ref erase(IteratorType) -- removes the element at a given position - @sa @ref erase(IteratorType, IteratorType) -- removes the elements in + @sa see @ref erase(IteratorType) -- removes the element at a given position + @sa see @ref erase(IteratorType, IteratorType) -- removes the elements in the given range - @sa @ref erase(const size_type) -- removes the element from an array at + @sa see @ref erase(const size_type) -- removes the element from an array at the given index @since version 1.0.0 @@ -21136,10 +21172,10 @@ class basic_json @liveexample{The example shows the effect of `erase()`.,erase__size_type} - @sa @ref erase(IteratorType) -- removes the element at a given position - @sa @ref erase(IteratorType, IteratorType) -- removes the elements in + @sa see @ref erase(IteratorType) -- removes the element at a given position + @sa see @ref erase(IteratorType, IteratorType) -- removes the elements in the given range - @sa @ref erase(const typename object_t::key_type&) -- removes the element + @sa see @ref erase(const typename object_t::key_type&) -- removes the element from an object at the given key @since version 1.0.0 @@ -21192,7 +21228,7 @@ class basic_json @liveexample{The example shows how `find()` is used.,find__key_type} - @sa @ref contains(KeyT&&) const -- checks whether a key exists + @sa see @ref contains(KeyT&&) const -- checks whether a key exists @since version 1.0.0 */ @@ -21274,8 +21310,8 @@ class basic_json @liveexample{The following code shows an example for `contains()`.,contains} - @sa @ref find(KeyT&&) -- returns an iterator to an object element - @sa @ref contains(const json_pointer&) const -- checks the existence for a JSON pointer + @sa see @ref find(KeyT&&) -- returns an iterator to an object element + @sa see @ref contains(const json_pointer&) const -- checks the existence for a JSON pointer @since version 3.6.0 */ @@ -21308,7 +21344,7 @@ class basic_json @liveexample{The following code shows an example for `contains()`.,contains_json_pointer} - @sa @ref contains(KeyT &&) const -- checks the existence of a key + @sa see @ref contains(KeyT &&) const -- checks the existence of a key @since version 3.7.0 */ @@ -21345,9 +21381,9 @@ class basic_json @liveexample{The following code shows an example for `begin()`.,begin} - @sa @ref cbegin() -- returns a const iterator to the beginning - @sa @ref end() -- returns an iterator to the end - @sa @ref cend() -- returns a const iterator to the end + @sa see @ref cbegin() -- returns a const iterator to the beginning + @sa see @ref end() -- returns an iterator to the end + @sa see @ref cend() -- returns a const iterator to the end @since version 1.0.0 */ @@ -21385,9 +21421,9 @@ class basic_json @liveexample{The following code shows an example for `cbegin()`.,cbegin} - @sa @ref begin() -- returns an iterator to the beginning - @sa @ref end() -- returns an iterator to the end - @sa @ref cend() -- returns a const iterator to the end + @sa see @ref begin() -- returns an iterator to the beginning + @sa see @ref end() -- returns an iterator to the end + @sa see @ref cend() -- returns a const iterator to the end @since version 1.0.0 */ @@ -21416,9 +21452,9 @@ class basic_json @liveexample{The following code shows an example for `end()`.,end} - @sa @ref cend() -- returns a const iterator to the end - @sa @ref begin() -- returns an iterator to the beginning - @sa @ref cbegin() -- returns a const iterator to the beginning + @sa see @ref cend() -- returns a const iterator to the end + @sa see @ref begin() -- returns an iterator to the beginning + @sa see @ref cbegin() -- returns a const iterator to the beginning @since version 1.0.0 */ @@ -21456,9 +21492,9 @@ class basic_json @liveexample{The following code shows an example for `cend()`.,cend} - @sa @ref end() -- returns an iterator to the end - @sa @ref begin() -- returns an iterator to the beginning - @sa @ref cbegin() -- returns a const iterator to the beginning + @sa see @ref end() -- returns an iterator to the end + @sa see @ref begin() -- returns an iterator to the beginning + @sa see @ref cbegin() -- returns a const iterator to the beginning @since version 1.0.0 */ @@ -21486,9 +21522,9 @@ class basic_json @liveexample{The following code shows an example for `rbegin()`.,rbegin} - @sa @ref crbegin() -- returns a const reverse iterator to the beginning - @sa @ref rend() -- returns a reverse iterator to the end - @sa @ref crend() -- returns a const reverse iterator to the end + @sa see @ref crbegin() -- returns a const reverse iterator to the beginning + @sa see @ref rend() -- returns a reverse iterator to the end + @sa see @ref crend() -- returns a const reverse iterator to the end @since version 1.0.0 */ @@ -21523,9 +21559,9 @@ class basic_json @liveexample{The following code shows an example for `rend()`.,rend} - @sa @ref crend() -- returns a const reverse iterator to the end - @sa @ref rbegin() -- returns a reverse iterator to the beginning - @sa @ref crbegin() -- returns a const reverse iterator to the beginning + @sa see @ref crend() -- returns a const reverse iterator to the end + @sa see @ref rbegin() -- returns a reverse iterator to the beginning + @sa see @ref crbegin() -- returns a const reverse iterator to the beginning @since version 1.0.0 */ @@ -21560,9 +21596,9 @@ class basic_json @liveexample{The following code shows an example for `crbegin()`.,crbegin} - @sa @ref rbegin() -- returns a reverse iterator to the beginning - @sa @ref rend() -- returns a reverse iterator to the end - @sa @ref crend() -- returns a const reverse iterator to the end + @sa see @ref rbegin() -- returns a reverse iterator to the beginning + @sa see @ref rend() -- returns a reverse iterator to the end + @sa see @ref crend() -- returns a const reverse iterator to the end @since version 1.0.0 */ @@ -21589,9 +21625,9 @@ class basic_json @liveexample{The following code shows an example for `crend()`.,crend} - @sa @ref rend() -- returns a reverse iterator to the end - @sa @ref rbegin() -- returns a reverse iterator to the beginning - @sa @ref crbegin() -- returns a const reverse iterator to the beginning + @sa see @ref rend() -- returns a reverse iterator to the end + @sa see @ref rbegin() -- returns a reverse iterator to the beginning + @sa see @ref crbegin() -- returns a const reverse iterator to the beginning @since version 1.0.0 */ @@ -21802,7 +21838,7 @@ class basic_json - The complexity is constant. - Has the semantics of `begin() == end()`. - @sa @ref size() -- returns the number of elements + @sa see @ref size() -- returns the number of elements @since version 1.0.0 */ @@ -21874,8 +21910,8 @@ class basic_json - The complexity is constant. - Has the semantics of `std::distance(begin(), end())`. - @sa @ref empty() -- checks whether the container is empty - @sa @ref max_size() -- returns the maximal number of elements + @sa see @ref empty() -- checks whether the container is empty + @sa see @ref max_size() -- returns the maximal number of elements @since version 1.0.0 */ @@ -21946,7 +21982,7 @@ class basic_json - Has the semantics of returning `b.size()` where `b` is the largest possible JSON value. - @sa @ref size() -- returns the number of elements + @sa see @ref size() -- returns the number of elements @since version 1.0.0 */ @@ -22016,7 +22052,7 @@ class basic_json @exceptionsafety No-throw guarantee: this function never throws exceptions. - @sa @ref basic_json(value_t) -- constructor that creates an object with the + @sa see @ref basic_json(value_t) -- constructor that creates an object with the same value than calling `clear()` @since version 1.0.0 @@ -22812,7 +22848,7 @@ class basic_json @since version 1.0.0 */ - void swap(array_t& other) + void swap(array_t& other) // NOLINT(bugprone-exception-escape) { // swap only works for arrays if (JSON_HEDLEY_LIKELY(is_array())) @@ -22845,7 +22881,7 @@ class basic_json @since version 1.0.0 */ - void swap(object_t& other) + void swap(object_t& other) // NOLINT(bugprone-exception-escape) { // swap only works for objects if (JSON_HEDLEY_LIKELY(is_object())) @@ -22878,7 +22914,7 @@ class basic_json @since version 1.0.0 */ - void swap(string_t& other) + void swap(string_t& other) // NOLINT(bugprone-exception-escape) { // swap only works for strings if (JSON_HEDLEY_LIKELY(is_string())) @@ -22911,7 +22947,7 @@ class basic_json @since version 3.8.0 */ - void swap(binary_t& other) + void swap(binary_t& other) // NOLINT(bugprone-exception-escape) { // swap only works for strings if (JSON_HEDLEY_LIKELY(is_binary())) @@ -22924,8 +22960,8 @@ class basic_json } } - /// @copydoc swap(binary_t) - void swap(typename binary_t::container_type& other) + /// @copydoc swap(binary_t&) + void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape) { // swap only works for strings if (JSON_HEDLEY_LIKELY(is_binary())) @@ -23077,7 +23113,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept + friend bool operator==(const_reference lhs, ScalarType rhs) noexcept { return lhs == basic_json(rhs); } @@ -23088,7 +23124,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept + friend bool operator==(ScalarType lhs, const_reference rhs) noexcept { return basic_json(lhs) == rhs; } @@ -23122,7 +23158,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator!=(const_reference lhs, const ScalarType rhs) noexcept + friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept { return lhs != basic_json(rhs); } @@ -23133,7 +23169,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator!=(const ScalarType lhs, const_reference rhs) noexcept + friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept { return basic_json(lhs) != rhs; } @@ -23243,7 +23279,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator<(const_reference lhs, const ScalarType rhs) noexcept + friend bool operator<(const_reference lhs, ScalarType rhs) noexcept { return lhs < basic_json(rhs); } @@ -23254,7 +23290,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator<(const ScalarType lhs, const_reference rhs) noexcept + friend bool operator<(ScalarType lhs, const_reference rhs) noexcept { return basic_json(lhs) < rhs; } @@ -23289,7 +23325,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator<=(const_reference lhs, const ScalarType rhs) noexcept + friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept { return lhs <= basic_json(rhs); } @@ -23300,7 +23336,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator<=(const ScalarType lhs, const_reference rhs) noexcept + friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept { return basic_json(lhs) <= rhs; } @@ -23335,7 +23371,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator>(const_reference lhs, const ScalarType rhs) noexcept + friend bool operator>(const_reference lhs, ScalarType rhs) noexcept { return lhs > basic_json(rhs); } @@ -23346,7 +23382,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator>(const ScalarType lhs, const_reference rhs) noexcept + friend bool operator>(ScalarType lhs, const_reference rhs) noexcept { return basic_json(lhs) > rhs; } @@ -23381,7 +23417,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator>=(const_reference lhs, const ScalarType rhs) noexcept + friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept { return lhs >= basic_json(rhs); } @@ -23392,7 +23428,7 @@ class basic_json */ template::value, int>::type = 0> - friend bool operator>=(const ScalarType lhs, const_reference rhs) noexcept + friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept { return basic_json(lhs) >= rhs; } @@ -23718,7 +23754,9 @@ class basic_json { auto ia = i.get(); return format == input_format_t::json + // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) + // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) : detail::binary_reader(std::move(ia)).sax_parse(format, sax, strict); } @@ -23798,8 +23836,8 @@ class basic_json @liveexample{The following code exemplifies `type_name()` for all JSON types.,type_name} - @sa @ref type() -- return the type of the JSON value - @sa @ref operator value_t() -- return the type of the JSON value (implicit) + @sa see @ref type() -- return the type of the JSON value + @sa see @ref operator value_t() -- return the type of the JSON value (implicit) @since version 1.0.0, public since 2.1.0, `const char*` and `noexcept` since 3.0.0 @@ -23940,10 +23978,10 @@ class basic_json vector in CBOR format.,to_cbor} @sa http://cbor.io - @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool, const cbor_tag_handler_t) for the + @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the analogous deserialization - @sa @ref to_msgpack(const basic_json&) for the related MessagePack format - @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the + @sa see @ref to_msgpack(const basic_json&) for the related MessagePack format + @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the related UBJSON format @since version 2.0.9; compact representation of floating-point numbers @@ -24037,9 +24075,9 @@ class basic_json vector in MessagePack format.,to_msgpack} @sa http://msgpack.org - @sa @ref from_msgpack for the analogous deserialization - @sa @ref to_cbor(const basic_json& for the related CBOR format - @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the + @sa see @ref from_msgpack for the analogous deserialization + @sa see @ref to_cbor(const basic_json& for the related CBOR format + @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the related UBJSON format @since version 2.0.9 @@ -24140,10 +24178,10 @@ class basic_json vector in UBJSON format.,to_ubjson} @sa http://ubjson.org - @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the + @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the analogous deserialization - @sa @ref to_cbor(const basic_json& for the related CBOR format - @sa @ref to_msgpack(const basic_json&) for the related MessagePack format + @sa see @ref to_cbor(const basic_json& for the related CBOR format + @sa see @ref to_msgpack(const basic_json&) for the related MessagePack format @since version 3.1.0 */ @@ -24218,12 +24256,12 @@ class basic_json vector in BSON format.,to_bson} @sa http://bsonspec.org/spec.html - @sa @ref from_bson(detail::input_adapter&&, const bool strict) for the + @sa see @ref from_bson(detail::input_adapter&&, const bool strict) for the analogous deserialization - @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the + @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the related UBJSON format - @sa @ref to_cbor(const basic_json&) for the related CBOR format - @sa @ref to_msgpack(const basic_json&) for the related MessagePack format + @sa see @ref to_cbor(const basic_json&) for the related CBOR format + @sa see @ref to_msgpack(const basic_json&) for the related MessagePack format */ static std::vector to_bson(const basic_json& j) { @@ -24238,7 +24276,7 @@ class basic_json @param j The JSON object to convert to BSON. @param o The output adapter that receives the binary BSON representation. @pre The input `j` shall be an object: `j.is_object() == true` - @sa @ref to_bson(const basic_json&) + @sa see @ref to_bson(const basic_json&) */ static void to_bson(const basic_json& j, detail::output_adapter o) { @@ -24345,10 +24383,10 @@ class basic_json format to a JSON value.,from_cbor} @sa http://cbor.io - @sa @ref to_cbor(const basic_json&) for the analogous serialization - @sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for the + @sa see @ref to_cbor(const basic_json&) for the analogous serialization + @sa see @ref from_msgpack(InputType&&, const bool, const bool) for the related MessagePack format - @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the + @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the related UBJSON format @since version 2.0.9; parameter @a start_index since 2.1.1; changed to @@ -24371,7 +24409,7 @@ class basic_json } /*! - @copydoc from_cbor(detail::input_adapter&&, const bool, const bool, const cbor_tag_handler_t) + @copydoc from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) */ template JSON_HEDLEY_WARN_UNUSED_RESULT @@ -24409,6 +24447,7 @@ class basic_json basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); + // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); return res ? result : basic_json(value_t::discarded); } @@ -24486,12 +24525,12 @@ class basic_json MessagePack format to a JSON value.,from_msgpack} @sa http://msgpack.org - @sa @ref to_msgpack(const basic_json&) for the analogous serialization - @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool, const cbor_tag_handler_t) for the + @sa see @ref to_msgpack(const basic_json&) for the analogous serialization + @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the related CBOR format - @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for + @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the related UBJSON format - @sa @ref from_bson(detail::input_adapter&&, const bool, const bool) for + @sa see @ref from_bson(InputType&&, const bool, const bool) for the related BSON format @since version 2.0.9; parameter @a start_index since 2.1.1; changed to @@ -24513,7 +24552,7 @@ class basic_json } /*! - @copydoc from_msgpack(detail::input_adapter&&, const bool, const bool) + @copydoc from_msgpack(InputType&&, const bool, const bool) */ template JSON_HEDLEY_WARN_UNUSED_RESULT @@ -24548,6 +24587,7 @@ class basic_json basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); + // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -24604,13 +24644,13 @@ class basic_json UBJSON format to a JSON value.,from_ubjson} @sa http://ubjson.org - @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the + @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the analogous serialization - @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool, const cbor_tag_handler_t) for the + @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the related CBOR format - @sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for + @sa see @ref from_msgpack(InputType&&, const bool, const bool) for the related MessagePack format - @sa @ref from_bson(detail::input_adapter&&, const bool, const bool) for + @sa see @ref from_bson(InputType&&, const bool, const bool) for the related BSON format @since version 3.1.0; added @a allow_exceptions parameter since 3.2.0 @@ -24629,7 +24669,7 @@ class basic_json } /*! - @copydoc from_ubjson(detail::input_adapter&&, const bool, const bool) + @copydoc from_ubjson(InputType&&, const bool, const bool) */ template JSON_HEDLEY_WARN_UNUSED_RESULT @@ -24663,6 +24703,7 @@ class basic_json basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); + // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -24720,12 +24761,12 @@ class basic_json BSON format to a JSON value.,from_bson} @sa http://bsonspec.org/spec.html - @sa @ref to_bson(const basic_json&) for the analogous serialization - @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool, const cbor_tag_handler_t) for the + @sa see @ref to_bson(const basic_json&) for the analogous serialization + @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the related CBOR format - @sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for + @sa see @ref from_msgpack(InputType&&, const bool, const bool) for the related MessagePack format - @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the + @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the related UBJSON format */ template @@ -24742,7 +24783,7 @@ class basic_json } /*! - @copydoc from_bson(detail::input_adapter&&, const bool, const bool) + @copydoc from_bson(InputType&&, const bool, const bool) */ template JSON_HEDLEY_WARN_UNUSED_RESULT @@ -24776,6 +24817,7 @@ class basic_json basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); auto ia = i.get(); + // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -24958,7 +25000,7 @@ class basic_json @liveexample{The following code shows how a JSON object is flattened to an object whose keys consist of JSON pointers.,flatten} - @sa @ref unflatten() for the reverse function + @sa see @ref unflatten() for the reverse function @since version 2.0.0 */ @@ -24995,7 +25037,7 @@ class basic_json @liveexample{The following code shows how a flattened JSON object is unflattened into the original nested JSON object.,unflatten} - @sa @ref flatten() for the reverse function + @sa see @ref flatten() for the reverse function @since version 2.0.0 */ @@ -25053,7 +25095,7 @@ class basic_json @liveexample{The following code shows how a JSON patch is applied to a value.,patch} - @sa @ref diff -- create a JSON patch by comparing two JSON values + @sa see @ref diff -- create a JSON patch by comparing two JSON values @sa [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902) @sa [RFC 6901 (JSON Pointer)](https://tools.ietf.org/html/rfc6901) @@ -25154,7 +25196,7 @@ class basic_json // if there exists a parent it cannot be primitive default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE } }; @@ -25210,12 +25252,14 @@ class basic_json // check if desired value is present if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end())) { + // NOLINTNEXTLINE(performance-inefficient-string-concatenation) JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", val)); } // check if result is of type string if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string())) { + // NOLINTNEXTLINE(performance-inefficient-string-concatenation) JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", val)); } @@ -25348,8 +25392,8 @@ class basic_json @liveexample{The following code shows how a JSON patch is created as a diff for two JSON values.,diff} - @sa @ref patch -- apply a JSON patch - @sa @ref merge_patch -- apply a JSON Merge Patch + @sa see @ref patch -- apply a JSON patch + @sa see @ref merge_patch -- apply a JSON Merge Patch @sa [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902) @@ -25430,12 +25474,12 @@ class basic_json for (auto it = source.cbegin(); it != source.cend(); ++it) { // escape the key name to be used in a JSON patch - const auto key = detail::escape(it.key()); + const auto path_key = path + "/" + detail::escape(it.key()); if (target.find(it.key()) != target.end()) { // recursive call to compare object values at key it - auto temp_diff = diff(it.value(), target[it.key()], path + "/" + key); + auto temp_diff = diff(it.value(), target[it.key()], path_key); result.insert(result.end(), temp_diff.begin(), temp_diff.end()); } else @@ -25443,7 +25487,7 @@ class basic_json // found a key that is not in o -> remove it result.push_back(object( { - {"op", "remove"}, {"path", path + "/" + key} + {"op", "remove"}, {"path", path_key} })); } } @@ -25454,10 +25498,10 @@ class basic_json if (source.find(it.key()) == source.end()) { // found a key that is not in this -> add it - const auto key = detail::escape(it.key()); + const auto path_key = path + "/" + detail::escape(it.key()); result.push_back( { - {"op", "add"}, {"path", path + "/" + key}, + {"op", "add"}, {"path", path_key}, {"value", it.value()} }); } @@ -25526,7 +25570,7 @@ class basic_json @liveexample{The following code shows how a JSON Merge Patch is applied to a JSON document.,merge_patch} - @sa @ref patch -- apply a JSON patch + @sa see @ref patch -- apply a JSON patch @sa [RFC 7396 (JSON Merge Patch)](https://tools.ietf.org/html/rfc7396) @since version 3.0.0 @@ -25625,8 +25669,8 @@ struct less<::nlohmann::detail::value_t> @since version 1.0.0 */ template<> -inline void swap(nlohmann::json& j1, nlohmann::json& j2) noexcept( - is_nothrow_move_constructible::value&& +inline void swap(nlohmann::json& j1, nlohmann::json& j2) noexcept( // NOLINT(readability-inconsistent-declaration-parameter-name) + is_nothrow_move_constructible::value&& // NOLINT(misc-redundant-expression) is_nothrow_move_assignable::value ) { @@ -25679,9 +25723,6 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std // restore GCC/clang diagnostic settings -#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) - #pragma GCC diagnostic pop -#endif #if defined(__clang__) #pragma GCC diagnostic pop #endif @@ -25700,6 +25741,8 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std #undef JSON_EXPLICIT // #include + + #undef JSON_HEDLEY_ALWAYS_INLINE #undef JSON_HEDLEY_ARM_VERSION #undef JSON_HEDLEY_ARM_VERSION_CHECK diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e5484fc731..15f55dfa5d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -91,57 +91,7 @@ endif() # one executable for each unit test file ############################################################################# -set(files - src/unit-algorithms.cpp - src/unit-allocator.cpp - src/unit-alt-string.cpp - src/unit-assert_macro.cpp - src/unit-bson.cpp - src/unit-capacity.cpp - src/unit-cbor.cpp - src/unit-class_const_iterator.cpp - src/unit-class_iterator.cpp - src/unit-class_lexer.cpp - src/unit-class_parser.cpp - src/unit-comparison.cpp - src/unit-concepts.cpp - src/unit-constructor1.cpp - src/unit-constructor2.cpp - src/unit-convenience.cpp - src/unit-conversions.cpp - src/unit-deserialization.cpp - src/unit-diagnostics.cpp - src/unit-element_access1.cpp - src/unit-element_access2.cpp - src/unit-hash.cpp - src/unit-inspection.cpp - src/unit-items.cpp - src/unit-iterators1.cpp - src/unit-iterators2.cpp - src/unit-json_patch.cpp - src/unit-json_pointer.cpp - src/unit-large_json.cpp - src/unit-merge_patch.cpp - src/unit-meta.cpp - src/unit-modifiers.cpp - src/unit-msgpack.cpp - src/unit-noexcept.cpp - src/unit-ordered_json.cpp - src/unit-ordered_map.cpp - src/unit-pointer_access.cpp - src/unit-readme.cpp - src/unit-reference_access.cpp - src/unit-regression1.cpp - src/unit-regression2.cpp - src/unit-serialization.cpp - src/unit-testsuites.cpp - src/unit-to_chars.cpp - src/unit-ubjson.cpp - src/unit-udt.cpp - src/unit-udt_macro.cpp - src/unit-unicode.cpp - src/unit-user_defined_input.cpp - src/unit-wstring.cpp) +file(GLOB files src/unit-*.cpp) foreach(file ${files}) get_filename_component(file_basename ${file} NAME_WE) @@ -173,7 +123,7 @@ foreach(file ${files}) COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${testcase} ${DOCTEST_TEST_FILTER} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} ) - set_tests_properties("${testcase}_valgrind" PROPERTIES LABELS "valgrind") + set_tests_properties("${testcase}_valgrind" PROPERTIES LABELS "valgrind" FIXTURES_REQUIRED TEST_DATA) endif() endforeach() diff --git a/test/src/UBSAN.supp b/test/src/UBSAN.supp deleted file mode 100644 index b19f043699..0000000000 --- a/test/src/UBSAN.supp +++ /dev/null @@ -1 +0,0 @@ -unsigned-integer-overflow:stl_bvector.h diff --git a/test/src/unit-algorithms.cpp b/test/src/unit-algorithms.cpp index c08e858a62..3f8340068b 100644 --- a/test/src/unit-algorithms.cpp +++ b/test/src/unit-algorithms.cpp @@ -43,7 +43,7 @@ TEST_CASE("algorithms") { CHECK(std::all_of(j_array.begin(), j_array.end(), [](const json & value) { - return value.size() > 0; + return !value.empty(); })); CHECK(std::all_of(j_object.begin(), j_object.end(), [](const json & value) { @@ -67,7 +67,7 @@ TEST_CASE("algorithms") { CHECK(std::none_of(j_array.begin(), j_array.end(), [](const json & value) { - return value.size() == 0; + return value.empty(); })); CHECK(std::none_of(j_object.begin(), j_object.end(), [](const json & value) { diff --git a/test/src/unit-allocator.cpp b/test/src/unit-allocator.cpp index ad78b8f9ea..962828d201 100644 --- a/test/src/unit-allocator.cpp +++ b/test/src/unit-allocator.cpp @@ -40,12 +40,12 @@ template struct bad_allocator : std::allocator { template - void construct(T*, Args&& ...) + void construct(T* /*unused*/, Args&& ... /*unused*/) { throw std::bad_alloc(); } }; -} +} // namespace TEST_CASE("bad_alloc") { @@ -85,10 +85,8 @@ struct my_allocator : std::allocator next_construct_fails = false; throw std::bad_alloc(); } - else - { - ::new (reinterpret_cast(p)) T(std::forward(args)...); - } + + ::new (reinterpret_cast(p)) T(std::forward(args)...); } void deallocate(T* p, std::size_t n) @@ -98,10 +96,8 @@ struct my_allocator : std::allocator next_deallocate_fails = false; throw std::bad_alloc(); } - else - { - std::allocator::deallocate(p, n); - } + + std::allocator::deallocate(p, n); } void destroy(T* p) @@ -111,10 +107,8 @@ struct my_allocator : std::allocator next_destroy_fails = false; throw std::bad_alloc(); } - else - { - p->~T(); - } + + p->~T(); } template @@ -133,7 +127,7 @@ void my_allocator_clean_up(T* p) alloc.destroy(p); alloc.deallocate(p, 1); } -} +} // namespace TEST_CASE("controlled bad_alloc") { @@ -239,9 +233,9 @@ namespace template struct allocator_no_forward : std::allocator { - allocator_no_forward() {} + allocator_no_forward() = default; template - allocator_no_forward(allocator_no_forward) {} + allocator_no_forward(allocator_no_forward /*unused*/) {} template struct rebind @@ -256,7 +250,7 @@ struct allocator_no_forward : std::allocator ::new (static_cast(p)) T(args...); } }; -} +} // namespace TEST_CASE("bad my_allocator::construct") { diff --git a/test/src/unit-alt-string.cpp b/test/src/unit-alt-string.cpp index b51a5a91d3..b526ae20f0 100644 --- a/test/src/unit-alt-string.cpp +++ b/test/src/unit-alt-string.cpp @@ -152,7 +152,7 @@ class alt_string private: std::string str_impl {}; - friend bool ::operator<(const char*, const alt_string&); + friend bool ::operator<(const char* /*op1*/, const alt_string& /*op2*/); }; void int_to_string(alt_string& target, std::size_t value) @@ -233,24 +233,24 @@ TEST_CASE("alternative string type") SECTION("parse") { - auto doc = alt_json::parse("{\"foo\": \"bar\"}"); + auto doc = alt_json::parse(R"({"foo": "bar"})"); alt_string dump = doc.dump(); CHECK(dump == R"({"foo":"bar"})"); } SECTION("items") { - auto doc = alt_json::parse("{\"foo\": \"bar\"}"); + auto doc = alt_json::parse(R"({"foo": "bar"})"); - for ( auto item : doc.items() ) + for (const auto& item : doc.items()) { - CHECK( item.key() == "foo" ); - CHECK( item.value() == "bar" ); + CHECK(item.key() == "foo"); + CHECK(item.value() == "bar"); } - auto doc_array = alt_json::parse("[\"foo\", \"bar\"]"); + auto doc_array = alt_json::parse(R"(["foo", "bar"])"); - for ( auto item : doc_array.items() ) + for (const auto& item : doc_array.items()) { if (item.key() == "0" ) { @@ -258,11 +258,11 @@ TEST_CASE("alternative string type") } else if (item.key() == "1" ) { - CHECK( item.value() == "bar" ); + CHECK(item.value() == "bar"); } else { - CHECK( false ); + CHECK(false); } } } diff --git a/test/src/unit-bson.cpp b/test/src/unit-bson.cpp index ef3c8d4088..23a2a938e6 100644 --- a/test/src/unit-bson.cpp +++ b/test/src/unit-bson.cpp @@ -687,42 +687,42 @@ class SaxCountdown return events_left-- > 0; } - bool boolean(bool) + bool boolean(bool /*unused*/) { return events_left-- > 0; } - bool number_integer(json::number_integer_t) + bool number_integer(json::number_integer_t /*unused*/) { return events_left-- > 0; } - bool number_unsigned(json::number_unsigned_t) + bool number_unsigned(json::number_unsigned_t /*unused*/) { return events_left-- > 0; } - bool number_float(json::number_float_t, const std::string&) + bool number_float(json::number_float_t /*unused*/, const std::string& /*unused*/) { return events_left-- > 0; } - bool string(std::string&) + bool string(std::string& /*unused*/) { return events_left-- > 0; } - bool binary(std::vector&) + bool binary(std::vector& /*unused*/) { return events_left-- > 0; } - bool start_object(std::size_t) + bool start_object(std::size_t /*unused*/) { return events_left-- > 0; } - bool key(std::string&) + bool key(std::string& /*unused*/) { return events_left-- > 0; } @@ -732,7 +732,7 @@ class SaxCountdown return events_left-- > 0; } - bool start_array(std::size_t) + bool start_array(std::size_t /*unused*/) { return events_left-- > 0; } @@ -742,7 +742,7 @@ class SaxCountdown return events_left-- > 0; } - bool parse_error(std::size_t, const std::string&, const json::exception&) + bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) // NOLINT(readability-convert-member-functions-to-static) { return false; } @@ -750,7 +750,7 @@ class SaxCountdown private: int events_left = 0; }; -} +} // namespace TEST_CASE("Incomplete BSON Input") { diff --git a/test/src/unit-capacity.cpp b/test/src/unit-capacity.cpp index 9be901bf90..eeee85f1e5 100644 --- a/test/src/unit-capacity.cpp +++ b/test/src/unit-capacity.cpp @@ -437,7 +437,7 @@ TEST_CASE("capacity") SECTION("boolean") { json j = true; - const json j_const(j); + const json j_const = true; SECTION("result of max_size") { @@ -449,7 +449,7 @@ TEST_CASE("capacity") SECTION("string") { json j = "hello world"; - const json j_const(j); + const json j_const = "hello world"; SECTION("result of max_size") { @@ -463,7 +463,7 @@ TEST_CASE("capacity") SECTION("empty array") { json j = json::array(); - const json j_const(j); + const json j_const = json::array(); SECTION("result of max_size") { @@ -475,7 +475,7 @@ TEST_CASE("capacity") SECTION("filled array") { json j = {1, 2, 3}; - const json j_const(j); + const json j_const = {1, 2, 3}; SECTION("result of max_size") { @@ -490,7 +490,7 @@ TEST_CASE("capacity") SECTION("empty object") { json j = json::object(); - const json j_const(j); + const json j_const = json::object(); SECTION("result of max_size") { @@ -502,7 +502,7 @@ TEST_CASE("capacity") SECTION("filled object") { json j = {{"one", 1}, {"two", 2}, {"three", 3}}; - const json j_const(j); + const json j_const = {{"one", 1}, {"two", 2}, {"three", 3}}; SECTION("result of max_size") { @@ -515,7 +515,7 @@ TEST_CASE("capacity") SECTION("number (integer)") { json j = -23; - const json j_const(j); + const json j_const = -23; SECTION("result of max_size") { @@ -527,7 +527,7 @@ TEST_CASE("capacity") SECTION("number (unsigned)") { json j = 23u; - const json j_const(j); + const json j_const = 23u; SECTION("result of max_size") { @@ -539,7 +539,7 @@ TEST_CASE("capacity") SECTION("number (float)") { json j = 23.42; - const json j_const(j); + const json j_const = 23.42; SECTION("result of max_size") { @@ -551,7 +551,7 @@ TEST_CASE("capacity") SECTION("null") { json j = nullptr; - const json j_const(j); + const json j_const = nullptr; SECTION("result of max_size") { diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index 9ed80c8f1a..f0baec98e8 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -54,42 +54,42 @@ class SaxCountdown return events_left-- > 0; } - bool boolean(bool) + bool boolean(bool /*unused*/) { return events_left-- > 0; } - bool number_integer(json::number_integer_t) + bool number_integer(json::number_integer_t /*unused*/) { return events_left-- > 0; } - bool number_unsigned(json::number_unsigned_t) + bool number_unsigned(json::number_unsigned_t /*unused*/) { return events_left-- > 0; } - bool number_float(json::number_float_t, const std::string&) + bool number_float(json::number_float_t /*unused*/, const std::string& /*unused*/) { return events_left-- > 0; } - bool string(std::string&) + bool string(std::string& /*unused*/) { return events_left-- > 0; } - bool binary(std::vector&) + bool binary(std::vector& /*unused*/) { return events_left-- > 0; } - bool start_object(std::size_t) + bool start_object(std::size_t /*unused*/) { return events_left-- > 0; } - bool key(std::string&) + bool key(std::string& /*unused*/) { return events_left-- > 0; } @@ -99,7 +99,7 @@ class SaxCountdown return events_left-- > 0; } - bool start_array(std::size_t) + bool start_array(std::size_t /*unused*/) { return events_left-- > 0; } @@ -109,7 +109,7 @@ class SaxCountdown return events_left-- > 0; } - bool parse_error(std::size_t, const std::string&, const json::exception&) + bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) // NOLINT(readability-convert-member-functions-to-static) { return false; } @@ -117,7 +117,7 @@ class SaxCountdown private: int events_left = 0; }; -} +} // namespace TEST_CASE("CBOR") { @@ -219,7 +219,7 @@ TEST_CASE("CBOR") // create expected byte vector std::vector expected; expected.push_back(static_cast(0x3b)); - uint64_t positive = static_cast(-1 - i); + auto positive = static_cast(-1 - i); expected.push_back(static_cast((positive >> 56) & 0xff)); expected.push_back(static_cast((positive >> 48) & 0xff)); expected.push_back(static_cast((positive >> 40) & 0xff)); @@ -276,7 +276,7 @@ TEST_CASE("CBOR") // create expected byte vector std::vector expected; expected.push_back(static_cast(0x3a)); - uint32_t positive = static_cast(static_cast(-1 - i) & 0x00000000ffffffff); + auto positive = static_cast(static_cast(-1 - i) & 0x00000000ffffffff); expected.push_back(static_cast((positive >> 24) & 0xff)); expected.push_back(static_cast((positive >> 16) & 0xff)); expected.push_back(static_cast((positive >> 8) & 0xff)); @@ -294,7 +294,7 @@ TEST_CASE("CBOR") (static_cast(result[3]) << 010) + static_cast(result[4]); CHECK(restored == positive); - CHECK(-1ll - restored == i); + CHECK(-1LL - restored == i); // roundtrip CHECK(json::from_cbor(result) == j); @@ -317,7 +317,7 @@ TEST_CASE("CBOR") // create expected byte vector std::vector expected; expected.push_back(static_cast(0x39)); - uint16_t positive = static_cast(-1 - i); + auto positive = static_cast(-1 - i); expected.push_back(static_cast((positive >> 8) & 0xff)); expected.push_back(static_cast(positive & 0xff)); @@ -328,7 +328,7 @@ TEST_CASE("CBOR") // check individual bytes CHECK(result[0] == 0x39); - uint16_t restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); + auto restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); CHECK(restored == positive); CHECK(-1 - restored == i); @@ -346,7 +346,7 @@ TEST_CASE("CBOR") const auto result = json::to_cbor(j); CHECK(result == expected); - int16_t restored = static_cast(-1 - ((result[1] << 8) + result[2])); + auto restored = static_cast(-1 - ((result[1] << 8) + result[2])); CHECK(restored == -9263); // roundtrip @@ -506,7 +506,7 @@ TEST_CASE("CBOR") // check individual bytes CHECK(result[0] == 0x19); - uint16_t restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); + auto restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); CHECK(restored == i); // roundtrip @@ -611,7 +611,7 @@ TEST_CASE("CBOR") SECTION("-32768..-129 (int 16)") { - for (int16_t i = -32768; i <= -129; ++i) + for (int16_t i = -32768; i <= int16_t(-129); ++i) { CAPTURE(i) @@ -634,7 +634,7 @@ TEST_CASE("CBOR") // check individual bytes CHECK(result[0] == 0xd1); - int16_t restored = static_cast((result[1] << 8) + result[2]); + auto restored = static_cast((result[1] << 8) + result[2]); CHECK(restored == i); // roundtrip @@ -699,7 +699,7 @@ TEST_CASE("CBOR") // check individual bytes CHECK(result[0] == 0x18); - uint8_t restored = static_cast(result[1]); + auto restored = static_cast(result[1]); CHECK(restored == i); // roundtrip @@ -733,7 +733,7 @@ TEST_CASE("CBOR") // check individual bytes CHECK(result[0] == 0x19); - uint16_t restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); + auto restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); CHECK(restored == i); // roundtrip @@ -940,7 +940,7 @@ TEST_CASE("CBOR") } SECTION("-3.40282e+38(lowest float)") { - double v = static_cast(std::numeric_limits::lowest()); + auto v = static_cast(std::numeric_limits::lowest()); json j = v; std::vector expected = { @@ -1340,7 +1340,7 @@ TEST_CASE("CBOR") SECTION("{\"a\": {\"b\": {\"c\": {}}}}") { - json j = json::parse("{\"a\": {\"b\": {\"c\": {}}}}"); + json j = json::parse(R"({"a": {"b": {"c": {}}}})"); std::vector expected = { 0xa1, 0x61, 0x61, 0xa1, 0x61, 0x62, 0xa1, 0x61, 0x63, 0xa0 @@ -2029,20 +2029,18 @@ TEST_CASE("CBOR roundtrips" * doctest::skip()) SECTION("input from flynn") { // most of these are excluded due to differences in key order (not a real problem) - auto exclude_packed = std::set - { - TEST_DATA_DIRECTORY "/json.org/1.json", - TEST_DATA_DIRECTORY "/json.org/2.json", - TEST_DATA_DIRECTORY "/json.org/3.json", - TEST_DATA_DIRECTORY "/json.org/4.json", - TEST_DATA_DIRECTORY "/json.org/5.json", - TEST_DATA_DIRECTORY "/json_testsuite/sample.json", // kills AppVeyor - TEST_DATA_DIRECTORY "/json_tests/pass1.json", - TEST_DATA_DIRECTORY "/regression/working_file.json", - TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object.json", - TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_duplicated_key.json", - TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_long_strings.json", - }; + std::set exclude_packed; + exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/1.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/2.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/3.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/4.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/5.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/json_testsuite/sample.json"); // kills AppVeyor + exclude_packed.insert(TEST_DATA_DIRECTORY "/json_tests/pass1.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/regression/working_file.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_duplicated_key.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_long_strings.json"); for (std::string filename : { @@ -2249,7 +2247,7 @@ TEST_CASE("CBOR roundtrips" * doctest::skip()) // parse CBOR file auto packed = utils::read_binary_file(filename + ".cbor"); - if (!exclude_packed.count(filename)) + if (exclude_packed.count(filename) == 0u) { { INFO_WITH_TEMP(filename + ": output adapters: std::vector"); @@ -2323,7 +2321,7 @@ TEST_CASE("all CBOR first bytes") // check that parse_error.112 is only thrown if the // first byte is in the unsupported set INFO_WITH_TEMP(e.what()); - if (std::find(unsupported.begin(), unsupported.end(), byte) != unsupported.end()) + if (unsupported.find(byte) != unsupported.end()) { CHECK(e.id == 112); } diff --git a/test/src/unit-class_lexer.cpp b/test/src/unit-class_lexer.cpp index 07d243a818..d94bdbe79f 100644 --- a/test/src/unit-class_lexer.cpp +++ b/test/src/unit-class_lexer.cpp @@ -36,19 +36,19 @@ using nlohmann::json; namespace { // shortcut to scan a string literal -json::lexer::token_type scan_string(const char* s, const bool ignore_comments = false); +json::lexer::token_type scan_string(const char* s, bool ignore_comments = false); json::lexer::token_type scan_string(const char* s, const bool ignore_comments) { auto ia = nlohmann::detail::input_adapter(s); - return nlohmann::detail::lexer(std::move(ia), ignore_comments).scan(); -} + return nlohmann::detail::lexer(std::move(ia), ignore_comments).scan(); // NOLINT(hicpp-move-const-arg,performance-move-const-arg) } +} // namespace -std::string get_error_message(const char* s, const bool ignore_comments = false); +std::string get_error_message(const char* s, bool ignore_comments = false); std::string get_error_message(const char* s, const bool ignore_comments) { auto ia = nlohmann::detail::input_adapter(s); - auto lexer = nlohmann::detail::lexer(std::move(ia), ignore_comments); + auto lexer = nlohmann::detail::lexer(std::move(ia), ignore_comments); // NOLINT(hicpp-move-const-arg,performance-move-const-arg) lexer.scan(); return lexer.get_error_message(); } diff --git a/test/src/unit-class_parser.cpp b/test/src/unit-class_parser.cpp index 2df07d6d44..247d9d5fe5 100644 --- a/test/src/unit-class_parser.cpp +++ b/test/src/unit-class_parser.cpp @@ -42,13 +42,13 @@ class SaxEventLogger public: bool null() { - events.push_back("null()"); + events.emplace_back("null()"); return true; } bool boolean(bool val) { - events.push_back(val ? "boolean(true)" : "boolean(false)"); + events.emplace_back(val ? "boolean(true)" : "boolean(false)"); return true; } @@ -64,7 +64,7 @@ class SaxEventLogger return true; } - bool number_float(json::number_float_t, const std::string& s) + bool number_float(json::number_float_t /*unused*/, const std::string& s) { events.push_back("number_float(" + s + ")"); return true; @@ -79,7 +79,7 @@ class SaxEventLogger bool binary(json::binary_t& val) { std::string binary_contents = "binary("; - std::string comma_space = ""; + std::string comma_space; for (auto b : val) { binary_contents.append(comma_space); @@ -95,7 +95,7 @@ class SaxEventLogger { if (elements == std::size_t(-1)) { - events.push_back("start_object()"); + events.emplace_back("start_object()"); } else { @@ -112,7 +112,7 @@ class SaxEventLogger bool end_object() { - events.push_back("end_object()"); + events.emplace_back("end_object()"); return true; } @@ -120,7 +120,7 @@ class SaxEventLogger { if (elements == std::size_t(-1)) { - events.push_back("start_array()"); + events.emplace_back("start_array()"); } else { @@ -131,11 +131,11 @@ class SaxEventLogger bool end_array() { - events.push_back("end_array()"); + events.emplace_back("end_array()"); return true; } - bool parse_error(std::size_t position, const std::string&, const json::exception&) + bool parse_error(std::size_t position, const std::string& /*unused*/, const json::exception& /*unused*/) { errored = true; events.push_back("parse_error(" + std::to_string(position) + ")"); @@ -157,42 +157,42 @@ class SaxCountdown : public nlohmann::json::json_sax_t return events_left-- > 0; } - bool boolean(bool) override + bool boolean(bool /*val*/) override { return events_left-- > 0; } - bool number_integer(json::number_integer_t) override + bool number_integer(json::number_integer_t /*val*/) override { return events_left-- > 0; } - bool number_unsigned(json::number_unsigned_t) override + bool number_unsigned(json::number_unsigned_t /*val*/) override { return events_left-- > 0; } - bool number_float(json::number_float_t, const std::string&) override + bool number_float(json::number_float_t /*val*/, const std::string& /*s*/) override { return events_left-- > 0; } - bool string(std::string&) override + bool string(std::string& /*val*/) override { return events_left-- > 0; } - bool binary(json::binary_t&) override + bool binary(json::binary_t& /*val*/) override { return events_left-- > 0; } - bool start_object(std::size_t) override + bool start_object(std::size_t /*elements*/) override { return events_left-- > 0; } - bool key(std::string&) override + bool key(std::string& /*val*/) override { return events_left-- > 0; } @@ -202,7 +202,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t return events_left-- > 0; } - bool start_array(std::size_t) override + bool start_array(std::size_t /*elements*/) override { return events_left-- > 0; } @@ -212,7 +212,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t return events_left-- > 0; } - bool parse_error(std::size_t, const std::string&, const json::exception&) override + bool parse_error(std::size_t /*position*/, const std::string& /*last_token*/, const json::exception& /*ex*/) override { return false; } @@ -267,7 +267,7 @@ bool accept_helper(const std::string& s) CHECK(json::parser(nlohmann::detail::input_adapter(s)).accept(false) == !el.errored); // 5. parse with simple callback - json::parser_callback_t cb = [](int, json::parse_event_t, json&) + json::parser_callback_t cb = [](int /*unused*/, json::parse_event_t /*unused*/, json& /*unused*/) { return true; }; @@ -395,7 +395,7 @@ TEST_CASE("parser class") CHECK_THROWS_AS(parser_helper("\uFF01"), json::parse_error&); CHECK_THROWS_AS(parser_helper("[-4:1,]"), json::parse_error&); // unescaped control characters - CHECK_THROWS_AS(parser_helper("\"\x00\""), json::parse_error&); + CHECK_THROWS_AS(parser_helper("\"\x00\""), json::parse_error&); // NOLINT(bugprone-string-literal-with-embedded-nul) CHECK_THROWS_AS(parser_helper("\"\x01\""), json::parse_error&); CHECK_THROWS_AS(parser_helper("\"\x02\""), json::parse_error&); CHECK_THROWS_AS(parser_helper("\"\x03\""), json::parse_error&); @@ -427,7 +427,7 @@ TEST_CASE("parser class") CHECK_THROWS_AS(parser_helper("\"\x1d\""), json::parse_error&); CHECK_THROWS_AS(parser_helper("\"\x1e\""), json::parse_error&); CHECK_THROWS_AS(parser_helper("\"\x1f\""), json::parse_error&); - CHECK_THROWS_WITH(parser_helper("\"\x00\""), "[json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing value - invalid string: missing closing quote; last read: '\"'"); + CHECK_THROWS_WITH(parser_helper("\"\x00\""), "[json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing value - invalid string: missing closing quote; last read: '\"'"); // NOLINT(bugprone-string-literal-with-embedded-nul) CHECK_THROWS_WITH(parser_helper("\"\x01\""), "[json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing value - invalid string: control character U+0001 (SOH) must be escaped to \\u0001; last read: '\"'"); CHECK_THROWS_WITH(parser_helper("\"\x02\""), "[json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing value - invalid string: control character U+0002 (STX) must be escaped to \\u0002; last read: '\"'"); CHECK_THROWS_WITH(parser_helper("\"\x03\""), "[json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing value - invalid string: control character U+0003 (ETX) must be escaped to \\u0003; last read: '\"'"); @@ -641,8 +641,8 @@ TEST_CASE("parser class") SECTION("overflow") { // overflows during parsing yield an exception - CHECK_THROWS_AS(parser_helper("1.18973e+4932") == json(), json::out_of_range&); - CHECK_THROWS_WITH(parser_helper("1.18973e+4932") == json(), + CHECK_THROWS_AS(parser_helper("1.18973e+4932").empty(), json::out_of_range&); + CHECK_THROWS_WITH(parser_helper("1.18973e+4932").empty(), "[json.exception.out_of_range.406] number overflow parsing '1.18973e+4932'"); } @@ -770,7 +770,7 @@ TEST_CASE("parser class") CHECK(accept_helper("\uFF01") == false); CHECK(accept_helper("[-4:1,]") == false); // unescaped control characters - CHECK(accept_helper("\"\x00\"") == false); + CHECK(accept_helper("\"\x00\"") == false); // NOLINT(bugprone-string-literal-with-embedded-nul) CHECK(accept_helper("\"\x01\"") == false); CHECK(accept_helper("\"\x02\"") == false); CHECK(accept_helper("\"\x03\"") == false); @@ -1155,7 +1155,7 @@ TEST_CASE("parser class") case ('r'): case ('t'): { - CHECK_NOTHROW(parser_helper(s.c_str())); + CHECK_NOTHROW(parser_helper(s)); break; } @@ -1168,11 +1168,11 @@ TEST_CASE("parser class") // any other combination of backslash and character is invalid default: { - CHECK_THROWS_AS(parser_helper(s.c_str()), json::parse_error&); + CHECK_THROWS_AS(parser_helper(s), json::parse_error&); // only check error message if c is not a control character if (c > 0x1f) { - CHECK_THROWS_WITH_STD_STR(parser_helper(s.c_str()), + CHECK_THROWS_WITH_STD_STR(parser_helper(s), "[json.exception.parse_error.101] parse error at line 1, column 3: syntax error while parsing value - invalid string: forbidden character after backslash; last read: '\"\\" + std::string(1, static_cast(c)) + "'"); } break; @@ -1233,49 +1233,49 @@ TEST_CASE("parser class") if (valid(c)) { CAPTURE(s1) - CHECK_NOTHROW(parser_helper(s1.c_str())); + CHECK_NOTHROW(parser_helper(s1)); CAPTURE(s2) - CHECK_NOTHROW(parser_helper(s2.c_str())); + CHECK_NOTHROW(parser_helper(s2)); CAPTURE(s3) - CHECK_NOTHROW(parser_helper(s3.c_str())); + CHECK_NOTHROW(parser_helper(s3)); CAPTURE(s4) - CHECK_NOTHROW(parser_helper(s4.c_str())); + CHECK_NOTHROW(parser_helper(s4)); } else { CAPTURE(s1) - CHECK_THROWS_AS(parser_helper(s1.c_str()), json::parse_error&); + CHECK_THROWS_AS(parser_helper(s1), json::parse_error&); // only check error message if c is not a control character if (c > 0x1f) { - CHECK_THROWS_WITH_STD_STR(parser_helper(s1.c_str()), + CHECK_THROWS_WITH_STD_STR(parser_helper(s1), "[json.exception.parse_error.101] parse error at line 1, column 7: syntax error while parsing value - invalid string: '\\u' must be followed by 4 hex digits; last read: '" + s1.substr(0, 7) + "'"); } CAPTURE(s2) - CHECK_THROWS_AS(parser_helper(s2.c_str()), json::parse_error&); + CHECK_THROWS_AS(parser_helper(s2), json::parse_error&); // only check error message if c is not a control character if (c > 0x1f) { - CHECK_THROWS_WITH_STD_STR(parser_helper(s2.c_str()), + CHECK_THROWS_WITH_STD_STR(parser_helper(s2), "[json.exception.parse_error.101] parse error at line 1, column 6: syntax error while parsing value - invalid string: '\\u' must be followed by 4 hex digits; last read: '" + s2.substr(0, 6) + "'"); } CAPTURE(s3) - CHECK_THROWS_AS(parser_helper(s3.c_str()), json::parse_error&); + CHECK_THROWS_AS(parser_helper(s3), json::parse_error&); // only check error message if c is not a control character if (c > 0x1f) { - CHECK_THROWS_WITH_STD_STR(parser_helper(s3.c_str()), + CHECK_THROWS_WITH_STD_STR(parser_helper(s3), "[json.exception.parse_error.101] parse error at line 1, column 5: syntax error while parsing value - invalid string: '\\u' must be followed by 4 hex digits; last read: '" + s3.substr(0, 5) + "'"); } CAPTURE(s4) - CHECK_THROWS_AS(parser_helper(s4.c_str()), json::parse_error&); + CHECK_THROWS_AS(parser_helper(s4), json::parse_error&); // only check error message if c is not a control character if (c > 0x1f) { - CHECK_THROWS_WITH_STD_STR(parser_helper(s4.c_str()), + CHECK_THROWS_WITH_STD_STR(parser_helper(s4), "[json.exception.parse_error.101] parse error at line 1, column 4: syntax error while parsing value - invalid string: '\\u' must be followed by 4 hex digits; last read: '" + s4.substr(0, 4) + "'"); } } @@ -1381,7 +1381,7 @@ TEST_CASE("parser class") case ('r'): case ('t'): { - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s.c_str()))).accept()); + CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s))).accept()); break; } @@ -1394,7 +1394,7 @@ TEST_CASE("parser class") // any other combination of backslash and character is invalid default: { - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s.c_str()))).accept() == false); + CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s))).accept() == false); break; } } @@ -1453,27 +1453,27 @@ TEST_CASE("parser class") if (valid(c)) { CAPTURE(s1) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s1.c_str()))).accept()); + CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s1))).accept()); CAPTURE(s2) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s2.c_str()))).accept()); + CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s2))).accept()); CAPTURE(s3) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s3.c_str()))).accept()); + CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s3))).accept()); CAPTURE(s4) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s4.c_str()))).accept()); + CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s4))).accept()); } else { CAPTURE(s1) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s1.c_str()))).accept() == false); + CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s1))).accept() == false); CAPTURE(s2) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s2.c_str()))).accept() == false); + CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s2))).accept() == false); CAPTURE(s3) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s3.c_str()))).accept() == false); + CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s3))).accept() == false); CAPTURE(s4) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s4.c_str()))).accept() == false); + CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s4))).accept() == false); } } } @@ -1499,16 +1499,9 @@ TEST_CASE("parser class") // test case to make sure the callback is properly evaluated after reading a key { - json::parser_callback_t cb = [](int, json::parse_event_t event, json&) + json::parser_callback_t cb = [](int /*unused*/, json::parse_event_t event, json& /*unused*/) { - if (event == json::parse_event_t::key) - { - return false; - } - else - { - return true; - } + return event != json::parse_event_t::key; }; json x = json::parse("{\"key\": false}", cb); @@ -1518,7 +1511,7 @@ TEST_CASE("parser class") SECTION("callback function") { - auto s_object = R"( + const auto* s_object = R"( { "foo": 2, "bar": { @@ -1527,11 +1520,11 @@ TEST_CASE("parser class") } )"; - auto s_array = R"( + const auto* s_array = R"( [1,2,[3,4,5],4,5] )"; - auto structured_array = R"( + const auto* structured_array = R"( [ 1, { @@ -1545,14 +1538,14 @@ TEST_CASE("parser class") SECTION("filter nothing") { - json j_object = json::parse(s_object, [](int, json::parse_event_t, const json&) + json j_object = json::parse(s_object, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) { return true; }); CHECK (j_object == json({{"foo", 2}, {"bar", {{"baz", 1}}}})); - json j_array = json::parse(s_array, [](int, json::parse_event_t, const json&) + json j_array = json::parse(s_array, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) { return true; }); @@ -1562,7 +1555,7 @@ TEST_CASE("parser class") SECTION("filter everything") { - json j_object = json::parse(s_object, [](int, json::parse_event_t, const json&) + json j_object = json::parse(s_object, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) { return false; }); @@ -1570,7 +1563,7 @@ TEST_CASE("parser class") // the top-level object will be discarded, leaving a null CHECK (j_object.is_null()); - json j_array = json::parse(s_array, [](int, json::parse_event_t, const json&) + json j_array = json::parse(s_array, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) { return false; }); @@ -1581,31 +1574,17 @@ TEST_CASE("parser class") SECTION("filter specific element") { - json j_object = json::parse(s_object, [](int, json::parse_event_t, const json & j) + json j_object = json::parse(s_object, [](int /*unused*/, json::parse_event_t /*unused*/, const json & j) { // filter all number(2) elements - if (j == json(2)) - { - return false; - } - else - { - return true; - } + return j != json(2); }); CHECK (j_object == json({{"bar", {{"baz", 1}}}})); - json j_array = json::parse(s_array, [](int, json::parse_event_t, const json & j) + json j_array = json::parse(s_array, [](int /*unused*/, json::parse_event_t /*unused*/, const json & j) { - if (j == json(2)) - { - return false; - } - else - { - return true; - } + return j != json(2); }); CHECK (j_array == json({1, {3, 4, 5}, 4, 5})); @@ -1613,32 +1592,18 @@ TEST_CASE("parser class") SECTION("filter object in array") { - json j_filtered1 = json::parse(structured_array, [](int, json::parse_event_t e, const json & parsed) + json j_filtered1 = json::parse(structured_array, [](int /*unused*/, json::parse_event_t e, const json & parsed) { - if (e == json::parse_event_t::object_end && parsed.contains("foo")) - { - return false; - } - else - { - return true; - } + return !(e == json::parse_event_t::object_end && parsed.contains("foo")); }); // the specified object will be discarded, and removed. CHECK (j_filtered1.size() == 2); CHECK (j_filtered1 == json({1, {{"qux", "baz"}}})); - json j_filtered2 = json::parse(structured_array, [](int, json::parse_event_t e, const json& /*parsed*/) + json j_filtered2 = json::parse(structured_array, [](int /*unused*/, json::parse_event_t e, const json& /*parsed*/) { - if (e == json::parse_event_t::object_end) - { - return false; - } - else - { - return true; - } + return e != json::parse_event_t::object_end; }); // removed all objects in array. @@ -1651,7 +1616,7 @@ TEST_CASE("parser class") SECTION("first closing event") { { - json j_object = json::parse(s_object, [](int, json::parse_event_t e, const json&) + json j_object = json::parse(s_object, [](int /*unused*/, json::parse_event_t e, const json& /*unused*/) { static bool first = true; if (e == json::parse_event_t::object_end && first) @@ -1659,10 +1624,8 @@ TEST_CASE("parser class") first = false; return false; } - else - { - return true; - } + + return true; }); // the first completed object will be discarded @@ -1670,7 +1633,7 @@ TEST_CASE("parser class") } { - json j_array = json::parse(s_array, [](int, json::parse_event_t e, const json&) + json j_array = json::parse(s_array, [](int /*unused*/, json::parse_event_t e, const json& /*unused*/) { static bool first = true; if (e == json::parse_event_t::array_end && first) @@ -1678,10 +1641,8 @@ TEST_CASE("parser class") first = false; return false; } - else - { - return true; - } + + return true; }); // the first completed array will be discarded @@ -1696,29 +1657,15 @@ TEST_CASE("parser class") // object and array is discarded only after the closing character // has been read - json j_empty_object = json::parse("{}", [](int, json::parse_event_t e, const json&) + json j_empty_object = json::parse("{}", [](int /*unused*/, json::parse_event_t e, const json& /*unused*/) { - if (e == json::parse_event_t::object_end) - { - return false; - } - else - { - return true; - } + return e != json::parse_event_t::object_end; }); CHECK(j_empty_object == json()); - json j_empty_array = json::parse("[]", [](int, json::parse_event_t e, const json&) + json j_empty_array = json::parse("[]", [](int /*unused*/, json::parse_event_t e, const json& /*unused*/) { - if (e == json::parse_event_t::array_end) - { - return false; - } - else - { - return true; - } + return e != json::parse_event_t::array_end; }); CHECK(j_empty_array == json()); } @@ -1744,7 +1691,7 @@ TEST_CASE("parser class") SECTION("from array") { - uint8_t v[] = {'t', 'r', 'u', 'e'}; + uint8_t v[] = {'t', 'r', 'u', 'e'}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) json j; json::parser(nlohmann::detail::input_adapter(std::begin(v), std::end(v))).parse(true, j); CHECK(j == json(true)); @@ -1784,7 +1731,7 @@ TEST_CASE("parser class") { SECTION("parser with callback") { - json::parser_callback_t cb = [](int, json::parse_event_t, json&) + json::parser_callback_t cb = [](int /*unused*/, json::parse_event_t /*unused*/, json& /*unused*/) { return true; }; diff --git a/test/src/unit-comparison.cpp b/test/src/unit-comparison.cpp index e375feca49..56b38cf6b0 100644 --- a/test/src/unit-comparison.cpp +++ b/test/src/unit-comparison.cpp @@ -41,7 +41,7 @@ bool f(A a, B b, U u = U()) { return u(a, b); } -} +} // namespace TEST_CASE("lexicographical comparison operators") { @@ -143,10 +143,10 @@ TEST_CASE("lexicographical comparison operators") // comparison with discarded elements json j_discarded(json::value_t::discarded); - for (size_t i = 0; i < j_values.size(); ++i) + for (const auto& v : j_values) { - CHECK( (j_values[i] == j_discarded) == false); - CHECK( (j_discarded == j_values[i]) == false); + CHECK( (v == j_discarded) == false); + CHECK( (j_discarded == v) == false); CHECK( (j_discarded == j_discarded) == false); } diff --git a/test/src/unit-constructor1.cpp b/test/src/unit-constructor1.cpp index 70b3e40470..884a1e6f1d 100644 --- a/test/src/unit-constructor1.cpp +++ b/test/src/unit-constructor1.cpp @@ -436,7 +436,7 @@ TEST_CASE("constructors") SECTION("char[]") { - char s[] {"Hello world"}; + char s[] {"Hello world"}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) json j(s); CHECK(j.type() == json::value_t::string); CHECK(j == j_reference); @@ -794,7 +794,7 @@ TEST_CASE("constructors") SECTION("integer literal with l suffix") { - json j(42l); + json j(42L); CHECK(j.type() == json::value_t::number_integer); CHECK(j == j_reference); } @@ -808,7 +808,7 @@ TEST_CASE("constructors") SECTION("integer literal with ll suffix") { - json j(42ll); + json j(42LL); CHECK(j.type() == json::value_t::number_integer); CHECK(j == j_reference); } @@ -892,7 +892,7 @@ TEST_CASE("constructors") SECTION("long double") { - long double n = 42.23l; + 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)); @@ -914,7 +914,7 @@ TEST_CASE("constructors") SECTION("integer literal with l suffix") { - json j(42.23l); + json j(42.23L); CHECK(j.type() == json::value_t::number_float); CHECK(j.m_value.number_float == Approx(j_reference.m_value.number_float)); } @@ -1115,84 +1115,113 @@ TEST_CASE("constructors") { SECTION("string") { - // This should break through any short string optimization in std::string - std::string source(1024, '!'); - const char* source_addr = source.data(); - SECTION("constructor with implicit types (array)") { + // This should break through any short string optimization in std::string + std::string source(1024, '!'); + const auto* source_addr = source.data(); json j = {std::move(source)}; - CHECK(j[0].get_ref().data() == source_addr); + const auto* target_addr = j[0].get_ref().data(); + const bool success = (target_addr == source_addr); + CHECK(success); } SECTION("constructor with implicit types (object)") { + // This should break through any short string optimization in std::string + std::string source(1024, '!'); + const auto* source_addr = source.data(); json j = {{"key", std::move(source)}}; - CHECK(j["key"].get_ref().data() == source_addr); + const auto* target_addr = j["key"].get_ref().data(); + const bool success = (target_addr == source_addr); + CHECK(success); } SECTION("constructor with implicit types (object key)") { + // This should break through any short string optimization in std::string + std::string source(1024, '!'); + const auto* source_addr = source.data(); json j = {{std::move(source), 42}}; - CHECK(j.get_ref().begin()->first.data() == source_addr); + const auto* target_addr = j.get_ref().begin()->first.data(); + const bool success = (target_addr == source_addr); + CHECK(success); } } SECTION("array") { - json::array_t source = {1, 2, 3}; - const json* source_addr = source.data(); - SECTION("constructor with implicit types (array)") { + json::array_t source = {1, 2, 3}; + const auto* source_addr = source.data(); json j {std::move(source)}; - CHECK(j[0].get_ref().data() == source_addr); + const auto* target_addr = j[0].get_ref().data(); + const bool success = (target_addr == source_addr); + CHECK(success); } SECTION("constructor with implicit types (object)") { + json::array_t source = {1, 2, 3}; + const auto* source_addr = source.data(); json j {{"key", std::move(source)}}; - CHECK(j["key"].get_ref().data() == source_addr); + const auto* target_addr = j["key"].get_ref().data(); + const bool success = (target_addr == source_addr); + CHECK(success); } SECTION("assignment with implicit types (array)") { + json::array_t source = {1, 2, 3}; + const auto* source_addr = source.data(); json j = {std::move(source)}; - CHECK(j[0].get_ref().data() == source_addr); + const auto* target_addr = j[0].get_ref().data(); + const bool success = (target_addr == source_addr); + CHECK(success); } SECTION("assignment with implicit types (object)") { + json::array_t source = {1, 2, 3}; + const auto* source_addr = source.data(); json j = {{"key", std::move(source)}}; - CHECK(j["key"].get_ref().data() == source_addr); + const auto* target_addr = j["key"].get_ref().data(); + const bool success = (target_addr == source_addr); + CHECK(success); } } SECTION("object") { - json::object_t source = {{"hello", "world"}}; - const json* source_addr = &source.at("hello"); - SECTION("constructor with implicit types (array)") { + json::object_t source = {{"hello", "world"}}; + const json* source_addr = &source.at("hello"); json j {std::move(source)}; CHECK(&(j[0].get_ref().at("hello")) == source_addr); } SECTION("constructor with implicit types (object)") { + json::object_t source = {{"hello", "world"}}; + const json* source_addr = &source.at("hello"); json j {{"key", std::move(source)}}; CHECK(&(j["key"].get_ref().at("hello")) == source_addr); } SECTION("assignment with implicit types (array)") { + json::object_t source = {{"hello", "world"}}; + const json* source_addr = &source.at("hello"); json j = {std::move(source)}; CHECK(&(j[0].get_ref().at("hello")) == source_addr); } SECTION("assignment with implicit types (object)") { + json::object_t source = {{"hello", "world"}}; + const json* source_addr = &source.at("hello"); json j = {{"key", std::move(source)}}; CHECK(&(j["key"].get_ref().at("hello")) == source_addr); } @@ -1200,29 +1229,34 @@ TEST_CASE("constructors") SECTION("json") { - json source {1, 2, 3}; - const json* source_addr = &source[0]; - SECTION("constructor with implicit types (array)") { + json source {1, 2, 3}; + const json* source_addr = &source[0]; json j {std::move(source), {}}; CHECK(&j[0][0] == source_addr); } SECTION("constructor with implicit types (object)") { + json source {1, 2, 3}; + const json* source_addr = &source[0]; json j {{"key", std::move(source)}}; CHECK(&j["key"][0] == source_addr); } SECTION("assignment with implicit types (array)") { + json source {1, 2, 3}; + const json* source_addr = &source[0]; json j = {std::move(source), {}}; CHECK(&j[0][0] == source_addr); } SECTION("assignment with implicit types (object)") { + json source {1, 2, 3}; + const json* source_addr = &source[0]; json j = {{"key", std::move(source)}}; CHECK(&j["key"][0] == source_addr); } diff --git a/test/src/unit-constructor2.cpp b/test/src/unit-constructor2.cpp index 27f4dfdcbc..4ffa96aad6 100644 --- a/test/src/unit-constructor2.cpp +++ b/test/src/unit-constructor2.cpp @@ -39,63 +39,63 @@ TEST_CASE("other constructors and destructor") SECTION("object") { json j {{"foo", 1}, {"bar", false}}; - json k(j); + json k(j); // NOLINT(performance-unnecessary-copy-initialization) CHECK(j == k); } SECTION("array") { json j {"foo", 1, 42.23, false}; - json k(j); + json k(j); // NOLINT(performance-unnecessary-copy-initialization) CHECK(j == k); } SECTION("null") { json j(nullptr); - json k(j); + json k(j); // NOLINT(performance-unnecessary-copy-initialization) CHECK(j == k); } SECTION("boolean") { json j(true); - json k(j); + json k(j); // NOLINT(performance-unnecessary-copy-initialization) CHECK(j == k); } SECTION("string") { json j("Hello world"); - json k(j); + json k(j); // NOLINT(performance-unnecessary-copy-initialization) CHECK(j == k); } SECTION("number (integer)") { json j(42); - json k(j); + json k(j); // NOLINT(performance-unnecessary-copy-initialization) CHECK(j == k); } SECTION("number (unsigned)") { json j(42u); - json k(j); + json k(j); // NOLINT(performance-unnecessary-copy-initialization) CHECK(j == k); } SECTION("number (floating-point)") { json j(42.23); - json k(j); + json k(j); // NOLINT(performance-unnecessary-copy-initialization) CHECK(j == k); } SECTION("binary") { json j = json::binary({1, 2, 3}); - json k(j); + json k(j); // NOLINT(performance-unnecessary-copy-initialization) CHECK(j == k); } } @@ -106,7 +106,7 @@ TEST_CASE("other constructors and destructor") CHECK(j.type() == json::value_t::object); json k(std::move(j)); CHECK(k.type() == json::value_t::object); - CHECK(j.type() == json::value_t::null); + CHECK(j.type() == json::value_t::null); // NOLINT: access after move is OK here } SECTION("copy assignment") @@ -188,20 +188,20 @@ TEST_CASE("other constructors and destructor") { SECTION("object") { - auto j = new json {{"foo", 1}, {"bar", false}}; - delete j; + auto* j = new json {{"foo", 1}, {"bar", false}}; // NOLINT(cppcoreguidelines-owning-memory) + delete j; // NOLINT(cppcoreguidelines-owning-memory) } SECTION("array") { - auto j = new json {"foo", 1, 1u, false, 23.42}; - delete j; + auto* j = new json {"foo", 1, 1u, false, 23.42}; // NOLINT(cppcoreguidelines-owning-memory) + delete j; // NOLINT(cppcoreguidelines-owning-memory) } SECTION("string") { - auto j = new json("Hello world"); - delete j; + auto* j = new json("Hello world"); // NOLINT(cppcoreguidelines-owning-memory) + delete j; // NOLINT(cppcoreguidelines-owning-memory) } } } diff --git a/test/src/unit-convenience.cpp b/test/src/unit-convenience.cpp index c75edac4ea..1d98cd8bec 100644 --- a/test/src/unit-convenience.cpp +++ b/test/src/unit-convenience.cpp @@ -37,7 +37,7 @@ using nlohmann::json; namespace { -void check_escaped(const char* original, const char* escaped = "", const bool ensure_ascii = false); +void check_escaped(const char* original, const char* escaped = "", bool ensure_ascii = false); void check_escaped(const char* original, const char* escaped, const bool ensure_ascii) { std::stringstream ss; @@ -45,7 +45,7 @@ void check_escaped(const char* original, const char* escaped, const bool ensure_ s.dump_escaped(original, ensure_ascii); CHECK(ss.str() == escaped); } -} +} // namespace TEST_CASE("convenience functions") { diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index 7f59c63ecf..4a544ea3f5 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -282,8 +282,8 @@ TEST_CASE("value conversion") SECTION("built-in arrays") { - const char str[] = "a string"; - const int nbs[] = {0, 1, 2}; + const char str[] = "a string"; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) + const int nbs[] = {0, 1, 2}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) json j2 = nbs; json j3 = str; @@ -387,8 +387,8 @@ TEST_CASE("value conversion") SECTION("built-in arrays") { - const int nbs[] = {0, 1, 2}; - int nbs2[] = {0, 0, 0}; + const int nbs[] = {0, 1, 2}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) + int nbs2[] = {0, 0, 0}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) json j2 = nbs; j2.get_to(nbs2); @@ -633,7 +633,7 @@ TEST_CASE("value conversion") SECTION("boolean_t") { - json::boolean_t b = j.get(); + auto b = j.get(); CHECK(json(b) == j); } @@ -726,25 +726,25 @@ TEST_CASE("value conversion") SECTION("number_integer_t") { - json::number_integer_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("number_unsigned_t") { - json::number_unsigned_t n = j_unsigned.get(); + auto n = j_unsigned.get(); CHECK(json(n) == j_unsigned); } SECTION("short") { - short n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("unsigned short") { - unsigned short n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } @@ -756,7 +756,7 @@ TEST_CASE("value conversion") SECTION("unsigned int") { - unsigned int n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } @@ -768,163 +768,163 @@ TEST_CASE("value conversion") SECTION("unsigned long") { - unsigned long n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("long long") { - long long n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("unsigned long long") { - unsigned long long n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("int8_t") { - int8_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("int16_t") { - int16_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("int32_t") { - int32_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("int64_t") { - int64_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("int8_fast_t") { - int_fast8_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("int16_fast_t") { - int_fast16_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("int32_fast_t") { - int_fast32_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("int64_fast_t") { - int_fast64_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("int8_least_t") { - int_least8_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("int16_least_t") { - int_least16_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("int32_least_t") { - int_least32_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("int64_least_t") { - int_least64_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("uint8_t") { - uint8_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("uint16_t") { - uint16_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("uint32_t") { - uint32_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("uint64_t") { - uint64_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("uint8_fast_t") { - uint_fast8_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("uint16_fast_t") { - uint_fast16_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("uint32_fast_t") { - uint_fast32_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("uint64_fast_t") { - uint_fast64_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("uint8_least_t") { - uint_least8_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("uint16_least_t") { - uint_least16_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("uint32_least_t") { - uint_least32_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("uint64_least_t") { - uint_least64_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } @@ -976,13 +976,13 @@ TEST_CASE("value conversion") SECTION("number_integer_t") { - json::number_integer_t n = j.get(); + auto n = j.get(); CHECK(json(n) == j); } SECTION("number_unsigned_t") { - json::number_unsigned_t n = j_unsigned.get(); + auto n = j_unsigned.get(); CHECK(json(n) == j_unsigned); } @@ -1187,19 +1187,19 @@ TEST_CASE("value conversion") SECTION("number_float_t") { - json::number_float_t n = j.get(); + auto n = j.get(); CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float)); } SECTION("float") { - float n = j.get(); + auto n = j.get(); CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float)); } SECTION("double") { - double n = j.get(); + auto n = j.get(); CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float)); } @@ -1639,6 +1639,7 @@ TEST_CASE("value conversion") enum class cards {kreuz, pik, herz, karo}; +// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - false positive NLOHMANN_JSON_SERIALIZE_ENUM(cards, { {cards::kreuz, "kreuz"}, @@ -1656,6 +1657,7 @@ enum TaskState TS_INVALID = -1, }; +// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - false positive NLOHMANN_JSON_SERIALIZE_ENUM(TaskState, { {TS_INVALID, nullptr}, diff --git a/test/src/unit-deserialization.cpp b/test/src/unit-deserialization.cpp index d2db7e80c3..293d2418fb 100644 --- a/test/src/unit-deserialization.cpp +++ b/test/src/unit-deserialization.cpp @@ -42,13 +42,13 @@ struct SaxEventLogger : public nlohmann::json_sax { bool null() override { - events.push_back("null()"); + events.emplace_back("null()"); return true; } bool boolean(bool val) override { - events.push_back(val ? "boolean(true)" : "boolean(false)"); + events.emplace_back(val ? "boolean(true)" : "boolean(false)"); return true; } @@ -64,7 +64,7 @@ struct SaxEventLogger : public nlohmann::json_sax return true; } - bool number_float(json::number_float_t, const std::string& s) override + bool number_float(json::number_float_t /*val*/, const std::string& s) override { events.push_back("number_float(" + s + ")"); return true; @@ -79,7 +79,7 @@ struct SaxEventLogger : public nlohmann::json_sax bool binary(json::binary_t& val) override { std::string binary_contents = "binary("; - std::string comma_space = ""; + std::string comma_space; for (auto b : val) { binary_contents.append(comma_space); @@ -95,7 +95,7 @@ struct SaxEventLogger : public nlohmann::json_sax { if (elements == std::size_t(-1)) { - events.push_back("start_object()"); + events.emplace_back("start_object()"); } else { @@ -112,7 +112,7 @@ struct SaxEventLogger : public nlohmann::json_sax bool end_object() override { - events.push_back("end_object()"); + events.emplace_back("end_object()"); return true; } @@ -120,7 +120,7 @@ struct SaxEventLogger : public nlohmann::json_sax { if (elements == std::size_t(-1)) { - events.push_back("start_array()"); + events.emplace_back("start_array()"); } else { @@ -131,11 +131,11 @@ struct SaxEventLogger : public nlohmann::json_sax bool end_array() override { - events.push_back("end_array()"); + events.emplace_back("end_array()"); return true; } - bool parse_error(std::size_t position, const std::string&, const json::exception&) override + bool parse_error(std::size_t position, const std::string& /*last_token*/, const json::exception& /*ex*/) override { events.push_back("parse_error(" + std::to_string(position) + ")"); return false; @@ -150,7 +150,7 @@ struct SaxEventLoggerExitAfterStartObject : public SaxEventLogger { if (elements == std::size_t(-1)) { - events.push_back("start_object()"); + events.emplace_back("start_object()"); } else { @@ -175,7 +175,7 @@ struct SaxEventLoggerExitAfterStartArray : public SaxEventLogger { if (elements == std::size_t(-1)) { - events.push_back("start_array()"); + events.emplace_back("start_array()"); } else { @@ -184,7 +184,7 @@ struct SaxEventLoggerExitAfterStartArray : public SaxEventLogger return false; } }; -} +} // namespace TEST_CASE("deserialization") { @@ -192,10 +192,12 @@ TEST_CASE("deserialization") { SECTION("stream") { - std::stringstream ss1, ss2, ss3; - ss1 << "[\"foo\",1,2,3,false,{\"one\":1}]"; - ss2 << "[\"foo\",1,2,3,false,{\"one\":1}]"; - ss3 << "[\"foo\",1,2,3,false,{\"one\":1}]"; + std::stringstream ss1; + std::stringstream ss2; + std::stringstream ss3; + ss1 << R"(["foo",1,2,3,false,{"one":1}])"; + ss2 << R"(["foo",1,2,3,false,{"one":1}])"; + ss3 << R"(["foo",1,2,3,false,{"one":1}])"; json j = json::parse(ss1); CHECK(json::accept(ss2)); CHECK(j == json({"foo", 1, 2, 3, false, {{"one", 1}}})); @@ -214,7 +216,7 @@ TEST_CASE("deserialization") SECTION("string literal") { - auto s = "[\"foo\",1,2,3,false,{\"one\":1}]"; + const auto* s = R"(["foo",1,2,3,false,{"one":1}])"; json j = json::parse(s); CHECK(json::accept(s)); CHECK(j == json({"foo", 1, 2, 3, false, {{"one", 1}}})); @@ -233,7 +235,7 @@ TEST_CASE("deserialization") SECTION("string_t") { - json::string_t s = "[\"foo\",1,2,3,false,{\"one\":1}]"; + json::string_t s = R"(["foo",1,2,3,false,{"one":1}])"; json j = json::parse(s); CHECK(json::accept(s)); CHECK(j == json({"foo", 1, 2, 3, false, {{"one", 1}}})); @@ -253,7 +255,7 @@ TEST_CASE("deserialization") SECTION("operator<<") { std::stringstream ss; - ss << "[\"foo\",1,2,3,false,{\"one\":1}]"; + ss << R"(["foo",1,2,3,false,{"one":1}])"; json j; j << ss; CHECK(j == json({"foo", 1, 2, 3, false, {{"one", 1}}})); @@ -262,7 +264,7 @@ TEST_CASE("deserialization") SECTION("operator>>") { std::stringstream ss; - ss << "[\"foo\",1,2,3,false,{\"one\":1}]"; + ss << R"(["foo",1,2,3,false,{"one":1}])"; json j; ss >> j; CHECK(j == json({"foo", 1, 2, 3, false, {{"one", 1}}})); @@ -278,12 +280,16 @@ TEST_CASE("deserialization") { SECTION("stream") { - std::stringstream ss1, ss2, ss3, ss4, ss5; - ss1 << "[\"foo\",1,2,3,false,{\"one\":1}"; - ss2 << "[\"foo\",1,2,3,false,{\"one\":1}"; - ss3 << "[\"foo\",1,2,3,false,{\"one\":1}"; - ss4 << "[\"foo\",1,2,3,false,{\"one\":1}"; - ss5 << "[\"foo\",1,2,3,false,{\"one\":1}"; + std::stringstream ss1; + std::stringstream ss2; + std::stringstream ss3; + std::stringstream ss4; + std::stringstream ss5; + ss1 << R"(["foo",1,2,3,false,{"one":1})"; + ss2 << R"(["foo",1,2,3,false,{"one":1})"; + ss3 << R"(["foo",1,2,3,false,{"one":1})"; + ss4 << R"(["foo",1,2,3,false,{"one":1})"; + ss5 << R"(["foo",1,2,3,false,{"one":1})"; json _; CHECK_THROWS_AS(_ = json::parse(ss1), json::parse_error&); @@ -309,7 +315,7 @@ TEST_CASE("deserialization") SECTION("string") { - json::string_t s = "[\"foo\",1,2,3,false,{\"one\":1}"; + json::string_t s = R"(["foo",1,2,3,false,{"one":1})"; json _; CHECK_THROWS_AS(_ = json::parse(s), json::parse_error&); CHECK_THROWS_WITH(_ = json::parse(s), @@ -334,9 +340,10 @@ TEST_CASE("deserialization") SECTION("operator<<") { - std::stringstream ss1, ss2; - ss1 << "[\"foo\",1,2,3,false,{\"one\":1}"; - ss2 << "[\"foo\",1,2,3,false,{\"one\":1}"; + std::stringstream ss1; + std::stringstream ss2; + ss1 << R"(["foo",1,2,3,false,{"one":1})"; + ss2 << R"(["foo",1,2,3,false,{"one":1})"; json j; CHECK_THROWS_AS(j << ss1, json::parse_error&); CHECK_THROWS_WITH(j << ss2, @@ -345,9 +352,10 @@ TEST_CASE("deserialization") SECTION("operator>>") { - std::stringstream ss1, ss2; - ss1 << "[\"foo\",1,2,3,false,{\"one\":1}"; - ss2 << "[\"foo\",1,2,3,false,{\"one\":1}"; + std::stringstream ss1; + std::stringstream ss2; + ss1 << R"(["foo",1,2,3,false,{"one":1})"; + ss2 << R"(["foo",1,2,3,false,{"one":1})"; json j; CHECK_THROWS_AS(ss1 >> j, json::parse_error&); CHECK_THROWS_WITH(ss2 >> j, @@ -392,7 +400,7 @@ TEST_CASE("deserialization") SECTION("from array") { - uint8_t v[] = {'t', 'r', 'u', 'e'}; + uint8_t v[] = {'t', 'r', 'u', 'e'}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) CHECK(json::parse(v) == json(true)); CHECK(json::accept(v)); @@ -404,7 +412,7 @@ TEST_CASE("deserialization") SECTION("from chars") { - uint8_t* v = new uint8_t[5]; + auto* v = new uint8_t[5]; // NOLINT(cppcoreguidelines-owning-memory) v[0] = 't'; v[1] = 'r'; v[2] = 'u'; @@ -418,7 +426,7 @@ TEST_CASE("deserialization") CHECK(l.events.size() == 1); CHECK(l.events == std::vector({"boolean(true)"})); - delete[] v; + delete[] v; // NOLINT(cppcoreguidelines-owning-memory) } SECTION("from std::string") @@ -488,7 +496,7 @@ TEST_CASE("deserialization") SECTION("from array") { - uint8_t v[] = {'t', 'r', 'u', 'e'}; + uint8_t v[] = {'t', 'r', 'u', 'e'}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) CHECK(json::parse(std::begin(v), std::end(v)) == json(true)); CHECK(json::accept(std::begin(v), std::end(v))); @@ -553,7 +561,7 @@ TEST_CASE("deserialization") { SECTION("case 1") { - uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u'}; + std::array v = {{'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u'}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -570,7 +578,7 @@ TEST_CASE("deserialization") SECTION("case 2") { - uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u', '1'}; + std::array v = {{'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u', '1'}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -587,7 +595,7 @@ TEST_CASE("deserialization") SECTION("case 3") { - uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u', '1', '1', '1', '1', '1', '1', '1', '1'}; + std::array v = {{'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u', '1', '1', '1', '1', '1', '1', '1', '1'}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -604,7 +612,7 @@ TEST_CASE("deserialization") SECTION("case 4") { - uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', 'u', '1', '1', '1', '1', '1', '1', '1', '1', '\\'}; + std::array v = {{'\"', 'a', 'a', 'a', 'a', 'a', 'a', 'u', '1', '1', '1', '1', '1', '1', '1', '1', '\\'}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -621,7 +629,7 @@ TEST_CASE("deserialization") SECTION("case 5") { - uint8_t v[] = {'\"', 0x7F, 0xC1}; + std::array v = {{'\"', 0x7F, 0xC1}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -638,7 +646,7 @@ TEST_CASE("deserialization") SECTION("case 6") { - uint8_t v[] = {'\"', 0x7F, 0xDF, 0x7F}; + std::array v = {{'\"', 0x7F, 0xDF, 0x7F}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_WITH(_ = json::parse(std::begin(v), std::end(v)), @@ -657,7 +665,7 @@ TEST_CASE("deserialization") SECTION("case 7") { - uint8_t v[] = {'\"', 0x7F, 0xDF, 0xC0}; + std::array v = {{'\"', 0x7F, 0xDF, 0xC0}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -674,7 +682,7 @@ TEST_CASE("deserialization") SECTION("case 8") { - uint8_t v[] = {'\"', 0x7F, 0xE0, 0x9F}; + std::array v = {{'\"', 0x7F, 0xE0, 0x9F}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -691,7 +699,7 @@ TEST_CASE("deserialization") SECTION("case 9") { - uint8_t v[] = {'\"', 0x7F, 0xEF, 0xC0}; + std::array v = {{'\"', 0x7F, 0xEF, 0xC0}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -708,7 +716,7 @@ TEST_CASE("deserialization") SECTION("case 10") { - uint8_t v[] = {'\"', 0x7F, 0xED, 0x7F}; + std::array v = {{'\"', 0x7F, 0xED, 0x7F}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -725,7 +733,7 @@ TEST_CASE("deserialization") SECTION("case 11") { - uint8_t v[] = {'\"', 0x7F, 0xF0, 0x8F}; + std::array v = {{'\"', 0x7F, 0xF0, 0x8F}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -742,7 +750,7 @@ TEST_CASE("deserialization") SECTION("case 12") { - uint8_t v[] = {'\"', 0x7F, 0xF0, 0xC0}; + std::array v = {{'\"', 0x7F, 0xF0, 0xC0}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -759,7 +767,7 @@ TEST_CASE("deserialization") SECTION("case 13") { - uint8_t v[] = {'\"', 0x7F, 0xF3, 0x7F}; + std::array v = {{'\"', 0x7F, 0xF3, 0x7F}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -776,7 +784,7 @@ TEST_CASE("deserialization") SECTION("case 14") { - uint8_t v[] = {'\"', 0x7F, 0xF3, 0xC0}; + std::array v = {{'\"', 0x7F, 0xF3, 0xC0}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -793,7 +801,7 @@ TEST_CASE("deserialization") SECTION("case 15") { - uint8_t v[] = {'\"', 0x7F, 0xF4, 0x7F}; + std::array v = {{'\"', 0x7F, 0xF4, 0x7F}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -810,7 +818,7 @@ TEST_CASE("deserialization") SECTION("case 16") { - uint8_t v[] = {'{', '\"', '\"', ':', '1', '1'}; + std::array v = {{'{', '\"', '\"', ':', '1', '1'}}; json _; CHECK_THROWS_AS(_ = json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK(!json::accept(std::begin(v), std::end(v))); @@ -860,7 +868,8 @@ TEST_CASE("deserialization") CHECK(json::parse(bom + "1") == 1); CHECK(json::parse(std::istringstream(bom + "1")) == 1); - SaxEventLogger l1, l2; + SaxEventLogger l1; + SaxEventLogger l2; CHECK(json::sax_parse(std::istringstream(bom + "1"), &l1)); CHECK(json::sax_parse(bom + "1", &l2)); CHECK(l1.events.size() == 1); @@ -886,7 +895,8 @@ TEST_CASE("deserialization") CHECK_THROWS_WITH(_ = json::parse(std::istringstream(bom.substr(0, 2))), "[json.exception.parse_error.101] parse error at line 1, column 3: syntax error while parsing value - invalid BOM; must be 0xEF 0xBB 0xBF if given; last read: '\xEF\xBB'"); - SaxEventLogger l1, l2; + SaxEventLogger l1; + SaxEventLogger l2; CHECK(!json::sax_parse(std::istringstream(bom.substr(0, 2)), &l1)); CHECK(!json::sax_parse(bom.substr(0, 2), &l2)); CHECK(l1.events.size() == 1); @@ -912,7 +922,8 @@ TEST_CASE("deserialization") CHECK_THROWS_WITH(_ = json::parse(std::istringstream(bom.substr(0, 1))), "[json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing value - invalid BOM; must be 0xEF 0xBB 0xBF if given; last read: '\xEF'"); - SaxEventLogger l1, l2; + SaxEventLogger l1; + SaxEventLogger l2; CHECK(!json::sax_parse(std::istringstream(bom.substr(0, 1)), &l1)); CHECK(!json::sax_parse(bom.substr(0, 1), &l2)); CHECK(l1.events.size() == 1); @@ -942,7 +953,7 @@ TEST_CASE("deserialization") CAPTURE(i1) CAPTURE(i2) - std::string s = ""; + std::string s; s.push_back(static_cast(bom[0] + i0)); s.push_back(static_cast(bom[1] + i1)); s.push_back(static_cast(bom[2] + i2)); @@ -1012,7 +1023,7 @@ TEST_CASE("deserialization") SECTION("SAX and early abort") { - std::string s = "[1, [\"string\", 43.12], null, {\"key1\": true, \"key2\": false}]"; + std::string s = R"([1, ["string", 43.12], null, {"key1": true, "key2": false}])"; SaxEventLogger default_logger; SaxEventLoggerExitAfterStartObject exit_after_start_object; diff --git a/test/src/unit-diagnostics.cpp b/test/src/unit-diagnostics.cpp index 1cea374a74..21ced33b1f 100644 --- a/test/src/unit-diagnostics.cpp +++ b/test/src/unit-diagnostics.cpp @@ -93,7 +93,8 @@ TEST_CASE("Better diagnostics") SECTION("Parse error") { - CHECK_THROWS_WITH_AS(json::parse(""), "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error); + json _; + CHECK_THROWS_WITH_AS(_ = json::parse(""), "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error); } SECTION("Regression test for https://github.com/nlohmann/json/pull/2562#pullrequestreview-574858448") diff --git a/test/src/unit-element_access2.cpp b/test/src/unit-element_access2.cpp index 18fc6a20ca..40b7ac9d6a 100644 --- a/test/src/unit-element_access2.cpp +++ b/test/src/unit-element_access2.cpp @@ -202,7 +202,7 @@ TEST_CASE("element access 2") SECTION("null") { json j_nonobject(json::value_t::null); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::null); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("foo", 1), @@ -214,7 +214,7 @@ TEST_CASE("element access 2") SECTION("boolean") { json j_nonobject(json::value_t::boolean); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::boolean); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("foo", 1), @@ -226,7 +226,7 @@ TEST_CASE("element access 2") SECTION("string") { json j_nonobject(json::value_t::string); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::string); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("foo", 1), @@ -238,7 +238,7 @@ TEST_CASE("element access 2") SECTION("array") { json j_nonobject(json::value_t::array); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::array); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("foo", 1), @@ -250,7 +250,7 @@ TEST_CASE("element access 2") SECTION("number (integer)") { json j_nonobject(json::value_t::number_integer); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::number_integer); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("foo", 1), @@ -262,7 +262,7 @@ TEST_CASE("element access 2") SECTION("number (unsigned)") { json j_nonobject(json::value_t::number_unsigned); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::number_unsigned); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("foo", 1), @@ -274,7 +274,7 @@ TEST_CASE("element access 2") SECTION("number (floating-point)") { json j_nonobject(json::value_t::number_float); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::number_float); CHECK_THROWS_AS(j_nonobject.value("foo", 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("foo", 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("foo", 1), @@ -320,7 +320,7 @@ TEST_CASE("element access 2") SECTION("null") { json j_nonobject(json::value_t::null); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::null); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), @@ -332,7 +332,7 @@ TEST_CASE("element access 2") SECTION("boolean") { json j_nonobject(json::value_t::boolean); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::boolean); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), @@ -344,7 +344,7 @@ TEST_CASE("element access 2") SECTION("string") { json j_nonobject(json::value_t::string); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::string); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), @@ -356,7 +356,7 @@ TEST_CASE("element access 2") SECTION("array") { json j_nonobject(json::value_t::array); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::array); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), @@ -368,7 +368,7 @@ TEST_CASE("element access 2") SECTION("number (integer)") { json j_nonobject(json::value_t::number_integer); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::number_integer); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), @@ -380,7 +380,7 @@ TEST_CASE("element access 2") SECTION("number (unsigned)") { json j_nonobject(json::value_t::number_unsigned); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::number_unsigned); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), @@ -392,7 +392,7 @@ TEST_CASE("element access 2") SECTION("number (floating-point)") { json j_nonobject(json::value_t::number_float); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::number_float); CHECK_THROWS_AS(j_nonobject.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_AS(j_nonobject_const.value("/foo"_json_pointer, 1), json::type_error&); CHECK_THROWS_WITH(j_nonobject.value("/foo"_json_pointer, 1), @@ -811,7 +811,7 @@ TEST_CASE("element access 2") { SECTION("existing element") { - for (auto key : + for (const auto* key : {"integer", "unsigned", "floating", "null", "string", "boolean", "object", "array" }) { @@ -900,7 +900,7 @@ TEST_CASE("element access 2") { SECTION("existing element") { - for (auto key : + for (const auto* key : {"integer", "unsigned", "floating", "null", "string", "boolean", "object", "array" }) { @@ -920,7 +920,7 @@ TEST_CASE("element access 2") SECTION("null") { json j_nonobject(json::value_t::null); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::null); CHECK(j_nonobject.count("foo") == 0); CHECK(j_nonobject_const.count("foo") == 0); } @@ -928,7 +928,7 @@ TEST_CASE("element access 2") SECTION("string") { json j_nonobject(json::value_t::string); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::string); CHECK(j_nonobject.count("foo") == 0); CHECK(j_nonobject_const.count("foo") == 0); } @@ -936,7 +936,7 @@ TEST_CASE("element access 2") SECTION("object") { json j_nonobject(json::value_t::object); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::object); CHECK(j_nonobject.count("foo") == 0); CHECK(j_nonobject_const.count("foo") == 0); } @@ -944,7 +944,7 @@ TEST_CASE("element access 2") SECTION("array") { json j_nonobject(json::value_t::array); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::array); CHECK(j_nonobject.count("foo") == 0); CHECK(j_nonobject_const.count("foo") == 0); } @@ -952,7 +952,7 @@ TEST_CASE("element access 2") SECTION("boolean") { json j_nonobject(json::value_t::boolean); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::boolean); CHECK(j_nonobject.count("foo") == 0); CHECK(j_nonobject_const.count("foo") == 0); } @@ -960,7 +960,7 @@ TEST_CASE("element access 2") SECTION("number (integer)") { json j_nonobject(json::value_t::number_integer); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::number_integer); CHECK(j_nonobject.count("foo") == 0); CHECK(j_nonobject_const.count("foo") == 0); } @@ -968,7 +968,7 @@ TEST_CASE("element access 2") SECTION("number (unsigned)") { json j_nonobject(json::value_t::number_unsigned); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::number_unsigned); CHECK(j_nonobject.count("foo") == 0); CHECK(j_nonobject_const.count("foo") == 0); } @@ -976,7 +976,7 @@ TEST_CASE("element access 2") SECTION("number (floating-point)") { json j_nonobject(json::value_t::number_float); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::number_float); CHECK(j_nonobject.count("foo") == 0); CHECK(j_nonobject_const.count("foo") == 0); } @@ -987,7 +987,7 @@ TEST_CASE("element access 2") { SECTION("existing element") { - for (auto key : + for (const auto* key : {"integer", "unsigned", "floating", "null", "string", "boolean", "object", "array" }) { @@ -1007,7 +1007,7 @@ TEST_CASE("element access 2") SECTION("null") { json j_nonobject(json::value_t::null); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::null); CHECK(j_nonobject.contains("foo") == false); CHECK(j_nonobject_const.contains("foo") == false); } @@ -1015,7 +1015,7 @@ TEST_CASE("element access 2") SECTION("string") { json j_nonobject(json::value_t::string); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::string); CHECK(j_nonobject.contains("foo") == false); CHECK(j_nonobject_const.contains("foo") == false); } @@ -1023,7 +1023,7 @@ TEST_CASE("element access 2") SECTION("object") { json j_nonobject(json::value_t::object); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::object); CHECK(j_nonobject.contains("foo") == false); CHECK(j_nonobject_const.contains("foo") == false); } @@ -1031,7 +1031,7 @@ TEST_CASE("element access 2") SECTION("array") { json j_nonobject(json::value_t::array); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::array); CHECK(j_nonobject.contains("foo") == false); CHECK(j_nonobject_const.contains("foo") == false); } @@ -1039,7 +1039,7 @@ TEST_CASE("element access 2") SECTION("boolean") { json j_nonobject(json::value_t::boolean); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::boolean); CHECK(j_nonobject.contains("foo") == false); CHECK(j_nonobject_const.contains("foo") == false); } @@ -1047,7 +1047,7 @@ TEST_CASE("element access 2") SECTION("number (integer)") { json j_nonobject(json::value_t::number_integer); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::number_integer); CHECK(j_nonobject.contains("foo") == false); CHECK(j_nonobject_const.contains("foo") == false); } @@ -1055,7 +1055,7 @@ TEST_CASE("element access 2") SECTION("number (unsigned)") { json j_nonobject(json::value_t::number_unsigned); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::number_unsigned); CHECK(j_nonobject.contains("foo") == false); CHECK(j_nonobject_const.contains("foo") == false); } @@ -1063,7 +1063,7 @@ TEST_CASE("element access 2") SECTION("number (floating-point)") { json j_nonobject(json::value_t::number_float); - const json j_nonobject_const(j_nonobject); + const json j_nonobject_const(json::value_t::number_float); CHECK(j_nonobject.contains("foo") == false); CHECK(j_nonobject_const.contains("foo") == false); } @@ -1078,7 +1078,7 @@ TEST_CASE("element access 2 (throwing tests)") SECTION("object") { json j = {{"integer", 1}, {"unsigned", 1u}, {"floating", 42.23}, {"null", nullptr}, {"string", "hello world"}, {"boolean", true}, {"object", json::object()}, {"array", {1, 2, 3}}}; - const json j_const = j; + const json j_const = {{"integer", 1}, {"unsigned", 1u}, {"floating", 42.23}, {"null", nullptr}, {"string", "hello world"}, {"boolean", true}, {"object", json::object()}, {"array", {1, 2, 3}}}; SECTION("access specified element with default value") { diff --git a/test/src/unit-items.cpp b/test/src/unit-items.cpp index 4caf76f237..6ecd90d7b3 100644 --- a/test/src/unit-items.cpp +++ b/test/src/unit-items.cpp @@ -48,7 +48,7 @@ TEST_CASE("iterator_wrapper") json j = { {"A", 1}, {"B", 2} }; int counter = 1; - for (auto i : json::iterator_wrapper(j)) + for (auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -125,7 +125,7 @@ TEST_CASE("iterator_wrapper") json j = { {"A", 1}, {"B", 2} }; int counter = 1; - for (const auto i : json::iterator_wrapper(j)) + for (const auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -194,7 +194,7 @@ TEST_CASE("iterator_wrapper") const json j = { {"A", 1}, {"B", 2} }; int counter = 1; - for (auto i : json::iterator_wrapper(j)) + for (auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -260,7 +260,7 @@ TEST_CASE("iterator_wrapper") const json j = { {"A", 1}, {"B", 2} }; int counter = 1; - for (const auto i : json::iterator_wrapper(j)) + for (const auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -329,7 +329,7 @@ TEST_CASE("iterator_wrapper") json j = { "A", "B" }; int counter = 1; - for (auto i : json::iterator_wrapper(j)) + for (auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -406,7 +406,7 @@ TEST_CASE("iterator_wrapper") json j = { "A", "B" }; int counter = 1; - for (const auto i : json::iterator_wrapper(j)) + for (const auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -475,7 +475,7 @@ TEST_CASE("iterator_wrapper") const json j = { "A", "B" }; int counter = 1; - for (auto i : json::iterator_wrapper(j)) + for (auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -541,7 +541,7 @@ TEST_CASE("iterator_wrapper") const json j = { "A", "B" }; int counter = 1; - for (const auto i : json::iterator_wrapper(j)) + for (const auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -610,7 +610,7 @@ TEST_CASE("iterator_wrapper") json j = 1; int counter = 1; - for (auto i : json::iterator_wrapper(j)) + for (auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy) { ++counter; CHECK(i.key() == ""); @@ -646,7 +646,7 @@ TEST_CASE("iterator_wrapper") json j = 1; int counter = 1; - for (const auto i : json::iterator_wrapper(j)) + for (const auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy) { ++counter; CHECK(i.key() == ""); @@ -679,7 +679,7 @@ TEST_CASE("iterator_wrapper") const json j = 1; int counter = 1; - for (auto i : json::iterator_wrapper(j)) + for (auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy) { ++counter; CHECK(i.key() == ""); @@ -709,7 +709,7 @@ TEST_CASE("iterator_wrapper") const json j = 1; int counter = 1; - for (const auto i : json::iterator_wrapper(j)) + for (const auto i : json::iterator_wrapper(j)) // NOLINT(performance-for-range-copy) { ++counter; CHECK(i.key() == ""); @@ -745,7 +745,7 @@ TEST_CASE("items()") json j = { {"A", 1}, {"B", 2} }; int counter = 1; - for (auto i : j.items()) + for (auto i : j.items()) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -822,7 +822,7 @@ TEST_CASE("items()") json j = { {"A", 1}, {"B", 2} }; int counter = 1; - for (const auto i : j.items()) + for (const auto i : j.items()) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -907,7 +907,7 @@ TEST_CASE("items()") const json j = { {"A", 1}, {"B", 2} }; int counter = 1; - for (auto i : j.items()) + for (auto i : j.items()) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -973,7 +973,7 @@ TEST_CASE("items()") const json j = { {"A", 1}, {"B", 2} }; int counter = 1; - for (const auto i : j.items()) + for (const auto i : j.items()) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -1042,7 +1042,7 @@ TEST_CASE("items()") json j = { "A", "B" }; int counter = 1; - for (auto i : j.items()) + for (auto i : j.items()) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -1119,7 +1119,7 @@ TEST_CASE("items()") json j = { "A", "B" }; int counter = 1; - for (const auto i : j.items()) + for (const auto i : j.items()) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -1188,7 +1188,7 @@ TEST_CASE("items()") const json j = { "A", "B" }; int counter = 1; - for (auto i : j.items()) + for (auto i : j.items()) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -1254,7 +1254,7 @@ TEST_CASE("items()") const json j = { "A", "B" }; int counter = 1; - for (const auto i : j.items()) + for (const auto i : j.items()) // NOLINT(performance-for-range-copy) { switch (counter++) { @@ -1323,7 +1323,7 @@ TEST_CASE("items()") json j = 1; int counter = 1; - for (auto i : j.items()) + for (auto i : j.items()) // NOLINT(performance-for-range-copy) { ++counter; CHECK(i.key() == ""); @@ -1359,7 +1359,7 @@ TEST_CASE("items()") json j = 1; int counter = 1; - for (const auto i : j.items()) + for (const auto i : j.items()) // NOLINT(performance-for-range-copy) { ++counter; CHECK(i.key() == ""); @@ -1392,7 +1392,7 @@ TEST_CASE("items()") const json j = 1; int counter = 1; - for (auto i : j.items()) + for (auto i : j.items()) // NOLINT(performance-for-range-copy) { ++counter; CHECK(i.key() == ""); @@ -1422,7 +1422,7 @@ TEST_CASE("items()") const json j = 1; int counter = 1; - for (const auto i : j.items()) + for (const auto i : j.items()) // NOLINT(performance-for-range-copy) { ++counter; CHECK(i.key() == ""); diff --git a/test/src/unit-json_patch.cpp b/test/src/unit-json_patch.cpp index af44e42326..7cab857022 100644 --- a/test/src/unit-json_patch.cpp +++ b/test/src/unit-json_patch.cpp @@ -1339,7 +1339,7 @@ TEST_CASE("JSON patch") SECTION("Tests from github.com/json-patch/json-patch-tests") { - for (auto filename : + for (const auto* filename : { TEST_DATA_DIRECTORY "/json-patch-tests/spec_tests.json", TEST_DATA_DIRECTORY "/json-patch-tests/tests.json" diff --git a/test/src/unit-json_pointer.cpp b/test/src/unit-json_pointer.cpp index 52a798fd29..d5881beac4 100644 --- a/test/src/unit-json_pointer.cpp +++ b/test/src/unit-json_pointer.cpp @@ -530,7 +530,7 @@ TEST_CASE("JSON pointers") SECTION("string representation") { - for (auto ptr : + for (const auto* ptr : {"", "/foo", "/foo/0", "/", "/a~1b", "/c%d", "/e^f", "/g|h", "/i\\j", "/k\"l", "/ ", "/m~0n" }) { diff --git a/test/src/unit-msgpack.cpp b/test/src/unit-msgpack.cpp index e49a4203c3..b9377dcbb4 100644 --- a/test/src/unit-msgpack.cpp +++ b/test/src/unit-msgpack.cpp @@ -52,42 +52,42 @@ class SaxCountdown return events_left-- > 0; } - bool boolean(bool) + bool boolean(bool /*unused*/) { return events_left-- > 0; } - bool number_integer(json::number_integer_t) + bool number_integer(json::number_integer_t /*unused*/) { return events_left-- > 0; } - bool number_unsigned(json::number_unsigned_t) + bool number_unsigned(json::number_unsigned_t /*unused*/) { return events_left-- > 0; } - bool number_float(json::number_float_t, const std::string&) + bool number_float(json::number_float_t /*unused*/, const std::string& /*unused*/) { return events_left-- > 0; } - bool string(std::string&) + bool string(std::string& /*unused*/) { return events_left-- > 0; } - bool binary(std::vector&) + bool binary(std::vector& /*unused*/) { return events_left-- > 0; } - bool start_object(std::size_t) + bool start_object(std::size_t /*unused*/) { return events_left-- > 0; } - bool key(std::string&) + bool key(std::string& /*unused*/) { return events_left-- > 0; } @@ -97,7 +97,7 @@ class SaxCountdown return events_left-- > 0; } - bool start_array(std::size_t) + bool start_array(std::size_t /*unused*/) { return events_left-- > 0; } @@ -107,7 +107,7 @@ class SaxCountdown return events_left-- > 0; } - bool parse_error(std::size_t, const std::string&, const json::exception&) + bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) // NOLINT(readability-convert-member-functions-to-static) { return false; } @@ -115,7 +115,7 @@ class SaxCountdown private: int events_left = 0; }; -} +} // namespace TEST_CASE("MessagePack") { @@ -258,7 +258,7 @@ TEST_CASE("MessagePack") // check individual bytes CHECK(result[0] == 0xcc); - uint8_t restored = static_cast(result[1]); + auto restored = static_cast(result[1]); CHECK(restored == i); // roundtrip @@ -293,7 +293,7 @@ TEST_CASE("MessagePack") // check individual bytes CHECK(result[0] == 0xcd); - uint16_t restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); + auto restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); CHECK(restored == i); // roundtrip @@ -436,7 +436,7 @@ TEST_CASE("MessagePack") const auto result = json::to_msgpack(j); CHECK(result == expected); - int16_t restored = static_cast((result[1] << 8) + result[2]); + auto restored = static_cast((result[1] << 8) + result[2]); CHECK(restored == -9263); // roundtrip @@ -446,7 +446,7 @@ TEST_CASE("MessagePack") SECTION("-32768..-129 (int 16)") { - for (int16_t i = -32768; i <= -129; ++i) + for (int16_t i = -32768; i <= int16_t(-129); ++i) { CAPTURE(i) @@ -469,7 +469,7 @@ TEST_CASE("MessagePack") // check individual bytes CHECK(result[0] == 0xd1); - int16_t restored = static_cast((result[1] << 8) + result[2]); + auto restored = static_cast((result[1] << 8) + result[2]); CHECK(restored == i); // roundtrip @@ -485,7 +485,7 @@ TEST_CASE("MessagePack") numbers.push_back(-65536); numbers.push_back(-77777); numbers.push_back(-1048576); - numbers.push_back(-2147483648ll); + numbers.push_back(-2147483648LL); for (auto i : numbers) { CAPTURE(i) @@ -527,7 +527,7 @@ TEST_CASE("MessagePack") { std::vector numbers; numbers.push_back(INT64_MIN); - numbers.push_back(-2147483649ll); + numbers.push_back(-2147483649LL); for (auto i : numbers) { CAPTURE(i) @@ -630,7 +630,7 @@ TEST_CASE("MessagePack") // check individual bytes CHECK(result[0] == 0xcc); - uint8_t restored = static_cast(result[1]); + auto restored = static_cast(result[1]); CHECK(restored == i); // roundtrip @@ -664,7 +664,7 @@ TEST_CASE("MessagePack") // check individual bytes CHECK(result[0] == 0xcd); - uint16_t restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); + auto restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); CHECK(restored == i); // roundtrip @@ -1088,7 +1088,7 @@ TEST_CASE("MessagePack") SECTION("{\"a\": {\"b\": {\"c\": {}}}}") { - json j = json::parse("{\"a\": {\"b\": {\"c\": {}}}}"); + json j = json::parse(R"({"a": {"b": {"c": {}}}})"); std::vector expected = { 0x81, 0xa1, 0x61, 0x81, 0xa1, 0x62, 0x81, 0xa1, 0x63, 0x80 @@ -1647,23 +1647,21 @@ TEST_CASE("MessagePack roundtrips" * doctest::skip()) SECTION("input from msgpack-python") { // most of these are excluded due to differences in key order (not a real problem) - auto exclude_packed = std::set - { - TEST_DATA_DIRECTORY "/json.org/1.json", - TEST_DATA_DIRECTORY "/json.org/2.json", - TEST_DATA_DIRECTORY "/json.org/3.json", - TEST_DATA_DIRECTORY "/json.org/4.json", - TEST_DATA_DIRECTORY "/json.org/5.json", - TEST_DATA_DIRECTORY "/json_testsuite/sample.json", // kills AppVeyor - TEST_DATA_DIRECTORY "/json_tests/pass1.json", - TEST_DATA_DIRECTORY "/regression/working_file.json", - TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object.json", - TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_basic.json", - TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_duplicated_key.json", - TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_long_strings.json", - TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_simple.json", - TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_string_unicode.json", - }; + std::set exclude_packed; + exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/1.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/2.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/3.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/4.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/json.org/5.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/json_testsuite/sample.json"); // kills AppVeyor + exclude_packed.insert(TEST_DATA_DIRECTORY "/json_tests/pass1.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/regression/working_file.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_basic.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_duplicated_key.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_long_strings.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_simple.json"); + exclude_packed.insert(TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_object_string_unicode.json"); for (std::string filename : { @@ -1870,7 +1868,7 @@ TEST_CASE("MessagePack roundtrips" * doctest::skip()) // parse MessagePack file auto packed = utils::read_binary_file(filename + ".msgpack"); - if (!exclude_packed.count(filename)) + if (exclude_packed.count(filename) == 0u) { { INFO_WITH_TEMP(filename + ": output adapters: std::vector"); diff --git a/test/src/unit-noexcept.cpp b/test/src/unit-noexcept.cpp index 7e657bf9fb..25de3410f6 100644 --- a/test/src/unit-noexcept.cpp +++ b/test/src/unit-noexcept.cpp @@ -42,16 +42,16 @@ enum test struct pod {}; struct pod_bis {}; -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); -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) {} +void to_json(json& /*unused*/, pod /*unused*/) noexcept; +void to_json(json& /*unused*/, pod_bis /*unused*/); +void from_json(const json& /*unused*/, pod /*unused*/) noexcept; +void from_json(const json& /*unused*/, pod_bis /*unused*/); +void to_json(json& /*unused*/, pod /*unused*/) noexcept {} +void to_json(json& /*unused*/, pod_bis /*unused*/) {} +void from_json(const json& /*unused*/, pod /*unused*/) noexcept {} +void from_json(const json& /*unused*/, pod_bis /*unused*/) {} -static json* j = nullptr; +json* j = nullptr; static_assert(noexcept(json{}), ""); static_assert(noexcept(nlohmann::to_json(*j, 2)), ""); @@ -66,7 +66,7 @@ static_assert(noexcept(json(pod{})), ""); static_assert(noexcept(j->get()), ""); static_assert(!noexcept(j->get()), ""); static_assert(noexcept(json(pod{})), ""); -} +} // namespace TEST_CASE("runtime checks") { diff --git a/test/src/unit-ordered_map.cpp b/test/src/unit-ordered_map.cpp index 645183d8c4..47d049de66 100644 --- a/test/src/unit-ordered_map.cpp +++ b/test/src/unit-ordered_map.cpp @@ -49,6 +49,7 @@ TEST_CASE("ordered_map") std::map m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}}; ordered_map om(m.begin(), m.end()); const auto com = om; + om.clear(); // silence a warning by forbidding having "const auto& com = om;" CHECK(com.size() == 3); } } diff --git a/test/src/unit-readme.cpp b/test/src/unit-readme.cpp index ed2a4dcee4..7db7b4e6bd 100644 --- a/test/src/unit-readme.cpp +++ b/test/src/unit-readme.cpp @@ -52,7 +52,7 @@ TEST_CASE("README" * doctest::skip()) { { // redirect std::cout for the README file - auto old_cout_buffer = std::cout.rdbuf(); + auto* old_cout_buffer = std::cout.rdbuf(); std::ostringstream new_stream; std::cout.rdbuf(new_stream.rdbuf()); { @@ -123,7 +123,7 @@ TEST_CASE("README" * doctest::skip()) { // create object from string literal - json j = "{ \"happy\": true, \"pi\": 3.141 }"_json; + json j = "{ \"happy\": true, \"pi\": 3.141 }"_json; // NOLINT(modernize-raw-string-literal) // or even nicer with a raw string literal auto j2 = R"( @@ -134,7 +134,7 @@ TEST_CASE("README" * doctest::skip()) )"_json; // or explicitly - auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }"); + auto j3 = json::parse(R"({"happy": true, "pi": 3.141})"); // explicit conversion to string std::string s = j.dump(); // {\"happy\":true,\"pi\":3.141} @@ -158,17 +158,17 @@ TEST_CASE("README" * doctest::skip()) j.push_back(true); // comparison - bool x = (j == "[\"foo\", 1, true]"_json); // true + bool x = (j == R"(["foo", 1, true])"_json); // true CHECK(x == true); // iterate the array - for (json::iterator it = j.begin(); it != j.end(); ++it) + for (json::iterator it = j.begin(); it != j.end(); ++it) // NOLINT(modernize-loop-convert) { std::cout << *it << '\n'; } // range-based for - for (auto element : j) + for (auto& element : j) { std::cout << element << '\n'; } diff --git a/test/src/unit-reference_access.cpp b/test/src/unit-reference_access.cpp index c983677658..125c7ea4f0 100644 --- a/test/src/unit-reference_access.cpp +++ b/test/src/unit-reference_access.cpp @@ -56,11 +56,11 @@ TEST_CASE("reference access") json value = {{"one", 1}, {"two", 2}}; // check if references are returned correctly - test_type& p1 = value.get_ref(); + auto& p1 = value.get_ref(); CHECK(&p1 == value.get_ptr()); CHECK(p1 == value.get()); - const test_type& p2 = value.get_ref(); + const auto& p2 = value.get_ref(); CHECK(&p2 == value.get_ptr()); CHECK(p2 == value.get()); @@ -95,7 +95,7 @@ TEST_CASE("reference access") // test_type& p1 = value.get_ref(); // check if references are returned correctly - const test_type& p2 = value.get_ref(); + const auto& p2 = value.get_ref(); CHECK(&p2 == value.get_ptr()); CHECK(p2 == value.get()); } @@ -106,11 +106,11 @@ TEST_CASE("reference access") json value = {1, 2, 3, 4}; // check if references are returned correctly - test_type& p1 = value.get_ref(); + auto& p1 = value.get_ref(); CHECK(&p1 == value.get_ptr()); CHECK(p1 == value.get()); - const test_type& p2 = value.get_ref(); + const auto& p2 = value.get_ref(); CHECK(&p2 == value.get_ptr()); CHECK(p2 == value.get()); @@ -142,11 +142,11 @@ TEST_CASE("reference access") json value = "hello"; // check if references are returned correctly - test_type& p1 = value.get_ref(); + auto& p1 = value.get_ref(); CHECK(&p1 == value.get_ptr()); CHECK(p1 == value.get()); - const test_type& p2 = value.get_ref(); + const auto& p2 = value.get_ref(); CHECK(&p2 == value.get_ptr()); CHECK(p2 == value.get()); @@ -178,11 +178,11 @@ TEST_CASE("reference access") json value = false; // check if references are returned correctly - test_type& p1 = value.get_ref(); + auto& p1 = value.get_ref(); CHECK(&p1 == value.get_ptr()); CHECK(p1 == value.get()); - const test_type& p2 = value.get_ref(); + const auto& p2 = value.get_ref(); CHECK(&p2 == value.get_ptr()); CHECK(p2 == value.get()); @@ -214,11 +214,11 @@ TEST_CASE("reference access") json value = -23; // check if references are returned correctly - test_type& p1 = value.get_ref(); + auto& p1 = value.get_ref(); CHECK(&p1 == value.get_ptr()); CHECK(p1 == value.get()); - const test_type& p2 = value.get_ref(); + const auto& p2 = value.get_ref(); CHECK(&p2 == value.get_ptr()); CHECK(p2 == value.get()); @@ -250,11 +250,11 @@ TEST_CASE("reference access") json value = 23u; // check if references are returned correctly - test_type& p1 = value.get_ref(); + auto& p1 = value.get_ref(); CHECK(&p1 == value.get_ptr()); CHECK(p1 == value.get()); - const test_type& p2 = value.get_ref(); + const auto& p2 = value.get_ref(); CHECK(&p2 == value.get_ptr()); CHECK(p2 == value.get()); @@ -286,11 +286,11 @@ TEST_CASE("reference access") json value = 42.23; // check if references are returned correctly - test_type& p1 = value.get_ref(); + auto& p1 = value.get_ref(); CHECK(&p1 == value.get_ptr()); CHECK(p1 == value.get()); - const test_type& p2 = value.get_ref(); + const auto& p2 = value.get_ref(); CHECK(&p2 == value.get_ptr()); CHECK(p2 == value.get()); diff --git a/test/src/unit-regression1.cpp b/test/src/unit-regression1.cpp index bcb34ca87c..3d4a42fe14 100644 --- a/test/src/unit-regression1.cpp +++ b/test/src/unit-regression1.cpp @@ -94,7 +94,7 @@ template struct foo_serializer < T, typename std::enable_if < !std::is_same::value >::type > { template - static void to_json(BasicJsonType& j, const T& value) noexcept + static void to_json(BasicJsonType& j, const T& value) noexcept // NOLINT(bugprone-exception-escape) { ::nlohmann::to_json(j, value); } @@ -104,7 +104,7 @@ struct foo_serializer < T, typename std::enable_if < !std::is_same::valu ::nlohmann::from_json(j, value); } }; -} +} // namespace ns using foo_json = nlohmann::basic_json>; @@ -115,10 +115,13 @@ using foo_json = nlohmann::basic_json> j1; i1_2_3 >> j2; i1_2_3 >> j3; @@ -1445,8 +1450,8 @@ TEST_CASE("regression tests 1") SECTION("issue #838 - incorrect parse error with binary data in keys") { - uint8_t key1[] = { 103, 92, 117, 48, 48, 48, 55, 92, 114, 215, 126, 214, 95, 92, 34, 174, 40, 71, 38, 174, 40, 71, 38, 223, 134, 247, 127, 0 }; - std::string key1_str(reinterpret_cast(key1)); + std::array key1 = {{ 103, 92, 117, 48, 48, 48, 55, 92, 114, 215, 126, 214, 95, 92, 34, 174, 40, 71, 38, 174, 40, 71, 38, 223, 134, 247, 127, 0 }}; + std::string key1_str(reinterpret_cast(key1.data())); json j = key1_str; CHECK_THROWS_AS(j.dump(), json::type_error&); CHECK_THROWS_WITH(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 10: 0x7E"); @@ -1528,7 +1533,7 @@ TEST_CASE("regression tests 1") SECTION("issue #971 - Add a SAX parser - late bug") { // a JSON text - auto text = R"( + const auto* text = R"( { "Image": { "Width": 800, @@ -1549,14 +1554,7 @@ TEST_CASE("regression tests 1") json::parser_callback_t cb = [](int /*depth*/, json::parse_event_t event, json & parsed) { // skip object elements with key "Thumbnail" - if (event == json::parse_event_t::key && parsed == json("Thumbnail")) - { - return false; - } - else - { - return true; - } + return !(event == json::parse_event_t::key && parsed == json("Thumbnail")); }; // parse (with callback) and serialize JSON diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 1e8c4922a7..6260176775 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -37,11 +37,9 @@ DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal") #include using nlohmann::json; -#include -#include #include #include -#include +#include #if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 #define JSON_HAS_CPP_17 @@ -68,19 +66,20 @@ namespace { struct NonDefaultFromJsonStruct { }; -inline bool operator== (NonDefaultFromJsonStruct const&, NonDefaultFromJsonStruct const&) +inline bool operator== (NonDefaultFromJsonStruct const& /*unused*/, NonDefaultFromJsonStruct const& /*unused*/) { return true; } enum class for_1647 { one, two }; +// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays): this is a false positive NLOHMANN_JSON_SERIALIZE_ENUM(for_1647, { {for_1647::one, "one"}, {for_1647::two, "two"}, }) -} +} // namespace ///////////////////////////////////////////////////////////////////// // for #1299 @@ -89,17 +88,19 @@ NLOHMANN_JSON_SERIALIZE_ENUM(for_1647, struct Data { Data() = default; - Data(const std::string& a_, const std::string b_) : a(a_), b(b_) {} + Data(std::string a_, std::string b_) : a(std::move(a_)), b(std::move(b_)) {} std::string a {}; std::string b {}; }; +void from_json(const json& j, Data& data); void from_json(const json& j, Data& data) { j["a"].get_to(data.a); j["b"].get_to(data.b); } +bool operator==(Data const& lhs, Data const& rhs); bool operator==(Data const& lhs, Data const& rhs) { return lhs.a == rhs.a && lhs.b == rhs.b; @@ -115,12 +116,12 @@ namespace nlohmann template <> struct adl_serializer { - static NonDefaultFromJsonStruct from_json (json const&) noexcept + static NonDefaultFromJsonStruct from_json (json const& /*unused*/) noexcept { return {}; } }; -} +} // namespace nlohmann ///////////////////////////////////////////////////////////////////// // for #1805 @@ -137,7 +138,7 @@ TEST_CASE("regression tests 2") { SECTION("issue #1001 - Fix memory leak during parser callback") { - auto geojsonExample = R"( + const auto* geojsonExample = R"( { "type": "FeatureCollection", "features": [ { "type": "Feature", @@ -172,7 +173,7 @@ TEST_CASE("regression tests 2") ] })"; - json::parser_callback_t cb = [&](int, json::parse_event_t event, json & parsed) + json::parser_callback_t cb = [&](int /*level*/, json::parse_event_t event, json & parsed) { // skip uninteresting events if (event == json::parse_event_t::value && !parsed.is_primitive()) @@ -288,7 +289,7 @@ TEST_CASE("regression tests 2") json dump_test; dump_test["1"] = std::string(length, -1); - std::string expected = "{\"1\":\""; + std::string expected = R"({"1":")"; for (int i = 0; i < length; ++i) { expected += "\\ufffd"; @@ -305,7 +306,7 @@ TEST_CASE("regression tests 2") json dump_test; dump_test["1"] = std::string(length, -2); - std::string expected = "{\"1\":\""; + std::string expected = R"({"1":")"; for (int i = 0; i < length; ++i) { expected += "\xEF\xBF\xBD"; @@ -318,29 +319,31 @@ TEST_CASE("regression tests 2") SECTION("test case in issue #1445") { nlohmann::json dump_test; - const int data[] = + const std::array data = { - 109, 108, 103, 125, -122, -53, 115, - 18, 3, 0, 102, 19, 1, 15, - -110, 13, -3, -1, -81, 32, 2, - 0, 0, 0, 0, 0, 0, 0, - 8, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, -80, 2, - 0, 0, 96, -118, 46, -116, 46, - 109, -84, -87, 108, 14, 109, -24, - -83, 13, -18, -51, -83, -52, -115, - 14, 6, 32, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, - 64, 3, 0, 0, 0, 35, -74, - -73, 55, 57, -128, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, - 0, 0, 33, 0, 0, 0, -96, - -54, -28, -26 + { + 109, 108, 103, 125, -122, -53, 115, + 18, 3, 0, 102, 19, 1, 15, + -110, 13, -3, -1, -81, 32, 2, + 0, 0, 0, 0, 0, 0, 0, + 8, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, -80, 2, + 0, 0, 96, -118, 46, -116, 46, + 109, -84, -87, 108, 14, 109, -24, + -83, 13, -18, -51, -83, -52, -115, + 14, 6, 32, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, + 64, 3, 0, 0, 0, 35, -74, + -73, 55, 57, -128, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, + 0, 0, 33, 0, 0, 0, -96, + -54, -28, -26 + } }; std::string s; - for (unsigned i = 0; i < sizeof(data) / sizeof(int); i++) + for (int i : data) { - s += static_cast(data[i]); + s += static_cast(i); } dump_test["1"] = s; dump_test.dump(-1, ' ', true, nlohmann::json::error_handler_t::replace); @@ -395,7 +398,7 @@ TEST_CASE("regression tests 2") SECTION("string array") { - const char input[] = { 'B', 0x00 }; + const std::array input = {{ 'B', 0x00 }}; json cbor = json::from_cbor(input, true, false); CHECK(cbor.is_discarded()); } @@ -430,8 +433,8 @@ TEST_CASE("regression tests 2") SECTION("issue #2067 - cannot serialize binary data to text JSON") { - const unsigned char data[] = {0x81, 0xA4, 0x64, 0x61, 0x74, 0x61, 0xC4, 0x0F, 0x33, 0x30, 0x30, 0x32, 0x33, 0x34, 0x30, 0x31, 0x30, 0x37, 0x30, 0x35, 0x30, 0x31, 0x30}; - json j = json::from_msgpack(data, sizeof(data) / sizeof(data[0])); + const std::array data = {{0x81, 0xA4, 0x64, 0x61, 0x74, 0x61, 0xC4, 0x0F, 0x33, 0x30, 0x30, 0x32, 0x33, 0x34, 0x30, 0x31, 0x30, 0x37, 0x30, 0x35, 0x30, 0x31, 0x30}}; + json j = json::from_msgpack(data.data(), data.size()); CHECK_NOTHROW( j.dump(4, // Indent ' ', // Indent char diff --git a/test/src/unit-serialization.cpp b/test/src/unit-serialization.cpp index 3a733d2ff4..2247d73a95 100644 --- a/test/src/unit-serialization.cpp +++ b/test/src/unit-serialization.cpp @@ -183,10 +183,10 @@ TEST_CASE("serialization") CHECK(to_string(j) == "\"" + expected + "\""); }; - test("{\"x\":5,\"y\":6}", "{\\\"x\\\":5,\\\"y\\\":6}"); - test("{\"x\":[10,null,null,null]}", "{\\\"x\\\":[10,null,null,null]}"); + test(R"({"x":5,"y":6})", R"({\"x\":5,\"y\":6})"); + test("{\"x\":[10,null,null,null]}", R"({\"x\":[10,null,null,null]})"); test("test", "test"); - test("[3,\"false\",false]", "[3,\\\"false\\\",false]"); + test("[3,\"false\",false]", R"([3,\"false\",false])"); } } diff --git a/test/src/unit-testsuites.cpp b/test/src/unit-testsuites.cpp index da8b27d78f..96239987d7 100644 --- a/test/src/unit-testsuites.cpp +++ b/test/src/unit-testsuites.cpp @@ -41,7 +41,7 @@ TEST_CASE("compliance tests from json.org") SECTION("expected failures") { - for (auto filename : + for (const auto* filename : { //TEST_DATA_DIRECTORY "/json_tests/fail1.json", TEST_DATA_DIRECTORY "/json_tests/fail2.json", @@ -90,7 +90,7 @@ TEST_CASE("compliance tests from json.org") // these tests fail above, because the parser does not end on EOF; // they succeed when the operator>> is used, because it does not // have this constraint - for (auto filename : + for (const auto* filename : { TEST_DATA_DIRECTORY "/json_tests/fail7.json", TEST_DATA_DIRECTORY "/json_tests/fail8.json", @@ -106,7 +106,7 @@ TEST_CASE("compliance tests from json.org") SECTION("expected passes") { - for (auto filename : + for (const auto* filename : { TEST_DATA_DIRECTORY "/json_tests/pass1.json", TEST_DATA_DIRECTORY "/json_tests/pass2.json", @@ -235,13 +235,9 @@ TEST_CASE("compliance tests from nativejson-benchmark") 5708990770823839524233143877797980545530986496.0); { - char n1e308[312]; // '1' followed by 308 '0' + std::string n1e308(312, '0'); // '1' followed by 308 '0' n1e308[0] = '['; n1e308[1] = '1'; - for (int j = 2; j < 310; j++) - { - n1e308[j] = '0'; - } n1e308[310] = ']'; n1e308[311] = '\0'; TEST_DOUBLE(n1e308, 1E308); @@ -272,20 +268,20 @@ TEST_CASE("compliance tests from nativejson-benchmark") TEST_STRING("[\"\"]", ""); TEST_STRING("[\"Hello\"]", "Hello"); - TEST_STRING("[\"Hello\\nWorld\"]", "Hello\nWorld"); + TEST_STRING(R"(["Hello\nWorld"])", "Hello\nWorld"); //TEST_STRING("[\"Hello\\u0000World\"]", "Hello\0World"); - TEST_STRING("[\"\\\"\\\\/\\b\\f\\n\\r\\t\"]", "\"\\/\b\f\n\r\t"); - TEST_STRING("[\"\\u0024\"]", "\x24"); // Dollar sign U+0024 - TEST_STRING("[\"\\u00A2\"]", "\xC2\xA2"); // Cents sign U+00A2 - TEST_STRING("[\"\\u20AC\"]", "\xE2\x82\xAC"); // Euro sign U+20AC - TEST_STRING("[\"\\uD834\\uDD1E\"]", "\xF0\x9D\x84\x9E"); // G clef sign U+1D11E + TEST_STRING(R"(["\"\\/\b\f\n\r\t"])", "\"\\/\b\f\n\r\t"); + TEST_STRING(R"(["\u0024"])", "$"); // Dollar sign U+0024 + TEST_STRING(R"(["\u00A2"])", "\xC2\xA2"); // Cents sign U+00A2 + TEST_STRING(R"(["\u20AC"])", "\xE2\x82\xAC"); // Euro sign U+20AC + TEST_STRING(R"(["\uD834\uDD1E"])", "\xF0\x9D\x84\x9E"); // G clef sign U+1D11E } SECTION("roundtrip") { // test cases are from https://github.com/miloyip/nativejson-benchmark/tree/master/test/data/roundtrip - for (auto filename : + for (const auto* filename : { TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip01.json", TEST_DATA_DIRECTORY "/json_roundtrip/roundtrip02.json", @@ -441,7 +437,7 @@ TEST_CASE("RFC 7159 examples") SECTION("13 Examples") { { - auto json_contents = R"( + const auto* json_contents = R"( { "Image": { "Width": 800, @@ -462,7 +458,7 @@ TEST_CASE("RFC 7159 examples") } { - auto json_contents = R"( + const auto* json_contents = R"( [ { "precision": "zip", @@ -500,7 +496,7 @@ TEST_CASE("nst's JSONTestSuite") { SECTION("y") { - for (auto filename : + for (const auto* filename : { TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_arraysWithSpaces.json", TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/y_array_empty-string.json", @@ -610,7 +606,7 @@ TEST_CASE("nst's JSONTestSuite") SECTION("n") { - for (auto filename : + for (const auto* filename : { TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/n_array_1_true_without_comma.json", TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/n_array_a_invalid_utf8.json", @@ -822,7 +818,7 @@ TEST_CASE("nst's JSONTestSuite") // these tests fail above, because the parser does not end on EOF; // they succeed when the operator>> is used, because it does not // have this constraint - for (auto filename : + for (const auto* filename : { TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/n_array_comma_after_close.json", TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/n_array_extra_close.json", @@ -852,7 +848,7 @@ TEST_CASE("nst's JSONTestSuite") SECTION("i -> y") { - for (auto filename : + for (const auto* filename : { // we do not pose a limit on nesting TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/i_structure_500_nested_arrays.json", @@ -876,7 +872,7 @@ TEST_CASE("nst's JSONTestSuite") // numbers that overflow during parsing SECTION("i/y -> n (out of range)") { - for (auto filename : + for (const auto* filename : { TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/i_number_neg_int_huge_exp.json", TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/i_number_pos_double_huge_exp.json", @@ -895,7 +891,7 @@ TEST_CASE("nst's JSONTestSuite") SECTION("i -> n") { - for (auto filename : + for (const auto* filename : { TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/i_object_key_lone_2nd_surrogate.json", TEST_DATA_DIRECTORY "/nst_json_testsuite/test_parsing/i_string_1st_surrogate_but_2nd_missing.json", @@ -928,7 +924,7 @@ TEST_CASE("nst's JSONTestSuite (2)") { SECTION("y") { - for (auto filename : + for (const auto* filename : { TEST_DATA_DIRECTORY "/nst_json_testsuite2/test_parsing/y_array_arraysWithSpaces.json", TEST_DATA_DIRECTORY "/nst_json_testsuite2/test_parsing/y_array_empty-string.json", @@ -1039,7 +1035,7 @@ TEST_CASE("nst's JSONTestSuite (2)") SECTION("n") { - for (auto filename : + for (const auto* filename : { TEST_DATA_DIRECTORY "/nst_json_testsuite2/test_parsing/n_array_1_true_without_comma.json", TEST_DATA_DIRECTORY "/nst_json_testsuite2/test_parsing/n_array_a_invalid_utf8.json", @@ -1241,7 +1237,7 @@ TEST_CASE("nst's JSONTestSuite (2)") SECTION("n (previously overflowed)") { - for (auto filename : + for (const auto* filename : { TEST_DATA_DIRECTORY "/nst_json_testsuite2/test_parsing/n_structure_100000_opening_arrays.json", TEST_DATA_DIRECTORY "/nst_json_testsuite2/test_parsing/n_structure_open_array_object.json" @@ -1256,7 +1252,7 @@ TEST_CASE("nst's JSONTestSuite (2)") SECTION("i -> y") { - for (auto filename : + for (const auto* filename : { TEST_DATA_DIRECTORY "/nst_json_testsuite2/test_parsing/i_number_double_huge_neg_exp.json", //TEST_DATA_DIRECTORY "/nst_json_testsuite2/test_parsing/i_number_huge_exp.json", @@ -1307,7 +1303,7 @@ TEST_CASE("nst's JSONTestSuite (2)") SECTION("i -> n") { - for (auto filename : + for (const auto* filename : { //TEST_DATA_DIRECTORY "/nst_json_testsuite2/test_parsing/i_number_double_huge_neg_exp.json", TEST_DATA_DIRECTORY "/nst_json_testsuite2/test_parsing/i_number_huge_exp.json", @@ -1373,7 +1369,7 @@ std::string trim(const std::string& str) size_t last = str.find_last_not_of(' '); return str.substr(first, (last - first + 1)); } -} +} // namespace TEST_CASE("Big List of Naughty Strings") { @@ -1399,7 +1395,7 @@ TEST_CASE("Big List of Naughty Strings") line = trim(line); // remove trailing comma - line = line.substr(0, line.find_last_of(",")); + line = line.substr(0, line.find_last_of(',')); // discard lines without at least two characters (quotes) if (line.size() < 2) diff --git a/test/src/unit-to_chars.cpp b/test/src/unit-to_chars.cpp index 2861928f8f..c9a01fab72 100644 --- a/test/src/unit-to_chars.cpp +++ b/test/src/unit-to_chars.cpp @@ -38,7 +38,7 @@ using nlohmann::detail::dtoa_impl::reinterpret_bits; namespace { -static float make_float(uint32_t sign_bit, uint32_t biased_exponent, uint32_t significand) +float make_float(uint32_t sign_bit, uint32_t biased_exponent, uint32_t significand) { assert(sign_bit == 0 || sign_bit == 1); assert(biased_exponent <= 0xFF); @@ -54,7 +54,7 @@ static float make_float(uint32_t sign_bit, uint32_t biased_exponent, uint32_t si } // ldexp -- convert f * 2^e to IEEE single precision -static float make_float(uint64_t f, int e) +float make_float(uint64_t f, int e) { constexpr uint64_t kHiddenBit = 0x00800000; constexpr uint64_t kSignificandMask = 0x007FFFFF; @@ -90,7 +90,7 @@ static float make_float(uint64_t f, int e) return reinterpret_bits(static_cast(bits)); } -static double make_double(uint64_t sign_bit, uint64_t biased_exponent, uint64_t significand) +double make_double(uint64_t sign_bit, uint64_t biased_exponent, uint64_t significand) { assert(sign_bit == 0 || sign_bit == 1); assert(biased_exponent <= 0x7FF); @@ -106,7 +106,7 @@ static double make_double(uint64_t sign_bit, uint64_t biased_exponent, uint64_t } // ldexp -- convert f * 2^e to IEEE double precision -static double make_double(uint64_t f, int e) +double make_double(uint64_t f, int e) { constexpr uint64_t kHiddenBit = 0x0010000000000000; constexpr uint64_t kSignificandMask = 0x000FFFFFFFFFFFFF; @@ -141,7 +141,7 @@ static double make_double(uint64_t f, int e) uint64_t bits = (f & kSignificandMask) | (biased_exponent << kPhysicalSignificandSize); return reinterpret_bits(bits); } -} +} // namespace TEST_CASE("digit gen") { @@ -153,12 +153,12 @@ TEST_CASE("digit gen") CAPTURE(digits) CAPTURE(expected_exponent) - char buf[32]; + std::array buf{}; int len = 0; int exponent = 0; - nlohmann::detail::dtoa_impl::grisu2(buf, len, exponent, number); + nlohmann::detail::dtoa_impl::grisu2(buf.data(), len, exponent, number); - CHECK(digits == std::string(buf, buf + len)); + CHECK(digits == std::string(buf.data(), buf.data() + len)); CHECK(expected_exponent == exponent); }; @@ -217,12 +217,12 @@ TEST_CASE("digit gen") CAPTURE(digits) CAPTURE(expected_exponent) - char buf[32]; + std::array buf{}; int len = 0; int exponent = 0; - nlohmann::detail::dtoa_impl::grisu2(buf, len, exponent, number); + nlohmann::detail::dtoa_impl::grisu2(buf.data(), len, exponent, number); - CHECK(digits == std::string(buf, buf + len)); + CHECK(digits == std::string(buf.data(), buf.data() + len)); CHECK(expected_exponent == exponent); }; @@ -360,7 +360,7 @@ TEST_CASE("formatting") auto check_float = [](float number, const std::string & expected) { std::array buf{}; - char* end = nlohmann::detail::to_chars(buf.data(), buf.data() + 32, number); + char* end = nlohmann::detail::to_chars(buf.data(), buf.data() + 32, number); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) std::string actual(buf.data(), end); CHECK(actual == expected); @@ -420,7 +420,7 @@ TEST_CASE("formatting") auto check_double = [](double number, const std::string & expected) { std::array buf{}; - char* end = nlohmann::detail::to_chars(buf.data(), buf.data() + 32, number); + char* end = nlohmann::detail::to_chars(buf.data(), buf.data() + 32, number); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) std::string actual(buf.data(), end); CHECK(actual == expected); diff --git a/test/src/unit-ubjson.cpp b/test/src/unit-ubjson.cpp index f60477a825..0e88371693 100644 --- a/test/src/unit-ubjson.cpp +++ b/test/src/unit-ubjson.cpp @@ -51,42 +51,42 @@ class SaxCountdown return events_left-- > 0; } - bool boolean(bool) + bool boolean(bool /*unused*/) { return events_left-- > 0; } - bool number_integer(json::number_integer_t) + bool number_integer(json::number_integer_t /*unused*/) { return events_left-- > 0; } - bool number_unsigned(json::number_unsigned_t) + bool number_unsigned(json::number_unsigned_t /*unused*/) { return events_left-- > 0; } - bool number_float(json::number_float_t, const std::string&) + bool number_float(json::number_float_t /*unused*/, const std::string& /*unused*/) { return events_left-- > 0; } - bool string(std::string&) + bool string(std::string& /*unused*/) { return events_left-- > 0; } - bool binary(std::vector&) + bool binary(std::vector& /*unused*/) { return events_left-- > 0; } - bool start_object(std::size_t) + bool start_object(std::size_t /*unused*/) { return events_left-- > 0; } - bool key(std::string&) + bool key(std::string& /*unused*/) { return events_left-- > 0; } @@ -96,7 +96,7 @@ class SaxCountdown return events_left-- > 0; } - bool start_array(std::size_t) + bool start_array(std::size_t /*unused*/) { return events_left-- > 0; } @@ -106,7 +106,7 @@ class SaxCountdown return events_left-- > 0; } - bool parse_error(std::size_t, const std::string&, const json::exception&) + bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) // NOLINT(readability-convert-member-functions-to-static) { return false; } @@ -114,7 +114,7 @@ class SaxCountdown private: int events_left = 0; }; -} +} // namespace TEST_CASE("UBJSON") { @@ -175,16 +175,16 @@ TEST_CASE("UBJSON") { std::vector numbers; numbers.push_back((std::numeric_limits::min)()); - numbers.push_back(-1000000000000000000ll); - numbers.push_back(-100000000000000000ll); - numbers.push_back(-10000000000000000ll); - numbers.push_back(-1000000000000000ll); - numbers.push_back(-100000000000000ll); - numbers.push_back(-10000000000000ll); - numbers.push_back(-1000000000000ll); - numbers.push_back(-100000000000ll); - numbers.push_back(-10000000000ll); - numbers.push_back(-2147483649ll); + numbers.push_back(-1000000000000000000LL); + numbers.push_back(-100000000000000000LL); + numbers.push_back(-10000000000000000LL); + numbers.push_back(-1000000000000000LL); + numbers.push_back(-100000000000000LL); + numbers.push_back(-10000000000000LL); + numbers.push_back(-1000000000000LL); + numbers.push_back(-100000000000LL); + numbers.push_back(-10000000000LL); + numbers.push_back(-2147483649LL); for (auto i : numbers) { CAPTURE(i) @@ -302,7 +302,7 @@ TEST_CASE("UBJSON") // check individual bytes CHECK(result[0] == 'I'); - int16_t restored = static_cast(((result[1] << 8) + result[2])); + auto restored = static_cast(((result[1] << 8) + result[2])); CHECK(restored == i); // roundtrip @@ -323,7 +323,7 @@ TEST_CASE("UBJSON") // check individual bytes CHECK(result[0] == 'I'); - int16_t restored = static_cast(((result[1] << 8) + result[2])); + auto restored = static_cast(((result[1] << 8) + result[2])); CHECK(restored == -9263); // roundtrip @@ -455,7 +455,7 @@ TEST_CASE("UBJSON") // check individual bytes CHECK(result[0] == 'I'); - uint16_t restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); + auto restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); CHECK(restored == i); // roundtrip @@ -583,7 +583,7 @@ TEST_CASE("UBJSON") // check individual bytes CHECK(result[0] == 'i'); - uint8_t restored = static_cast(result[1]); + auto restored = static_cast(result[1]); CHECK(restored == i); // roundtrip @@ -616,7 +616,7 @@ TEST_CASE("UBJSON") // check individual bytes CHECK(result[0] == 'U'); - uint8_t restored = static_cast(result[1]); + auto restored = static_cast(result[1]); CHECK(restored == i); // roundtrip @@ -650,7 +650,7 @@ TEST_CASE("UBJSON") // check individual bytes CHECK(result[0] == 'I'); - uint16_t restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); + auto restored = static_cast(static_cast(result[1]) * 256 + static_cast(result[2])); CHECK(restored == i); // roundtrip @@ -1535,7 +1535,7 @@ TEST_CASE("UBJSON") { SECTION("size=false type=false") { - json j = json::parse("{\"a\": {\"b\": {\"c\": {}}}}"); + json j = json::parse(R"({"a": {"b": {"c": {}}}})"); std::vector expected = { '{', 'i', 1, 'a', '{', 'i', 1, 'b', '{', 'i', 1, 'c', '{', '}', '}', '}', '}' @@ -1550,7 +1550,7 @@ TEST_CASE("UBJSON") SECTION("size=true type=false") { - json j = json::parse("{\"a\": {\"b\": {\"c\": {}}}}"); + json j = json::parse(R"({"a": {"b": {"c": {}}}})"); std::vector expected = { '{', '#', 'i', 1, 'i', 1, 'a', '{', '#', 'i', 1, 'i', 1, 'b', '{', '#', 'i', 1, 'i', 1, 'c', '{', '#', 'i', 0 @@ -1565,7 +1565,7 @@ TEST_CASE("UBJSON") SECTION("size=true type=true") { - json j = json::parse("{\"a\": {\"b\": {\"c\": {}}}}"); + json j = json::parse(R"({"a": {"b": {"c": {}}}})"); std::vector expected = { '{', '$', '{', '#', 'i', 1, 'i', 1, 'a', '$', '{', '#', 'i', 1, 'i', 1, 'b', '$', '{', '#', 'i', 1, 'i', 1, 'c', '#', 'i', 0 @@ -1610,7 +1610,7 @@ TEST_CASE("UBJSON") CHECK_THROWS_AS(_ = json::from_ubjson(v_ubjson), json::out_of_range&); json j; - nlohmann::detail::json_sax_dom_callback_parser scp(j, [](int, json::parse_event_t, const json&) + nlohmann::detail::json_sax_dom_callback_parser scp(j, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) { return true; }); @@ -1624,7 +1624,7 @@ TEST_CASE("UBJSON") CHECK_THROWS_AS(_ = json::from_ubjson(v_ubjson), json::out_of_range&); json j; - nlohmann::detail::json_sax_dom_callback_parser scp(j, [](int, json::parse_event_t, const json&) + nlohmann::detail::json_sax_dom_callback_parser scp(j, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) { return true; }); @@ -2439,7 +2439,7 @@ TEST_CASE("all UBJSON first bytes") // check that parse_error.112 is only thrown if the // first byte is not in the supported set INFO_WITH_TEMP(e.what()); - if (std::find(supported.begin(), supported.end(), byte) == supported.end()) + if (supported.find(byte) == supported.end()) { CHECK(e.id == 112); } diff --git a/test/src/unit-udt.cpp b/test/src/unit-udt.cpp index b237655056..2bebd8f599 100644 --- a/test/src/unit-udt.cpp +++ b/test/src/unit-udt.cpp @@ -32,10 +32,11 @@ SOFTWARE. #include using nlohmann::json; -#include #include +#include #include #include +#include namespace udt { @@ -55,40 +56,40 @@ struct age struct name { std::string m_val; - name(const std::string rhs = "") : m_val(rhs) {} + name(std::string rhs = "") : m_val(std::move(rhs)) {} }; struct address { std::string m_val; - address(const std::string rhs = "") : m_val(rhs) {} + address(std::string rhs = "") : m_val(std::move(rhs)) {} }; struct person { - age m_age; - name m_name; - country m_country; - person() : m_age(), m_name(), m_country() {} - person(const age& a, const name& n, const country& c) : m_age(a), m_name(n), m_country(c) {} + age m_age{}; + name m_name{}; + country m_country{}; + person() = default; + person(const age& a, name n, const country& c) : m_age(a), m_name(std::move(n)), m_country(c) {} }; struct contact { - person m_person; - address m_address; - contact() : m_person(), m_address() {} - contact(const person& p, const address& a) : m_person(p), m_address(a) {} + person m_person{}; + address m_address{}; + contact() = default; + contact(person p, address a) : m_person(std::move(p)), m_address(std::move(a)) {} }; struct contact_book { - name m_book_name; - std::vector m_contacts; - contact_book() : m_book_name(), m_contacts() {} - contact_book(const name& n, const std::vector& c) : m_book_name(n), m_contacts(c) {} + name m_book_name{}; + std::vector m_contacts{}; + contact_book() = default; + contact_book(name n, std::vector c) : m_book_name(std::move(n)), m_contacts(std::move(c)) {} }; -} +} // namespace udt // to_json methods namespace udt @@ -178,7 +179,7 @@ static bool operator==(const contact_book& lhs, const contact_book& rhs) return std::tie(lhs.m_book_name, lhs.m_contacts) == std::tie(rhs.m_book_name, rhs.m_contacts); } -} +} // namespace udt // from_json methods namespace udt @@ -199,7 +200,7 @@ template static void from_json(const BasicJsonType& j, country& c) { const auto str = j.template get(); - static const std::map m = + const std::map m = { {"中华人民共和国", country::china}, {"France", country::france}, @@ -207,7 +208,7 @@ static void from_json(const BasicJsonType& j, country& c) }; const auto it = m.find(str); - // TODO test exceptions + // TODO(nlohmann) test exceptions c = it->second; } @@ -235,7 +236,7 @@ static void from_json(const nlohmann::json& j, contact_book& cb) cb.m_book_name = j["name"].get(); cb.m_contacts = j["contacts"].get>(); } -} +} // namespace udt TEST_CASE("basic usage" * doctest::test_suite("udt")) { @@ -344,11 +345,11 @@ namespace udt { struct legacy_type { - std::string number; - legacy_type() : number() {} - legacy_type(const std::string& n) : number(n) {} + std::string number{}; + legacy_type() = default; + legacy_type(std::string n) : number(std::move(n)) {} }; -} +} // namespace udt namespace nlohmann { @@ -375,7 +376,7 @@ struct adl_serializer> } else { - opt.reset(new T(j.get())); + opt.reset(new T(j.get())); // NOLINT(cppcoreguidelines-owning-memory) } } }; @@ -393,7 +394,7 @@ struct adl_serializer l.number = std::to_string(j.get()); } }; -} +} // namespace nlohmann TEST_CASE("adl_serializer specialization" * doctest::test_suite("udt")) { @@ -406,7 +407,7 @@ TEST_CASE("adl_serializer specialization" * doctest::test_suite("udt")) json j = optPerson; CHECK(j.is_null()); - optPerson.reset(new udt::person{{42}, {"John Doe"}, udt::country::russia}); + optPerson.reset(new udt::person{{42}, {"John Doe"}, udt::country::russia}); // NOLINT(cppcoreguidelines-owning-memory,modernize-make-shared) j = optPerson; CHECK_FALSE(j.is_null()); @@ -453,23 +454,23 @@ template <> struct adl_serializer> { using type = std::vector; - static void to_json(json& j, const type&) + static void to_json(json& j, const type& /*type*/) { j = "hijacked!"; } - static void from_json(const json&, type& opt) + static void from_json(const json& /*unnamed*/, type& opt) { opt = {42.0, 42.0, 42.0}; } // preferred version - static type from_json(const json&) + static type from_json(const json& /*unnamed*/) { return {4.0, 5.0, 6.0}; } }; -} +} // namespace nlohmann TEST_CASE("even supported types can be specialized" * doctest::test_suite("udt")) { @@ -504,13 +505,11 @@ struct adl_serializer> { return nullptr; } - else - { - return std::unique_ptr(new T(j.get())); - } + + return std::unique_ptr(new T(j.get())); } }; -} +} // namespace nlohmann TEST_CASE("Non-copyable types" * doctest::test_suite("udt")) { @@ -521,7 +520,7 @@ TEST_CASE("Non-copyable types" * doctest::test_suite("udt")) json j = optPerson; CHECK(j.is_null()); - optPerson.reset(new udt::person{{42}, {"John Doe"}, udt::country::russia}); + optPerson.reset(new udt::person{{42}, {"John Doe"}, udt::country::russia}); // NOLINT(cppcoreguidelines-owning-memory,modernize-make-unique) j = optPerson; CHECK_FALSE(j.is_null()); @@ -566,8 +565,8 @@ struct pod_serializer std::is_pod::value && std::is_class::value, int >::type = 0 > static void from_json(const BasicJsonType& j, U& t) { - std::uint64_t value; - // TODO The following block is no longer relevant in this serializer, make another one that shows the issue + std::uint64_t value = 0; + // The following block is no longer relevant in this serializer, make another one that shows the issue // the problem arises only when one from_json method is defined without any constraint // // Why cannot we simply use: j.get() ? @@ -582,7 +581,7 @@ struct pod_serializer // calling get calls from_json, for now, we cannot do this in custom // serializers nlohmann::from_json(j, value); - auto bytes = static_cast(static_cast(&value)); + auto* bytes = static_cast(static_cast(&value)); std::memcpy(&t, bytes, sizeof(value)); } @@ -601,8 +600,8 @@ struct pod_serializer std::is_pod::value && std::is_class::value, int >::type = 0 > static void to_json(BasicJsonType& j, const T& t) noexcept { - auto bytes = static_cast< const unsigned char*>(static_cast(&t)); - std::uint64_t value; + const auto* bytes = static_cast< const unsigned char*>(static_cast(&t)); + std::uint64_t value = 0; std::memcpy(&value, bytes, sizeof(value)); nlohmann::to_json(j, value); } @@ -619,9 +618,9 @@ struct small_pod struct non_pod { - std::string s; - non_pod() : s() {} - non_pod(const std::string& S) : s(S) {} + std::string s{}; + non_pod() = default; + non_pod(std::string S) : s(std::move(S)) {} }; template @@ -651,7 +650,7 @@ static std::ostream& operator<<(std::ostream& os, small_pod l) { return os << "begin: " << l.begin << ", middle: " << l.middle << ", end: " << l.end; } -} +} // namespace udt TEST_CASE("custom serializer for pods" * doctest::test_suite("udt")) { @@ -803,7 +802,7 @@ struct is_constructible_patched : std::false_type {}; template struct is_constructible_patched())))> : std::true_type {}; -} +} // namespace TEST_CASE("an incomplete type does not trigger a compiler error in non-evaluated context" * doctest::test_suite("udt")) { @@ -822,8 +821,8 @@ class Evil int m_i = 0; }; -void from_json(const json&, Evil&) {} -} +void from_json(const json& /*unused*/, Evil& /*unused*/) {} +} // namespace TEST_CASE("Issue #924") { diff --git a/test/src/unit-udt_macro.cpp b/test/src/unit-udt_macro.cpp index a13ac006b3..0235981981 100644 --- a/test/src/unit-udt_macro.cpp +++ b/test/src/unit-udt_macro.cpp @@ -39,7 +39,7 @@ namespace persons class person_with_private_data { private: - std::string name = ""; + std::string name{}; int age = 0; json metadata = nullptr; @@ -62,7 +62,7 @@ class person_with_private_data class person_without_private_data_1 { public: - std::string name = ""; + std::string name{}; int age = 0; json metadata = nullptr; @@ -84,7 +84,7 @@ class person_without_private_data_1 class person_without_private_data_2 { public: - std::string name = ""; + std::string name{}; int age = 0; json metadata = nullptr; diff --git a/test/src/unit-unicode.cpp b/test/src/unit-unicode.cpp index 654c48c2a3..b45e990a7b 100644 --- a/test/src/unit-unicode.cpp +++ b/test/src/unit-unicode.cpp @@ -168,7 +168,7 @@ void check_utf8string(bool success_expected, int byte1, int byte2 = -1, int byte CHECK_THROWS_AS(_ = json::parse(json_string), json::parse_error&); } } -} +} // namespace TEST_CASE("Unicode" * doctest::skip()) { @@ -1159,7 +1159,7 @@ TEST_CASE("Unicode" * doctest::skip()) SECTION("check JSON Pointers") { - for (auto s : j) + for (const auto& s : j) { // skip non-string JSON values if (!s.is_string()) @@ -1176,7 +1176,7 @@ TEST_CASE("Unicode" * doctest::skip()) } // JSON Pointers must begin with "/" - ptr = "/" + ptr; + ptr.insert(0, "/"); CHECK_NOTHROW(json::json_pointer("/" + ptr)); @@ -1256,7 +1256,7 @@ void roundtrip(bool success_expected, const std::string& s) CHECK_THROWS_AS(_ = json::parse(ps), json::parse_error&); } } -} +} // namespace TEST_CASE("Markus Kuhn's UTF-8 decoder capability and stress test") { diff --git a/test/src/unit-user_defined_input.cpp b/test/src/unit-user_defined_input.cpp index 4b84e8e71c..689f450b78 100644 --- a/test/src/unit-user_defined_input.cpp +++ b/test/src/unit-user_defined_input.cpp @@ -60,7 +60,7 @@ const char* begin(const MyContainer& c) const char* end(const MyContainer& c) { - return c.data + strlen(c.data); + return c.data + strlen(c.data); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) } TEST_CASE("Custom container non-member begin/end") @@ -88,7 +88,7 @@ TEST_CASE("Custom container member begin/end") const char* end() const { - return data + strlen(data); + return data + strlen(data); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) } }; @@ -114,7 +114,7 @@ TEST_CASE("Custom iterator") MyIterator& operator++() { - ++ptr; + ++ptr; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) return *this; } @@ -131,8 +131,15 @@ TEST_CASE("Custom iterator") const char* ptr; }; + // avoid -Wunused-local-typedefs + CHECK(std::is_same::value); + CHECK(std::is_same::value); + CHECK(std::is_same::value); + CHECK(std::is_same::value); + CHECK(std::is_same::value); + MyIterator begin{raw_data}; - MyIterator end{raw_data + strlen(raw_data)}; + MyIterator end{raw_data + strlen(raw_data)}; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) json as_json = json::parse(begin, end); CHECK(as_json.at(0) == 1); diff --git a/test/src/unit-wstring.cpp b/test/src/unit-wstring.cpp index a5b80be742..b8ee2eaeb2 100644 --- a/test/src/unit-wstring.cpp +++ b/test/src/unit-wstring.cpp @@ -51,7 +51,7 @@ bool u32string_is_utf32() { return (std::u32string(U"💩") == std::u32string(U"\U0001F4A9")); } -} +} // namespace TEST_CASE("wide strings") { diff --git a/test/thirdparty/doctest/doctest.h b/test/thirdparty/doctest/doctest.h index ae9c4d4109..7712dd6b63 100644 --- a/test/thirdparty/doctest/doctest.h +++ b/test/thirdparty/doctest/doctest.h @@ -48,8 +48,8 @@ #define DOCTEST_VERSION_MAJOR 2 #define DOCTEST_VERSION_MINOR 4 -#define DOCTEST_VERSION_PATCH 3 -#define DOCTEST_VERSION_STR "2.4.3" +#define DOCTEST_VERSION_PATCH 4 +#define DOCTEST_VERSION_STR "2.4.4" #define DOCTEST_VERSION \ (DOCTEST_VERSION_MAJOR * 10000 + DOCTEST_VERSION_MINOR * 100 + DOCTEST_VERSION_PATCH) @@ -3718,6 +3718,7 @@ namespace detail { } bool TestCase::operator<(const TestCase& other) const { + // this will be used only to differentiate between test cases - not relevant for sorting if(m_line != other.m_line) return m_line < other.m_line; const int file_cmp = m_file.compare(other.m_file); @@ -4043,15 +4044,29 @@ namespace { struct FatalConditionHandler { static LONG CALLBACK handleException(PEXCEPTION_POINTERS ExceptionInfo) { - for(size_t i = 0; i < DOCTEST_COUNTOF(signalDefs); ++i) { - if(ExceptionInfo->ExceptionRecord->ExceptionCode == signalDefs[i].id) { - reportFatal(signalDefs[i].name); - break; + // Multiple threads may enter this filter/handler at once. We want the error message to be printed on the + // console just once no matter how many threads have crashed. + static std::mutex mutex; + static bool execute = true; + { + std::lock_guard lock(mutex); + if(execute) { + bool reported = false; + for(size_t i = 0; i < DOCTEST_COUNTOF(signalDefs); ++i) { + if(ExceptionInfo->ExceptionRecord->ExceptionCode == signalDefs[i].id) { + reportFatal(signalDefs[i].name); + reported = true; + break; + } + } + if(reported == false) + reportFatal("Unhandled SEH exception caught"); + if(isDebuggerActive() && !g_cs->no_breaks) + DOCTEST_BREAK_INTO_DEBUGGER(); } + execute = false; } - // If its not an exception we care about, pass it along. - // This stops us from eating debugger breaks etc. - return EXCEPTION_CONTINUE_SEARCH; + std::exit(EXIT_FAILURE); } FatalConditionHandler() { @@ -4073,6 +4088,8 @@ namespace { original_terminate_handler = std::get_terminate(); std::set_terminate([]() noexcept { reportFatal("Terminate handler called"); + if(isDebuggerActive() && !g_cs->no_breaks) + DOCTEST_BREAK_INTO_DEBUGGER(); std::exit(EXIT_FAILURE); // explicitly exit - otherwise the SIGABRT handler may be called as well }); @@ -4083,8 +4100,29 @@ namespace { prev_sigabrt_handler = std::signal(SIGABRT, [](int signal) noexcept { if(signal == SIGABRT) { reportFatal("SIGABRT - Abort (abnormal termination) signal"); + if(isDebuggerActive() && !g_cs->no_breaks) + DOCTEST_BREAK_INTO_DEBUGGER(); + std::exit(EXIT_FAILURE); } }); + + // The following settings are taken from google test, and more + // specifically from UnitTest::Run() inside of gtest.cc + + // the user does not want to see pop-up dialogs about crashes + prev_error_mode_1 = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | + SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX); + // This forces the abort message to go to stderr in all circumstances. + prev_error_mode_2 = _set_error_mode(_OUT_TO_STDERR); + // In the debug version, Visual Studio pops up a separate dialog + // offering a choice to debug the aborted program - we want to disable that. + prev_abort_behavior = _set_abort_behavior(0x0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); + // In debug mode, the Windows CRT can crash with an assertion over invalid + // input (e.g. passing an invalid file descriptor). The default handling + // for these assertions is to pop up a dialog and wait for user input. + // Instead ask the CRT to dump such assertions to stderr non-interactively. + prev_report_mode = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); + prev_report_file = _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); } static void reset() { @@ -4092,16 +4130,25 @@ namespace { // Unregister handler and restore the old guarantee SetUnhandledExceptionFilter(previousTop); SetThreadStackGuarantee(&guaranteeSize); - previousTop = nullptr; - isSet = false; std::set_terminate(original_terminate_handler); std::signal(SIGABRT, prev_sigabrt_handler); + SetErrorMode(prev_error_mode_1); + _set_error_mode(prev_error_mode_2); + _set_abort_behavior(prev_abort_behavior, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); + _CrtSetReportMode(_CRT_ASSERT, prev_report_mode); + _CrtSetReportFile(_CRT_ASSERT, prev_report_file); + isSet = false; } } ~FatalConditionHandler() { reset(); } private: + static UINT prev_error_mode_1; + static int prev_error_mode_2; + static unsigned int prev_abort_behavior; + static int prev_report_mode; + static _HFILE prev_report_file; static void (*prev_sigabrt_handler)(int); static std::terminate_handler original_terminate_handler; static bool isSet; @@ -4109,6 +4156,11 @@ namespace { static LPTOP_LEVEL_EXCEPTION_FILTER previousTop; }; + UINT FatalConditionHandler::prev_error_mode_1; + int FatalConditionHandler::prev_error_mode_2; + unsigned int FatalConditionHandler::prev_abort_behavior; + int FatalConditionHandler::prev_report_mode; + _HFILE FatalConditionHandler::prev_report_file; void (*FatalConditionHandler::prev_sigabrt_handler)(int); std::terminate_handler FatalConditionHandler::original_terminate_handler; bool FatalConditionHandler::isSet = false; @@ -5046,7 +5098,6 @@ namespace { struct JUnitTestCaseData { -DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations") // gmtime static std::string getCurrentTimestamp() { // Beware, this is not reentrant because of backward compatibility issues // Also, UTC only, again because of backward compatibility (%z is C++11) @@ -5054,16 +5105,19 @@ DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations") // gmtime std::time(&rawtime); auto const timeStampSize = sizeof("2017-01-16T17:06:45Z"); - std::tm* timeInfo; - timeInfo = std::gmtime(&rawtime); + std::tm timeInfo; +#ifdef DOCTEST_PLATFORM_WINDOWS + gmtime_s(&timeInfo, &rawtime); +#else // DOCTEST_PLATFORM_WINDOWS + gmtime_r(&rawtime, &timeInfo); +#endif // DOCTEST_PLATFORM_WINDOWS char timeStamp[timeStampSize]; const char* const fmt = "%Y-%m-%dT%H:%M:%SZ"; - std::strftime(timeStamp, timeStampSize, fmt, timeInfo); + std::strftime(timeStamp, timeStampSize, fmt, &timeInfo); return std::string(timeStamp); } -DOCTEST_CLANG_SUPPRESS_WARNING_POP struct JUnitTestMessage { diff --git a/test/thirdparty/fifo_map/fifo_map.hpp b/test/thirdparty/fifo_map/fifo_map.hpp index c281e3be3a..cfa38c97b5 100644 --- a/test/thirdparty/fifo_map/fifo_map.hpp +++ b/test/thirdparty/fifo_map/fifo_map.hpp @@ -99,7 +99,7 @@ template < class T, class Compare = fifo_map_compare, class Allocator = std::allocator> - > class fifo_map + > class fifo_map // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions,-warnings-as-errors) { public: using key_type = Key; @@ -514,10 +514,10 @@ template < internal_map_type m_map; }; -} +} // namespace nlohmann // specialization of std::swap -namespace std +namespace std // NOLINT(cert-dcl58-cpp,-warnings-as-errors) { template inline void swap(nlohmann::fifo_map& m1, @@ -525,6 +525,6 @@ inline void swap(nlohmann::fifo_map& m1, { m1.swap(m2); } -} +} // namespace std #endif From ca9a1f2fd463a0e2d7f1942da17f6ff37cdb36e3 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 24 Mar 2021 12:46:34 +0100 Subject: [PATCH 069/143] :memo: add note to outdated exceptions --- doc/mkdocs/docs/home/exceptions.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/doc/mkdocs/docs/home/exceptions.md b/doc/mkdocs/docs/home/exceptions.md index da68f21495..d5d07362f1 100644 --- a/doc/mkdocs/docs/home/exceptions.md +++ b/doc/mkdocs/docs/home/exceptions.md @@ -188,6 +188,10 @@ JSON uses the `\uxxxx` format to describe Unicode characters. Code points above parse error at 14: missing or wrong low surrogate ``` +!!! note + + This exception is not used any more. Instead [json.exception.parse_error.101](#jsonexceptionparse_error101) with a more detailed description is used. + ### json.exception.parse_error.103 Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid. @@ -198,6 +202,10 @@ Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are inva parse error: code points above 0x10FFFF are invalid ``` +!!! note + + This exception is not used any more. Instead [json.exception.parse_error.101](#jsonexceptionparse_error101) with a more detailed description is used. + ### json.exception.parse_error.104 [RFC 6902](https://tools.ietf.org/html/rfc6902) requires a JSON Patch document to be a JSON document that represents an array of objects. From 322bc99d8ebede17b213029b43814643760b336f Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Wed, 24 Mar 2021 13:03:36 +0100 Subject: [PATCH 070/143] Reran amalgamate. --- single_include/nlohmann/json.hpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 6ced652e14..62223ed28b 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -4000,8 +4000,7 @@ auto from_json(BasicJsonType&& j, identity_tag> tag) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } return from_json_inplace_array_impl(std::forward(j), tag, make_index_sequence {}); @@ -4119,8 +4118,7 @@ auto from_json(BasicJsonType&& j, TupleRelated&& t) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); } return from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<3> {}); From 41dbd50313b7573190832acace516c6416f72d9b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 24 Mar 2021 13:27:00 +0100 Subject: [PATCH 071/143] :page_facing_up: use code from Google Abseil --- README.md | 2 + include/nlohmann/detail/meta/cpp_future.hpp | 110 +++++++++++++++++--- single_include/nlohmann/json.hpp | 110 +++++++++++++++++--- 3 files changed, 188 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index d428e4c473..6d0153c091 100644 --- a/README.md +++ b/README.md @@ -1284,6 +1284,8 @@ The class contains a slightly modified version of the Grisu2 algorithm from Flor The class contains a copy of [Hedley](https://nemequ.github.io/hedley/) from Evan Nemerson which is licensed as [CC0-1.0](https://creativecommons.org/publicdomain/zero/1.0/). +The class contains parts of [Google Abseil](https://github.com/abseil/abseil-cpp) which is licensed under the [Apache 2.0 License](https://opensource.org/licenses/Apache-2.0). + ## Contact If you have questions regarding the library, I would like to invite you to [open an issue at GitHub](https://github.com/nlohmann/json/issues/new/choose). Please describe your request, problem, or question as detailed as possible, and also mention the version of the library you are using as well as the version of your compiler and operating system. Opening an issue at GitHub allows other users and contributors to this library to collaborate. For instance, I have little experience with MSVC, and most issues in this regard have been solved by a growing community. If you have a look at the [closed issues](https://github.com/nlohmann/json/issues?q=is%3Aissue+is%3Aclosed), you will see that we react quite timely in most cases. diff --git a/include/nlohmann/detail/meta/cpp_future.hpp b/include/nlohmann/detail/meta/cpp_future.hpp index 4ba1a55714..e24518fafe 100644 --- a/include/nlohmann/detail/meta/cpp_future.hpp +++ b/include/nlohmann/detail/meta/cpp_future.hpp @@ -28,36 +28,112 @@ using std::index_sequence_for; template using enable_if_t = typename std::enable_if::type; -// source: https://stackoverflow.com/a/32223343 -template -struct index_sequence +// The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h +// which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0. + +//// START OF CODE FROM GOOGLE ABSEIL + +// integer_sequence +// +// Class template representing a compile-time integer sequence. An instantiation +// of `integer_sequence` has a sequence of integers encoded in its +// type through its template arguments (which is a common need when +// working with C++11 variadic templates). `absl::integer_sequence` is designed +// to be a drop-in replacement for C++14's `std::integer_sequence`. +// +// Example: +// +// template< class T, T... Ints > +// void user_function(integer_sequence); +// +// int main() +// { +// // user_function's `T` will be deduced to `int` and `Ints...` +// // will be deduced to `0, 1, 2, 3, 4`. +// user_function(make_integer_sequence()); +// } +template +struct integer_sequence { - using type = index_sequence; - using value_type = std::size_t; + using value_type = T; static constexpr std::size_t size() noexcept { return sizeof...(Ints); } }; -template -struct merge_and_renumber; +// index_sequence +// +// A helper template for an `integer_sequence` of `size_t`, +// `absl::index_sequence` is designed to be a drop-in replacement for C++14's +// `std::index_sequence`. +template +using index_sequence = integer_sequence; -template -struct merge_and_renumber, index_sequence> - : index_sequence < I1..., (sizeof...(I1) + I2)... > {}; +namespace utility_internal +{ -template -struct make_index_sequence - : merge_and_renumber < typename make_index_sequence < N / 2 >::type, - typename make_index_sequence < N - N / 2 >::type > {}; +template +struct Extend; -template<> struct make_index_sequence<0> : index_sequence<> {}; -template<> struct make_index_sequence<1> : index_sequence<0> {}; +// Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency. +template +struct Extend, SeqSize, 0> +{ + using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >; +}; -template +template +struct Extend, SeqSize, 1> +{ + using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >; +}; + +// Recursion helper for 'make_integer_sequence'. +// 'Gen::type' is an alias for 'integer_sequence'. +template +struct Gen +{ + using type = + typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type; +}; + +template +struct Gen +{ + using type = integer_sequence; +}; + +} // namespace utility_internal + +// Compile-time sequences of integers + +// make_integer_sequence +// +// This template alias is equivalent to +// `integer_sequence`, and is designed to be a drop-in +// replacement for C++14's `std::make_integer_sequence`. +template +using make_integer_sequence = typename utility_internal::Gen::type; + +// make_index_sequence +// +// This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`, +// and is designed to be a drop-in replacement for C++14's +// `std::make_index_sequence`. +template +using make_index_sequence = make_integer_sequence; + +// index_sequence_for +// +// Converts a typename pack into an index sequence of the same length, and +// is designed to be a drop-in replacement for C++14's +// `std::index_sequence_for()` +template using index_sequence_for = make_index_sequence; +//// END OF CODE FROM GOOGLE ABSEIL + #endif // dispatch utility (taken from ranges-v3) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 282f33291f..4493dd6542 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3041,36 +3041,112 @@ using std::index_sequence_for; template using enable_if_t = typename std::enable_if::type; -// source: https://stackoverflow.com/a/32223343 -template -struct index_sequence +// The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h +// which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0. + +//// START OF CODE FROM GOOGLE ABSEIL + +// integer_sequence +// +// Class template representing a compile-time integer sequence. An instantiation +// of `integer_sequence` has a sequence of integers encoded in its +// type through its template arguments (which is a common need when +// working with C++11 variadic templates). `absl::integer_sequence` is designed +// to be a drop-in replacement for C++14's `std::integer_sequence`. +// +// Example: +// +// template< class T, T... Ints > +// void user_function(integer_sequence); +// +// int main() +// { +// // user_function's `T` will be deduced to `int` and `Ints...` +// // will be deduced to `0, 1, 2, 3, 4`. +// user_function(make_integer_sequence()); +// } +template +struct integer_sequence { - using type = index_sequence; - using value_type = std::size_t; + using value_type = T; static constexpr std::size_t size() noexcept { return sizeof...(Ints); } }; -template -struct merge_and_renumber; +// index_sequence +// +// A helper template for an `integer_sequence` of `size_t`, +// `absl::index_sequence` is designed to be a drop-in replacement for C++14's +// `std::index_sequence`. +template +using index_sequence = integer_sequence; + +namespace utility_internal +{ + +template +struct Extend; + +// Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency. +template +struct Extend, SeqSize, 0> +{ + using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >; +}; + +template +struct Extend, SeqSize, 1> +{ + using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >; +}; + +// Recursion helper for 'make_integer_sequence'. +// 'Gen::type' is an alias for 'integer_sequence'. +template +struct Gen +{ + using type = + typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type; +}; + +template +struct Gen +{ + using type = integer_sequence; +}; -template -struct merge_and_renumber, index_sequence> - : index_sequence < I1..., (sizeof...(I1) + I2)... > {}; +} // namespace utility_internal -template -struct make_index_sequence - : merge_and_renumber < typename make_index_sequence < N / 2 >::type, - typename make_index_sequence < N - N / 2 >::type > {}; +// Compile-time sequences of integers -template<> struct make_index_sequence<0> : index_sequence<> {}; -template<> struct make_index_sequence<1> : index_sequence<0> {}; +// make_integer_sequence +// +// This template alias is equivalent to +// `integer_sequence`, and is designed to be a drop-in +// replacement for C++14's `std::make_integer_sequence`. +template +using make_integer_sequence = typename utility_internal::Gen::type; -template +// make_index_sequence +// +// This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`, +// and is designed to be a drop-in replacement for C++14's +// `std::make_index_sequence`. +template +using make_index_sequence = make_integer_sequence; + +// index_sequence_for +// +// Converts a typename pack into an index sequence of the same length, and +// is designed to be a drop-in replacement for C++14's +// `std::index_sequence_for()` +template using index_sequence_for = make_index_sequence; +//// END OF CODE FROM GOOGLE ABSEIL + #endif // dispatch utility (taken from ranges-v3) From 8e79917d3244d75a7bbe1b3586aa157eaf9f9198 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Wed, 24 Mar 2021 13:27:59 +0100 Subject: [PATCH 072/143] Fix clang-tidy complaints. --- test/src/unit-regression2.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 416ca16c59..f9873f5f4a 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -140,7 +140,7 @@ struct NotSerializableData struct NonDefaultConstructible { - explicit NonDefaultConstructible (int x) : x(x) { } + explicit NonDefaultConstructible (int a) : x(a) { } int x; }; @@ -154,7 +154,7 @@ struct adl_serializer return NonDefaultConstructible(j.get()); } }; -} +} // namespace nlohmann TEST_CASE("regression tests 2") From 9d5735117528b6935da62dd29aa57dc811b6f823 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 24 Mar 2021 20:27:12 +0100 Subject: [PATCH 073/143] :bug: set correct value for JSON_ImplicitConversions in ci_test_noimplicitconversions --- cmake/ci.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/ci.cmake b/cmake/ci.cmake index 5085c369dd..4e0d74b12e 100644 --- a/cmake/ci.cmake +++ b/cmake/ci.cmake @@ -441,7 +441,7 @@ add_custom_target(ci_test_noexceptions add_custom_target(ci_test_noimplicitconversions COMMAND CXX=${CLANG_TOOL} ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug -GNinja - -DJSON_BuildTests=ON -DJSON_MultipleHeaders=ON -DJSON_ImplicitConversions=ON + -DJSON_BuildTests=ON -DJSON_MultipleHeaders=ON -DJSON_ImplicitConversions=OFF -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_noimplicitconversions COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_noimplicitconversions COMMAND cd ${PROJECT_BINARY_DIR}/build_noimplicitconversions && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure From 2b865131d88dd37f5aa64944673fde9a344df19c Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Wed, 24 Mar 2021 23:33:21 +0100 Subject: [PATCH 074/143] Fixing CI errors. --- include/nlohmann/detail/conversions/from_json.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- test/src/unit-regression2.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index c661dd66ee..4e4efd0a66 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -258,7 +258,7 @@ template < typename BasicJsonType, typename T, std::size_t... Idx > std::array from_json_inplace_array_impl(BasicJsonType&& j, identity_tag> /*unused*/, index_sequence /*unused*/) { - return { std::forward(j).at(Idx).template get()... }; + return { { std::forward(j).at(Idx).template get()... } }; } template < typename BasicJsonType, typename T, std::size_t N > diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 62223ed28b..6fde2fd8c5 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3991,7 +3991,7 @@ template < typename BasicJsonType, typename T, std::size_t... Idx > std::array from_json_inplace_array_impl(BasicJsonType&& j, identity_tag> /*unused*/, index_sequence /*unused*/) { - return { std::forward(j).at(Idx).template get()... }; + return { { std::forward(j).at(Idx).template get()... } }; } template < typename BasicJsonType, typename T, std::size_t N > diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index f9873f5f4a..4d48e4765b 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -149,7 +149,7 @@ namespace nlohmann template <> struct adl_serializer { - static NonDefaultConstructible from_json (json const& j) noexcept + static NonDefaultConstructible from_json (json const& j) { return NonDefaultConstructible(j.get()); } From ef826b6fba5522e087c97870ff36aa547302ff97 Mon Sep 17 00:00:00 2001 From: "Benjamin A. Beasley" Date: Thu, 25 Mar 2021 13:16:04 -0400 Subject: [PATCH 075/143] Update doctest from 2.4.4 to 2.4.6 (fixes #2686) --- test/thirdparty/doctest/LICENSE.txt | 4 +- test/thirdparty/doctest/doctest.h | 586 +++++++++++++++++++--------- 2 files changed, 393 insertions(+), 197 deletions(-) mode change 100755 => 100644 test/thirdparty/doctest/LICENSE.txt diff --git a/test/thirdparty/doctest/LICENSE.txt b/test/thirdparty/doctest/LICENSE.txt old mode 100755 new mode 100644 index 155dbe9428..d67bb64f9d --- a/test/thirdparty/doctest/LICENSE.txt +++ b/test/thirdparty/doctest/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016-2020 Viktor Kirilov +Copyright (c) 2016-2021 Viktor Kirilov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. diff --git a/test/thirdparty/doctest/doctest.h b/test/thirdparty/doctest/doctest.h index 7712dd6b63..42eb039979 100644 --- a/test/thirdparty/doctest/doctest.h +++ b/test/thirdparty/doctest/doctest.h @@ -4,7 +4,7 @@ // // doctest.h - the lightest feature-rich C++ single-header testing framework for unit tests and TDD // -// Copyright (c) 2016-2020 Viktor Kirilov +// Copyright (c) 2016-2021 Viktor Kirilov // // Distributed under the MIT Software License // See accompanying file LICENSE.txt or copy at @@ -48,8 +48,8 @@ #define DOCTEST_VERSION_MAJOR 2 #define DOCTEST_VERSION_MINOR 4 -#define DOCTEST_VERSION_PATCH 4 -#define DOCTEST_VERSION_STR "2.4.4" +#define DOCTEST_VERSION_PATCH 6 +#define DOCTEST_VERSION_STR "2.4.6" #define DOCTEST_VERSION \ (DOCTEST_VERSION_MAJOR * 10000 + DOCTEST_VERSION_MINOR * 100 + DOCTEST_VERSION_PATCH) @@ -354,7 +354,7 @@ DOCTEST_MSVC_SUPPRESS_WARNING(26812) // Prefer 'enum class' over 'enum' #define DOCTEST_GLOBAL_NO_WARNINGS(var) \ DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wglobal-constructors") \ DOCTEST_CLANG_SUPPRESS_WARNING("-Wunused-variable") \ - static int var DOCTEST_UNUSED // NOLINT(fuchsia-statically-constructed-objects,cert-err58-cpp) + static const int var DOCTEST_UNUSED // NOLINT(fuchsia-statically-constructed-objects,cert-err58-cpp) #define DOCTEST_GLOBAL_NO_WARNINGS_END() DOCTEST_CLANG_SUPPRESS_WARNING_POP #ifndef DOCTEST_BREAK_INTO_DEBUGGER @@ -362,16 +362,16 @@ DOCTEST_MSVC_SUPPRESS_WARNING(26812) // Prefer 'enum class' over 'enum' #ifdef DOCTEST_PLATFORM_LINUX #if defined(__GNUC__) && (defined(__i386) || defined(__x86_64)) // Break at the location of the failing check if possible -#define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("int $3\n" : :) +#define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("int $3\n" : :) // NOLINT (hicpp-no-assembler) #else #include #define DOCTEST_BREAK_INTO_DEBUGGER() raise(SIGTRAP) #endif #elif defined(DOCTEST_PLATFORM_MAC) #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(__i386) -#define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("int $3\n" : :) +#define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("int $3\n" : :) // NOLINT (hicpp-no-assembler) #else -#define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("brk #0"); +#define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("brk #0"); // NOLINT (hicpp-no-assembler) #endif #elif DOCTEST_MSVC #define DOCTEST_BREAK_INTO_DEBUGGER() __debugbreak() @@ -656,12 +656,14 @@ DOCTEST_INTERFACE const char* skipPathFromFilename(const char* file); struct DOCTEST_INTERFACE TestCaseData { - String m_file; // the file in which the test was registered + String m_file; // the file in which the test was registered (using String - see #350) unsigned m_line; // the line where the test was registered const char* m_name; // name of the test case const char* m_test_suite; // the test suite in which the test was added const char* m_description; bool m_skip; + bool m_no_breaks; + bool m_no_output; bool m_may_fail; bool m_should_fail; int m_expected_failures; @@ -715,12 +717,18 @@ struct DOCTEST_INTERFACE IContextScope virtual void stringify(std::ostream*) const = 0; }; +namespace detail { + struct DOCTEST_INTERFACE TestCase; +} // namespace detail + struct ContextOptions //!OCLINT too many fields { std::ostream* cout; // stdout stream - std::cout by default std::ostream* cerr; // stderr stream - std::cerr by default String binary_name; // the test binary name + const detail::TestCase* currentTest = nullptr; + // == parameters from the command line String out; // output filename String order_by; // how tests should be ordered @@ -773,6 +781,29 @@ namespace detail { template struct remove_reference { typedef T type; }; template struct remove_reference { typedef T type; }; + template U declval(int); + + template T declval(long); + + template auto declval() DOCTEST_NOEXCEPT -> decltype(declval(0)) ; + + template struct is_lvalue_reference { const static bool value=false; }; + template struct is_lvalue_reference { const static bool value=true; }; + + template + inline T&& forward(typename remove_reference::type& t) DOCTEST_NOEXCEPT + { + return static_cast(t); + } + + template + inline T&& forward(typename remove_reference::type&& t) DOCTEST_NOEXCEPT + { + static_assert(!is_lvalue_reference::value, + "Can not forward an rvalue as an lvalue."); + return static_cast(t); + } + template struct remove_const { typedef T type; }; template struct remove_const { typedef T type; }; #ifdef DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS @@ -1040,10 +1071,20 @@ namespace detail { return toString(lhs) + op + toString(rhs); } +#if DOCTEST_CLANG && DOCTEST_CLANG < DOCTEST_COMPILER(3, 6, 0) +DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wunused-comparison") +#endif + +// This will check if there is any way it could find a operator like member or friend and uses it. +// If not it doesn't find the operator or if the operator at global scope is defined after +// this template, the template won't be instantiated due to SFINAE. Once the template is not +// instantiated it can look for global operator using normal conversions. +#define SFINAE_OP(ret,op) decltype(doctest::detail::declval() op doctest::detail::declval(),static_cast(0)) + #define DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(op, op_str, op_macro) \ template \ - DOCTEST_NOINLINE Result operator op(const DOCTEST_REF_WRAP(R) rhs) { \ - bool res = op_macro(lhs, rhs); \ + DOCTEST_NOINLINE SFINAE_OP(Result,op) operator op(R&& rhs) { \ + bool res = op_macro(doctest::detail::forward(lhs), doctest::detail::forward(rhs)); \ if(m_at & assertType::is_false) \ res = !res; \ if(!res || doctest::getContextOptions()->success) \ @@ -1171,12 +1212,16 @@ namespace detail { L lhs; assertType::Enum m_at; - explicit Expression_lhs(L in, assertType::Enum at) - : lhs(in) + explicit Expression_lhs(L&& in, assertType::Enum at) + : lhs(doctest::detail::forward(in)) , m_at(at) {} DOCTEST_NOINLINE operator Result() { - bool res = !!lhs; +// this is needed only foc MSVC 2015: +// https://ci.appveyor.com/project/onqtam/doctest/builds/38181202 +DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4800) // 'int': forcing value to bool + bool res = static_cast(lhs); +DOCTEST_MSVC_SUPPRESS_WARNING_POP if(m_at & assertType::is_false) //!OCLINT bitwise operator in conditional res = !res; @@ -1185,6 +1230,10 @@ namespace detail { return Result(res); } + /* This is required for user-defined conversions from Expression_lhs to L */ + //operator L() const { return lhs; } + operator L() const { return lhs; } + // clang-format off DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(==, " == ", DOCTEST_CMP_EQ) //!OCLINT bitwise operator in conditional DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(!=, " != ", DOCTEST_CMP_NE) //!OCLINT bitwise operator in conditional @@ -1225,6 +1274,10 @@ namespace detail { #endif // DOCTEST_CONFIG_NO_COMPARISON_WARNING_SUPPRESSION +#if DOCTEST_CLANG && DOCTEST_CLANG < DOCTEST_COMPILER(3, 6, 0) +DOCTEST_CLANG_SUPPRESS_WARNING_POP +#endif + struct DOCTEST_INTERFACE ExpressionDecomposer { assertType::Enum m_at; @@ -1236,8 +1289,8 @@ namespace detail { // https://github.com/catchorg/Catch2/issues/870 // https://github.com/catchorg/Catch2/issues/565 template - Expression_lhs operator<<(const DOCTEST_REF_WRAP(L) operand) { - return Expression_lhs(operand, m_at); + Expression_lhs operator<<(L &&operand) { + return Expression_lhs(doctest::detail::forward(operand), m_at); } }; @@ -1246,6 +1299,8 @@ namespace detail { const char* m_test_suite; const char* m_description; bool m_skip; + bool m_no_breaks; + bool m_no_output; bool m_may_fail; bool m_should_fail; int m_expected_failures; @@ -1524,7 +1579,7 @@ namespace detail { template class ContextScope : public ContextScopeBase { - const L &lambda_; + const L lambda_; public: explicit ContextScope(const L &lambda) : lambda_(lambda) {} @@ -1585,6 +1640,8 @@ namespace detail { DOCTEST_DEFINE_DECORATOR(test_suite, const char*, ""); DOCTEST_DEFINE_DECORATOR(description, const char*, ""); DOCTEST_DEFINE_DECORATOR(skip, bool, true); +DOCTEST_DEFINE_DECORATOR(no_breaks, bool, true); +DOCTEST_DEFINE_DECORATOR(no_output, bool, true); DOCTEST_DEFINE_DECORATOR(timeout, double, 0); DOCTEST_DEFINE_DECORATOR(may_fail, bool, true); DOCTEST_DEFINE_DECORATOR(should_fail, bool, true); @@ -1921,10 +1978,12 @@ int registerReporter(const char* name, int priority, bool isReporter) { static DOCTEST_NOINLINE doctest::detail::TestSuite& getCurrentTestSuite() { \ DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4640) \ DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wexit-time-destructors") \ - static doctest::detail::TestSuite data; \ + DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wmissing-field-initializers") \ + static doctest::detail::TestSuite data{}; \ static bool inited = false; \ DOCTEST_MSVC_SUPPRESS_WARNING_POP \ DOCTEST_CLANG_SUPPRESS_WARNING_POP \ + DOCTEST_GCC_SUPPRESS_WARNING_POP \ if(!inited) { \ data* decorators; \ inited = true; \ @@ -1979,17 +2038,15 @@ int registerReporter(const char* name, int priority, bool isReporter) { // for logging #define DOCTEST_INFO(...) \ DOCTEST_INFO_IMPL(DOCTEST_ANONYMOUS(_DOCTEST_CAPTURE_), DOCTEST_ANONYMOUS(_DOCTEST_CAPTURE_), \ - DOCTEST_ANONYMOUS(_DOCTEST_CAPTURE_), __VA_ARGS__) + __VA_ARGS__) -#define DOCTEST_INFO_IMPL(lambda_name, mb_name, s_name, ...) \ - DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4626) \ - auto lambda_name = [&](std::ostream* s_name) { \ +#define DOCTEST_INFO_IMPL(mb_name, s_name, ...) \ + auto DOCTEST_ANONYMOUS(_DOCTEST_CAPTURE_) = doctest::detail::MakeContextScope( \ + [&](std::ostream* s_name) { \ doctest::detail::MessageBuilder mb_name(__FILE__, __LINE__, doctest::assertType::is_warn); \ mb_name.m_stream = s_name; \ mb_name * __VA_ARGS__; \ - }; \ - DOCTEST_MSVC_SUPPRESS_WARNING_POP \ - auto DOCTEST_ANONYMOUS(_DOCTEST_CAPTURE_) = doctest::detail::MakeContextScope(lambda_name) + }) #define DOCTEST_CAPTURE(x) DOCTEST_INFO(#x " := ", x) @@ -2461,7 +2518,7 @@ int registerReporter(const char* name, int priority, bool isReporter) { #define DOCTEST_FAST_CHECK_UNARY_FALSE DOCTEST_CHECK_UNARY_FALSE #define DOCTEST_FAST_REQUIRE_UNARY_FALSE DOCTEST_REQUIRE_UNARY_FALSE -#define DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE DOCTEST_TEST_CASE_TEMPLATE_INVOKE +#define DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE(id, ...) DOCTEST_TEST_CASE_TEMPLATE_INVOKE(id,__VA_ARGS__) // clang-format on // BDD style macros @@ -2481,138 +2538,138 @@ int registerReporter(const char* name, int priority, bool isReporter) { // == SHORT VERSIONS OF THE MACROS #if !defined(DOCTEST_CONFIG_NO_SHORT_MACRO_NAMES) -#define TEST_CASE DOCTEST_TEST_CASE -#define TEST_CASE_CLASS DOCTEST_TEST_CASE_CLASS -#define TEST_CASE_FIXTURE DOCTEST_TEST_CASE_FIXTURE -#define TYPE_TO_STRING DOCTEST_TYPE_TO_STRING -#define TEST_CASE_TEMPLATE DOCTEST_TEST_CASE_TEMPLATE -#define TEST_CASE_TEMPLATE_DEFINE DOCTEST_TEST_CASE_TEMPLATE_DEFINE -#define TEST_CASE_TEMPLATE_INVOKE DOCTEST_TEST_CASE_TEMPLATE_INVOKE -#define TEST_CASE_TEMPLATE_APPLY DOCTEST_TEST_CASE_TEMPLATE_APPLY -#define SUBCASE DOCTEST_SUBCASE -#define TEST_SUITE DOCTEST_TEST_SUITE -#define TEST_SUITE_BEGIN DOCTEST_TEST_SUITE_BEGIN +#define TEST_CASE(name) DOCTEST_TEST_CASE(name) +#define TEST_CASE_CLASS(name) DOCTEST_TEST_CASE_CLASS(name) +#define TEST_CASE_FIXTURE(x, name) DOCTEST_TEST_CASE_FIXTURE(x, name) +#define TYPE_TO_STRING(...) DOCTEST_TYPE_TO_STRING(__VA_ARGS__) +#define TEST_CASE_TEMPLATE(name, T, ...) DOCTEST_TEST_CASE_TEMPLATE(name, T, __VA_ARGS__) +#define TEST_CASE_TEMPLATE_DEFINE(name, T, id) DOCTEST_TEST_CASE_TEMPLATE_DEFINE(name, T, id) +#define TEST_CASE_TEMPLATE_INVOKE(id, ...) DOCTEST_TEST_CASE_TEMPLATE_INVOKE(id, __VA_ARGS__) +#define TEST_CASE_TEMPLATE_APPLY(id, ...) DOCTEST_TEST_CASE_TEMPLATE_APPLY(id, __VA_ARGS__) +#define SUBCASE(name) DOCTEST_SUBCASE(name) +#define TEST_SUITE(decorators) DOCTEST_TEST_SUITE(decorators) +#define TEST_SUITE_BEGIN(name) DOCTEST_TEST_SUITE_BEGIN(name) #define TEST_SUITE_END DOCTEST_TEST_SUITE_END -#define REGISTER_EXCEPTION_TRANSLATOR DOCTEST_REGISTER_EXCEPTION_TRANSLATOR -#define REGISTER_REPORTER DOCTEST_REGISTER_REPORTER -#define REGISTER_LISTENER DOCTEST_REGISTER_LISTENER -#define INFO DOCTEST_INFO -#define CAPTURE DOCTEST_CAPTURE -#define ADD_MESSAGE_AT DOCTEST_ADD_MESSAGE_AT -#define ADD_FAIL_CHECK_AT DOCTEST_ADD_FAIL_CHECK_AT -#define ADD_FAIL_AT DOCTEST_ADD_FAIL_AT -#define MESSAGE DOCTEST_MESSAGE -#define FAIL_CHECK DOCTEST_FAIL_CHECK -#define FAIL DOCTEST_FAIL -#define TO_LVALUE DOCTEST_TO_LVALUE - -#define WARN DOCTEST_WARN -#define WARN_FALSE DOCTEST_WARN_FALSE -#define WARN_THROWS DOCTEST_WARN_THROWS -#define WARN_THROWS_AS DOCTEST_WARN_THROWS_AS -#define WARN_THROWS_WITH DOCTEST_WARN_THROWS_WITH -#define WARN_THROWS_WITH_AS DOCTEST_WARN_THROWS_WITH_AS -#define WARN_NOTHROW DOCTEST_WARN_NOTHROW -#define CHECK DOCTEST_CHECK -#define CHECK_FALSE DOCTEST_CHECK_FALSE -#define CHECK_THROWS DOCTEST_CHECK_THROWS -#define CHECK_THROWS_AS DOCTEST_CHECK_THROWS_AS -#define CHECK_THROWS_WITH DOCTEST_CHECK_THROWS_WITH -#define CHECK_THROWS_WITH_AS DOCTEST_CHECK_THROWS_WITH_AS -#define CHECK_NOTHROW DOCTEST_CHECK_NOTHROW -#define REQUIRE DOCTEST_REQUIRE -#define REQUIRE_FALSE DOCTEST_REQUIRE_FALSE -#define REQUIRE_THROWS DOCTEST_REQUIRE_THROWS -#define REQUIRE_THROWS_AS DOCTEST_REQUIRE_THROWS_AS -#define REQUIRE_THROWS_WITH DOCTEST_REQUIRE_THROWS_WITH -#define REQUIRE_THROWS_WITH_AS DOCTEST_REQUIRE_THROWS_WITH_AS -#define REQUIRE_NOTHROW DOCTEST_REQUIRE_NOTHROW - -#define WARN_MESSAGE DOCTEST_WARN_MESSAGE -#define WARN_FALSE_MESSAGE DOCTEST_WARN_FALSE_MESSAGE -#define WARN_THROWS_MESSAGE DOCTEST_WARN_THROWS_MESSAGE -#define WARN_THROWS_AS_MESSAGE DOCTEST_WARN_THROWS_AS_MESSAGE -#define WARN_THROWS_WITH_MESSAGE DOCTEST_WARN_THROWS_WITH_MESSAGE -#define WARN_THROWS_WITH_AS_MESSAGE DOCTEST_WARN_THROWS_WITH_AS_MESSAGE -#define WARN_NOTHROW_MESSAGE DOCTEST_WARN_NOTHROW_MESSAGE -#define CHECK_MESSAGE DOCTEST_CHECK_MESSAGE -#define CHECK_FALSE_MESSAGE DOCTEST_CHECK_FALSE_MESSAGE -#define CHECK_THROWS_MESSAGE DOCTEST_CHECK_THROWS_MESSAGE -#define CHECK_THROWS_AS_MESSAGE DOCTEST_CHECK_THROWS_AS_MESSAGE -#define CHECK_THROWS_WITH_MESSAGE DOCTEST_CHECK_THROWS_WITH_MESSAGE -#define CHECK_THROWS_WITH_AS_MESSAGE DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE -#define CHECK_NOTHROW_MESSAGE DOCTEST_CHECK_NOTHROW_MESSAGE -#define REQUIRE_MESSAGE DOCTEST_REQUIRE_MESSAGE -#define REQUIRE_FALSE_MESSAGE DOCTEST_REQUIRE_FALSE_MESSAGE -#define REQUIRE_THROWS_MESSAGE DOCTEST_REQUIRE_THROWS_MESSAGE -#define REQUIRE_THROWS_AS_MESSAGE DOCTEST_REQUIRE_THROWS_AS_MESSAGE -#define REQUIRE_THROWS_WITH_MESSAGE DOCTEST_REQUIRE_THROWS_WITH_MESSAGE -#define REQUIRE_THROWS_WITH_AS_MESSAGE DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE -#define REQUIRE_NOTHROW_MESSAGE DOCTEST_REQUIRE_NOTHROW_MESSAGE - -#define SCENARIO DOCTEST_SCENARIO -#define SCENARIO_CLASS DOCTEST_SCENARIO_CLASS -#define SCENARIO_TEMPLATE DOCTEST_SCENARIO_TEMPLATE -#define SCENARIO_TEMPLATE_DEFINE DOCTEST_SCENARIO_TEMPLATE_DEFINE -#define GIVEN DOCTEST_GIVEN -#define WHEN DOCTEST_WHEN -#define AND_WHEN DOCTEST_AND_WHEN -#define THEN DOCTEST_THEN -#define AND_THEN DOCTEST_AND_THEN - -#define WARN_EQ DOCTEST_WARN_EQ -#define CHECK_EQ DOCTEST_CHECK_EQ -#define REQUIRE_EQ DOCTEST_REQUIRE_EQ -#define WARN_NE DOCTEST_WARN_NE -#define CHECK_NE DOCTEST_CHECK_NE -#define REQUIRE_NE DOCTEST_REQUIRE_NE -#define WARN_GT DOCTEST_WARN_GT -#define CHECK_GT DOCTEST_CHECK_GT -#define REQUIRE_GT DOCTEST_REQUIRE_GT -#define WARN_LT DOCTEST_WARN_LT -#define CHECK_LT DOCTEST_CHECK_LT -#define REQUIRE_LT DOCTEST_REQUIRE_LT -#define WARN_GE DOCTEST_WARN_GE -#define CHECK_GE DOCTEST_CHECK_GE -#define REQUIRE_GE DOCTEST_REQUIRE_GE -#define WARN_LE DOCTEST_WARN_LE -#define CHECK_LE DOCTEST_CHECK_LE -#define REQUIRE_LE DOCTEST_REQUIRE_LE -#define WARN_UNARY DOCTEST_WARN_UNARY -#define CHECK_UNARY DOCTEST_CHECK_UNARY -#define REQUIRE_UNARY DOCTEST_REQUIRE_UNARY -#define WARN_UNARY_FALSE DOCTEST_WARN_UNARY_FALSE -#define CHECK_UNARY_FALSE DOCTEST_CHECK_UNARY_FALSE -#define REQUIRE_UNARY_FALSE DOCTEST_REQUIRE_UNARY_FALSE +#define REGISTER_EXCEPTION_TRANSLATOR(signature) DOCTEST_REGISTER_EXCEPTION_TRANSLATOR(signature) +#define REGISTER_REPORTER(name, priority, reporter) DOCTEST_REGISTER_REPORTER(name, priority, reporter) +#define REGISTER_LISTENER(name, priority, reporter) DOCTEST_REGISTER_LISTENER(name, priority, reporter) +#define INFO(...) DOCTEST_INFO(__VA_ARGS__) +#define CAPTURE(x) DOCTEST_CAPTURE(x) +#define ADD_MESSAGE_AT(file, line, ...) DOCTEST_ADD_MESSAGE_AT(file, line, __VA_ARGS__) +#define ADD_FAIL_CHECK_AT(file, line, ...) DOCTEST_ADD_FAIL_CHECK_AT(file, line, __VA_ARGS__) +#define ADD_FAIL_AT(file, line, ...) DOCTEST_ADD_FAIL_AT(file, line, __VA_ARGS__) +#define MESSAGE(...) DOCTEST_MESSAGE(__VA_ARGS__) +#define FAIL_CHECK(...) DOCTEST_FAIL_CHECK(__VA_ARGS__) +#define FAIL(...) DOCTEST_FAIL(__VA_ARGS__) +#define TO_LVALUE(...) DOCTEST_TO_LVALUE(__VA_ARGS__) + +#define WARN(...) DOCTEST_WARN(__VA_ARGS__) +#define WARN_FALSE(...) DOCTEST_WARN_FALSE(__VA_ARGS__) +#define WARN_THROWS(...) DOCTEST_WARN_THROWS(__VA_ARGS__) +#define WARN_THROWS_AS(expr, ...) DOCTEST_WARN_THROWS_AS(expr, __VA_ARGS__) +#define WARN_THROWS_WITH(expr, ...) DOCTEST_WARN_THROWS_WITH(expr, __VA_ARGS__) +#define WARN_THROWS_WITH_AS(expr, with, ...) DOCTEST_WARN_THROWS_WITH_AS(expr, with, __VA_ARGS__) +#define WARN_NOTHROW(...) DOCTEST_WARN_NOTHROW(__VA_ARGS__) +#define CHECK(...) DOCTEST_CHECK(__VA_ARGS__) +#define CHECK_FALSE(...) DOCTEST_CHECK_FALSE(__VA_ARGS__) +#define CHECK_THROWS(...) DOCTEST_CHECK_THROWS(__VA_ARGS__) +#define CHECK_THROWS_AS(expr, ...) DOCTEST_CHECK_THROWS_AS(expr, __VA_ARGS__) +#define CHECK_THROWS_WITH(expr, ...) DOCTEST_CHECK_THROWS_WITH(expr, __VA_ARGS__) +#define CHECK_THROWS_WITH_AS(expr, with, ...) DOCTEST_CHECK_THROWS_WITH_AS(expr, with, __VA_ARGS__) +#define CHECK_NOTHROW(...) DOCTEST_CHECK_NOTHROW(__VA_ARGS__) +#define REQUIRE(...) DOCTEST_REQUIRE(__VA_ARGS__) +#define REQUIRE_FALSE(...) DOCTEST_REQUIRE_FALSE(__VA_ARGS__) +#define REQUIRE_THROWS(...) DOCTEST_REQUIRE_THROWS(__VA_ARGS__) +#define REQUIRE_THROWS_AS(expr, ...) DOCTEST_REQUIRE_THROWS_AS(expr, __VA_ARGS__) +#define REQUIRE_THROWS_WITH(expr, ...) DOCTEST_REQUIRE_THROWS_WITH(expr, __VA_ARGS__) +#define REQUIRE_THROWS_WITH_AS(expr, with, ...) DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, __VA_ARGS__) +#define REQUIRE_NOTHROW(...) DOCTEST_REQUIRE_NOTHROW(__VA_ARGS__) + +#define WARN_MESSAGE(cond, ...) DOCTEST_WARN_MESSAGE(cond, __VA_ARGS__) +#define WARN_FALSE_MESSAGE(cond, ...) DOCTEST_WARN_FALSE_MESSAGE(cond, __VA_ARGS__) +#define WARN_THROWS_MESSAGE(expr, ...) DOCTEST_WARN_THROWS_MESSAGE(expr, __VA_ARGS__) +#define WARN_THROWS_AS_MESSAGE(expr, ex, ...) DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, __VA_ARGS__) +#define WARN_THROWS_WITH_MESSAGE(expr, with, ...) DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, __VA_ARGS__) +#define WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, __VA_ARGS__) +#define WARN_NOTHROW_MESSAGE(expr, ...) DOCTEST_WARN_NOTHROW_MESSAGE(expr, __VA_ARGS__) +#define CHECK_MESSAGE(cond, ...) DOCTEST_CHECK_MESSAGE(cond, __VA_ARGS__) +#define CHECK_FALSE_MESSAGE(cond, ...) DOCTEST_CHECK_FALSE_MESSAGE(cond, __VA_ARGS__) +#define CHECK_THROWS_MESSAGE(expr, ...) DOCTEST_CHECK_THROWS_MESSAGE(expr, __VA_ARGS__) +#define CHECK_THROWS_AS_MESSAGE(expr, ex, ...) DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, __VA_ARGS__) +#define CHECK_THROWS_WITH_MESSAGE(expr, with, ...) DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, __VA_ARGS__) +#define CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, __VA_ARGS__) +#define CHECK_NOTHROW_MESSAGE(expr, ...) DOCTEST_CHECK_NOTHROW_MESSAGE(expr, __VA_ARGS__) +#define REQUIRE_MESSAGE(cond, ...) DOCTEST_REQUIRE_MESSAGE(cond, __VA_ARGS__) +#define REQUIRE_FALSE_MESSAGE(cond, ...) DOCTEST_REQUIRE_FALSE_MESSAGE(cond, __VA_ARGS__) +#define REQUIRE_THROWS_MESSAGE(expr, ...) DOCTEST_REQUIRE_THROWS_MESSAGE(expr, __VA_ARGS__) +#define REQUIRE_THROWS_AS_MESSAGE(expr, ex, ...) DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, __VA_ARGS__) +#define REQUIRE_THROWS_WITH_MESSAGE(expr, with, ...) DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, __VA_ARGS__) +#define REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, __VA_ARGS__) +#define REQUIRE_NOTHROW_MESSAGE(expr, ...) DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, __VA_ARGS__) + +#define SCENARIO(name) DOCTEST_SCENARIO(name) +#define SCENARIO_CLASS(name) DOCTEST_SCENARIO_CLASS(name) +#define SCENARIO_TEMPLATE(name, T, ...) DOCTEST_SCENARIO_TEMPLATE(name, T, __VA_ARGS__) +#define SCENARIO_TEMPLATE_DEFINE(name, T, id) DOCTEST_SCENARIO_TEMPLATE_DEFINE(name, T, id) +#define GIVEN(name) DOCTEST_GIVEN(name) +#define WHEN(name) DOCTEST_WHEN(name) +#define AND_WHEN(name) DOCTEST_AND_WHEN(name) +#define THEN(name) DOCTEST_THEN(name) +#define AND_THEN(name) DOCTEST_AND_THEN(name) + +#define WARN_EQ(...) DOCTEST_WARN_EQ(__VA_ARGS__) +#define CHECK_EQ(...) DOCTEST_CHECK_EQ(__VA_ARGS__) +#define REQUIRE_EQ(...) DOCTEST_REQUIRE_EQ(__VA_ARGS__) +#define WARN_NE(...) DOCTEST_WARN_NE(__VA_ARGS__) +#define CHECK_NE(...) DOCTEST_CHECK_NE(__VA_ARGS__) +#define REQUIRE_NE(...) DOCTEST_REQUIRE_NE(__VA_ARGS__) +#define WARN_GT(...) DOCTEST_WARN_GT(__VA_ARGS__) +#define CHECK_GT(...) DOCTEST_CHECK_GT(__VA_ARGS__) +#define REQUIRE_GT(...) DOCTEST_REQUIRE_GT(__VA_ARGS__) +#define WARN_LT(...) DOCTEST_WARN_LT(__VA_ARGS__) +#define CHECK_LT(...) DOCTEST_CHECK_LT(__VA_ARGS__) +#define REQUIRE_LT(...) DOCTEST_REQUIRE_LT(__VA_ARGS__) +#define WARN_GE(...) DOCTEST_WARN_GE(__VA_ARGS__) +#define CHECK_GE(...) DOCTEST_CHECK_GE(__VA_ARGS__) +#define REQUIRE_GE(...) DOCTEST_REQUIRE_GE(__VA_ARGS__) +#define WARN_LE(...) DOCTEST_WARN_LE(__VA_ARGS__) +#define CHECK_LE(...) DOCTEST_CHECK_LE(__VA_ARGS__) +#define REQUIRE_LE(...) DOCTEST_REQUIRE_LE(__VA_ARGS__) +#define WARN_UNARY(...) DOCTEST_WARN_UNARY(__VA_ARGS__) +#define CHECK_UNARY(...) DOCTEST_CHECK_UNARY(__VA_ARGS__) +#define REQUIRE_UNARY(...) DOCTEST_REQUIRE_UNARY(__VA_ARGS__) +#define WARN_UNARY_FALSE(...) DOCTEST_WARN_UNARY_FALSE(__VA_ARGS__) +#define CHECK_UNARY_FALSE(...) DOCTEST_CHECK_UNARY_FALSE(__VA_ARGS__) +#define REQUIRE_UNARY_FALSE(...) DOCTEST_REQUIRE_UNARY_FALSE(__VA_ARGS__) // KEPT FOR BACKWARDS COMPATIBILITY -#define FAST_WARN_EQ DOCTEST_FAST_WARN_EQ -#define FAST_CHECK_EQ DOCTEST_FAST_CHECK_EQ -#define FAST_REQUIRE_EQ DOCTEST_FAST_REQUIRE_EQ -#define FAST_WARN_NE DOCTEST_FAST_WARN_NE -#define FAST_CHECK_NE DOCTEST_FAST_CHECK_NE -#define FAST_REQUIRE_NE DOCTEST_FAST_REQUIRE_NE -#define FAST_WARN_GT DOCTEST_FAST_WARN_GT -#define FAST_CHECK_GT DOCTEST_FAST_CHECK_GT -#define FAST_REQUIRE_GT DOCTEST_FAST_REQUIRE_GT -#define FAST_WARN_LT DOCTEST_FAST_WARN_LT -#define FAST_CHECK_LT DOCTEST_FAST_CHECK_LT -#define FAST_REQUIRE_LT DOCTEST_FAST_REQUIRE_LT -#define FAST_WARN_GE DOCTEST_FAST_WARN_GE -#define FAST_CHECK_GE DOCTEST_FAST_CHECK_GE -#define FAST_REQUIRE_GE DOCTEST_FAST_REQUIRE_GE -#define FAST_WARN_LE DOCTEST_FAST_WARN_LE -#define FAST_CHECK_LE DOCTEST_FAST_CHECK_LE -#define FAST_REQUIRE_LE DOCTEST_FAST_REQUIRE_LE - -#define FAST_WARN_UNARY DOCTEST_FAST_WARN_UNARY -#define FAST_CHECK_UNARY DOCTEST_FAST_CHECK_UNARY -#define FAST_REQUIRE_UNARY DOCTEST_FAST_REQUIRE_UNARY -#define FAST_WARN_UNARY_FALSE DOCTEST_FAST_WARN_UNARY_FALSE -#define FAST_CHECK_UNARY_FALSE DOCTEST_FAST_CHECK_UNARY_FALSE -#define FAST_REQUIRE_UNARY_FALSE DOCTEST_FAST_REQUIRE_UNARY_FALSE - -#define TEST_CASE_TEMPLATE_INSTANTIATE DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE +#define FAST_WARN_EQ(...) DOCTEST_FAST_WARN_EQ(__VA_ARGS__) +#define FAST_CHECK_EQ(...) DOCTEST_FAST_CHECK_EQ(__VA_ARGS__) +#define FAST_REQUIRE_EQ(...) DOCTEST_FAST_REQUIRE_EQ(__VA_ARGS__) +#define FAST_WARN_NE(...) DOCTEST_FAST_WARN_NE(__VA_ARGS__) +#define FAST_CHECK_NE(...) DOCTEST_FAST_CHECK_NE(__VA_ARGS__) +#define FAST_REQUIRE_NE(...) DOCTEST_FAST_REQUIRE_NE(__VA_ARGS__) +#define FAST_WARN_GT(...) DOCTEST_FAST_WARN_GT(__VA_ARGS__) +#define FAST_CHECK_GT(...) DOCTEST_FAST_CHECK_GT(__VA_ARGS__) +#define FAST_REQUIRE_GT(...) DOCTEST_FAST_REQUIRE_GT(__VA_ARGS__) +#define FAST_WARN_LT(...) DOCTEST_FAST_WARN_LT(__VA_ARGS__) +#define FAST_CHECK_LT(...) DOCTEST_FAST_CHECK_LT(__VA_ARGS__) +#define FAST_REQUIRE_LT(...) DOCTEST_FAST_REQUIRE_LT(__VA_ARGS__) +#define FAST_WARN_GE(...) DOCTEST_FAST_WARN_GE(__VA_ARGS__) +#define FAST_CHECK_GE(...) DOCTEST_FAST_CHECK_GE(__VA_ARGS__) +#define FAST_REQUIRE_GE(...) DOCTEST_FAST_REQUIRE_GE(__VA_ARGS__) +#define FAST_WARN_LE(...) DOCTEST_FAST_WARN_LE(__VA_ARGS__) +#define FAST_CHECK_LE(...) DOCTEST_FAST_CHECK_LE(__VA_ARGS__) +#define FAST_REQUIRE_LE(...) DOCTEST_FAST_REQUIRE_LE(__VA_ARGS__) + +#define FAST_WARN_UNARY(...) DOCTEST_FAST_WARN_UNARY(__VA_ARGS__) +#define FAST_CHECK_UNARY(...) DOCTEST_FAST_CHECK_UNARY(__VA_ARGS__) +#define FAST_REQUIRE_UNARY(...) DOCTEST_FAST_REQUIRE_UNARY(__VA_ARGS__) +#define FAST_WARN_UNARY_FALSE(...) DOCTEST_FAST_WARN_UNARY_FALSE(__VA_ARGS__) +#define FAST_CHECK_UNARY_FALSE(...) DOCTEST_FAST_CHECK_UNARY_FALSE(__VA_ARGS__) +#define FAST_REQUIRE_UNARY_FALSE(...) DOCTEST_FAST_REQUIRE_UNARY_FALSE(__VA_ARGS__) + +#define TEST_CASE_TEMPLATE_INSTANTIATE(id, ...) DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE(id, __VA_ARGS__) #endif // DOCTEST_CONFIG_NO_SHORT_MACRO_NAMES @@ -2689,6 +2746,7 @@ DOCTEST_CLANG_SUPPRESS_WARNING("-Wmissing-field-initializers") DOCTEST_CLANG_SUPPRESS_WARNING("-Wc++98-compat") DOCTEST_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic") DOCTEST_CLANG_SUPPRESS_WARNING("-Wunused-member-function") +DOCTEST_CLANG_SUPPRESS_WARNING("-Wnonportable-system-include-path") DOCTEST_GCC_SUPPRESS_WARNING_PUSH DOCTEST_GCC_SUPPRESS_WARNING("-Wunknown-pragmas") @@ -2794,11 +2852,7 @@ DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN #ifdef __AFXDLL #include #else -#if defined(__MINGW32__) || defined(__MINGW64__) #include -#else // MINGW -#include -#endif // MINGW #endif #include @@ -2834,12 +2888,24 @@ DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END #define DOCTEST_THREAD_LOCAL thread_local #endif +#ifndef DOCTEST_MULTI_LANE_ATOMICS_THREAD_LANES +#define DOCTEST_MULTI_LANE_ATOMICS_THREAD_LANES 32 +#endif + +#ifndef DOCTEST_MULTI_LANE_ATOMICS_CACHE_LINE_SIZE +#define DOCTEST_MULTI_LANE_ATOMICS_CACHE_LINE_SIZE 64 +#endif + #ifdef DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS #define DOCTEST_OPTIONS_PREFIX_DISPLAY DOCTEST_CONFIG_OPTIONS_PREFIX #else #define DOCTEST_OPTIONS_PREFIX_DISPLAY "" #endif +#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) +#define DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS +#endif + namespace doctest { bool is_running_in_test = false; @@ -2972,18 +3038,105 @@ typedef timer_large_integer::type ticks_t; ticks_t m_ticks = 0; }; +#ifdef DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS + template + using AtomicOrMultiLaneAtomic = std::atomic; +#else // DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS + // Provides a multilane implementation of an atomic variable that supports add, sub, load, + // store. Instead of using a single atomic variable, this splits up into multiple ones, + // each sitting on a separate cache line. The goal is to provide a speedup when most + // operations are modifying. It achieves this with two properties: + // + // * Multiple atomics are used, so chance of congestion from the same atomic is reduced. + // * Each atomic sits on a separate cache line, so false sharing is reduced. + // + // The disadvantage is that there is a small overhead due to the use of TLS, and load/store + // is slower because all atomics have to be accessed. + template + class MultiLaneAtomic + { + struct CacheLineAlignedAtomic + { + std::atomic atomic{}; + char padding[DOCTEST_MULTI_LANE_ATOMICS_CACHE_LINE_SIZE - sizeof(std::atomic)]; + }; + CacheLineAlignedAtomic m_atomics[DOCTEST_MULTI_LANE_ATOMICS_THREAD_LANES]; + + static_assert(sizeof(CacheLineAlignedAtomic) == DOCTEST_MULTI_LANE_ATOMICS_CACHE_LINE_SIZE, + "guarantee one atomic takes exactly one cache line"); + + public: + T operator++() DOCTEST_NOEXCEPT { return fetch_add(1) + 1; } + + T operator++(int) DOCTEST_NOEXCEPT { return fetch_add(1); } + + T fetch_add(T arg, std::memory_order order = std::memory_order_seq_cst) DOCTEST_NOEXCEPT { + return myAtomic().fetch_add(arg, order); + } + + T fetch_sub(T arg, std::memory_order order = std::memory_order_seq_cst) DOCTEST_NOEXCEPT { + return myAtomic().fetch_sub(arg, order); + } + + operator T() const DOCTEST_NOEXCEPT { return load(); } + + T load(std::memory_order order = std::memory_order_seq_cst) const DOCTEST_NOEXCEPT { + auto result = T(); + for(auto const& c : m_atomics) { + result += c.atomic.load(order); + } + return result; + } + + T operator=(T desired) DOCTEST_NOEXCEPT { + store(desired); + return desired; + } + + void store(T desired, std::memory_order order = std::memory_order_seq_cst) DOCTEST_NOEXCEPT { + // first value becomes desired", all others become 0. + for(auto& c : m_atomics) { + c.atomic.store(desired, order); + desired = {}; + } + } + + private: + // Each thread has a different atomic that it operates on. If more than NumLanes threads + // use this, some will use the same atomic. So performance will degrate a bit, but still + // everything will work. + // + // The logic here is a bit tricky. The call should be as fast as possible, so that there + // is minimal to no overhead in determining the correct atomic for the current thread. + // + // 1. A global static counter laneCounter counts continuously up. + // 2. Each successive thread will use modulo operation of that counter so it gets an atomic + // assigned in a round-robin fashion. + // 3. This tlsLaneIdx is stored in the thread local data, so it is directly available with + // little overhead. + std::atomic& myAtomic() DOCTEST_NOEXCEPT { + static std::atomic laneCounter; + DOCTEST_THREAD_LOCAL size_t tlsLaneIdx = + laneCounter++ % DOCTEST_MULTI_LANE_ATOMICS_THREAD_LANES; + + return m_atomics[tlsLaneIdx].atomic; + } + }; + + template + using AtomicOrMultiLaneAtomic = MultiLaneAtomic; +#endif // DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS + // this holds both parameters from the command line and runtime data for tests struct ContextState : ContextOptions, TestRunStats, CurrentTestCaseStats { - std::atomic numAssertsCurrentTest_atomic; - std::atomic numAssertsFailedCurrentTest_atomic; + AtomicOrMultiLaneAtomic numAssertsCurrentTest_atomic; + AtomicOrMultiLaneAtomic numAssertsFailedCurrentTest_atomic; std::vector> filters = decltype(filters)(9); // 9 different filters std::vector reporters_currently_used; - const TestCase* currentTest = nullptr; - assert_handler ah = nullptr; Timer timer; @@ -3093,14 +3246,16 @@ String::String(const char* in) String::String(const char* in, unsigned in_size) { using namespace std; if(in_size <= last) { - memcpy(buf, in, in_size + 1); + memcpy(buf, in, in_size); + buf[in_size] = '\0'; setLast(last - in_size); } else { setOnHeap(); data.size = in_size; data.capacity = data.size + 1; data.ptr = new char[data.capacity]; - memcpy(data.ptr, in, in_size + 1); + memcpy(data.ptr, in, in_size); + data.ptr[in_size] = '\0'; } } @@ -3471,7 +3626,7 @@ int registerReporter(const char*, int, IReporter*) { return 0; } namespace doctest_detail_test_suite_ns { // holds the current test suite doctest::detail::TestSuite& getCurrentTestSuite() { - static doctest::detail::TestSuite data; + static doctest::detail::TestSuite data{}; return data; } } // namespace doctest_detail_test_suite_ns @@ -3583,7 +3738,7 @@ namespace detail { Subcase::Subcase(const String& name, const char* file, int line) : m_signature({name, file, line}) { - ContextState* s = g_cs; + auto* s = g_cs; // check subcase filters if(s->subcasesStack.size() < size_t(s->subcase_filter_levels)) { @@ -3625,7 +3780,7 @@ namespace detail { g_cs->subcasesPassed.insert(g_cs->subcasesStack); g_cs->subcasesStack.pop_back(); -#if defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411L +#if defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411L && (!defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200) if(std::uncaught_exceptions() > 0 #else if(std::uncaught_exception() @@ -3660,6 +3815,8 @@ namespace detail { // clear state m_description = nullptr; m_skip = false; + m_no_breaks = false; + m_no_output = false; m_may_fail = false; m_should_fail = false; m_expected_failures = 0; @@ -3675,6 +3832,8 @@ namespace detail { m_test_suite = test_suite.m_test_suite; m_description = test_suite.m_description; m_skip = test_suite.m_skip; + m_no_breaks = test_suite.m_no_breaks; + m_no_output = test_suite.m_no_output; m_may_fail = test_suite.m_may_fail; m_should_fail = test_suite.m_should_fail; m_expected_failures = test_suite.m_expected_failures; @@ -3721,14 +3880,20 @@ namespace detail { // this will be used only to differentiate between test cases - not relevant for sorting if(m_line != other.m_line) return m_line < other.m_line; - const int file_cmp = m_file.compare(other.m_file); - if(file_cmp != 0) - return file_cmp < 0; const int name_cmp = strcmp(m_name, other.m_name); if(name_cmp != 0) return name_cmp < 0; + const int file_cmp = m_file.compare(other.m_file); + if(file_cmp != 0) + return file_cmp < 0; return m_template_id < other.m_template_id; } + + // all the registered tests + std::set& getRegisteredTests() { + static std::set data; + return data; + } } // namespace detail namespace { using namespace detail; @@ -3760,12 +3925,6 @@ namespace { return suiteOrderComparator(lhs, rhs); } - // all the registered tests - std::set& getRegisteredTests() { - static std::set data; - return data; - } - #ifdef DOCTEST_CONFIG_COLORS_WINDOWS HANDLE g_stdoutHandle; WORD g_origFgAttrs; @@ -3994,7 +4153,7 @@ namespace detail { // ContextScope has been destroyed (base class destructors run after derived class destructors). // Instead, ContextScope calls this method directly from its destructor. void ContextScopeBase::destroy() { -#if defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411L +#if defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411L && (!defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200) if(std::uncaught_exceptions() > 0) { #else if(std::uncaught_exception()) { @@ -4016,7 +4175,9 @@ namespace { #if !defined(DOCTEST_CONFIG_POSIX_SIGNALS) && !defined(DOCTEST_CONFIG_WINDOWS_SEH) struct FatalConditionHandler { - void reset() {} + static void reset() {} + static void allocateAltStackMem() {} + static void freeAltStackMem() {} }; #else // DOCTEST_CONFIG_POSIX_SIGNALS || DOCTEST_CONFIG_WINDOWS_SEH @@ -4069,6 +4230,9 @@ namespace { std::exit(EXIT_FAILURE); } + static void allocateAltStackMem() {} + static void freeAltStackMem() {} + FatalConditionHandler() { isSet = true; // 32k seems enough for doctest to handle stack overflow, @@ -4086,7 +4250,7 @@ namespace { // - std::terminate is called FROM THE TEST RUNNER THREAD // - an exception is thrown from a destructor FROM THE TEST RUNNER THREAD original_terminate_handler = std::get_terminate(); - std::set_terminate([]() noexcept { + std::set_terminate([]() DOCTEST_NOEXCEPT { reportFatal("Terminate handler called"); if(isDebuggerActive() && !g_cs->no_breaks) DOCTEST_BREAK_INTO_DEBUGGER(); @@ -4097,7 +4261,7 @@ namespace { // - std::terminate is called FROM A DIFFERENT THREAD // - an exception is thrown from a destructor FROM A DIFFERENT THREAD // - an uncaught exception is thrown FROM A DIFFERENT THREAD - prev_sigabrt_handler = std::signal(SIGABRT, [](int signal) noexcept { + prev_sigabrt_handler = std::signal(SIGABRT, [](int signal) DOCTEST_NOEXCEPT { if(signal == SIGABRT) { reportFatal("SIGABRT - Abort (abnormal termination) signal"); if(isDebuggerActive() && !g_cs->no_breaks) @@ -4135,8 +4299,8 @@ namespace { SetErrorMode(prev_error_mode_1); _set_error_mode(prev_error_mode_2); _set_abort_behavior(prev_abort_behavior, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); - _CrtSetReportMode(_CRT_ASSERT, prev_report_mode); - _CrtSetReportFile(_CRT_ASSERT, prev_report_file); + static_cast(_CrtSetReportMode(_CRT_ASSERT, prev_report_mode)); + static_cast(_CrtSetReportFile(_CRT_ASSERT, prev_report_file)); isSet = false; } } @@ -4186,7 +4350,8 @@ namespace { static bool isSet; static struct sigaction oldSigActions[DOCTEST_COUNTOF(signalDefs)]; static stack_t oldSigStack; - static char altStackMem[4 * SIGSTKSZ]; + static size_t altStackSize; + static char* altStackMem; static void handleSignal(int sig) { const char* name = ""; @@ -4202,11 +4367,19 @@ namespace { raise(sig); } + static void allocateAltStackMem() { + altStackMem = new char[altStackSize]; + } + + static void freeAltStackMem() { + delete[] altStackMem; + } + FatalConditionHandler() { isSet = true; stack_t sigStack; sigStack.ss_sp = altStackMem; - sigStack.ss_size = sizeof(altStackMem); + sigStack.ss_size = altStackSize; sigStack.ss_flags = 0; sigaltstack(&sigStack, &oldSigStack); struct sigaction sa = {}; @@ -4231,10 +4404,11 @@ namespace { } }; - bool FatalConditionHandler::isSet = false; + bool FatalConditionHandler::isSet = false; struct sigaction FatalConditionHandler::oldSigActions[DOCTEST_COUNTOF(signalDefs)] = {}; - stack_t FatalConditionHandler::oldSigStack = {}; - char FatalConditionHandler::altStackMem[] = {}; + stack_t FatalConditionHandler::oldSigStack = {}; + size_t FatalConditionHandler::altStackSize = 4 * SIGSTKSZ; + char* FatalConditionHandler::altStackMem = nullptr; #endif // DOCTEST_PLATFORM_WINDOWS #endif // DOCTEST_CONFIG_POSIX_SIGNALS || DOCTEST_CONFIG_WINDOWS_SEH @@ -4336,8 +4510,8 @@ namespace detail { failed_out_of_a_testing_context(*this); } - return m_failed && isDebuggerActive() && - !getContextOptions()->no_breaks; // break into debugger + return m_failed && isDebuggerActive() && !getContextOptions()->no_breaks && + (g_cs->currentTest == nullptr || !g_cs->currentTest->m_no_breaks); // break into debugger } void ResultBuilder::react() const { @@ -4387,7 +4561,8 @@ namespace detail { addFailedAssert(m_severity); } - return isDebuggerActive() && !getContextOptions()->no_breaks && !isWarn; // break + return isDebuggerActive() && !getContextOptions()->no_breaks && !isWarn && + (g_cs->currentTest == nullptr || !g_cs->currentTest->m_no_breaks); // break into debugger } void MessageBuilder::react() { @@ -5495,7 +5670,7 @@ namespace { << Whitespace(sizePrefixDisplay*1) << "output filename\n"; s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "ob, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "order-by= " << Whitespace(sizePrefixDisplay*1) << "how the tests should be ordered\n"; - s << Whitespace(sizePrefixDisplay*3) << " - by [file/suite/name/rand]\n"; + s << Whitespace(sizePrefixDisplay*3) << " - [file/suite/name/rand/none]\n"; s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "rs, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "rand-seed= " << Whitespace(sizePrefixDisplay*1) << "seed for random ordering\n"; s << " -" DOCTEST_OPTIONS_PREFIX_DISPLAY "f, --" DOCTEST_OPTIONS_PREFIX_DISPLAY "first= " @@ -5668,6 +5843,9 @@ namespace { } void test_case_end(const CurrentTestCaseStats& st) override { + if(tc->m_no_output) + return; + // log the preamble of the test case only if there is something // else to print - something other than that an assert has failed if(opt.duration || @@ -5702,6 +5880,9 @@ namespace { } void test_case_exception(const TestCaseException& e) override { + if(tc->m_no_output) + return; + logTestStart(); file_line_to_stream(tc->m_file.c_str(), tc->m_line, " "); @@ -5736,7 +5917,7 @@ namespace { } void log_assert(const AssertData& rb) override { - if(!rb.m_failed && !opt.success) + if((!rb.m_failed && !opt.success) || tc->m_no_output) return; std::lock_guard lock(mutex); @@ -5752,6 +5933,9 @@ namespace { } void log_message(const MessageData& mb) override { + if(tc->m_no_output) + return; + std::lock_guard lock(mutex); logTestStart(); @@ -5782,8 +5966,10 @@ namespace { bool with_col = g_no_colors; \ g_no_colors = false; \ ConsoleReporter::func(arg); \ - DOCTEST_OUTPUT_DEBUG_STRING(oss.str().c_str()); \ - oss.str(""); \ + if(oss.tellp() != std::streampos{}) { \ + DOCTEST_OUTPUT_DEBUG_STRING(oss.str().c_str()); \ + oss.str(""); \ + } \ g_no_colors = with_col; \ } @@ -5970,7 +6156,7 @@ void Context::parseArgs(int argc, const char* const* argv, bool withDefaults) { #define DOCTEST_PARSE_AS_BOOL_OR_FLAG(name, sname, var, default) \ if(parseIntOption(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX name "=", option_bool, intRes) || \ parseIntOption(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX sname "=", option_bool, intRes)) \ - p->var = !!intRes; \ + p->var = static_cast(intRes); \ else if(parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX name) || \ parseFlag(argc, argv, DOCTEST_CONFIG_OPTIONS_PREFIX sname)) \ p->var = true; \ @@ -6115,7 +6301,11 @@ int Context::run() { p->cout = &fstr; } + FatalConditionHandler::allocateAltStackMem(); + auto cleanup_and_return = [&]() { + FatalConditionHandler::freeAltStackMem(); + if(fstr.is_open()) fstr.close(); @@ -6187,6 +6377,9 @@ int Context::run() { first[i] = first[idxToSwap]; first[idxToSwap] = temp; } + } else if(p->order_by.compare("none", true) == 0) { + // means no sorting - beneficial for death tests which call into the executable + // with a specific test case in mind - we don't want to slow down the startup times } } @@ -6286,10 +6479,13 @@ int Context::run() { #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS try { #endif // DOCTEST_CONFIG_NO_EXCEPTIONS +// MSVC 2015 diagnoses fatalConditionHandler as unused (because reset() is a static method) +DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4101) // unreferenced local variable FatalConditionHandler fatalConditionHandler; // Handle signals // execute the test tc.m_test(); fatalConditionHandler.reset(); +DOCTEST_MSVC_SUPPRESS_WARNING_POP #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS } catch(const TestFailureException&) { p->failure_flags |= TestCaseFailureReason::AssertFailure; From c591b6ca6c7151f1922b1709d622eef96d18b46e Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 27 Mar 2021 10:43:05 +0100 Subject: [PATCH 076/143] :construction_worker: move some MSVC jobs from AppVeyor to GitHub Actions --- .github/workflows/windows.yml | 112 +++++++++++++++++++++++++++++++++- appveyor.yml | 33 +--------- 2 files changed, 110 insertions(+), 35 deletions(-) diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index ae243762fe..d88de6246c 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -3,17 +3,109 @@ name: Windows on: [push, pull_request] jobs: + mingw: + runs-on: windows-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up MinGW + uses: egor-tensin/setup-mingw@v2 + with: + platform: x64 + - name: cmake + run: cmake -S . -B build -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On + - name: build + run: cmake --build build --parallel 10 + - name: test + run: cd build ; ctest -j 10 -C Debug --output-on-failure + + mingw_win32: + runs-on: windows-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up MinGW + uses: egor-tensin/setup-mingw@v2 + with: + platform: x86 + - name: cmake + run: cmake -S . -B build -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On + - name: build + run: cmake --build build --parallel 10 + - name: test + run: cd build ; ctest -j 10 -C Debug --output-on-failure + + msvc2017: + runs-on: windows-2016 + + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -G "Visual Studio 15 2017" -DJSON_BuildTests=On + - name: build + run: cmake --build build --config Release --parallel 10 + - name: test + run: cd build ; ctest -j 10 -C Release --output-on-failure + + msvc2017_latest: + runs-on: windows-2016 + + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -G "Visual Studio 15 2017" -DJSON_BuildTests=On -DCMAKE_CXX_FLAGS="/permissive- /std:c++latest /utf-8" + - name: build + run: cmake --build build --config Release --parallel 10 + - name: test + run: cd build ; ctest -j 10 -C Release --output-on-failure + + msvc2017_win32: + runs-on: windows-2016 + + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -G "Visual Studio 15 2017" -A "Win32" -DJSON_BuildTests=On + - name: build + run: cmake --build build --config Release --parallel 10 + - name: test + run: cd build ; ctest -j 10 -C Release --output-on-failure + msvc2019: runs-on: windows-latest steps: - uses: actions/checkout@v2 - name: cmake - run: cmake -S . -B build -G "Visual Studio 16 2019" -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On + run: cmake -S . -B build -G "Visual Studio 16 2019" -DJSON_BuildTests=On - name: build - run: cmake --build build --parallel 10 + run: cmake --build build --config Release --parallel 10 - name: test - run: cd build ; ctest -j 10 -C Debug --exclude-regex "test-unicode" --output-on-failure + run: cd build ; ctest -j 10 -C Release --output-on-failure + + msvc2019_latest: + runs-on: windows-latest + + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -G "Visual Studio 16 2019" -DJSON_BuildTests=On -DCMAKE_CXX_FLAGS="/permissive- /std:c++latest /utf-8" + - name: build + run: cmake --build build --config Release --parallel 10 + - name: test + run: cd build ; ctest -j 10 -C Release --output-on-failure + + msvc2019_win32: + runs-on: windows-latest + + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -G "Visual Studio 16 2019" -A "Win32" -DJSON_BuildTests=On + - name: build + run: cmake --build build --config Release --parallel 10 + - name: test + run: cd build ; ctest -j 10 -C Release --output-on-failure clang10: runs-on: windows-latest @@ -29,6 +121,20 @@ jobs: - name: test run: cd build ; ctest -j 10 -C Debug --exclude-regex "test-unicode" --output-on-failure + clang11: + runs-on: windows-latest + + steps: + - uses: actions/checkout@v2 + - name: install Clang + run: curl -fsSL -o LLVM11.exe https://github.com/llvm/llvm-project/releases/download/llvmorg-11.0.0/LLVM-11.0.0-win64.exe ; 7z x LLVM11.exe -y -o"C:/Program Files/LLVM" + - name: cmake + run: cmake -S . -B build -DCMAKE_CXX_COMPILER="C:/Program Files/LLVM/bin/clang++.exe" -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On + - name: build + run: cmake --build build --parallel 10 + - name: test + run: cd build ; ctest -j 10 -C Debug --exclude-regex "test-unicode" --output-on-failure + clang-cl-10-x64: runs-on: windows-latest diff --git a/appveyor.yml b/appveyor.yml index 5836f88d0e..d525c3a549 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -34,24 +34,6 @@ environment: CMAKE_OPTIONS: "" GENERATOR: Visual Studio 16 2019 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 - configuration: Debug - COMPILER: mingw - platform: x86 - CXX_FLAGS: "" - LINKER_FLAGS: "" - CMAKE_OPTIONS: "" - GENERATOR: Ninja - - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 - configuration: Release - COMPILER: mingw - platform: x86 - CXX_FLAGS: "" - LINKER_FLAGS: "" - CMAKE_OPTIONS: "" - GENERATOR: Ninja - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 configuration: Release platform: x86 @@ -85,14 +67,6 @@ environment: CMAKE_OPTIONS: "-DJSON_ImplicitConversions=OFF" GENERATOR: Visual Studio 16 2019 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 - configuration: Release - platform: x64 - CXX_FLAGS: "" - LINKER_FLAGS: "" - CMAKE_OPTIONS: "" - GENERATOR: Visual Studio 16 2019 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 configuration: Release platform: x64 @@ -122,18 +96,13 @@ init: - msbuild /version install: - - if "%COMPILER%"=="mingw" appveyor DownloadFile https://github.com/ninja-build/ninja/releases/download/v1.6.0/ninja-win.zip -FileName ninja.zip - - if "%COMPILER%"=="mingw" 7z x ninja.zip -oC:\projects\deps\ninja > nul - - if "%COMPILER%"=="mingw" set PATH=C:\projects\deps\ninja;%PATH% - - if "%COMPILER%"=="mingw" set PATH=C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64\bin;%PATH% - - if "%COMPILER%"=="mingw" g++ --version - if "%platform%"=="x86" set GENERATOR_PLATFORM=Win32 before_build: # for with_win_header build, inject the inclusion of Windows.h to the single-header library - ps: if ($env:name -Eq "with_win_header") { $header_path = "single_include\nlohmann\json.hpp" } - ps: if ($env:name -Eq "with_win_header") { "#include `n" + (Get-Content $header_path | Out-String) | Set-Content $header_path } - - if "%GENERATOR%"=="Ninja" (cmake . -G "%GENERATOR%" -DCMAKE_BUILD_TYPE="%configuration%" -DCMAKE_CXX_FLAGS="%CXX_FLAGS%" -DCMAKE_EXE_LINKER_FLAGS="%LINKER_FLAGS%" -DCMAKE_IGNORE_PATH="C:/Program Files/Git/usr/bin" -DJSON_BuildTests=On "%CMAKE_OPTIONS%") else (cmake . -G "%GENERATOR%" -A "%GENERATOR_PLATFORM%" -DCMAKE_CXX_FLAGS="%CXX_FLAGS%" -DCMAKE_EXE_LINKER_FLAGS="%LINKER_FLAGS%" -DCMAKE_IGNORE_PATH="C:/Program Files/Git/usr/bin" -DJSON_BuildTests=On "%CMAKE_OPTIONS%") + - cmake . -G "%GENERATOR%" -A "%GENERATOR_PLATFORM%" -DCMAKE_CXX_FLAGS="%CXX_FLAGS%" -DCMAKE_EXE_LINKER_FLAGS="%LINKER_FLAGS%" -DCMAKE_IGNORE_PATH="C:/Program Files/Git/usr/bin" -DJSON_BuildTests=On "%CMAKE_OPTIONS%" build_script: - cmake --build . --config "%configuration%" From 1f1fa9f30eb4cc76b6d3f1b0278e050ebec6d5b1 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 27 Mar 2021 14:44:39 +0100 Subject: [PATCH 077/143] :fire: remove some Travis builds --- .github/workflows/ubuntu.yml | 117 ++++++----------------------- .travis.yml | 142 ----------------------------------- cmake/ci.cmake | 24 +++++- test/CMakeLists.txt | 16 +++- 4 files changed, 57 insertions(+), 242 deletions(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index d09b640fe4..a61f0dd75e 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -17,27 +17,6 @@ jobs: - name: build run: cmake --build build --target ci_test_clang_cxx20 - ci_clang_analyze: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: install_ninja - run: | - sudo apt update - sudo apt install ninja-build - shell: bash - - name: install_clang - run: | - wget https://apt.llvm.org/llvm.sh - chmod +x llvm.sh - sudo ./llvm.sh 11 - sudo apt-get install clang-tools-11 - shell: bash - - name: cmake - run: cmake -S . -B build -DJSON_CI=On - - name: build - run: cmake --build build --target ci_clang_analyze - ci_test_clang: runs-on: ubuntu-latest steps: @@ -69,94 +48,31 @@ jobs: - name: build run: cmake --build build --target ci_test_gcc - ci_test_valgrind: - runs-on: ubuntu-latest - container: nlohmann/json-ci:latest - steps: - - uses: actions/checkout@v2 - - name: cmake - run: cmake -S . -B build -DJSON_CI=On - - name: build - run: cmake --build build --target ci_test_valgrind - - ci_cppcheck: - runs-on: ubuntu-latest - container: nlohmann/json-ci:latest - steps: - - uses: actions/checkout@v2 - - name: cmake - run: cmake -S . -B build -DJSON_CI=On - - name: build - run: cmake --build build --target ci_cppcheck - - ci_cpplint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: cmake - run: cmake -S . -B build -DJSON_CI=On - - name: build - run: cmake --build build --target ci_cpplint - - ci_clang_tidy: + ci_static_analysis: runs-on: ubuntu-latest container: nlohmann/json-ci:latest + strategy: + matrix: + target: [ci_clang_tidy, ci_cppcheck, ci_test_valgrind, ci_test_clang_sanitizer, ci_test_amalgamation, ci_clang_analyze, ci_cpplint] steps: - uses: actions/checkout@v2 - name: cmake run: cmake -S . -B build -DJSON_CI=On - name: build - run: cmake --build build --target ci_clang_tidy + run: cmake --build build --target ${{ matrix.target }} - ci_test_amalgamation: + ci_cmake_options: runs-on: ubuntu-latest container: nlohmann/json-ci:latest + strategy: + matrix: + target: [ci_test_diagnostics, ci_test_noexceptions, ci_test_noimplicitconversions] steps: - uses: actions/checkout@v2 - name: cmake run: cmake -S . -B build -DJSON_CI=On - name: build - run: cmake --build build --target ci_test_amalgamation - - ci_test_diagnostics: - runs-on: ubuntu-latest - container: nlohmann/json-ci:latest - steps: - - uses: actions/checkout@v2 - - name: cmake - run: cmake -S . -B build -DJSON_CI=On - - name: build - run: cmake --build build --target ci_test_diagnostics - - ci_test_noexceptions: - runs-on: ubuntu-latest - container: nlohmann/json-ci:latest - steps: - - uses: actions/checkout@v2 - - name: cmake - run: cmake -S . -B build -DJSON_CI=On - - name: build - run: cmake --build build --target ci_test_noexceptions - - ci_test_noimplicitconversions: - runs-on: ubuntu-latest - container: nlohmann/json-ci:latest - steps: - - uses: actions/checkout@v2 - - name: cmake - run: cmake -S . -B build -DJSON_CI=On - - name: build - run: cmake --build build --target ci_test_noimplicitconversions - - ci_test_clang_sanitizer: - runs-on: ubuntu-latest - container: nlohmann/json-ci:latest - steps: - - uses: actions/checkout@v2 - - name: cmake - run: cmake -S . -B build -DJSON_CI=On - - name: build - run: cmake --build build --target ci_test_clang_sanitizer + run: cmake --build build --target ${{ matrix.target }} ci_test_coverage: runs-on: ubuntu-latest @@ -177,3 +93,16 @@ jobs: with: github-token: ${{ secrets.GITHUB_TOKEN }} path-to-lcov: /__w/json/json/build/json.info.filtered.noexcept + + ci_test_compilers: + runs-on: ubuntu-latest + container: nlohmann/json-ci:latest + strategy: + matrix: + compiler: [g++-4.8, g++-4.9, g++-5, g++-7, g++-8, g++-9, g++-10, clang++-3.5, clang++-3.6, clang++-3.7, clang++-3.8, clang++-3.9, clang++-4.0, clang++-5.0, clang++-6.0, clang++-7, clang++-8, clang++-9, clang++-10, clang++-11] + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_test_compiler_${{ matrix.compiler }} diff --git a/.travis.yml b/.travis.yml index c5208b31f9..bdfbe722be 100644 --- a/.travis.yml +++ b/.travis.yml @@ -54,30 +54,6 @@ matrix: # Linux / GCC - - os: linux - compiler: gcc - env: COMPILER=g++-4.8 - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-4.8', 'ninja-build'] - - - os: linux - compiler: gcc - env: COMPILER=g++-4.9 - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-4.9', 'ninja-build'] - - - os: linux - compiler: gcc - env: COMPILER=g++-5 - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-5', 'ninja-build'] - - os: linux compiler: gcc env: COMPILER=g++-6 @@ -86,124 +62,6 @@ matrix: sources: ['ubuntu-toolchain-r-test'] packages: ['g++-6', 'ninja-build'] - - os: linux - compiler: gcc - env: COMPILER=g++-7 - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-7', 'ninja-build'] - - - os: linux - compiler: gcc - env: COMPILER=g++-8 - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-8', 'ninja-build'] - - - os: linux - compiler: gcc - env: COMPILER=g++-9 - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-9', 'ninja-build'] - - - os: linux - compiler: gcc - env: - - COMPILER=g++-9 - - CXX_STANDARD=17 - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-9', 'ninja-build'] - - # Linux / Clang - - - os: linux - compiler: clang - env: COMPILER=clang++-3.5 - addons: - apt: - sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.5'] - packages: ['g++-6', 'clang-3.5', 'ninja-build'] - - - os: linux - compiler: clang - env: COMPILER=clang++-3.6 - addons: - apt: - sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.6'] - packages: ['g++-6', 'clang-3.6', 'ninja-build'] - - - os: linux - compiler: clang - env: COMPILER=clang++-3.7 - addons: - apt: - sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.7'] - packages: ['g++-6', 'clang-3.7', 'ninja-build'] - - - os: linux - compiler: clang - env: COMPILER=clang++-3.8 - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-6', 'clang-3.8', 'ninja-build'] - - - os: linux - compiler: clang - env: COMPILER=clang++-3.9 - addons: - apt: - sources: ['ubuntu-toolchain-r-test'] - packages: ['g++-6', 'clang-3.9', 'ninja-build'] - - - os: linux - compiler: clang - env: COMPILER=clang++-4.0 - addons: - apt: - sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-4.0'] - packages: ['g++-6', 'clang-4.0', 'ninja-build'] - - - os: linux - compiler: clang - env: COMPILER=clang++-5.0 - addons: - apt: - sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-5.0'] - packages: ['g++-6', 'clang-5.0', 'ninja-build'] - - - os: linux - compiler: clang - env: COMPILER=clang++-6.0 - addons: - apt: - sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-6.0'] - packages: ['g++-6', 'clang-6.0', 'ninja-build'] - - - os: linux - compiler: clang - env: COMPILER=clang++-7 - addons: - apt: - sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-7'] - packages: ['g++-6', 'clang-7', 'ninja-build'] - - - os: linux - compiler: clang - env: - - COMPILER=clang++-7 - - CXX_STANDARD=17 - addons: - apt: - sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-7'] - packages: ['g++-7', 'clang-7', 'ninja-build'] - ################ # build script # ################ diff --git a/cmake/ci.cmake b/cmake/ci.cmake index 4e0d74b12e..b167cc93b4 100644 --- a/cmake/ci.cmake +++ b/cmake/ci.cmake @@ -30,7 +30,7 @@ execute_process(COMMAND ${CPPCHECK_TOOL} --version OUTPUT_VARIABLE CPPCHECK_TOOL string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" CPPCHECK_TOOL_VERSION "${CPPCHECK_TOOL_VERSION}") message(STATUS "🔖 Cppcheck ${CPPCHECK_TOOL_VERSION} (${CPPCHECK_TOOL})") -find_program(GCC_TOOL NAMES g++-HEAD g++-11 g++-10 g++) +find_program(GCC_TOOL NAMES g++-HEAD g++-11 g++-latest) execute_process(COMMAND ${GCC_TOOL} --version OUTPUT_VARIABLE GCC_TOOL_VERSION ERROR_VARIABLE GCC_TOOL_VERSION) string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" GCC_TOOL_VERSION "${GCC_TOOL_VERSION}") message(STATUS "🔖 GCC ${GCC_TOOL_VERSION} (${GCC_TOOL})") @@ -85,7 +85,7 @@ find_program(SCAN_BUILD_TOOL NAMES scan-build-11 scan-build) file(GLOB_RECURSE SRC_FILES ${PROJECT_SOURCE_DIR}/include/nlohmann/*.hpp) ############################################################################### -# Different C++ Standards. +# Thorough check with recent compilers ############################################################################### set(CLANG_CXXFLAGS "-std=c++11 \ @@ -784,6 +784,26 @@ add_custom_target(ci_cmake_flags COMMENT "Check CMake flags" ) +############################################################################### +# Use more installed compilers. +############################################################################### + +foreach(COMPILER g++-4.8 g++-4.9 g++-5 g++-7 g++-8 g++-9 g++-10 clang++-3.5 clang++-3.6 clang++-3.7 clang++-3.8 clang++-3.9 clang++-4.0 clang++-5.0 clang++-6.0 clang++-7 clang++-8 clang++-9 clang++-10 clang++-11) + find_program(COMPILER_TOOL NAMES ${COMPILER}) + if (COMPILER_TOOL) + add_custom_target(ci_test_compiler_${COMPILER} + COMMAND CXX=${COMPILER} ${CMAKE_COMMAND} + -DCMAKE_BUILD_TYPE=Debug -GNinja + -DJSON_BuildTests=ON -DJSON_FastTests=ON + -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_compiler_${COMPILER} + COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_compiler_${COMPILER} + COMMAND cd ${PROJECT_BINARY_DIR}/build_compiler_${COMPILER} && ${CMAKE_CTEST_COMMAND} --parallel ${N} --exclude-regex "test-unicode" --output-on-failure + COMMENT "Compile and test with ${COMPILER}" + ) + endif() + unset(COMPILER_TOOL CACHE) +endforeach() + ############################################################################### # Clean up all generated files. ############################################################################### diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 15f55dfa5d..d8c5864251 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,6 +2,7 @@ option(JSON_Sanitizer "Build test suite with Clang sanitizer" OFF) option(JSON_Valgrind "Execute test suite with Valgrind" OFF) option(JSON_NoExceptions "Build test suite without exceptions" OFF) option(JSON_Coverage "Build test suite with coverage information" OFF) +option(JSON_FastTests "Whether to skip expensive tests" OFF) # download test data include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/download_test_data.cmake) @@ -112,10 +113,17 @@ foreach(file ${files}) target_link_libraries(${testcase} PRIVATE --coverage) endif() - add_test(NAME "${testcase}" - COMMAND ${testcase} ${DOCTEST_TEST_FILTER} --no-skip - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - ) + if (JSON_FastTests) + add_test(NAME "${testcase}" + COMMAND ${testcase} ${DOCTEST_TEST_FILTER} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + ) + else() + add_test(NAME "${testcase}" + COMMAND ${testcase} ${DOCTEST_TEST_FILTER} --no-skip + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + ) + endif() set_tests_properties("${testcase}" PROPERTIES LABELS "all" FIXTURES_REQUIRED TEST_DATA) if(JSON_Valgrind) From f268035946341113eec63ebed33fedaaecb357d8 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 28 Mar 2021 13:37:04 +0200 Subject: [PATCH 078/143] :construction_worker: consolidate CI --- .github/workflows/macos.yml | 24 ++++--- .github/workflows/ubuntu.yml | 4 +- .github/workflows/windows.yml | 120 ++++++++++++---------------------- .travis.yml | 6 -- README.md | 74 +++++++++++++-------- appveyor.yml | 32 --------- 6 files changed, 102 insertions(+), 158 deletions(-) diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index aad3a787de..1a3acbb7ac 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -3,15 +3,19 @@ name: macOS on: [push, pull_request] jobs: - build: - - runs-on: macos-latest + xcode: + runs-on: macos-10.15 + strategy: + matrix: + xcode: [12.4, 12.3, 12.2, 12.1.1, 12.1, 12, 11.7, 11.6, 11.5, 11.4.1, 11.3.1, 11.2.1, 10.3] + env: + DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer steps: - - uses: actions/checkout@v2 - - name: cmake - run: cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On - - name: build - run: cmake --build build --parallel 10 - - name: test - run: cd build ; ctest -j 10 --output-on-failure + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On -DJSON_FastTests=ON + - name: build + run: cmake --build build --parallel 10 + - name: test + run: cd build ; ctest -j 10 --output-on-failure diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index a61f0dd75e..5cc0c87c0d 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -99,10 +99,10 @@ jobs: container: nlohmann/json-ci:latest strategy: matrix: - compiler: [g++-4.8, g++-4.9, g++-5, g++-7, g++-8, g++-9, g++-10, clang++-3.5, clang++-3.6, clang++-3.7, clang++-3.8, clang++-3.9, clang++-4.0, clang++-5.0, clang++-6.0, clang++-7, clang++-8, clang++-9, clang++-10, clang++-11] + compiler: [g++-4.8, g++-4.9, g++-5, g++-7, g++-8, g++-9, g++-10, clang++-3.5, clang++-3.6, clang++-3.7, clang++-3.8, clang++-3.9, clang++-4.0, clang++-5.0, clang++-6.0, clang++-7, clang++-8, clang++-9, clang++-10] steps: - uses: actions/checkout@v2 - name: cmake run: cmake -S . -B build -DJSON_CI=On - name: build - run: cmake --build build --target ci_test_compiler_${{ matrix.compiler }} + run: cmake --build build --target ci_test_compiler_${{ matrix.compiler }} \ No newline at end of file diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index d88de6246c..6ebe431b37 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -5,29 +5,16 @@ on: [push, pull_request] jobs: mingw: runs-on: windows-latest + strategy: + matrix: + architecture: [x64, x86] steps: - uses: actions/checkout@v2 - name: Set up MinGW uses: egor-tensin/setup-mingw@v2 with: - platform: x64 - - name: cmake - run: cmake -S . -B build -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On - - name: build - run: cmake --build build --parallel 10 - - name: test - run: cd build ; ctest -j 10 -C Debug --output-on-failure - - mingw_win32: - runs-on: windows-latest - - steps: - - uses: actions/checkout@v2 - - name: Set up MinGW - uses: egor-tensin/setup-mingw@v2 - with: - platform: x86 + platform: ${{ matrix.architecture }} - name: cmake run: cmake -S . -B build -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On - name: build @@ -37,35 +24,34 @@ jobs: msvc2017: runs-on: windows-2016 + strategy: + matrix: + build_type: [Debug, Release] + architecture: [Win32, x64] steps: - uses: actions/checkout@v2 - name: cmake - run: cmake -S . -B build -G "Visual Studio 15 2017" -DJSON_BuildTests=On - - name: build - run: cmake --build build --config Release --parallel 10 - - name: test - run: cd build ; ctest -j 10 -C Release --output-on-failure - - msvc2017_latest: - runs-on: windows-2016 - - steps: - - uses: actions/checkout@v2 + run: cmake -S . -B build -G "Visual Studio 15 2017" -A ${{ matrix.architecture }} -DJSON_BuildTests=On -DCMAKE_EXE_LINKER_FLAGS="/STACK:4000000" + if: matrix.build_type == 'Release' && matrix.architecture == 'x64' - name: cmake - run: cmake -S . -B build -G "Visual Studio 15 2017" -DJSON_BuildTests=On -DCMAKE_CXX_FLAGS="/permissive- /std:c++latest /utf-8" + run: cmake -S . -B build -G "Visual Studio 15 2017" -A ${{ matrix.architecture }} -DJSON_BuildTests=On + if: matrix.build_type == 'Release' && matrix.architecture != 'x64' + - name: cmake + run: cmake -S . -B build -G "Visual Studio 15 2017" -A ${{ matrix.architecture }} -DJSON_BuildTests=On -DJSON_FastTests=ON + if: matrix.build_type == 'Debug' - name: build - run: cmake --build build --config Release --parallel 10 + run: cmake --build build --config ${{ matrix.build_type }} --parallel 10 - name: test - run: cd build ; ctest -j 10 -C Release --output-on-failure + run: cd build ; ctest -j 10 -C ${{ matrix.build_type }} --output-on-failure - msvc2017_win32: + msvc2017_latest: runs-on: windows-2016 steps: - uses: actions/checkout@v2 - name: cmake - run: cmake -S . -B build -G "Visual Studio 15 2017" -A "Win32" -DJSON_BuildTests=On + run: cmake -S . -B build -G "Visual Studio 15 2017" -DJSON_BuildTests=On -DCMAKE_CXX_FLAGS="/permissive- /std:c++latest /utf-8 /W4" - name: build run: cmake --build build --config Release --parallel 10 - name: test @@ -73,47 +59,46 @@ jobs: msvc2019: runs-on: windows-latest + strategy: + matrix: + build_type: [Debug, Release] + architecture: [Win32, x64] steps: - uses: actions/checkout@v2 - name: cmake - run: cmake -S . -B build -G "Visual Studio 16 2019" -DJSON_BuildTests=On - - name: build - run: cmake --build build --config Release --parallel 10 - - name: test - run: cd build ; ctest -j 10 -C Release --output-on-failure - - msvc2019_latest: - runs-on: windows-latest - - steps: - - uses: actions/checkout@v2 + run: cmake -S . -B build -G "Visual Studio 16 2019" -A ${{ matrix.architecture }} -DJSON_BuildTests=On + if: matrix.build_type == 'Release' - name: cmake - run: cmake -S . -B build -G "Visual Studio 16 2019" -DJSON_BuildTests=On -DCMAKE_CXX_FLAGS="/permissive- /std:c++latest /utf-8" + run: cmake -S . -B build -G "Visual Studio 16 2019" -A ${{ matrix.architecture }} -DJSON_BuildTests=On -DJSON_FastTests=ON + if: matrix.build_type == 'Debug' - name: build - run: cmake --build build --config Release --parallel 10 + run: cmake --build build --config ${{ matrix.build_type }} --parallel 10 - name: test - run: cd build ; ctest -j 10 -C Release --output-on-failure + run: cd build ; ctest -j 10 -C ${{ matrix.build_type }} --output-on-failure - msvc2019_win32: + msvc2019_latest: runs-on: windows-latest steps: - uses: actions/checkout@v2 - name: cmake - run: cmake -S . -B build -G "Visual Studio 16 2019" -A "Win32" -DJSON_BuildTests=On + run: cmake -S . -B build -G "Visual Studio 16 2019" -DJSON_BuildTests=On -DCMAKE_CXX_FLAGS="/permissive- /std:c++latest /utf-8 /W4" - name: build run: cmake --build build --config Release --parallel 10 - name: test run: cd build ; ctest -j 10 -C Release --output-on-failure - clang10: + clang: runs-on: windows-latest + strategy: + matrix: + version: [10, 11] steps: - uses: actions/checkout@v2 - name: install Clang - run: curl -fsSL -o LLVM10.exe https://github.com/llvm/llvm-project/releases/download/llvmorg-10.0.0/LLVM-10.0.0-win64.exe ; 7z x LLVM10.exe -y -o"C:/Program Files/LLVM" + run: curl -fsSL -o LLVM${{ matrix.version }}.exe https://github.com/llvm/llvm-project/releases/download/llvmorg-${{ matrix.version }}.0.0/LLVM-${{ matrix.version }}.0.0-win64.exe ; 7z x LLVM${{ matrix.version }}.exe -y -o"C:/Program Files/LLVM" - name: cmake run: cmake -S . -B build -DCMAKE_CXX_COMPILER="C:/Program Files/LLVM/bin/clang++.exe" -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On - name: build @@ -121,39 +106,16 @@ jobs: - name: test run: cd build ; ctest -j 10 -C Debug --exclude-regex "test-unicode" --output-on-failure - clang11: - runs-on: windows-latest - - steps: - - uses: actions/checkout@v2 - - name: install Clang - run: curl -fsSL -o LLVM11.exe https://github.com/llvm/llvm-project/releases/download/llvmorg-11.0.0/LLVM-11.0.0-win64.exe ; 7z x LLVM11.exe -y -o"C:/Program Files/LLVM" - - name: cmake - run: cmake -S . -B build -DCMAKE_CXX_COMPILER="C:/Program Files/LLVM/bin/clang++.exe" -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On - - name: build - run: cmake --build build --parallel 10 - - name: test - run: cd build ; ctest -j 10 -C Debug --exclude-regex "test-unicode" --output-on-failure - - clang-cl-10-x64: - runs-on: windows-latest - - steps: - - uses: actions/checkout@v2 - - name: cmake - run: cmake -S . -B build -G "Visual Studio 16 2019" -A x64 -T ClangCL -DJSON_BuildTests=On - - name: build - run: cmake --build build --config Debug --parallel 10 - - name: test - run: cd build ; ctest -j 10 -C Debug --exclude-regex "test-unicode" --output-on-failure - - clang-cl-10-x86: + clang-cl-11: runs-on: windows-latest + strategy: + matrix: + architecture: [Win32, x64] steps: - uses: actions/checkout@v2 - name: cmake - run: cmake -S . -B build -G "Visual Studio 16 2019" -A Win32 -T ClangCL -DJSON_BuildTests=On + run: cmake -S . -B build -G "Visual Studio 16 2019" -A ${{ matrix.architecture }} -T ClangCL -DJSON_BuildTests=On - name: build run: cmake --build build --config Debug --parallel 10 - name: test diff --git a/.travis.yml b/.travis.yml index bdfbe722be..e20b4465d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,12 +46,6 @@ matrix: - os: osx osx_image: xcode10.2 - - os: osx - osx_image: xcode11.2 - - - os: osx - osx_image: xcode12 - # Linux / GCC - os: linux diff --git a/README.md b/README.md index 6d0153c091..81d3a30169 100644 --- a/README.md +++ b/README.md @@ -1232,35 +1232,51 @@ The following compilers are currently used in continuous integration at [Travis] | Compiler | Operating System | CI Provider | |-------------------------------------------------------------------|--------------------|----------------| | Apple Clang 10.0.1 (clang-1001.0.46.4); Xcode 10.2.1 | macOS 10.14.4 | Travis | -| Apple Clang 11.0.0 (clang-1100.0.33.12); Xcode 11.2.1 | macOS 10.14.6 | Travis | -| Apple Clang 11.0.3 (clang-1103.0.32.59); Xcode 11.4.1 | macOS 10.15.4 | GitHub Actions | -| Apple Clang 12.0.0 (clang-1200.0.22.7); Xcode 11.4.1 | macOS 10.15.5 | Travis | -| Clang 3.5.0 (3.5.0-4ubuntu2\~trusty2) | Ubuntu 14.04.5 LTS | Travis | -| Clang 3.6.2 (3.6.2-svn240577-1\~exp1) | Ubuntu 14.04.5 LTS | Travis | -| Clang 3.7.1 (3.7.1-svn253571-1\~exp1) | Ubuntu 14.04.5 LTS | Travis | -| Clang 3.8.0 (3.8.0-2ubuntu3\~trusty5) | Ubuntu 14.04.5 LTS | Travis | -| Clang 3.9.1 (3.9.1-4ubuntu3\~14.04.3) | Ubuntu 14.04.5 LTS | Travis | -| Clang 4.0.1 (4.0.1-svn305264-1\~exp1) | Ubuntu 14.04.5 LTS | Travis | -| Clang 5.0.2 (version 5.0.2-svn328729-1\~exp1\~20180509123505.100) | Ubuntu 14.04.5 LTS | Travis | -| Clang 6.0.1 (6.0.1-svn334776-1\~exp1\~20190309042707.121) | Ubuntu 14.04.5 LTS | Travis | -| Clang 7.1.0 (7.1.0-svn353565-1\~exp1\~20190419134007.64) | Ubuntu 14.04.5 LTS | Travis | -| Clang 7.5.0 (Ubuntu 7.5.0-3ubuntu1\~18.04) | Ubuntu 18.04.4 LTS | Travis | -| Clang 9.0.0 (x86_64-pc-windows-msvc) | Windows-10.0.17763 | GitHub Actions | -| Clang 10.0.0 (x86_64-pc-windows-msvc) | Windows-10.0.17763 | GitHub Actions | -| GCC 4.8.5 (Ubuntu 4.8.5-4ubuntu8\~14.04.2) | Ubuntu 14.04.5 LTS | Travis | -| GCC 4.9.4 (Ubuntu 4.9.4-2ubuntu1\~14.04.1) | Ubuntu 14.04.5 LTS | Travis | -| GCC 5.5.0 (Ubuntu 5.5.0-12ubuntu1\~14.04) | Ubuntu 14.04.5 LTS | Travis | -| GCC 6.5.0 (Ubuntu 6.5.0-2ubuntu1\~14.04.1) | Ubuntu 14.04.5 LTS | Travis | -| GCC 7.3.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project) | Windows-6.3.9600 | AppVeyor | -| GCC 7.5.0 (Ubuntu 7.5.0-3ubuntu1\~14.04.1) | Ubuntu 14.04.5 LTS | Travis | -| GCC 7.5.0 (Ubuntu 7.5.0-3ubuntu1\~18.04) | Ubuntu 18.04.4 LTS | GitHub Actions | -| GCC 8.4.0 (Ubuntu 8.4.0-1ubuntu1\~14.04) | Ubuntu 14.04.5 LTS | Travis | -| GCC 9.3.0 (Ubuntu 9.3.0-11ubuntu0\~14.04) | Ubuntu 14.04.5 LTS | Travis | -| MSVC 19.0.24241.7 (Build Engine version 14.0.25420.1) | Windows-6.3.9600 | AppVeyor | -| MSVC 19.16.27035.0 (15.9.21+g9802d43bc3 for .NET Framework) | Windows-10.0.14393 | AppVeyor | -| MSVC 19.25.28614.0 (Build Engine version 16.5.0+d4cbfca49 for .NET Framework) | Windows-10.0.17763 | AppVeyor | -| MSVC 19.25.28614.0 (Build Engine version 16.5.0+d4cbfca49 for .NET Framework) | Windows-10.0.17763 | GitHub Actions | -| MSVC 19.25.28614.0 (Build Engine version 16.5.0+d4cbfca49 for .NET Framework) with ClangCL 10.0.0 | Windows-10.0.17763 | GitHub Actions | +| Apple Clang 10.0.1 (clang-1001.0.46.4); Xcode 10.3 | macOS 10.15.7 | GitHub Actions | +| Apple Clang 11.0.0 (clang-1100.0.33.12); Xcode 11.2.1 | macOS 10.15.7 | GitHub Actions | +| Apple Clang 11.0.0 (clang-1100.0.33.17); Xcode 11.3.1 | macOS 10.15.7 | GitHub Actions | +| Apple Clang 11.0.3 (clang-1103.0.32.59); Xcode 11.4.1 | macOS 10.15.7 | GitHub Actions | +| Apple Clang 11.0.3 (clang-1103.0.32.62); Xcode 11.5 | macOS 10.15.7 | GitHub Actions | +| Apple Clang 11.0.3 (clang-1103.0.32.62); Xcode 11.6 | macOS 10.15.7 | GitHub Actions | +| Apple Clang 11.0.3 (clang-1103.0.32.62); Xcode 11.7 | macOS 10.15.7 | GitHub Actions | +| Apple Clang 12.0.0 (clang-1200.0.32.2); Xcode 12 | macOS 10.15.7 | GitHub Actions | +| Apple Clang 12.0.0 (clang-1200.0.32.21); Xcode 12.1 | macOS 10.15.7 | GitHub Actions | +| Apple Clang 12.0.0 (clang-1200.0.32.21); Xcode 12.1.1 | macOS 10.15.7 | GitHub Actions | +| Apple Clang 12.0.0 (clang-1200.0.32.27); Xcode 12.2 | macOS 10.15.7 | GitHub Actions | +| Apple Clang 12.0.0 (clang-1200.0.32.28); Xcode 12.3 | macOS 10.15.7 | GitHub Actions | +| Apple Clang 12.0.0 (clang-1200.0.32.29); Xcode 12.4 | macOS 10.15.7 | GitHub Actions | +| GCC 4.8.5 (Ubuntu 4.8.5-4ubuntu2) | Ubuntu 20.04.2 LTS | GitHub Actions | +| GCC 4.9.3 (Ubuntu 4.9.3-13ubuntu2) | Ubuntu 20.04.2 LTS | GitHub Actions | +| GCC 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.12) | Ubuntu 20.04.2 LTS | GitHub Actions | +| GCC 6.5.0 (Ubuntu 6.5.0-2ubuntu1~14.04.1) | Ubuntu 14.04.5 LTS | Travis | +| GCC 7.5.0 (Ubuntu 7.5.0-6ubuntu2) | Ubuntu 20.04.2 LTS | GitHub Actions | +| GCC 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project) | Windows-10.0.17763 | GitHub Actions | +| GCC 8.1.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project) | Windows-10.0.17763 | GitHub Actions | +| GCC 8.4.0 (Ubuntu 8.4.0-3ubuntu2) | Ubuntu 20.04.2 LTS | GitHub Actions | +| GCC 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) | Ubuntu 20.04.2 LTS | GitHub Actions | +| GCC 10.2.0 (Ubuntu 10.2.0-5ubuntu1~20.04) | Ubuntu 20.04.2 LTS | GitHub Actions | +| GCC 11.0.1 20210321 (experimental) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 3.5.2 (3.5.2-3ubuntu1) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 3.6.2 (3.6.2-3ubuntu2) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 3.7.1 (3.7.1-2ubuntu2) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 3.8.0 (3.8.0-2ubuntu4) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 3.9.1 (3.9.1-4ubuntu3\~16.04.2) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 4.0.0 (4.0.0-1ubuntu1\~16.04.2) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 5.0.0 (5.0.0-3\~16.04.1) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 6.0.1 (6.0.1-14) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 7.0.1 (7.0.1-12) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 8.0.1 (8.0.1-9) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 9.0.1 (9.0.1-12) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 10.0.0 (10.0.0-4ubuntu1) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 10.0.0 with GNU-like command-line | Windows-10.0.17763 | GitHub Actions | +| Clang 11.0.0 with GNU-like command-line | Windows-10.0.17763 | GitHub Actions | +| Clang 11.0.0 with MSVC-like command-line | Windows-10.0.17763 | GitHub Actions | +| Clang 11.1.0 (11.1.0-++20210204121720+1fdec59bffc1-1~exp1~20210203232336.162 | Ubuntu 20.04.2 LTS | GitHub Actions | +| Visual Studio 14 2015 MSVC 19.0.24241.7 (Build Engine version 14.0.25420.1) | Windows-6.3.9600 | AppVeyor | +| Visual Studio 15 2017 MSVC 19.16.27035.0 (Build Engine version 15.9.21+g9802d43bc3 for .NET Framework) | Windows-10.0.14393 | AppVeyor | +| Visual Studio 15 2017 MSVC 19.16.27045.0 (Build Engine version 15.9.21+g9802d43bc3 for .NET Framework) | Windows-10.0.14393 | GitHub Actions | +| Visual Studio 16 2019 MSVC 19.28.29912.0 (Build Engine version 16.9.0+57a23d249 for .NET Framework) | Windows-10.0.17763 | GitHub Actions | +| Visual Studio 16 2019 MSVC 19.28.29912.0 (Build Engine version 16.9.0+57a23d249 for .NET Framework) | Windows-10.0.17763 | AppVeyor | ## License diff --git a/appveyor.yml b/appveyor.yml index d525c3a549..4dba417c63 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -10,30 +10,6 @@ environment: CMAKE_OPTIONS: "" GENERATOR: Visual Studio 14 2015 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 - configuration: Debug - platform: x86 - CXX_FLAGS: "" - LINKER_FLAGS: "" - CMAKE_OPTIONS: "" - GENERATOR: Visual Studio 15 2017 - - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 - configuration: Debug - platform: x86 - CXX_FLAGS: "" - LINKER_FLAGS: "" - CMAKE_OPTIONS: "" - GENERATOR: Visual Studio 16 2019 - - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 - configuration: Debug - platform: x64 - CXX_FLAGS: "" - LINKER_FLAGS: "" - CMAKE_OPTIONS: "" - GENERATOR: Visual Studio 16 2019 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 configuration: Release platform: x86 @@ -83,14 +59,6 @@ environment: CMAKE_OPTIONS: "" GENERATOR: Visual Studio 15 2017 - - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 - configuration: Release - platform: x64 - CXX_FLAGS: "" - LINKER_FLAGS: "" - CMAKE_OPTIONS: "" - GENERATOR: Visual Studio 16 2019 - init: - cmake --version - msbuild /version From ae99a31bea5638b08231685fb1414063e73af637 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 28 Mar 2021 14:48:58 +0200 Subject: [PATCH 079/143] :construction: add more CI tests --- .github/workflows/ubuntu.yml | 32 ++++++++++++++++---------------- cmake/ci.cmake | 8 ++++---- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 5cc0c87c0d..d79968983d 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -3,20 +3,6 @@ name: Ubuntu on: [push, pull_request] jobs: - ci_test_clang_cxx20: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: install_clang - run: | - sudo apt update - sudo apt install clang-10 ninja-build - shell: bash - - name: cmake - run: cmake -S . -B build -DJSON_CI=On - - name: build - run: cmake --build build --target ci_test_clang_cxx20 - ci_test_clang: runs-on: ubuntu-latest steps: @@ -53,7 +39,7 @@ jobs: container: nlohmann/json-ci:latest strategy: matrix: - target: [ci_clang_tidy, ci_cppcheck, ci_test_valgrind, ci_test_clang_sanitizer, ci_test_amalgamation, ci_clang_analyze, ci_cpplint] + target: [ci_clang_tidy, ci_cppcheck, ci_test_valgrind, ci_test_clang_sanitizer, ci_test_amalgamation, ci_clang_analyze, ci_cpplint, ci_cmake_flags, ci_single_binaries, ci_reproducible_tests, ci_non_git_tests, ci_offline_testdata] steps: - uses: actions/checkout@v2 - name: cmake @@ -105,4 +91,18 @@ jobs: - name: cmake run: cmake -S . -B build -DJSON_CI=On - name: build - run: cmake --build build --target ci_test_compiler_${{ matrix.compiler }} \ No newline at end of file + run: cmake --build build --target ci_test_compiler_${{ matrix.compiler }} + + ci_test_standards: + runs-on: ubuntu-latest + container: nlohmann/json-ci:latest + strategy: + matrix: + standard: [11, 14, 17, 20] + compiler: [gcc, clang] + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -DJSON_CI=On + - name: build + run: cmake --build build --target ci_test_{{ matrix.compiler }}_cxx{{ matrix.standard }} diff --git a/cmake/ci.cmake b/cmake/ci.cmake index b167cc93b4..5807d8f18e 100644 --- a/cmake/ci.cmake +++ b/cmake/ci.cmake @@ -401,7 +401,7 @@ foreach(CXX_STANDARD 11 14 17 20) COMMAND CXX=${GCC_TOOL} ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug -GNinja -DCMAKE_CXX_STANDARD=${CXX_STANDARD} -DCMAKE_CXX_STANDARD_REQUIRED=ON - -DJSON_BuildTests=ON + -DJSON_BuildTests=ON -DJSON_FastTests=ON -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_gcc_cxx${CXX_STANDARD} COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_gcc_cxx${CXX_STANDARD} COMMAND cd ${PROJECT_BINARY_DIR}/build_gcc_cxx${CXX_STANDARD} && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure @@ -650,7 +650,7 @@ add_custom_target(ci_offline_testdata COMMAND cd ${PROJECT_BINARY_DIR}/build_offline_testdata/test_data && ${GIT_TOOL} clone -c advice.detachedHead=false --branch v3.0.0 https://github.com/nlohmann/json_test_data.git --quiet --depth 1 COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug -GNinja - -DJSON_BuildTests=ON -DJSON_TestDataDirectory=${PROJECT_BINARY_DIR}/build_offline_testdata/test_data/json_test_data + -DJSON_BuildTests=ON -DJSON_FastTests=ON -DJSON_TestDataDirectory=${PROJECT_BINARY_DIR}/build_offline_testdata/test_data/json_test_data -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_offline_testdata COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_offline_testdata COMMAND cd ${PROJECT_BINARY_DIR}/build_offline_testdata && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure @@ -666,7 +666,7 @@ add_custom_target(ci_non_git_tests COMMAND cd ${PROJECT_SOURCE_DIR} && for FILE in `${GIT_TOOL} ls-tree --name-only HEAD`\; do cp -r $$FILE ${PROJECT_BINARY_DIR}/build_non_git_tests/sources \; done COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug -GNinja - -DJSON_BuildTests=ON + -DJSON_BuildTests=ON -DJSON_FastTests=ON -S${PROJECT_BINARY_DIR}/build_non_git_tests/sources -B${PROJECT_BINARY_DIR}/build_non_git_tests COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_non_git_tests COMMAND cd ${PROJECT_BINARY_DIR}/build_non_git_tests && ${CMAKE_CTEST_COMMAND} --parallel ${N} -LE git_required --output-on-failure @@ -680,7 +680,7 @@ add_custom_target(ci_non_git_tests add_custom_target(ci_reproducible_tests COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug -GNinja - -DJSON_BuildTests=ON + -DJSON_BuildTests=ON -DJSON_FastTests=ON -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_reproducible_tests COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_reproducible_tests COMMAND cd ${PROJECT_BINARY_DIR}/build_reproducible_tests && ${CMAKE_CTEST_COMMAND} --parallel ${N} -LE not_reproducible --output-on-failure From 077fe41a82c2f1e1d58e2fb523e463654903a542 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 28 Mar 2021 14:59:42 +0200 Subject: [PATCH 080/143] :construction: do not check pushes to non-default branches --- .github/workflows/macos.yml | 7 ++++++- .github/workflows/ubuntu.yml | 7 ++++++- .github/workflows/windows.yml | 7 ++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 1a3acbb7ac..42e9098b56 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -1,6 +1,11 @@ name: macOS -on: [push, pull_request] +on: + push: + branches: + - develop + - master + pull_request: jobs: xcode: diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index d79968983d..b7ba6dc737 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -1,6 +1,11 @@ name: Ubuntu -on: [push, pull_request] +on: + push: + branches: + - develop + - master + pull_request: jobs: ci_test_clang: diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 6ebe431b37..c9f4e3b9b3 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -1,6 +1,11 @@ name: Windows -on: [push, pull_request] +on: + push: + branches: + - develop + - master + pull_request: jobs: mingw: From d927f44576ebbffcef7f2085ace9694cb4cbbd93 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 28 Mar 2021 15:30:37 +0200 Subject: [PATCH 081/143] :green_heart: fix build --- .github/workflows/ubuntu.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index b7ba6dc737..95073fa113 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -110,4 +110,4 @@ jobs: - name: cmake run: cmake -S . -B build -DJSON_CI=On - name: build - run: cmake --build build --target ci_test_{{ matrix.compiler }}_cxx{{ matrix.standard }} + run: cmake --build build --target ci_test_${{ matrix.compiler }}_cxx${{ matrix.standard }} From 53a9850eebb88c6ff95f6042d08d5c0cc9d18097 Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Mon, 29 Mar 2021 21:43:49 +0000 Subject: [PATCH 082/143] pkg-config.pc.in: Don't concatenate paths See: https://github.com/jtojnar/cmake-snips#assuming-cmake_install_dir-is-relative-path --- cmake/pkg-config.pc.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/pkg-config.pc.in b/cmake/pkg-config.pc.in index 3541abf0ba..d36317f09a 100644 --- a/cmake/pkg-config.pc.in +++ b/cmake/pkg-config.pc.in @@ -1,4 +1,4 @@ Name: ${PROJECT_NAME} Description: JSON for Modern C++ Version: ${PROJECT_VERSION} -Cflags: -I${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR} +Cflags: -I${CMAKE_INSTALL_FULL_INCLUDEDIR} From e8dbd7be86e3dbbacd9c8cd4dd8c7a2340217d91 Mon Sep 17 00:00:00 2001 From: raduteo <57729561+raduteo@users.noreply.github.com> Date: Fri, 9 Apr 2021 09:35:58 -0400 Subject: [PATCH 083/143] Update json.hpp Proposed fix for #2706 --- single_include/nlohmann/json.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 4493dd6542..7ef4befb29 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18145,9 +18145,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec })); } JSON_CATCH(...) {} // LCOV_EXCL_LINE -#else - static_cast(check_parents); #endif + static_cast(check_parents); } void set_parents() From 35d79203ecafc0fbb5578c037184990512cf9fff Mon Sep 17 00:00:00 2001 From: raduteo <57729561+raduteo@users.noreply.github.com> Date: Mon, 12 Apr 2021 19:47:02 -0400 Subject: [PATCH 084/143] Update json.hpp --- include/nlohmann/json.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 3c9fa9199a..06ff303294 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1256,9 +1256,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec })); } JSON_CATCH(...) {} // LCOV_EXCL_LINE -#else - static_cast(check_parents); #endif + static_cast(check_parents); } void set_parents() From 1a1381f071156cffdc8f3d90d413e721ea6467a7 Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Wed, 21 Apr 2021 10:24:01 +0200 Subject: [PATCH 085/143] Fixes #2728 includes some macros to be defined for using without file io. --- include/nlohmann/detail/input/input_adapters.hpp | 4 ++++ include/nlohmann/detail/output/output_adapters.hpp | 4 ++++ include/nlohmann/json.hpp | 8 ++++---- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 63921ca55c..9bb4a2b483 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -26,6 +26,7 @@ enum class input_format_t { json, cbor, msgpack, ubjson, bson }; // input adapters // //////////////////// +#ifndef JSON_NO_IO /*! Input adapter for stdio file access. This adapter read only 1 byte and do not use any buffer. This adapter is a very low level adapter. @@ -115,6 +116,7 @@ class input_stream_adapter std::istream* is = nullptr; std::streambuf* sb = nullptr; }; +#endif // JSON_NO_IO // General-purpose iterator-based adapter. It might not be as fast as // theoretically possible for some containers, but it is extremely versatile. @@ -381,6 +383,7 @@ auto input_adapter(const ContainerType& container) -> decltype(input_adapter(beg return input_adapter(begin(container), end(container)); } +#ifndef JSON_NO_IO // Special cases with fast paths inline file_input_adapter input_adapter(std::FILE* file) { @@ -396,6 +399,7 @@ inline input_stream_adapter input_adapter(std::istream&& stream) { return input_stream_adapter(stream); } +#endif // JSON_NO_IO using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval(), std::declval())); diff --git a/include/nlohmann/detail/output/output_adapters.hpp b/include/nlohmann/detail/output/output_adapters.hpp index 71ca65b92d..46c82e9fda 100644 --- a/include/nlohmann/detail/output/output_adapters.hpp +++ b/include/nlohmann/detail/output/output_adapters.hpp @@ -50,6 +50,7 @@ class output_vector_adapter : public output_adapter_protocol std::vector& v; }; +#ifndef JSON_NO_IO /// output adapter for output streams template class output_stream_adapter : public output_adapter_protocol @@ -73,6 +74,7 @@ class output_stream_adapter : public output_adapter_protocol private: std::basic_ostream& stream; }; +#endif // JSON_NO_IO /// output adapter for basic_string template> @@ -105,8 +107,10 @@ class output_adapter output_adapter(std::vector& vec) : oa(std::make_shared>(vec)) {} +#ifndef JSON_NO_IO output_adapter(std::basic_ostream& s) : oa(std::make_shared>(s)) {} +#endif // JSON_NO_IO output_adapter(StringType& s) : oa(std::make_shared>(s)) {} diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index a9e6c76d11..a783e3d596 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -6521,7 +6521,7 @@ class basic_json /// @name serialization /// @{ - +#ifndef JSON_NO_IO /*! @brief serialize to stream @@ -6581,7 +6581,7 @@ class basic_json { return o << j; } - +#endif // JSON_NO_IO /// @} @@ -6837,7 +6837,7 @@ class basic_json ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) : detail::binary_reader(std::move(ia)).sax_parse(format, sax, strict); } - +#ifndef JSON_NO_IO /*! @brief deserialize from stream @deprecated This stream operator is deprecated and will be removed in @@ -6882,7 +6882,7 @@ class basic_json parser(detail::input_adapter(i)).parse(false, j); return i; } - +#endif // JSON_NO_IO /// @} /////////////////////////// From 31c4c8f36c835003e876c3f9daa18e235a43fca4 Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Wed, 21 Apr 2021 11:03:28 +0200 Subject: [PATCH 086/143] Fixes #2730 Added define JSON_VERSION_IS_PREDEFINED for skipping automatic JSON_HAS_CPP_* detection --- include/nlohmann/detail/macro_scope.hpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index bd36048f1f..111b4eeef8 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -20,15 +20,18 @@ #endif // C++ language standard detection -#if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) - #define JSON_HAS_CPP_20 - #define JSON_HAS_CPP_17 - #define JSON_HAS_CPP_14 -#elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 - #define JSON_HAS_CPP_17 - #define JSON_HAS_CPP_14 -#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) - #define JSON_HAS_CPP_14 +// if the user wants to manually specify the used c++ version this is skipped +#ifndef JSON_VERSION_IS_PREDEFINED + #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) + #define JSON_HAS_CPP_20 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 + #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 + #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) + #define JSON_HAS_CPP_14 + #endif #endif // disable documentation warnings on clang From 38952643c5b64e7d22f3259ae39550de176f1b51 Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Wed, 21 Apr 2021 12:46:37 +0200 Subject: [PATCH 087/143] generate amalgation to fix CI for #2730 --- single_include/nlohmann/json.hpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 4493dd6542..4af5a62f47 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2229,15 +2229,18 @@ JSON_HEDLEY_DIAGNOSTIC_POP #endif // C++ language standard detection -#if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) - #define JSON_HAS_CPP_20 - #define JSON_HAS_CPP_17 - #define JSON_HAS_CPP_14 -#elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 - #define JSON_HAS_CPP_17 - #define JSON_HAS_CPP_14 -#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) - #define JSON_HAS_CPP_14 +// if the user wants to manually specify the used c++ version this is skipped +#ifndef JSON_VERSION_IS_PREDEFINED + #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) + #define JSON_HAS_CPP_20 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 + #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 + #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) + #define JSON_HAS_CPP_14 + #endif #endif // disable documentation warnings on clang @@ -17094,7 +17097,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec detail::parser_callback_tcb = nullptr, const bool allow_exceptions = true, const bool ignore_comments = false - ) + ) { return ::nlohmann::detail::parser(std::move(adapter), std::move(cb), allow_exceptions, ignore_comments); @@ -25748,7 +25751,7 @@ template<> inline void swap(nlohmann::json& j1, nlohmann::json& j2) noexcept( // NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible::value&& // NOLINT(misc-redundant-expression) is_nothrow_move_assignable::value - ) +) { j1.swap(j2); } From 6b267e3cb15b7422bf1e4409774d87ee667b5fe4 Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Wed, 21 Apr 2021 12:53:58 +0200 Subject: [PATCH 088/143] fixed amalgation file for #2730 --- single_include/nlohmann/json.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 4af5a62f47..3fdf8d686f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -17097,7 +17097,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec detail::parser_callback_tcb = nullptr, const bool allow_exceptions = true, const bool ignore_comments = false - ) + ) { return ::nlohmann::detail::parser(std::move(adapter), std::move(cb), allow_exceptions, ignore_comments); @@ -25751,7 +25751,7 @@ template<> inline void swap(nlohmann::json& j1, nlohmann::json& j2) noexcept( // NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible::value&& // NOLINT(misc-redundant-expression) is_nothrow_move_assignable::value -) + ) { j1.swap(j2); } From 0a2de2f5b9149c80825d2cf19ebf7f2c5cb8b511 Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Wed, 21 Apr 2021 12:54:55 +0200 Subject: [PATCH 089/143] fixed amalgation file for #2728 --- single_include/nlohmann/json.hpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index a70aaf8cbc..752f8d8aff 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -4747,6 +4747,7 @@ enum class input_format_t { json, cbor, msgpack, ubjson, bson }; // input adapters // //////////////////// +#ifndef JSON_NO_IO /*! Input adapter for stdio file access. This adapter read only 1 byte and do not use any buffer. This adapter is a very low level adapter. @@ -4836,6 +4837,7 @@ class input_stream_adapter std::istream* is = nullptr; std::streambuf* sb = nullptr; }; +#endif // JSON_NO_IO // General-purpose iterator-based adapter. It might not be as fast as // theoretically possible for some containers, but it is extremely versatile. @@ -5102,6 +5104,7 @@ auto input_adapter(const ContainerType& container) -> decltype(input_adapter(beg return input_adapter(begin(container), end(container)); } +#ifndef JSON_NO_IO // Special cases with fast paths inline file_input_adapter input_adapter(std::FILE* file) { @@ -5117,6 +5120,7 @@ inline input_stream_adapter input_adapter(std::istream&& stream) { return input_stream_adapter(stream); } +#endif // JSON_NO_IO using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval(), std::declval())); @@ -12664,6 +12668,7 @@ class output_vector_adapter : public output_adapter_protocol std::vector& v; }; +#ifndef JSON_NO_IO /// output adapter for output streams template class output_stream_adapter : public output_adapter_protocol @@ -12687,6 +12692,7 @@ class output_stream_adapter : public output_adapter_protocol private: std::basic_ostream& stream; }; +#endif // JSON_NO_IO /// output adapter for basic_string template> @@ -12719,8 +12725,10 @@ class output_adapter output_adapter(std::vector& vec) : oa(std::make_shared>(vec)) {} +#ifndef JSON_NO_IO output_adapter(std::basic_ostream& s) : oa(std::make_shared>(s)) {} +#endif // JSON_NO_IO output_adapter(StringType& s) : oa(std::make_shared>(s)) {} @@ -23009,7 +23017,7 @@ class basic_json /// @name serialization /// @{ - +#ifndef JSON_NO_IO /*! @brief serialize to stream @@ -23069,7 +23077,7 @@ class basic_json { return o << j; } - +#endif // JSON_NO_IO /// @} @@ -23325,7 +23333,7 @@ class basic_json ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) : detail::binary_reader(std::move(ia)).sax_parse(format, sax, strict); } - +#ifndef JSON_NO_IO /*! @brief deserialize from stream @deprecated This stream operator is deprecated and will be removed in @@ -23370,7 +23378,7 @@ class basic_json parser(detail::input_adapter(i)).parse(false, j); return i; } - +#endif // JSON_NO_IO /// @} /////////////////////////// From 10fc5162e7130fd04872164303b8eacdf3168cd4 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 24 Apr 2021 13:24:36 +0200 Subject: [PATCH 090/143] :arrow_up: use Clang 12 where available --- cmake/ci.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/ci.cmake b/cmake/ci.cmake index 5807d8f18e..785d5f8d60 100644 --- a/cmake/ci.cmake +++ b/cmake/ci.cmake @@ -13,12 +13,12 @@ execute_process(COMMAND ${ASTYLE_TOOL} --version OUTPUT_VARIABLE ASTYLE_TOOL_VER string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" ASTYLE_TOOL_VERSION "${ASTYLE_TOOL_VERSION}") message(STATUS "🔖 Artistic Style ${ASTYLE_TOOL_VERSION} (${ASTYLE_TOOL})") -find_program(CLANG_TOOL NAMES clang++-HEAD clang++-11 clang++) +find_program(CLANG_TOOL NAMES clang++-HEAD clang++-12 clang++-11 clang++) execute_process(COMMAND ${CLANG_TOOL} --version OUTPUT_VARIABLE CLANG_TOOL_VERSION ERROR_VARIABLE CLANG_TOOL_VERSION) string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" CLANG_TOOL_VERSION "${CLANG_TOOL_VERSION}") message(STATUS "🔖 Clang ${CLANG_TOOL_VERSION} (${CLANG_TOOL})") -find_program(CLANG_TIDY_TOOL NAMES clang-tidy-11 clang-tidy) +find_program(CLANG_TIDY_TOOL NAMES clang-tidy-12 clang-tidy-11 clang-tidy) execute_process(COMMAND ${CLANG_TIDY_TOOL} --version OUTPUT_VARIABLE CLANG_TIDY_TOOL_VERSION ERROR_VARIABLE CLANG_TIDY_TOOL_VERSION) string(REGEX MATCH "[0-9]+(\\.[0-9]+)+" CLANG_TIDY_TOOL_VERSION "${CLANG_TIDY_TOOL_VERSION}") message(STATUS "🔖 Clang-Tidy ${CLANG_TIDY_TOOL_VERSION} (${CLANG_TIDY_TOOL})") From 0638a27ed35d47cf2aa9a96ce0c104fb14cb072d Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 24 Apr 2021 13:50:30 +0200 Subject: [PATCH 091/143] :construction_worker: add step for Infer --- .github/workflows/ubuntu.yml | 2 +- cmake/ci.cmake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 95073fa113..6e324a0d99 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -44,7 +44,7 @@ jobs: container: nlohmann/json-ci:latest strategy: matrix: - target: [ci_clang_tidy, ci_cppcheck, ci_test_valgrind, ci_test_clang_sanitizer, ci_test_amalgamation, ci_clang_analyze, ci_cpplint, ci_cmake_flags, ci_single_binaries, ci_reproducible_tests, ci_non_git_tests, ci_offline_testdata] + target: [ci_clang_tidy, ci_cppcheck, ci_test_valgrind, ci_test_clang_sanitizer, ci_test_amalgamation, ci_clang_analyze, ci_cpplint, ci_cmake_flags, ci_single_binaries, ci_reproducible_tests, ci_non_git_tests, ci_offline_testdata, ci_infer] steps: - uses: actions/checkout@v2 - name: cmake diff --git a/cmake/ci.cmake b/cmake/ci.cmake index 785d5f8d60..3d546359fe 100644 --- a/cmake/ci.cmake +++ b/cmake/ci.cmake @@ -637,7 +637,7 @@ add_custom_target(ci_pvs_studio add_custom_target(ci_infer COMMAND mkdir -p ${PROJECT_BINARY_DIR}/build_infer COMMAND cd ${PROJECT_BINARY_DIR}/build_infer && ${INFER_TOOL} compile -- ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${PROJECT_SOURCE_DIR} -DJSON_BuildTests=ON -DJSON_MultipleHeaders=ON - COMMAND cd ${PROJECT_BINARY_DIR}/build_infer && ${INFER_TOOL} run -- make --parallel ${N} + COMMAND cd ${PROJECT_BINARY_DIR}/build_infer && ${INFER_TOOL} run -- make COMMENT "Check code with Infer" ) From 608fbffcb1582b24a5c3247bdf5e760cbe35abb8 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 24 Apr 2021 13:51:53 +0200 Subject: [PATCH 092/143] :memo: update compiler versions --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 81d3a30169..bb931803cf 100644 --- a/README.md +++ b/README.md @@ -1201,8 +1201,8 @@ auto cbor = json::to_msgpack(j); // 0xD5 (fixext2), 0x10, 0xCA, 0xFE Though it's 2021 already, the support for C++11 is still a bit sparse. Currently, the following compilers are known to work: - GCC 4.8 - 11.0 (and possibly later) -- Clang 3.4 - 11.0 (and possibly later) -- Apple Clang 9.1 - 12.3 (and possibly later) +- Clang 3.4 - 12.0 (and possibly later) +- Apple Clang 9.1 - 12.4 (and possibly later) - Intel C++ Compiler 17.0.2 (and possibly later) - Microsoft Visual C++ 2015 / Build Tools 14.0.25123.0 (and possibly later) - Microsoft Visual C++ 2017 / Build Tools 15.5.180.51428 (and possibly later) @@ -1271,7 +1271,8 @@ The following compilers are currently used in continuous integration at [Travis] | Clang 10.0.0 with GNU-like command-line | Windows-10.0.17763 | GitHub Actions | | Clang 11.0.0 with GNU-like command-line | Windows-10.0.17763 | GitHub Actions | | Clang 11.0.0 with MSVC-like command-line | Windows-10.0.17763 | GitHub Actions | -| Clang 11.1.0 (11.1.0-++20210204121720+1fdec59bffc1-1~exp1~20210203232336.162 | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 11.0.0 (11.0.0-2~ubuntu20.04.1) | Ubuntu 20.04.2 LTS | GitHub Actions | +| Clang 12.1.0 (12.0.1-++20210423082613+072c90a863aa-1~exp1~20210423063319.76 | Ubuntu 20.04.2 LTS | GitHub Actions | | Visual Studio 14 2015 MSVC 19.0.24241.7 (Build Engine version 14.0.25420.1) | Windows-6.3.9600 | AppVeyor | | Visual Studio 15 2017 MSVC 19.16.27035.0 (Build Engine version 15.9.21+g9802d43bc3 for .NET Framework) | Windows-10.0.14393 | AppVeyor | | Visual Studio 15 2017 MSVC 19.16.27045.0 (Build Engine version 15.9.21+g9802d43bc3 for .NET Framework) | Windows-10.0.14393 | GitHub Actions | From 52717b74736e50ee4e108539624bd7a567e98d27 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 24 Apr 2021 14:27:13 +0200 Subject: [PATCH 093/143] :construction_worker: add clang 11 --- .github/workflows/ubuntu.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 6e324a0d99..fec68370a6 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -90,7 +90,7 @@ jobs: container: nlohmann/json-ci:latest strategy: matrix: - compiler: [g++-4.8, g++-4.9, g++-5, g++-7, g++-8, g++-9, g++-10, clang++-3.5, clang++-3.6, clang++-3.7, clang++-3.8, clang++-3.9, clang++-4.0, clang++-5.0, clang++-6.0, clang++-7, clang++-8, clang++-9, clang++-10] + compiler: [g++-4.8, g++-4.9, g++-5, g++-7, g++-8, g++-9, g++-10, clang++-3.5, clang++-3.6, clang++-3.7, clang++-3.8, clang++-3.9, clang++-4.0, clang++-5.0, clang++-6.0, clang++-7, clang++-8, clang++-9, clang++-10, clang++-11] steps: - uses: actions/checkout@v2 - name: cmake From f6e9654aaaec89700e9afdaa1e7ea70904683c63 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 25 Apr 2021 14:33:12 +0200 Subject: [PATCH 094/143] :mute: suppress unsigned integer warnings --- cmake/ci.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/ci.cmake b/cmake/ci.cmake index 3d546359fe..d7d94b2bf8 100644 --- a/cmake/ci.cmake +++ b/cmake/ci.cmake @@ -486,7 +486,7 @@ add_custom_target(ci_test_coverage # Sanitizers. ############################################################################### -set(CLANG_CXX_FLAGS_SANITIZER "-g -O1 -fsanitize=address -fsanitize=undefined -fsanitize=integer -fsanitize=nullability -fno-omit-frame-pointer -fno-sanitize-recover=all -fsanitize-recover=unsigned-integer-overflow") +set(CLANG_CXX_FLAGS_SANITIZER "-g -O1 -fsanitize=address -fsanitize=undefined -fsanitize=integer -fsanitize=nullability -fno-omit-frame-pointer -fno-sanitize-recover=all -fno-sanitize=unsigned-integer-overflow -fno-sanitize=unsigned-shift-base") add_custom_target(ci_test_clang_sanitizer COMMAND CXX=${CLANG_TOOL} CXXFLAGS=${CLANG_CXX_FLAGS_SANITIZER} ${CMAKE_COMMAND} From 5a77314c5e786964c52ab7cd84e15f548fe04b33 Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Mon, 26 Apr 2021 08:07:25 +0200 Subject: [PATCH 095/143] replaced define JSON_VERSION_IS_PREDEFINED with JSON_HAS_CPP_11 --- include/nlohmann/detail/macro_scope.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 111b4eeef8..663c2fbd31 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -20,8 +20,8 @@ #endif // C++ language standard detection -// if the user wants to manually specify the used c++ version this is skipped -#ifndef JSON_VERSION_IS_PREDEFINED +// if the user manually specified the used c++ version this is skipped +#if !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11) #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) #define JSON_HAS_CPP_20 #define JSON_HAS_CPP_17 @@ -32,6 +32,8 @@ #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) #define JSON_HAS_CPP_14 #endif + // the cpp 11 flag is always specified because it is the minimal required version + #define JSON_HAS_CPP_11 #endif // disable documentation warnings on clang From d140a1c777b73198745f66577a4dc4fb08b0f135 Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Mon, 26 Apr 2021 08:39:24 +0200 Subject: [PATCH 096/143] fixed amalgamation file --- single_include/nlohmann/json.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 3fdf8d686f..62c6c485ad 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2229,8 +2229,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP #endif // C++ language standard detection -// if the user wants to manually specify the used c++ version this is skipped -#ifndef JSON_VERSION_IS_PREDEFINED +// if the user manually specified the used c++ version this is skipped +#if !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11) #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) #define JSON_HAS_CPP_20 #define JSON_HAS_CPP_17 @@ -2241,6 +2241,8 @@ JSON_HEDLEY_DIAGNOSTIC_POP #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) #define JSON_HAS_CPP_14 #endif + // the cpp 11 flag is always specified because it is the minimal required version + #define JSON_HAS_CPP_11 #endif // disable documentation warnings on clang From cdfe86548673808042d8abe1dc61e8a86b87dc58 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 28 Apr 2021 20:33:05 +0200 Subject: [PATCH 097/143] :memo: add documentation for numbers --- .github/CONTRIBUTING.md | 2 +- doc/images/json_syntax_number.png | Bin 0 -> 37014 bytes doc/mkdocs/Makefile | 2 +- doc/mkdocs/docs/api/basic_json/array_t.md | 4 +- doc/mkdocs/docs/api/basic_json/boolean_t.md | 2 +- .../docs/api/basic_json/number_float_t.md | 4 +- .../docs/api/basic_json/number_integer_t.md | 6 +- .../docs/api/basic_json/number_unsigned_t.md | 6 +- doc/mkdocs/docs/api/basic_json/object_t.md | 6 +- doc/mkdocs/docs/api/basic_json/string_t.md | 4 +- doc/mkdocs/docs/features/iterators.md | 2 +- .../features/{types.md => types/index.md} | 28 +- .../docs/features/types/number_handling.md | 313 ++++++++++++++++++ doc/mkdocs/docs/home/releases.md | 2 +- doc/mkdocs/mkdocs.yml | 6 +- include/nlohmann/detail/input/lexer.hpp | 6 +- include/nlohmann/json.hpp | 36 +- single_include/nlohmann/json.hpp | 42 +-- test/src/unit-class_parser.cpp | 8 +- test/src/unit-testsuites.cpp | 4 +- 20 files changed, 400 insertions(+), 83 deletions(-) create mode 100644 doc/images/json_syntax_number.png rename doc/mkdocs/docs/features/{types.md => types/index.md} (91%) create mode 100644 doc/mkdocs/docs/features/types/number_handling.md diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 7f12d50708..a9c0b46923 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -58,7 +58,7 @@ To make changes, you need to edit the following files: - 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](https://json.org) conformance**. If you propose a conformant extension of JSON to be supported by the library, please motivate this extension. - We shall not extend the library to **support comments**. There is quite some [controversy](https://www.reddit.com/r/programming/comments/4v6chu/why_json_doesnt_support_comments_douglas_crockford/) around this topic, and there were quite some [issues](https://github.com/nlohmann/json/issues/376) on this. We believe that JSON is fine without comments. - - We do not preserve the **insertion order of object elements**. The [JSON standard](https://tools.ietf.org/html/rfc7159.html) defines objects as "an unordered collection of zero or more name/value pairs". To this end, this library does not preserve insertion order of name/value pairs. (In fact, keys will be traversed in alphabetical order as `std::map` with `std::less` is used by default.) Note this behavior conforms to the standard, and we shall not change it to any other order. If you do want to preserve the insertion order, you can specialize the object type with containers like [`tsl::ordered_map`](https://github.com/Tessil/ordered-map) or [`nlohmann::fifo_map`](https://github.com/nlohmann/fifo_map). + - We do not preserve the **insertion order of object elements**. The [JSON standard](https://tools.ietf.org/html/rfc8259.html) defines objects as "an unordered collection of zero or more name/value pairs". To this end, this library does not preserve insertion order of name/value pairs. (In fact, keys will be traversed in alphabetical order as `std::map` with `std::less` is used by default.) Note this behavior conforms to the standard, and we shall not change it to any other order. If you do want to preserve the insertion order, you can specialize the object type with containers like [`tsl::ordered_map`](https://github.com/Tessil/ordered-map) or [`nlohmann::fifo_map`](https://github.com/nlohmann/fifo_map). - Please do not open pull requests that address **multiple issues**. diff --git a/doc/images/json_syntax_number.png b/doc/images/json_syntax_number.png new file mode 100644 index 0000000000000000000000000000000000000000..be23ffa69d02833fb3b109b6a8c3e50e9701e2ff GIT binary patch literal 37014 zcmeFZXIPWX(!h;$W@5>P;qE*%L)L%(T)(v{w;ihy(#2p}B{5PC^Osv-(P zr1!33=)HrK|0X=odEaxso&T5f;r)4C!0x@XGrP01Gqba^hTYdxIY)V!^2CV~=kBR0 zB2Jtj6F6~#oQ&i&_>z2a_A2;u(iNd1f1a7)>f5dQD)uR)EsyG%;WgAzlTHd0;l z^J{W*^XF6Ym(dcs2fI?LjY;`)`90IF0e&4Zl=3H5s4yfe)c-GjykS8mG8g10tHy9g zmqY8|oZo|xuQezB*e{UrAD0LI*!%HhF#b7(d8re?a^I+*43g)o+5B0xpFLw#eG9o>%C!w=S>alKWHKjjLxKJi5LUVK z)|$x!Rs0h9T5Ty|a1x$|60)KPXuZvTKNIq1pI^e(j-72EO$uCEkuUY*l$csvZ%~tZ zfx@yJPCKmev>i#Y3mln=Brv<_7l;1j^@hgfafTr#p-_ExEkfi<(Bm`B9}nRHuc(=} zHg2kY7@5oc*}XH<7%OXAJoib+EWLhwNg*=u^!UyG@=yC>HR4PAA74iYE@;||-}xk! zrgvi|je@y(ajCUl!e$W8whMV86iiH_uXNV5IN45lCO>zD)j;p9vmSM0d*?{)yzFUN zS&g-*!|!ohZEhb*R{wx)eljC(fNyN0a%;DG^$!PXjwF~5P`RJ#0yemsTrK*RO`|pc z-tVySd2NKdcCZvQrq1?0{qfkC+0tQc;l;YkvTMD|7+PVmxJB-fmo_=R`nhkK7t?!d#5uPG@Tm8A%6wxA&PhIHW zA8(F^vpa61m-2XOi`fd3{~Vd#c;F;`_+WZg!8bsRJjFig)rQz!q7ICwIEwV#(o0{g zagvnV``y%4%dE?Bhhe#q*o6Xtfz%86d(AXTTu-y`W-;rnOZq8)g19r&r(3&Nap*o89KN?hCMlPt2wfPycRvX6*yPm>(pamdUmUxM!M}`a+Gp6mfq~{5eh!X z*|mn*3!T&DUhHes4vY!#7Kt_+4+|W*0~_#Ux64aDS+l!~44^%DxR(?n(X(YiI(bCZ zYv#a5#{H?_F5I%C?L9iB808R1{kB!O{7#~bk4Qds=%mbJ*~BIP{Pe?gr}LN6WZ5FU z|HuYMadaW{qvvY+lN=JM-f7rZv3cT(#dL9>^crWXcL(%OrhH^(s%$57j`WCmAsZc@ zZI)+%la_sNkEUVcmTnFEHktkR0o?P3Lm~B~#O=h7wdA9wXSbZ*pO-FC#I3FsUz0p+ z>z&9pn|pUHU*{d#QmA@9VRD}(#`kN-g9oKeTZyNP&m4W@#EfAddpg(bx`LHMf(~r_PJcTrTf>n{Eq=niRtog1V?Zn zqmNdW<(f45zHfIB%Mln_my}d$YBJhVtlAzm+4fM%l_N5sl8L`ks_3WHMvu#BY-HfD z7geQ(;Bu+U!j0JG}L4kUOgX>a*G}&D_S&~c0?jq zy6vgaFDz~$w{ zdVyj^ARIZ-zsFi9`Oe(ePA;T!p$qZ!aGj-!<>t~aFi{)Q z!pI6{^|(!Ce!JiBfpF%vH$L`FBG0#6KpZ?2pG3a%!Rl~*k6Fa{@#J&oa-RzBI#YQz z3l;rEFxQ}J9_7${2+iE=fwr1^$^xYX%|}p{Z)M4RHvh2F!zSgiGO*a-!O8_pN?LxJ zI+oe3QK<{DjZScgL<}Kr*)~t}UhIM9ataQ;JG;fpT0Va+b{Kej%h()~!f&tnWYjKC z3-`64xD08>*_jq>vU+DMqvpn&!RzC$4sp+yJoi{Olloow=CLlMEp4k}&LRfUYH9aU z6RTf5zT!;#$YshXZF+`v|L+)CZNEv%Njek5;>U*K!?R2CE`Ptt zz&1ZxVkr7{mhbI9va7SC$3`ys(<;XdL@|bp-ZOF(=S|b;uQ$y_#@6{nV$~DJL?1h2 zbWn)tcy*XcwThF#1~N%`VeZu8FG6FnYF#$_)Kt=HyC=V`@9s6*aw92|%ZHnxTl<<%ecg+KYRmsdk}bFQxcJ^mgl^+m0po8p%+46WPf>Mwly#l# zk({{o0Cy*-sqMw%bc1j8smpIgnH)#&{FO{%7VdTtDc+03O7*7Z3wW|W8I3flq8EIL zRKRZRj@k&e8V524b*S@@zYxoEgi$n?XxWO_X+61$RsWD+9MdT7v!*CEbl2S{ebD*c zHD3eG1Bd%%$gZe^sBdnk4z_|?3aIFWI!1?lHSHfvtap#?LJm9=9}*V2T(t4M4w!&X z%$CiEaY$beUB@is-pO{Zld#PR&y5_JuAPRB;<=u4zE2l7mVKzP>W{o+40enA-20M) zb3BrP^D*YQ{w0utk#v)kl^LHwL&@B*J7(3sID=vi8edb|JUa57>w6Hi-nD!gzIOkS zhZ6Ia9``cG&?nb9?86D6z|qv&a1@ILr%B@P=lSH0e{6SetVqmMbdpnhTsR3cS+Tm| z_(Rw{ciz3zI#UkEko7LttfkLo!^#yZi#P*)i1}sL&$3yUck``^{0glZh9!g}IJ{9t zLO6qQ#p9YR+7u^!Dq42GVV#pavuVThX4l$u4Vetvi$I+c%fM>CG@IS$v@d6-+E$n< zErNoFPM4bNl;97twT}{QnC_cUW?K2O4Cnq_G<9;G5b`87rEHM3>vZwiU5rEOB*^C# zL&C+2$NPPVaxCipZECst{;Lo*@e(bxO?tZDxecZ z2yZr0vPOA)aCXsgw3vQ<^(}-+(Mj+Hq~bEth1VV6CH12R%fFIj^8#izY46&K7YlCN z-E-&tY^7#1opacy>1j5<#hGc)c^%f=hc|_x1xdX;D|?d{p{*wGsko=|Wf@%% zcOO0y%nm=~?&z(DbGQ&xS5?BMn>iR0astA=uTJTDJ6A4Mckq00Ok%asnQ=Yd|BK-F z8X0q$H%Mo9@dB`@?fD-Q`FsJ=umL+yIwXJ(>&f-8(Z2jYlOHRzQc5yCfN^ znBG2o0GI%-ODid_-iV7^x+U&8PLuq#4Y~kVhCKLF*&usRd9jkD9o}q04{SWi2)w}4_fQ|IAnB0?r zRi1h#&7uSs8*S?sz6($6aBvSp3f_fZZ+L)l`$7)Oypx*H)lF(cVYy?1acf9X7T{$6 z{%DpOs($zhnRNP^4Ml~N@);YTb6f$Y=)6 znyG6_`V8sUmq~289nEaaiZB-53Eb1_&k-!$N4)Xf8F_g0r$$`6uTLO7!B}Q~R-oR^ zZ|)SFkq^KBqs{u^?!m2JhJpO+Y|WLPJWn+jx+2AWgM^|=78j>Pgd!cwT&I@9O@2!T z3g2tw2{-;J7mjpg@HnWQNf{h?oW3by9`D8zor1OsEvr0ybkO1RaJ(XV`s%~7CS&{RsDf@{bCIXH#o!O0oowsS#cSHQ4C|wfo)`S8+#NoOe+(scmp*yg z`>lU68Efu8EKE=PTNZ}QFCQ(zcZpkX(F+|E3WnaR>fLpzn%~!b`1(>(VUC^mo#I>C z3<(z?NAZAu5p$-HK*Kr1h?}ZwUES%y7lozFrYk(!+huQbA#{;{F$+I#6|;-Q2J&va zS>~Z|LNCM@b5EGP`IcKV(J0jMG2uzs=8dNezL`zUG;ggB)=RHVS{FH-u77_}r{lhA zI$6Q?9T&Hq%LgQGD(>nLt@RmpPZlI3 z)!epK%AWJ>*7ox`W)n8q`2%#TiylsMe5gbteSvWzLi=ao*KelRO$&1t{1O%vhsJ~o zhhO6kMF9QD`_De*eWaXixyakU^S6*d*bwH*Jm#!D)VOFJ!gH!ZdAjU7PUyv zTsOsmw%P(*JT&@^TFJ9iV8)|XX`C(5ACr%dbUE!Kr-ho@%u`o30nR@ZQ;Yb~!W)O$-Nc{`g@5koaDFi0_n`s$~gJu-%m_#1$}A``<QC?{oC*R3nvvTbl)te5l>DKQ}o4@f0)#{jA zoc_^>Ba+dyx0NDiP`X(6Zbrn|{lSTP!$NGfT}8(`+bUBI&dIH6C!wq?RnrMM4aca2 z4xd1u=ypZ;I=OuBtF<{3u4GL*2{x(^TUNJm(xKx5CN38EHg zr5>}|;+yJm?-8A2J6_&>}?i$D02BMI#aNxV`W*HD8v1JYS3K1;(O_% znpM%WO<~KvA5xFAEK5}Bhf1um*^cxi&aaj`AXVIU-SxGcLAO59YD6A=R%;7aklwJ=@18|*tAET!Hybt$RaNL+yEqngxuj}2xp0pzer4bAtSyst zoGhfAYR@B$H07k&_^!4T`64t}O?x7(FQmSpJ>*uJto=<``X-nA)x9OVW#4bZ^U|>y zT9vQNXh!i1zU_axAAA}N4ZBVR^9``AniY&Xd%0vO&-GD#yixGpt3&AmQs(yg>)KL* z{T5JtzRcR$OrekaAM_*Q@8)y+J*h_uU6>p5+9{mc7~P&xU;S$sVOzB%6Kxr!`JAS@ z%A?Xif2nPB&ZWSh-YKbKvx1F!tjoP$=tI-8H7o~}rnS=5$>>Ju_oWSM2Ft0X-eXC9 zgpaPCRasbWwrn@`G)XGj>)9n@MI!}C*(v>heQb0zlC^tY#%NN|mh?PV|EAyQVYNMV z`exc>@xaibUF zl8UT9#ZO-XRRp7Ydq_rjhfR2n8LJ;xU9rH2wt|r>-&Yr4{@!L*nO?-9J>@LysT}ML zo57l;R`?c0PN2-5z>O+9@=FdW^!2yx`LF{W!)IXFJMAAB_&DX)xPMNh*?s3Op%mJ& zO3{yCq&qX?5n8i4Q#`Vi!fGmOZ;x@)BmboRdme5|(Cu z{|;Nv&qA=e06ol)?B2+n9cgapYftV#)6w<)ra4NC+jiaDrNQ}3cH-y|Vw0&)jf|EI zOU~H39oEilj@o&byeOQ*eV8~V)qk6Bchlq|R=B&{@cc&o$g+DI4?qrhH2VRE*qX2T zaN8d566WjQe^3$7+1HoNMOwe`V`{S^%aRQR?`ufdf_6tNg{{`HRMHLe)=D@jlFO2n zxnK8-;3&ahBq9U#sCK}mvdM21t!*EZvNsC1^wqQBC50q%PD0_&i2$7lO3a1Ru&)37 z`R^>qriM)@;ojYa^LBRMBybFH%WUtDV0-yoPW7I(yw*1G|`(~4mq%qwOy1QOgVj|?pb^I*{Nptw<#;?yw2#YM#1sh)F ztJvD=%L~7LqUabEz;YrYB`^X>x}jz?Th2KveT0>`knIgor&A$z47zZcs7s&^E98Lm zelM3F&sEzC)w4+eVqgK)Y^o3|SItzVq`am;Jf?Z<#{{oeL#z!i2^8KX3j4$g-$BE( zU!~gL+w?9Tyg1NX#YJXVE=Lf8I66p>QDRfiSlg6E<2;#_WN?0Mrp&hH3^`FGcs;B> zl_C{Pu%zkrlEaP2C3c2_XJo@OofmC*&p_`G3qQ!_L=wVNyp`s+N3sn)(ga2$7a8lB zt`Y?aHJB7U66HSz9c)V6`!WK zAvGYRy;-OwP8mo4chTDdxlq-}C!J!?g^JOdU|*DS2H=COT7!st+ZO3aMV40we>PQp z$>H9~d0+3oBC+FPt^7#a>umgBZMz(81)@8=w<9#Cd$nE`6Xibch4r{VuanL&Z0vil z3r0836J7ArT3-KO%*$vR|F3J;ScUy9M9FRml{^wRQ=u`c#-VL^$-tCX)L9QOsViH< zDPCG%KjiPS@w@Q1bMI!LScv1w=4%zaAZd`wxEwY}Uf3+*_HEI@j|7GX%M_l5mUN4O zQrB|2t?T2n7j@?YEK2T+4X_-u+Q;f7Q?<7=T&ZKPoRpHB|Cog{NfH-E%4WOJ&$Ol; z(vU_W;F zOEF9~=pdP?guXClDYoikO+lH?xHP*>uMY6ZDZ5bY%&tkTM@|j3O_zO2rY1Yh=^&c-;m`?ey z!kS@Ev1MHYma{8m@F~wZOeCxEmd5m9&Pw9NZ&xEy@S|$=QjUtw(WXg}&R&ym; zF;rc|l>h+rD=&U_xW+7yHiVZUv&ZaKva1r*O}|gy{uzV>RCrMc-hcz7oahJp^RN0c z2aRcdUoY(WcJ=Zx+Y;1dUyXTPb7Wehm_&Pqo7O+A)aTC^=t59~b$-m}ey(mbex_my zDxQw^i7b-3Tz%$PVdNlWyB^JUOtI#e)^1;Pg$2IL#qaKD2c2#oZ<9HOY0c@j594S! zx}Ez)r-H)t=D?cudgaL~5a3pjWtSi$;%Uu6NcAYMhYDGqw_Z2#I(H(jJyO@vX~bP4g;rut1RmwE|z6cJEpqMYtT$Kx^m?`G-vl*V0qjH4}Gx zkiWQO>Ge(!3|mf|7RtOb5h=zKD50Z{%f2XTPUM>sNBmxM)V0g^?5h@JD8WSs!J38X z2(Kd`oMosaS}TZX=&qo9%?eC$;d5$40ey21)eyS{wKv?w1)9dk6L<5KIJL%4`8v4v zp;BSunkfmyI|TBoplU22k9HkB-4GjIa_HTaL^a3MqXSCJg5Q$8u38vzWxk+AYg1Y$ z&h%IKnX1)wiZ!+#Yg9W#i;Lz$hbRBHPR3(7c&CHlS6yK6V5RauR(^bHT#_(HiHZHb zQ^a*dUytv9@iU@_sgLFh4kgtX+R=V!dmb-c$69*gvG+(^W)J^t_tKOm;XO8jm#gE^L+1(#4 zxGMm)f|b&VSL2&{G*~rSWDsPRpwmuDl#hHhu8Zq87mU+Z_limut#Q(-$e=G|6Kx+xMAL_Q zOIa#|EI~;uOlGPaZ$@|Yd#G$TqUMAC>GUadC~@HsCVXhlxBE<_j(hzfN^o%?Zj{>h zZfUKD2zqMf2g-sjA}AA=@c-<3`11sZJ?l`9g*9kFU%TPx(9gI{Cu03}T zbr@Tr7x1Go^Ubkqf#!`5ncmh(gqu;<;n@gwKc^R|7W7!>8o7*16OQ)D4_|_R`wn5v z=RaLlk|wb^I&dhsae0&S^G}6L9>S&=vKf~nIKse_ePn`KR#DI*_{HU2d5cejn*XiX z!0Lt`lEYAUamqbP|3bH+$v+S=JPz@(=}Q(@C6Kz!R2OE|oV-hYT-k)$f4(7XElSK2 zVV}1hWb$T2^ewe>TO$>u1dQC-%_?l?z@{dYE9Ux2xd{9}r%YxcJv`4!_>nJ*b%s&x)FwmWBX+ls)w-fl&l>7(`d?MK8dn6x7 zq*$i-bTGb?oj4MKlHGo+BG7o{XsRsf9RJ}JBAuKytDi%8#Qmbd<)ct6yWXon*t^W( zrY9HsR>%n(=^Sd>jF|y!Oq`VqFcP0=_ecnAk9zDSZ{+>Cq%tg%z*E)wi@L~Wg1iG( zhHdCeQ|%CZo1wcQT|Zyx(=P+p6|m|x;}w4C;cq;r2$P(0zV*RnsB)k=n}FaB9e>cY zLI<(GIn@zg*_zD^GqF>|h!(dV+luX?Dv_`Ti1pG%dSBU~#>81X#{d`FQEK2sJ#sgp zBrQ>O){I~v#2J)ujOoz1cMWg59&uK(boI+1}*4&9>XHVBuai2 zb`bG}544kZu5Ce`*VHll&1@b5fDhRR(72siu5i;`xK>f*)=fG1NvH(h7h-6efzOr3 z3inETS<}0BAtHydPau<|63b?sP0Se>%BBi=Fe%sby=$$~l%@_WxlJXq<;z#@n_4(j zhY6)L0i+T+k=EYBF21?pdXB>v*%7#BH3QsGr2_1NmIM-ReJDuM3`DJ{zdS*8NPNVr?_�)(qmJGY<%4C?Rkp9U{5) z3*bH#m)BpMX9}fH< z9Ke~>yLvl+B%GswzP#bbX}}>N6xvSEk-~no0vGr*fpFu%mNVI?OHX^dT@09EmOJM`=t3Emt>r(uVS|Y?X9ZCe6tV;Q28?5fAZSyAxmP`v z;%9&`g|3>%lF8uuME=07R9n&`w4W0`bC~`2u?087I!a_quunY0x7a!D0 z&{c0ooX!Ikcw_u2BaGB07wH4S-_8|50D=vo>;?esc)5x&8BNUSQkuq5V;`P5d(SBc z|L`J=)HWBndg5;hchiV<>H zK0LZVe9}%8+cei5k8He!iF=QN#w)*H5ybfAU1_i-%d^|(&mML{;d|%fI}y)-=1mxH zR*!bi#B24^tW%d-{`GTG&Oo}OEE!=+Ahpez|HC6;WrWewA^6eyuectMUUuYibn911F_Cd1*P8`XZIWais$#o$M_W6EO ztKG6JM}=pNRe)_Mu%rv#QD1tliTN?D7mgI`!RkokBCp>%g04J+x*G%voObRy1O_K-txt&=LE6-h-m81M%k6*n3;b< zie;PPJ$&wbKSKs>O}d#<10%g<#}vSSW8Sns+pLx0&$g*?8~r*6SS0wVLEvZX=E&{E_O^mpMTxus^L}JoYCf zt*3Mn=^zKP28^FJ}qzi~TNlO!V6_3z74N&=OjJ2a>_`xr|l{l%UT3 zMmGcVRD-4Z+Zbkc zW86xA{=IrWG|C~CO)Vg?-$nY8R%cl6JL!k)wr>WPyhafmbo?F9zbbFt%Qt>*DAUc7=tU42Gu+A++mi@t28Y2pe zSN99&E?PUw8zQ-odVF&t7vbNA7GEe{k%G~ zPI0E&!swILmiGnF_-=O7lzWeXNIfl)$01NX-ab7Azo%V8FF1d}zdHcBrQi2sb``(gaS+W>q;d{@=#o434)_1j9kg`{5*V+8FySgZ&cMNo)(`8g~%yH~7 zov%-x2RjoKEw;g{vPAiIq-A)u-TiMI;7=z)6(=z7u#6Uf+jv*(ue@scQoQPMT4hn2 zj$o-y!YXp}>hHdU5`kz+Kz|daetnnqyb>=Y+o+^zarO7kWkLr^s+B$I$6n_mO|WR=*UF z{KfcPS};K*v6RLqN{yKP=yoyrZgi{P1WaV&~j^=erRic|<>}ijjm^5w$_?utn zCt)9crEm=0pbf^fdIuDziTN{;u@#rm?$O(_rcc{aYew~l-x5%!kpxPsN zhrH|MK?P84O$D?x^cta?Ku>=)R^Mv=^N(S~L$9y$xS3lo$rcerzGv#6Hkl`7+!hnr z+)rPQ_9#ZXC9W=lr1@5*9-QMnsu%+srcWWA?KcTSS2BAq1{a&v&Tog%_viZFkW7I< zkB0qjo-T2TW)Zl>vdBpjg}HCWxcdHXK?Y!LGf;3?CAY@FwKFWe%@AW+cdlIsj@ElL9GObS)^uKoJxSk;HAw;T<}&KVQLV7A(k z3HEG>R5%ubTV&%y_Ld}W_w*4Z6{w2pd}p}y$WLI*%DP-cO0h(K1`;IMs+5}|Jg$?)qUnh{^tGL-(+*qzu_Nf`h!Jj#{l3vJx9I{PSc>RbqD|l zeL{tey(|$O4Py3OkIT%(lTQ$0$KNf+TtGOQP?9IJV8`Ae1li!LW4MFl^qtutgPUlJekc*n~1f_zwRA>id zuaG=T7?0{Nqv6(Q6Jk^iKBy&|FYt67gkqi}OVh2hpLO7rG!@_{JL$HZ`LEM~(6D$X zofT7|5)?4L1w5xyFmogbqtT3(NuMVmOZ+wRCcfe3ya+)Vy{t2iDw1*<+tVQe(uBn~ z&~Hr>lpKXN)u%LrD!=B#C&Z>N*x0c6;05B~EXI9STgu`Zx~}Mhvn7Kvu0br#ckV)p z&l;l$dCu&<)wp|Fz!J*8%@}wua+umJ)Cp00mxMXLrS5SZg5x8PJ7hfl%8j4W5y!=^o4LXyqNyiLMgf&RaLu;oDOD921Fyt&eG*0tKDIHz5C2IRnQlYXEhpL3uF&#@QHqcPZ?G|x%y>MQKdXV(O9 zJneV!y3%q>B?TYH(WI8JljN-EoPp7z~4m(x-r#J7I&tJK5Pv7 zSg}I5R)+{+q#dC@6PKGwIfx%7-cuM|yeG0{3Fe4}lnwDGD#-abmYPa{SMapYMPzpQ zE~#SIpe7?M*ros}{agAXo#!8YaL_{987_<+-H#i10pekI!P#-Rl+_1$u}cO10EFIp zL!=qxmE5`zZ$0e9jop(K9wIfsAu<(GPI@fCW>RR*G0klwY7X+Bna}UN`lze zFsrf(-}@Nk3t|MWicUTlU?3cf<44WNGv$jxOt+|aO*&Qf5~eT?-~)%`dNr00KELI7 zF>x93(40eiCM_Q-0%-w)Zh50tUhge#gB*q1c0-=Y(j4HJbRjzmn%i&4v+UadXR_ON zyMpJ_C7h(sGr_*)DdF6&$Y48x5gyJ%nf7gV(emYlf(3bi6MQ}h4Fb^|&cpM8qStYF zK_OK#(MMl=8?c(ePP}vSDyNRdaf^f}3st7w86Xx$*vEtAKHxqM1lxLJ{bXq6@gGwo zB0;u54wn&q?`LPsg;OxO$Xg)H5DZBKT>DM}7P|nc9#-@mT=FQM#ny%*Z#mNe-e)40 zGl>zt<3Qeje*YWLbXo5e3}lhVyY*EblotS3>ryW=;%fyVkK_f&J|X=d`a9%T&@{uV zk}--@LS~xfC66Gb-(j6h1crow@SFkl@%N8R0vF(fI36L!%s%@XzZ%i_ypyR~>=_8o z>EdW0woi95EG0lfLz##ejGcm@@_9iNFMM~k=3c2NM9Wq^u;#9>uR^%a78d(GKex0r{LL`MNAW-cK_Vv zH^&2_L6r;A1~0%a$+YbKb~C0Y8%3K8_7F2x3N5q2G}#E$k0(swClXxZVQtX4Rx%EL zcR-x4xO;3&ULT-lfSF-&Y&N_v1*v6!Evy0LFvwx>R!%44)vcQLAQSQ>x1=wF$X^vJ zJFt#(UEP8~ppU&?aHcdsEDg;~=`>>!f{<4>PFFrR09uL-CRXG^4uzMRGul)DE1#!= zyFT?uBJ%`HX+|QNa8tEu!(@5B=M5Ya$8j0!b_v*jal39rM>|!TnB+$Utg^`rD%0=@PQGm7D@F6cyzwTJf6)Nv3adSfH3Rn4i$F6OJFZ2*s)UAId{ zg0aVs-!!^*RYiNXAa=J`_hPO)3Hd;7rkrp!-TU4WlgPRAv8zniZudTZ40Bq?ay|Zy zYlUjfwP;Ad3nSEa+Y1nG5wMyVDepe6ao%BW6ZO`4WV&&lp?WcAGVM zI^^ofUW}>DiuG;zRRg?&*P!cFT4g!*i9O`V^P0a-@9|D;{=oUb=V{0g-?C$hc|+1rl`=VMx9cHf;N55p*EPHi z$9zRl5K_Y$maA8QAa{0GSjab3| z7G1k&GkYrN9y1=y*REf9|7^t*yefQ*TRLw4MiJg;KlIPh?pa|=u$Wb)rwH_8FJbMOWM(cu7Epomx$Q9F2>a`QB@!-EItjIYAi@6mU*XhdZOAx>ZQNOYh%Rs&zEllHHNl z!v*!}#)I^a-(koa*pQ>&rjAFdVHMJcLnKY}wZx0x5T|aG12T&OABh2grJ}4MH0BkBw^^W?9M*L6W3X zAI5nlEr8un1#m?a{Z|xgsVOweQt%%rU1=Ytma&%1mXmuIJcX)3AOIwatxQB*eTlhUa+r=!WYvuq`7>sd0 zG!%ZHd@I5j+ivOdxqYK+EyI!sA=H^0+5W(eDUqKeMPg@wIo>G{PW*C|X7 zvS&!4=;F;65q~fCtOtUG&8nR4>6P(Mb^bC2RWmaV$1vxhoF0w6xXKrgho5jA56d1q z%7(i$e(U|^V-PIlpz*GFvh%;%_gk*@h9h$T6K#@O6>+SDEMbsHARzg>9?#$pa)?37 zg1S=6B@i7tA9jn_rsLM58)Y;%bjA$56x2l=UfpppC~wL2pE{%|1Vfjei$D&6RJX-G z;dlH6hQ+1jinu3W0T}GXx5^RxUBb*@W^GXPczvht2>oc$hQe|0KEh406-Xk=K#3Ap zcY_JNDxXIV!^5!nijgV;Kb^^)?B2YZt zDGZo^dZ#l?#XyNsCj)nF-0^!2@?|WyF90=40a4d9Tv;_3ybpFXw++))x)Ddc2hWr1 zKtEl@yi|ThIB}{H@`C>7{a(EqpFk~MBL%&CfxPhxT+ji(ow5jTZk?b41c{D&RL6T( zpjuAdT#DT}F=m(~uns(;xk(yk0m`$LNPB;F`Fxay#eING&&jEo>%6cqE(r^^WN^6~@DAQk}tUd_%IDr)I*aP!t1?8$uR)*P2k3~PAAi>Zr=cl>%O=FWy#`5WexcmK**nX}~ zd(Q?^xX5$6jS@t5+AUWi9Hq9&8eRsqPfBw_@2)1Q4b<=X%5n3YUSZl^O{0aiorj}F z%u@bhvrz+n0)?kAqDt4}L(DpM$Gf_%qjkDoD#2YJBW69smLpW)F@_$sF4q80&)-9h z6jV(}L3_A2;f0FuN7oRu>}y4e@IZ7PDRUM|{1m!ZtdtJsk8BJEpb%L~H+K)RNss=w_yofX+|& z&PB(>FGw9A$X}lWsc5(3O)nYkj_4-Tb&7(HZ_&M4@1<6(IY0Y>nhRPKiTaOm)z0|AHUlzJZ&)&#lOp`o5fm z9}MBQb<;_9KKKn%01_~zEc%IuTNR5c4ah>$)3#Sr+pk0uz zX#Of$N}%X7b=d??EO|{mE9NpFv`UGD@03ok4n8B4wL4b5uaT6V0I*(D8dqc<5_r1u zgY7(f=^4Ve91to9ru_^=0rfM<$M4VX|0?&?3@Ds93rJg7Q*Z8&!!sHv0nlI)P2%~p zTVNXB+Iwrp%LeHd2x<1IRbj}7Gyt_c+=~7gs||SiB5w(RxWF?g)lN+!mim}ukcA)Y z=nkn1r-%cTT$jd>_!9S0D?rpq$47V-1J5JAiOCfHpWPtPWF#cxKDT}9`|n4LQknIXjOCyr{A{Oi zdY=6~<_{MdbqNMyeYPj$Vn!&Klcmh^+--p#bDT2`tW6H*XQ~VSAC{W88(rNjm>;<{ z1}6pe3o}tSZIa*@3N!2tk!;ZAAY_$dk$o$-$`fHb-Y?w5Vy((om6tNaVj=zN z8}o|<<6-QU_dA<24#XLAGAsp6PL&C75TxjbWz^k5Dlr0+ekfUU=Zwcsvk_L+n`&c6 zl^ivs2&6=qhIPPpS))u=hCNlJbB+EkGy%d!zO@i8a|9JpM%;Elj^AIqmO}l2_nH(IK32STz zzZp#s_@v|1_6$o9ivlmtfGGT*EBYS*!t2ieYesdSf_+;5?W!exFbW@FH5rF+BU-5t z%i&72QY*lxG2pVIZ!h=5(Zp?y;sZq8EQXcDmswWMz$r_j_06Lxm^UQ7VS;(+u~*fEys$6LaHxU*AF1pYN=*T4e`& z<1`GE%&^Vvi$3y3SYV>9j5XT0f1Az?I}$D$e<-HY`pBQfj=KF-RUU2q&eLt0g*SeJ zU3i(+sRz^lA~DMY8`LuKnq0D(j@(a8Yf%SG`1xU62Rs0Y z2QQ%+*c7kjfXfDj)LoM{`L^j>CP8DWINs`dUKJXy?o#vJ(HoOug1skrGDV4cUCUl zT6QK6q4`TX`8+2j>u6gm{{*80zu^=&XdW;3L*BMCwD(o?T~o6%4SQEs65&5uJ+>W+ zWTKj1-2Kt544Wvr*?(}?zfNWPF%stP+Wb{!S(-37s=Zsy6xJY{Gx=8ZYvY#4FWh-H z3vUN|2WJeUghG+7JvTRkZ-eV3Kdh+g#@rFl)D#2j=K1-r1~UARRq2CAXZZ=uoAh?G z^#$^tvrGF6E=t0uiLITk^Uq;VdxgXE`{>raJ+_0`+gb!ivfQi20NW%U%ucoKfIF;( zzP>9DG+NuEYWKTisrCwur1_y7`c2YWCU%g5xjg($?$3ScxQns-SH$8E({#zt!?+M# z(nY=HAIi0`*FU{YHuKOV|6sA0b`jPyyL)jJbb6qbx#!(%C*52w7vinGc{f<5XUVV& z!Ev4++;}NFzecqhm2Fd16p!u+!9Kra}(AJ>j|`wH9w5`Mh9d zDzGln!7VNgZO9s%)ZcW82}aiKDOdLph|KDHXFqN$JSTbiDLKPsy$&cCIp-l$7%*db z0b#`LT(*{IFW&joAVPyJr3Yl1*2}9nD1SWf-+23p=uN#3uC_acx_0O$YZ?@5Mw1&~ zo?shx-}I;9KsgF(Dx#}m`lUQ765eR1sp=_sE*+}|W49vxTs<|ba|ZnHmhMVRA|Ow! z>yFMhxpWg*3D352buA}j_nzN|{}pXEz+FE%e`&fMm{XsL3(Gb-*33xpaTit!qqOV2raiZmiYm=)C9dkw#E|N~3>^abuew}^e z2R+Az$2WuSG?<0Hu3s&gMG&=?a+t|4Q5DN({v7Wd9>aLg zsB|m>w%!c$e6fd(ocOKd>~$mb1IRb6D^OoNxBe=|;d@I|Y@O|XVZ2>ZM-hO*E$5T< zxY!gSojKlXkM&0Bw_TOV?&|PZ)+ii$d{`J@i{4^p7H?F?Ig%cd`hh1Dqn6}I{&^88 zdl7HEIfwWVZpZMT8$nvH+paj&Ru7%`}e)~uvS)*J$q($HUru7 zzQ5;rGxc!O1>y819ybD`Lue|d8AR+;tZQCW1@2XyAz@{15W?9yDU!>qX6W~~AG@$rXkT&rpr-QO}P-wy?&mX3Wfe55F*mYaFguQE+h`skb+YuZc5q_*8~ z&)i9xwep#;WQigzh~tLgvt%1#0krkG%*WlIiioM?v(#(*pf#%PON4soM_M>No2pC1 z)hVTvCwBELIkxr8BZZit-%qbjKS&zFSwRr>r#6r8f(y5?ThhgeTdQe=ii=CFyofE0F2hiG?Rx2)WElrh>|D4(1W;?FV?`~c!u z2;cY^{}19cEcLEh=HxXd^IXiGFo_{INFG7nOciIEEz^R6^5~1G-0MXYi8WkT^UGm> zdd0Pa3bmXgXlTcJlaVDvM@wKTLdsV`3sr2HrHDIK274xUNBisjlRsOM3d$rNmfR|` zkoG%*P?+2hhvA|~JEm!L62QSW9EP1-Ec^agoj(wN4fSzZ?esoj8`5XUowC^b4N?cpRjuJiQyT@$Y>mBBlzar z8Q!Z>U{zEt)G0Ko{XvxM$Hd3o$x_KfVKVN^S^O>1^+F3uLru# z8w+p9PJU^k>l(yr)zR-id+U9SJo)+@Z7`tuVI&@v7v`f)TWap}bqh^iYPYxDXIF3i zTuH}RO(jyrq<&-BZGbm6WT-Nk;Xsl3uEynnh?|kixm^ald12A-+u=X4(59z+!H36K zGfY-?qcmEDRtbId#ly8pPyKGqC%U!XA=OW^&E%Ia5$O@&OvM4oZ}$!xsRWueD+nO9 zoxC61$qEQiJ(ig$rrYBLh3ZE(WbXQ5fMIOz#xs6HVsJKOYzsu<38UKS3gHH5^t9c; z{-fBJPyQ^k1M~ZkgBN`cs>W!q>X|=la?P8g^N?&I3voUY?H0KvqL1m;4d!vHzB~PLBC_<;5TVdWIIwx3qkys+(0b;@P6+X%PM^V zKRxuWg7|+*6a3lKR;W0TXK2V+Lkr1^2ecJ@YyD@H_Ns_@UW|Zt5$a-N%`g&67y?|#JF0#ZB|K= z8vGM@25HSJMAtW?O?+|Fr%=WC;GF$LX+Mv5>Fm+DE$G1sQkV81ixN2}^1yr3dG$pU z?j4l<4X|NK#Ii3KL9;{rAr$cz=*58`J*6Y!V{}}b@>;qnA&emC#y8OKBPft(Up6{b z^#Yw604!ceiMy<7D#^VUOoo=i6!oq{1;LBXIv=yKNpb$w@XIKlqBtHT85i0;Y&vSv zSM#d&{*#S}lVF=k>VY9#y{vkx^f|uq%|bvxzmR*5DhsA1rA$b=L>3`x{rwv2cv_@C zg1wmP&xqFo+unOPyX_j%akcDy4>?%!PbY)JPByliD(+s0L#-+@K$2lenj@Wj0|OEb zV6KB!2TjWT`*AV--`(F`O|_^DVV(7bY$izuhG>F*yRb!bjxufy+7pm`(*%W+R8$T3OB=sNL8ID6~QLbIpd0B1U3*)UTL2;RWcRKK#xWEj^S^_%W)Mu^ok=izI zU6Yg#pFM{S@dESz%?~L0Z^8oSc;>k>&b$W&1lMQD7}NVVrGew^z~Lyd46+l%X6ovm z2X$bYf;B)KindNMW`W>tu4SCxbC~Fol6L*0k=1OAN)E_@h5Yxcj26m&5AT-9TOvw|GsI1!O63;DT`i%wcx| z0Gyg%^c84wNgN0+=lcdAj0vPJ@${V~-!Orh2^_;x8;Gg_0(}@e{v{Z z0&nD^xWl74kgxm_M;EAsCjlP4=QpEa{pLk1*cSPl)^N>(<1HQpSLL>kc*TDFjQ?#Y z-t9MAmKK;H#7TN6fa6Kpy7$ejXZ6XcR5Xgo$ZtCvG)x(z!| zfn%5R$}R5PL;Y6-&~eatEda_hylyEij zfofvF8Hs)i+#z|AAD=SEvQMN-?5guB;iZCM9?*&!KftGd={!EU>@D1t+7e}1$3cx z7K>W=GIJ#Cd7bq%=$7gi>B9dYduq~jA_PKjKfO>t9 zcOB{{_V*Pi7yh(Xj#Ds_jS+Fu53pY!)ZFnnX}gIt1kyvyd!Aq~e!Ia+^>Iowau7yZ zEpVSOAi0|gMfpU(qxiyPH|U%?`T)lzUshTCXnXJ`jwWb5%N5apv+^;j2m?Uh)~C@^C`&;jJt`? zc0pdccY%)6&$fvE%g%oBlj>HI%uMhg=X1q1Ocy#R(=D!61dlyo$3bhl*sy$GIa5aB zNG>L`+>KyWFNpnv(G9~dzn=+Tswi8_*xW_fc=l639NmMOU6azo!c5aWa~FupJZ-@Z z*fct*i80x+Q_jz*7HRk2dnwxWgP&fm#h_JxowD>Z!1 z9(=vU`s5nJxBGV3r0qbYzA!`czzC5db%#juu}VyQ6epY(zy~Fz3w=mXsXsM%i8j5# z7}m0rUC__pAj@B4%b5n=vbIyL345Ssd$wBrG8hZp`B>INtfDVC??&WdR%V!(bRepR zq#`3BT~D$-VPqQV1Qj08&`A}RBlFNOeC4?oGHL%3V0SfGumy~_wwBC zq-#CwHl!qZn9og2VhuxeoW&7IE(dA0$|3ag`t8-tI5~6OuVD}=3f>|t;!Xk4x8|uEj!P(FdjZg+wAwBZwLHGT-*DT z>h#y(1V!lIeYz&4HpL*d$vnqb4mUaJ4GTnidVTf=vnr&Zg?lmm4DbR5AF|u`41`iK zMfF`oihTNaA%y3R<}N60vX+j_lS9t$@~&a^RDr)aKLWm@Ao_N@ZW9_A{eszwfQ#`9 zo^5@-{~KO;G(nH{^yq%a3e*8V%0RAR0{oS`gGN7&!2n!(m(dN%o1TFSGTOCVd3&br zqPh=UAUYlok3`}-L~iHIbqnDwA?@}K2u#(QZzA&bP^#$KzTbWyd!aE)5 zbgy@#@^n{cB0~UC7p`IKADfixk~Z-`ZR_nGm}e`{wcO}yaB6+PUG18CpNDW&Z*{)w zpLR>2FkA1&Rqw{VQ>kPWlaKhLailH1%MV@jU5{l=eO$fh)!EKsKqzQ4ue(uqA-BAK zMviM@PxCuWGx;%OXx~jpXn4{FdvI5hBCM4D3U*~YVN;*vUHzb)X2p>EN*c#ya!aDe zZEHFkBe`rX`~-4`;o&chPO{0Lu>?Zq*%;^*26*_qy0(=%05_G-`6PX{ywt|ueOoR*hbaRBM;5e^!;n~Q;f=XB~2`=?Lhg_62+KJpGDgNz|mvUQ( zagY;>Tg8IE$Nl=V3s0e0U&hRy=VWI+C4^-krkOF_NzIpwNUhh)bX%uK#T%Q7h1Ukh zhw&hUip|wxtr%E6X+yGiv|cRg!soHtkm2_7;eHy834u6yI=}%B)tcSuZ~nR-hiy7q z98u7uehq&MlqDJz2$5mK>w(76QF#{$;XO@>_1Ht;pcpm7zns^2lH}4x_~?3fk|NQz z)@Kza2eEOs%crv{wk-gKZg263q5WlO1;AmG*f{FMD|Ds*ScP(@E9%|U>xycJmn3Yg z?V5XSXeXjl7s2ZfLYH8)UITSl=y%pC=>^Pk3&A`SM8|a*+w_F|H_DY(4R&*NPU`et*10Eo_@xI3Fk!OEC{@NRd0Vzi@hgkpP<}3k2 z<>BMGXLiuXBzaWt9qJnFI_^_X3v6FMS=`hJcxJ@f_{LE~q2UD?I2(nQeq&8mlSGl{ z5oI*}?Q1Vo6Qh%vi#5MIUCD;xgzgMRJlVt(UwOvc(j!U}ep5(Gm!TA@vm1%E9S^$okG*)N6%%9{d;7M1%J@-P}p3X!5HPf!`7g3_-huf4Tf>3mwjUZ&C z7f!2FaeLffgO=9&94n`7mc-IxC3e}Dg@2Rcg4zbpb!(gDn9}`yQVDFH>mYyddQ~|F zP}SMHHkmR}4VOHQ6pc4Npp_c3t#!a7&F4h(RA;sB3apPgckohdTJ<0sb7>;>VA-5A zx7Jn3I5!H&hS|Q<=uA2x6HHEWM5iL;TJfQ75Z{-nXuX^#z(W#}-pB7mtXJL|ABi(m zTlhPS8bnrx9o3FhHF+l>r#&EHML>!CXG}Ex@&e=>O&cDaQ{gb_rF{v{W|JB({q>8s z;5f@MmV>;<3BejCBYs#C zd;U}&DaM2%(o3!5Lfo~xn;bkRXYjvUkru;EknNQQ8n5_dcl~1eFrk(%e$w?IrRqJl zRqQQiaC%-7Kg+3E)zcWK`h5(6aELY_!|E&23gB`Yu7EX6p%IDc{>SY%% zu++C9=pA!zR-i#vzF7rVb@s{CuDIjG=z}jJi&WpmSL$KCCHJG0n-T9vwdYIq4gE6L z!0X5)MkS+a*V%-;CN+0IL&{s^^Lfa{7#zJ4Com0{wd2`1glLa;NSM4v0kblRQr16R z4qVDF`9Ck=bJX+uY3I}DzglUwVOKMz%y*OCuwh4sABQ0tGb}%Hw2lgJ>>bEcegBrpV{sD z@^h+|>F?M(7*u5=ecR8nMBFEOjJ?WU>QGdXCKV(5!U;F~BaZ3LQ%9JJlgnjITAj2i zTa7ZtMpX|mjgw>nc#R%nQsSV|4 z0r}yki}Nh;mQ0C38S4VJj`rc#Mtt~dIJL+ppNNA?1pyOjGo>O>PrH6LFYpWjosy|k?G4ktUG?$qXO7ZrIV0DYbe)Lsn4jI zazUsVzk#y2(y_;2)o?TCm8KKI4NJ^Lp@iA|Ns^^22Th{Ln)^eZfCD-8sr7R=CEz+J z{OwGf_)-mBAhfIgOjC@Y%0x=J2E4nSFoT#v@?g&{(35r;&G(HlgO#qLyZ$}=cp}zH zMm?HnP>^Twwwkm9Zkp2lg|IIUs>)gQgp3;X} zY!;I8fFjHK?8k6?w{gKcV6%+1ZQTN1k9OxWZMos3h@^@yp96>=aNb>OQ72zI6Z@A5bt%~J7nUxkUv(H z)Qhp<;H)a69gD2JzW;GQ%3h0_P!CR%dJPJJZtmX#Jpwez2^4~Fj_jNLA_kxig@QY+ zH!{Wk$muibH6xu;VenqgB)c8Lh!D^2IL>8d8v_XoJvDTzf@=Mui5&!uQ18%3Qv|2& zerQPkYlC&bqn`N5ILpF%%FmZRZpgX2TRrtEwo8(nR$wDoq!9(2jOttr?LYq>MmJFT z*GEFBDpRTJnz;r;5oUCvy)RKnF1(ie;m>qTAlYE-`to?krD)+ zdjGKbQbJ3=h5|cMp9l8cn6`vnUVVDgW!5!l;hx>kH75`?d8nj}{#gII^EPBPH%E2Z zjr$vFbG!tzYpKm+G8OAAY8u_-lRGf$8j&s%Layn}r?KMOC?n%vMpy#U_>=4gGao6T zwA>_=;czUZWIm>`%9c*x^&R$+$gTG_*bl~4gFMM@C7QE`>j|cya$=(Rx$<-wcThX@ z7(sRXYxeTJyRtVHF1sxU$9m{fiff(@4p4EDyk^cXzx_}K&8c(@3%8~B^+jd_A=M)U zka<;Q@hw=M0-cj_PFie?VR#E{*?hFrKMF8tG7JL-Z=0^|Dri8ey$Bt19?g@uZPgI( zyt@v!^vc&HjSLYVRLF#Yx0TgqCPJ^a2pt% zuX00xNsvrN)s^8QEB1VtdJdt^OSBN8lb7$Z=Ege0^!^8dkG69W<_3N(c6vXuVYI9p zC4tb=2>#O>-z7#LxI0)r%5eA&I+jloQpz2{0?jRcJ@gwn-6d<>p@Hz-E(PHHG@#fz zhQO2uf+#uunsR-{1fp&ALeaMDfMxsr!$_u$u{8X)2ZCYQFow%dvOdxf#6_)^TfJGmTkLIi&RD54n2~!a_}N zn8S_r{Qk}oM|r(D*7za>T6W=U1*asT+dPQoys5IxF312hg39b4*9gg5tv7P8l93r-qf$ndXRtz9v9ligGUet8SLJ_I=; zB%h`RB%4&<;bF!Pv%qe_C!z0cN;>5xYdWp&-Xf-AU1v`a!X2}BriS(@9`GVvei{e3 zx|C?ysYPN)siSyTs|eob2TF`ltqBIqVv)?32tB#wFo62?;aar#P6m)`WnrE%u2f5sJRK;I7w){ zLO^0UW||t!4{m}`5lSIdF3aKc$%EAdxaYS@(R7(TW~*^D%zQ&FTF&zz+<@1uggY(H zMb^7VV|=7YLs@n?anI=M6WV$BXXw?7%cnayowwoit=t78DW4X>S_Iwg_^;{<;hy81 z^p3Q)bB0zweTC>e1k}YBz0YbQ<3lE>RsVw~!ki%RxIS~FM!@OmZrtJa^2@1)o$Taz zo?My799wGZP)`eU+d&9g8NK9|6P=)6o2jw|6C8cfTVG~-Cf)nT^9C<_*&|n0lT2Baz4mXwxPHF zY5K(Fxbjl*d-FM!EJIvBEf+jXl^1<$qu@GySi^t{fLQcXT_F28uF12P=QhfBacZhY^tI7JJS+@`-Q7bmEKdm9?ai3F` z9PdEOOeLj=eaSJ3lBbwJn|aEU+?PWC|@D-FaZ0V2i!m zJx-_;x`h5SR2lJ#@vIp~&lB-vVrCBJEm1GM@XrE4&mdxqsK|F{so_dCzc%Gv?92lY zX@W2^{NbKV?hbusKhoWQMgkU%x~h9%HGQ&Sb$v;5l4B$%vg*xlr(0h0%~Zrdtu*@G zv#Nv^i>C~l#N^AR(+A3ElS{DNY3`75q9&BhVB#w3)2syoo#c5}@WwaV{H8_DnR-hX z&Hb9*_6}Kx_sPiIvfKPuCbR$2^fpWG{-6gdrdF$BD>lGGAcBt=S%0@$KxB{BV^LC@ z(~eCsf}Lj71LVU_0mAx~`b()zuMYzJvme}Y;<1?`@aWz3MigRR`;u5wf#qMZ_NGLX z5@H2v-de3L0dK(+nsmjw)g26qi79$#>6LCCt4{OQ85OMODzhl{pwLH&c)%@UOFBMI zAo_Og3(6EW1F*L|b9n?So6sC_GN^JL+zf72EjBu?vMx~QPFjc@6u-JF zeDPyu=f}Ur#o%}7Ru1PzoZqm&0VHhcA#ZPoR{eyC5wbzy-0oY>rdTcaJ#TzQ?2;J$ zMT|5Hi;t5}UDzvtc&U<2UvQm2*%vgB`_CX&5oY(|^gxmd$jt#_#|9ljY#**{J&Il0 z$}3=poalrXYBvw4PTTl4FFak$DJBN`mS>g?SM@px;~g8HS!#}5Z-wp39zBXiEF*ie zL4(jVBqpufISbPPt!7FwW%O@)kz7y=9yx|`Yi%|6bX_a&`^EHpE~)_EwH+f_5xL+LLy@yw+2Hl=pb6$O>H^cGML6ttao~1J!WtS(mz+_%O?- zFc0UmT~o==eLMBTnrL??)NJyore1b@R=~#(zhwz8a&ADXhK`ko>E$ebHK2f?>I|i0 z$W}cKwl0-73pnI}}A{=~uU`O=BW8Z!! zUp(~=co2y~B{Kq1!N5;{5{?J5Xhv`UautQgU6oYC+FAlPX!F6sPyk_YaonIYFZEi= zq{>tACV^>mW=%1kQ1u^k!e*)?dfTXptuKu&80m_)aC%6`^rIj<{j^y9D(Fycao}^c zhcET6cGJ=?D6khU_4I=>(9ocRQSF2(s2Z7GCJa(HnWbYaY^+Q}sww;SpS0NUevBju zhUjZ4|FhrTZ@ukN7m7H2JhOL3C=qL}kRvH8vDCYW>)&o2R9_sGg?zgHiOze2aaik) znz||>c6?7PW;hS6qOF*SsX90IISJ#bqR-joOt)?EIx8qJkZ7%R2l^$k%LQ8gPX(OV z_5Jz`bMv;Yx1L7)R_+74sil|t0O~b>mdixEmi$X59&4a~oJ*Pe7fCxoMZLdfm%64^ z7hAF35y6C`O65iUfR^ARH5P1=HkY%|Be%cqg%7*qSqc%sz2^|+VSb1;tIsp*Cru^@ z!<65zE2TQdtBqsVP6+$DjYmuVrSYBxJ!JSO9eNWlM~im13@$D0%HzH5qfQii45e(} zi~ifwaIn%!6OeZ7H+cH;jeB6=39#u;b*4}$8jTU8^sF1+Uzw;loN~std!&PAIP^!Q zL5Ut0eJO9Ns%5;f)U1=jU(ukKjgWD7TPiTAHJTkCqjCiMbnTFR+r;%fkXXDuk1dV& zp&7RtcFJ31gB=bT+?jWKYEsNKdjQgr9_Hg)zEBC$43{%FW&OKd@GzyO-NN!2(# zHCUEAF*V*fQ&_hip3OlhMtb&@@r)(z!&2*x5B=fvtS_|ij;^Mi^6UNX+0=vTEuTm7 zRVbB79bZF#@AVBsMa51fp?8rRIo!c5JdsOZ7qf*B_W#ue5g-J~CiN}?j<}YrlbjT6 zhg^*N$GG<4lRma{s_uDxh@pK{lyOVWZ(+fTFyLs{L@Cej;<8uJ!uxZW>=GTj_jowu z$gbLRr1Bod##@Y_=%D?NM%{qIPNJ=)U~n&j;JEps+|c)%&azI!Hv7r_A zeB@`dzxTOMx%1%QYz9oQ5&ySzhviIuw(#UZxeK@K04+QR(NiN z>XECrdIp^-5xd!l7B2}fJ+R0YERHoG7qF=p2b`MV3IgDfI93h}vbARnN?zsP4=?q( zfB5caVdFDxvcEccMbdmwH_!OFQ#VqpB6K3+gFMp>@%vDlwcOxBX>_`ZOo`1Jnep&z zjnX20O665>uIaPYE497qnGE|N8YKWS6!ms1cl`j9im<4|vH+ZDAgK?hmQRQerM zFP$y~PqrUEun*2zb)M4z%l-aSn(s2G)wMsxQICZMSu26PPt zn^kt{vSwh6N^gU}9pxBG`AZ{5D-*e|`9dY!AR07pd{RvbSsfPorIimDs(P=_9Ka={ zsrQ^l(WUbLvT^+E_ZZrjmuzS}SIEQAur}@!c#`rVdn^YA$-wvs9^7AQur?T9=jcbd zif#}=m}yo@6sj zftOAzZVYBv*&8}en$w1_;^cI&cbcmD6y(7GS*G3(8lpB9^d$ASBpCU1Ae@x-9q*=9gK3=*0o&sLj4eR!^-IvPl z+42xvQnuzxjE%oPOj$wh#~OU-Te%+y7_`}y zD`?k7*8GHSuSC+N4=EnU12lYz2&&jC$P1s3c}0>D7%Az_6*iILP&`8|6@B?X}vF} z_QHb-QvGWJqV)eipOb%F2$AoAdF(nnA~8t4)irM>WS|l>_jrm2HSy-X?Xs)F&NKM8 z0a@+yloh!cqs+<`3iMuwgs>N3s+Zb0t;0>pAj!ng8wVLFfL;)_gzl-c&7{hJX2M$s z4_a~2(QZnUmT({n++z^RMI(kC)BlPYw(d($JLHu#-VGE-?DO5@2fdl(;GP*1^_Qax zU`TG~1OGIg+ERTxg~n_Socu6_!?BXi%NS_zB7JZN4z>c@#d=!vGbeM8)OW z;0tW#8rE-sZbB5)1#cttW?w$CVehTNZ*-X-H~yg~$G&OvDtgM)s^` zIp#DrU};K5V)7?&mP1A0tiic&fgyWZyJseYE(E+_>9P{qTPkRuXMfvYEHMFt{h189 z2K2o@r)BY)M6?e*h*ZHM1%tyr_S4D9i5e5Z%&glyXhQ?oCv`q)Wv2#wnTlr5y)+`4 zBVjYTLw0SaAkaeNnfUp$h~w4ISm<`Kf%;K>Uf#XnfoB7nDP-pf{hyoCkQN@4hBND?Plu{ZnYadW3JR%+MI1uK+I?Oue0`lShE6Xmk=70aF-Tw69!lF@1iuFib>lBhzA&Zd1_I>I3JfdCl37X_nuSzQ zOb7@F?8Lf47)hWaQvISr>Fl*NPm*IkXCor|VEk$wg{5-fKJOy+c}dUm1$XZ8LGC~g z4ZPi8*G{iPMeZiuciSx>0!XS^6~=k|&4}h+)oc-itVec9MsIf80%$%J zER?;z&qoke_~BP}F4Td3tj!70YZMq72&mmIW*FD?J$R>1eZZ4&_vUN--tX6=@FBCi zNlpkPUPW1goiLQ|#<%Za@BvT-+i%w1Co>6fcdXTHPKI*lhFC|*RCJMGSx7R$zsLuR zR+d*JyAHX87JMipq5NxvkCyta=>AOChl$7{G*XP)d+qH6DMV;L-i|CbUV%?!NFSuh z#h-)iT06WbpM>+m^oq|atTtRWYvCTp)1@qQt1PwCQ{whW*i*gQQ%JS+&y$8fR}NoG z>gg>XFOx?*FK%8`)&q76&T2=ZV(HWbn;zqq14Hy3PR%r~eLj2#<;Kwx@KVgN7-U|g zRS>H{3l+SLa_zKw`faE{(uXUm%&LbiAshBRMD23!aP<;O)Qb;c-j^{84FMi>D05Nm z*z($}eJrrrG_rbx-MrWhlWbzX9`E-1q{9L_6#N=2iTkMydJp2LcIBF%0K70yCWDF~ zTiHsGgZTZS12zLg5IK_@@X5Ar&^PGAnk4u?$!>d;{KAunj1XeXccBc2(1bjwkWa{% z15HH4$J2_{(DGMWM7@NNqJ=5F@nFA>96Aqz5h!>d5e#ZtcQ$PPwyaM?f(t!sQyg_l zp^NmrXExyMB&95QnpVh_{-Z`S%-q1P{&5EK4ym@3^_m>;S=8?SP2BVlT!{G3YBQ4& z`ETC0nGKi*hNuY6zM<${Zq#TSPnmPHwFdw#ZK|P8F`x#gDmgWb7jUNvTJ#u(NOV9@1_p6sh{XXiv z$4P0kMpMgXqIR$&qYrN9)@=UTEqe_jzI5DWz%FY2T>t67kPw0CJREb&qCA2Sde*tc zt*YqB6a4KO#Mg3nQOp14_13HUvP)y42qGw+2jTzWA$R-g_h<}G`7=Qs2oCt8D696O JRK_gue*uKEw#)zk literal 0 HcmV?d00001 diff --git a/doc/mkdocs/Makefile b/doc/mkdocs/Makefile index 85bc6a9201..1f3ccf54e7 100644 --- a/doc/mkdocs/Makefile +++ b/doc/mkdocs/Makefile @@ -12,7 +12,7 @@ prepare_files: clean # create subfolders mkdir docs/images docs/examples # copy images - cp -vr ../json.gif ../images/range-begin-end.svg ../images/range-rbegin-rend.svg ../images/callback_events.png docs/images + cp -vr ../json.gif ../images/range-begin-end.svg ../images/range-rbegin-rend.svg ../images/callback_events.png ../images/json_syntax_number.png docs/images # copy examples cp -vr ../examples/*.cpp ../examples/*.output docs/examples diff --git a/doc/mkdocs/docs/api/basic_json/array_t.md b/doc/mkdocs/docs/api/basic_json/array_t.md index 89e39dbf83..f84e645bcf 100644 --- a/doc/mkdocs/docs/api/basic_json/array_t.md +++ b/doc/mkdocs/docs/api/basic_json/array_t.md @@ -6,7 +6,7 @@ using array_t = ArrayType>; The type used to store JSON arrays. -[RFC 7159](http://rfc7159.net/rfc7159) describes JSON arrays as follows: +[RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON arrays as follows: > An array is an ordered sequence of zero or more values. To store objects in C++, a type is defined by the template parameters explained below. @@ -35,7 +35,7 @@ std::vector< #### Limits -[RFC 7159](http://rfc7159.net/rfc7159) specifies: +[RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the maximum depth of nesting. In this class, the array's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be diff --git a/doc/mkdocs/docs/api/basic_json/boolean_t.md b/doc/mkdocs/docs/api/basic_json/boolean_t.md index 926a5f9f46..a611514104 100644 --- a/doc/mkdocs/docs/api/basic_json/boolean_t.md +++ b/doc/mkdocs/docs/api/basic_json/boolean_t.md @@ -6,7 +6,7 @@ using boolean_t = BooleanType; The type used to store JSON booleans. -[RFC 7159](http://rfc7159.net/rfc7159) implicitly describes a boolean as a type which differentiates the two literals +[RFC 8259](https://tools.ietf.org/html/rfc8259) implicitly describes a boolean as a type which differentiates the two literals `#!json true` and `#!json false`. To store objects in C++, a type is defined by the template parameter `BooleanType` which chooses the type to use. diff --git a/doc/mkdocs/docs/api/basic_json/number_float_t.md b/doc/mkdocs/docs/api/basic_json/number_float_t.md index c6785eb260..0d0182b803 100644 --- a/doc/mkdocs/docs/api/basic_json/number_float_t.md +++ b/doc/mkdocs/docs/api/basic_json/number_float_t.md @@ -6,7 +6,7 @@ using number_float_t = NumberFloatType; The type used to store JSON numbers (floating-point). -[RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows: +[RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: > The representation of numbers is similar to that used in most programming languages. A number is represented in base > 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may > be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that @@ -35,7 +35,7 @@ With the default values for `NumberFloatType` (`double`), the default value for #### Limits -[RFC 7159](http://rfc7159.net/rfc7159) states: +[RFC 8259](https://tools.ietf.org/html/rfc8259) states: > This specification allows implementations to set limits on the range and precision of numbers accepted. Since software > that implements IEEE 754-2008 binary64 (double precision) numbers is generally available and widely used, good > interoperability can be achieved by implementations that expect no more precision or range than these provide, in the diff --git a/doc/mkdocs/docs/api/basic_json/number_integer_t.md b/doc/mkdocs/docs/api/basic_json/number_integer_t.md index c10cb7803a..616b0b8a21 100644 --- a/doc/mkdocs/docs/api/basic_json/number_integer_t.md +++ b/doc/mkdocs/docs/api/basic_json/number_integer_t.md @@ -6,7 +6,7 @@ using number_integer_t = NumberIntegerType; The type used to store JSON numbers (integers). -[RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows: +[RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: > The representation of numbers is similar to that used in most programming languages. A number is represented in base > 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may > be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that @@ -36,7 +36,7 @@ With the default values for `NumberIntegerType` (`std::int64_t`), the default va #### Limits -[RFC 7159](http://rfc7159.net/rfc7159) specifies: +[RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the range and precision of numbers. When the default type is used, the maximal integer number that can be stored is `9223372036854775807` (INT64_MAX) and @@ -44,7 +44,7 @@ the minimal integer number that can be stored is `-9223372036854775808` (INT64_M range will yield over/underflow when used in a constructor. During deserialization, too large or small integer numbers will be automatically be stored as [`number_unsigned_t`](number_unsigned_t.md) or [`number_float_t`](number_float_t.md). -[RFC 7159](http://rfc7159.net/rfc7159) further states: +[RFC 8259](https://tools.ietf.org/html/rfc8259) further states: > Note that when such software is used, numbers that are integers and are in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are > interoperable in the sense that implementations will agree exactly on their numeric values. diff --git a/doc/mkdocs/docs/api/basic_json/number_unsigned_t.md b/doc/mkdocs/docs/api/basic_json/number_unsigned_t.md index a28e25351f..49f0e31780 100644 --- a/doc/mkdocs/docs/api/basic_json/number_unsigned_t.md +++ b/doc/mkdocs/docs/api/basic_json/number_unsigned_t.md @@ -6,7 +6,7 @@ using number_unsigned_t = NumberUnsignedType; The type used to store JSON numbers (unsigned). -[RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows: +[RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: > The representation of numbers is similar to that used in most programming languages. A number is represented in base > 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may > be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that @@ -36,7 +36,7 @@ With the default values for `NumberUnsignedType` (`std::uint64_t`), the default #### Limits -[RFC 7159](http://rfc7159.net/rfc7159) specifies: +[RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the range and precision of numbers. When the default type is used, the maximal integer number that can be stored is `18446744073709551615` (UINT64_MAX) and @@ -44,7 +44,7 @@ the minimal integer number that can be stored is `0`. Integer numbers that are o when used in a constructor. During deserialization, too large or small integer numbers will be automatically be stored as [`number_integer_t`](number_integer_t.md) or [`number_float_t`](number_float_t.md). -[RFC 7159](http://rfc7159.net/rfc7159) further states: +[RFC 8259](https://tools.ietf.org/html/rfc8259) further states: > Note that when such software is used, numbers that are integers and are in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are > interoperable in the sense that implementations will agree exactly on their numeric values. diff --git a/doc/mkdocs/docs/api/basic_json/object_t.md b/doc/mkdocs/docs/api/basic_json/object_t.md index efc18c41f3..e937d847ed 100644 --- a/doc/mkdocs/docs/api/basic_json/object_t.md +++ b/doc/mkdocs/docs/api/basic_json/object_t.md @@ -9,7 +9,7 @@ using object_t = ObjectType An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a > string, number, boolean, null, object, or array. @@ -73,7 +73,7 @@ behavior: #### Limits -[RFC 7159](http://rfc7159.net/rfc7159) specifies: +[RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the maximum depth of nesting. In this class, the object's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be @@ -90,7 +90,7 @@ Objects are stored as pointers in a `basic_json` type. That is, for any access t The order name/value pairs are added to the object is *not* preserved by the library. Therefore, iterating an object may return name/value pairs in a different order than they were originally stored. In fact, keys will be traversed in alphabetical order as `std::map` with `std::less` is used by default. Please note this behavior conforms to -[RFC 7159](http://rfc7159.net/rfc7159), because any order implements the specified "unordered" nature of JSON objects. +[RFC 8259](https://tools.ietf.org/html/rfc8259), because any order implements the specified "unordered" nature of JSON objects. ## Version history diff --git a/doc/mkdocs/docs/api/basic_json/string_t.md b/doc/mkdocs/docs/api/basic_json/string_t.md index acdd351b54..f6287f8949 100644 --- a/doc/mkdocs/docs/api/basic_json/string_t.md +++ b/doc/mkdocs/docs/api/basic_json/string_t.md @@ -6,7 +6,7 @@ using string_t = StringType; The type used to store JSON strings. -[RFC 7159](http://rfc7159.net/rfc7159) describes JSON strings as follows: +[RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON strings as follows: > A string is a sequence of zero or more Unicode characters. To store objects in C++, a type is defined by the template parameter described below. Unicode values are split by the @@ -31,7 +31,7 @@ the number of bytes in the string rather than the number of characters or glyphs #### String comparison -[RFC 7159](http://rfc7159.net/rfc7159) states: +[RFC 8259](https://tools.ietf.org/html/rfc8259) states: > Software implementations are typically required to test names of object members for equality. Implementations that > transform the textual representation into sequences of Unicode code units and then perform the comparison numerically, > code unit by code unit, are interoperable in the sense that implementations will agree in all cases on equality or diff --git a/doc/mkdocs/docs/features/iterators.md b/doc/mkdocs/docs/features/iterators.md index a8b88e2d87..fd9d6791a6 100644 --- a/doc/mkdocs/docs/features/iterators.md +++ b/doc/mkdocs/docs/features/iterators.md @@ -10,7 +10,7 @@ As for other containers, `begin()` returns an iterator to the first value and `e ### Iteration order for objects -When iterating over objects, values are ordered with respect to the `object_comparator_t` type which defaults to `std::less`. See the [types documentation](types.md#key-order) for more information. +When iterating over objects, values are ordered with respect to the `object_comparator_t` type which defaults to `std::less`. See the [types documentation](types/index.md#key-order) for more information. ??? example diff --git a/doc/mkdocs/docs/features/types.md b/doc/mkdocs/docs/features/types/index.md similarity index 91% rename from doc/mkdocs/docs/features/types.md rename to doc/mkdocs/docs/features/types/index.md index 94e40cbaf3..b2da9a9111 100644 --- a/doc/mkdocs/docs/features/types.md +++ b/doc/mkdocs/docs/features/types/index.md @@ -1,4 +1,4 @@ -# Types +# Overview This page gives an overview how JSON values are stored and how this can be configured. @@ -107,7 +107,7 @@ using binary_t = nlohmann::byte_container_with_subtype; ## Objects -[RFC 7159](http://rfc7159.net/rfc7159) describes JSON objects as follows: +[RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON objects as follows: > An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array. @@ -135,11 +135,11 @@ The choice of `object_t` influences the behavior of the JSON class. With the def ### Key order -The order name/value pairs are added to the object is *not* preserved by the library. Therefore, iterating an object may return name/value pairs in a different order than they were originally stored. In fact, keys will be traversed in alphabetical order as `std::map` with `std::less` is used by default. Please note this behavior conforms to [RFC 7159](http://rfc7159.net/rfc7159), because any order implements the specified "unordered" nature of JSON objects. +The order name/value pairs are added to the object is *not* preserved by the library. Therefore, iterating an object may return name/value pairs in a different order than they were originally stored. In fact, keys will be traversed in alphabetical order as `std::map` with `std::less` is used by default. Please note this behavior conforms to [RFC 8259](https://tools.ietf.org/html/rfc8259), because any order implements the specified "unordered" nature of JSON objects. ### Limits -[RFC 7159](http://rfc7159.net/rfc7159) specifies: +[RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the maximum depth of nesting. @@ -152,7 +152,7 @@ Objects are stored as pointers in a `basic_json` type. That is, for any access t ## Arrays -[RFC 7159](http://rfc7159.net/rfc7159) describes JSON arrays as follows: +[RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON arrays as follows: > An array is an ordered sequence of zero or more values. @@ -169,7 +169,7 @@ std::vector< ### Limits -[RFC 7159](http://rfc7159.net/rfc7159) specifies: +[RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the maximum depth of nesting. @@ -182,7 +182,7 @@ Arrays are stored as pointers in a `basic_json` type. That is, for any access to ## Strings -[RFC 7159](http://rfc7159.net/rfc7159) describes JSON strings as follows: +[RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON strings as follows: > A string is a sequence of zero or more Unicode characters. @@ -198,7 +198,7 @@ Strings are stored in UTF-8 encoding. Therefore, functions like `std::string::si ### String comparison -[RFC 7159](http://rfc7159.net/rfc7159) states: +[RFC 8259](https://tools.ietf.org/html/rfc8259) states: > Software implementations are typically required to test names of object members for equality. Implementations that transform the textual representation into sequences of Unicode code units and then perform the comparison numerically, code unit by code unit, are interoperable in the sense that implementations will agree in all cases on equality or inequality of two strings. For example, implementations that compare strings with escaped characters unconverted may incorrectly find that `"a\\b"` and `"a\u005Cb"` are not equal. @@ -211,7 +211,7 @@ String values are stored as pointers in a `basic_json` type. That is, for any ac ## Booleans -[RFC 7159](http://rfc7159.net/rfc7159) implicitly describes a boolean as a type which differentiates the two literals `true` and `false`. +[RFC 8259](https://tools.ietf.org/html/rfc8259) implicitly describes a boolean as a type which differentiates the two literals `true` and `false`. ### Default type @@ -223,7 +223,9 @@ Boolean values are stored directly inside a `basic_json` type. ## Numbers -[RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows: +See the [number handling](number_handling.md) article for a detailed discussion on how numbers are handled by this library. + +[RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: > The representation of numbers is similar to that used in most programming languages. A number is represented in base 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted. @@ -242,7 +244,7 @@ With the default values for *NumberFloatType* (`#!cpp double`), the default valu ### Limits -[RFC 7159](http://rfc7159.net/rfc7159) specifies: +[RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the range and precision of numbers. @@ -250,13 +252,13 @@ When the default type is used, the maximal integer number that can be stored is When the default type is used, the maximal unsigned integer number that can be stored is `#!c 18446744073709551615` (`UINT64_MAX`) and the minimal integer number that can be stored is `#!c 0`. Integer numbers that are out of range will yield over/underflow when used in a constructor. During deserialization, too large or small integer numbers will be automatically be stored as `number_integer_t` or `number_float_t`. -[RFC 7159](http://rfc7159.net/rfc7159) further states: +[RFC 8259](https://tools.ietf.org/html/rfc8259) further states: > Note that when such software is used, numbers that are integers and are in the range $[-2^{53}+1, 2^{53}-1]$ are interoperable in the sense that implementations will agree exactly on their numeric values. As this range is a subrange of the exactly supported range [`INT64_MIN`, `INT64_MAX`], this class's integer type is interoperable. -[RFC 7159](http://rfc7159.net/rfc7159) states: +[RFC 8259](https://tools.ietf.org/html/rfc8259) states: > This specification allows implementations to set limits on the range and precision of numbers accepted. Since software that implements IEEE 754-2008 binary64 (double precision) numbers is generally available and widely used, good interoperability can be achieved by implementations that expect no more precision or range than these provide, in the sense that implementations will approximate JSON numbers within the expected precision. diff --git a/doc/mkdocs/docs/features/types/number_handling.md b/doc/mkdocs/docs/features/types/number_handling.md new file mode 100644 index 0000000000..4224c155b2 --- /dev/null +++ b/doc/mkdocs/docs/features/types/number_handling.md @@ -0,0 +1,313 @@ +# Number Handling + +This document describes how the library is handling numbers. + +## Background + +This section briefly summarizes how the JSON specification describes how numbers should be handled. + +### JSON number syntax + +JSON defines the syntax of numbers as follows: + +!!! quote "[RFC 8259](https://tools.ietf.org/html/rfc8259#section-6), Section 6" + + The representation of numbers is similar to that used in most + programming languages. A number is represented in base 10 using + decimal digits. It contains an integer component that may be + prefixed with an optional minus sign, which may be followed by a + fraction part and/or an exponent part. Leading zeros are not + allowed. + + A fraction part is a decimal point followed by one or more digits. + + An exponent part begins with the letter E in uppercase or lowercase, + which may be followed by a plus or minus sign. The E and optional + sign are followed by one or more digits. + +The following railroad diagram from [json.org](https://json.org) visualizes the number syntax: + +![Syntax for JSON numbers](../../images/json_syntax_number.png) + +### Number interoperability + +On number interoperability, the following remarks are made: + +!!! quote "[RFC 8259](https://tools.ietf.org/html/rfc8259#section-6), Section 6" + + This specification allows implementations to set limits on the range + and precision of numbers accepted. Since software that implements + IEEE 754 binary64 (double precision) numbers [IEEE754] is generally + available and widely used, good interoperability can be achieved by + implementations that expect no more precision or range than these + provide, in the sense that implementations will approximate JSON + numbers within the expected precision. A JSON number such as 1E400 + or 3.141592653589793238462643383279 may indicate potential + interoperability problems, since it suggests that the software that + created it expects receiving software to have greater capabilities + for numeric magnitude and precision than is widely available. + + Note that when such software is used, numbers that are integers and + are in the range $[-2^{53}+1, 2^{53}-1]$ are interoperable in the + sense that implementations will agree exactly on their numeric + values. + +## Library implementation + +This section describes how the above number specification is implemented by this library. + +### Number storage + +In the default [`json`](../../api/json.md) type, numbers are stored as `#!c std::uint64_t`, `#!c std::int64_t`, and +`#!c double`, respectively. Thereby, `#!c std::uint64_t` and `#!c std::int64_t` are used only if they can store the +number without loss of precision. If this is impossible (e.g., if the number is too large), the number is stored as +`#!c double`. + +!!! info "Notes" + + - Numbers with a decimal digit or scientific notation are always stored as `#!c double`. + - The number types can be changed, see [Template number types](#template-number-types). + - As of version 3.9.1, the conversion is realized by + [`std::strtoull`](https://en.cppreference.com/w/cpp/string/byte/strtoul), + [`std::strtoll`](https://en.cppreference.com/w/cpp/string/byte/strtol), and + [`std::strtod`](https://en.cppreference.com/w/cpp/string/byte/strtof), respectively. + +!!! example "Examples" + + - Integer `#!c -12345678912345789123456789` is smaller than `#!c INT64_MIN` and will be stored as floating-point + number `#!c -1.2345678912345788e+25`. + - Integer `#!c 1E3` will be stored as floating-point number `#!c 1000.0`. + +### Number limits + +- Any 64 bit signed or unsigned integer can be stored without loss of precision. +- Numbers exceeding the limits of `#!c double` (i.e., numbers that after conversion via +[`std::strtod`](https://en.cppreference.com/w/cpp/string/byte/strtof) are not satisfying +[`std::isfinite`](https://en.cppreference.com/w/cpp/numeric/math/isfinite) such as `#!c 1E400`) will throw exception +[`json.exception.out_of_range.406`](../../home/exceptions.md#jsonexceptionout_of_range406) during parsing. +- Floating-point numbers are rounded to the next number representable as `double`. For instance +`#!c 3.141592653589793238462643383279` is stored as [`0x400921fb54442d18`](https://float.exposed/0x400921fb54442d18). +This is the same behavior as the code `#!c double x = 3.141592653589793238462643383279;`. + +!!! success "Interoperability" + + - The library interoperable with respect to the specification, because its supported range $[-2^{63}, 2^{64}-1]$ is + larger than the described range $[-2^{53}+1, 2^{53}-1]$. + - All integers outside the range $[-2^{63}, 2^{64}-1]$, as well as floating-point numbers are stored as `double`. + This also concurs with the specification above. + +### Number serialization + +- Integer numbers are serialized as is; that is, no scientific notation is used. +- Floating-point numbers are serialized as specified by the `#!c %g` printf modifier with + [`std::numeric_limits::max_digits10`](https://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10) + significant digits). The rationale is to use the shortest representation while still allow round-tripping. + +!!! hint "Notes regarding precision of floating-point numbers" + + As described above, floating-point numbers are rounded to the nearest double and serialized with the shortest + representation to allow round-tripping. This can yield confusing examples: + + - The serialization can have fewer decimal places than the input: `#!c 2555.5599999999999` will be serialized as + `#!c 2555.56`. The reverse can also be true. + - The serialization can be in scientific notation even if the input is not: `#!c 0.0000972439793401814` will be + serialized as `#!c 9.72439793401814e-05`. The reverse can also be true: `#!c 12345E-5` will be serialized as + `#!c 0.12345`. + - Conversions from `#!c float` to `#!c double` can also introduce rouding errors: + ```cpp + float f = 0.3; + json j = f; + std::cout << j << '\n'; + ``` + yields `#!c 0.30000001192092896`. + + All examples here can be reproduced by passing the original double value to + + ```cpp + std::printf("%.*g\n", std::numeric_limits::max_digits10, double_value); + ``` + +#### NaN handling + +NaN (not-a-number) cannot be expressed with the number syntax described above and are in fact explicitly excluded: + +!!! quote "[RFC 8259](https://tools.ietf.org/html/rfc8259#section-6), Section 6" + + Numeric values that cannot be represented in the grammar below (such + as Infinity and NaN) are not permitted. + +That is, there is no way to *parse* a NaN value. However, NaN values can be stored in a JSON value by assignment. + +This library serializes NaN values as `#!js null`. This corresponds to the behavior of JavaScript's +[`JSON.stringify`](https://www.w3schools.com/js/js_json_stringify.asp) function. + +!!! example + + The following example shows how a NaN value is stored in a `json` value. + + ```cpp + int main() + { + double val = std::numeric_limits::quiet_NaN(); + std::cout << "val=" << val << std::endl; + json j = val; + std::cout << "j=" << j.dump() << std::endl; + val = j; + std::cout << "val=" << val << std::endl; + } + ``` + + output: + + ``` + val=nan + j=null + val=nan + ``` + +### Number comparison + +Floating-point inside JSON values numbers are compared with `#!c json::number_float_t::operator==` which is +`#!c double::operator==` by default. + +!!! example "Alternative comparison functions" + + To compare floating-point while respecting an epsilon, an alternative + [comparison function](https://github.com/mariokonrad/marnav/blob/master/include/marnav/math/floatingpoint.hpp#L34-#L39) + could be used, for instance + + ```cpp + template::value, T>::type> + inline bool is_same(T a, T b, T epsilon = std::numeric_limits::epsilon()) noexcept + { + return std::abs(a - b) <= epsilon; + } + ``` + Or you can self-define an operator equal function like this: + + ```cpp + bool my_equal(const_reference lhs, const_reference rhs) + { + const auto lhs_type lhs.type(); + const auto rhs_type rhs.type(); + if (lhs_type == rhs_type) + { + switch(lhs_type) + { + // self_defined case + case value_t::number_float: + return std::abs(lhs - rhs) <= std::numeric_limits::epsilon(); + + // other cases remain the same with the original + ... + } + } + ... + } + ``` + + (see [#703](https://github.com/nlohmann/json/issues/703) for more information.) + +!!! note + + NaN values never compare equal to themselves or to other NaN values. See [#514](https://github.com/nlohmann/json/issues/514). + +### Number conversion + +Just like the C++ language itself, the `get` family of functions allows conversions between unsigned and signed +integers, and between integers and floating-point values to integers. This behavior may be surprising. + +!!! warning "Unconditional number conversions" + + ```cpp hl_lines="3" + double d = 42.3; // non-integer double value 42.3 + json jd = d; // stores double value 42.3 + std::int64_t i = jd.get(); // now i==42; no warning or error is produced + ``` + + Note the last line with throw a [`json.exception.type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) + exception if `jd` is not a numerical type, for instance a string. + +The rationale is twofold: + +1. JSON does not define a number type or precision (see [#json-specification](above)). +2. C++ also allows to silently convert between number types. + +!!! success "Conditional number conversion" + + The code above can be solved by explicitly checking the nature of the value with members such as + [`is_number_integer()`](../../api/basic_json/is_number_integer.md) or + [`is_number_unsigned()`](../../api/basic_json/is_number_unsigned.md): + + ```cpp hl_lines="2" + // check if jd is really integer-valued + if (jd.is_number_integer()) + { + // if so, do the conversion and use i + std::int64_t i = jd.get(); + // ... + } + else + { + // otherwise, take appropriate action + // ... + } + ``` + + Note this approach also has the advantage that it can react on non-numerical JSON value types such as strings. + + (Example taken from [#777](https://github.com/nlohmann/json/issues/777#issuecomment-459968458).) + +### Determine number types + +As the example in [Number conversion](#number_conversion) shows, there are different functions to determine the type of +the stored number: + +- [`is_number()`](../../api/basic_json/is_number.md) returns `#!c true` for any number type +- [`is_number_integer()`](../../api/basic_json/is_number_integer.md) returns `#!c true` for signed and unsigned integers +- [`is_number_unsigned()`](../../api/basic_json/is_number_unsigned.md) returns `#!c true` for unsigned integers only +- [`is_number_float()`](../../api/basic_json/is_number_float.md) returns `#!c true` for floating-point numbers +- [`type_name()`](../../api/basic_json/type_name.md) returns `#!c "number"` for any number type +- [`type()`](../../api/basic_json/type.md) returns an different enumerator of + [`value_t`](../../api/basic_json/value_t.md) for all number types + +| function | unsigned integer | signed integer | floating-point | string | +| -------- | ---------------- | -------------- | -------------- | ------ | +| [`is_number()`](../../api/basic_json/is_number.md) | `#!c true` | `#!c true` | `#!c true` | `#!c false` | +| [`is_number_integer()`](../../api/basic_json/is_number_integer.md) | `#!c true` | `#!c true` | `#!c false` | `#!c false` | +| [`is_number_unsigned()`](../../api/basic_json/is_number_unsigned.md) | `#!c true` | `#!c false` | `#!c false` | `#!c false` | +| [`is_number_float()`](../../api/basic_json/is_number_float.md) | `#!c false` | `#!c false` | `#!c true` | `#!c false` | +| [`type_name()`](../../api/basic_json/type_name.md) | `#!c "number"` | `#!c "number"` | `#!c "number"` | `#!c "string"` | +| [`type()`](../../api/basic_json/type.md) | `number_unsigned` | `number_integer` | `number_float` | `string` | + +### Template number types + +The number types can be changed with template parameters. + +| position | number type | default type | possible values | +| -------- | ----------- | ------------ | --------------- | +| 5 | signed integers | `#!c std::int64_t` | `#!c std::int32_t`, `#!c std::int16_t`, etc. | +| 6 | unsigned integers | `#!c std::uint64_t` | `#!c std::uint32_t`, `#!c std::uint16_t`, etc. | +| 7 | floating-point | `#!c double` | `#!c float`, `#!c long double` | + +!!! info "Constraints on number types" + + - The type for signed integers must be convertible from `#!c long long`. The type for floating-point numbers is used + in case of overflow. + - The type for unsigned integers must be convertible from `#!c unsigned long long`. The type for floating-point + numbers is used in case of overflow. + - The types for signed and unsigned integers must be distinct, see + [#2573](https://github.com/nlohmann/json/issues/2573). + - Only `#!c double`, `#!c float`, and `#!c long double` are supported for floating-point numbers. + +!!! example + + A `basic_json` type that uses `#!c long double` as floating-point type. + + ```cpp hl_lines="2" + using json_ld = nlohmann::basic_json; + ``` + + Note values should then be parsed with `json_ld::parse` rather than `json::parse` as the latter would parse + floating-point values to `#!c double` before then converting them to `#!c long double`. diff --git a/doc/mkdocs/docs/home/releases.md b/doc/mkdocs/docs/home/releases.md index 0cb890b0ae..298c3772bf 100644 --- a/doc/mkdocs/docs/home/releases.md +++ b/doc/mkdocs/docs/home/releases.md @@ -1167,7 +1167,7 @@ As `noexcept` and `constexpr` specifier have been added to several functions, th - Parser error messages are still very vague and contain no information on the error location. - The implemented `diff` function is rather primitive and does not create minimal diffs. - The name of function `iteration_wrapper` may change in the future and the function will be deprecated in the next release. -- Roundtripping (i.e., parsing a JSON value from a string, serializing it, and comparing the strings) of floating-point numbers is not 100% accurate. Note that [RFC 7159](https://tools.ietf.org/html/rfc7159) defines no format to internally represent numbers and states not requirement for roundtripping. Nevertheless, benchmarks like [Native JSON Benchmark](https://github.com/miloyip/nativejson-benchmark) treat roundtripping deviations as conformance errors. +- Roundtripping (i.e., parsing a JSON value from a string, serializing it, and comparing the strings) of floating-point numbers is not 100% accurate. Note that [RFC 8259](https://tools.ietf.org/html/rfc8259) defines no format to internally represent numbers and states not requirement for roundtripping. Nevertheless, benchmarks like [Native JSON Benchmark](https://github.com/miloyip/nativejson-benchmark) treat roundtripping deviations as conformance errors. ## v1.1.0 diff --git a/doc/mkdocs/mkdocs.yml b/doc/mkdocs/mkdocs.yml index e1552d39b8..b7ad380fe3 100644 --- a/doc/mkdocs/mkdocs.yml +++ b/doc/mkdocs/mkdocs.yml @@ -9,7 +9,7 @@ repo_url: https://github.com/nlohmann/json edit_uri: edit/develop/doc/mkdocs/docs # Copyright -copyright: Copyright © 2013 - 2020 Niels Lohmann +copyright: Copyright © 2013 - 2021 Niels Lohmann # Configuration theme: @@ -62,7 +62,9 @@ nav: - features/parsing/sax_interface.md - features/enum_conversion.md - features/macros.md - - features/types.md + - Types: + - features/types/index.md + - features/types/number_handling.md - Integration: - integration/index.md - integration/cmake.md diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index 7db2722619..3a47167e9c 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -231,7 +231,7 @@ class lexer : public lexer_base /*! @brief scan a string literal - This function scans a string according to Sect. 7 of RFC 7159. While + This function scans a string according to Sect. 7 of RFC 8259. While scanning, bytes are escaped and copied into buffer token_buffer. Then the function returns successfully, token_buffer is *not* null-terminated (as it may contain \0 bytes), and token_buffer.size() is the number of bytes in the @@ -921,10 +921,10 @@ class lexer : public lexer_base /*! @brief scan a number literal - This function scans a string according to Sect. 6 of RFC 7159. + This function scans a string according to Sect. 6 of RFC 8259. The function is realized with a deterministic finite state machine derived - from the grammar described in RFC 7159. Starting in state "init", the + from the grammar described in RFC 8259. Starting in state "init", the input is read and used to determined the next state. Only state "done" accepts the number. State "error" is a trap state to model errors. In the table below, "anything" means any character but the ones listed before. diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 337b64c9d8..0bbaa9503a 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -163,8 +163,8 @@ The invariants are checked by member function assert_invariant(). @note ObjectType trick from https://stackoverflow.com/a/9860911 @endinternal -@see [RFC 7159: The JavaScript Object Notation (JSON) Data Interchange -Format](http://rfc7159.net/rfc7159) +@see [RFC 8259: The JavaScript Object Notation (JSON) Data Interchange +Format](https://tools.ietf.org/html/rfc8259) @since version 1.0.0 @@ -425,7 +425,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /*! @brief a type for an object - [RFC 7159](http://rfc7159.net/rfc7159) describes JSON objects as follows: + [RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON objects as follows: > An object is an unordered collection of zero or more name/value pairs, > where a name is a string and a value is a string, number, boolean, null, > object, or array. @@ -479,7 +479,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #### Limits - [RFC 7159](http://rfc7159.net/rfc7159) specifies: + [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the maximum depth of nesting. In this class, the object's limit of nesting is not explicitly constrained. @@ -502,7 +502,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec name/value pairs in a different order than they were originally stored. In fact, keys will be traversed in alphabetical order as `std::map` with `std::less` is used by default. Please note this behavior conforms to [RFC - 7159](http://rfc7159.net/rfc7159), because any order implements the + 8259](https://tools.ietf.org/html/rfc8259), because any order implements the specified "unordered" nature of JSON objects. */ using object_t = ObjectType An array is an ordered sequence of zero or more values. To store objects in C++, a type is defined by the template parameters @@ -538,7 +538,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #### Limits - [RFC 7159](http://rfc7159.net/rfc7159) specifies: + [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the maximum depth of nesting. In this class, the array's limit of nesting is not explicitly constrained. @@ -560,7 +560,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /*! @brief a type for a string - [RFC 7159](http://rfc7159.net/rfc7159) describes JSON strings as follows: + [RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON strings as follows: > A string is a sequence of zero or more Unicode characters. To store objects in C++, a type is defined by the template parameter @@ -587,7 +587,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #### String comparison - [RFC 7159](http://rfc7159.net/rfc7159) states: + [RFC 8259](https://tools.ietf.org/html/rfc8259) states: > Software implementations are typically required to test names of object > members for equality. Implementations that transform the textual > representation into sequences of Unicode code units and then perform the @@ -613,7 +613,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /*! @brief a type for a boolean - [RFC 7159](http://rfc7159.net/rfc7159) implicitly describes a boolean as a + [RFC 8259](https://tools.ietf.org/html/rfc8259) implicitly describes a boolean as a type which differentiates the two literals `true` and `false`. To store objects in C++, a type is defined by the template parameter @a @@ -639,7 +639,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /*! @brief a type for a number (integer) - [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows: + [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: > The representation of numbers is similar to that used in most > programming languages. A number is represented in base 10 using decimal > digits. It contains an integer component that may be prefixed with an @@ -677,7 +677,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #### Limits - [RFC 7159](http://rfc7159.net/rfc7159) specifies: + [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the range and precision of numbers. When the default type is used, the maximal integer number that can be @@ -688,7 +688,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec will be automatically be stored as @ref number_unsigned_t or @ref number_float_t. - [RFC 7159](http://rfc7159.net/rfc7159) further states: + [RFC 8259](https://tools.ietf.org/html/rfc8259) further states: > Note that when such software is used, numbers that are integers and are > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense > that implementations will agree exactly on their numeric values. @@ -711,7 +711,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /*! @brief a type for a number (unsigned) - [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows: + [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: > The representation of numbers is similar to that used in most > programming languages. A number is represented in base 10 using decimal > digits. It contains an integer component that may be prefixed with an @@ -749,7 +749,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #### Limits - [RFC 7159](http://rfc7159.net/rfc7159) specifies: + [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the range and precision of numbers. When the default type is used, the maximal integer number that can be @@ -759,7 +759,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec deserialization, too large or small integer numbers will be automatically be stored as @ref number_integer_t or @ref number_float_t. - [RFC 7159](http://rfc7159.net/rfc7159) further states: + [RFC 8259](https://tools.ietf.org/html/rfc8259) further states: > Note that when such software is used, numbers that are integers and are > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense > that implementations will agree exactly on their numeric values. @@ -782,7 +782,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /*! @brief a type for a number (floating-point) - [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows: + [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: > The representation of numbers is similar to that used in most > programming languages. A number is represented in base 10 using decimal > digits. It contains an integer component that may be prefixed with an @@ -820,7 +820,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #### Limits - [RFC 7159](http://rfc7159.net/rfc7159) states: + [RFC 8259](https://tools.ietf.org/html/rfc8259) states: > This specification allows implementations to set limits on the range and > precision of numbers accepted. Since software that implements IEEE > 754-2008 binary64 (double precision) numbers is generally available and diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 97f77b4faa..4ba979fd2e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -6649,7 +6649,7 @@ class lexer : public lexer_base /*! @brief scan a string literal - This function scans a string according to Sect. 7 of RFC 7159. While + This function scans a string according to Sect. 7 of RFC 8259. While scanning, bytes are escaped and copied into buffer token_buffer. Then the function returns successfully, token_buffer is *not* null-terminated (as it may contain \0 bytes), and token_buffer.size() is the number of bytes in the @@ -7339,10 +7339,10 @@ class lexer : public lexer_base /*! @brief scan a number literal - This function scans a string according to Sect. 6 of RFC 7159. + This function scans a string according to Sect. 6 of RFC 8259. The function is realized with a deterministic finite state machine derived - from the grammar described in RFC 7159. Starting in state "init", the + from the grammar described in RFC 8259. Starting in state "init", the input is read and used to determined the next state. Only state "done" accepts the number. State "error" is a trap state to model errors. In the table below, "anything" means any character but the ones listed before. @@ -17178,8 +17178,8 @@ The invariants are checked by member function assert_invariant(). @note ObjectType trick from https://stackoverflow.com/a/9860911 @endinternal -@see [RFC 7159: The JavaScript Object Notation (JSON) Data Interchange -Format](http://rfc7159.net/rfc7159) +@see [RFC 8259: The JavaScript Object Notation (JSON) Data Interchange +Format](https://tools.ietf.org/html/rfc8259) @since version 1.0.0 @@ -17440,7 +17440,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /*! @brief a type for an object - [RFC 7159](http://rfc7159.net/rfc7159) describes JSON objects as follows: + [RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON objects as follows: > An object is an unordered collection of zero or more name/value pairs, > where a name is a string and a value is a string, number, boolean, null, > object, or array. @@ -17494,7 +17494,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #### Limits - [RFC 7159](http://rfc7159.net/rfc7159) specifies: + [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the maximum depth of nesting. In this class, the object's limit of nesting is not explicitly constrained. @@ -17517,7 +17517,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec name/value pairs in a different order than they were originally stored. In fact, keys will be traversed in alphabetical order as `std::map` with `std::less` is used by default. Please note this behavior conforms to [RFC - 7159](http://rfc7159.net/rfc7159), because any order implements the + 8259](https://tools.ietf.org/html/rfc8259), because any order implements the specified "unordered" nature of JSON objects. */ using object_t = ObjectType An array is an ordered sequence of zero or more values. To store objects in C++, a type is defined by the template parameters @@ -17553,7 +17553,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #### Limits - [RFC 7159](http://rfc7159.net/rfc7159) specifies: + [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the maximum depth of nesting. In this class, the array's limit of nesting is not explicitly constrained. @@ -17575,7 +17575,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /*! @brief a type for a string - [RFC 7159](http://rfc7159.net/rfc7159) describes JSON strings as follows: + [RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON strings as follows: > A string is a sequence of zero or more Unicode characters. To store objects in C++, a type is defined by the template parameter @@ -17602,7 +17602,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #### String comparison - [RFC 7159](http://rfc7159.net/rfc7159) states: + [RFC 8259](https://tools.ietf.org/html/rfc8259) states: > Software implementations are typically required to test names of object > members for equality. Implementations that transform the textual > representation into sequences of Unicode code units and then perform the @@ -17628,7 +17628,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /*! @brief a type for a boolean - [RFC 7159](http://rfc7159.net/rfc7159) implicitly describes a boolean as a + [RFC 8259](https://tools.ietf.org/html/rfc8259) implicitly describes a boolean as a type which differentiates the two literals `true` and `false`. To store objects in C++, a type is defined by the template parameter @a @@ -17654,7 +17654,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /*! @brief a type for a number (integer) - [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows: + [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: > The representation of numbers is similar to that used in most > programming languages. A number is represented in base 10 using decimal > digits. It contains an integer component that may be prefixed with an @@ -17692,7 +17692,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #### Limits - [RFC 7159](http://rfc7159.net/rfc7159) specifies: + [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the range and precision of numbers. When the default type is used, the maximal integer number that can be @@ -17703,7 +17703,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec will be automatically be stored as @ref number_unsigned_t or @ref number_float_t. - [RFC 7159](http://rfc7159.net/rfc7159) further states: + [RFC 8259](https://tools.ietf.org/html/rfc8259) further states: > Note that when such software is used, numbers that are integers and are > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense > that implementations will agree exactly on their numeric values. @@ -17726,7 +17726,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /*! @brief a type for a number (unsigned) - [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows: + [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: > The representation of numbers is similar to that used in most > programming languages. A number is represented in base 10 using decimal > digits. It contains an integer component that may be prefixed with an @@ -17764,7 +17764,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #### Limits - [RFC 7159](http://rfc7159.net/rfc7159) specifies: + [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: > An implementation may set limits on the range and precision of numbers. When the default type is used, the maximal integer number that can be @@ -17774,7 +17774,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec deserialization, too large or small integer numbers will be automatically be stored as @ref number_integer_t or @ref number_float_t. - [RFC 7159](http://rfc7159.net/rfc7159) further states: + [RFC 8259](https://tools.ietf.org/html/rfc8259) further states: > Note that when such software is used, numbers that are integers and are > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense > that implementations will agree exactly on their numeric values. @@ -17797,7 +17797,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /*! @brief a type for a number (floating-point) - [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows: + [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: > The representation of numbers is similar to that used in most > programming languages. A number is represented in base 10 using decimal > digits. It contains an integer component that may be prefixed with an @@ -17835,7 +17835,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #### Limits - [RFC 7159](http://rfc7159.net/rfc7159) states: + [RFC 8259](https://tools.ietf.org/html/rfc8259) states: > This specification allows implementations to set limits on the range and > precision of numbers accepted. Since software that implements IEEE > 754-2008 binary64 (double precision) numbers is generally available and diff --git a/test/src/unit-class_parser.cpp b/test/src/unit-class_parser.cpp index 247d9d5fe5..b0cdb0b357 100644 --- a/test/src/unit-class_parser.cpp +++ b/test/src/unit-class_parser.cpp @@ -589,7 +589,7 @@ TEST_CASE("parser class") SECTION("edge cases") { - // From RFC7159, Section 6: + // From RFC8259, Section 6: // Note that when such software is used, numbers that are // integers and are in the range [-(2**53)+1, (2**53)-1] // are interoperable in the sense that implementations will @@ -603,7 +603,7 @@ TEST_CASE("parser class") SECTION("over the edge cases") // issue #178 - Integer conversion to unsigned (incorrect handling of 64 bit integers) { - // While RFC7159, Section 6 specifies a preference for support + // While RFC8259, Section 6 specifies a preference for support // for ranges in range of IEEE 754-2008 binary64 (double precision) // this does not accommodate 64 bit integers without loss of accuracy. // As 64 bit integers are now widely used in software, it is desirable @@ -888,7 +888,7 @@ TEST_CASE("parser class") SECTION("edge cases") { - // From RFC7159, Section 6: + // From RFC8259, Section 6: // Note that when such software is used, numbers that are // integers and are in the range [-(2**53)+1, (2**53)-1] // are interoperable in the sense that implementations will @@ -902,7 +902,7 @@ TEST_CASE("parser class") SECTION("over the edge cases") // issue #178 - Integer conversion to unsigned (incorrect handling of 64 bit integers) { - // While RFC7159, Section 6 specifies a preference for support + // While RFC8259, Section 6 specifies a preference for support // for ranges in range of IEEE 754-2008 binary64 (double precision) // this does not accommodate 64 bit integers without loss of accuracy. // As 64 bit integers are now widely used in software, it is desirable diff --git a/test/src/unit-testsuites.cpp b/test/src/unit-testsuites.cpp index 96239987d7..8071d4c74a 100644 --- a/test/src/unit-testsuites.cpp +++ b/test/src/unit-testsuites.cpp @@ -418,9 +418,9 @@ TEST_CASE("json.org examples") } } -TEST_CASE("RFC 7159 examples") +TEST_CASE("RFC 8259 examples") { - // here, we list all JSON values from the RFC 7159 document + // here, we list all JSON values from the RFC 8259 document SECTION("7. Strings") { From d0ab2b86c34368f8f470c807041cefa6e2ff7a03 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 29 Apr 2021 17:19:57 +0200 Subject: [PATCH 098/143] :memo: update documentation --- doc/mkdocs/docs/features/macros.md | 4 ++++ include/nlohmann/detail/macro_unscope.hpp | 2 ++ single_include/nlohmann/json.hpp | 2 ++ 3 files changed, 8 insertions(+) diff --git a/doc/mkdocs/docs/features/macros.md b/doc/mkdocs/docs/features/macros.md index b468c091af..ee401c5dcd 100644 --- a/doc/mkdocs/docs/features/macros.md +++ b/doc/mkdocs/docs/features/macros.md @@ -20,6 +20,10 @@ When enabled, exception messages contain a [JSON Pointer](json_pointer.md) to th The diagnostics messages can also be controlled with the CMake option `JSON_Diagnostics` (`OFF` by default) which sets `JSON_DIAGNOSTICS` accordingly. +## `JSON_HAS_CPP_11`, `JSON_HAS_CPP_14`, `JSON_HAS_CPP_17`, `JSON_HAS_CPP_20` + +The library targets C++11, but also supports some features introduced in later C++ versions (e.g., `std::string_view` support for C++17). For these new features, the library implements some preprocessor checks to determine the C++ standard. By defining any of these symbols, the internal check is overridden and the provided C++ version is unconditionally assumed. This can be helpful for compilers that only implement parts of the standard and would be detected incorrectly. + ## `JSON_NOEXCEPTION` Exceptions can be switched off by defining the symbol `JSON_NOEXCEPTION`. diff --git a/include/nlohmann/detail/macro_unscope.hpp b/include/nlohmann/detail/macro_unscope.hpp index 21c091daf9..28be047aa2 100644 --- a/include/nlohmann/detail/macro_unscope.hpp +++ b/include/nlohmann/detail/macro_unscope.hpp @@ -12,8 +12,10 @@ #undef JSON_THROW #undef JSON_TRY #undef JSON_PRIVATE_UNLESS_TESTED +#undef JSON_HAS_CPP_11 #undef JSON_HAS_CPP_14 #undef JSON_HAS_CPP_17 +#undef JSON_HAS_CPP_20 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION #undef NLOHMANN_BASIC_JSON_TPL #undef JSON_EXPLICIT diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 7f16e7743d..b3c568b1a1 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -25972,8 +25972,10 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std #undef JSON_THROW #undef JSON_TRY #undef JSON_PRIVATE_UNLESS_TESTED +#undef JSON_HAS_CPP_11 #undef JSON_HAS_CPP_14 #undef JSON_HAS_CPP_17 +#undef JSON_HAS_CPP_20 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION #undef NLOHMANN_BASIC_JSON_TPL #undef JSON_EXPLICIT From 193bf9d6b698d93e10bd37f6f82757fe3a4348c2 Mon Sep 17 00:00:00 2001 From: Morten Fyhn Amundsen Date: Mon, 3 May 2021 09:46:00 +0200 Subject: [PATCH 099/143] Fix typo in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bb931803cf..3309190e01 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,7 @@ target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json) Since CMake v3.11, [FetchContent](https://cmake.org/cmake/help/v3.11/module/FetchContent.html) can -be used to automatically download the repository as a dependency at configure type. +be used to automatically download the repository as a dependency at configure time. Example: ```cmake From b08139ea4b5940c396311c4f4f428dbebc061e02 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 4 May 2021 10:22:34 +0200 Subject: [PATCH 100/143] :recycle: replace EOF with std::char_traits::eof() --- include/nlohmann/detail/input/input_adapters.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 5d0b59b57a..dced9bfffa 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -105,7 +105,7 @@ class input_stream_adapter { auto res = sb->sbumpc(); // set eof manually, as we don't use the istream interface. - if (JSON_HEDLEY_UNLIKELY(res == EOF)) + if (JSON_HEDLEY_UNLIKELY(res == std::char_traits::eof())) { is->clear(is->rdstate() | std::ios::eofbit); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index b3c568b1a1..2716e1e7b3 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5332,7 +5332,7 @@ class input_stream_adapter { auto res = sb->sbumpc(); // set eof manually, as we don't use the istream interface. - if (JSON_HEDLEY_UNLIKELY(res == EOF)) + if (JSON_HEDLEY_UNLIKELY(res == std::char_traits::eof())) { is->clear(is->rdstate() | std::ios::eofbit); } From 1d8869f8bc5958af0a3a2f83177f843cbd3cd868 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 8 May 2021 13:33:30 +0200 Subject: [PATCH 101/143] :construction_worker: add Drone CI --- .drone.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .drone.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000000..22be5cc4dd --- /dev/null +++ b/.drone.yml @@ -0,0 +1,14 @@ +kind: pipeline +name: test-on-arm64 + +platform: + arch: arm64 + +steps: +- name: build + image: gcc + commands: + - mkdir build + - cd build + - cmake .. + - cmake --build . From 77a5cd247edf673157ae70a983623e4b63ba2ec4 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 8 May 2021 13:56:19 +0200 Subject: [PATCH 102/143] :wrench: build cmake --- .drone.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.drone.yml b/.drone.yml index 22be5cc4dd..847724dbc9 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,6 +8,11 @@ steps: - name: build image: gcc commands: + - wget https://github.com/Kitware/CMake/releases/download/v3.20.2/cmake-3.20.2.tar.gz + - tar xfz cmake-3.20.2.tar.gz + - cd cmake-3.20.2 + - ./configure + - make - mkdir build - cd build - cmake .. From a6e2fd1b15d763fb5dc6837f72fce269a9fc4a9c Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 8 May 2021 14:41:43 +0200 Subject: [PATCH 103/143] :wrench: use built CMake --- .drone.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.drone.yml b/.drone.yml index 847724dbc9..83678f5d49 100644 --- a/.drone.yml +++ b/.drone.yml @@ -12,8 +12,9 @@ steps: - tar xfz cmake-3.20.2.tar.gz - cd cmake-3.20.2 - ./configure - - make + - make cmake -j10 + - cd .. - mkdir build - cd build - - cmake .. + - ../cmake-3.20.2/bin/cmake .. - cmake --build . From 27a71ccdd8d1729b15e08023bfe13755875eb499 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 8 May 2021 20:42:24 +0200 Subject: [PATCH 104/143] :wrench: fix path --- .drone.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 83678f5d49..0ea69aa3f2 100644 --- a/.drone.yml +++ b/.drone.yml @@ -17,4 +17,6 @@ steps: - mkdir build - cd build - ../cmake-3.20.2/bin/cmake .. - - cmake --build . + - make -j10 + - cd test + - ctest -j10 From 9ec8f4efa86484aa43cd2c2e46072dfa1a8dbcbb Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 9 May 2021 10:39:37 +0200 Subject: [PATCH 105/143] :wrench: fix ctest path --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 0ea69aa3f2..36a7960451 100644 --- a/.drone.yml +++ b/.drone.yml @@ -19,4 +19,4 @@ steps: - ../cmake-3.20.2/bin/cmake .. - make -j10 - cd test - - ctest -j10 + - ../../cmake-3.20.2/bin/ctest -j10 From f3193e1e94a7ab5c1382562e5ac17b8b63017381 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 9 May 2021 11:07:57 +0200 Subject: [PATCH 106/143] :wrench: build ctest --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 36a7960451..4416c85220 100644 --- a/.drone.yml +++ b/.drone.yml @@ -12,7 +12,7 @@ steps: - tar xfz cmake-3.20.2.tar.gz - cd cmake-3.20.2 - ./configure - - make cmake -j10 + - make cmake ctest -j10 - cd .. - mkdir build - cd build From 42f251229007014c33f43dbe99fbaf302e157a1c Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 9 May 2021 13:00:35 +0200 Subject: [PATCH 107/143] :wrench: skip slow tests --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 4416c85220..3cdefe4b9e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -16,7 +16,7 @@ steps: - cd .. - mkdir build - cd build - - ../cmake-3.20.2/bin/cmake .. + - ../cmake-3.20.2/bin/cmake .. -DJSON_FastTests=ON - make -j10 - cd test - ../../cmake-3.20.2/bin/ctest -j10 From e6e7309514daa2aed5a8726bad4d7fb7d8df39e7 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 9 May 2021 13:31:27 +0200 Subject: [PATCH 108/143] :construction_worker: arm and arm64 --- .drone.yml | 52 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/.drone.yml b/.drone.yml index 3cdefe4b9e..4f87bc52ce 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,22 +1,32 @@ -kind: pipeline -name: test-on-arm64 +local Pipeline(version, arch) = { + kind: "pipeline", + name: "test-on-"+arch, + platform: { + arch: arch + } + steps: [ + { + name: "build", + image: "gcc", + commands: [ + "wget https://github.com/Kitware/CMake/releases/download/v3.20.2/cmake-3.20.2.tar.gz", + "tar xfz cmake-3.20.2.tar.gz", + "cd cmake-3.20.2", + "./configure", + "make cmake ctest -j10", + "cd ..", + "mkdir build", + "cd build", + "../cmake-3.20.2/bin/cmake .. -DJSON_FastTests=ON", + "make -j10", + "cd test", + "../../cmake-3.20.2/bin/ctest -j10" + ] + } + ] +}; -platform: - arch: arm64 - -steps: -- name: build - image: gcc - commands: - - wget https://github.com/Kitware/CMake/releases/download/v3.20.2/cmake-3.20.2.tar.gz - - tar xfz cmake-3.20.2.tar.gz - - cd cmake-3.20.2 - - ./configure - - make cmake ctest -j10 - - cd .. - - mkdir build - - cd build - - ../cmake-3.20.2/bin/cmake .. -DJSON_FastTests=ON - - make -j10 - - cd test - - ../../cmake-3.20.2/bin/ctest -j10 +[ + Pipeline("arm"), + Pipeline("arm64") +] From df6fa6c11c1473a5133653b89726b7df75893402 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 9 May 2021 13:32:50 +0200 Subject: [PATCH 109/143] :construction_worker: arm and arm64 --- .drone.yml | 76 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/.drone.yml b/.drone.yml index 4f87bc52ce..f773009a2a 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,32 +1,46 @@ -local Pipeline(version, arch) = { - kind: "pipeline", - name: "test-on-"+arch, - platform: { - arch: arch - } - steps: [ - { - name: "build", - image: "gcc", - commands: [ - "wget https://github.com/Kitware/CMake/releases/download/v3.20.2/cmake-3.20.2.tar.gz", - "tar xfz cmake-3.20.2.tar.gz", - "cd cmake-3.20.2", - "./configure", - "make cmake ctest -j10", - "cd ..", - "mkdir build", - "cd build", - "../cmake-3.20.2/bin/cmake .. -DJSON_FastTests=ON", - "make -j10", - "cd test", - "../../cmake-3.20.2/bin/ctest -j10" - ] - } - ] -}; +kind: pipeline +name: test-on-arm -[ - Pipeline("arm"), - Pipeline("arm64") -] +platform: + arch: arm + +steps: +- name: build + image: gcc + commands: + - wget https://github.com/Kitware/CMake/releases/download/v3.20.2/cmake-3.20.2.tar.gz + - tar xfz cmake-3.20.2.tar.gz + - cd cmake-3.20.2 + - ./configure + - make cmake ctest -j10 + - cd .. + - mkdir build + - cd build + - ../cmake-3.20.2/bin/cmake .. -DJSON_FastTests=ON + - make -j10 + - cd test + - ../../cmake-3.20.2/bin/ctest -j10 + +--- +kind: pipeline +name: test-on-arm64 + +platform: + arch: arm64 + +steps: +- name: build + image: gcc + commands: + - wget https://github.com/Kitware/CMake/releases/download/v3.20.2/cmake-3.20.2.tar.gz + - tar xfz cmake-3.20.2.tar.gz + - cd cmake-3.20.2 + - ./configure + - make cmake ctest -j10 + - cd .. + - mkdir build + - cd build + - ../cmake-3.20.2/bin/cmake .. -DJSON_FastTests=ON + - make -j10 + - cd test + - ../../cmake-3.20.2/bin/ctest -j10 From 9746f72d7151494c1b50a1d3c3ca47eeb67d62f3 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 9 May 2021 13:34:28 +0200 Subject: [PATCH 110/143] :hammer: do not check certificate --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index f773009a2a..471ce10953 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,7 +8,7 @@ steps: - name: build image: gcc commands: - - wget https://github.com/Kitware/CMake/releases/download/v3.20.2/cmake-3.20.2.tar.gz + - wget --no-check-certificates https://github.com/Kitware/CMake/releases/download/v3.20.2/cmake-3.20.2.tar.gz - tar xfz cmake-3.20.2.tar.gz - cd cmake-3.20.2 - ./configure From 3685dbdcfdf9379869f82a07473ce89c7c35eab1 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 9 May 2021 13:41:40 +0200 Subject: [PATCH 111/143] :wrench: checkout cmake via git --- .drone.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.drone.yml b/.drone.yml index 471ce10953..9fdae5a0a4 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,18 +8,17 @@ steps: - name: build image: gcc commands: - - wget --no-check-certificates https://github.com/Kitware/CMake/releases/download/v3.20.2/cmake-3.20.2.tar.gz - - tar xfz cmake-3.20.2.tar.gz - - cd cmake-3.20.2 + - git clone https://github.com/Kitware/CMake.git + - cd CMake - ./configure - make cmake ctest -j10 - cd .. - mkdir build - cd build - - ../cmake-3.20.2/bin/cmake .. -DJSON_FastTests=ON + - ../CMake/bin/cmake .. -DJSON_FastTests=ON - make -j10 - cd test - - ../../cmake-3.20.2/bin/ctest -j10 + - ../../CMake/bin/ctest -j10 --- kind: pipeline From c73bd8b251b42c1d5941faa3e142d66c7567fa81 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 9 May 2021 13:44:21 +0200 Subject: [PATCH 112/143] :wrench: ignore certificate --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 9fdae5a0a4..ebb8615c82 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,7 +8,7 @@ steps: - name: build image: gcc commands: - - git clone https://github.com/Kitware/CMake.git + - git -c http.sslVerify=false clone https://github.com/Kitware/CMake.git - cd CMake - ./configure - make cmake ctest -j10 From 0c62748b75a9750f7ce0c39e052782d9c41981a7 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 9 May 2021 13:46:48 +0200 Subject: [PATCH 113/143] :wrench: change repository --- .drone.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.drone.yml b/.drone.yml index ebb8615c82..c2f32dd32d 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,17 +8,17 @@ steps: - name: build image: gcc commands: - - git -c http.sslVerify=false clone https://github.com/Kitware/CMake.git - - cd CMake + - git -c http.sslVerify=false clone https://gitlab.kitware.com/cmake/cmake.git + - cd cmake - ./configure - make cmake ctest -j10 - cd .. - mkdir build - cd build - - ../CMake/bin/cmake .. -DJSON_FastTests=ON + - ../cmake/bin/cmake .. -DJSON_FastTests=ON - make -j10 - cd test - - ../../CMake/bin/ctest -j10 + - ../../cmake/bin/ctest -j10 --- kind: pipeline From 014724f362b0869e99e01c01cc86cc02198646d3 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 9 May 2021 13:49:42 +0200 Subject: [PATCH 114/143] :alembic: change path --- .drone.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.drone.yml b/.drone.yml index c2f32dd32d..f5b0933f25 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,6 +8,8 @@ steps: - name: build image: gcc commands: + - mkdir cm + - cd cm - git -c http.sslVerify=false clone https://gitlab.kitware.com/cmake/cmake.git - cd cmake - ./configure From a44b29ecf104d2a496fb4da875fca14655e207be Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 9 May 2021 13:52:16 +0200 Subject: [PATCH 115/143] :wrench: fix paths --- .drone.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.drone.yml b/.drone.yml index f5b0933f25..43e515af42 100644 --- a/.drone.yml +++ b/.drone.yml @@ -17,10 +17,10 @@ steps: - cd .. - mkdir build - cd build - - ../cmake/bin/cmake .. -DJSON_FastTests=ON + - ../cm/cmake/bin/cmake .. -DJSON_FastTests=ON - make -j10 - cd test - - ../../cmake/bin/ctest -j10 + - ../../cm/cmake/bin/ctest -j10 --- kind: pipeline From e856b507918f2d013089122eaa8bd9c925a9b815 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 9 May 2021 14:22:46 +0200 Subject: [PATCH 116/143] :fire: remove arm build --- .drone.yml | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/.drone.yml b/.drone.yml index 43e515af42..3cdefe4b9e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,29 +1,4 @@ kind: pipeline -name: test-on-arm - -platform: - arch: arm - -steps: -- name: build - image: gcc - commands: - - mkdir cm - - cd cm - - git -c http.sslVerify=false clone https://gitlab.kitware.com/cmake/cmake.git - - cd cmake - - ./configure - - make cmake ctest -j10 - - cd .. - - mkdir build - - cd build - - ../cm/cmake/bin/cmake .. -DJSON_FastTests=ON - - make -j10 - - cd test - - ../../cm/cmake/bin/ctest -j10 - ---- -kind: pipeline name: test-on-arm64 platform: From 7cafc5c63f412bf5e2583b8ea71b34996f6283ea Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 10 May 2021 14:23:27 +0200 Subject: [PATCH 117/143] :memo: document Drone CI --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3309190e01..95d64752b9 100644 --- a/README.md +++ b/README.md @@ -1227,7 +1227,7 @@ Please note: - Unsupported versions of GCC and Clang are rejected by `#error` directives. This can be switched off by defining `JSON_SKIP_UNSUPPORTED_COMPILER_CHECK`. Note that you can expect no support in this case. -The following compilers are currently used in continuous integration at [Travis](https://travis-ci.org/nlohmann/json), [AppVeyor](https://ci.appveyor.com/project/nlohmann/json), and [GitHub Actions](https://github.com/nlohmann/json/actions): +The following compilers are currently used in continuous integration at [Travis](https://travis-ci.org/nlohmann/json), [AppVeyor](https://ci.appveyor.com/project/nlohmann/json), [Drone CI](https://cloud.drone.io/nlohmann/json), and [GitHub Actions](https://github.com/nlohmann/json/actions): | Compiler | Operating System | CI Provider | |-------------------------------------------------------------------|--------------------|----------------| @@ -1256,6 +1256,7 @@ The following compilers are currently used in continuous integration at [Travis] | GCC 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) | Ubuntu 20.04.2 LTS | GitHub Actions | | GCC 10.2.0 (Ubuntu 10.2.0-5ubuntu1~20.04) | Ubuntu 20.04.2 LTS | GitHub Actions | | GCC 11.0.1 20210321 (experimental) | Ubuntu 20.04.2 LTS | GitHub Actions | +| GCC 11.1.0 | Ubuntu (aarch64) | Drone CI | | Clang 3.5.2 (3.5.2-3ubuntu1) | Ubuntu 20.04.2 LTS | GitHub Actions | | Clang 3.6.2 (3.6.2-3ubuntu2) | Ubuntu 20.04.2 LTS | GitHub Actions | | Clang 3.7.1 (3.7.1-2ubuntu2) | Ubuntu 20.04.2 LTS | GitHub Actions | From 6d7959e05d2ef67eb841f190c41fbbb3d13073de Mon Sep 17 00:00:00 2001 From: Jason Dsouza <61184127+jasmcaus@users.noreply.github.com> Date: Wed, 12 May 2021 19:25:06 +0530 Subject: [PATCH 118/143] Keep consistent formatting --- CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1636575594..ee763cc340 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ project(nlohmann_json VERSION 3.9.1 LANGUAGES CXX) ## set(MAIN_PROJECT OFF) if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) - set(MAIN_PROJECT ON) + set(MAIN_PROJECT ON) endif() ## @@ -113,8 +113,8 @@ endif() # Install a pkg-config file, so other tools can find this. CONFIGURE_FILE( - "${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in" - "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" ) ## @@ -160,7 +160,7 @@ if(JSON_Install) FILES ${NLOHMANN_NATVIS_FILE} DESTINATION . ) -endif() + endif() # NLOHMANN_ADD_NATVIS export( TARGETS ${NLOHMANN_JSON_TARGET_NAME} NAMESPACE ${PROJECT_NAME}:: @@ -180,4 +180,4 @@ endif() FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" DESTINATION ${NLOHMANN_JSON_PKGCONFIG_INSTALL_DIR} ) -endif() +endif() # JSON_Install From 7935a683f5ac931938ba7532534d19ed2b18cb80 Mon Sep 17 00:00:00 2001 From: offa Date: Wed, 12 May 2021 16:46:32 +0200 Subject: [PATCH 119/143] link to conan package in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 95d64752b9..242e09841f 100644 --- a/README.md +++ b/README.md @@ -206,7 +206,7 @@ If you are using the [Meson Build System](https://mesonbuild.com), add this sour The provided meson.build can also be used as an alternative to cmake for installing `nlohmann_json` system-wide in which case a pkg-config file is installed. To use it, simply have your build system require the `nlohmann_json` pkg-config dependency. In Meson, it is preferred to use the [`dependency()`](https://mesonbuild.com/Reference-manual.html#dependency) object with a subproject fallback, rather than using the subproject directly. -If you are using [Conan](https://www.conan.io/) to manage your dependencies, merely add `nlohmann_json/x.y.z` to your `conanfile`'s requires, where `x.y.z` is the release version you want to use. Please file issues [here](https://github.com/conan-io/conan-center-index/issues) if you experience problems with the packages. +If you are using [Conan](https://www.conan.io/) to manage your dependencies, merely add [`nlohmann_json/x.y.z`](https://conan.io/center/nlohmann_json) to your `conanfile`'s requires, where `x.y.z` is the release version you want to use. Please file issues [here](https://github.com/conan-io/conan-center-index/issues) if you experience problems with the packages. If you are using [Spack](https://www.spack.io/) to manage your dependencies, you can use the [`nlohmann-json` package](https://spack.readthedocs.io/en/latest/package_list.html#nlohmann-json). Please see the [spack project](https://github.com/spack/spack) for any issues regarding the packaging. From f118091eaa73f436ec05ad6ec69b750683f12a3b Mon Sep 17 00:00:00 2001 From: Jason Dsouza <61184127+jasmcaus@users.noreply.github.com> Date: Sat, 15 May 2021 23:39:15 +0530 Subject: [PATCH 120/143] Update CMakeLists.txt --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ee763cc340..3c629ee6a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -160,7 +160,7 @@ if(JSON_Install) FILES ${NLOHMANN_NATVIS_FILE} DESTINATION . ) - endif() # NLOHMANN_ADD_NATVIS + endif() export( TARGETS ${NLOHMANN_JSON_TARGET_NAME} NAMESPACE ${PROJECT_NAME}:: @@ -180,4 +180,4 @@ if(JSON_Install) FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" DESTINATION ${NLOHMANN_JSON_PKGCONFIG_INSTALL_DIR} ) -endif() # JSON_Install +endif() From 9a599ae63a5b319a07fc1cca9871694fe687cef6 Mon Sep 17 00:00:00 2001 From: TotalCaesar659 <14265316+TotalCaesar659@users.noreply.github.com> Date: Fri, 28 May 2021 00:53:17 +0300 Subject: [PATCH 121/143] Update URLs to HTTPS --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 242e09841f..127b8d2054 100644 --- a/README.md +++ b/README.md @@ -1126,7 +1126,7 @@ Other Important points: ### Binary formats (BSON, CBOR, MessagePack, and UBJSON) -Though JSON is a ubiquitous data format, it is not a very compact format suitable for data exchange, for instance over a network. Hence, the library supports [BSON](http://bsonspec.org) (Binary JSON), [CBOR](https://cbor.io) (Concise Binary Object Representation), [MessagePack](https://msgpack.org), and [UBJSON](http://ubjson.org) (Universal Binary JSON Specification) to efficiently encode JSON values to byte vectors and to decode such vectors. +Though JSON is a ubiquitous data format, it is not a very compact format suitable for data exchange, for instance over a network. Hence, the library supports [BSON](https://bsonspec.org) (Binary JSON), [CBOR](https://cbor.io) (Concise Binary Object Representation), [MessagePack](https://msgpack.org), and [UBJSON](https://ubjson.org) (Universal Binary JSON Specification) to efficiently encode JSON values to byte vectors and to decode such vectors. ```cpp // create a JSON value @@ -1282,9 +1282,9 @@ The following compilers are currently used in continuous integration at [Travis] ## License - + -The class is licensed under the [MIT License](http://opensource.org/licenses/MIT): +The class is licensed under the [MIT License](https://opensource.org/licenses/MIT): Copyright © 2013-2021 [Niels Lohmann](https://nlohmann.me) @@ -1296,9 +1296,9 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I * * * -The class contains the UTF-8 Decoder from Bjoern Hoehrmann which is licensed under the [MIT License](http://opensource.org/licenses/MIT) (see above). Copyright © 2008-2009 [Björn Hoehrmann](https://bjoern.hoehrmann.de/) +The class contains the UTF-8 Decoder from Bjoern Hoehrmann which is licensed under the [MIT License](https://opensource.org/licenses/MIT) (see above). Copyright © 2008-2009 [Björn Hoehrmann](https://bjoern.hoehrmann.de/) -The class contains a slightly modified version of the Grisu2 algorithm from Florian Loitsch which is licensed under the [MIT License](http://opensource.org/licenses/MIT) (see above). Copyright © 2009 [Florian Loitsch](https://florian.loitsch.com/) +The class contains a slightly modified version of the Grisu2 algorithm from Florian Loitsch which is licensed under the [MIT License](https://opensource.org/licenses/MIT) (see above). Copyright © 2009 [Florian Loitsch](https://florian.loitsch.com/) The class contains a copy of [Hedley](https://nemequ.github.io/hedley/) from Evan Nemerson which is licensed as [CC0-1.0](https://creativecommons.org/publicdomain/zero/1.0/). @@ -1574,7 +1574,7 @@ The library itself consists of a single header file licensed under the MIT licen - [**libFuzzer**](https://llvm.org/docs/LibFuzzer.html) to implement fuzz testing for OSS-Fuzz - [**OSS-Fuzz**](https://github.com/google/oss-fuzz) for continuous fuzz testing of the library ([project repository](https://github.com/google/oss-fuzz/tree/master/projects/json)) - [**Probot**](https://probot.github.io) for automating maintainer tasks such as closing stale issues, requesting missing information, or detecting toxic comments. -- [**send_to_wandbox**](https://github.com/nlohmann/json/blob/develop/doc/scripts/send_to_wandbox.py) to send code examples to [Wandbox](http://melpon.org/wandbox) +- [**send_to_wandbox**](https://github.com/nlohmann/json/blob/develop/doc/scripts/send_to_wandbox.py) to send code examples to [Wandbox](https://wandbox.org) - [**Travis**](https://travis-ci.org) for [continuous integration](https://travis-ci.org/nlohmann/json) on Linux and macOS - [**Valgrind**](https://valgrind.org) to check for correct memory management - [**Wandbox**](https://wandbox.org) for [online examples](https://wandbox.org/permlink/3lCHrFUZANONKv7a) From 311730bc3dbf09b4b739ea682fe406eb4f5499b5 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 28 May 2021 22:23:50 +0200 Subject: [PATCH 122/143] :construction_worker: add C++ standards to macOS matrix #2491 --- .github/workflows/macos.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 42e9098b56..22d81a05fd 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -13,13 +13,14 @@ jobs: strategy: matrix: xcode: [12.4, 12.3, 12.2, 12.1.1, 12.1, 12, 11.7, 11.6, 11.5, 11.4.1, 11.3.1, 11.2.1, 10.3] + standard: [11, 14, 17, 20] env: DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer steps: - uses: actions/checkout@v2 - name: cmake - run: cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On -DJSON_FastTests=ON + run: cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On -DJSON_FastTests=ON -DCMAKE_CXX_STANDARD={{ matrix.standard }} -DCMAKE_CXX_STANDARD_REQUIRED=ON - name: build run: cmake --build build --parallel 10 - name: test From 9f5ee3f1453bc2921bee720c99868ebfcdace4cd Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 28 May 2021 22:27:46 +0200 Subject: [PATCH 123/143] :construction_worker: fix syntax --- .github/workflows/macos.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 22d81a05fd..ecc51d6f5a 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -20,7 +20,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: cmake - run: cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On -DJSON_FastTests=ON -DCMAKE_CXX_STANDARD={{ matrix.standard }} -DCMAKE_CXX_STANDARD_REQUIRED=ON + run: cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On -DJSON_FastTests=ON -DCMAKE_CXX_STANDARD=${{ matrix.standard }} -DCMAKE_CXX_STANDARD_REQUIRED=ON - name: build run: cmake --build build --parallel 10 - name: test From 2b685c744bacd31cfd286a05de5d512bd142a2e9 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 29 May 2021 13:10:28 +0200 Subject: [PATCH 124/143] :construction_worker: reduce load --- .github/workflows/macos.yml | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index ecc51d6f5a..13d1192fd3 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -13,10 +13,26 @@ jobs: strategy: matrix: xcode: [12.4, 12.3, 12.2, 12.1.1, 12.1, 12, 11.7, 11.6, 11.5, 11.4.1, 11.3.1, 11.2.1, 10.3] - standard: [11, 14, 17, 20] env: DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer + steps: + - uses: actions/checkout@v2 + - name: cmake + run: cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On -DJSON_FastTests=ON + - name: build + run: cmake --build build --parallel 10 + - name: test + run: cd build ; ctest -j 10 --output-on-failure + + xcode_standards: + runs-on: macos-10.15 + strategy: + matrix: + standard: [11, 14, 17, 20] + env: + DEVELOPER_DIR: /Applications/Xcode_12.4.app/Contents/Developer + steps: - uses: actions/checkout@v2 - name: cmake From 30e52eb0464bb7654da7393bc2804b7a15662d24 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 30 May 2021 13:28:58 +0200 Subject: [PATCH 125/143] :construction_worker: execute all tests --- .github/workflows/macos.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 13d1192fd3..bf9549fcf8 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -36,7 +36,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: cmake - run: cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On -DJSON_FastTests=ON -DCMAKE_CXX_STANDARD=${{ matrix.standard }} -DCMAKE_CXX_STANDARD_REQUIRED=ON + run: cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -DJSON_BuildTests=On -DCMAKE_CXX_STANDARD=${{ matrix.standard }} -DCMAKE_CXX_STANDARD_REQUIRED=ON - name: build run: cmake --build build --parallel 10 - name: test From ae9bbbc9412656e9d71be0862ba919dccf0b21ec Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Mon, 31 May 2021 14:26:45 +0200 Subject: [PATCH 126/143] include io only if JSON_NO_IO is not set for #2728 --- include/nlohmann/detail/input/input_adapters.hpp | 7 +++++-- include/nlohmann/detail/output/output_adapters.hpp | 8 ++++++-- include/nlohmann/json.hpp | 6 ++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 9bb4a2b483..fd9a3ab88e 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -2,9 +2,7 @@ #include // array #include // size_t -#include //FILE * #include // strlen -#include // istream #include // begin, end, iterator_traits, random_access_iterator_tag, distance, next #include // shared_ptr, make_shared, addressof #include // accumulate @@ -12,6 +10,11 @@ #include // enable_if, is_base_of, is_pointer, is_integral, remove_pointer #include // pair, declval +#ifndef JSON_NO_IO + #include //FILE * + #include // istream +#endif // JSON_NO_IO + #include #include diff --git a/include/nlohmann/detail/output/output_adapters.hpp b/include/nlohmann/detail/output/output_adapters.hpp index 46c82e9fda..d192da4e51 100644 --- a/include/nlohmann/detail/output/output_adapters.hpp +++ b/include/nlohmann/detail/output/output_adapters.hpp @@ -2,12 +2,16 @@ #include // copy #include // size_t -#include // streamsize #include // back_inserter #include // shared_ptr, make_shared -#include // basic_ostream #include // basic_string #include // vector + +#ifndef JSON_NO_IO + #include // streamsize + #include // basic_ostream +#endif // JSON_NO_IO + #include namespace nlohmann diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index a783e3d596..70e263bf47 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -38,7 +38,9 @@ SOFTWARE. #include // nullptr_t, ptrdiff_t, size_t #include // hash, less #include // initializer_list -#include // istream, ostream +#ifndef JSON_NO_IO + #include // istream, ostream +#endif // JSON_NO_IO #include // random_access_iterator_tag #include // unique_ptr #include // accumulate @@ -6882,7 +6884,7 @@ class basic_json parser(detail::input_adapter(i)).parse(false, j); return i; } -#endif // JSON_NO_IO +#endif // JSON_NO_IO /// @} /////////////////////////// From e939b59683281cc4c061597868e78786a9812482 Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Mon, 31 May 2021 14:27:23 +0200 Subject: [PATCH 127/143] fixed amalgation file for #2728 --- single_include/nlohmann/json.hpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 752f8d8aff..3ccab30018 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -38,7 +38,9 @@ SOFTWARE. #include // nullptr_t, ptrdiff_t, size_t #include // hash, less #include // initializer_list -#include // istream, ostream +#ifndef JSON_NO_IO + #include // istream, ostream +#endif // JSON_NO_IO #include // random_access_iterator_tag #include // unique_ptr #include // accumulate @@ -4721,9 +4723,7 @@ std::size_t hash(const BasicJsonType& j) #include // array #include // size_t -#include //FILE * #include // strlen -#include // istream #include // begin, end, iterator_traits, random_access_iterator_tag, distance, next #include // shared_ptr, make_shared, addressof #include // accumulate @@ -4731,6 +4731,11 @@ std::size_t hash(const BasicJsonType& j) #include // enable_if, is_base_of, is_pointer, is_integral, remove_pointer #include // pair, declval +#ifndef JSON_NO_IO + #include //FILE * + #include // istream +#endif // JSON_NO_IO + // #include // #include @@ -12619,12 +12624,16 @@ class json_ref #include // copy #include // size_t -#include // streamsize #include // back_inserter #include // shared_ptr, make_shared -#include // basic_ostream #include // basic_string #include // vector + +#ifndef JSON_NO_IO + #include // streamsize + #include // basic_ostream +#endif // JSON_NO_IO + // #include @@ -23378,7 +23387,7 @@ class basic_json parser(detail::input_adapter(i)).parse(false, j); return i; } -#endif // JSON_NO_IO +#endif // JSON_NO_IO /// @} /////////////////////////// From 032f32ebe43cb44ebbebb4540769c9b36353f3a1 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 13 Jun 2021 14:04:20 +0200 Subject: [PATCH 128/143] :construction_worker: add Clang 12 --- .github/workflows/windows.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index c9f4e3b9b3..c696cb9dd3 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -98,7 +98,7 @@ jobs: runs-on: windows-latest strategy: matrix: - version: [10, 11] + version: [10, 11, 12] steps: - uses: actions/checkout@v2 From 802895b48303efb066c5b6b53a65166824c984b2 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 13 Jun 2021 14:14:10 +0200 Subject: [PATCH 129/143] :construction_worker: remove Clang 10 --- .github/workflows/windows.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index c696cb9dd3..fad51b632e 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -98,7 +98,7 @@ jobs: runs-on: windows-latest strategy: matrix: - version: [10, 11, 12] + version: [11, 12] steps: - uses: actions/checkout@v2 From 9710108d6aa8ec3b17564b1bc62ea904e570b3d9 Mon Sep 17 00:00:00 2001 From: justanotheranonymoususer Date: Thu, 17 Jun 2021 21:39:29 +0300 Subject: [PATCH 130/143] Consistency with `using` in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 127b8d2054..1c11617b26 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ Thanks everyone! #include // for convenience -using json = nlohmann::json; +using nlohmann::json; ``` to the files you want to process JSON and set the necessary switches to enable C++11 (e.g., `-std=c++11` for GCC and Clang). From c3d7fcb76ced6746e67fa9a63bb995468b090134 Mon Sep 17 00:00:00 2001 From: justanotheranonymoususer Date: Fri, 18 Jun 2021 11:04:53 +0300 Subject: [PATCH 131/143] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1c11617b26..96e7a22e11 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ Thanks everyone! #include // for convenience -using nlohmann::json; +using json = nlohmann::json; ``` to the files you want to process JSON and set the necessary switches to enable C++11 (e.g., `-std=c++11` for GCC and Clang). @@ -870,7 +870,7 @@ assert(p == p2); To make this work with one of your types, you only need to provide two functions: ```cpp -using nlohmann::json; +using json = nlohmann::json; namespace ns { void to_json(json& j, const person& p) { From e970f310668bd19d74c3596b8eb3cfa2daaa90b2 Mon Sep 17 00:00:00 2001 From: justanotheranonymoususer Date: Fri, 18 Jun 2021 12:44:27 +0300 Subject: [PATCH 132/143] Update arbitrary_types.md --- doc/mkdocs/docs/features/arbitrary_types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/mkdocs/docs/features/arbitrary_types.md b/doc/mkdocs/docs/features/arbitrary_types.md index 23913bba22..4234191309 100644 --- a/doc/mkdocs/docs/features/arbitrary_types.md +++ b/doc/mkdocs/docs/features/arbitrary_types.md @@ -54,7 +54,7 @@ assert(p == p2); To make this work with one of your types, you only need to provide two functions: ```cpp -using nlohmann::json; +using json = nlohmann::json; namespace ns { void to_json(json& j, const person& p) { From b7e493e98ad8351e5e9d0d21550e350d04fa34e3 Mon Sep 17 00:00:00 2001 From: Rafail Giavrimis <47496212+grafail@users.noreply.github.com> Date: Tue, 1 Jun 2021 13:17:53 +0100 Subject: [PATCH 133/143] Specified git branch for google benchmark fetch The default branch was renamed from "master" to "main", which breaks the cloning process. --- benchmarks/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index ee4db1912d..1243f54eaf 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -11,6 +11,7 @@ include(FetchContent) FetchContent_Declare( benchmark GIT_REPOSITORY https://github.com/google/benchmark.git + GIT_TAG origin/main GIT_SHALLOW TRUE ) From 71fd9bd954e96643e21c74bb7085c1e19873255b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 6 Jul 2021 08:46:17 +0200 Subject: [PATCH 134/143] :memo: fix documentation (#2845) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 96e7a22e11..eabf1302a6 100644 --- a/README.md +++ b/README.md @@ -1650,7 +1650,7 @@ $ ctest --output-on-failure Note that during the `ctest` stage, several JSON test files are downloaded from an [external repository](https://github.com/nlohmann/json_test_data). If policies forbid downloading artifacts during testing, you can download the files yourself and pass the directory with the test files via `-DJSON_TestDataDirectory=path` to CMake. Then, no Internet connectivity is required. See [issue #2189](https://github.com/nlohmann/json/issues/2189) for more information. -In case you have downloaded the library rather than checked out the code via Git, test `cmake_fetch_content_configure`. Please execute `ctest -LE git_required` to skip these tests. See [issue #2189](https://github.com/nlohmann/json/issues/2189) for more information. +In case you have downloaded the library rather than checked out the code via Git, test `cmake_fetch_content_configure` will fail. Please execute `ctest -LE git_required` to skip these tests. See [issue #2189](https://github.com/nlohmann/json/issues/2189) for more information. Some tests change the installed files and hence make the whole process not reproducible. Please execute `ctest -LE not_reproducible` to skip these tests. See [issue #2324](https://github.com/nlohmann/json/issues/2324) for more information. From 5c8d0af5cea8614b1b48ad382550748651154582 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 10 Jul 2021 12:50:59 +0200 Subject: [PATCH 135/143] :memo: add comment for handling of negative zeros #2854 --- doc/mkdocs/docs/features/types/number_handling.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/doc/mkdocs/docs/features/types/number_handling.md b/doc/mkdocs/docs/features/types/number_handling.md index 4224c155b2..aa8df7ad86 100644 --- a/doc/mkdocs/docs/features/types/number_handling.md +++ b/doc/mkdocs/docs/features/types/number_handling.md @@ -96,6 +96,21 @@ This is the same behavior as the code `#!c double x = 3.141592653589793238462643 - All integers outside the range $[-2^{63}, 2^{64}-1]$, as well as floating-point numbers are stored as `double`. This also concurs with the specification above. +### Zeros + +The JSON number grammar allows for different ways to express zero, and this library will store zeros differently: + +| Literal | Stored value and type | Serialization | +| ------- | --------------------- | ------------- | +| `0` | `#!c std::uint64_t(0)` | `0` | +| `-0` | `#!c std::int64_t(0)` | `0` | +| `0.0` | `#!c double(0.0)` | `0.0` | +| `-0.0` | `#!c double(-0.0)` | `-0.0` | +| `0E0` | `#!c double(0.0)` | `0.0` | +| `-0E0` | `#!c double(-0.0)` | `-0.0` | + +That is, `-0` is stored as a signed integer, but the serialization does not reproduce the `-`. + ### Number serialization - Integer numbers are serialized as is; that is, no scientific notation is used. From a82f66beed60cab657a909be7eb9192dbcb3982f Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 10 Jul 2021 12:51:18 +0200 Subject: [PATCH 136/143] :memo: document JSON_NO_IO macro #2842 --- doc/mkdocs/docs/features/macros.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/mkdocs/docs/features/macros.md b/doc/mkdocs/docs/features/macros.md index ee401c5dcd..d008393f76 100644 --- a/doc/mkdocs/docs/features/macros.md +++ b/doc/mkdocs/docs/features/macros.md @@ -32,6 +32,10 @@ When defining `JSON_NOEXCEPTION`, `#!cpp try` is replaced by `#!cpp if (true)`, The same effect is achieved by setting the compiler flag `-fno-exceptions`. +## `JSON_NO_IO` + +When defined, headers ``, ``, ``, ``, and `` are not included and parse functions relying on these headers are excluded. This is relevant for environment where these I/O functions are disallowed for security reasons (e.g., Intel Software Guard Extensions (SGX)). + ## `JSON_SKIP_UNSUPPORTED_COMPILER_CHECK` When defined, the library will not create a compile error when a known unsupported compiler is detected. This allows to use the library with compilers that do not fully support C++11 and may only work if unsupported features are not used. From 7442c24662dd97fbbf12077b1dfa1aa0da8258ea Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 10 Jul 2021 13:44:13 +0200 Subject: [PATCH 137/143] :rotating_light: suppress missingReturn warnings --- cmake/ci.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/ci.cmake b/cmake/ci.cmake index d7d94b2bf8..e2d175d411 100644 --- a/cmake/ci.cmake +++ b/cmake/ci.cmake @@ -560,7 +560,7 @@ add_custom_target(ci_clang_analyze ############################################################################### add_custom_target(ci_cppcheck - COMMAND ${CPPCHECK_TOOL} --enable=warning --inline-suppr --inconclusive --force --std=c++11 ${PROJECT_SOURCE_DIR}/single_include/nlohmann/json.hpp --error-exitcode=1 + COMMAND ${CPPCHECK_TOOL} --enable=warning --suppress=missingReturn --inline-suppr --inconclusive --force --std=c++11 ${PROJECT_SOURCE_DIR}/single_include/nlohmann/json.hpp --error-exitcode=1 COMMENT "Check code with Cppcheck" ) From 249688037fab6144bb0279aeed687efcde4b3baa Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 11 Jul 2021 09:33:43 +0200 Subject: [PATCH 138/143] :alembic: fix coverage --- cmake/ci.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/ci.cmake b/cmake/ci.cmake index e2d175d411..84cbfe4c22 100644 --- a/cmake/ci.cmake +++ b/cmake/ci.cmake @@ -467,7 +467,7 @@ add_custom_target(ci_test_diagnostics ############################################################################### add_custom_target(ci_test_coverage - COMMAND CXX=${GCC_TOOL} ${CMAKE_COMMAND} + COMMAND CXX=g++ ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug -GNinja -DCMAKE_CXX_FLAGS="--coverage;-fprofile-arcs;-ftest-coverage" -DJSON_BuildTests=ON -DJSON_MultipleHeaders=ON -S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_coverage @@ -475,7 +475,7 @@ add_custom_target(ci_test_coverage COMMAND cd ${PROJECT_BINARY_DIR}/build_coverage && ${CMAKE_CTEST_COMMAND} --parallel ${N} --output-on-failure COMMAND ${LCOV_TOOL} --directory . --capture --output-file json.info --rc lcov_branch_coverage=1 - COMMAND ${LCOV_TOOL} -e json.info ${SRC_FILES} --output-file json.info.filtered --gcov-tool ${GCOV_TOOL} --rc lcov_branch_coverage=1 + COMMAND ${LCOV_TOOL} -e json.info ${SRC_FILES} --output-file json.info.filtered --rc lcov_branch_coverage=1 COMMAND ${CMAKE_SOURCE_DIR}/test/thirdparty/imapdl/filterbr.py json.info.filtered > json.info.filtered.noexcept COMMAND genhtml --title "JSON for Modern C++" --legend --demangle-cpp --output-directory html --show-details --branch-coverage json.info.filtered.noexcept From d40e98ecef776b909af4d91ffa266a966290e73f Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 12 Jul 2021 13:38:28 +0200 Subject: [PATCH 139/143] :bug: fix assertion failure #2838 --- include/nlohmann/json.hpp | 61 ++++++++++++++++++++++++++------ single_include/nlohmann/json.hpp | 61 ++++++++++++++++++++++++++------ test/src/unit-diagnostics.cpp | 57 +++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 22 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index c55bdf54bd..0622d1a9c3 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -5371,8 +5371,18 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // add element to array (move semantics) + const auto capacity = m_value.array->capacity(); m_value.array->push_back(std::move(val)); - set_parent(m_value.array->back()); + if (capacity == m_value.array->capacity()) + { + // capacity has not changed: updating parent of last element is sufficient + set_parent(m_value.array->back()); + } + else + { + // capacity has changed: update all elements' parents + set_parents(); + } // if val is moved from, basic_json move constructor marks it null so we do not call the destructor } @@ -5407,7 +5417,18 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // add element to array + const auto capacity = m_value.array->capacity(); m_value.array->push_back(val); + if (capacity == m_value.array->capacity()) + { + // capacity has not changed: updating parent of last element is sufficient + set_parent(m_value.array->back()); + } + else + { + // capacity has changed: update all elements' parents + set_parents(); + } set_parent(m_value.array->back()); } @@ -5562,12 +5583,18 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // add element to array (perfect forwarding) -#ifdef JSON_HAS_CPP_17 - return set_parent(m_value.array->emplace_back(std::forward(args)...)); -#else + const auto capacity = m_value.array->capacity(); m_value.array->emplace_back(std::forward(args)...); - return set_parent(m_value.array->back()); -#endif + + if (capacity == m_value.array->capacity()) + { + // capacity has not changed: updating parent of last element is sufficient + return set_parent(m_value.array->back()); + } + + // capacity has changed: update all elements' parents + set_parents(); + return m_value.array->back(); } /*! @@ -5630,12 +5657,13 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /// @note: This uses std::distance to support GCC 4.8, /// see https://github.com/nlohmann/json/pull/1257 template - iterator insert_iterator(const_iterator pos, Args&& ... args) + iterator insert_iterator(const_iterator pos, typename iterator::difference_type cnt, Args&& ... args) { iterator result(this); JSON_ASSERT(m_value.array != nullptr); auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator); + const auto capacity = m_value.array->capacity(); m_value.array->insert(pos.m_it.array_iterator, std::forward(args)...); result.m_it.array_iterator = m_value.array->begin() + insert_pos; @@ -5643,6 +5671,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val); // but the return value of insert is missing in GCC 4.8, so it is written this way instead. + if (capacity == m_value.array->capacity()) + { + // capacity has not changed: updating parent of inserted elements is sufficient + set_parents(result, cnt); + } + else + { + // capacity has changed: update all elements' parents + set_parents(); + } + return result; } @@ -5680,7 +5719,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return set_parents(insert_iterator(pos, val), static_cast(1)); + return insert_iterator(pos, static_cast(1), val); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); @@ -5731,7 +5770,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return set_parents(insert_iterator(pos, cnt, val), static_cast(cnt)); + return insert_iterator(pos, static_cast(cnt), cnt, val); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); @@ -5793,7 +5832,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return set_parents(insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator), std::distance(first, last)); + return insert_iterator(pos, std::distance(first, last), first.m_it.array_iterator, last.m_it.array_iterator); } /*! @@ -5835,7 +5874,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return set_parents(insert_iterator(pos, ilist.begin(), ilist.end()), static_cast(ilist.size())); + return insert_iterator(pos, static_cast(ilist.size()), ilist.begin(), ilist.end()); } /*! diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index cbe69ef472..999d136aa3 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -22406,8 +22406,18 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // add element to array (move semantics) + const auto capacity = m_value.array->capacity(); m_value.array->push_back(std::move(val)); - set_parent(m_value.array->back()); + if (capacity == m_value.array->capacity()) + { + // capacity has not changed: updating parent of last element is sufficient + set_parent(m_value.array->back()); + } + else + { + // capacity has changed: update all elements' parents + set_parents(); + } // if val is moved from, basic_json move constructor marks it null so we do not call the destructor } @@ -22442,7 +22452,18 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // add element to array + const auto capacity = m_value.array->capacity(); m_value.array->push_back(val); + if (capacity == m_value.array->capacity()) + { + // capacity has not changed: updating parent of last element is sufficient + set_parent(m_value.array->back()); + } + else + { + // capacity has changed: update all elements' parents + set_parents(); + } set_parent(m_value.array->back()); } @@ -22597,12 +22618,18 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // add element to array (perfect forwarding) -#ifdef JSON_HAS_CPP_17 - return set_parent(m_value.array->emplace_back(std::forward(args)...)); -#else + const auto capacity = m_value.array->capacity(); m_value.array->emplace_back(std::forward(args)...); - return set_parent(m_value.array->back()); -#endif + + if (capacity == m_value.array->capacity()) + { + // capacity has not changed: updating parent of last element is sufficient + return set_parent(m_value.array->back()); + } + + // capacity has changed: update all elements' parents + set_parents(); + return m_value.array->back(); } /*! @@ -22665,12 +22692,13 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /// @note: This uses std::distance to support GCC 4.8, /// see https://github.com/nlohmann/json/pull/1257 template - iterator insert_iterator(const_iterator pos, Args&& ... args) + iterator insert_iterator(const_iterator pos, typename iterator::difference_type cnt, Args&& ... args) { iterator result(this); JSON_ASSERT(m_value.array != nullptr); auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator); + const auto capacity = m_value.array->capacity(); m_value.array->insert(pos.m_it.array_iterator, std::forward(args)...); result.m_it.array_iterator = m_value.array->begin() + insert_pos; @@ -22678,6 +22706,17 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val); // but the return value of insert is missing in GCC 4.8, so it is written this way instead. + if (capacity == m_value.array->capacity()) + { + // capacity has not changed: updating parent of inserted elements is sufficient + set_parents(result, cnt); + } + else + { + // capacity has changed: update all elements' parents + set_parents(); + } + return result; } @@ -22715,7 +22754,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return set_parents(insert_iterator(pos, val), static_cast(1)); + return insert_iterator(pos, static_cast(1), val); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); @@ -22766,7 +22805,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return set_parents(insert_iterator(pos, cnt, val), static_cast(cnt)); + return insert_iterator(pos, static_cast(cnt), cnt, val); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); @@ -22828,7 +22867,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return set_parents(insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator), std::distance(first, last)); + return insert_iterator(pos, std::distance(first, last), first.m_it.array_iterator, last.m_it.array_iterator); } /*! @@ -22870,7 +22909,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return set_parents(insert_iterator(pos, ilist.begin(), ilist.end()), static_cast(ilist.size())); + return insert_iterator(pos, static_cast(ilist.size()), ilist.begin(), ilist.end()); } /*! diff --git a/test/src/unit-diagnostics.cpp b/test/src/unit-diagnostics.cpp index 21ced33b1f..c0b5a64764 100644 --- a/test/src/unit-diagnostics.cpp +++ b/test/src/unit-diagnostics.cpp @@ -109,4 +109,61 @@ TEST_CASE("Better diagnostics") j["/foo"] = {1, 2, 3}; CHECK_THROWS_WITH_AS(j.unflatten(), "[json.exception.type_error.315] (/~1foo) values in object must be primitive", json::type_error); } + + SECTION("Regression test for https://github.com/nlohmann/json/issues/2838") + { + // void push_back(basic_json&& val) + { + json j_arr = json::array(); + j_arr.push_back(json::object()); + j_arr.push_back(json::object()); + json j_obj = json::object(); + j_obj["key"] = j_arr; + } + + // void push_back(const basic_json& val) + { + json j_arr = json::array(); + auto object = json::object(); + j_arr.push_back(object); + j_arr.push_back(object); + json j_obj = json::object(); + j_obj["key"] = j_arr; + } + + // reference emplace_back(Args&& ... args) + { + json j_arr = json::array(); + j_arr.emplace_back(json::object()); + j_arr.emplace_back(json::object()); + json j_obj = json::object(); + j_obj["key"] = j_arr; + } + + // iterator insert(const_iterator pos, const basic_json& val) + { + json j_arr = json::array(); + j_arr.insert(j_arr.begin(), json::object()); + j_arr.insert(j_arr.begin(), json::object()); + json j_obj = json::object(); + j_obj["key"] = j_arr; + } + + // iterator insert(const_iterator pos, size_type cnt, const basic_json& val) + { + json j_arr = json::array(); + j_arr.insert(j_arr.begin(), 2, json::object()); + json j_obj = json::object(); + j_obj["key"] = j_arr; + } + + // iterator insert(const_iterator pos, const_iterator first, const_iterator last) + { + json j_arr = json::array(); + json j_objects = {json::object(), json::object()}; + j_arr.insert(j_arr.begin(), j_objects.begin(), j_objects.end()); + json j_obj = json::object(); + j_obj["key"] = j_arr; + } + } } From b0730f29cf2376a27591197eb793410370fdd32b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 12 Jul 2021 15:24:06 +0200 Subject: [PATCH 140/143] :bug: fix logics --- include/nlohmann/json.hpp | 23 ++++++----------------- single_include/nlohmann/json.hpp | 23 ++++++----------------- test/src/unit-diagnostics.cpp | 8 ++++++++ 3 files changed, 20 insertions(+), 34 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 0622d1a9c3..710c5a5cff 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -5657,13 +5657,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /// @note: This uses std::distance to support GCC 4.8, /// see https://github.com/nlohmann/json/pull/1257 template - iterator insert_iterator(const_iterator pos, typename iterator::difference_type cnt, Args&& ... args) + iterator insert_iterator(const_iterator pos, Args&& ... args) { iterator result(this); JSON_ASSERT(m_value.array != nullptr); auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator); - const auto capacity = m_value.array->capacity(); m_value.array->insert(pos.m_it.array_iterator, std::forward(args)...); result.m_it.array_iterator = m_value.array->begin() + insert_pos; @@ -5671,17 +5670,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val); // but the return value of insert is missing in GCC 4.8, so it is written this way instead. - if (capacity == m_value.array->capacity()) - { - // capacity has not changed: updating parent of inserted elements is sufficient - set_parents(result, cnt); - } - else - { - // capacity has changed: update all elements' parents - set_parents(); - } - + set_parents(); return result; } @@ -5719,7 +5708,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return insert_iterator(pos, static_cast(1), val); + return insert_iterator(pos, val); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); @@ -5770,7 +5759,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return insert_iterator(pos, static_cast(cnt), cnt, val); + return insert_iterator(pos, cnt, val); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); @@ -5832,7 +5821,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return insert_iterator(pos, std::distance(first, last), first.m_it.array_iterator, last.m_it.array_iterator); + return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); } /*! @@ -5874,7 +5863,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return insert_iterator(pos, static_cast(ilist.size()), ilist.begin(), ilist.end()); + return insert_iterator(pos, ilist.begin(), ilist.end()); } /*! diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 999d136aa3..3700505c96 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -22692,13 +22692,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec /// @note: This uses std::distance to support GCC 4.8, /// see https://github.com/nlohmann/json/pull/1257 template - iterator insert_iterator(const_iterator pos, typename iterator::difference_type cnt, Args&& ... args) + iterator insert_iterator(const_iterator pos, Args&& ... args) { iterator result(this); JSON_ASSERT(m_value.array != nullptr); auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator); - const auto capacity = m_value.array->capacity(); m_value.array->insert(pos.m_it.array_iterator, std::forward(args)...); result.m_it.array_iterator = m_value.array->begin() + insert_pos; @@ -22706,17 +22705,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val); // but the return value of insert is missing in GCC 4.8, so it is written this way instead. - if (capacity == m_value.array->capacity()) - { - // capacity has not changed: updating parent of inserted elements is sufficient - set_parents(result, cnt); - } - else - { - // capacity has changed: update all elements' parents - set_parents(); - } - + set_parents(); return result; } @@ -22754,7 +22743,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return insert_iterator(pos, static_cast(1), val); + return insert_iterator(pos, val); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); @@ -22805,7 +22794,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return insert_iterator(pos, static_cast(cnt), cnt, val); + return insert_iterator(pos, cnt, val); } JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); @@ -22867,7 +22856,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return insert_iterator(pos, std::distance(first, last), first.m_it.array_iterator, last.m_it.array_iterator); + return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); } /*! @@ -22909,7 +22898,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // insert to array and return iterator - return insert_iterator(pos, static_cast(ilist.size()), ilist.begin(), ilist.end()); + return insert_iterator(pos, ilist.begin(), ilist.end()); } /*! diff --git a/test/src/unit-diagnostics.cpp b/test/src/unit-diagnostics.cpp index c0b5a64764..ebbe64f384 100644 --- a/test/src/unit-diagnostics.cpp +++ b/test/src/unit-diagnostics.cpp @@ -117,6 +117,8 @@ TEST_CASE("Better diagnostics") json j_arr = json::array(); j_arr.push_back(json::object()); j_arr.push_back(json::object()); + j_arr.push_back(json::object()); + j_arr.push_back(json::object()); json j_obj = json::object(); j_obj["key"] = j_arr; } @@ -127,6 +129,8 @@ TEST_CASE("Better diagnostics") auto object = json::object(); j_arr.push_back(object); j_arr.push_back(object); + j_arr.push_back(object); + j_arr.push_back(object); json j_obj = json::object(); j_obj["key"] = j_arr; } @@ -136,6 +140,8 @@ TEST_CASE("Better diagnostics") json j_arr = json::array(); j_arr.emplace_back(json::object()); j_arr.emplace_back(json::object()); + j_arr.emplace_back(json::object()); + j_arr.emplace_back(json::object()); json j_obj = json::object(); j_obj["key"] = j_arr; } @@ -145,6 +151,8 @@ TEST_CASE("Better diagnostics") json j_arr = json::array(); j_arr.insert(j_arr.begin(), json::object()); j_arr.insert(j_arr.begin(), json::object()); + j_arr.insert(j_arr.begin(), json::object()); + j_arr.insert(j_arr.begin(), json::object()); json j_obj = json::object(); j_obj["key"] = j_arr; } From bc7e8faa4f1f463ef97b2c1491b1f8886bec154b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 12 Jul 2021 19:21:07 +0200 Subject: [PATCH 141/143] :fire: remove duplicated line --- include/nlohmann/json.hpp | 1 - single_include/nlohmann/json.hpp | 1 - 2 files changed, 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 710c5a5cff..ae77747b53 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -5429,7 +5429,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // capacity has changed: update all elements' parents set_parents(); } - set_parent(m_value.array->back()); } /*! diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 3700505c96..2500eb9cb4 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -22464,7 +22464,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // capacity has changed: update all elements' parents set_parents(); } - set_parent(m_value.array->back()); } /*! From 3bb9467073f366bfb2b46c9c6a0c1fe11219da60 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 13 Jul 2021 15:27:27 +0200 Subject: [PATCH 142/143] :recycle: move capacity check to set_parent function --- include/nlohmann/json.hpp | 53 ++++++++++++-------------------- single_include/nlohmann/json.hpp | 53 ++++++++++++-------------------- 2 files changed, 38 insertions(+), 68 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index ae77747b53..d18f082b4d 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1304,9 +1304,21 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec return it; } - reference set_parent(reference j) + reference set_parent(reference j, std::size_t old_capacity = -1) { #if JSON_DIAGNOSTICS + if (old_capacity != -1) + { + // see https://github.com/nlohmann/json/issues/2838 + JSON_ASSERT(type() == value_t::array); + if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity)) + { + // capacity has changed: update all parents + set_parents(); + return j; + } + } + j.m_parent = this; #else static_cast(j); @@ -5371,18 +5383,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // add element to array (move semantics) - const auto capacity = m_value.array->capacity(); + const auto old_capacity = m_value.array->capacity(); m_value.array->push_back(std::move(val)); - if (capacity == m_value.array->capacity()) - { - // capacity has not changed: updating parent of last element is sufficient - set_parent(m_value.array->back()); - } - else - { - // capacity has changed: update all elements' parents - set_parents(); - } + set_parent(m_value.array->back(), old_capacity); // if val is moved from, basic_json move constructor marks it null so we do not call the destructor } @@ -5417,18 +5420,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // add element to array - const auto capacity = m_value.array->capacity(); + const auto old_capacity = m_value.array->capacity(); m_value.array->push_back(val); - if (capacity == m_value.array->capacity()) - { - // capacity has not changed: updating parent of last element is sufficient - set_parent(m_value.array->back()); - } - else - { - // capacity has changed: update all elements' parents - set_parents(); - } + set_parent(m_value.array->back(), old_capacity); } /*! @@ -5582,18 +5576,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // add element to array (perfect forwarding) - const auto capacity = m_value.array->capacity(); + const auto old_capacity = m_value.array->capacity(); m_value.array->emplace_back(std::forward(args)...); - - if (capacity == m_value.array->capacity()) - { - // capacity has not changed: updating parent of last element is sufficient - return set_parent(m_value.array->back()); - } - - // capacity has changed: update all elements' parents - set_parents(); - return m_value.array->back(); + return set_parent(m_value.array->back(), old_capacity); } /*! diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2500eb9cb4..3b5a7a75de 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18339,9 +18339,21 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec return it; } - reference set_parent(reference j) + reference set_parent(reference j, std::size_t old_capacity = -1) { #if JSON_DIAGNOSTICS + if (old_capacity != -1) + { + // see https://github.com/nlohmann/json/issues/2838 + JSON_ASSERT(type() == value_t::array); + if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity)) + { + // capacity has changed: update all parents + set_parents(); + return j; + } + } + j.m_parent = this; #else static_cast(j); @@ -22406,18 +22418,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // add element to array (move semantics) - const auto capacity = m_value.array->capacity(); + const auto old_capacity = m_value.array->capacity(); m_value.array->push_back(std::move(val)); - if (capacity == m_value.array->capacity()) - { - // capacity has not changed: updating parent of last element is sufficient - set_parent(m_value.array->back()); - } - else - { - // capacity has changed: update all elements' parents - set_parents(); - } + set_parent(m_value.array->back(), old_capacity); // if val is moved from, basic_json move constructor marks it null so we do not call the destructor } @@ -22452,18 +22455,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // add element to array - const auto capacity = m_value.array->capacity(); + const auto old_capacity = m_value.array->capacity(); m_value.array->push_back(val); - if (capacity == m_value.array->capacity()) - { - // capacity has not changed: updating parent of last element is sufficient - set_parent(m_value.array->back()); - } - else - { - // capacity has changed: update all elements' parents - set_parents(); - } + set_parent(m_value.array->back(), old_capacity); } /*! @@ -22617,18 +22611,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } // add element to array (perfect forwarding) - const auto capacity = m_value.array->capacity(); + const auto old_capacity = m_value.array->capacity(); m_value.array->emplace_back(std::forward(args)...); - - if (capacity == m_value.array->capacity()) - { - // capacity has not changed: updating parent of last element is sufficient - return set_parent(m_value.array->back()); - } - - // capacity has changed: update all elements' parents - set_parents(); - return m_value.array->back(); + return set_parent(m_value.array->back(), old_capacity); } /*! From a711e1f5a721b12987b5748da37d3b8c2992764b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 13 Jul 2021 15:37:57 +0200 Subject: [PATCH 143/143] :rotating_light: fix warnings --- include/nlohmann/json.hpp | 5 +++-- single_include/nlohmann/json.hpp | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index d18f082b4d..a337c1c692 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1304,10 +1304,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec return it; } - reference set_parent(reference j, std::size_t old_capacity = -1) + reference set_parent(reference j, std::size_t old_capacity = std::size_t(-1)) { #if JSON_DIAGNOSTICS - if (old_capacity != -1) + if (old_capacity != std::size_t(-1)) { // see https://github.com/nlohmann/json/issues/2838 JSON_ASSERT(type() == value_t::array); @@ -1322,6 +1322,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec j.m_parent = this; #else static_cast(j); + static_cast(old_capacity); #endif return j; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 3b5a7a75de..429964dd7e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18339,10 +18339,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec return it; } - reference set_parent(reference j, std::size_t old_capacity = -1) + reference set_parent(reference j, std::size_t old_capacity = std::size_t(-1)) { #if JSON_DIAGNOSTICS - if (old_capacity != -1) + if (old_capacity != std::size_t(-1)) { // see https://github.com/nlohmann/json/issues/2838 JSON_ASSERT(type() == value_t::array); @@ -18357,6 +18357,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec j.m_parent = this; #else static_cast(j); + static_cast(old_capacity); #endif return j; }