Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ The options are named like the macros.

<pre><code class="language-cpp">#define <a href='doc_debug_assert.md#debug_assert.hpp'>DEBUG_ASSERT_MARK_UNREACHABLE</a>

#define <a href='doc_debug_assert.md#debug_assert.hpp'>DEBUG_ASSERT_PURE_FUNCTION</a>

#define <a href='doc_debug_assert.md#debug_assert.hpp'>DEBUG_ASSERT_ASSUME</a>(Expr)

#define <a href='doc_debug_assert.md#debug_assert.hpp'>DEBUG_ASSERT_FORCE_INLINE</a>
Expand Down
22 changes: 20 additions & 2 deletions debug_assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@
#endif
#endif

#ifndef DEBUG_ASSERT_PURE_FUNCTION
#ifdef __GNUC__
#define DEBUG_ASSERT_PURE_FUNCTION __attribute__((pure))
#else
/// Hint to the compiler that a function is pure.
/// Define it yourself prior to including the header to override it.
#define DEBUG_ASSERT_PURE_FUNCTION
#endif
#endif

// checking for clang must come first because clang also defines __GNUC__.
#if !defined(DEBUG_ASSERT_ASSUME) && defined(__clang__)
// __has_builtin may not work in other compilers.
#if __has_builtin(__builtin_assume)
#define DEBUG_ASSERT_ASSUME(Expr) __builtin_assume(Expr)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use defined(__clang__) && __has_builtin... there and then just have a single #elif defined(__GNUC__)? Avoids the repetition.

#endif
#endif

#ifndef DEBUG_ASSERT_ASSUME
#ifdef __GNUC__
#define DEBUG_ASSERT_ASSUME(Expr) \
Expand Down Expand Up @@ -309,7 +327,7 @@ namespace debug_assert
/// This should not be necessary, the regular version is optimized away
/// completely.
#define DEBUG_ASSERT(Expr, ...) \
debug_assert::detail::do_assert([&]() noexcept { return Expr; }, \
debug_assert::detail::do_assert([&]() DEBUG_ASSERT_PURE_FUNCTION noexcept { return Expr; }, \
DEBUG_ASSERT_CUR_SOURCE_LOCATION, #Expr, __VA_ARGS__)

/// Marks a branch as unreachable.
Expand All @@ -333,7 +351,7 @@ namespace debug_assert
/// This should not be necessary, the regular version is optimized away
/// completely.
#define DEBUG_UNREACHABLE(...) \
debug_assert::detail::do_assert([&]() noexcept { return false; }, \
debug_assert::detail::do_assert([&]() DEBUG_ASSERT_PURE_FUNCTION noexcept { return false; }, \
DEBUG_ASSERT_CUR_SOURCE_LOCATION, "", __VA_ARGS__)
#else
#define DEBUG_ASSERT(Expr, ...) DEBUG_ASSERT_ASSUME(Expr)
Expand Down