• 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_basic/handler.h"
17 #include "pw_preprocessor/arguments.h"
18 #include "pw_preprocessor/compiler.h"
19 #include "pw_preprocessor/util.h"
20 
21 // Die with a message with many attributes included. This is the crash macro
22 // frontend that funnels everything into the C handler provided by the user,
23 // pw_assert_basic_HandleFailure().
24 #define PW_HANDLE_CRASH(...)     \
25   pw_assert_basic_HandleFailure( \
26       __FILE__, __LINE__, __func__ PW_COMMA_ARGS(__VA_ARGS__))
27 
28 // Die with a message with many attributes included. This is the crash macro
29 // frontend that funnels everything into the C handler provided by the user,
30 // pw_assert_basic_HandleFailure().
31 #define PW_HANDLE_ASSERT_FAILURE(condition_string, message, ...)  \
32   pw_assert_basic_HandleFailure(__FILE__,                         \
33                                 __LINE__,                         \
34                                 __func__,                         \
35                                 "Check failed: " condition_string \
36                                 ". " message PW_COMMA_ARGS(__VA_ARGS__))
37 
38 // Sample assert failure message produced by the below implementation:
39 //
40 //   Check failed: current_sensor (=610) < new_sensor (=50). More details!
41 //
42 // Putting the value next to the operand makes the string easier to read.
43 
44 // clang-format off
45 // This is too hairy for clang format to handle and retain readability.
46 #define PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE(arg_a_str,         \
47                                                 arg_a_val,         \
48                                                 comparison_op_str, \
49                                                 arg_b_str,         \
50                                                 arg_b_val,         \
51                                                 type_fmt,          \
52                                                 message, ...)      \
53   pw_assert_basic_HandleFailure(                                   \
54            __FILE__,                                               \
55            __LINE__,                                               \
56            __func__,                                               \
57            "Check failed: "                                        \
58                arg_a_str " (=" type_fmt ") "                       \
59                comparison_op_str " "                               \
60                arg_b_str " (=" type_fmt ")"                        \
61                ". " message,                                       \
62             arg_a_val, arg_b_val PW_COMMA_ARGS(__VA_ARGS__))
63 // clang-format on
64