xref: /aosp_15_r20/system/chre/platform/include/chre/platform/assert.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CHRE_PLATFORM_ASSERT_H_
18 #define CHRE_PLATFORM_ASSERT_H_
19 
20 #include "chre/platform/log.h"
21 
22 /**
23  * @file
24  * Includes the platform-specific header file that supplies an assertion macro.
25  * The platform header must supply the following symbol as a macro or free
26  * function:
27  *
28  *   CHRE_ASSERT(scalar expression)
29  *
30  * Where expression will be checked to be false (ie: compares equal to zero) and
31  * terminate the program if found to be the case.
32  */
33 
34 #if defined(CHRE_ASSERTIONS_ENABLED) && defined(CHRE_ASSERTIONS_DISABLED)
35 #error "CHRE_ASSERT is both enabled and disabled!"
36 
37 #elif defined(CHRE_ASSERTIONS_ENABLED)
38 
39 #include "chre/target_platform/assert.h"
40 
41 #ifndef CHRE_ASSERT
42 #error "CHRE_ASSERT must be defined by the target platform's assert.h"
43 #endif  // CHRE_ASSERT
44 
45 #elif defined(CHRE_ASSERTIONS_DISABLED)
46 
47 #define CHRE_ASSERT(condition) ((void)(condition))
48 
49 #else
50 #error "CHRE_ASSERTIONS_ENABLED or CHRE_ASSERTIONS_DISABLED must be defined"
51 #endif  // CHRE_ASSERTIONS_ENABLED
52 
53 #ifdef __cplusplus
54 #define CHRE_ASSERT_NOT_NULL(ptr) CHRE_ASSERT((ptr) != nullptr)
55 #else
56 #define CHRE_ASSERT_NOT_NULL(ptr) CHRE_ASSERT((ptr) != NULL)
57 #endif
58 
59 /**
60  * Combination macro that always logs an error message if the condition
61  * evaluates to false.
62  *
63  * Note that the supplied condition may be evaluated more than once.
64  *
65  * @param condition Boolean expression which evaluates to false in the failure
66  *        case
67  * @param fmt Format string to pass to LOGE
68  * @param ... Arguments to pass to LOGE
69  */
70 #define CHRE_ASSERT_LOG(condition, fmt, ...) \
71   do {                                       \
72     if (!(condition)) {                      \
73       LOGE("Assert: " fmt, ##__VA_ARGS__);   \
74       CHRE_ASSERT(condition);                \
75     }                                        \
76   } while (0)
77 
78 /**
79  * Defines "if not test" macros that allow code to not assert when running
80  * on-device unit tests if the assertion isn't useful during testing.
81  */
82 #ifdef CHRE_ON_DEVICE_TESTS_ENABLED
83 #define CHRE_ASSERT_LOG_IF_NOT_TEST(condition, fmt, ...)
84 #define CHRE_ASSERT_IF_NOT_TEST(condition) ((void)(condition))
85 #else
86 #define CHRE_ASSERT_LOG_IF_NOT_TEST(condition, fmt, ...) \
87   CHRE_ASSERT_LOG(condition, fmt, ##__VA_ARGS__)
88 #define CHRE_ASSERT_IF_NOT_TEST(condition) CHRE_ASSERT(condition)
89 #endif
90 
91 #endif  // CHRE_PLATFORM_ASSERT_H_
92