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
7 changes: 3 additions & 4 deletions .github/workflows/ci_coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ jobs:
--filter ${GITHUB_WORKSPACE}/src \
--exclude ${GITHUB_WORKSPACE}/include/hibf/contrib \
--exclude ${GITHUB_WORKSPACE}/test/include/hibf/test/iterator_test_template.hpp \
--exclude-lines-by-pattern '^\s*$' \
--exclude-lines-by-pattern '^\s*};$' \
--exclude-unreachable-branches \
--exclude-throw-branches \
--exclude-lines-by-pattern '^\s*}|^\s*};|^\s*HIBF_UNREACHABLE;' \
--exclude-noncode-lines \
--exclude-throw-branches \
--exclude-unreachable-branches \
--merge-mode-functions separate \
-j \
--cobertura \
Expand Down
18 changes: 18 additions & 0 deletions include/hibf/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@
# error "This is not a C++ compiler."
#endif

/*!\brief Macro to mark unreachable code paths.
* \details
* In debug mode, it triggers an assertion failure.
* In release mode, it calls `std::unreachable`.
* ### Example
* \include test/snippet/platform_unreachable.cpp
*/
#ifndef HIBF_UNREACHABLE
// The do { ... } while (0) is a common pattern to enforce the semicolon after the macro.
// clang-format off
# ifndef NDEBUG
# define HIBF_UNREACHABLE do { assert(false); } while (0) // GCOVR_EXCL_LINE
# else
# define HIBF_UNREACHABLE do { std::unreachable(); } while (0)
# endif
#endif
// clang-format on

// ============================================================================
// Dependencies
// ============================================================================
Expand Down
9 changes: 2 additions & 7 deletions src/interleaved_bloom_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,11 @@ interleaved_bloom_filter::membership_agent_type::bulk_contains(size_t const valu
size_t const bin_words_ = ibf_ptr->bin_words;
size_t const hash_funs_ = ibf_ptr->hash_funs;

#ifndef NDEBUG
assert(bin_words_ != 0u);
assert(hash_funs_ != 0u);
#else
// Removes case for bin_words_ == 0u. The same statment inside the switch-case wouldn't have that effect.
if (bin_words_ == 0u)
__builtin_unreachable();
HIBF_UNREACHABLE;
if (hash_funs_ == 0u)
__builtin_unreachable();
#endif
HIBF_UNREACHABLE;

for (size_t i = 0; i < hash_funs_; ++i)
bloom_filter_indices[i] = ibf_ptr->hash_and_fit(value, ibf_ptr->hash_seeds[i]) / 64u;
Expand Down
4 changes: 2 additions & 2 deletions test/coverage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ project (hibf_test_coverage CXX)
# Add a custom build type: Coverage

set (CMAKE_CXX_FLAGS_COVERAGE
"${CMAKE_CXX_FLAGS_DEBUG} --coverage -fprofile-arcs -ftest-coverage ${HIBF_FPROFILE_ABS_PATH}"
"${CMAKE_CXX_FLAGS_DEBUG} --coverage -fprofile-arcs -ftest-coverage -fprofile-abs-path"
CACHE STRING "Flags used by the C++ compiler during coverage builds." FORCE)
set (CMAKE_C_FLAGS_COVERAGE
"${CMAKE_C_FLAGS_DEBUG} --coverage -fprofile-arcs -ftest-coverage ${HIBF_FPROFILE_ABS_PATH}"
"${CMAKE_C_FLAGS_DEBUG} --coverage -fprofile-arcs -ftest-coverage -fprofile-abs-path"
CACHE STRING "Flags used by the C compiler during coverage builds." FORCE)
set (CMAKE_EXE_LINKER_FLAGS_COVERAGE
"${CMAKE_EXE_LINKER_FLAGS_DEBUG} -Wl,-lgcov"
Expand Down
22 changes: 22 additions & 0 deletions test/snippet/platform_unreachable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2006-2025, Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025, Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: CC0-1.0

#include <utility> // for unreachable

#include <hibf/platform.hpp> // for HIBF_UNREACHABLE

int foo(int const i)
{
// The compiler will not generate the default branch.
// Note that an input of any `i` other than `0` and `1` is undefined behavior!
switch (i)
{
case 0:
return -5;
case 1:
return 3;
default:
HIBF_UNREACHABLE; // HIBF_UNREACHABLE must be followed by a semicolon.
}
}