[glib] gmem.h: Use typeof() in g_steal_pointer() macro



commit 1a6be022600550272638e858a7fbef5e57ce45ba
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Tue May 22 11:26:47 2018 -0400

    gmem.h: Use typeof() in g_steal_pointer() macro
    
    g_steal_pointer is both an inline function, returning gpointer, and a
    macro that casts the return value to the type of its argument. The first
    version of the macro uses '0 ? (*(pp)) : (g_steal_pointer) (pp)' to cast
    the return value to the type of *pp, but this fails to yield warnings
    about incompatible pointer types with current gcc. Apparently the
    ternary operator is optimized away before the type of the expression is
    determined.
    
    The typeof() (or __typeof__()) operator allows an explicit cast.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742456
    
    https://bugzilla.gnome.org/show_bug.cgi?id=796341

 glib/gmem.h | 6 ++++++
 1 file changed, 6 insertions(+)
---
diff --git a/glib/gmem.h b/glib/gmem.h
index 9530512d0..5cccb045c 100644
--- a/glib/gmem.h
+++ b/glib/gmem.h
@@ -196,8 +196,14 @@ g_steal_pointer (gpointer pp)
 }
 
 /* type safety */
+#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) && !defined(__cplusplus) 
&& GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
+#define g_steal_pointer(pp) ((__typeof__(*pp)) (g_steal_pointer) (pp))
+#else  /* __GNUC__ */
+/* This version does not depend on gcc extensions, but gcc does not warn
+ * about incompatible-pointer-types: */
 #define g_steal_pointer(pp) \
   (0 ? (*(pp)) : (g_steal_pointer) (pp))
+#endif /* __GNUC__ */
 
 /* Optimise: avoid the call to the (slower) _n function if we can
  * determine at compile-time that no overflow happens.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]