From 73bd14640944522275fe8833b5ed2767092371af Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 4 Nov 2022 14:51:13 -0400 Subject: [PATCH] macros: generate uniq variable name in _G_BOOLEAN_EXPR()/G_LIKELY() This avoids a -Wshadow warning when nesting G_LIKELY() inside each other due to _g_boolean_var_. This can be easily encountered when using macros: ``` #define GET_VALUE(arg) \ ({ \ typeof (arg) _arg = (arg); \ \ g_assert (_arg); \ get_value (_arg); \ }) g_assert (GET_VALUE (a) > 5); ``` __COUNTER__ is a GCC extension, but the definition of _G_BOOLEAN_EXPR() is already inside a #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) block. Closes: #1211 --- glib/gmacros.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/glib/gmacros.h b/glib/gmacros.h index 9fe827082..36d2238fc 100644 --- a/glib/gmacros.h +++ b/glib/gmacros.h @@ -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