From 5fccc8772fb986889208e83081b3793b0d538dcd Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Sun, 6 Jan 2013 16:47:27 +0100 Subject: [PATCH] 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 --- ulut/ul_utdefs.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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 -- 2.39.2