Bitcoin Core  23.99.0
P2P Digital Currency
Classes | Macros | Functions
check.h File Reference
#include <tinyformat.h>
#include <stdexcept>
Include dependency graph for check.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  NonFatalCheckError
 

Macros

#define format_internal_error(msg, file, line, func, report)
 
#define CHECK_NONFATAL(condition)   inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
 Identity function. More...
 
#define Assert(val)   inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
 Identity function. More...
 
#define Assume(val)   inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val)
 Assume is the identity function. More...
 
#define NONFATAL_UNREACHABLE()
 NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code. More...
 

Functions

template<typename T >
T && inline_check_non_fatal (T &&val, const char *file, int line, const char *func, const char *assertion)
 Helper for CHECK_NONFATAL() More...
 
void assertion_fail (const char *file, int line, const char *func, const char *assertion)
 Helper for Assert() More...
 
template<bool IS_ASSERT, typename T >
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() More...
 

Macro Definition Documentation

◆ Assert

#define Assert (   val)    inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)

Identity function.

Abort if the value compares equal to zero

Definition at line 74 of file check.h.

◆ Assume

#define Assume (   val)    inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val)

Assume is the identity function.

  • Should be used to run non-fatal checks. In debug builds it behaves like Assert()/assert() to notify developers and testers about non-fatal errors. In production it doesn't warn or log anything.
  • For fatal errors, use Assert().
  • For non-fatal errors in interactive sessions (e.g. RPC or command line interfaces), CHECK_NONFATAL() might be more appropriate.

Definition at line 86 of file check.h.

◆ CHECK_NONFATAL

#define CHECK_NONFATAL (   condition)    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)

Identity function.

Throw a NonFatalCheckError when the condition evaluates to false

This should only be used

  • where the condition is assumed to be true, not for error handling or validating user input
  • where a failure to fulfill the condition is recoverable and does not abort the program

For example in RPC code, where it is undesirable to crash the whole program, this can be generally used to replace asserts or recoverable logic errors. A NonFatalCheckError in RPC code is caught and passed as a string to the RPC caller, which can then report the issue to the developers.

Definition at line 47 of file check.h.

◆ format_internal_error

#define format_internal_error (   msg,
  file,
  line,
  func,
  report 
)
Value:
strprintf("Internal bug detected: \"%s\"\n%s:%d (%s)\nPlease report this issue here: %s\n", \
msg, file, line, func, report)

Definition at line 21 of file check.h.

◆ NONFATAL_UNREACHABLE

#define NONFATAL_UNREACHABLE ( )
Value:
format_internal_error("Unreachable code reached (non-fatal)", \
__FILE__, __LINE__, __func__, PACKAGE_BUGREPORT))

NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code.

It throws a NonFatalCheckError. This is used to mark code that is not yet implemented or is not yet reachable.

Definition at line 92 of file check.h.

Function Documentation

◆ assertion_fail()

void assertion_fail ( const char *  file,
int  line,
const char *  func,
const char *  assertion 
)

Helper for Assert()

Definition at line 9 of file check.cpp.

Here is the caller graph for this function:

◆ inline_assertion_check()

template<bool IS_ASSERT, typename T >
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 at line 59 of file check.h.

Here is the call graph for this function:

◆ inline_check_non_fatal()

template<typename T >
T&& inline_check_non_fatal ( T &&  val,
const char *  file,
int  line,
const char *  func,
const char *  assertion 
)

Helper for CHECK_NONFATAL()

Definition at line 27 of file check.h.

PACKAGE_BUGREPORT
#define PACKAGE_BUGREPORT
Definition: bitcoin-config.h:353
format_internal_error
#define format_internal_error(msg, file, line, func, report)
Definition: check.h:21
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
NonFatalCheckError
Definition: check.h:16