Bitcoin ABC  0.26.3
P2P Digital Currency
check.h
Go to the documentation of this file.
1 // Copyright (c) 2019 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)
9 #include <config/bitcoin-config.h>
10 #endif
11 
12 #include <attributes.h>
13 #include <tinyformat.h>
14 
15 #include <stdexcept>
16 
17 class NonFatalCheckError : public std::runtime_error {
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 " \
23  "issue here: %s\n", \
24  msg, file, line, func, report)
25 
27 template <typename T>
28 T &&inline_check_non_fatal(LIFETIMEBOUND T &&val, const char *file, int line,
29  const char *func, const char *assertion) {
30  if (!(val)) {
32  assertion, file, line, func, PACKAGE_BUGREPORT));
33  }
34 
35  return std::forward<T>(val);
36 }
37 
53 #define CHECK_NONFATAL(condition) \
54  inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
55 
56 #if defined(NDEBUG)
57 #error "Cannot compile without assertions!"
58 #endif
59 
61 void assertion_fail(const char *file, int line, const char *func,
62  const char *assertion);
63 
65 template <bool IS_ASSERT, typename T>
67  [[maybe_unused]] const char *file,
68  [[maybe_unused]] int line,
69  [[maybe_unused]] const char *func,
70  [[maybe_unused]] const char *assertion) {
71  if constexpr (IS_ASSERT
72 #ifdef ABORT_ON_FAILED_ASSUME
73  || true
74 #endif
75  ) {
76  if (!val) {
77  assertion_fail(file, line, func, assertion);
78  }
79  }
80  return std::forward<T>(val);
81 }
82 
84 #define Assert(val) \
85  inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
86 
97 #define Assume(val) \
98  inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val)
99 
106 #define NONFATAL_UNREACHABLE() \
107  throw NonFatalCheckError(format_internal_error( \
108  "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__, \
109  PACKAGE_BUGREPORT))
110 
111 #endif // BITCOIN_UTIL_CHECK_H
#define LIFETIMEBOUND
Definition: attributes.h:16
#define format_internal_error(msg, file, line, func, report)
Definition: check.h:21
T && inline_check_non_fatal(LIFETIMEBOUND T &&val, const char *file, int line, const char *func, const char *assertion)
Helper for CHECK_NONFATAL()
Definition: check.h:28
T && inline_assertion_check(LIFETIMEBOUND 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:66
void assertion_fail(const char *file, int line, const char *func, const char *assertion)
Helper for Assert()
Definition: check.cpp:9