1 // Copyright 2020 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include "pw_assert/config.h" // For PW_ASSERT_ENABLE_DEBUG 17 #include "pw_assert_backend/assert_backend.h" 18 19 // A header- and constexpr-safe version of PW_CHECK(). 20 // 21 // If the given condition is false, crash the system. Otherwise, do nothing. 22 // The condition is guaranteed to be evaluated. 23 // 24 // IMPORTANT: Unlike the PW_CHECK_*() suite of macros, not all backends for 25 // this API capture rich information like line numbers, the file, expression 26 // arguments, or the stringified expression. Use these macros only when 27 // absolutely necessary -- in headers, constexpr contexts, or in rare cases 28 // where the call site overhead of a full PW_CHECK must be avoided. Use 29 // PW_CHECK_*() whenever possible. 30 #define PW_ASSERT(condition) \ 31 do { \ 32 if (!(condition)) { \ 33 PW_ASSERT_HANDLE_FAILURE(#condition); \ 34 } \ 35 } while (0) 36 37 // A header- and constexpr-safe version of PW_DCHECK(). 38 // 39 // Same as PW_ASSERT(), except that if PW_ASSERT_ENABLE_DEBUG == 1, the assert 40 // is disabled and condition is not evaluated. 41 // 42 // IMPORTANT: Unlike the PW_CHECK_*() suite of macros, not all backends for 43 // this API capture rich information like line numbers, the file, expression 44 // arguments, or the stringified expression. Use these macros only when 45 // absolutely necessary -- in headers, constexpr contexts, or in rare cases 46 // where the call site overhead of a full PW_CHECK must be avoided. Use 47 // PW_CHECK_*() whenever possible. 48 #define PW_DASSERT(condition) \ 49 do { \ 50 if ((PW_ASSERT_ENABLE_DEBUG == 1) && !(condition)) { \ 51 PW_ASSERT_HANDLE_FAILURE(#condition); \ 52 } \ 53 } while (0) 54 55 // A header- and constexpr-safe version of PW_CHECK_OK(). 56 // 57 // If the condition does not evaluate to PW_STATUS_OK, crash. 58 // Otherwise, do nothing. The expression is guaranteed to be evaluated. 59 // 60 // Unlike `PW_CHECK_OK`, this macro does not currently log the failed status 61 // kind. 62 // 63 // IMPORTANT: Unlike the PW_CHECK_*() suite of macros, not all backends for 64 // this API capture rich information like line numbers, the file, expression 65 // arguments, or the stringified expression. Use these macros only when 66 // absolutely necessary -- in headers, constexpr contexts, or in rare cases 67 // where the call site overhead of a full PW_CHECK must be avoided. Use 68 // PW_CHECK_*() whenever possible. 69 #define PW_ASSERT_OK(expression, ...) \ 70 do { \ 71 const _PW_ASSERT_OK_STATUS _pw_assert_ok_status = (expression); \ 72 if (_pw_assert_ok_status != PW_STATUS_OK) { \ 73 PW_ASSERT_HANDLE_FAILURE(#expression); \ 74 } \ 75 } while (0) 76 77 #ifdef __cplusplus 78 #define _PW_ASSERT_OK_STATUS ::pw::Status 79 #else 80 #define _PW_ASSERT_OK_STATUS pw_Status 81 #endif // __cplusplus 82