macros: Tighten up argument type checking using typeof or _Generic
@pwithnall
Submitted by Philip Withnall Link to original bug (#793897)
Description
Currently, we have a bunch of type checks in macros implemented using ternary conditions. For example, in gatomic.h:
#define g_atomic_int_get(atomic)
(G_GNUC_EXTENSION ({
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));
(void) (0 ? *(atomic) ^ *(atomic) : 1);
(gint) __atomic_load_4 ((atomic), __ATOMIC_SEQ_CST);
}))
If appropriate compiler support is available, we can do better than this, either using typeof() (GCC extension), or _Generic (C11):
https://stackoverflow.com/a/8154822/2931197 http://en.cppreference.com/w/c/language/generic
It might be possible to produce a generic macro (G_MACRO_TYPE_CHECK(argument, type)?) which hides all the compiler differences in a single place.
Regardless of this, we should continue to use static inlines for new API where possible, to avoid the whole mess of macros.
This follows up from an SO discussion: https://stackoverflow.com/questions/48942797/could-someone-explain-this-macro-define-g-once-init-enterlocation-which-is-in/48943499?noredirect=1#comment84910741_48943499
Bug #589503 is vaguely related.
Version: 2.55.x