From bf3cb6977f386d559993e80dae630c0a3f15d871 Mon Sep 17 00:00:00 2001 From: bernie Date: Mon, 20 Feb 2006 14:34:58 +0000 Subject: [PATCH] Use portable type checking. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@531 38d2e660-2303-0410-9eaa-f027e97ec537 --- cfg/compiler.h | 24 +++++++++++++++++++++++- cfg/macros.h | 9 ++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/cfg/compiler.h b/cfg/compiler.h index d4e7e7b1..b0a29046 100755 --- a/cfg/compiler.h +++ b/cfg/compiler.h @@ -14,6 +14,9 @@ /*#* *#* $Log$ + *#* Revision 1.11 2006/02/20 14:34:58 bernie + *#* Use portable type checking. + *#* *#* Revision 1.10 2006/02/15 09:12:01 bernie *#* Fixes for ARM/IAR support. *#* @@ -198,6 +201,13 @@ #define DEPRECATED __attribute__((__deprecated__)) #endif + #ifndef __cplusplus + #define ASSERT_TYPE_EQUAL(var1, var2) \ + STATIC_ASSERT(__builtin_types_compatible_p(typeof(var1), typeof(var2))) + #define ASSERT_TYPE_IS(var, type) \ + STATIC_ASSERT(__builtin_types_compatible_p(typeof(var), type)) + #endif + /* Include some standard C89/C99 stuff */ #include #include @@ -479,6 +489,18 @@ typedef unsigned char page_t; /*!< Type for banked memory pages. */ /*! Issue a compilation error if the \a condition is false */ #define STATIC_ASSERT(condition) \ - UNUSED_VAR(extern char,PP_CAT(CT_ASSERT___, __LINE__)[(condition) ? 1 : -1]) + UNUSED_VAR(extern char, STATIC_ASSERTION_FAILED__[(condition) ? 1 : -1]) + +#ifndef ASSERT_TYPE_EQUAL +/*! Ensure two variables have the same type. */ +#define ASSERT_TYPE_EQUAL(var1, var2) \ + do { (void)(&(var1) == &(var2)); } while(0) +#endif + +#ifndef ASSERT_TYPE_IS +/*! Ensure variable is of specified type. */ +#define ASSERT_TYPE_IS(var, type) \ + do { (void)(&var == (type *)0); } while(0) +#endif #endif /* DEVLIB_COMPILER_H */ diff --git a/cfg/macros.h b/cfg/macros.h index ded2261b..081b2033 100755 --- a/cfg/macros.h +++ b/cfg/macros.h @@ -14,6 +14,9 @@ /*#* *#* $Log$ + *#* Revision 1.7 2006/02/20 14:34:58 bernie + *#* Use portable type checking. + *#* *#* Revision 1.6 2006/02/10 12:36:57 bernie *#* Pacify IAR warnings for side-effects. *#* @@ -102,13 +105,13 @@ #define MIN(a,b) ({ \ typeof(a) _a = (a); \ typeof(b) _b = (b); \ - (void)(&_a == &_b); /* ensure same type */ \ + ASSERT_TYPE_EQUAL(_a, _b); \ (_a < _b) ? _a : _b; \ }) #define MAX(a,b) ({ \ typeof(a) _a = (a); \ typeof(b) _b = (b); \ - (void)(&_a == &_b); /* ensure same type */ \ + ASSERT_TYPE_EQUAL(_a, _b); \ (_a > _b) ? _a : _b; \ }) #else /* !(COMPILER_STATEMENT_EXPRESSIONS && COMPILER_TYPEOF) */ @@ -132,8 +135,8 @@ */ #define SWAP(a, b) \ do { \ - (void)(&(a) == &(b)); /* type check */ \ typeof(a) tmp; \ + ASSERT_TYPE_EQUAL(a, b); \ tmp = (a); \ (a) = (b); \ (b) = tmp; \ -- 2.25.1