Mark code that should be unreachable (#77)

Introduce a new [[noreturn]] unreachable() function that marks code as unreachable and throws a logic error if it is executed.
Fix build error due to Windows min/max macros.
Fix linker errors from referring to a non-constexpr extern from a constexpr.
Fix warnings about narrowing conversions.

Signed-off-by: Dan Rose <dan@digilabs.io>
This commit is contained in:
Dan Rose 2019-12-08 16:03:30 -06:00 committed by GitHub
parent 9b264c6480
commit c25f22e565
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 85 additions and 24 deletions

View file

@ -15,8 +15,8 @@
#ifndef RMW_CYCLONEDDS_CPP__EXCEPTION_HPP_
#define RMW_CYCLONEDDS_CPP__EXCEPTION_HPP_
#include <stdexcept>
#include <string>
#include <exception>
namespace rmw_cyclonedds_cpp
{
@ -35,6 +35,20 @@ protected:
std::string m_message;
};
/// Stub for code that should never be reachable by design.
/// If it is possible to reach the code due to bad data or other runtime conditions,
/// use a runtime_error instead
[[noreturn]] inline void unreachable()
{
#if defined(__has_builtin)
#if __has_builtin(__builtin_unreachable)
__builtin_unreachable();
#endif
#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
__builtin_unreachable();
#endif
throw std::logic_error("This code should be unreachable.");
}
} // namespace rmw_cyclonedds_cpp
#endif // RMW_CYCLONEDDS_CPP__EXCEPTION_HPP_