]> rtime.felk.cvut.cz Git - ulut.git/commitdiff
Attempt to define safe cast mechanism to remove const and volatile qualifiers.
authorPavel Pisa <pisa@cmp.felk.cvut.cz>
Sun, 6 Jan 2013 15:47:27 +0000 (16:47 +0100)
committerPavel Pisa <pisa@cmp.felk.cvut.cz>
Sun, 6 Jan 2013 15:47:27 +0000 (16:47 +0100)
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 <pisa@cmp.felk.cvut.cz>
ulut/ul_utdefs.h

index 9c38fe5b3b2468fc8141999bd541923cb65e6a41..b00ea9207b41539305555bc0f2defa6417225d09 100644 (file)
@@ -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