std-aux: fix NM_LIKELY()/NM_UNLIKELY() macros

Fix this compile error when "defined(__GNUC__) && (__GNUC__ > 2) &&
defined(__OPTIMIZE__)" doesn't match:

  In file included from ../src/libnm-std-aux/nm-default-std.h:102,
                   from ../src/libnm-std-aux/nm-std-utils.c:3:
  ../src/libnm-std-aux/nm-std-aux.h: In function ‘NM_ALIGN_TO’:
  ../src/libnm-std-aux/nm-std-aux.h:160:6: error: expected expression before ‘{’ token
    160 |     ({                                 \
        |      ^
  ../src/libnm-std-aux/nm-std-aux.h:169:31: note: in expansion of macro ‘_NM_BOOLEAN_EXPR_IMPL’
    169 | #define NM_BOOLEAN_EXPR(expr) _NM_BOOLEAN_EXPR_IMPL(NM_UNIQ, expr)
        |                               ^~~~~~~~~~~~~~~~~~~~~
  ../src/libnm-std-aux/nm-std-aux.h:175:27: note: in expansion of macro ‘NM_BOOLEAN_EXPR’
    175 | #define NM_LIKELY(expr)   NM_BOOLEAN_EXPR(expr)
        |                           ^~~~~~~~~~~~~~~
  ../src/libnm-std-aux/nm-std-aux.h:238:19: note: in expansion of macro ‘NM_LIKELY’
    238 |         } else if NM_LIKELY (cond) {                                  \
        |                   ^~~~~~~~~
  ../src/libnm-std-aux/nm-std-aux.h:449:5: note: in expansion of macro ‘nm_assert’
    449 |     nm_assert(nm_utils_is_power_of_two(ali));
        |     ^~~~~~~~~

The NM_LIKELY()/NM_UNLIKELY() macros should be alwas called enclosed
in parentheses like in:

 if (NM_LIKELY(i == 1)) ...

and should be expanded with only a single pair of parentheses so that
expressions like (i = 1) generate a compiler warning.

Fixes: 030d68aef7 ('shared: add nm_assert() to "nm-std-aux.h"')
This commit is contained in:
Beniamino Galvani
2022-11-03 16:15:15 +01:00
parent 941e8b70f8
commit 8bd72d5f2e

View File

@@ -169,8 +169,8 @@ typedef uint64_t _nm_bitwise nm_be64_t;
#define NM_BOOLEAN_EXPR(expr) _NM_BOOLEAN_EXPR_IMPL(NM_UNIQ, expr)
#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
#define NM_LIKELY(expr) (__builtin_expect(NM_BOOLEAN_EXPR(expr), 1))
#define NM_UNLIKELY(expr) (__builtin_expect(NM_BOOLEAN_EXPR(expr), 0))
#define NM_LIKELY(expr) __builtin_expect(NM_BOOLEAN_EXPR(expr), 1)
#define NM_UNLIKELY(expr) __builtin_expect(NM_BOOLEAN_EXPR(expr), 0)
#else
#define NM_LIKELY(expr) NM_BOOLEAN_EXPR(expr)
#define NM_UNLIKELY(expr) NM_BOOLEAN_EXPR(expr)
@@ -235,7 +235,7 @@ _nm_assert_fail_internal(const char *assertion,
* the assertion does not fail). */ \
if (NM_MORE_ASSERTS_EFFECTIVE == 0) { \
/* pass */ \
} else if NM_LIKELY (cond) { \
} else if (NM_LIKELY(cond)) { \
/* pass */ \
} else { \
_nm_assert_fail(#cond); \
@@ -250,7 +250,7 @@ _nm_assert_fail_internal(const char *assertion,
*
* As such, nm_assert() is async-signal-safe (provided @cond is, and
* the assertion does not fail). */ \
if NM_LIKELY (cond) { \
if (NM_LIKELY(cond)) { \
/* pass */ \
} else if (NM_MORE_ASSERTS_EFFECTIVE == 0) { \
/* pass */ \