1/* Copyright (C) 2009 The Android Open Source Project 2** 3** This software is licensed under the terms of the GNU General Public 4** License version 2, as published by the Free Software Foundation, and 5** may be copied, distributed, and modified under those terms. 6** 7** This program is distributed in the hope that it will be useful, 8** but WITHOUT ANY WARRANTY; without even the implied warranty of 9** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10** GNU General Public License for more details. 11*/ 12#ifndef ANDROID_UTILS_ASSERT_H 13#define ANDROID_UTILS_ASSERT_H 14 15#include <stdarg.h> 16 17/* These are always defined, so you can write your own macros that 18 * call them, independently of the value of ACONFIG_USE_ASSERT 19 */ 20 21/* Used internally by the macros to register the current source location */ 22void _android_assert_loc(const char* fileName, 23 long fileLineno, 24 const char* functionName); 25 26/* Call this after _android_assert_loc() to dump an assertion failed message 27 * just before panicking, i.e. abort the current program 28 */ 29void __attribute__((noreturn)) android_assert_fail(const char* messageFmt, ...); 30 31/* See _android_assert_loc() */ 32#define _ANDROID_ASSERT_LOC() \ 33 _android_assert_loc(__FILE__,__LINE__,__FUNCTION__) 34 35/* Report an assertion failure then panic. Arguments are formatted string */ 36#define _ANDROID_ASSERT_FAIL(...) \ 37 android_assert_fail(__VA_ARGS__) 38 39/* Report an unreachable code */ 40#define _ANDROID_ASSERT_UNREACHED(...) \ 41 do { \ 42 _ANDROID_ASSERT_LOC(); \ 43 android_assert_fail(__VA_ARGS__); \ 44 } while (0); 45 46/* Check that 'cond' is true, and report an assertion failure otherwise */ 47#define _ANDROID_ASSERT(cond,...) \ 48 do { \ 49 if (!(cond)) { \ 50 _ANDROID_ASSERT_LOC(); \ 51 android_assert_fail(__VA_ARGS__); \ 52 } \ 53 } while (0) 54 55/* Check that 'cond' is boolean true (i.e. not 0), and report an assertion 56 * failure otherwise. */ 57#define _ANDROID_ASSERT_BOOL(cond_,expected_) \ 58 do { \ 59 int cond_result_ = !!(cond_); \ 60 int cond_expected_ = !!(expected_); \ 61 if (cond_result_ != cond_expected_) { \ 62 _ANDROID_ASSERT_LOC(); \ 63 android_assert_fail("%s is %s instead of %s\n",\ 64 #cond_, \ 65 cond_result_ ? "TRUE" : "FALSE", \ 66 cond_expected_ ? "TRUE" : "FALSE" ); \ 67 } \ 68 } while (0) 69 70/* Assert that a given expression is of a given integer value */ 71#define _ANDROID_ASSERT_INT(cond_,expected_) \ 72 do { \ 73 int cond_result_ = (cond_); \ 74 int cond_expected_ = (expected_); \ 75 if (cond_result_ != cond_expected_) { \ 76 _ANDROID_ASSERT_LOC(); \ 77 android_assert_fail("%s is %d instead of %d\n", \ 78 #cond_ , cond_result_, cond_expected_); \ 79 } \ 80 } while (0) 81 82#define _ANDROID_ASSERT_INT_OP(cond_,expected_,op_) \ 83 do { \ 84 int cond_result_ = (cond_); \ 85 int cond_expected_ = (expected_); \ 86 if (!(cond_result_ _op cond_expected_)) { \ 87 _ANDROID_ASSERT_LOC(); \ 88 android_assert_fail("%s is %d and should be %s %d\n", \ 89 #cond_ , cond_result_, #op_, cond_expected_); \ 90 } \ 91 } while (0) 92 93# define _ANDROID_ASSERT_PTR(cond_,expected_) \ 94 do { \ 95 void* cond_result_ = (cond_); \ 96 void* cond_expected_ = (void*)(expected_); \ 97 if (cond_result_ != cond_expected_) { \ 98 _ANDROID_ASSERT_LOC(); \ 99 android_assert_fail("%s is %p instead of %p\n", \ 100 #cond_ , cond_result_, cond_expected_); \ 101 } \ 102 } while (0) 103 104# define _ANDROID_NEVER_NULL(ptr_) \ 105 do { \ 106 void* never_ptr_ = (ptr_); \ 107 if (never_ptr_ == NULL) { \ 108 _ANDROID_ASSERT_LOC(); \ 109 android_assert_fail("%s is NULL\n", #ptr_); \ 110 } \ 111 } while (0) 112 113 114 115#ifdef ACONFIG_USE_ASSERT 116 117# define AASSERT_LOC() _ANDROID_ASSERT_LOC() 118# define AASSERT_FAIL(...) _ANDROID_ASSERT_FAIL(__VA_ARGS__) 119 120/* Assert we never reach some code point */ 121# define AASSERT_UNREACHED(...) _ANDROID_ASSERT_UNREACHED(__VA_ARGS__) 122 123 124/* Generic assertion, must be followed by formatted string parameters */ 125# define AASSERT(cond,...) _ANDROID_ASSERT(cond,__VA_ARGS__) 126 127/* Assert a condition evaluates to a given boolean */ 128# define AASSERT_BOOL(cond_,expected_) _ANDROID_ASSERT_BOOL(cond_,expected_) 129 130/* Assert a condition evaluates to a given integer */ 131# define AASSERT_INT(cond_,expected_) _ANDROID_ASSERT_INT(cond_,expected_) 132 133# define AASSERT_INT_LT(cond_,expected_) _ANDROID_ASSERT_INT_OP(cond_,expected_,< ) 134# define AASSERT_INT_LTE(cond_,expected_) _ANDROID_ASSERT_INT_OP(cond_,expected_,<= ) 135# define AASSERT_INT_GT(cond_,expected_) _ANDROID_ASSERT_INT_OP(cond_,expected_,> ) 136# define AASSERT_INT_GTE(cond_,expected_) _ANDROID_ASSERT_INT_OP(cond_,expected_,>= ) 137# define AASSERT_INT_EQ(cond_,expected_) _ANDROID_ASSERT_INT_OP(cond_,expected_,==) 138# define AASSERT_INT_NEQ(cond_,expected_) _ANDROID_ASSERT_INT_OP(cond_,expected_,!=) 139 140# define AASSERT_PTR(cond_,expected_) _ANDROID_ASSERT_PTR(cond_,expected_) 141 142# define ANEVER_NULL(ptr_) _ANDROID_NEVER_NULL(ptr_) 143 144#else /* !ACONFIG_USE_ASSERT */ 145 146# define AASSERT_LOC() ((void)0) 147# define AASSERT_FAIL(...) ((void)0) 148# define AASSERT_UNREACHED(...) ((void)0) 149 150/* for side-effects */ 151# define AASSERT(cond,...) ((void)(cond), (void)0) 152# define AASSERT_BOOL(cond,val) ((void)(cond), (void)0) 153# define AASSERT_INT(cond,val) AASSERT_BOOL(cond,val) 154# define AASSERT_PTR(cond,val) AASSERT_BOOL(cond,val) 155# define ANEVER_NULL(ptr) ((void)(ptr), (void)0) 156 157#endif /* !ACONFIG_USE_ASSERT */ 158 159# define AASSERT_TRUE(cond_) AASSERT_BOOL(cond_,1) 160# define AASSERT_FALSE(cond_) AASSERT_BOOL(cond_,0) 161 162 163/* this can be used to redirect the assertion log to something 164 * other than stderr. Note that android_assert_fail also calls 165 * android_vpanic. 166 */ 167typedef void (*AAssertLogFunc)( const char* fmt, va_list args ); 168void android_assert_registerLog( AAssertLogFunc logger ); 169 170#endif /* ANDROID_UTILS_ASSERT_H */ 171