From: Pavel Pisa Date: Sun, 6 Jan 2013 15:47:27 +0000 (+0100) Subject: Attempt to define safe cast mechanism to remove const and volatile qualifiers. X-Git-Tag: ul_drv-1.0.0-release~2 X-Git-Url: https://rtime.felk.cvut.cz/gitweb/ulut.git/commitdiff_plain/5fccc8772fb986889208e83081b3793b0d538dcd?ds=sidebyside Attempt to define safe cast mechanism to remove const and volatile qualifiers. The mechanism works but only when used inside function. But the most of problematic assignments are in structures initializers in our projects. Signed-off-by: Pavel Pisa --- diff --git a/ulut/ul_utdefs.h b/ulut/ul_utdefs.h index 9c38fe5..b00ea92 100644 --- a/ulut/ul_utdefs.h +++ b/ulut/ul_utdefs.h @@ -146,6 +146,31 @@ extern "C" { #endif #endif /*UL_ATTR_REENTRANT*/ +/* The cast idea based on libHX by Jan Engelhardt */ +#define UL_TYPEOF_REFX(ref_asterisks, ptr_type) \ + 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\ + (UL_TYPEOF_REFX(ref_asterisks, expr), \ + UL_TYPEOF_REFX(ref_asterisks,new_type)), \ + "Qualifiers stripping cast to incompatible type"); \ + (new_type)(expr); \ +}) +#else /*__GNUC__*/ +#define UL_CAST_UNQX(ref_asterisks, new_type, expr) ((new_type)(expr)) +#endif /*__GNUC__*/ + +#define UL_CAST_UNQ1(new_type, expr) \ + UL_CAST_UNQX(*, new_type, expr) + +#define UL_CAST_UNQ2(new_type, expr) \ + UL_CAST_UNQX(**, new_type, expr) + +#define UL_CAST_UNQ3(new_type, expr) \ + UL_CAST_UNQX(**, new_type, expr) + #ifdef __cplusplus } /* extern "C"*/ #endif