Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. C/C++
  4. pw_assert
Loading...
Searching...
No Matches
pw_assert

Overview

Flexible crash triggering and handling.

Main docs: Home | Backends

Macros

#define PW_ASSERT_HANDLE_FAILURE(condition_string)   pw_assert_HandleFailure()
 
#define PW_HANDLE_CRASH(...)
 
#define PW_HANDLE_ASSERT_FAILURE(condition_string, ...)
 
#define PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE(arg_a_str, arg_a_val, comparison_op_str, arg_b_str, arg_b_val, type_fmt, ...)
 
#define PW_ASSERT(condition)
 
#define PW_DASSERT(condition)
 
#define PW_ASSERT_OK(expression, ...)
 
#define PW_ASSERT_NOTNULL(ptr)
 
#define PW_ASSERT_ENABLE_DEBUG   1
 
#define PW_ASSERT_CAPTURE_VALUES   1
 
#define PW_ASSERT_BASIC_ACTION_ABORT   100
 
#define PW_ASSERT_BASIC_ACTION_EXIT   101
 
#define PW_ASSERT_BASIC_ACTION_LOOP   102
 
#define PW_ASSERT_BASIC_ACTION   PW_ASSERT_BASIC_ACTION_ABORT
 
#define PW_ASSERT_BASIC_SHOW_BANNER   1
 Whether to show the CRASH ASCII art banner.
 
#define PW_ASSERT_BASIC_USE_COLORS   1
 Whether to use ANSI colors.
 
#define PW_CRASH(...)
 
#define PW_CHECK(condition, ...)
 
#define PW_DCHECK(...)
 
#define PW_CHECK_INT_LE(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, <=, argb, int, "%d", __VA_ARGS__)
 Asserts a <= b (int).
 
#define PW_CHECK_INT_LT(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, < , argb, int, "%d", __VA_ARGS__)
 Asserts a < b (int).
 
#define PW_CHECK_INT_GE(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, >=, argb, int, "%d", __VA_ARGS__)
 Asserts a >= b (int).
 
#define PW_CHECK_INT_GT(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, > , argb, int, "%d", __VA_ARGS__)
 Asserts a > b (int).
 
#define PW_CHECK_INT_EQ(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, ==, argb, int, "%d", __VA_ARGS__)
 Asserts a == b (int).
 
#define PW_CHECK_INT_NE(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, !=, argb, int, "%d", __VA_ARGS__)
 Asserts a != b (int).
 
