Skip to content

Commit 59258d2

Browse files
authored
Replace instances of std::abort with a custom unreachable (#841)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent f6f1638 commit 59258d2

File tree

6 files changed

+32
-13
lines changed

6 files changed

+32
-13
lines changed

src/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME runtime
1010
input_stream.cc
1111
output_stream.cc
1212
varint.h
13+
unreachable.h
1314

1415
loader.cc
1516
loader_v1_any.h

src/runtime/decoder_any.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <sourcemeta/jsonbinpack/runtime_plan_wrap.h>
55

6+
#include "unreachable.h"
7+
68
#include <cassert> // assert
79
#include <cstdint> // std::uint8_t, std::uint16_t, std::uint64_t
810

@@ -84,8 +86,7 @@ auto Decoder::ANY_PACKED_TYPE_TAG_BYTE_PREFIX(
8486
}
8587

8688
// We should never get here. If so, it is definitely a bug
87-
assert(false);
88-
std::abort();
89+
unreachable();
8990
} else {
9091
switch (type) {
9192
case TYPE_POSITIVE_INTEGER_BYTE:
@@ -142,8 +143,7 @@ auto Decoder::ANY_PACKED_TYPE_TAG_BYTE_PREFIX(
142143
}
143144

144145
// We should never get here. If so, it is definitely a bug
145-
assert(false);
146-
std::abort();
146+
unreachable();
147147
}
148148
}
149149

src/runtime/decoder_common.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include <sourcemeta/jsonbinpack/runtime_decoder.h>
22

3+
#include "unreachable.h"
4+
35
#include <cassert> // assert
4-
#include <cstdlib> // std::abort
56
#include <variant> // std::get
67

78
namespace sourcemeta::jsonbinpack {
@@ -39,8 +40,7 @@ auto Decoder::read(const Plan &encoding) -> sourcemeta::jsontoolkit::JSON {
3940
}
4041

4142
// We should never get here. If so, it is definitely a bug
42-
assert(false);
43-
std::abort();
43+
unreachable();
4444
}
4545

4646
} // namespace sourcemeta::jsonbinpack

src/runtime/encoder_any.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <sourcemeta/jsonbinpack/runtime_encoder.h>
33
#include <sourcemeta/jsonbinpack/runtime_plan_wrap.h>
44

5+
#include "unreachable.h"
6+
57
#include <algorithm> // std::find_if
68
#include <cassert> // assert
79
#include <cstdint> // std::uint8_t, std::int64_t, std::uint64_t
@@ -177,9 +179,8 @@ auto Encoder::ANY_PACKED_TYPE_TAG_BYTE_PREFIX(
177179
document,
178180
{size, wrap(std::move(key_encoding)), wrap(std::move(value_encoding))});
179181
} else {
180-
// We should never get here.
181-
assert(false);
182-
std::abort();
182+
// We should never get here
183+
unreachable();
183184
}
184185
}
185186

src/runtime/encoder_common.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include <sourcemeta/jsonbinpack/runtime_encoder.h>
22

3+
#include "unreachable.h"
4+
35
#include <cassert> // assert
4-
#include <cstdlib> // std::abort
56
#include <variant> // std::get
67

78
namespace sourcemeta::jsonbinpack {
@@ -41,8 +42,7 @@ auto Encoder::write(const sourcemeta::jsontoolkit::JSON &document,
4142
}
4243

4344
// We should never get here. If so, it is definitely a bug
44-
assert(false);
45-
std::abort();
45+
unreachable();
4646
}
4747

4848
} // namespace sourcemeta::jsonbinpack

src/runtime/unreachable.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef SOURCEMETA_JSONBINPACK_RUNTIME_UNREACHABLE_H_
2+
#define SOURCEMETA_JSONBINPACK_RUNTIME_UNREACHABLE_H_
3+
4+
#include <cassert> // assert
5+
6+
// Until we are on C++23 and can use std::unreachable
7+
// See https://en.cppreference.com/w/cpp/utility/unreachable
8+
[[noreturn]] inline void unreachable() {
9+
assert(false);
10+
#if defined(_MSC_VER) && !defined(__clang__)
11+
__assume(false);
12+
#else
13+
__builtin_unreachable();
14+
#endif
15+
}
16+
17+
#endif

0 commit comments

Comments
 (0)