Skip to content

Commit

Permalink
address review comment
Browse files Browse the repository at this point in the history
Signed-off-by: Harinath Nampally <[email protected]>
  • Loading branch information
hnampally committed Jan 26, 2025
1 parent 32cbaee commit c2d0b8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
31 changes: 13 additions & 18 deletions include/nlohmann/detail/macro_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@

#include <nlohmann/detail/abi_macros.hpp>

NLOHMANN_JSON_NAMESPACE_BEGIN
namespace detail
{
template<typename T>
[[noreturn]] inline void throw_if_exceptions_enabled(T&& exception)
{
#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND) || defined(EXCEPTIONS)
throw std::forward<T>(exception);
#else
// Forward the exception (even if unused) and abort
std::forward<T>(exception);
std::abort();
#endif
}
} // namespace detail
NLOHMANN_JSON_NAMESPACE_END

// exclude unsupported compilers
#if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
Expand Down Expand Up @@ -272,6 +256,17 @@ NLOHMANN_JSON_NAMESPACE_END
e = ((it != std::end(m)) ? it : std::begin(m))->first; \
}

NLOHMANN_JSON_NAMESPACE_BEGIN
namespace detail
{
template<typename T>
[[noreturn]] inline void json_throw_from_serialize_macro(T&& exception)
{
JSON_THROW(std::forward<T>(exception));
static_cast<void>(exception);

Check notice on line 266 in include/nlohmann/detail/macro_scope.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

include/nlohmann/detail/macro_scope.hpp#L266

Statements following noreturn function 'abort()' will never be executed.
}
} // namespace detail
NLOHMANN_JSON_NAMESPACE_END
/*!
@brief macro to briefly define a mapping between an enum and JSON
@def NLOHMANN_JSON_SERIALIZE_ENUM_STRICT
Expand All @@ -292,7 +287,7 @@ NLOHMANN_JSON_NAMESPACE_END
}); \
if (it == std::end(m)) { \
auto value = static_cast<typename std::underlying_type<ENUM_TYPE>::type>(e); \
nlohmann::detail::throw_if_exceptions_enabled(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't serialize - enum value ", std::to_string(value), " out of range"), &j)); \
nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't serialize - enum value ", std::to_string(value), " out of range"), &j)); \
} \
j = it->second; \
} \
Expand All @@ -309,7 +304,7 @@ NLOHMANN_JSON_NAMESPACE_END
return ej_pair.second == j; \
}); \
if (it == std::end(m)) \
nlohmann::detail::throw_if_exceptions_enabled(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't deserialize - invalid json value : ", j.dump()), &j)); \
nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't deserialize - invalid json value : ", j.dump()), &j)); \
e = it->first; \
}

Expand Down
31 changes: 13 additions & 18 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2383,22 +2383,6 @@ JSON_HEDLEY_DIAGNOSTIC_POP
// #include <nlohmann/detail/abi_macros.hpp>


NLOHMANN_JSON_NAMESPACE_BEGIN
namespace detail
{
template<typename T>
[[noreturn]] inline void throw_if_exceptions_enabled(T&& exception)
{
#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND) || defined(EXCEPTIONS)
throw std::forward<T>(exception);
#else
// Forward the exception (even if unused) and abort
std::forward<T>(exception);
std::abort();
#endif
}
} // namespace detail
NLOHMANN_JSON_NAMESPACE_END

// exclude unsupported compilers
#if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
Expand Down Expand Up @@ -2638,6 +2622,17 @@ NLOHMANN_JSON_NAMESPACE_END
e = ((it != std::end(m)) ? it : std::begin(m))->first; \
}

NLOHMANN_JSON_NAMESPACE_BEGIN
namespace detail
{
template<typename T>
[[noreturn]] inline void json_throw_from_serialize_macro(T&& exception)
{
JSON_THROW(std::forward<T>(exception));
static_cast<void>(exception);
}
} // namespace detail
NLOHMANN_JSON_NAMESPACE_END
/*!
@brief macro to briefly define a mapping between an enum and JSON
@def NLOHMANN_JSON_SERIALIZE_ENUM_STRICT
Expand All @@ -2658,7 +2653,7 @@ NLOHMANN_JSON_NAMESPACE_END
}); \
if (it == std::end(m)) { \
auto value = static_cast<typename std::underlying_type<ENUM_TYPE>::type>(e); \
nlohmann::detail::throw_if_exceptions_enabled(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't serialize - enum value ", std::to_string(value), " out of range"), &j)); \
nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't serialize - enum value ", std::to_string(value), " out of range"), &j)); \
} \
j = it->second; \
} \
Expand All @@ -2675,7 +2670,7 @@ NLOHMANN_JSON_NAMESPACE_END
return ej_pair.second == j; \
}); \
if (it == std::end(m)) \
nlohmann::detail::throw_if_exceptions_enabled(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't deserialize - invalid json value : ", j.dump()), &j)); \
nlohmann::detail::json_throw_from_serialize_macro(nlohmann::detail::type_error::create(302, nlohmann::detail::concat("can't deserialize - invalid json value : ", j.dump()), &j)); \
e = it->first; \
}

Expand Down

0 comments on commit c2d0b8b

Please sign in to comment.