]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
crimson/os: use a constexpr function for sanity test
authorKefu Chai <kchai@redhat.com>
Tue, 8 Dec 2020 08:29:17 +0000 (16:29 +0800)
committerKefu Chai <kchai@redhat.com>
Tue, 8 Dec 2020 08:55:13 +0000 (16:55 +0800)
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 <kchai@redhat.com>
src/crimson/os/seastore/onode_manager/staged-fltree/stages/stage_types.h

index 2bd90d3e5ac27d95fab636414e14971e499d9a5f..60a0fbec72f462b7e11aa94c0c57a943643e7678 100644 (file)
@@ -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 <match_stage_t STAGE>
   const std::optional<MatchKindCMP>& 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<MatchKindCMP>&
   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 <match_stage_t STAGE>
   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<STAGE + 1>() == MatchKindCMP::EQ);
     }
@@ -118,7 +120,7 @@ struct _check_GT_t<STAGE_RIGHT> {
 };
 template <match_stage_t STAGE>
 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<STAGE + 1>() == MatchKindCMP::EQ);
   }
@@ -127,7 +129,7 @@ const bool MatchHistory::is_GT() const {
 
 template <match_stage_t STAGE>
 struct staged_position_t {
-  static_assert(STAGE > STAGE_BOTTOM && STAGE <= STAGE_TOP);
+  static_assert(is_valid_stage(STAGE));
   using me_t = staged_position_t<STAGE>;
   using nxt_t = staged_position_t<STAGE - 1>;
   bool is_end() const {