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 * Defines the CHRE_ASSERT and CHRE_ASSERT_LOG macros for CHRE platforms. 25 * Platforms must supply an implementation for assertCondition or use the shared 26 * implementation. 27 */ 28 29 #if defined(CHRE_ASSERTIONS_ENABLED) 30 31 #define CHRE_ASSERT(condition) \ 32 do { \ 33 if (!(condition)) { \ 34 chreDoAssert(CHRE_FILENAME, __LINE__); \ 35 } \ 36 } while (0) 37 38 #elif defined(CHRE_ASSERTIONS_DISABLED) 39 40 #define CHRE_ASSERT(condition) ((void)(condition)) 41 42 #else 43 #error "CHRE_ASSERTIONS_ENABLED or CHRE_ASSERTIONS_DISABLED must be defined" 44 #endif // CHRE_ASSERTIONS_ENABLED 45 46 #ifdef __cplusplus 47 #define CHRE_ASSERT_NOT_NULL(ptr) CHRE_ASSERT((ptr) != nullptr) 48 #else 49 #define CHRE_ASSERT_NOT_NULL(ptr) CHRE_ASSERT((ptr) != NULL) 50 #endif 51 52 /** 53 * Combination macro that always logs an error message if the condition 54 * evaluates to false. 55 * 56 * Note that the supplied condition may be evaluated more than once. 57 * 58 * @param condition Boolean expression which evaluates to false in the failure 59 * case 60 * @param fmt Format string to pass to LOGE 61 * @param ... Arguments to pass to LOGE 62 */ 63 #define CHRE_ASSERT_LOG(condition, fmt, ...) \ 64 do { \ 65 if (!(condition)) { \ 66 LOGE("Assert: " fmt, ##__VA_ARGS__); \ 67 CHRE_ASSERT(condition); \ 68 } \ 69 } while (0) 70 71 /** 72 * Defines "if not test" macros that allow code to not assert when running 73 * on-device unit tests if the assertion isn't useful during testing. 74 */ 75 #ifdef CHRE_ON_DEVICE_TESTS_ENABLED 76 #define CHRE_ASSERT_LOG_IF_NOT_TEST(condition, fmt, ...) 77 #define CHRE_ASSERT_IF_NOT_TEST(condition) ((void)(condition)) 78 #else 79 #define CHRE_ASSERT_LOG_IF_NOT_TEST(condition, fmt, ...) \ 80 CHRE_ASSERT_LOG(condition, fmt, ##__VA_ARGS__) 81 #define CHRE_ASSERT_IF_NOT_TEST(condition) CHRE_ASSERT(condition) 82 #endif 83 84 #ifdef __cplusplus 85 extern "C" { 86 #endif 87 88 /** 89 * Performs assertion while logging the filename and line provided. 90 * 91 * @param filename The filename containing the assertion being fired. 92 * @param line The line that contains the assertion being fired. 93 */ 94 void chreDoAssert(const char *filename, size_t line); 95 96 #ifdef __cplusplus 97 } 98 #endif 99 100 #endif // CHRE_PLATFORM_ASSERT_H_ 101