1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_assert: 2*61c4878aSAndroid Build Coastguard Worker 3*61c4878aSAndroid Build Coastguard Worker========= 4*61c4878aSAndroid Build Coastguard Workerpw_assert 5*61c4878aSAndroid Build Coastguard Worker========= 6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module:: 7*61c4878aSAndroid Build Coastguard Worker :name: pw_assert 8*61c4878aSAndroid Build Coastguard Worker 9*61c4878aSAndroid Build Coastguard Worker-------- 10*61c4878aSAndroid Build Coastguard WorkerOverview 11*61c4878aSAndroid Build Coastguard Worker-------- 12*61c4878aSAndroid Build Coastguard WorkerPigweed's assert module enables applications to check preconditions, triggering 13*61c4878aSAndroid Build Coastguard Workera crash if the condition is not met. Consistent use of asserts is one aspect of 14*61c4878aSAndroid Build Coastguard Workerdefensive programming that can lead to more reliable and less buggy code. 15*61c4878aSAndroid Build Coastguard Worker 16*61c4878aSAndroid Build Coastguard WorkerThe assert API facilitates flexible crash handling through Pigweed's facade 17*61c4878aSAndroid Build Coastguard Workermechanism. The API is designed to enable features like: 18*61c4878aSAndroid Build Coastguard Worker 19*61c4878aSAndroid Build Coastguard Worker- Optional ancillary printf-style messages along assertions 20*61c4878aSAndroid Build Coastguard Worker- Capturing actual values of binary operator assertions like ``a < b`` 21*61c4878aSAndroid Build Coastguard Worker- Compatibility with pw_tokenizer for reduced binary code size 22*61c4878aSAndroid Build Coastguard Worker 23*61c4878aSAndroid Build Coastguard WorkerThe ``pw_assert`` API provides three classes of macros: 24*61c4878aSAndroid Build Coastguard Worker 25*61c4878aSAndroid Build Coastguard Worker- **PW_CRASH(format, ...)** - Trigger a crash with a message. 26*61c4878aSAndroid Build Coastguard Worker- **PW_CHECK(condition[, format, ...])** - Assert a condition, optionally with 27*61c4878aSAndroid Build Coastguard Worker a message. 28*61c4878aSAndroid Build Coastguard Worker- **PW_CHECK_<type>_<cmp>(a, b[, fmt, ...])** - Assert that the expression ``a 29*61c4878aSAndroid Build Coastguard Worker <cmp> b`` is true, optionally with a message. 30*61c4878aSAndroid Build Coastguard Worker- **PW_ASSERT(condition)** - Header- and constexpr-safe assert. 31*61c4878aSAndroid Build Coastguard Worker 32*61c4878aSAndroid Build Coastguard Worker.. tip:: 33*61c4878aSAndroid Build Coastguard Worker 34*61c4878aSAndroid Build Coastguard Worker All of the ``CHECK`` macros optionally support a message with additional 35*61c4878aSAndroid Build Coastguard Worker arguments, to assist in debugging when an assert triggers: 36*61c4878aSAndroid Build Coastguard Worker 37*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 38*61c4878aSAndroid Build Coastguard Worker 39*61c4878aSAndroid Build Coastguard Worker PW_CHECK_INT_LE(ItemCount(), 100); 40*61c4878aSAndroid Build Coastguard Worker PW_CHECK_INT_LE(ItemCount(), 100, "System state: %s", GetStateStr()); 41*61c4878aSAndroid Build Coastguard Worker 42*61c4878aSAndroid Build Coastguard Worker To ensure compatibility with :ref:`module-pw_assert_log` and 43*61c4878aSAndroid Build Coastguard Worker :ref:`module-pw_log_tokenized`, the message must be a string literal. 44*61c4878aSAndroid Build Coastguard Worker 45*61c4878aSAndroid Build Coastguard WorkerExample 46*61c4878aSAndroid Build Coastguard Worker======= 47*61c4878aSAndroid Build Coastguard Worker 48*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 49*61c4878aSAndroid Build Coastguard Worker 50*61c4878aSAndroid Build Coastguard Worker #include "pw_assert/check.h" 51*61c4878aSAndroid Build Coastguard Worker 52*61c4878aSAndroid Build Coastguard Worker int main() { 53*61c4878aSAndroid Build Coastguard Worker bool sensor_running = StartSensor(&msg); 54*61c4878aSAndroid Build Coastguard Worker PW_CHECK(sensor_running, "Sensor failed to start; code: %s", msg); 55*61c4878aSAndroid Build Coastguard Worker 56*61c4878aSAndroid Build Coastguard Worker int temperature_c = ReadSensorCelcius(); 57*61c4878aSAndroid Build Coastguard Worker PW_CHECK_INT_LE(temperature_c, 100, 58*61c4878aSAndroid Build Coastguard Worker "System is way out of heat spec; state=%s", 59*61c4878aSAndroid Build Coastguard Worker ReadSensorStateString()); 60*61c4878aSAndroid Build Coastguard Worker } 61*61c4878aSAndroid Build Coastguard Worker 62*61c4878aSAndroid Build Coastguard Worker.. tip:: 63*61c4878aSAndroid Build Coastguard Worker 64*61c4878aSAndroid Build Coastguard Worker All macros have both a ``CHECK`` and ``DCHECK`` variant. The ``CHECK`` 65*61c4878aSAndroid Build Coastguard Worker variant is always enabled, even in production. Generally, we advise making 66*61c4878aSAndroid Build Coastguard Worker most asserts ``CHECK`` rather than ``DCHECK``, unless there is a critical 67*61c4878aSAndroid Build Coastguard Worker performance or code size reason to use ``DCHECK``. 68*61c4878aSAndroid Build Coastguard Worker 69*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 70*61c4878aSAndroid Build Coastguard Worker 71*61c4878aSAndroid Build Coastguard Worker // This assert is always enabled, even in production. 72*61c4878aSAndroid Build Coastguard Worker PW_CHECK_INT_LE(ItemCount(), 100); 73*61c4878aSAndroid Build Coastguard Worker 74*61c4878aSAndroid Build Coastguard Worker // This assert is enabled based on ``PW_ASSERT_ENABLE_DEBUG``. 75*61c4878aSAndroid Build Coastguard Worker // The functions ItemCount() and GetStateStr() are never called. 76*61c4878aSAndroid Build Coastguard Worker PW_DCHECK_INT_LE(ItemCount(), 100, "System state: %s", GetStateStr()); 77*61c4878aSAndroid Build Coastguard Worker 78*61c4878aSAndroid Build Coastguard Worker.. tip:: 79*61c4878aSAndroid Build Coastguard Worker 80*61c4878aSAndroid Build Coastguard Worker Use ``PW_ASSERT`` from ``pw_assert/assert.h`` for asserts in headers or 81*61c4878aSAndroid Build Coastguard Worker asserting in ``constexpr`` contexts. 82*61c4878aSAndroid Build Coastguard Worker 83*61c4878aSAndroid Build Coastguard WorkerStructure of Assert Modules 84*61c4878aSAndroid Build Coastguard Worker=========================== 85*61c4878aSAndroid Build Coastguard WorkerThe module is split into two components: 86*61c4878aSAndroid Build Coastguard Worker 87*61c4878aSAndroid Build Coastguard Worker1. The **facade** (this module) which is only a macro interface layer, and 88*61c4878aSAndroid Build Coastguard Worker performs the actual checks for the conditions. 89*61c4878aSAndroid Build Coastguard Worker2. The **backend**, provided elsewhere, that handles the consequences of an 90*61c4878aSAndroid Build Coastguard Worker assert failing. Example backends include ``pw_assert_basic``, which prints a 91*61c4878aSAndroid Build Coastguard Worker useful message and either quits the application (on host) or hangs in a 92*61c4878aSAndroid Build Coastguard Worker while loop (on device). In the future, there will be a tokenized assert 93*61c4878aSAndroid Build Coastguard Worker backend. This is also where application or product specific crash handling 94*61c4878aSAndroid Build Coastguard Worker would go. 95*61c4878aSAndroid Build Coastguard Worker 96*61c4878aSAndroid Build Coastguard Worker.. mermaid:: 97*61c4878aSAndroid Build Coastguard Worker 98*61c4878aSAndroid Build Coastguard Worker graph LR 99*61c4878aSAndroid Build Coastguard Worker facade --> backend 100*61c4878aSAndroid Build Coastguard Worker 101*61c4878aSAndroid Build Coastguard WorkerSee the Backend API section below for more details. 102*61c4878aSAndroid Build Coastguard Worker 103*61c4878aSAndroid Build Coastguard Worker---------- 104*61c4878aSAndroid Build Coastguard WorkerFacade API 105*61c4878aSAndroid Build Coastguard Worker---------- 106*61c4878aSAndroid Build Coastguard WorkerThe below functions describe the assert API functions that applications should 107*61c4878aSAndroid Build Coastguard Workerinvoke to assert. These macros are found in the ``pw_assert/check.h`` header. 108*61c4878aSAndroid Build Coastguard Worker 109*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_CRASH(format, ...) 110*61c4878aSAndroid Build Coastguard Worker 111*61c4878aSAndroid Build Coastguard Worker Trigger a crash with a message. Replaces LOG_FATAL() in other systems. Can 112*61c4878aSAndroid Build Coastguard Worker include a message with format arguments; for example: 113*61c4878aSAndroid Build Coastguard Worker 114*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 115*61c4878aSAndroid Build Coastguard Worker 116*61c4878aSAndroid Build Coastguard Worker PW_CRASH("Unexpected: frobnitz in state: %s", frobnitz_state); 117*61c4878aSAndroid Build Coastguard Worker 118*61c4878aSAndroid Build Coastguard Worker Note: ``PW_CRASH`` is the equivalent of ``LOG_FATAL`` in other systems, where 119*61c4878aSAndroid Build Coastguard Worker a device crash is triggered with a message. In Pigweed, logging and 120*61c4878aSAndroid Build Coastguard Worker crashing/asserting are separated. There is a ``LOG_CRITICAL`` level in the 121*61c4878aSAndroid Build Coastguard Worker logging module, but it does not have side effects; for ``LOG_FATAL``, instead 122*61c4878aSAndroid Build Coastguard Worker use this macro (``PW_CRASH``). 123*61c4878aSAndroid Build Coastguard Worker 124*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_CHECK(condition) 125*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_CHECK(condition, format, ...) 126*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_DCHECK(condition) 127*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_DCHECK(condition, format, ...) 128*61c4878aSAndroid Build Coastguard Worker 129*61c4878aSAndroid Build Coastguard Worker Assert that a condition is true, optionally including a message with 130*61c4878aSAndroid Build Coastguard Worker arguments to report if the codition is false. 131*61c4878aSAndroid Build Coastguard Worker 132*61c4878aSAndroid Build Coastguard Worker The ``DCHECK`` variants only run if ``PW_ASSERT_ENABLE_DEBUG`` is enabled; 133*61c4878aSAndroid Build Coastguard Worker otherwise, the entire statement is removed (and the expression not evaluated). 134*61c4878aSAndroid Build Coastguard Worker 135*61c4878aSAndroid Build Coastguard Worker Example: 136*61c4878aSAndroid Build Coastguard Worker 137*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 138*61c4878aSAndroid Build Coastguard Worker 139*61c4878aSAndroid Build Coastguard Worker PW_CHECK(StartTurbines()); 140*61c4878aSAndroid Build Coastguard Worker PW_CHECK(StartWarpDrive(), "Oddly warp drive couldn't start; ruh-roh!"); 141*61c4878aSAndroid Build Coastguard Worker PW_CHECK(RunSelfTest(), "Failure in self test; try %d", TestAttempts()); 142*61c4878aSAndroid Build Coastguard Worker 143*61c4878aSAndroid Build Coastguard Worker .. attention:: 144*61c4878aSAndroid Build Coastguard Worker 145*61c4878aSAndroid Build Coastguard Worker Don't use use ``PW_CHECK`` for binary comparisons or status checks! 146*61c4878aSAndroid Build Coastguard Worker 147*61c4878aSAndroid Build Coastguard Worker Instead, use the ``PW_CHECK_<TYPE>_<OP>`` macros. These macros enable 148*61c4878aSAndroid Build Coastguard Worker capturing the value of the operands, and also tokenizing them if using a 149*61c4878aSAndroid Build Coastguard Worker tokenizing assert backend. For example, if ``x`` and ``b`` are integers, 150*61c4878aSAndroid Build Coastguard Worker use instead ``PW_CHECK_INT_LT(x, b)``. 151*61c4878aSAndroid Build Coastguard Worker 152*61c4878aSAndroid Build Coastguard Worker Additionally, use ``PW_CHECK_OK(status)`` when checking for an OK status, 153*61c4878aSAndroid Build Coastguard Worker since it enables showing a human-readable status string rather than an 154*61c4878aSAndroid Build Coastguard Worker integer (e.g. ``status == RESOURCE_EXHAUSTED`` instead of ``status == 5``. 155*61c4878aSAndroid Build Coastguard Worker This works with any expression convertible to ``pw::Status``, including 156*61c4878aSAndroid Build Coastguard Worker ``pw::StatusWithString`` and ``pw::Result<T>``. 157*61c4878aSAndroid Build Coastguard Worker 158*61c4878aSAndroid Build Coastguard Worker +------------------------------------+-------------------------------------+ 159*61c4878aSAndroid Build Coastguard Worker | **Do NOT do this** | **Do this instead** | 160*61c4878aSAndroid Build Coastguard Worker +------------------------------------+-------------------------------------+ 161*61c4878aSAndroid Build Coastguard Worker | ``PW_CHECK(a_int < b_int)`` | ``PW_CHECK_INT_LT(a_int, b_int)`` | 162*61c4878aSAndroid Build Coastguard Worker +------------------------------------+-------------------------------------+ 163*61c4878aSAndroid Build Coastguard Worker | ``PW_CHECK(a_ptr <= b_ptr)`` | ``PW_CHECK_PTR_LE(a_ptr, b_ptr)`` | 164*61c4878aSAndroid Build Coastguard Worker +------------------------------------+-------------------------------------+ 165*61c4878aSAndroid Build Coastguard Worker | ``PW_CHECK(Temp() <= 10.0)`` | ``PW_CHECK_FLOAT_EXACT_LE(`` | 166*61c4878aSAndroid Build Coastguard Worker | | `` Temp(), 10.0)`` | 167*61c4878aSAndroid Build Coastguard Worker +------------------------------------+-------------------------------------+ 168*61c4878aSAndroid Build Coastguard Worker | ``PW_CHECK(Foo() == OkStatus())`` | ``PW_CHECK_OK(Foo())`` | 169*61c4878aSAndroid Build Coastguard Worker +------------------------------------+-------------------------------------+ 170*61c4878aSAndroid Build Coastguard Worker 171*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_CHECK_NOTNULL(ptr) 172*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_CHECK_NOTNULL(ptr, format, ...) 173*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_DCHECK_NOTNULL(ptr) 174*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_DCHECK_NOTNULL(ptr, format, ...) 175*61c4878aSAndroid Build Coastguard Worker 176*61c4878aSAndroid Build Coastguard Worker Assert that the given pointer is not ``NULL``, optionally including a message 177*61c4878aSAndroid Build Coastguard Worker with arguments to report if the pointer is ``NULL``. 178*61c4878aSAndroid Build Coastguard Worker 179*61c4878aSAndroid Build Coastguard Worker The ``DCHECK`` variants only run if ``PW_ASSERT_ENABLE_DEBUG`` is enabled; 180*61c4878aSAndroid Build Coastguard Worker otherwise, the entire statement is removed (and the expression not evaluated). 181*61c4878aSAndroid Build Coastguard Worker 182*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 183*61c4878aSAndroid Build Coastguard Worker 184*61c4878aSAndroid Build Coastguard Worker Foo* foo = GetTheFoo() 185*61c4878aSAndroid Build Coastguard Worker PW_CHECK_NOTNULL(foo); 186*61c4878aSAndroid Build Coastguard Worker 187*61c4878aSAndroid Build Coastguard Worker Bar* bar = GetSomeBar(); 188*61c4878aSAndroid Build Coastguard Worker PW_CHECK_NOTNULL(bar, "Weirdly got NULL bar; state: %d", MyState()); 189*61c4878aSAndroid Build Coastguard Worker 190*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_CHECK_TYPE_OP(a, b) 191*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_CHECK_TYPE_OP(a, b, format, ...) 192*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_DCHECK_TYPE_OP(a, b) 193*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_DCHECK_TYPE_OP(a, b, format, ...) 194*61c4878aSAndroid Build Coastguard Worker 195*61c4878aSAndroid Build Coastguard Worker Asserts that ``a OP b`` is true, where ``a`` and ``b`` are converted to 196*61c4878aSAndroid Build Coastguard Worker ``TYPE``; with ``OP`` and ``TYPE`` described below. 197*61c4878aSAndroid Build Coastguard Worker 198*61c4878aSAndroid Build Coastguard Worker If present, the optional format message is reported on failure. Depending on 199*61c4878aSAndroid Build Coastguard Worker the backend, values of ``a`` and ``b`` will also be reported. 200*61c4878aSAndroid Build Coastguard Worker 201*61c4878aSAndroid Build Coastguard Worker The ``DCHECK`` variants only run if ``PW_ASSERT_ENABLE_DEBUG`` is enabled; 202*61c4878aSAndroid Build Coastguard Worker otherwise, the entire statement is removed (and the expression not evaluated). 203*61c4878aSAndroid Build Coastguard Worker 204*61c4878aSAndroid Build Coastguard Worker Example, with no message: 205*61c4878aSAndroid Build Coastguard Worker 206*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 207*61c4878aSAndroid Build Coastguard Worker 208*61c4878aSAndroid Build Coastguard Worker PW_CHECK_INT_LE(CurrentTemperature(), 100); 209*61c4878aSAndroid Build Coastguard Worker PW_CHECK_INT_LE(ItemCount(), 100); 210*61c4878aSAndroid Build Coastguard Worker 211*61c4878aSAndroid Build Coastguard Worker Example, with an included message and arguments: 212*61c4878aSAndroid Build Coastguard Worker 213*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 214*61c4878aSAndroid Build Coastguard Worker 215*61c4878aSAndroid Build Coastguard Worker PW_CHECK_FLOAT_EXACT_GE(BatteryVoltage(), 3.2, 216*61c4878aSAndroid Build Coastguard Worker "System state=%s", SysState()); 217*61c4878aSAndroid Build Coastguard Worker 218*61c4878aSAndroid Build Coastguard Worker Below is the full list of binary comparison assert macros, along with the 219*61c4878aSAndroid Build Coastguard Worker type specifier. The specifier is irrelevant to application authors but is 220*61c4878aSAndroid Build Coastguard Worker needed for backend implementers. 221*61c4878aSAndroid Build Coastguard Worker 222*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 223*61c4878aSAndroid Build Coastguard Worker | Macro | a, b type | condition | a, b format specifier | 224*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 225*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_INT_LE | int | a <= b | %d | 226*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 227*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_INT_LT | int | a < b | %d | 228*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 229*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_INT_GE | int | a >= b | %d | 230*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 231*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_INT_GT | int | a > b | %d | 232*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 233*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_INT_EQ | int | a == b | %d | 234*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 235*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_INT_NE | int | a != b | %d | 236*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 237*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_UINT_LE | unsigned int | a <= b | %u | 238*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 239*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_UINT_LT | unsigned int | a < b | %u | 240*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 241*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_UINT_GE | unsigned int | a >= b | %u | 242*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 243*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_UINT_GT | unsigned int | a > b | %u | 244*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 245*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_UINT_EQ | unsigned int | a == b | %u | 246*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 247*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_UINT_NE | unsigned int | a != b | %u | 248*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 249*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_PTR_LE | void* | a <= b | %p | 250*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 251*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_PTR_LT | void* | a < b | %p | 252*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 253*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_PTR_GE | void* | a >= b | %p | 254*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 255*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_PTR_GT | void* | a > b | %p | 256*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 257*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_PTR_EQ | void* | a == b | %p | 258*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 259*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_PTR_NE | void* | a != b | %p | 260*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 261*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_FLOAT_EXACT_LE | float | a <= b | %f | 262*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 263*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_FLOAT_EXACT_LT | float | a < b | %f | 264*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 265*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_FLOAT_EXACT_GE | float | a >= b | %f | 266*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 267*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_FLOAT_EXACT_GT | float | a > b | %f | 268*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 269*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_FLOAT_EXACT_EQ | float | a == b | %f | 270*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 271*61c4878aSAndroid Build Coastguard Worker | PW_CHECK_FLOAT_EXACT_NE | float | a != b | %f | 272*61c4878aSAndroid Build Coastguard Worker +-------------------------+--------------+-----------+-----------------------+ 273*61c4878aSAndroid Build Coastguard Worker 274*61c4878aSAndroid Build Coastguard Worker The above ``CHECK_*_*()`` are also available in DCHECK variants, which will 275*61c4878aSAndroid Build Coastguard Worker only evaluate their arguments and trigger if the ``PW_ASSERT_ENABLE_DEBUG`` 276*61c4878aSAndroid Build Coastguard Worker macro is enabled. 277*61c4878aSAndroid Build Coastguard Worker 278*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 279*61c4878aSAndroid Build Coastguard Worker | Macro | a, b type | condition | a, b format | 280*61c4878aSAndroid Build Coastguard Worker | | | | specifier | 281*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 282*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_INT_LE | int | a <= b | %d | 283*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 284*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_INT_LT | int | a < b | %d | 285*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 286*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_INT_GE | int | a >= b | %d | 287*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 288*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_INT_GT | int | a > b | %d | 289*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 290*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_INT_EQ | int | a == b | %d | 291*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 292*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_INT_NE | int | a != b | %d | 293*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 294*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_UINT_LE | unsigned int | a <= b | %u | 295*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 296*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_UINT_LT | unsigned int | a < b | %u | 297*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 298*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_UINT_GE | unsigned int | a >= b | %u | 299*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 300*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_UINT_GT | unsigned int | a > b | %u | 301*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 302*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_UINT_EQ | unsigned int | a == b | %u | 303*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 304*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_UINT_NE | unsigned int | a != b | %u | 305*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 306*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_PTR_LE | void* | a <= b | %p | 307*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 308*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_PTR_LT | void* | a < b | %p | 309*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 310*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_PTR_GE | void* | a >= b | %p | 311*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 312*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_PTR_GT | void* | a > b | %p | 313*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 314*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_PTR_EQ | void* | a == b | %p | 315*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 316*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_PTR_NE | void* | a != b | %p | 317*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 318*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_FLOAT_EXACT_LE | float | a <= b | %f | 319*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 320*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_FLOAT_EXACT_LT | float | a < b | %f | 321*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 322*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_FLOAT_EXACT_GE | float | a >= b | %f | 323*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 324*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_FLOAT_EXACT_GT | float | a > b | %f | 325*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 326*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_FLOAT_EXACT_EQ | float | a == b | %f | 327*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 328*61c4878aSAndroid Build Coastguard Worker | PW_DCHECK_FLOAT_EXACT_NE | float | a != b | %f | 329*61c4878aSAndroid Build Coastguard Worker +--------------------------+--------------+-----------+----------------------+ 330*61c4878aSAndroid Build Coastguard Worker 331*61c4878aSAndroid Build Coastguard Worker.. attention:: 332*61c4878aSAndroid Build Coastguard Worker 333*61c4878aSAndroid Build Coastguard Worker For float, proper comparator checks which take floating point 334*61c4878aSAndroid Build Coastguard Worker precision and ergo error accumulation into account are not provided on 335*61c4878aSAndroid Build Coastguard Worker purpose as this comes with some complexity and requires application 336*61c4878aSAndroid Build Coastguard Worker specific tolerances in terms of Units of Least Precision (ULP). Instead, 337*61c4878aSAndroid Build Coastguard Worker carefully consider how floating point precision and error impact the data 338*61c4878aSAndroid Build Coastguard Worker they are bounding and whether checks are appropriate. 339*61c4878aSAndroid Build Coastguard Worker 340*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_CHECK_FLOAT_NEAR(a, b, abs_tolerance) 341*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_CHECK_FLOAT_NEAR(a, b, abs_tolerance, format, ...) 342*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_DCHECK_FLOAT_NEAR(a, b, abs_tolerance) 343*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_DCHECK_FLOAT_NEAR(a, b, abs_tolerance, format, ...) 344*61c4878aSAndroid Build Coastguard Worker 345*61c4878aSAndroid Build Coastguard Worker Asserts that ``(a >= b - abs_tolerance) && (a <= b + abs_tolerance)`` is true, 346*61c4878aSAndroid Build Coastguard Worker where ``a``, ``b``, and ``abs_tolerance`` are converted to ``float``. 347*61c4878aSAndroid Build Coastguard Worker 348*61c4878aSAndroid Build Coastguard Worker .. note:: 349*61c4878aSAndroid Build Coastguard Worker This also asserts that ``abs_tolerance >= 0``. 350*61c4878aSAndroid Build Coastguard Worker 351*61c4878aSAndroid Build Coastguard Worker The ``DCHECK`` variants only run if ``PW_ASSERT_ENABLE_DEBUG`` is enabled; 352*61c4878aSAndroid Build Coastguard Worker otherwise, the entire statement is removed (and the expression not evaluated). 353*61c4878aSAndroid Build Coastguard Worker 354*61c4878aSAndroid Build Coastguard Worker Example, with no message: 355*61c4878aSAndroid Build Coastguard Worker 356*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 357*61c4878aSAndroid Build Coastguard Worker 358*61c4878aSAndroid Build Coastguard Worker PW_CHECK_FLOAT_NEAR(cos(0.0f), 1, 0.001); 359*61c4878aSAndroid Build Coastguard Worker 360*61c4878aSAndroid Build Coastguard Worker Example, with an included message and arguments: 361*61c4878aSAndroid Build Coastguard Worker 362*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 363*61c4878aSAndroid Build Coastguard Worker 364*61c4878aSAndroid Build Coastguard Worker PW_CHECK_FLOAT_NEAR(FirstOperation(), RedundantOperation(), 0.1, 365*61c4878aSAndroid Build Coastguard Worker "System state=%s", SysState()); 366*61c4878aSAndroid Build Coastguard Worker 367*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_CHECK_OK(status) 368*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_CHECK_OK(status, format, ...) 369*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_DCHECK_OK(status) 370*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_DCHECK_OK(status, format, ...) 371*61c4878aSAndroid Build Coastguard Worker 372*61c4878aSAndroid Build Coastguard Worker Assert that ``status`` evaluates to ``pw::OkStatus()`` (in C++) or 373*61c4878aSAndroid Build Coastguard Worker ``PW_STATUS_OK`` (in C). Optionally include a message with arguments to 374*61c4878aSAndroid Build Coastguard Worker report. 375*61c4878aSAndroid Build Coastguard Worker 376*61c4878aSAndroid Build Coastguard Worker ``status`` can be a ``pw::Status``, or (in C++ only) any expression 377*61c4878aSAndroid Build Coastguard Worker convertible to ``pw::Status``, including ``pw::StatusWithString`` and 378*61c4878aSAndroid Build Coastguard Worker ``pw::Result<T>``. 379*61c4878aSAndroid Build Coastguard Worker 380*61c4878aSAndroid Build Coastguard Worker The ``DCHECK`` variants only run if ``PW_ASSERT_ENABLE_DEBUG`` is defined; 381*61c4878aSAndroid Build Coastguard Worker otherwise, the entire statement is removed (and the expression not evaluated). 382*61c4878aSAndroid Build Coastguard Worker 383*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 384*61c4878aSAndroid Build Coastguard Worker 385*61c4878aSAndroid Build Coastguard Worker pw::Status operation_status = DoSomeOperation(); 386*61c4878aSAndroid Build Coastguard Worker PW_CHECK_OK(operation_status); 387*61c4878aSAndroid Build Coastguard Worker 388*61c4878aSAndroid Build Coastguard Worker // Any expression that evaluates to a pw::Status or pw_Status works. 389*61c4878aSAndroid Build Coastguard Worker PW_CHECK_OK(DoTheThing(), "System state: %s", SystemState()); 390*61c4878aSAndroid Build Coastguard Worker 391*61c4878aSAndroid Build Coastguard Worker // pw::Result<T> works. 392*61c4878aSAndroid Build Coastguard Worker pw::Result<int> result = GetSomething(); 393*61c4878aSAndroid Build Coastguard Worker PW_CHECK_OK(result); 394*61c4878aSAndroid Build Coastguard Worker 395*61c4878aSAndroid Build Coastguard Worker // C works too. 396*61c4878aSAndroid Build Coastguard Worker pw_Status c_status = DoMoreThings(); 397*61c4878aSAndroid Build Coastguard Worker PW_CHECK_OK(c_status, "System state: %s", SystemState()); 398*61c4878aSAndroid Build Coastguard Worker 399*61c4878aSAndroid Build Coastguard Worker .. note:: 400*61c4878aSAndroid Build Coastguard Worker 401*61c4878aSAndroid Build Coastguard Worker Using ``PW_CHECK_OK(status)`` instead of ``PW_CHECK(status == OkStatus())`` 402*61c4878aSAndroid Build Coastguard Worker enables displaying an error message with a string version of the error 403*61c4878aSAndroid Build Coastguard Worker code; for example ``status == RESOURCE_EXHAUSTED`` instead of ``status == 404*61c4878aSAndroid Build Coastguard Worker 5``. 405*61c4878aSAndroid Build Coastguard Worker 406*61c4878aSAndroid Build Coastguard Worker``%`` in conditions 407*61c4878aSAndroid Build Coastguard Worker=================== 408*61c4878aSAndroid Build Coastguard Worker``PW_CHECK`` conditions cannot contain the ``%`` character (e.g. from the 409*61c4878aSAndroid Build Coastguard Workermodulus operator), since it may be interpreted as a printf-style format 410*61c4878aSAndroid Build Coastguard Workerspecifier. Some backends (:ref:`module-pw_assert_tokenized` in particular) may 411*61c4878aSAndroid Build Coastguard Workerinclude the condition in the format string as a size optimization. 412*61c4878aSAndroid Build Coastguard WorkerUnintentionally introducing an extra %-style argument could lead to problems, so 413*61c4878aSAndroid Build Coastguard Worker``pw_assert`` prevents this. 414*61c4878aSAndroid Build Coastguard Worker 415*61c4878aSAndroid Build Coastguard WorkerUsing a % in a ``PW_CHECK`` condition causes errors like the following: 416*61c4878aSAndroid Build Coastguard Worker 417*61c4878aSAndroid Build Coastguard Worker.. code-block:: none 418*61c4878aSAndroid Build Coastguard Worker 419*61c4878aSAndroid Build Coastguard Worker ../pw_assert/public/pw_assert/internal/check_impl.h:237:7: note: expanded from macro '_PW_CHECK_BINARY_ARG_HANDLER' 420*61c4878aSAndroid Build Coastguard Worker 237 | arg_a_str arg_b_str); /* cannot use '%' here; call mod via a function */ \ 421*61c4878aSAndroid Build Coastguard Worker 422*61c4878aSAndroid Build Coastguard WorkerTo avoid errors like this, do not use ``%`` in ``PW_CHECK`` conditions. Modulus 423*61c4878aSAndroid Build Coastguard Workercan be moved to a separate statement outside of the ``PW_CHECK`` or invoked via 424*61c4878aSAndroid Build Coastguard Workera function or `std::modulus 425*61c4878aSAndroid Build Coastguard Worker<https://en.cppreference.com/w/cpp/utility/functional/modulus>`_. 426*61c4878aSAndroid Build Coastguard Worker 427*61c4878aSAndroid Build Coastguard WorkerThis restriction may be removed in the future (`b/235149326 428*61c4878aSAndroid Build Coastguard Worker<https://issues.pigweed.dev/issues/235149326>`_) 429*61c4878aSAndroid Build Coastguard Worker 430*61c4878aSAndroid Build Coastguard Worker.. _module-pw_assert-assert-api: 431*61c4878aSAndroid Build Coastguard Worker 432*61c4878aSAndroid Build Coastguard Worker---------- 433*61c4878aSAndroid Build Coastguard WorkerAssert API 434*61c4878aSAndroid Build Coastguard Worker---------- 435*61c4878aSAndroid Build Coastguard WorkerThe normal ``PW_CHECK_*`` and ``PW_DCHECK_*`` family of macros are intended to 436*61c4878aSAndroid Build Coastguard Workerprovide rich debug information, like the file, line number, value of operands 437*61c4878aSAndroid Build Coastguard Workerin boolean comparisons, and more. However, this comes at a cost: these macros 438*61c4878aSAndroid Build Coastguard Workerdepend directly on the backend headers, and may perform complicated call-site 439*61c4878aSAndroid Build Coastguard Workertransformations like tokenization. 440*61c4878aSAndroid Build Coastguard Worker 441*61c4878aSAndroid Build Coastguard WorkerThere are several issues with the normal ``PW_CHECK_*`` suite of macros: 442*61c4878aSAndroid Build Coastguard Worker 443*61c4878aSAndroid Build Coastguard Worker1. ``PW_CHECK_*`` in headers can cause ODR violations in the case of tokenized 444*61c4878aSAndroid Build Coastguard Worker asserts, due to differing module choices. 445*61c4878aSAndroid Build Coastguard Worker2. ``PW_CHECK_*`` is not constexpr-safe. 446*61c4878aSAndroid Build Coastguard Worker3. ``PW_CHECK_*`` can cause code bloat with some backends; this is the tradeoff 447*61c4878aSAndroid Build Coastguard Worker to get rich assert information. 448*61c4878aSAndroid Build Coastguard Worker4. ``PW_CHECK_*`` can trigger circular dependencies when asserts are used from 449*61c4878aSAndroid Build Coastguard Worker low-level contexts, like in ``<span>``. 450*61c4878aSAndroid Build Coastguard Worker 451*61c4878aSAndroid Build Coastguard Worker**PW_ASSERT** solves all of the above problems: No risk of ODR violations, are 452*61c4878aSAndroid Build Coastguard Workerconstexpr safe, and have a tiny call site footprint; and there is no header 453*61c4878aSAndroid Build Coastguard Workerdependency on the backend preventing circular include issues. However, there 454*61c4878aSAndroid Build Coastguard Workerare **no format messages, no captured line number, no captured file, no captured 455*61c4878aSAndroid Build Coastguard Workerexpression, or anything other than a binary indication of failure**. 456*61c4878aSAndroid Build Coastguard Worker 457*61c4878aSAndroid Build Coastguard WorkerExample 458*61c4878aSAndroid Build Coastguard Worker======= 459*61c4878aSAndroid Build Coastguard Worker 460*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp 461*61c4878aSAndroid Build Coastguard Worker 462*61c4878aSAndroid Build Coastguard Worker // This example demonstrates asserting in a header. 463*61c4878aSAndroid Build Coastguard Worker 464*61c4878aSAndroid Build Coastguard Worker #include "pw_assert/assert.h" 465*61c4878aSAndroid Build Coastguard Worker 466*61c4878aSAndroid Build Coastguard Worker class InlinedSubsystem { 467*61c4878aSAndroid Build Coastguard Worker public: 468*61c4878aSAndroid Build Coastguard Worker void DoSomething() { 469*61c4878aSAndroid Build Coastguard Worker // GOOD: No problem; PW_ASSERT is fine to inline and place in a header. 470*61c4878aSAndroid Build Coastguard Worker PW_ASSERT(IsEnabled()); 471*61c4878aSAndroid Build Coastguard Worker } 472*61c4878aSAndroid Build Coastguard Worker void DoSomethingElse() { 473*61c4878aSAndroid Build Coastguard Worker // BAD: Generally avoid using PW_DCHECK() or PW_CHECK in headers. If you 474*61c4878aSAndroid Build Coastguard Worker // want rich asserts or logs, move the function into the .cc file, and 475*61c4878aSAndroid Build Coastguard Worker // then use PW_CHECK there. 476*61c4878aSAndroid Build Coastguard Worker PW_DCHECK(IsEnabled()); // DON'T DO THIS 477*61c4878aSAndroid Build Coastguard Worker } 478*61c4878aSAndroid Build Coastguard Worker }; 479*61c4878aSAndroid Build Coastguard Worker 480*61c4878aSAndroid Build Coastguard WorkerPW_ASSERT API Reference 481*61c4878aSAndroid Build Coastguard Worker======================= 482*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_ASSERT(condition) 483*61c4878aSAndroid Build Coastguard Worker 484*61c4878aSAndroid Build Coastguard Worker A header- and constexpr-safe version of ``PW_CHECK()``. 485*61c4878aSAndroid Build Coastguard Worker 486*61c4878aSAndroid Build Coastguard Worker If the given condition is false, crash the system. Otherwise, do nothing. 487*61c4878aSAndroid Build Coastguard Worker The condition is guaranteed to be evaluated. This assert implementation is 488*61c4878aSAndroid Build Coastguard Worker guaranteed to be constexpr-safe. 489*61c4878aSAndroid Build Coastguard Worker 490*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_DASSERT(condition) 491*61c4878aSAndroid Build Coastguard Worker 492*61c4878aSAndroid Build Coastguard Worker A header- and constexpr-safe version of ``PW_DCHECK()``. 493*61c4878aSAndroid Build Coastguard Worker 494*61c4878aSAndroid Build Coastguard Worker Same as ``PW_ASSERT()``, except that if ``PW_ASSERT_ENABLE_DEBUG == 0``, the 495*61c4878aSAndroid Build Coastguard Worker assert is disabled and condition is not evaluated. 496*61c4878aSAndroid Build Coastguard Worker 497*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_ASSERT_OK(expression) 498*61c4878aSAndroid Build Coastguard Worker 499*61c4878aSAndroid Build Coastguard Worker A header- and constexpr-safe version of ``PW_CHECK_OK()``. 500*61c4878aSAndroid Build Coastguard Worker 501*61c4878aSAndroid Build Coastguard Worker If the given expression is not `OK`, crash the system. Otherwise, do nothing. 502*61c4878aSAndroid Build Coastguard Worker The condition is guarenteed to be evaluated. 503*61c4878aSAndroid Build Coastguard Worker 504*61c4878aSAndroid Build Coastguard Worker.. attention:: 505*61c4878aSAndroid Build Coastguard Worker 506*61c4878aSAndroid Build Coastguard Worker Unlike the ``PW_CHECK_*()`` suite of macros, ``PW_ASSERT()`` and 507*61c4878aSAndroid Build Coastguard Worker ``PW_DASSERT()`` capture no rich information like line numbers, the file, 508*61c4878aSAndroid Build Coastguard Worker expression arguments, or the stringified expression. Use these macros **only 509*61c4878aSAndroid Build Coastguard Worker when absolutely necessary**---in headers, constexpr contexts, or in rare cases 510*61c4878aSAndroid Build Coastguard Worker where the call site overhead of a full PW_CHECK must be avoided. 511*61c4878aSAndroid Build Coastguard Worker 512*61c4878aSAndroid Build Coastguard Worker Use ``PW_CHECK_*()`` whenever possible. 513*61c4878aSAndroid Build Coastguard Worker 514*61c4878aSAndroid Build Coastguard WorkerPW_ASSERT API Backend 515*61c4878aSAndroid Build Coastguard Worker===================== 516*61c4878aSAndroid Build Coastguard WorkerThe ``PW_ASSERT`` API ultimately calls the C function 517*61c4878aSAndroid Build Coastguard Worker``pw_assert_HandleFailure()``, which must be provided by the ``pw_assert`` 518*61c4878aSAndroid Build Coastguard Workerbackend. The ``pw_assert_HandleFailure()`` function must not return. 519*61c4878aSAndroid Build Coastguard Worker 520*61c4878aSAndroid Build Coastguard Worker.. _module-pw_assert-circular-deps: 521*61c4878aSAndroid Build Coastguard Worker 522*61c4878aSAndroid Build Coastguard WorkerAvoiding Circular Dependencies With ``PW_ASSERT`` 523*61c4878aSAndroid Build Coastguard Worker================================================= 524*61c4878aSAndroid Build Coastguard WorkerBecause asserts are so widely used, including in low-level libraries, it is 525*61c4878aSAndroid Build Coastguard Workercommon for the ``pw_assert`` backend to cause circular dependencies. Because of 526*61c4878aSAndroid Build Coastguard Workerthis, assert backends may avoid declaring explicit dependencies, instead relying 527*61c4878aSAndroid Build Coastguard Workeron include paths to access header files. 528*61c4878aSAndroid Build Coastguard Worker 529*61c4878aSAndroid Build Coastguard WorkerGN 530*61c4878aSAndroid Build Coastguard Worker-- 531*61c4878aSAndroid Build Coastguard WorkerIn GN, the ``pw_assert`` backend's full implementation with true dependencies is 532*61c4878aSAndroid Build Coastguard Workermade available through the ``$dir_pw_assert:impl`` group. When 533*61c4878aSAndroid Build Coastguard Worker``pw_assert_BACKEND`` is set, ``$dir_pw_assert:impl`` must be listed in the 534*61c4878aSAndroid Build Coastguard Worker``pw_build_LINK_DEPS`` variable. See :ref:`module-pw_build-link-deps`. 535*61c4878aSAndroid Build Coastguard Worker 536*61c4878aSAndroid Build Coastguard WorkerIn the ``pw_assert``, the backend's full implementation is placed in the 537*61c4878aSAndroid Build Coastguard Worker``$pw_assert_BACKEND.impl`` target. ``$dir_pw_assert:impl`` depends on this 538*61c4878aSAndroid Build Coastguard Workerbackend target. The ``$pw_assert_BACKEND.impl`` target may be an empty group if 539*61c4878aSAndroid Build Coastguard Workerthe backend target can use its dependencies directly without causing circular 540*61c4878aSAndroid Build Coastguard Workerdependencies. 541*61c4878aSAndroid Build Coastguard Worker 542*61c4878aSAndroid Build Coastguard WorkerIn order to break dependency cycles, the ``pw_assert_BACKEND`` target may need 543*61c4878aSAndroid Build Coastguard Workerto directly provide dependencies through include paths only, rather than GN 544*61c4878aSAndroid Build Coastguard Worker``public_deps``. In this case, GN header checking can be disabled with 545*61c4878aSAndroid Build Coastguard Worker``check_includes = false``. 546*61c4878aSAndroid Build Coastguard Worker 547*61c4878aSAndroid Build Coastguard WorkerBazel 548*61c4878aSAndroid Build Coastguard Worker----- 549*61c4878aSAndroid Build Coastguard WorkerIn Bazel, assert backends may break dependency cycles by placing the full 550*61c4878aSAndroid Build Coastguard Workerimplementation in an ``impl`` target, like ``//pw_assert_basic:impl`` or 551*61c4878aSAndroid Build Coastguard Worker``//pw_assert_tokenized:impl``. The ``//pw_assert:backend_impl`` label flag 552*61c4878aSAndroid Build Coastguard Workershould be set to the ``impl`` target required by the assert backend used by the 553*61c4878aSAndroid Build Coastguard Workerplatform. 554*61c4878aSAndroid Build Coastguard Worker 555*61c4878aSAndroid Build Coastguard WorkerYou must add a dependency on the ``@pigweed//pw_assert:backend_impl`` target to 556*61c4878aSAndroid Build Coastguard Workerany binary using ``pw_assert``. 557*61c4878aSAndroid Build Coastguard Worker 558*61c4878aSAndroid Build Coastguard WorkerSee :ref:`docs-build_system-bazel_link-extra-lib` for a general discussion of 559*61c4878aSAndroid Build Coastguard Workercyclic dependencies in low-level libraries in Bazel. 560*61c4878aSAndroid Build Coastguard Worker 561*61c4878aSAndroid Build Coastguard Worker.. _module-pw_assert-backend_api: 562*61c4878aSAndroid Build Coastguard Worker 563*61c4878aSAndroid Build Coastguard Worker----------- 564*61c4878aSAndroid Build Coastguard WorkerBackend API 565*61c4878aSAndroid Build Coastguard Worker----------- 566*61c4878aSAndroid Build Coastguard WorkerThe backend controls what to do in the case of an assertion failure. In the 567*61c4878aSAndroid Build Coastguard Workermost basic cases, the backend could display the assertion failure on something 568*61c4878aSAndroid Build Coastguard Workerlike sys_io and halt in a while loop waiting for a debugger. In other cases, 569*61c4878aSAndroid Build Coastguard Workerthe backend could store crash details like the current thread's stack to flash. 570*61c4878aSAndroid Build Coastguard Worker 571*61c4878aSAndroid Build Coastguard WorkerThis facade module (``pw_assert``) does not provide a backend. See 572*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_assert_basic` for a basic implementation. 573*61c4878aSAndroid Build Coastguard Worker 574*61c4878aSAndroid Build Coastguard Worker.. attention:: 575*61c4878aSAndroid Build Coastguard Worker 576*61c4878aSAndroid Build Coastguard Worker The facade macros (``PW_CRASH`` and related) are expected to behave like they 577*61c4878aSAndroid Build Coastguard Worker have the ``[[noreturn]]`` attribute set. This implies that the backend handler 578*61c4878aSAndroid Build Coastguard Worker functions, ``PW_HANDLE_*`` defined by the backend, must not return. 579*61c4878aSAndroid Build Coastguard Worker 580*61c4878aSAndroid Build Coastguard Worker In other words, the device must reboot. 581*61c4878aSAndroid Build Coastguard Worker 582*61c4878aSAndroid Build Coastguard WorkerThe backend must provide the header 583*61c4878aSAndroid Build Coastguard Worker 584*61c4878aSAndroid Build Coastguard Worker``pw_assert_backend/check_backend.h`` 585*61c4878aSAndroid Build Coastguard Worker 586*61c4878aSAndroid Build Coastguard Workerand that header must define the following macros: 587*61c4878aSAndroid Build Coastguard Worker 588*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_HANDLE_CRASH(message, ...) 589*61c4878aSAndroid Build Coastguard Worker 590*61c4878aSAndroid Build Coastguard Worker Trigger a system crash or halt, and if possible, deliver the specified 591*61c4878aSAndroid Build Coastguard Worker message and arguments to the user or developer. 592*61c4878aSAndroid Build Coastguard Worker 593*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_HANDLE_ASSERT_FAILURE(condition_str, message, ...) 594*61c4878aSAndroid Build Coastguard Worker 595*61c4878aSAndroid Build Coastguard Worker Trigger a system crash or halt, and if possible, deliver the condition string 596*61c4878aSAndroid Build Coastguard Worker (indicating what expression was false) and the message with format arguments, 597*61c4878aSAndroid Build Coastguard Worker to the user or developer. 598*61c4878aSAndroid Build Coastguard Worker 599*61c4878aSAndroid Build Coastguard Worker This macro is invoked from the ``PW_CHECK`` facade macro if condition is 600*61c4878aSAndroid Build Coastguard Worker false. 601*61c4878aSAndroid Build Coastguard Worker 602*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE( \ 603*61c4878aSAndroid Build Coastguard Worker a_str, a_val, op_str, b_str, b_val, type_fmt, message, ...) 604*61c4878aSAndroid Build Coastguard Worker 605*61c4878aSAndroid Build Coastguard Worker Trigger a system crash or halt for a failed binary comparison assert (e.g. 606*61c4878aSAndroid Build Coastguard Worker any of the ``PW_CHECK_<type>_<op>`` macros). The handler should combine the 607*61c4878aSAndroid Build Coastguard Worker assert components into a useful message for the user; though in some cases 608*61c4878aSAndroid Build Coastguard Worker this may not be possible. 609*61c4878aSAndroid Build Coastguard Worker 610*61c4878aSAndroid Build Coastguard Worker Consider the following example: 611*61c4878aSAndroid Build Coastguard Worker 612*61c4878aSAndroid Build Coastguard Worker .. code-block:: cpp 613*61c4878aSAndroid Build Coastguard Worker 614*61c4878aSAndroid Build Coastguard Worker int temp = 16; 615*61c4878aSAndroid Build Coastguard Worker int max_temp = 15; 616*61c4878aSAndroid Build Coastguard Worker PW_CHECK_INT_LE(temp, MAX_TEMP, "Got too hot; state: %s", GetSystemState()); 617*61c4878aSAndroid Build Coastguard Worker 618*61c4878aSAndroid Build Coastguard Worker In this block, the assert will trigger, which will cause the facade to invoke 619*61c4878aSAndroid Build Coastguard Worker the handler macro. Below is the meaning of the arguments, referencing to the 620*61c4878aSAndroid Build Coastguard Worker example: 621*61c4878aSAndroid Build Coastguard Worker 622*61c4878aSAndroid Build Coastguard Worker - ``a_str`` - Stringified first operand. In the example: ``"temp"``. 623*61c4878aSAndroid Build Coastguard Worker - ``a_val`` - The value of the first operand. In the example: ``16``. 624*61c4878aSAndroid Build Coastguard Worker - ``op_str`` - The string version of the operator. In the example: "<=". 625*61c4878aSAndroid Build Coastguard Worker - ``b_str`` - Stringified second operand. In the example: ``"max_temp"``. 626*61c4878aSAndroid Build Coastguard Worker - ``b_val`` - The value of the second operand. In the example: ``15``. 627*61c4878aSAndroid Build Coastguard Worker - ``type_fmt`` - The format code for the type. In the example: ``"%d"``. 628*61c4878aSAndroid Build Coastguard Worker - ``message, ...`` - A formatted message to go with the assert. In the 629*61c4878aSAndroid Build Coastguard Worker example: ``"Got too hot; state: %s", "ON_FIRE"``. 630*61c4878aSAndroid Build Coastguard Worker 631*61c4878aSAndroid Build Coastguard Worker .. tip:: 632*61c4878aSAndroid Build Coastguard Worker 633*61c4878aSAndroid Build Coastguard Worker See :ref:`module-pw_assert_basic` for one way to combine these arguments 634*61c4878aSAndroid Build Coastguard Worker into a meaningful error message. 635*61c4878aSAndroid Build Coastguard Worker 636*61c4878aSAndroid Build Coastguard WorkerAdditionally, the backend must provide a link-time function for the 637*61c4878aSAndroid Build Coastguard Worker``PW_ASSERT`` assert handler. This does not need to appear in the backend 638*61c4878aSAndroid Build Coastguard Workerheader, but instead is in a ``.cc`` file. 639*61c4878aSAndroid Build Coastguard Worker 640*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: pw_assert_HandleFailure() 641*61c4878aSAndroid Build Coastguard Worker 642*61c4878aSAndroid Build Coastguard Worker Handle a low-level crash. This crash entry happens through 643*61c4878aSAndroid Build Coastguard Worker ``pw_assert/assert.h``. In this crash handler, there is no access to line, 644*61c4878aSAndroid Build Coastguard Worker file, expression, or other rich assert information. Backends should do 645*61c4878aSAndroid Build Coastguard Worker something reasonable in this case; typically, capturing the stack is useful. 646*61c4878aSAndroid Build Coastguard Worker 647*61c4878aSAndroid Build Coastguard WorkerBackend Build Targets 648*61c4878aSAndroid Build Coastguard Worker===================== 649*61c4878aSAndroid Build Coastguard WorkerIn GN, the backend must provide a ``pw_assert.impl`` build target in the same 650*61c4878aSAndroid Build Coastguard Workerdirectory as the backend target. If the main backend target's dependencies would 651*61c4878aSAndroid Build Coastguard Workercause dependency cycles, the actual backend implementation with its full 652*61c4878aSAndroid Build Coastguard Workerdependencies is placed in the ``pw_assert.impl`` target. If this is not 653*61c4878aSAndroid Build Coastguard Workernecessary, ``pw_assert.impl`` can be an empty group. Circular dependencies are a 654*61c4878aSAndroid Build Coastguard Workercommon problem with ``pw_assert`` because it is so widely used. See 655*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_assert-circular-deps`. 656*61c4878aSAndroid Build Coastguard Worker 657*61c4878aSAndroid Build Coastguard WorkerMacro-based PW_ASSERT()/PW_DASSERT() backend 658*61c4878aSAndroid Build Coastguard Worker============================================ 659*61c4878aSAndroid Build Coastguard WorkerThe pw_assert API is being re-assessed to provide more helpful information in 660*61c4878aSAndroid Build Coastguard Workercontexts where ``PW_CHECK_*()`` macros cannot be used. A first step towards this 661*61c4878aSAndroid Build Coastguard Workeris providing a macro-based backend API for the ``PW_ASSERT()`` and 662*61c4878aSAndroid Build Coastguard Worker``PW_DASSERT()`` macros. 663*61c4878aSAndroid Build Coastguard Worker 664*61c4878aSAndroid Build Coastguard Worker.. warning:: 665*61c4878aSAndroid Build Coastguard Worker This part of ``pw_assert``'s API is transitional, and any project-specific 666*61c4878aSAndroid Build Coastguard Worker reliance on any of the API mentioned here will likely experience breakages. 667*61c4878aSAndroid Build Coastguard Worker In particular, ``PW_ASSERT_HANDLE_FAILURE`` and ``PW_HANDLE_ASSERT_FAILURE`` 668*61c4878aSAndroid Build Coastguard Worker are extremely confusingly similar and are NOT interchangeable. 669*61c4878aSAndroid Build Coastguard Worker 670*61c4878aSAndroid Build Coastguard WorkerA macro-based backend for the ``PW_ASSERT()`` macros must provide the following 671*61c4878aSAndroid Build Coastguard Workermacro in a header at ``pw_assert_backend/assert_backend.h``. 672*61c4878aSAndroid Build Coastguard Worker 673*61c4878aSAndroid Build Coastguard Worker.. cpp:function:: PW_ASSERT_HANDLE_FAILURE(expression) 674*61c4878aSAndroid Build Coastguard Worker 675*61c4878aSAndroid Build Coastguard Worker Handle a low-level crash. This crash entry happens through 676*61c4878aSAndroid Build Coastguard Worker ``pw_assert/assert.h``. Backends must ensure their implementation is safe for 677*61c4878aSAndroid Build Coastguard Worker usage in headers, constexpr contexts, and templates. This macro should expand 678*61c4878aSAndroid Build Coastguard Worker to an expression that does not return. 679*61c4878aSAndroid Build Coastguard Worker 680*61c4878aSAndroid Build Coastguard WorkerSimilar to the ``PW_CHECK_*()`` facade, the header backend that provides an 681*61c4878aSAndroid Build Coastguard Workerexpansion for the ``PW_ASSERT_HANDLE_FAILURE()`` macro can be controlled in the 682*61c4878aSAndroid Build Coastguard WorkerGN build using the ``pw_assert_LITE_BACKEND`` build argument. In addition to 683*61c4878aSAndroid Build Coastguard Workerthe header-based target at ``${pw_assert_LITE_BACKEND}``, a source set at 684*61c4878aSAndroid Build Coastguard Worker``${pw_assert_LITE_BACKEND}.impl`` is also required as a way to reduce the 685*61c4878aSAndroid Build Coastguard Workerimpact of :ref:`circular dependencies <module-pw_assert-circular-deps>`. 686*61c4878aSAndroid Build Coastguard Worker 687*61c4878aSAndroid Build Coastguard Worker-------------------------- 688*61c4878aSAndroid Build Coastguard WorkerFrequently Asked Questions 689*61c4878aSAndroid Build Coastguard Worker-------------------------- 690*61c4878aSAndroid Build Coastguard Worker 691*61c4878aSAndroid Build Coastguard WorkerWhen should DCHECK_* be used instead of CHECK_* and vice versa? 692*61c4878aSAndroid Build Coastguard Worker=============================================================== 693*61c4878aSAndroid Build Coastguard WorkerThere is no hard and fast rule for when to use one or the other. 694*61c4878aSAndroid Build Coastguard Worker 695*61c4878aSAndroid Build Coastguard WorkerIn theory, ``DCHECK_*`` macros should never be used and all the asserts should 696*61c4878aSAndroid Build Coastguard Workerremain active in production. In practice, **assert statements come at a binary 697*61c4878aSAndroid Build Coastguard Workersize and runtime cost**, even when using extensions like a tokenized assert 698*61c4878aSAndroid Build Coastguard Workerbackend that strips the stringified assert expression from the binary. Each 699*61c4878aSAndroid Build Coastguard Workerassert is **at least a branch with a function call**; depending on the assert 700*61c4878aSAndroid Build Coastguard Workerbackend, that function call may take several arguments (like the message, the 701*61c4878aSAndroid Build Coastguard Workerfile line number, the module, etc). These function calls can take 10-20 bytes 702*61c4878aSAndroid Build Coastguard Workeror more of ROM each. Thus, there is a balance to be struct between ``DCHECK_*`` 703*61c4878aSAndroid Build Coastguard Workerand ``CHECK_*``. 704*61c4878aSAndroid Build Coastguard Worker 705*61c4878aSAndroid Build Coastguard WorkerPigweed uses these conventions to decide between ``CHECK_*`` and ``DCHECK_*``: 706*61c4878aSAndroid Build Coastguard Worker 707*61c4878aSAndroid Build Coastguard Worker- **Prefer to use CHECK_* at public API boundaries** of modules, where an 708*61c4878aSAndroid Build Coastguard Worker invalid value is a clear programmer bug. In certain cases use ``DCHECK_*`` to 709*61c4878aSAndroid Build Coastguard Worker keep binary size small when in production; for example, in modules with a 710*61c4878aSAndroid Build Coastguard Worker large public API surface, or modules with many inlined functions in headers. 711*61c4878aSAndroid Build Coastguard Worker- **Avoid using CHECK_* macros in headers.** It is still OK to use ``CHECK_*`` 712*61c4878aSAndroid Build Coastguard Worker macros in headers, but carefully consider the cost, since inlined use of the 713*61c4878aSAndroid Build Coastguard Worker ``CHECK_*`` macros in headers will expand to the full assert cost for every 714*61c4878aSAndroid Build Coastguard Worker translation unit that includes the header and calls the function with the 715*61c4878aSAndroid Build Coastguard Worker ``CHECK_*`` instance. ``DCHECK_*`` macros are are better, but even they come 716*61c4878aSAndroid Build Coastguard Worker at a cost, since it is preferable to be able to compile a binary in debug 717*61c4878aSAndroid Build Coastguard Worker mode for as long as possible on the road to production. 718*61c4878aSAndroid Build Coastguard Worker- **Prefer to use DCHECK_* variants for internal asserts** that attempt to 719*61c4878aSAndroid Build Coastguard Worker catch module-author level programming errors. For example, use DCHECKs to 720*61c4878aSAndroid Build Coastguard Worker verify internal function preconditions, or other invariants that should 721*61c4878aSAndroid Build Coastguard Worker always be true but will likely never fire in production. In some cases using 722*61c4878aSAndroid Build Coastguard Worker ``CHECK_*`` macros for internal consistency checking can make sense, if the 723*61c4878aSAndroid Build Coastguard Worker runtime cost is low and there are only a couple of instances. 724*61c4878aSAndroid Build Coastguard Worker 725*61c4878aSAndroid Build Coastguard Worker.. tip:: 726*61c4878aSAndroid Build Coastguard Worker 727*61c4878aSAndroid Build Coastguard Worker **Do not return error status codes for obvious API misuse** 728*61c4878aSAndroid Build Coastguard Worker 729*61c4878aSAndroid Build Coastguard Worker Returning an error code may **mask the earliest sign of a bug** because 730*61c4878aSAndroid Build Coastguard Worker notifying the developer of the problem depends on correct propagation of the 731*61c4878aSAndroid Build Coastguard Worker error to upper levels of the system. Instead, prefer to use the ``CHECK_*`` 732*61c4878aSAndroid Build Coastguard Worker or ``DCHECK_*`` macros to ensure a prompt termination and warning to the 733*61c4878aSAndroid Build Coastguard Worker developer. 734*61c4878aSAndroid Build Coastguard Worker 735*61c4878aSAndroid Build Coastguard Worker **Error status codes should be reserved for system misbehaviour or expected 736*61c4878aSAndroid Build Coastguard Worker exceptional cases**, like a sensor is not yet ready, or a storage subsystem 737*61c4878aSAndroid Build Coastguard Worker is full when writing. Doing ``CHECK_*`` assertions in those cases would be a 738*61c4878aSAndroid Build Coastguard Worker mistake; so use error codes in those cases instead. 739*61c4878aSAndroid Build Coastguard Worker 740*61c4878aSAndroid Build Coastguard WorkerHow should objects be asserted against or compared? 741*61c4878aSAndroid Build Coastguard Worker=================================================== 742*61c4878aSAndroid Build Coastguard WorkerUnfortunately, there is no native mechanism for this, and instead the way to 743*61c4878aSAndroid Build Coastguard Workerassert object states or comparisons is with the normal ``PW_CHECK_*`` macros 744*61c4878aSAndroid Build Coastguard Workerthat operate on booleans, ints, and floats. 745*61c4878aSAndroid Build Coastguard Worker 746*61c4878aSAndroid Build Coastguard WorkerThis is due to the requirement of supporting C and also tokenization. It may be 747*61c4878aSAndroid Build Coastguard Workerpossible support rich object comparisons by defining a convention for 748*61c4878aSAndroid Build Coastguard Workerstringifying objects; however, this hasn't been added yet. Additionally, such a 749*61c4878aSAndroid Build Coastguard Workermechanism would not work well with tokenization. In particular, it would 750*61c4878aSAndroid Build Coastguard Workerrequire runtime stringifying arguments and rendering them with ``%s``, which 751*61c4878aSAndroid Build Coastguard Workerleads to binary bloat even with tokenization. So it is likely that a rich 752*61c4878aSAndroid Build Coastguard Workerobject assert API won't be added. 753*61c4878aSAndroid Build Coastguard Worker 754*61c4878aSAndroid Build Coastguard WorkerWhy was the assert facade designed this way? 755*61c4878aSAndroid Build Coastguard Worker============================================ 756*61c4878aSAndroid Build Coastguard WorkerThe Pigweed assert API was designed taking into account the needs of several 757*61c4878aSAndroid Build Coastguard Workerpast projects the team members were involved with. Based on those experiences, 758*61c4878aSAndroid Build Coastguard Workerthe following were key requirements for the API: 759*61c4878aSAndroid Build Coastguard Worker 760*61c4878aSAndroid Build Coastguard Worker1. **C compatibility** - Since asserts are typically invoked from arbitrary 761*61c4878aSAndroid Build Coastguard Worker contexts, including from vendor or third party code, the assert system must 762*61c4878aSAndroid Build Coastguard Worker have a C-compatible API. Some API functions working only in C++ is 763*61c4878aSAndroid Build Coastguard Worker acceptable, as long as the key functions work in C. 764*61c4878aSAndroid Build Coastguard Worker2. **Capturing both expressions and values** - Since asserts can trigger in 765*61c4878aSAndroid Build Coastguard Worker ways that are not repeatable, it is important to capture rich diagnostic 766*61c4878aSAndroid Build Coastguard Worker information to help identifying the root cause of the fault. For asserts, 767*61c4878aSAndroid Build Coastguard Worker this means including the failing expression text, and optionally also 768*61c4878aSAndroid Build Coastguard Worker capturing failing expression values. For example, instead of capturing an 769*61c4878aSAndroid Build Coastguard Worker error with the expression (``x < y``), capturing an error with the 770*61c4878aSAndroid Build Coastguard Worker expression and values(``x < y, with x = 10, y = 0``). 771*61c4878aSAndroid Build Coastguard Worker3. **Tokenization compatible** - It's important that the assert expressions 772*61c4878aSAndroid Build Coastguard Worker support tokenization; both the expression itself (e.g. ``a < b``) and the 773*61c4878aSAndroid Build Coastguard Worker message attached to the expression. For example: ``PW_CHECK(ItWorks(), "Ruh 774*61c4878aSAndroid Build Coastguard Worker roh: %d", some_int)``. 775*61c4878aSAndroid Build Coastguard Worker4. **Customizable assert handling** - Most products need to support custom 776*61c4878aSAndroid Build Coastguard Worker handling of asserts. In some cases, an assert might trigger printing out 777*61c4878aSAndroid Build Coastguard Worker details to a UART; in other cases, it might trigger saving a log entry to 778*61c4878aSAndroid Build Coastguard Worker flash. The assert system must support this customization. 779*61c4878aSAndroid Build Coastguard Worker 780*61c4878aSAndroid Build Coastguard WorkerThe combination of #1, #2, and #3 led to the structure of the API. In 781*61c4878aSAndroid Build Coastguard Workerparticular, the need to support tokenized asserts and the need to support 782*61c4878aSAndroid Build Coastguard Workercapturing values led to the choice of having ``PW_CHECK_INT_LE(a, b)`` instead 783*61c4878aSAndroid Build Coastguard Workerof ``PW_CHECK(a <= b)``. Needing to support tokenization is what drove the 784*61c4878aSAndroid Build Coastguard Workerfacade & backend arrangement, since the backend must provide the raw macros for 785*61c4878aSAndroid Build Coastguard Workerasserting in that case, rather than terminating at a C-style API. 786*61c4878aSAndroid Build Coastguard Worker 787*61c4878aSAndroid Build Coastguard WorkerWhy isn't there a ``PW_CHECK_LE``? Why is the type (e.g. ``INT``) needed? 788*61c4878aSAndroid Build Coastguard Worker========================================================================= 789*61c4878aSAndroid Build Coastguard WorkerThe problem with asserts like ``PW_CHECK_LE(a, b)`` instead of 790*61c4878aSAndroid Build Coastguard Worker``PW_CHECK_INT_LE(a, b)`` or ``PW_CHECK_FLOAT_EXACT_LE(a, b)`` is that to 791*61c4878aSAndroid Build Coastguard Workercapture the arguments with the tokenizer, we need to know the types. Using the 792*61c4878aSAndroid Build Coastguard Workerpreprocessor, it is impossible to dispatch based on the types of ``a`` and 793*61c4878aSAndroid Build Coastguard Worker``b``, so unfortunately having a separate macro for each of the types commonly 794*61c4878aSAndroid Build Coastguard Workerasserted on is necessary. 795*61c4878aSAndroid Build Coastguard Worker 796*61c4878aSAndroid Build Coastguard Worker---------------------------- 797*61c4878aSAndroid Build Coastguard WorkerModule Configuration Options 798*61c4878aSAndroid Build Coastguard Worker---------------------------- 799*61c4878aSAndroid Build Coastguard WorkerThe following configurations can be adjusted via compile-time configuration of 800*61c4878aSAndroid Build Coastguard Workerthis module, see the 801*61c4878aSAndroid Build Coastguard Worker:ref:`module documentation <module-structure-compile-time-configuration>` for 802*61c4878aSAndroid Build Coastguard Workermore details. 803*61c4878aSAndroid Build Coastguard Worker 804*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_ASSERT_ENABLE_DEBUG 805*61c4878aSAndroid Build Coastguard Worker 806*61c4878aSAndroid Build Coastguard Worker Controls whether ``DCHECK`` and ``DASSERT`` are enabled. 807*61c4878aSAndroid Build Coastguard Worker 808*61c4878aSAndroid Build Coastguard Worker This defaults to being disabled if ``NDEBUG`` is defined, else it is enabled 809*61c4878aSAndroid Build Coastguard Worker by default. 810*61c4878aSAndroid Build Coastguard Worker 811*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_ASSERT_CAPTURE_VALUES 812*61c4878aSAndroid Build Coastguard Worker 813*61c4878aSAndroid Build Coastguard Worker Controls whether the evaluated values of a CHECK statement are captured as 814*61c4878aSAndroid Build Coastguard Worker arguments to the final string. Disabling this will reduce code size at CHECK 815*61c4878aSAndroid Build Coastguard Worker callsites, but slightly reduces debugability. 816*61c4878aSAndroid Build Coastguard Worker 817*61c4878aSAndroid Build Coastguard Worker This defaults to enabled. 818*61c4878aSAndroid Build Coastguard Worker 819*61c4878aSAndroid Build Coastguard Worker------------- 820*61c4878aSAndroid Build Coastguard WorkerCompatibility 821*61c4878aSAndroid Build Coastguard Worker------------- 822*61c4878aSAndroid Build Coastguard WorkerThe facade is compatible with both C and C++. 823*61c4878aSAndroid Build Coastguard Worker 824*61c4878aSAndroid Build Coastguard Worker--------------------------------------- 825*61c4878aSAndroid Build Coastguard WorkerC Standard Library `assert` Replacement 826*61c4878aSAndroid Build Coastguard Worker--------------------------------------- 827*61c4878aSAndroid Build Coastguard WorkerAn optional replacement of the C standard Library's `assert` macro is provided 828*61c4878aSAndroid Build Coastguard Workerthrough the `libc_assert` target which fully implements replacement `assert.h` 829*61c4878aSAndroid Build Coastguard Workerand `cassert` headers using `PW_ASSERT`. While this is effective for porting 830*61c4878aSAndroid Build Coastguard Workerexternal code to microcontrollers, we do not advise embedded projects use the 831*61c4878aSAndroid Build Coastguard Worker`assert` macro unless absolutely necessary. 832*61c4878aSAndroid Build Coastguard Worker 833*61c4878aSAndroid Build Coastguard Worker---------------- 834*61c4878aSAndroid Build Coastguard WorkerRoadmap & Status 835*61c4878aSAndroid Build Coastguard Worker---------------- 836*61c4878aSAndroid Build Coastguard WorkerThe Pigweed assert subsystem consiststs of several modules that work in 837*61c4878aSAndroid Build Coastguard Workercoordination. This module is the facade (API), then a number of backends are 838*61c4878aSAndroid Build Coastguard Workeravailable to handle assert failures. Products can also define their own 839*61c4878aSAndroid Build Coastguard Workerbackends. In some cases, the backends will have backends (like 840*61c4878aSAndroid Build Coastguard Worker``pw_log_tokenized``). 841*61c4878aSAndroid Build Coastguard Worker 842*61c4878aSAndroid Build Coastguard WorkerBelow is a brief summary of what modules are ready for use: 843*61c4878aSAndroid Build Coastguard Worker 844*61c4878aSAndroid Build Coastguard WorkerAvailable Assert Backends 845*61c4878aSAndroid Build Coastguard Worker========================= 846*61c4878aSAndroid Build Coastguard WorkerSee :ref:`module-pw_assert-backends`. 847*61c4878aSAndroid Build Coastguard Worker 848*61c4878aSAndroid Build Coastguard WorkerMissing Functionality 849*61c4878aSAndroid Build Coastguard Worker===================== 850*61c4878aSAndroid Build Coastguard Worker- **Stack traces** - Pigweed doesn't have a reliable stack walker, which makes 851*61c4878aSAndroid Build Coastguard Worker displaying a stack trace on crash harder. We plan to add this eventually. 852*61c4878aSAndroid Build Coastguard Worker- **Snapshot integration** - Pigweed doesn't yet have a rich system state 853*61c4878aSAndroid Build Coastguard Worker capture system that can capture state like number of tasks, available memory, 854*61c4878aSAndroid Build Coastguard Worker and so on. Snapshot facilities are the obvious ones to run inside an assert 855*61c4878aSAndroid Build Coastguard Worker handler. It'll happen someday. 856*61c4878aSAndroid Build Coastguard Worker 857*61c4878aSAndroid Build Coastguard Worker.. toctree:: 858*61c4878aSAndroid Build Coastguard Worker :maxdepth: 1 859*61c4878aSAndroid Build Coastguard Worker 860*61c4878aSAndroid Build Coastguard Worker Backends <backends> 861