#define PW_DCHECK_INT_LE(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_INT_LE(__VA_ARGS__)
 Debug check: asserts a <= b (int) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_INT_LT(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_INT_LT(__VA_ARGS__)
 Debug check: asserts a < b (int) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_INT_GE(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_INT_GE(__VA_ARGS__)
 Debug check: asserts a >= b (int) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_INT_GT(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_INT_GT(__VA_ARGS__)
 Debug check: asserts a > b (int) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_INT_EQ(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_INT_EQ(__VA_ARGS__)
 Debug check: asserts a == b (int) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_INT_NE(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_INT_NE(__VA_ARGS__)
 Debug check: asserts a != b (int) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_CHECK_UINT_LE(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, <=, argb, unsigned int, "%u", __VA_ARGS__)
 Asserts a <= b (unsigned int).
 
#define PW_CHECK_UINT_LT(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, < , argb, unsigned int, "%u", __VA_ARGS__)
 Asserts a < b (unsigned int).
 
#define PW_CHECK_UINT_GE(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, >=, argb, unsigned int, "%u", __VA_ARGS__)
 Asserts a >= b (unsigned int).
 
#define PW_CHECK_UINT_GT(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, > , argb, unsigned int, "%u", __VA_ARGS__)
 Asserts a > b (unsigned int).
 
#define PW_CHECK_UINT_EQ(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, ==, argb, unsigned int, "%u", __VA_ARGS__)
 Asserts a == b (unsigned int).
 
#define PW_CHECK_UINT_NE(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, !=, argb, unsigned int, "%u", __VA_ARGS__)
 Asserts a != b (unsigned int).
 
#define PW_DCHECK_UINT_LE(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_UINT_LE(__VA_ARGS__)
 Debug check: asserts a <= b (unsigned int) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_UINT_LT(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_UINT_LT(__VA_ARGS__)
 Debug check: asserts a < b (unsigned int) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_UINT_GE(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_UINT_GE(__VA_ARGS__)
 Debug check: asserts a >= b (unsigned int) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_UINT_GT(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_UINT_GT(__VA_ARGS__)
 Debug check: asserts a > b (unsigned int) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_UINT_EQ(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_UINT_EQ(__VA_ARGS__)
 Debug check: asserts a == b (unsigned int) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_UINT_NE(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_UINT_NE(__VA_ARGS__)
 Debug check: asserts a != b (unsigned int) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_CHECK_PTR_LE(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, <=, argb, const void*, "%p", __VA_ARGS__)
 Asserts a <= b (const void*).
 
#define PW_CHECK_PTR_LT(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, < , argb, const void*, "%p", __VA_ARGS__)
 Asserts a < b (const void*).
 
#define PW_CHECK_PTR_GE(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, >=, argb, const void*, "%p", __VA_ARGS__)
 Asserts a >= b (const void*).
 
#define PW_CHECK_PTR_GT(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, > , argb, const void*, "%p", __VA_ARGS__)
 Asserts a > b (const void*).
 
#define PW_CHECK_PTR_EQ(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, ==, argb, const void*, "%p", __VA_ARGS__)
 Asserts a == b (const void*).
 
#define PW_CHECK_PTR_NE(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, !=, argb, const void*, "%p", __VA_ARGS__)
 Asserts a != b (const void*).
 
#define PW_CHECK_ADD(a, b, out, ...)
 
#define PW_CHECK_SUB(a, b, out, ...)
 
#define PW_CHECK_MUL(a, b, out, ...)
 
#define PW_CHECK_NOTNULL(arga, ...)    _PW_CHECK_BINARY_CMP_IMPL(arga, !=, nullptr, const void*, "%p", __VA_ARGS__)
 
#define PW_DCHECK_PTR_LE(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_PTR_LE(__VA_ARGS__)
 Debug check: asserts a <= b (const void*) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_PTR_LT(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_PTR_LT(__VA_ARGS__)
 Debug check: asserts a < b (const void*) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_PTR_GE(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_PTR_GE(__VA_ARGS__)
 Debug check: asserts a >= b (const void*) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_PTR_GT(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_PTR_GT(__VA_ARGS__)
 Debug check: asserts a > b (const void*) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_PTR_EQ(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_PTR_EQ(__VA_ARGS__)
 Debug check: asserts a == b (const void*) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_PTR_NE(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_PTR_NE(__VA_ARGS__)
 Debug check: asserts a != b (const void*) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_NOTNULL(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_NOTNULL(__VA_ARGS__)
 Debug variant of PW_CHECK_NOTNULL that only runs if PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_CHECK_FLOAT_NEAR(arga, argb, abs_tolerance, ...)    _PW_CHECK_FLOAT_NEAR(arga, argb, abs_tolerance, __VA_ARGS__)
 
#define PW_CHECK_FLOAT_EXACT_LE(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, <=, argb, float, "%f", __VA_ARGS__)
 Asserts a <= b (float).
 
#define PW_CHECK_FLOAT_EXACT_LT(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, < , argb, float, "%f", __VA_ARGS__)
 Asserts a < b (float).
 
#define PW_CHECK_FLOAT_EXACT_GE(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, >=, argb, float, "%f", __VA_ARGS__)
 Asserts a >= b (float).
 
#define PW_CHECK_FLOAT_EXACT_GT(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, > , argb, float, "%f", __VA_ARGS__)
 Asserts a > b (float).
 
#define PW_CHECK_FLOAT_EXACT_EQ(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, ==, argb, float, "%f", __VA_ARGS__)
 Asserts a == b (float).
 
#define PW_CHECK_FLOAT_EXACT_NE(arga, argb, ...)   _PW_CHECK_BINARY_CMP_IMPL(arga, !=, argb, float, "%f", __VA_ARGS__)
 Asserts a != b (float).
 
#define PW_DCHECK_FLOAT_NEAR(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_NEAR(__VA_ARGS__)
 Debug variant of PW_CHECK_FLOAT_NEAR that only runs if PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_FLOAT_EXACT_LE(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_EXACT_LE(__VA_ARGS__)
 Debug check: asserts a <= b (float) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_FLOAT_EXACT_LT(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_EXACT_LT(__VA_ARGS__)
 Debug check: asserts a < b (float) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_FLOAT_EXACT_GE(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_EXACT_GE(__VA_ARGS__)
 Debug check: asserts a >= b (float) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_FLOAT_EXACT_GT(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_EXACT_GT(__VA_ARGS__)
 Debug check: asserts a > b (float) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_FLOAT_EXACT_EQ(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_EXACT_EQ(__VA_ARGS__)
 Debug check: asserts a == b (float) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_DCHECK_FLOAT_EXACT_NE(...)   if (!(PW_ASSERT_ENABLE_DEBUG)) {} else PW_CHECK_FLOAT_EXACT_NE(__VA_ARGS__)
 Debug check: asserts a != b (float) when PW_ASSERT_ENABLE_DEBUG is enabled.
 
#define PW_CHECK_OK(expression, ...)
 
#define _PW_CHECK_OK_STATUS   auto
 
#define _PW_CHECK_OK_TO_STATUS(expr)   ::pw::internal::ConvertToStatus(expr)
 
#define _PW_CHECK_OK_IS_OK(status)   (status).ok()
 
#define _PW_CHECK_OK_STATUS_STRING(status)    ::pw::internal::StatusToCheckString(status)
 
#define PW_DCHECK_OK(...)
 
#define CRASH   PW_CRASH
 
#define CHECK   PW_CHECK
 
#define CHECK_PTR_LE   PW_CHECK_PTR_LE
 
#define CHECK_PTR_LT   PW_CHECK_PTR_LT
 
#define CHECK_PTR_GE   PW_CHECK_PTR_GE
 
#define CHECK_PTR_GT   PW_CHECK_PTR_GT
 
#define CHECK_PTR_EQ   PW_CHECK_PTR_EQ
 
#define CHECK_PTR_NE   PW_CHECK_PTR_NE
 
#define CHECK_NOTNULL   PW_CHECK_NOTNULL
 
#define CHECK_INT_LE   PW_CHECK_INT_LE
 
#define CHECK_INT_LT   PW_CHECK_INT_LT
 
#define CHECK_INT_GE   PW_CHECK_INT_GE
 
#define CHECK_INT_GT   PW_CHECK_INT_GT
 
#define CHECK_INT_EQ   PW_CHECK_INT_EQ
 
#define CHECK_INT_NE   PW_CHECK_INT_NE
 
#define CHECK_UINT_LE   PW_CHECK_UINT_LE
 
#define CHECK_UINT_LT   PW_CHECK_UINT_LT
 
#define CHECK_UINT_GE   PW_CHECK_UINT_GE
 
#define CHECK_UINT_GT   PW_CHECK_UINT_GT
 
#define CHECK_UINT_EQ   PW_CHECK_UINT_EQ
 
#define CHECK_UINT_NE   PW_CHECK_UINT_NE
 
#define CHECK_FLOAT_NEAR   PW_CHECK_FLOAT_NEAR
 
#define CHECK_FLOAT_EXACT_LE   PW_CHECK_FLOAT_EXACT_LE
 
#define CHECK_FLOAT_EXACT_LT   PW_CHECK_FLOAT_EXACT_LT
 
#define CHECK_FLOAT_EXACT_GE   PW_CHECK_FLOAT_EXACT_GE
 
#define CHECK_FLOAT_EXACT_GT   PW_CHECK_FLOAT_EXACT_GT
 
#define CHECK_FLOAT_EXACT_EQ   PW_CHECK_FLOAT_EXACT_EQ
 
#define CHECK_FLOAT_EXACT_NE   PW_CHECK_FLOAT_EXACT_NE
 
#define CHECK_OK   PW_CHECK_OK
 
#define DCHECK   PW_DCHECK
 
#define DCHECK_PTR_LE   PW_DCHECK_PTR_LE
 
#define DCHECK_PTR_LT   PW_DCHECK_PTR_LT
 
#define DCHECK_PTR_GE   PW_DCHECK_PTR_GE
 
#define DCHECK_PTR_GT   PW_DCHECK_PTR_GT
 
#define DCHECK_PTR_EQ   PW_DCHECK_PTR_EQ
 
#define DCHECK_PTR_NE   PW_DCHECK_PTR_NE
 
#define DCHECK_NOTNULL   PW_DCHECK_NOTNULL
 
#define DCHECK_INT_LE   PW_DCHECK_INT_LE
 
#define DCHECK_INT_LT   PW_DCHECK_INT_LT
 
#define DCHECK_INT_GE   PW_DCHECK_INT_GE
 
#define DCHECK_INT_GT   PW_DCHECK_INT_GT
 
#define DCHECK_INT_EQ   PW_DCHECK_INT_EQ
 
#define DCHECK_INT_NE   PW_DCHECK_INT_NE
 
#define DCHECK_UINT_LE   PW_DCHECK_UINT_LE
 
#define DCHECK_UINT_LT   PW_DCHECK_UINT_LT
 
#define DCHECK_UINT_GE   PW_DCHECK_UINT_GE
 
#define DCHECK_UINT_GT   PW_DCHECK_UINT_GT
 
#define DCHECK_UINT_EQ   PW_DCHECK_UINT_EQ
 
#define DCHECK_UINT_NE   PW_DCHECK_UINT_NE
 
#define DCHECK_FLOAT_NEAR   PW_DCHECK_FLOAT_NEAR
 
#define DCHECK_FLOAT_EXACT_LT   PW_DCHECK_FLOAT_EXACT_LT
 
#define DCHECK_FLOAT_EXACT_LE   PW_DCHECK_FLOAT_EXACT_LE
 
#define DCHECK_FLOAT_EXACT_GT   PW_DCHECK_FLOAT_EXACT_GT
 
#define DCHECK_FLOAT_EXACT_GE   PW_DCHECK_FLOAT_EXACT_GE
 
#define DCHECK_FLOAT_EXACT_EQ   PW_DCHECK_FLOAT_EXACT_EQ
 
#define DCHECK_FLOAT_EXACT_NE   PW_DCHECK_FLOAT_EXACT_NE
 
#define DCHECK_OK   PW_DCHECK_OK
 

Functions

PW_NO_RETURN void pw_assert_HandleFailure (void)
 

Macro Definition Documentation

◆ PW_ASSERT

#define PW_ASSERT (   condition)
Value:
do { \
if (!(condition)) { \
PW_ASSERT_failed_in_constant_expression_(); \
PW_ASSERT_HANDLE_FAILURE(#condition); \
} \
} while (0)

A header- and constexpr-safe version of PW_CHECK().

If the given condition is false, crash the system. Otherwise, do nothing. The condition is guaranteed to be evaluated. This assert implementation is guaranteed to be constexpr-safe.

Warning
Unlike the PW_CHECK_*() suite of macros, not all backends for this API capture rich information like line numbers, the file, expression arguments, or the stringified expression. Use these macros only when absolutely necessary — in headers, constexpr contexts, or in rare cases where the call site overhead of a full PW_CHECK must be avoided. Use PW_CHECK_*() whenever possible.

◆ PW_ASSERT_BASIC_ACTION

#define PW_ASSERT_BASIC_ACTION   PW_ASSERT_BASIC_ACTION_ABORT

Set to one of the following to define how pw_assert_basic should act after an assert failure:

  • PW_ASSERT_BASIC_ACTION_ABORT: Call std::abort()
  • PW_ASSERT_BASIC_ACTION_EXIT: Call std::_Exit(-1)
  • PW_ASSERT_BASIC_ACTION_LOOP: Loop forever

◆ PW_ASSERT_CAPTURE_VALUES

#define PW_ASSERT_CAPTURE_VALUES   1

Controls whether the evaluated values of a CHECK statement are captured as arguments to the final string. Disabling this will reduce code size at CHECK callsites, but slightly reduces debuggability.

This defaults to enabled.

◆ PW_ASSERT_ENABLE_DEBUG

#define PW_ASSERT_ENABLE_DEBUG   1

Controls whether DCHECK and DASSERT are enabled.

This defaults to being disabled if NDEBUG is defined, else it is enabled by default.

◆ PW_ASSERT_HANDLE_FAILURE

#define PW_ASSERT_HANDLE_FAILURE (   condition_string)    pw_assert_HandleFailure()

Handles a low-level crash from pw_assert/assert.h. Backends must ensure their implementation is safe for usage in headers, constexpr contexts, and templates. This macro should expand to an expression that does not return.

◆ PW_ASSERT_NOTNULL

#define PW_ASSERT_NOTNULL (   ptr)
Value:
([]([[maybe_unused]] auto&& _pw_assert_notnull_ptr) constexpr \
-> decltype(auto) { \
if (_pw_assert_notnull_ptr == nullptr) { \
PW_ASSERT_failed_in_constant_expression_(); \
PW_ASSERT_HANDLE_FAILURE(#ptr " != nullptr"); \
} \
return static_cast<decltype(_pw_assert_notnull_ptr)&&>( \
_pw_assert_notnull_ptr); \
}(ptr))

A header- and constexpr-safe version of PW_CHECK_NOTNULL().

If the given pointer is nullptr, crash the system. Otherwise, return the pointer unchanged. The expression is guaranteed to be evaluated once.

In C++, ptr may be a raw pointer, function pointer, or smart pointer (any type comparable to nullptr). The argument's type and value category are preserved, making it suitable for initializing member variables in constructor initializer lists. This macro is only available in C++.

Warning
Unlike the PW_CHECK_*() suite of macros, not all backends for this API capture rich information like line numbers, the file, expression arguments, or the stringified expression. Use these macros only when absolutely necessary — in headers, constexpr contexts, or in rare cases where the call site overhead of a full PW_CHECK must be avoided. Use PW_CHECK_*() whenever possible.

◆ PW_ASSERT_OK

#define PW_ASSERT_OK (   expression,
  ... 
)
Value:
do { \
if (!::pw::internal::ConvertToStatus(expression).ok()) { \
PW_ASSERT_HANDLE_FAILURE(#expression); \
} \
} while (0)

A header- and constexpr-safe version of PW_CHECK_OK().

If expression does not evaluate to an OK status, crash. Otherwise, do nothing. The expression is guaranteed to be evaluated.

In C++, expression must evaluate to a value convertible to a status via pw::internal::ConvertToStatus(). This includes pw::Status, pw_Status, pw::StatusWithSize, pw::Result, and custom pw::StatusBase-derived types.

In C, expression must evaluate to a pw_Status value.

Unlike PW_CHECK_OK, this macro does not currently log the failed status kind.

Warning
Unlike the PW_CHECK_*() suite of macros, not all backends for this API capture rich information like line numbers, the file, expression arguments, or the stringified expression. Use these macros only when absolutely necessary — in headers, constexpr contexts, or in rare cases where the call site overhead of a full PW_CHECK must be avoided. Use PW_CHECK_*() whenever possible.

◆ PW_CHECK

#define PW_CHECK (   condition,
  ... 
)
Value:
do { \
if (!(condition)) { \
_pw_assert_ConditionCannotContainThePercentCharacter( \
#condition); /* cannot use '%' in PW_CHECK conditions */ \
if (0) { /* Check args but don't execute to avoid multiple evaluation */ \
_pw_assert_CheckMessageArguments(" " __VA_ARGS__); \
} \
PW_HANDLE_ASSERT_FAILURE(#condition, "" __VA_ARGS__); \
} \
} while (0)

Asserts that a condition is true, optionally including a message with arguments to report if the condition is false.

Overloads:

◆ PW_CHECK_ADD

#define PW_CHECK_ADD (   a,
  b,
  out,
  ... 
)
Value:
PW_PRAGMA(message "Deprecated; use PW_CHECK(pw::CheckedAdd(...)) instead") \
PW_CHECK(!PW_ADD_OVERFLOW(a, b, out), __VA_ARGS__)
#define PW_PRAGMA(contents)
Definition: compiler.h:220

◆ PW_CHECK_FLOAT_NEAR

#define PW_CHECK_FLOAT_NEAR (   arga,
  argb,
  abs_tolerance,
  ... 
)     _PW_CHECK_FLOAT_NEAR(arga, argb, abs_tolerance, __VA_ARGS__)

Asserts that (a >= b - abs_tolerance) && (a <= b + abs_tolerance) is true, where a, b, and abs_tolerance are converted to float. Also asserts that abs_tolerance >= 0.

Overloads:

◆ PW_CHECK_MUL

#define PW_CHECK_MUL (   a,
  b,
  out,
  ... 
)
Value:
PW_PRAGMA(message "Deprecated; use PW_CHECK(pw::CheckedMul(...)) instead") \
PW_CHECK(!PW_MUL_OVERFLOW(a, b, out), __VA_ARGS__)

◆ PW_CHECK_NOTNULL

#define PW_CHECK_NOTNULL (   arga,
  ... 
)     _PW_CHECK_BINARY_CMP_IMPL(arga, !=, nullptr, const void*, "%p", __VA_ARGS__)

Asserts that the given pointer is not NULL (nullptr in C++), optionally including a message with arguments to report if the pointer is NULL.

Overloads:

◆ PW_CHECK_OK

#define PW_CHECK_OK (   expression,
  ... 
)
Value:
do { \
const _PW_CHECK_OK_STATUS _pw_assert_check_ok_status = \
_PW_CHECK_OK_TO_STATUS(expression); \
if (!_PW_CHECK_OK_IS_OK(_pw_assert_check_ok_status)) { \
_PW_CHECK_BINARY_ARG_HANDLER( \
#expression, \
_PW_CHECK_OK_STATUS_STRING(_pw_assert_check_ok_status), \
"==", \
"OkStatus()", \
"OK", \
"%s", \
"" __VA_ARGS__); \
} \
} while (0)

Asserts that expression evaluates to pw::OkStatus() (in C++) or PW_STATUS_OK (in C). Optionally includes a message with arguments to report.

In C++, expression must evaluate to a value convertible to a status via pw::internal::ConvertToStatus(). This includes pw::Status, pw_Status, pw::StatusWithSize, pw::Result, and custom pw::StatusBase-derived types.

In C, expression must evaluate to a pw_Status value.

Overloads:

◆ PW_CHECK_SUB

#define PW_CHECK_SUB (   a,
  b,
  out,
  ... 
)
Value:
PW_PRAGMA(message "Deprecated; use PW_CHECK(pw::CheckedSub(...)) instead") \
PW_CHECK(!PW_SUB_OVERFLOW(a, b, out), __VA_ARGS__)

◆ PW_CRASH

#define PW_CRASH (   ...)
Value:
do { \
if (0) { /* Check args but don't execute to avoid multiple evaluation */ \
_pw_assert_CheckMessageArguments(" " __VA_ARGS__); \
} \
PW_HANDLE_CRASH(__VA_ARGS__); \
} while (0)

Triggers a crash with a message. Replaces LOG_FATAL() in other systems. Can include a message with format arguments; for example:

PW_CRASH("Unexpected: frobnitz in state: %s", frobnitz_state);
#define PW_CRASH(...)
Definition: check_impl.h:37

◆ PW_DASSERT

#define PW_DASSERT (   condition)
Value:
do { \
if ((PW_ASSERT_ENABLE_DEBUG == 1) && !(condition)) { \
PW_ASSERT_failed_in_constant_expression_(); \
PW_ASSERT_HANDLE_FAILURE(#condition); \
} \
} while (0)
#define PW_ASSERT_ENABLE_DEBUG
Definition: config.h:28

A header- and constexpr-safe version of PW_DCHECK().

Same as PW_ASSERT(), except that if PW_ASSERT_ENABLE_DEBUG == 0, the assert is disabled and condition is not evaluated.

Warning
Unlike the PW_CHECK_*() suite of macros, not all backends for this API capture rich information like line numbers, the file, expression arguments, or the stringified expression. Use these macros only when absolutely necessary — in headers, constexpr contexts, or in rare cases where the call site overhead of a full PW_CHECK must be avoided. Use PW_CHECK_*() whenever possible.

◆ PW_DCHECK

#define PW_DCHECK (   ...)
Value:
do { \
PW_CHECK(__VA_ARGS__); \
} \
} while (0)

Debug variant of PW_CHECK that only runs if PW_ASSERT_ENABLE_DEBUG is enabled; otherwise, the entire statement is removed (and the expression not evaluated).

Overloads:

◆ PW_DCHECK_OK

#define PW_DCHECK_OK (   ...)
Value:
} else \
PW_CHECK_OK(__VA_ARGS__)

Debug variant of PW_CHECK_OK that only runs if PW_ASSERT_ENABLE_DEBUG is enabled.

Overloads:

◆ PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE

#define PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE (   arg_a_str,
  arg_a_val,
  comparison_op_str,
  arg_b_str,
  arg_b_val,
  type_fmt,
  ... 
)
Value:
PW_ASSERT_PRINT_EXPRESSION("CHECK", \
arg_a_str " " comparison_op_str " " arg_b_str); \
fprintf(stderr, \
" \033[33mEVALUATED CONDITION\033[0m\n\n " arg_a_str \
" (=" type_fmt ") " comparison_op_str " " arg_b_str " (=" type_fmt \
")" \
".\n\n", \
arg_a_val, \
arg_b_val); \
_PW_ASSERT_PRINT_MESSAGE_AND_ABORT(__VA_ARGS__)

Triggers a system crash or halt for a failed binary comparison assert (e.g. any of the PW_CHECK_<type>_<op> macros). The handler should combine the assert components into a useful message for the user; though in some cases this may not be possible.

  • arg_a_str - Stringified first operand.
  • arg_a_val - The value of the first operand.
  • comparison_op_str - The string version of the operator.
  • arg_b_str - Stringified second operand.
  • arg_b_val - The value of the second operand.
  • type_fmt - The format code for the type.
  • ... - A formatted message and arguments to go with the assert.

◆ PW_HANDLE_ASSERT_FAILURE

#define PW_HANDLE_ASSERT_FAILURE (   condition_string,
  ... 
)
Value:
PW_ASSERT_PRINT_EXPRESSION("CHECK", condition_string); \
_PW_ASSERT_PRINT_MESSAGE_AND_ABORT(__VA_ARGS__)

Triggers a system crash or halt, and if possible, delivers the condition string (indicating what expression was false) and the message with format arguments, to the user or developer.

This macro is invoked from the PW_CHECK facade macro if condition is false.

◆ PW_HANDLE_CRASH

#define PW_HANDLE_CRASH (   ...)
Value:
PW_ASSERT_PRINT_EXPRESSION("CRASH", "PW_CRASH()"); \
_PW_ASSERT_PRINT_MESSAGE_AND_ABORT(__VA_ARGS__)

Triggers a system crash or halt, and if possible, delivers the specified message and arguments to the user or developer.

Function Documentation

◆ pw_assert_HandleFailure()

PW_NO_RETURN void pw_assert_HandleFailure ( void  )

Handles a low-level crash from pw_assert/assert.h. In this crash handler, there is no access to line, file, expression, or other rich assert information. Must not return.