From 700cc70bbabb7f75db9ce3a8c36f5e1e164da4aa Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Tue, 8 Dec 2020 16:29:17 +0800 Subject: [PATCH] crimson/os: use a constexpr function for sanity test since match_stage_t is an alias of uint_8. it is an unsigned type, so an instance of this type should be always greater or equal to 0. this silences warning like: stage_types.h:37:50: warning: comparison is always true due to limited range of data type [-Wtype-limits] 37 | static_assert(STAGE >= STAGE_BOTTOM && STAGE <= STAGE_TOP); | ~~~~~~^~~~~~~~~~~~ also, GCC complains if it is able to assure that the compare always returns true. so a function helps to silence it. this warning is meaningless per-se, as we are using static_assert() to perform sanity test at *compile-time*. Signed-off-by: Kefu Chai --- .../staged-fltree/stages/stage_types.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/crimson/os/seastore/onode_manager/staged-fltree/stages/stage_types.h b/src/crimson/os/seastore/onode_manager/staged-fltree/stages/stage_types.h index 2bd90d3e5ac..60a0fbec72f 100644 --- a/src/crimson/os/seastore/onode_manager/staged-fltree/stages/stage_types.h +++ b/src/crimson/os/seastore/onode_manager/staged-fltree/stages/stage_types.h @@ -19,7 +19,9 @@ constexpr match_stage_t STAGE_STRING = 1u; // nspace/oid constexpr match_stage_t STAGE_RIGHT = 0u; // snap/gen constexpr auto STAGE_TOP = STAGE_LEFT; constexpr auto STAGE_BOTTOM = STAGE_RIGHT; - +constexpr bool is_valid_stage(match_stage_t stage) { + return std::clamp(stage, STAGE_BOTTOM, STAGE_TOP) == stage; +} // TODO: replace by // using match_history_t = int8_t; // left_m, str_m, right_m @@ -34,7 +36,7 @@ constexpr auto STAGE_BOTTOM = STAGE_RIGHT; struct MatchHistory { template const std::optional& get() const { - static_assert(STAGE >= STAGE_BOTTOM && STAGE <= STAGE_TOP); + static_assert(is_valid_stage(STAGE)); if constexpr (STAGE == STAGE_RIGHT) { return right_match; } else if (STAGE == STAGE_STRING) { @@ -46,7 +48,7 @@ struct MatchHistory { const std::optional& get_by_stage(match_stage_t stage) const { - assert(stage >= STAGE_BOTTOM && stage <= STAGE_TOP); + assert(is_valid_stage(stage)); if (stage == STAGE_RIGHT) { return right_match; } else if (stage == STAGE_STRING) { @@ -61,7 +63,7 @@ struct MatchHistory { template void set(MatchKindCMP match) { - static_assert(STAGE >= STAGE_BOTTOM && STAGE <= STAGE_TOP); + static_assert(is_valid_stage(STAGE)); if constexpr (STAGE < STAGE_TOP) { assert(*get() == MatchKindCMP::EQ); } @@ -118,7 +120,7 @@ struct _check_GT_t { }; template const bool MatchHistory::is_GT() const { - static_assert(STAGE >= STAGE_BOTTOM && STAGE <= STAGE_TOP); + static_assert(is_valid_stage(STAGE)); if constexpr (STAGE < STAGE_TOP) { assert(get() == MatchKindCMP::EQ); } @@ -127,7 +129,7 @@ const bool MatchHistory::is_GT() const { template struct staged_position_t { - static_assert(STAGE > STAGE_BOTTOM && STAGE <= STAGE_TOP); + static_assert(is_valid_stage(STAGE)); using me_t = staged_position_t; using nxt_t = staged_position_t; bool is_end() const { -- 2.39.5