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
This commit is contained in:
Thomas Haller 2022-11-04 14:51:13 -04:00 committed by Ray Strode
parent 79b06ab34f
commit 73bd146409

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