From 6691271f8f3034e958ba6520f24d7ef9581a7030 Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Fri, 5 Sep 2014 19:25:08 +0200 Subject: [PATCH] Const and volatile qualifiers removal (UL_CAST_UNQX) which supports global initializers. Signed-off-by: Pavel Pisa --- ulut/ul_utdefs.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/ulut/ul_utdefs.h b/ulut/ul_utdefs.h index b00ea92..adee9bd 100644 --- a/ulut/ul_utdefs.h +++ b/ulut/ul_utdefs.h @@ -151,13 +151,22 @@ extern "C" { typeof(ref_asterisks(union { int z; typeof(ptr_type) x; }){0}.x) #ifdef __GNUC__ -#define UL_CAST_UNQX(ref_asterisks, new_type, expr) ({ \ - UL_BUILD_BUG_ON_MSG(!__builtin_types_compatible_p\ +/* Const and volatile qualifiers removal cast + * The expression is cast by (typeof((typeof(new_type[1])){0}[0])) + * which is equivalent to (new_type) cast for matching types. + * This quite complicated method is used because both comma operator + * and compound expressions are not allowed in global variables + * initializers. + */ +#define UL_CAST_UNQX(ref_asterisks, new_type, expr) ( \ + (typeof((typeof(new_type[1 - 2 * \ + !__builtin_types_compatible_p \ (UL_TYPEOF_REFX(ref_asterisks, expr), \ - UL_TYPEOF_REFX(ref_asterisks,new_type)), \ - "Qualifiers stripping cast to incompatible type"); \ - (new_type)(expr); \ -}) + UL_TYPEOF_REFX(ref_asterisks, new_type)) \ + ])){0}[0]) \ + ) \ + (expr) \ +) #else /*__GNUC__*/ #define UL_CAST_UNQX(ref_asterisks, new_type, expr) ((new_type)(expr)) #endif /*__GNUC__*/ -- 2.39.2