Introduce NDN_CXX_UNREACHABLE macro
Change-Id: I5549c436cfb8fb30da94cbbdbc221c622fca24c2
diff --git a/ndn-cxx/delegation-list.cpp b/ndn-cxx/delegation-list.cpp
index d999f92..47cf021 100644
--- a/ndn-cxx/delegation-list.cpp
+++ b/ndn-cxx/delegation-list.cpp
@@ -185,8 +185,8 @@
}
return false;
}
- BOOST_ASSERT_MSG(false, "Unknown onConflict");
- return false;
+
+ NDN_THROW(std::invalid_argument("Unknown InsertConflictResolution"));
}
void
diff --git a/ndn-cxx/detail/common.hpp b/ndn-cxx/detail/common.hpp
index b63d709..e80af57 100644
--- a/ndn-cxx/detail/common.hpp
+++ b/ndn-cxx/detail/common.hpp
@@ -20,8 +20,8 @@
*/
/** \file
- * \brief import common constructs for ndn-cxx library internal use
- * \warning This file is an implementation detail of ndn-cxx library.
+ * \brief Common includes and macros used throughout the library.
+ * \warning This file is an implementation detail of the ndn-cxx library.
* Aliases imported in this file MUST NOT be used outside of ndn-cxx.
*/
@@ -30,6 +30,11 @@
#include "ndn-cxx/detail/config.hpp"
+// require C++14
+#if __cplusplus < 201402L
+#error "ndn-cxx applications must be compiled using the C++14 standard (-std=c++14)"
+#endif
+
// ndn-cxx specific macros declared in this and other headers must have NDN_CXX_ prefix
// to avoid conflicts with other projects that include ndn-cxx headers.
#ifdef NDN_CXX_HAVE_TESTS
@@ -44,11 +49,6 @@
#define NDN_CXX_PROTECTED_WITH_TESTS_ELSE_PRIVATE private
#endif
-// require C++14
-#if __cplusplus < 201402L
-#error "ndn-cxx applications must be compiled using the C++14 standard (-std=c++14)"
-#endif
-
#include <algorithm>
#include <cstddef>
#include <cstdint>
diff --git a/ndn-cxx/encoding/tlv.hpp b/ndn-cxx/encoding/tlv.hpp
index 06a431a..5706e52 100644
--- a/ndn-cxx/encoding/tlv.hpp
+++ b/ndn-cxx/encoding/tlv.hpp
@@ -352,8 +352,7 @@
return true;
}
default: {
- BOOST_ASSERT(false);
- return false;
+ NDN_CXX_UNREACHABLE;
}
}
}
diff --git a/ndn-cxx/impl/name-component-types.hpp b/ndn-cxx/impl/name-component-types.hpp
index 63fa4f2..2a8563c 100644
--- a/ndn-cxx/impl/name-component-types.hpp
+++ b/ndn-cxx/impl/name-component-types.hpp
@@ -91,10 +91,9 @@
* \pre getAltUriPrefix() != nullptr
*/
virtual Component
- parseAltUriValue(const std::string& input) const
+ parseAltUriValue(const std::string&) const
{
- BOOST_ASSERT(false);
- return Component();
+ NDN_CXX_UNREACHABLE;
}
/** \brief Write URI representation of \p comp to \p os.
diff --git a/ndn-cxx/impl/pending-interest.hpp b/ndn-cxx/impl/pending-interest.hpp
index 15cedbc..a766056 100644
--- a/ndn-cxx/impl/pending-interest.hpp
+++ b/ndn-cxx/impl/pending-interest.hpp
@@ -56,8 +56,7 @@
case PendingInterestOrigin::FORWARDER:
return os << "forwarder";
}
- BOOST_ASSERT(false);
- return os;
+ NDN_CXX_UNREACHABLE;
}
/**
diff --git a/ndn-cxx/key-locator.cpp b/ndn-cxx/key-locator.cpp
index a482e19..2084dc1 100644
--- a/ndn-cxx/key-locator.cpp
+++ b/ndn-cxx/key-locator.cpp
@@ -127,7 +127,7 @@
case 3:
return get<uint32_t>(m_locator);
default:
- BOOST_ASSERT(false);
+ NDN_CXX_UNREACHABLE;
}
}
diff --git a/ndn-cxx/util/backports.hpp b/ndn-cxx/util/backports.hpp
index adb8d03..afe0036 100644
--- a/ndn-cxx/util/backports.hpp
+++ b/ndn-cxx/util/backports.hpp
@@ -24,6 +24,10 @@
#include "ndn-cxx/detail/common.hpp"
+#include <boost/predef/compiler/clang.h>
+#include <boost/predef/compiler/gcc.h>
+#include <boost/predef/compiler/visualc.h>
+
#ifdef __has_cpp_attribute
# define NDN_CXX_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
#else
@@ -42,12 +46,23 @@
# define NDN_CXX_FALLTHROUGH [[clang::fallthrough]]
#elif NDN_CXX_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
# define NDN_CXX_FALLTHROUGH [[gnu::fallthrough]]
-#elif defined(__GNUC__) && (__GNUC__ >= 7)
+#elif BOOST_COMP_GNUC >= BOOST_VERSION_NUMBER(7,0,0)
# define NDN_CXX_FALLTHROUGH __attribute__((fallthrough))
#else
# define NDN_CXX_FALLTHROUGH ((void)0)
#endif
+#ifndef NDEBUG
+# define NDN_CXX_UNREACHABLE BOOST_ASSERT(false)
+#elif BOOST_COMP_GNUC || BOOST_COMP_CLANG
+# define NDN_CXX_UNREACHABLE __builtin_unreachable()
+#elif BOOST_COMP_MSVC
+# define NDN_CXX_UNREACHABLE __assume(0)
+#else
+# include <cstdlib>
+# define NDN_CXX_UNREACHABLE std::abort()
+#endif
+
#include "ndn-cxx/util/backports-ostream-joiner.hpp"
#include "ndn-cxx/util/nonstd/any.hpp"
#include "ndn-cxx/util/nonstd/optional.hpp"