Bitcoin Core  25.99.0
P2P Digital Currency
check.h
Go to the documentation of this file.
1 // Copyright (c) 2019-2022 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 #include <attributes.h>
9 
10 #include <stdexcept>
11 #include <string>
12 #include <string_view>
13 #include <utility>
14 
15 std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func);
16 
17 class NonFatalCheckError : public std::runtime_error
18 {
19 public:
20  NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func);
21 };
22 
23 #define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), __FILE__, __LINE__, __func__)
24 
26 template <typename T>
27 T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, const char* func, const char* assertion)
28 {
29  if (!val) {
30  throw NonFatalCheckError{assertion, file, line, func};
31  }
32  return std::forward<T>(val);
33 }
34 
46 #define CHECK_NONFATAL(condition) \
47  inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
48 
49 #if defined(NDEBUG)
50 #error "Cannot compile without assertions!"
51 #endif
52 
54 void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion);
55 
57 template <bool IS_ASSERT, typename T>
58 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)
59 {
60  if constexpr (IS_ASSERT
61 #ifdef ABORT_ON_FAILED_ASSUME
62  || true
63 #endif
64  ) {
65  if (!val) {
66  assertion_fail(file, line, func, assertion);
67  }
68  }
69  return std::forward<T>(val);
70 }
71 
73 #define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
74 
85 #define Assume(val) inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val)
86 
90 #define NONFATAL_UNREACHABLE() \
91  throw NonFatalCheckError( \
92  "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__)
93 
94 #endif // BITCOIN_UTIL_CHECK_H
#define LIFETIMEBOUND
Definition: attributes.h:16
std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func)
Definition: check.cpp:19
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:27
void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion)
Helper for Assert()
Definition: check.cpp:32
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:58
NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func)
Definition: check.cpp:27
#define T(expected, seed, data)