Bitcoin ABC 0.33.10
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
17class 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
27template <typename T>
28T &&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
38#if defined(NDEBUG)
39#error "Cannot compile without assertions!"
40#endif
41
43void assertion_fail(const char *file, int line, const char *func,
44 const char *assertion);
45
47template <bool IS_ASSERT, typename T>
49 [[maybe_unused]] const char *file,
50 [[maybe_unused]] int line,
51 [[maybe_unused]] const char *func,
52 [[maybe_unused]] const char *assertion) {
53 if constexpr (IS_ASSERT
54#ifdef ABORT_ON_FAILED_ASSUME
55 || true
56#endif
57 ) {
58 if (!val) {
59 assertion_fail(file, line, func, assertion);
60 }
61 }
62 return std::forward<T>(val);
63}
64
65// All macros may use __func__ inside a lambda, so put them under nolint.
66// NOLINTBEGIN(bugprone-lambda-function-name)
67
83#define CHECK_NONFATAL(condition) \
84 inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
85
87#define Assert(val) \
88 inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
89
100#define Assume(val) \
101 inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val)
102
109#define NONFATAL_UNREACHABLE() \
110 throw NonFatalCheckError(format_internal_error( \
111 "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__, \
112 PACKAGE_BUGREPORT))
113
114// NOLINTEND(bugprone-lambda-function-name)
115
116#endif // BITCOIN_UTIL_CHECK_H
#define LIFETIMEBOUND
Definition: attributes.h:16
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
#define format_internal_error(msg, file, line, func, report)
Definition: check.h:21
T && inline_assertion_check(LIFETIMEBOUND T &&val, const char *file, int line, const char *func, const char *assertion)
Helper for Assert()/Assume()
Definition: check.h:48
void assertion_fail(const char *file, int line, const char *func, const char *assertion)
Helper for Assert()
Definition: check.cpp:9