Bitcoin Core  23.99.0
P2P Digital Currency
check.h
Go to the documentation of this file.
1 // Copyright (c) 2019-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_UTIL_CHECK_H
6 #define BITCOIN_UTIL_CHECK_H
7 
8 #if defined(HAVE_CONFIG_H)
10 #endif
11 
12 #include <tinyformat.h>
13 
14 #include <stdexcept>
15 
16 class NonFatalCheckError : public std::runtime_error
17 {
18  using std::runtime_error::runtime_error;
19 };
20 
21 #define format_internal_error(msg, file, line, func, report) \
22  strprintf("Internal bug detected: \"%s\"\n%s:%d (%s)\nPlease report this issue here: %s\n", \
23  msg, file, line, func, report)
24 
26 template <typename T>
27 T&& inline_check_non_fatal(T&& val, const char* file, int line, const char* func, const char* assertion)
28 {
29  if (!(val)) {
30  throw NonFatalCheckError(
31  format_internal_error(assertion, file, line, func, PACKAGE_BUGREPORT));
32  }
33  return std::forward<T>(val);
34 }
35 
47 #define CHECK_NONFATAL(condition) \
48  inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
49 
50 #if defined(NDEBUG)
51 #error "Cannot compile without assertions!"
52 #endif
53 
55 void assertion_fail(const char* file, int line, const char* func, const char* assertion);
56 
58 template <bool IS_ASSERT, typename T>
59 T&& inline_assertion_check(T&& val, [[maybe_unused]] const char* file, [[maybe_unused]] int line, [[maybe_unused]] const char* func, [[maybe_unused]] const char* assertion)
60 {
61  if constexpr (IS_ASSERT
62 #ifdef ABORT_ON_FAILED_ASSUME
63  || true
64 #endif
65  ) {
66  if (!val) {
67  assertion_fail(file, line, func, assertion);
68  }
69  }
70  return std::forward<T>(val);
71 }
72 
74 #define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
75 
86 #define Assume(val) inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val)
87 
92 #define NONFATAL_UNREACHABLE() \
93  throw NonFatalCheckError( \
94  format_internal_error("Unreachable code reached (non-fatal)", \
95  __FILE__, __LINE__, __func__, PACKAGE_BUGREPORT))
96 
97 #endif // BITCOIN_UTIL_CHECK_H
inline_check_non_fatal
T && inline_check_non_fatal(T &&val, const char *file, int line, const char *func, const char *assertion)
Helper for CHECK_NONFATAL()
Definition: check.h:27
PACKAGE_BUGREPORT
#define PACKAGE_BUGREPORT
Definition: bitcoin-config.h:353
assertion_fail
void assertion_fail(const char *file, int line, const char *func, const char *assertion)
Helper for Assert()
Definition: check.cpp:9
bitcoin-config.h
tinyformat.h
format_internal_error
#define format_internal_error(msg, file, line, func, report)
Definition: check.h:21
inline_assertion_check
T && inline_assertion_check(T &&val, [[maybe_unused]] const char *file, [[maybe_unused]] int line, [[maybe_unused]] const char *func, [[maybe_unused]] const char *assertion)
Helper for Assert()/Assume()
Definition: check.h:59
T
#define T(expected, seed, data)
NonFatalCheckError
Definition: check.h:16