• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "android/utils/compiler.h"
18 
19 ANDROID_BEGIN_HEADER
20 
21 /* These are always defined, so you can write your own macros that
22  * call them, independently of the value of ACONFIG_USE_ASSERT
23  */
24 
25 /* Used internally by the macros to register the current source location */
26 void  _android_assert_loc(const char*  fileName,
27                           long         fileLineno,
28                           const char*  functionName);
29 
30 /* Call this after _android_assert_loc() to dump an assertion failed message
31  * just before panicking, i.e. abort the current program
32  */
33 void __attribute__((noreturn)) android_assert_fail(const char*  messageFmt, ...);
34 
35 /* See _android_assert_loc() */
36 #define  _ANDROID_ASSERT_LOC()  \
37     _android_assert_loc(__FILE__,__LINE__,__FUNCTION__)
38 
39 /* Report an assertion failure then panic. Arguments are formatted string */
40 #define  _ANDROID_ASSERT_FAIL(...) \
41     android_assert_fail(__VA_ARGS__)
42 
43 /* Report an unreachable code */
44 #define  _ANDROID_ASSERT_UNREACHED(...)   \
45     do { \
46         _ANDROID_ASSERT_LOC(); \
47         android_assert_fail(__VA_ARGS__); \
48     } while (0);
49 
50 /* Check that 'cond' is true, and report an assertion failure otherwise */
51 #define  _ANDROID_ASSERT(cond,...)  \
52     do { \
53         if (!(cond)) { \
54             _ANDROID_ASSERT_LOC(); \
55             android_assert_fail(__VA_ARGS__); \
56         } \
57     } while (0)
58 
59 /* Check that 'cond' is boolean true (i.e. not 0), and report an assertion
60  * failure otherwise. */
61 #define  _ANDROID_ASSERT_BOOL(cond_,expected_)    \
62     do { \
63         int  cond_result_   = !!(cond_); \
64         int  cond_expected_ = !!(expected_); \
65         if (cond_result_ != cond_expected_) { \
66             _ANDROID_ASSERT_LOC(); \
67             android_assert_fail("%s is %s instead of %s\n",\
68                #cond_, \
69                cond_result_ ? "TRUE" : "FALSE", \
70                cond_expected_ ? "TRUE" : "FALSE" ); \
71         } \
72     } while (0)
73 
74 /* Assert that a given expression is of a given integer value */
75 #define  _ANDROID_ASSERT_INT(cond_,expected_)  \
76     do { \
77         int  cond_result_ = (cond_); \
78         int  cond_expected_ = (expected_); \
79         if (cond_result_ != cond_expected_) { \
80             _ANDROID_ASSERT_LOC(); \
81             android_assert_fail("%s is %d instead of %d\n", \
82                                 #cond_ , cond_result_, cond_expected_); \
83         } \
84     } while (0)
85 
86 #define  _ANDROID_ASSERT_INT_OP(cond_,expected_,op_) \
87     do { \
88         int  cond_result_ = (cond_); \
89         int  cond_expected_ = (expected_); \
90         if (!(cond_result_ _op cond_expected_)) { \
91             _ANDROID_ASSERT_LOC(); \
92             android_assert_fail("%s is %d and should be %s %d\n", \
93                                 #cond_ , cond_result_, #op_, cond_expected_); \
94         } \
95     } while (0)
96 
97 #  define  _ANDROID_ASSERT_PTR(cond_,expected_)  \
98     do { \
99         void*  cond_result_ = (cond_); \
100         void*  cond_expected_ = (void*)(expected_); \
101         if (cond_result_ != cond_expected_) { \
102             _ANDROID_ASSERT_LOC(); \
103             android_assert_fail("%s is %p instead of %p\n", \
104                                 #cond_ , cond_result_, cond_expected_); \
105         } \
106     } while (0)
107 
108 #  define  _ANDROID_NEVER_NULL(ptr_)  \
109     do { \
110         void*  never_ptr_ = (ptr_); \
111         if (never_ptr_ == NULL) { \
112             _ANDROID_ASSERT_LOC(); \
113             android_assert_fail("%s is NULL\n", #ptr_); \
114         } \
115     } while (0)
116 
117 
118 
119 #ifdef ACONFIG_USE_ASSERT
120 
121 #  define  AASSERT_LOC()   _ANDROID_ASSERT_LOC()
122 #  define  AASSERT_FAIL(...) _ANDROID_ASSERT_FAIL(__VA_ARGS__)
123 
124 /* Assert we never reach some code point */
125 #  define  AASSERT_UNREACHED(...)   _ANDROID_ASSERT_UNREACHED(__VA_ARGS__)
126 
127 
128 /* Generic assertion, must be followed by formatted string parameters */
129 #  define  AASSERT(cond,...)  _ANDROID_ASSERT(cond,__VA_ARGS__)
130 
131 /* Assert a condition evaluates to a given boolean */
132 #  define  AASSERT_BOOL(cond_,expected_)   _ANDROID_ASSERT_BOOL(cond_,expected_)
133 
134 /* Assert a condition evaluates to a given integer */
135 #  define  AASSERT_INT(cond_,expected_)  _ANDROID_ASSERT_INT(cond_,expected_)
136 
137 #  define  AASSERT_INT_LT(cond_,expected_)  _ANDROID_ASSERT_INT_OP(cond_,expected_,< )
138 #  define  AASSERT_INT_LTE(cond_,expected_) _ANDROID_ASSERT_INT_OP(cond_,expected_,<= )
139 #  define  AASSERT_INT_GT(cond_,expected_)  _ANDROID_ASSERT_INT_OP(cond_,expected_,> )
140 #  define  AASSERT_INT_GTE(cond_,expected_) _ANDROID_ASSERT_INT_OP(cond_,expected_,>= )
141 #  define  AASSERT_INT_EQ(cond_,expected_)  _ANDROID_ASSERT_INT_OP(cond_,expected_,==)
142 #  define  AASSERT_INT_NEQ(cond_,expected_) _ANDROID_ASSERT_INT_OP(cond_,expected_,!=)
143 
144 #  define  AASSERT_PTR(cond_,expected_)  _ANDROID_ASSERT_PTR(cond_,expected_)
145 
146 #  define  ANEVER_NULL(ptr_)   _ANDROID_NEVER_NULL(ptr_)
147 
148 #else /* !ACONFIG_USE_ASSERT */
149 
150 #  define AASSERT_LOC()              ((void)0)
151 #  define  AASSERT_FAIL(...)        ((void)0)
152 #  define  AASSERT_UNREACHED(...)   ((void)0)
153 
154 /* for side-effects */
155 #  define  AASSERT(cond,...)             ((void)(cond), (void)0)
156 #  define  AASSERT_BOOL(cond,val)        ((void)(cond), (void)0)
157 #  define  AASSERT_INT(cond,val)         AASSERT_BOOL(cond,val)
158 #  define  AASSERT_PTR(cond,val)         AASSERT_BOOL(cond,val)
159 #  define  ANEVER_NULL(ptr)              ((void)(ptr), (void)0)
160 
161 #endif /* !ACONFIG_USE_ASSERT */
162 
163 #  define  AASSERT_TRUE(cond_)   AASSERT_BOOL(cond_,1)
164 #  define  AASSERT_FALSE(cond_)  AASSERT_BOOL(cond_,0)
165 
166 
167 /* this can be used to redirect the assertion log to something
168  * other than stderr. Note that android_assert_fail also calls
169  * android_vpanic.
170  */
171 typedef void (*AAssertLogFunc)( const char*  fmt, va_list  args );
172 void  android_assert_registerLog( AAssertLogFunc  logger );
173 
174 ANDROID_END_HEADER
175 
176 #endif /* ANDROID_UTILS_ASSERT_H */
177