aboutsummaryrefslogtreecommitdiffstats
path: root/android/utils/assert.h
blob: 30fe15b18e39cfe8388f4f6c5a4221395ddec831 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright (C) 2009 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/
#ifndef ANDROID_UTILS_ASSERT_H
#define ANDROID_UTILS_ASSERT_H

#include <stdarg.h>

/* These are always defined, so you can write your own macros that
 * call them, independently of the value of ACONFIG_USE_ASSERT
 */

/* Used internally by the macros to register the current source location */
void  _android_assert_loc(const char*  fileName,
                          long         fileLineno,
                          const char*  functionName);

/* Call this after _android_assert_loc() to dump an assertion failed message
 * just before panicking, i.e. abort the current program
 */
void __attribute__((noreturn)) android_assert_fail(const char*  messageFmt, ...);

/* See _android_assert_loc() */
#define  _ANDROID_ASSERT_LOC()  \
    _android_assert_loc(__FILE__,__LINE__,__FUNCTION__)

/* Report an assertion failure then panic. Arguments are formatted string */
#define  _ANDROID_ASSERT_FAIL(...) \
    android_assert_fail(__VA_ARGS__)

/* Report an unreachable code */
#define  _ANDROID_ASSERT_UNREACHED(...)   \
    do { \
        _ANDROID_ASSERT_LOC(); \
        android_assert_fail(__VA_ARGS__); \
    } while (0);

/* Check that 'cond' is true, and report an assertion failure otherwise */
#define  _ANDROID_ASSERT(cond,...)  \
    do { \
        if (!(cond)) { \
            _ANDROID_ASSERT_LOC(); \
            android_assert_fail(__VA_ARGS__); \
        } \
    } while (0)

/* Check that 'cond' is boolean true (i.e. not 0), and report an assertion
 * failure otherwise. */
#define  _ANDROID_ASSERT_BOOL(cond_,expected_)    \
    do { \
        int  cond_result_   = !!(cond_); \
        int  cond_expected_ = !!(expected_); \
        if (cond_result_ != cond_expected_) { \
            _ANDROID_ASSERT_LOC(); \
            android_assert_fail("%s is %s instead of %s\n",\
               #cond_, \
               cond_result_ ? "TRUE" : "FALSE", \
               cond_expected_ ? "TRUE" : "FALSE" ); \
        } \
    } while (0)

/* Assert that a given expression is of a given integer value */
#define  _ANDROID_ASSERT_INT(cond_,expected_)  \
    do { \
        int  cond_result_ = (cond_); \
        int  cond_expected_ = (expected_); \
        if (cond_result_ != cond_expected_) { \
            _ANDROID_ASSERT_LOC(); \
            android_assert_fail("%s is %d instead of %d\n", \
                                #cond_ , cond_result_, cond_expected_); \
        } \
    } while (0)

#define  _ANDROID_ASSERT_INT_OP(cond_,expected_,op_) \
    do { \
        int  cond_result_ = (cond_); \
        int  cond_expected_ = (expected_); \
        if (!(cond_result_ _op cond_expected_)) { \
            _ANDROID_ASSERT_LOC(); \
            android_assert_fail("%s is %d and should be %s %d\n", \
                                #cond_ , cond_result_, #op_, cond_expected_); \
        } \
    } while (0)

#  define  _ANDROID_ASSERT_PTR(cond_,expected_)  \
    do { \
        void*  cond_result_ = (cond_); \
        void*  cond_expected_ = (void*)(expected_); \
        if (cond_result_ != cond_expected_) { \
            _ANDROID_ASSERT_LOC(); \
            android_assert_fail("%s is %p instead of %p\n", \
                                #cond_ , cond_result_, cond_expected_); \
        } \
    } while (0)

#  define  _ANDROID_NEVER_NULL(ptr_)  \
    do { \
        void*  never_ptr_ = (ptr_); \
        if (never_ptr_ == NULL) { \
            _ANDROID_ASSERT_LOC(); \
            android_assert_fail("%s is NULL\n", #ptr_); \
        } \
    } while (0)



#ifdef ACONFIG_USE_ASSERT

#  define  AASSERT_LOC()   _ANDROID_ASSERT_LOC()
#  define  AASSERT_FAIL(...) _ANDROID_ASSERT_FAIL(__VA_ARGS__)

/* Assert we never reach some code point */
#  define  AASSERT_UNREACHED(...)   _ANDROID_ASSERT_UNREACHED(__VA_ARGS__)


/* Generic assertion, must be followed by formatted string parameters */
#  define  AASSERT(cond,...)  _ANDROID_ASSERT(cond,__VA_ARGS__)

/* Assert a condition evaluates to a given boolean */
#  define  AASSERT_BOOL(cond_,expected_)   _ANDROID_ASSERT_BOOL(cond_,expected_)

/* Assert a condition evaluates to a given integer */
#  define  AASSERT_INT(cond_,expected_)  _ANDROID_ASSERT_INT(cond_,expected_)

#  define  AASSERT_INT_LT(cond_,expected_)  _ANDROID_ASSERT_INT_OP(cond_,expected_,< )
#  define  AASSERT_INT_LTE(cond_,expected_) _ANDROID_ASSERT_INT_OP(cond_,expected_,<= )
#  define  AASSERT_INT_GT(cond_,expected_)  _ANDROID_ASSERT_INT_OP(cond_,expected_,> )
#  define  AASSERT_INT_GTE(cond_,expected_) _ANDROID_ASSERT_INT_OP(cond_,expected_,>= )
#  define  AASSERT_INT_EQ(cond_,expected_)  _ANDROID_ASSERT_INT_OP(cond_,expected_,==)
#  define  AASSERT_INT_NEQ(cond_,expected_) _ANDROID_ASSERT_INT_OP(cond_,expected_,!=)

#  define  AASSERT_PTR(cond_,expected_)  _ANDROID_ASSERT_PTR(cond_,expected_)

#  define  ANEVER_NULL(ptr_)   _ANDROID_NEVER_NULL(ptr_)

#else /* !ACONFIG_USE_ASSERT */

#  define AASSERT_LOC()              ((void)0)
#  define  AASSERT_FAIL(...)        ((void)0)
#  define  AASSERT_UNREACHED(...)   ((void)0)

/* for side-effects */
#  define  AASSERT(cond,...)             ((void)(cond), (void)0)
#  define  AASSERT_BOOL(cond,val)        ((void)(cond), (void)0)
#  define  AASSERT_INT(cond,val)         AASSERT_BOOL(cond,val)
#  define  AASSERT_PTR(cond,val)         AASSERT_BOOL(cond,val)
#  define  ANEVER_NULL(ptr)              ((void)(ptr), (void)0)

#endif /* !ACONFIG_USE_ASSERT */

#  define  AASSERT_TRUE(cond_)   AASSERT_BOOL(cond_,1)
#  define  AASSERT_FALSE(cond_)  AASSERT_BOOL(cond_,0)


/* this can be used to redirect the assertion log to something
 * other than stderr. Note that android_assert_fail also calls
 * android_vpanic.
 */
typedef void (*AAssertLogFunc)( const char*  fmt, va_list  args );
void  android_assert_registerLog( AAssertLogFunc  logger );

#endif /* ANDROID_UTILS_ASSERT_H */