diff options
author | Aurèle Oulès <aurele@oules.com> | 2022-04-09 02:14:02 +0200 |
---|---|---|
committer | Aurèle Oulès <aurele@oules.com> | 2022-04-16 15:07:41 +0200 |
commit | ee02c8bd9aedad8cfd3c2618035fe275da025fb9 (patch) | |
tree | 72fe1415dda3b7f515243c025c5bc4dab761650a /src/util/check.h | |
parent | e0680bbce8b89ad5de9d1b9f818b76df68d72696 (diff) | |
download | bitcoin-ee02c8bd9aedad8cfd3c2618035fe275da025fb9.tar.xz |
util/check: Add CHECK_NONFATAL identity function, NONFATAL_UNREACHABLE AND UNREACHABLE macros
Diffstat (limited to 'src/util/check.h')
-rw-r--r-- | src/util/check.h | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/src/util/check.h b/src/util/check.h index 4ee65c8d34..91d62e262d 100644 --- a/src/util/check.h +++ b/src/util/check.h @@ -18,8 +18,24 @@ class NonFatalCheckError : public std::runtime_error using std::runtime_error::runtime_error; }; +#define format_internal_error(msg, file, line, func, report) \ + strprintf("Internal bug detected: \"%s\"\n%s:%d (%s)\nPlease report this issue here: %s\n", \ + msg, file, line, func, report) + +/** Helper for CHECK_NONFATAL() */ +template <typename T> +T&& inline_check_non_fatal(T&& val, const char* file, int line, const char* func, const char* assertion) +{ + if (!(val)) { + throw NonFatalCheckError( + format_internal_error(assertion, file, line, func, PACKAGE_BUGREPORT)); + } + + return std::forward<T>(val); +} + /** - * Throw a NonFatalCheckError when the condition evaluates to false + * 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 @@ -29,18 +45,8 @@ class NonFatalCheckError : public std::runtime_error * 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. */ -#define CHECK_NONFATAL(condition) \ - do { \ - if (!(condition)) { \ - throw NonFatalCheckError( \ - strprintf("Internal bug detected: '%s'\n" \ - "%s:%d (%s)\n" \ - "You may report this issue here: %s\n", \ - (#condition), \ - __FILE__, __LINE__, __func__, \ - PACKAGE_BUGREPORT)); \ - } \ - } while (false) +#define CHECK_NONFATAL(condition) \ + inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition) #if defined(NDEBUG) #error "Cannot compile without assertions!" @@ -80,4 +86,13 @@ T&& inline_assertion_check(T&& val, [[maybe_unused]] const char* file, [[maybe_u */ #define Assume(val) inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val) +/** + * 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. + */ +#define NONFATAL_UNREACHABLE() \ + throw NonFatalCheckError( \ + format_internal_error("Unreachable code reached (non-fatal)", \ + __FILE__, __LINE__, __func__, PACKAGE_BUGREPORT)) + #endif // BITCOIN_UTIL_CHECK_H |