LCOV - code coverage report
Current view: top level - src/util - check.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 87.5 % 8 7
Test Date: 2024-08-28 04:44:32 Functions: 62.0 % 92 57
Branches: 60.0 % 10 6

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2019-2022 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                 :             : #include <attributes.h>
       9                 :             : 
      10                 :             : #include <cassert> // IWYU pragma: export
      11                 :             : #include <stdexcept>
      12                 :             : #include <string>
      13                 :             : #include <string_view>
      14                 :             : #include <utility>
      15                 :             : 
      16                 :             : std::string StrFormatInternalBug(std::string_view msg, std::string_view file, int line, std::string_view func);
      17                 :             : 
      18                 :             : class NonFatalCheckError : public std::runtime_error
      19                 :             : {
      20                 :             : public:
      21                 :             :     NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func);
      22                 :             : };
      23                 :             : 
      24                 :             : /** Helper for CHECK_NONFATAL() */
      25                 :             : template <typename T>
      26         [ -  + ]:      166427 : T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, const char* func, const char* assertion)
      27                 :             : {
      28         [ +  + ]:      166427 :     if (!val) {
      29         [ +  - ]:           8 :         throw NonFatalCheckError{assertion, file, line, func};
      30                 :             :     }
      31                 :      166419 :     return std::forward<T>(val);
      32                 :             : }
      33                 :             : 
      34                 :             : #if defined(NDEBUG)
      35                 :             : #error "Cannot compile without assertions!"
      36                 :             : #endif
      37                 :             : 
      38                 :             : /** Helper for Assert() */
      39                 :             : void assertion_fail(std::string_view file, int line, std::string_view func, std::string_view assertion);
      40                 :             : 
      41                 :             : /** Helper for Assert()/Assume() */
      42                 :             : template <bool IS_ASSERT, typename T>
      43         [ -  + ]:    16209943 : T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] const char* file, [[maybe_unused]] int line, [[maybe_unused]] const char* func, [[maybe_unused]] const char* assertion)
      44                 :             : {
      45                 :             :     if constexpr (IS_ASSERT
      46                 :             : #ifdef ABORT_ON_FAILED_ASSUME
      47                 :             :                   || true
      48                 :             : #endif
      49                 :             :     ) {
      50         [ -  + ]:     9922708 :         if (!val) {
      51                 :           0 :             assertion_fail(file, line, func, assertion);
      52                 :             :         }
      53                 :             :     }
      54                 :    16209943 :     return std::forward<T>(val);
      55                 :             : }
      56                 :             : 
      57                 :             : // All macros may use __func__ inside a lambda, so put them under nolint.
      58                 :             : // NOLINTBEGIN(bugprone-lambda-function-name)
      59                 :             : 
      60                 :             : #define STR_INTERNAL_BUG(msg) StrFormatInternalBug((msg), __FILE__, __LINE__, __func__)
      61                 :             : 
      62                 :             : /**
      63                 :             :  * Identity function. Throw a NonFatalCheckError when the condition evaluates to false
      64                 :             :  *
      65                 :             :  * This should only be used
      66                 :             :  * - where the condition is assumed to be true, not for error handling or validating user input
      67                 :             :  * - where a failure to fulfill the condition is recoverable and does not abort the program
      68                 :             :  *
      69                 :             :  * For example in RPC code, where it is undesirable to crash the whole program, this can be generally used to replace
      70                 :             :  * asserts or recoverable logic errors. A NonFatalCheckError in RPC code is caught and passed as a string to the RPC
      71                 :             :  * caller, which can then report the issue to the developers.
      72                 :             :  */
      73                 :             : #define CHECK_NONFATAL(condition) \
      74                 :             :     inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
      75                 :             : 
      76                 :             : /** Identity function. Abort if the value compares equal to zero */
      77                 :             : #define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
      78                 :             : 
      79                 :             : /**
      80                 :             :  * Assume is the identity function.
      81                 :             :  *
      82                 :             :  * - Should be used to run non-fatal checks. In debug builds it behaves like
      83                 :             :  *   Assert()/assert() to notify developers and testers about non-fatal errors.
      84                 :             :  *   In production it doesn't warn or log anything.
      85                 :             :  * - For fatal errors, use Assert().
      86                 :             :  * - For non-fatal errors in interactive sessions (e.g. RPC or command line
      87                 :             :  *   interfaces), CHECK_NONFATAL() might be more appropriate.
      88                 :             :  */
      89                 :             : #define Assume(val) inline_assertion_check<false>(val, __FILE__, __LINE__, __func__, #val)
      90                 :             : 
      91                 :             : /**
      92                 :             :  * NONFATAL_UNREACHABLE() is a macro that is used to mark unreachable code. It throws a NonFatalCheckError.
      93                 :             :  */
      94                 :             : #define NONFATAL_UNREACHABLE()                                        \
      95                 :             :     throw NonFatalCheckError(                                         \
      96                 :             :         "Unreachable code reached (non-fatal)", __FILE__, __LINE__, __func__)
      97                 :             : 
      98                 :             : // NOLINTEND(bugprone-lambda-function-name)
      99                 :             : 
     100                 :             : #endif // BITCOIN_UTIL_CHECK_H
        

Generated by: LCOV version 2.0-1