Merge branch 'old-fixes-solve-new-problems' into 'main'

macros: generate uniq variable name in _G_BOOLEAN_EXPR()/G_LIKELY()

Closes #1211

See merge request GNOME/glib!3059
This commit is contained in:
Ray Strode 2022-11-04 19:29:57 +00:00
commit 57a510df86
2 changed files with 9 additions and 5 deletions

View File

@ -1181,15 +1181,16 @@
* putting assignments in g_return_if_fail ().
*/
#if G_GNUC_CHECK_VERSION(2, 0) && defined(__OPTIMIZE__)
#define _G_BOOLEAN_EXPR(expr) \
#define _G_BOOLEAN_EXPR_IMPL(uniq, expr) \
G_GNUC_EXTENSION ({ \
int _g_boolean_var_; \
int G_PASTE (_g_boolean_var_, uniq); \
if (expr) \
_g_boolean_var_ = 1; \
G_PASTE (_g_boolean_var_, uniq) = 1; \
else \
_g_boolean_var_ = 0; \
_g_boolean_var_; \
G_PASTE (_g_boolean_var_, uniq) = 0; \
G_PASTE (_g_boolean_var_, uniq); \
})
#define _G_BOOLEAN_EXPR(expr) _G_BOOLEAN_EXPR_IMPL (__COUNTER__, expr)
#define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
#define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))
#else

View File

@ -1264,6 +1264,9 @@ test_has_prefix_macro (void)
g_assert_true (g_str_has_prefix ("foo", ""));
g_assert_true (g_str_has_prefix ("foo", "foo"));
g_assert_true (g_str_has_prefix ("", ""));
/* Testing the nested G_UNLIKELY */
g_assert_false (G_UNLIKELY (g_str_has_prefix ("foo", "aaa")));
}
/* Testing g_str_has_suffix() function avoiding the optimizing macro */