File tree Expand file tree Collapse file tree 4 files changed +43
-7
lines changed
Expand file tree Collapse file tree 4 files changed +43
-7
lines changed Original file line number Diff line number Diff line change 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 \
Original file line number Diff line number Diff line change 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// ============================================================================
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments