xref: /aosp_15_r20/external/pigweed/pw_assert_basic/public/pw_assert_basic/assert_basic.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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_basic/handler.h"
17 #include "pw_preprocessor/arguments.h"
18 #include "pw_preprocessor/compiler.h"
19 #include "pw_preprocessor/util.h"
20 
21 // Use __PRETTY_FUNCTION__, a GNU extension, in place of the __func__ macro when
22 // supported. __PRETTY_FUNCTION__ expands to the full C++ function name.
23 #ifdef __GNUC__
24 #define _PW_ASSERT_BASIC_FUNCTION_NAME __PRETTY_FUNCTION__
25 #else
26 #define _PW_ASSERT_BASIC_FUNCTION_NAME __func__
27 #endif  // __GNUC__
28 
29 // Die with a message with many attributes included. This is the crash macro
30 // frontend that funnels everything into the C handler provided by the user,
31 // pw_assert_basic_HandleFailure().
32 #define PW_HANDLE_CRASH(...)     \
33   pw_assert_basic_HandleFailure( \
34       __FILE__,                  \
35       __LINE__,                  \
36       _PW_ASSERT_BASIC_FUNCTION_NAME PW_COMMA_ARGS(__VA_ARGS__))
37 
38 // Die with a message with many attributes included. This is the crash macro
39 // frontend that funnels everything into the C handler provided by the user,
40 // pw_assert_basic_HandleFailure().
41 #define PW_HANDLE_ASSERT_FAILURE(condition_string, message, ...)  \
42   pw_assert_basic_HandleFailure(__FILE__,                         \
43                                 __LINE__,                         \
44                                 _PW_ASSERT_BASIC_FUNCTION_NAME,   \
45                                 "Check failed: " condition_string \
46                                 ". " message PW_COMMA_ARGS(__VA_ARGS__))
47 
48 // Sample assert failure message produced by the below implementation:
49 //
50 //   Check failed: current_sensor (=610) < new_sensor (=50). More details!
51 //
52 // Putting the value next to the operand makes the string easier to read.
53 
54 // clang-format off
55 // This is too hairy for clang format to handle and retain readability.
56 #define PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE(arg_a_str,         \
57                                                 arg_a_val,         \
58                                                 comparison_op_str, \
59                                                 arg_b_str,         \
60                                                 arg_b_val,         \
61                                                 type_fmt,          \
62                                                 message, ...)      \
63   pw_assert_basic_HandleFailure(                                   \
64       __FILE__,                                                    \
65       __LINE__,                                                    \
66       _PW_ASSERT_BASIC_FUNCTION_NAME,                              \
67       "Check failed: "                                             \
68           arg_a_str " (=" type_fmt ") "                            \
69           comparison_op_str " "                                    \
70           arg_b_str " (=" type_fmt ")"                             \
71           ". " message,                                            \
72       arg_a_val, arg_b_val PW_COMMA_ARGS(__VA_ARGS__))
73 // clang-format on
74