Repository: bitcoin/bitcoin
File: src/util/check.h (C/C++)

Code before:
 *
 * 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.
 */
#define CHECK_NONFATAL(condition) \
    inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)

/** Identity function. Abort if the value compares equal to zero */
#define Assert(val) inline_assertion_check<true>(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.
 */
#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.
 */
#define NONFATAL_UNREACHABLE()                                        \
    throw NonFatalCheckError(                                         \
        "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__)

// NOLINTEND(bugprone-lambda-function-name)

#if defined(__has_feature)
#    if __has_feature(address_sanitizer)
#       include <sanitizer/asan_interface.h>
#    endif
#endif

#ifndef ASAN_POISON_MEMORY_REGION
#   define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
#   define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
#endif

#endif // BITCOIN_UTIL_CHECK_H


Review comment (bug):
nit: This is a case of clang-format failing to understand what is happening inside a macro.

https://releases.llvm.org/17.0.1/tools/clang/docs/ClangFormatStyleOptions.html#cpp11bracedliststyle

https://github.com/bitcoin/bitcoin/blob/fada379589a17e86396aa7c2ce458ff2ff602b84/src/.clang-format#L87

If I in the same commit change this to...

```C++
    throw NonFatalCheckError{"Unreachable code reached (non-fatal)", std::source_location::current()}
```

...and also add...
```C++
void Func_NONFATAL_UNREACHABLE()
{
    throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() };
}
```
Then run this afterwards...
```shell
₿ git diff -U0 HEAD~1.. | ./contrib/devtools/clang-format-diff.py -p1 -i -v
```
...it results in:
```diff
 #define NONFATAL_UNREACHABLE() \
-    throw NonFatalCheckError{"Unreachable code reached (non-fatal)", std::source_location::current()}
+    throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() }

 void Func_NONFATAL_UNREACHABLE()
 {
-    throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() };
+    throw NonFatalCheckError{"Unreachable code reached (non-fatal)", std::source_location::current()};
 }
```
Proving what clang-format is aiming to do when it better understands the context. (I'm running 19.1.7 which seems to have the same behavior as PR author's).


Code after:
 * 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) \
    inline_check_non_fatal(condition, std::source_location::current(), #condition)

/** Identity function. Abort if the value compares equal to zero */
#define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #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.
 */
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)

/**
 * NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code. It throws a NonFatalCheckError.
 */
#define NONFATAL_UNREACHABLE() \
    throw NonFatalCheckError { "Unreachable code reached (non-fatal)", std::source_location::current() }

#if defined(__has_feature)
#    if __has_feature(address_sanitizer)
#       include <sanitizer/asan_interface.h>
#    endif
#endif

#ifndef ASAN_POISON_MEMORY_REGION
#   define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
#   define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
#endif

#endif // BITCOIN_UTIL_CHECK_H