• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/options.h"  // For PW_ASSERT_ENABLE_DEBUG
17 #include "pw_preprocessor/util.h"
18 
19 // For backwards compatibility, include check.h from assert.h.
20 // TODO(pwbug/350): Remove this include when projects have migrated.
21 #include "pw_assert/check.h"
22 
23 PW_EXTERN_C_START
24 
25 void pw_assert_HandleFailure(void);
26 
27 PW_EXTERN_C_END
28 
29 // A header- and constexpr-safe version of PW_CHECK().
30 //
31 // If the given condition is false, crash the system. Otherwise, do nothing.
32 // The condition is guaranteed to be evaluated. This assert implementation is
33 // guaranteed to be constexpr-safe.
34 //
35 // IMPORTANT: Unlike the PW_CHECK_*() suite of macros, this API captures no
36 // rich information like line numbers, the file, expression arguments, or the
37 // stringified expression. Use these macros only when absolutely necessary --
38 // in headers, constexr contexts, or in rare cases where the call site overhead
39 // of a full PW_CHECK must be avoided. Use PW_CHECK_*() whenever possible.
40 #define PW_ASSERT(condition)     \
41   do {                           \
42     if (!(condition)) {          \
43       pw_assert_HandleFailure(); \
44     }                            \
45   } while (0)
46 
47 // A header- and constexpr-safe version of PW_DCHECK().
48 //
49 // Same as PW_ASSERT(), except that if PW_ASSERT_ENABLE_DEBUG == 1, the assert
50 // is disabled and condition is not evaluated.
51 //
52 // IMPORTANT: Unlike the PW_CHECK_*() suite of macros, this API captures no
53 // rich information like line numbers, the file, expression arguments, or the
54 // stringified expression. Use these macros only when absolutely necessary --
55 // in headers, constexr contexts, or in rare cases where the call site overhead
56 // of a full PW_CHECK must be avoided. Use PW_DCHECK_*() whenever possible.
57 #define PW_DASSERT(condition)                            \
58   do {                                                   \
59     if ((PW_ASSERT_ENABLE_DEBUG == 1) && !(condition)) { \
60       pw_assert_HandleFailure();                         \
61     }                                                    \
62   } while (0)
63