From 2eb9aae537618ace13445cc0877721cd886e6081 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 6 Nov 2026 12:09:24 +0100 Subject: [PATCH] Fix CI: unused parameter without exceptions, cpplint, BUILD.bazel - json_sax_error_reporter::parse_error: mark the exception unused, as JSON_THROW does not use it with JSON_NOEXCEPTION - unit-contiguous_parser.cpp: compare the exceptions of both paths only when exceptions are enabled (invalid input aborted the test otherwise) - silence cpplint's readability/braces false positives on the if statements inside the parser's macros - add contiguous_parser.hpp to BUILD.bazel Signed-off-by: Niels Lohmann --- BUILD.bazel | 1 + include/nlohmann/detail/input/contiguous_parser.hpp | 11 ++++++----- single_include/nlohmann/json.hpp | 11 ++++++----- tests/src/unit-contiguous_parser.cpp | 9 +++++---- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 2b1c9f146..b7908bcf7 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -30,6 +30,7 @@ cc_library( "include/nlohmann/detail/exceptions.hpp", "include/nlohmann/detail/hash.hpp", "include/nlohmann/detail/input/binary_reader.hpp", + "include/nlohmann/detail/input/contiguous_parser.hpp", "include/nlohmann/detail/input/input_adapters.hpp", "include/nlohmann/detail/input/json_sax.hpp", "include/nlohmann/detail/input/lexer.hpp", diff --git a/include/nlohmann/detail/input/contiguous_parser.hpp b/include/nlohmann/detail/input/contiguous_parser.hpp index bcaa9b543..4093cbdb5 100644 --- a/include/nlohmann/detail/input/contiguous_parser.hpp +++ b/include/nlohmann/detail/input/contiguous_parser.hpp @@ -120,7 +120,7 @@ class contiguous_parser #define JSON_CONTIGUOUS_OPEN(is_object) \ do \ { \ - if (JSON_HEDLEY_UNLIKELY(depth != 0 && (depth % 64) == 0)) \ + if (JSON_HEDLEY_UNLIKELY(depth != 0 && (depth % 64) == 0)) /* NOLINT(readability/braces) */ \ { \ outer.push_back(kinds); \ kinds = 0; \ @@ -226,13 +226,13 @@ class contiguous_parser // what may follow a value in an array: a comma and the next value, or the end #define JSON_CONTIGUOUS_AFTER_ELEMENT \ JSON_CONTIGUOUS_WS(); \ - if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == ',')) \ + if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == ',')) /* NOLINT(readability/braces) */ \ { \ ++p; \ JSON_CONTIGUOUS_WS(); \ goto array_value; \ } \ - if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == ']')) \ + if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == ']')) /* NOLINT(readability/braces) */ \ { \ ++p; \ if (JSON_HEDLEY_UNLIKELY(!report(sax->end_array()))) \ @@ -246,13 +246,13 @@ class contiguous_parser // what may follow a value in an object: a comma and the next member, or the end #define JSON_CONTIGUOUS_AFTER_MEMBER \ JSON_CONTIGUOUS_WS(); \ - if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == ',')) \ + if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == ',')) /* NOLINT(readability/braces) */ \ { \ ++p; \ JSON_CONTIGUOUS_WS(); \ goto object_key; \ } \ - if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == '}')) \ + if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == '}')) /* NOLINT(readability/braces) */ \ { \ ++p; \ if (JSON_HEDLEY_UNLIKELY(!report(sax->end_object()))) \ @@ -836,6 +836,7 @@ class json_sax_error_reporter : public json_sax_acceptor template bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const Exception& ex) { + static_cast(ex); // (unused without exceptions) if (allow_exceptions) { JSON_THROW(ex); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 30417876b..aebd9b25a 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -23962,7 +23962,7 @@ class contiguous_parser #define JSON_CONTIGUOUS_OPEN(is_object) \ do \ { \ - if (JSON_HEDLEY_UNLIKELY(depth != 0 && (depth % 64) == 0)) \ + if (JSON_HEDLEY_UNLIKELY(depth != 0 && (depth % 64) == 0)) /* NOLINT(readability/braces) */ \ { \ outer.push_back(kinds); \ kinds = 0; \ @@ -24068,13 +24068,13 @@ class contiguous_parser // what may follow a value in an array: a comma and the next value, or the end #define JSON_CONTIGUOUS_AFTER_ELEMENT \ JSON_CONTIGUOUS_WS(); \ - if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == ',')) \ + if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == ',')) /* NOLINT(readability/braces) */ \ { \ ++p; \ JSON_CONTIGUOUS_WS(); \ goto array_value; \ } \ - if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == ']')) \ + if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == ']')) /* NOLINT(readability/braces) */ \ { \ ++p; \ if (JSON_HEDLEY_UNLIKELY(!report(sax->end_array()))) \ @@ -24088,13 +24088,13 @@ class contiguous_parser // what may follow a value in an object: a comma and the next member, or the end #define JSON_CONTIGUOUS_AFTER_MEMBER \ JSON_CONTIGUOUS_WS(); \ - if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == ',')) \ + if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == ',')) /* NOLINT(readability/braces) */ \ { \ ++p; \ JSON_CONTIGUOUS_WS(); \ goto object_key; \ } \ - if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == '}')) \ + if (JSON_HEDLEY_LIKELY(JSON_CONTIGUOUS_CUR() == '}')) /* NOLINT(readability/braces) */ \ { \ ++p; \ if (JSON_HEDLEY_UNLIKELY(!report(sax->end_object()))) \ @@ -24678,6 +24678,7 @@ class json_sax_error_reporter : public json_sax_acceptor template bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const Exception& ex) { + static_cast(ex); // (unused without exceptions) if (allow_exceptions) { JSON_THROW(ex); diff --git a/tests/src/unit-contiguous_parser.cpp b/tests/src/unit-contiguous_parser.cpp index 043fa95c3..a12094e59 100644 --- a/tests/src/unit-contiguous_parser.cpp +++ b/tests/src/unit-contiguous_parser.cpp @@ -86,10 +86,10 @@ std::string describe_value(const Json& j, int depth = 0) return out; } +#if !defined(JSON_NOEXCEPTION) template std::string outcome(Parse parse) { -#if !defined(JSON_NOEXCEPTION) try { return describe_value(parse()); @@ -98,10 +98,8 @@ std::string outcome(Parse parse) { return std::string("exception: ") + e.what(); } -#else - return describe_value(parse()); -#endif } +#endif /// every SAX event with its values, the text of float tokens, and the parse /// error with its position, token and message; returns false after @@ -220,6 +218,8 @@ void check_same(const std::string& text) CAPTURE(text) const std::list bytes(text.begin(), text.end()); +#if !defined(JSON_NOEXCEPTION) + // with exceptions: the value, or the same exception for both const std::string contiguous = outcome([&] { return Json::parse(text); @@ -229,6 +229,7 @@ void check_same(const std::string& text) return Json::parse(bytes.begin(), bytes.end()); }); CHECK(contiguous == byte_path); +#endif // without exceptions: the value, or discarded for both const Json c = Json::parse(text, nullptr, false);