util: Allow Assert() in contexts without __func__

Without this, compile warnings could be hit about __func__ being only
valid inside functions.

warning: predefined identifier is only valid inside function [-Wpredefined-identifier-outside-function]
note: expanded from macro Assert
  115 | #define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
      |                                                                           ^

Ref https://github.com/bitcoin/bitcoin/pull/32740#discussion_r2486258473
This commit is contained in:
MarcoFalke
2025-11-04 22:16:27 +01:00
parent 33389f1144
commit fa5fbcd615
2 changed files with 25 additions and 22 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2022 The Bitcoin Core developers
// Copyright (c) 2022-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
@@ -11,30 +11,32 @@
#include <cstdio>
#include <cstdlib>
#include <source_location>
#include <string>
#include <string_view>
std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func)
std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc)
{
return strprintf("Internal bug detected: %s\n%s:%d (%s)\n"
"%s %s\n"
"Please report this issue here: %s\n",
msg, file, line, func, CLIENT_NAME, FormatFullVersion(), CLIENT_BUGREPORT);
msg, loc.file_name(), loc.line(), loc.function_name(),
CLIENT_NAME, FormatFullVersion(), CLIENT_BUGREPORT);
}
NonFatalCheckError::NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func)
: std::runtime_error{StrFormatInternalBug(msg, file, line, func)}
NonFatalCheckError::NonFatalCheckError(std::string_view msg, const std::source_location& loc)
: std::runtime_error{StrFormatInternalBug(msg, loc)}
{
}
bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts{false};
void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion)
void assertion_fail(const std::source_location& loc, std::string_view assertion)
{
if (g_detail_test_only_CheckFailuresAreExceptionsNotAborts) {
throw NonFatalCheckError{assertion, file, line, func};
throw NonFatalCheckError{assertion, loc};
}
auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", file, line, func, assertion);
auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", loc.file_name(), loc.line(), loc.function_name(), assertion);
fwrite(str.data(), 1, str.size(), stderr);
std::abort();
}