Guard JSON_HEDLEY_DIAGNOSTIC_PUSH/POP with the same compiler check as the pragma they bracket

Addresses review feedback from @gregmarr on PR #5485: the push/pop calls
were unconditional, so compilers other than the one the ignored-pragma
targets (e.g. MSVC, or GCC where the pair only applies under __clang__)
now did a needless push/pop with nothing suppressed in between. Move the
existing #ifdef __GNUC__ / #if defined(__clang__) guard to also cover the
push/pop, restoring the original zero-overhead behavior on other compilers
while still emitting the pragma itself through Hedley.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-09-08 19:55:54 +02:00
parent d0e5a087e9
commit c7d3d9016c
6 changed files with 40 additions and 16 deletions
@@ -1074,8 +1074,8 @@ char* to_chars(char* first, const char* last, FloatType value)
*first++ = '-';
}
JSON_HEDLEY_DIAGNOSTIC_PUSH
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_PUSH
JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal")
#endif
if (value == 0) // +-0
@@ -1086,7 +1086,9 @@ char* to_chars(char* first, const char* last, FloatType value)
*first++ = '0';
return first;
}
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_POP
#endif
JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
+4 -2
View File
@@ -32,8 +32,8 @@
// functions to. As a result, we suppress this warning here to avoid client
// code stumbling over this. See https://github.com/nlohmann/json/issues/4087
// for a discussion.
JSON_HEDLEY_DIAGNOSTIC_PUSH
#if defined(__clang__)
JSON_HEDLEY_DIAGNOSTIC_PUSH
JSON_HEDLEY_PRAGMA(clang diagnostic ignored "-Wweak-vtables")
#endif
@@ -286,4 +286,6 @@ class other_error : public exception
} // namespace detail
NLOHMANN_JSON_NAMESPACE_END
JSON_HEDLEY_DIAGNOSTIC_POP
#if defined(__clang__)
JSON_HEDLEY_DIAGNOSTIC_POP
#endif
@@ -208,8 +208,8 @@ namespace std
{
// Fix: https://github.com/nlohmann/json/issues/1401
JSON_HEDLEY_DIAGNOSTIC_PUSH
#if defined(__clang__)
JSON_HEDLEY_DIAGNOSTIC_PUSH
JSON_HEDLEY_PRAGMA(clang diagnostic ignored "-Wmismatched-tags")
#endif
template<typename IteratorType>
@@ -224,7 +224,9 @@ class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
get<N>(std::declval <
::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
};
JSON_HEDLEY_DIAGNOSTIC_POP
#if defined(__clang__)
JSON_HEDLEY_DIAGNOSTIC_POP
#endif
} // namespace std
@@ -1849,8 +1849,8 @@ class binary_writer
void write_compact_float(const number_float_t n, detail::input_format_t format)
{
JSON_HEDLEY_DIAGNOSTIC_PUSH
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_PUSH
JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal")
#endif
if (!std::isfinite(n) || ((static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
@@ -1869,7 +1869,9 @@ class binary_writer
: get_msgpack_float_prefix(n));
write_number(n);
}
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_POP
#endif
}
public:
+6 -2
View File
@@ -3769,13 +3769,15 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
bool operator==(const_reference rhs) const noexcept
{
JSON_HEDLEY_DIAGNOSTIC_PUSH
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_PUSH
JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal")
#endif
const_reference lhs = *this;
JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_POP
#endif
}
/// @brief comparison: equal
@@ -3860,12 +3862,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
friend bool operator==(const_reference lhs, const_reference rhs) noexcept
{
JSON_HEDLEY_DIAGNOSTIC_PUSH
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_PUSH
JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal")
#endif
JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_POP
#endif
}
/// @brief comparison: equal
+20 -8
View File
@@ -4944,8 +4944,8 @@ NLOHMANN_JSON_NAMESPACE_END
// functions to. As a result, we suppress this warning here to avoid client
// code stumbling over this. See https://github.com/nlohmann/json/issues/4087
// for a discussion.
JSON_HEDLEY_DIAGNOSTIC_PUSH
#if defined(__clang__)
JSON_HEDLEY_DIAGNOSTIC_PUSH
JSON_HEDLEY_PRAGMA(clang diagnostic ignored "-Wweak-vtables")
#endif
@@ -5198,7 +5198,9 @@ class other_error : public exception
} // namespace detail
NLOHMANN_JSON_NAMESPACE_END
JSON_HEDLEY_DIAGNOSTIC_POP
#if defined(__clang__)
JSON_HEDLEY_DIAGNOSTIC_POP
#endif
// #include <nlohmann/detail/macro_scope.hpp>
@@ -6205,8 +6207,8 @@ namespace std
{
// Fix: https://github.com/nlohmann/json/issues/1401
JSON_HEDLEY_DIAGNOSTIC_PUSH
#if defined(__clang__)
JSON_HEDLEY_DIAGNOSTIC_PUSH
JSON_HEDLEY_PRAGMA(clang diagnostic ignored "-Wmismatched-tags")
#endif
template<typename IteratorType>
@@ -6221,7 +6223,9 @@ class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
get<N>(std::declval <
::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
};
JSON_HEDLEY_DIAGNOSTIC_POP
#if defined(__clang__)
JSON_HEDLEY_DIAGNOSTIC_POP
#endif
} // namespace std
@@ -18855,8 +18859,8 @@ class binary_writer
void write_compact_float(const number_float_t n, detail::input_format_t format)
{
JSON_HEDLEY_DIAGNOSTIC_PUSH
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_PUSH
JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal")
#endif
if (!std::isfinite(n) || ((static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
@@ -18875,7 +18879,9 @@ class binary_writer
: get_msgpack_float_prefix(n));
write_number(n);
}
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_POP
#endif
}
public:
@@ -20048,8 +20054,8 @@ char* to_chars(char* first, const char* last, FloatType value)
*first++ = '-';
}
JSON_HEDLEY_DIAGNOSTIC_PUSH
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_PUSH
JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal")
#endif
if (value == 0) // +-0
@@ -20060,7 +20066,9 @@ char* to_chars(char* first, const char* last, FloatType value)
*first++ = '0';
return first;
}
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_POP
#endif
JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
@@ -25191,13 +25199,15 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
bool operator==(const_reference rhs) const noexcept
{
JSON_HEDLEY_DIAGNOSTIC_PUSH
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_PUSH
JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal")
#endif
const_reference lhs = *this;
JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_POP
#endif
}
/// @brief comparison: equal
@@ -25282,12 +25292,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
friend bool operator==(const_reference lhs, const_reference rhs) noexcept
{
JSON_HEDLEY_DIAGNOSTIC_PUSH
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_PUSH
JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal")
#endif
JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
#ifdef __GNUC__
JSON_HEDLEY_DIAGNOSTIC_POP
#endif
}
/// @brief comparison: equal