diff options
author | David 'Digit' Turner <digit@android.com> | 2011-04-05 12:44:27 +0200 |
---|---|---|
committer | David 'Digit' Turner <digit@android.com> | 2011-04-11 18:08:11 +0200 |
commit | 2086c639648ccecc9081705a279249ce939d818b (patch) | |
tree | b1f535a0a9995aabfb1895ee0a0e588eeeab9a98 /android/utils | |
parent | bbb81047a39e39021b3b10e2d86af146d514bf8c (diff) | |
download | external_qemu-2086c639648ccecc9081705a279249ce939d818b.zip external_qemu-2086c639648ccecc9081705a279249ce939d818b.tar.gz external_qemu-2086c639648ccecc9081705a279249ce939d818b.tar.bz2 |
<android/utils/assert.h>: Small refactoring
Change-Id: If69874ace211e043d82ffd8349f54396fd334e94
Diffstat (limited to 'android/utils')
-rw-r--r-- | android/utils/assert.h | 96 |
1 files changed, 73 insertions, 23 deletions
diff --git a/android/utils/assert.h b/android/utils/assert.h index 320ead5..30fe15b 100644 --- a/android/utils/assert.h +++ b/android/utils/assert.h @@ -14,44 +14,52 @@ #include <stdarg.h> -#ifdef ACONFIG_USE_ASSERT +/* 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); -#define AASSERT_LOC() \ +/* 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__) -# define AASSERT_FAIL(...) \ +/* Report an assertion failure then panic. Arguments are formatted string */ +#define _ANDROID_ASSERT_FAIL(...) \ android_assert_fail(__VA_ARGS__) -void __attribute__((noreturn)) android_assert_fail(const char* messageFmt, ...); - -/* Assert we never reach some code point */ -# define AASSERT_UNREACHED(...) \ +/* Report an unreachable code */ +#define _ANDROID_ASSERT_UNREACHED(...) \ do { \ - AASSERT_LOC(); \ - android_assert_fail("Unreachable code"); \ + _ANDROID_ASSERT_LOC(); \ + android_assert_fail(__VA_ARGS__); \ } while (0); - -/* Generic assertion, must be followed by formatted string parameters */ -# define AASSERT(cond,...) \ +/* Check that 'cond' is true, and report an assertion failure otherwise */ +#define _ANDROID_ASSERT(cond,...) \ do { \ if (!(cond)) { \ - AASSERT_LOC(); \ + _ANDROID_ASSERT_LOC(); \ android_assert_fail(__VA_ARGS__); \ } \ } while (0) -/* Assert a condition evaluates to a given boolean */ -# define AASSERT_BOOL(cond_,expected_) \ +/* 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_) { \ - AASSERT_LOC(); \ + _ANDROID_ASSERT_LOC(); \ android_assert_fail("%s is %s instead of %s\n",\ #cond_, \ cond_result_ ? "TRUE" : "FALSE", \ @@ -59,38 +67,80 @@ void __attribute__((noreturn)) android_assert_fail(const char* messageFmt, ...) } \ } while (0) -/* Assert a condition evaluates to a given integer */ -# define AASSERT_INT(cond_,expected_) \ +/* 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_) { \ - AASSERT_LOC(); \ + _ANDROID_ASSERT_LOC(); \ android_assert_fail("%s is %d instead of %d\n", \ #cond_ , cond_result_, cond_expected_); \ } \ } while (0) -# define AASSERT_PTR(cond_,expected_) \ +#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_) { \ - AASSERT_LOC(); \ + _ANDROID_ASSERT_LOC(); \ android_assert_fail("%s is %p instead of %p\n", \ #cond_ , cond_result_, cond_expected_); \ } \ } while (0) -# define ANEVER_NULL(ptr_) \ +# define _ANDROID_NEVER_NULL(ptr_) \ do { \ void* never_ptr_ = (ptr_); \ if (never_ptr_ == NULL) { \ - AASSERT_LOC(); \ + _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) |