shared: re-define _G_BOOLEAN_EXPR() to allow nesting g_assert()

g_assert() uses G_LIKELY(), which in turn uses _G_BOOLEAN_EXPR().
As glib's version of _G_BOOLEAN_EXPR() uses a local variable
_g_boolean_var_, we cannot nest a G_LIKELY() inside a G_LIKELY(),
or inside a g_assert(), or a g_assert() inside a g_assert().

Workaround that, by redefining the macro.

I already encountered this problem before, when having a nm_assert()
inside a ({...}) block, inside a g_assert(). Then I just avoided that
combination, but this situation is quite easy to encounter.
This commit is contained in:
Thomas Haller 2016-10-11 12:23:02 +02:00
parent 97d3b07753
commit 6b904a51ee
2 changed files with 36 additions and 0 deletions

View file

@ -5426,6 +5426,15 @@ test_nm_set_out (void)
call_count = 0;
NM_SET_OUT (p_val, do_test_nm_set_out_called (&call_count));
g_assert_cmpint (call_count, ==, 0);
/* test that we successfully re-defined _G_BOOLEAN_EXPR() */
#define _T1(a) \
({ \
g_assert (a > 5); \
a; \
})
g_assert (_T1 (3) > 1);
#undef _T1
}
/*****************************************************************************/

View file

@ -731,6 +731,33 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) {
#define false 0
#endif
#ifdef _G_BOOLEAN_EXPR
/* g_assert() uses G_LIKELY(), which in turn uses _G_BOOLEAN_EXPR().
* As glib's implementation uses a local variable _g_boolean_var_,
* we cannot do
* g_assert (some_macro ());
* where some_macro() itself expands to ({g_assert(); ...}).
* In other words, you cannot have a g_assert() inside a g_assert()
* without getting a -Werror=shadow failure.
*
* Workaround that by re-defining _G_BOOLEAN_EXPR()
**/
#undef _G_BOOLEAN_EXPR
#define __NM_G_BOOLEAN_EXPR_IMPL(v, expr) \
({ \
int NM_UNIQ_T(V, v); \
\
if (expr) \
NM_UNIQ_T(V, v) = 1; \
else \
NM_UNIQ_T(V, v) = 0; \
NM_UNIQ_T(V, v); \
})
#define _G_BOOLEAN_EXPR(expr) __NM_G_BOOLEAN_EXPR_IMPL (NM_UNIQ, expr)
#endif
/*****************************************************************************/
#endif /* __NM_MACROS_INTERNAL_H__ */