Skip to content

Commit 3c3b183

Browse files
committed
[MISC] Add HIBF_UNREACHABLE
1 parent 6e90be6 commit 3c3b183

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

.github/workflows/ci_coverage.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ jobs:
7878
--exclude ${GITHUB_WORKSPACE}/test/include/hibf/test/iterator_test_template.hpp \
7979
--exclude-lines-by-pattern '^\s*$' \
8080
--exclude-lines-by-pattern '^\s*};$' \
81+
--exclude-lines-by-pattern '^\s*HIBF_UNREACHABLE;' \
8182
--exclude-unreachable-branches \
8283
--exclude-throw-branches \
8384
--exclude-noncode-lines \

include/hibf/platform.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@
101101
# error "This is not a C++ compiler."
102102
#endif
103103

104+
/*!\brief Macro to mark unreachable code paths.
105+
* \details
106+
* In debug mode, it triggers an assertion failure.
107+
* In release mode, it calls `std::unreachable`.
108+
* ### Example
109+
* \include test/snippet/platform_unreachable.cpp
110+
*/
111+
#ifndef HIBF_UNREACHABLE
112+
// The do { ... } while (0) is a common pattern to enforce the semicolon after the macro.
113+
// clang-format off
114+
# ifndef NDEBUG
115+
# define HIBF_UNREACHABLE do { assert(false); } while (0) // GCOVR_EXCL_LINE
116+
# else
117+
# define HIBF_UNREACHABLE do { std::unreachable(); } while (0)
118+
# endif
119+
#endif
120+
// clang-format on
121+
104122
// ============================================================================
105123
// Dependencies
106124
// ============================================================================

src/interleaved_bloom_filter.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,11 @@ interleaved_bloom_filter::membership_agent_type::bulk_contains(size_t const valu
210210
size_t const bin_words_ = ibf_ptr->bin_words;
211211
size_t const hash_funs_ = ibf_ptr->hash_funs;
212212

213-
#ifndef NDEBUG
214-
assert(bin_words_ != 0u);
215-
assert(hash_funs_ != 0u);
216-
#else
217213
// Removes case for bin_words_ == 0u. The same statment inside the switch-case wouldn't have that effect.
218214
if (bin_words_ == 0u)
219-
__builtin_unreachable();
215+
HIBF_UNREACHABLE;
220216
if (hash_funs_ == 0u)
221-
__builtin_unreachable();
222-
#endif
217+
HIBF_UNREACHABLE;
223218

224219
for (size_t i = 0; i < hash_funs_; ++i)
225220
bloom_filter_indices[i] = ibf_ptr->hash_and_fit(value, ibf_ptr->hash_seeds[i]) / 64u;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-FileCopyrightText: 2006-2025, Knut Reinert & Freie Universität Berlin
2+
// SPDX-FileCopyrightText: 2016-2025, Knut Reinert & MPI für molekulare Genetik
3+
// SPDX-License-Identifier: CC0-1.0
4+
5+
#include <utility> // for unreachable
6+
7+
#include <hibf/platform.hpp> // for HIBF_UNREACHABLE
8+
9+
int foo(int const i)
10+
{
11+
// The compiler will not generate the default branch.
12+
// Note that an input of any `i` other than `0` and `1` is undefined behavior!
13+
switch (i)
14+
{
15+
case 0:
16+
return -5;
17+
case 1:
18+
return 3;
19+
default:
20+
HIBF_UNREACHABLE; // HIBF_UNREACHABLE must be followed by a semicolon.
21+
}
22+
}

0 commit comments

Comments
 (0)