Skip to content

Commit

Permalink
compat/glib: work around glib bug that causes a compilation error in …
Browse files Browse the repository at this point in the history
…clang++

Signed-off-by: Balazs Scheidler <balazs.scheidler@axoflow.com>
  • Loading branch information
bazsi committed Dec 3, 2023
1 parent d5866f7 commit a33b9c1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
8 changes: 0 additions & 8 deletions configure.ac
Expand Up @@ -480,14 +480,6 @@ dnl C++
# Ok, here is the deal
# clang cannot compile the current source, it has nultiple issues
# replace it till not fixed them
if test "x$CXX" = "xclang"; then
AC_MSG_ERROR(["clang cannot compile syslog-ng C++ sources, try using gcc instead (export CXX=gcc) for C++ compilation (NOTE: on macOS you need Homebrew gcc not the Apple one)"])
elif test "x$CC" = "xclang"; then
CPP_COMPILER="gcc"
AC_MSG_WARN(["clang cannot compile syslog-ng C++ sources, switched to $CPP_COMPILER for C++ compilation (NOTE: on macOS you need Homebrew gcc not the Apple one)"])
else
CPP_COMPILER="$CXX $CC"
fi
# This one is useful for checking the available compilers
# AC_MSG_RESULT(gcc = $(which gcc) - g++ = $(which g++) - clang = $(which clang))
# AC_MSG_RESULT(Paths: gcc = $(ls -Al $(which gcc)) - g++ = $(ls -Al $(which g++)) - clang = $(ls -Al $(which clang)))
Expand Down
32 changes: 32 additions & 0 deletions lib/compat/glib.h
Expand Up @@ -59,4 +59,36 @@ gboolean slng_g_hash_table_insert (GHashTable *hash_table, gpointer key, gpointe
gunichar g_utf8_get_char_validated_fixed (const gchar *p, gssize max_len);
#endif

#if defined(__clang__) && defined(g_atomic_pointer_compare_and_exchange) && defined(__ATOMIC_SEQ_CST) && defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)

/* workaround clang++ compilation error */

#if !(defined(glib_typeof) && defined(G_CXX_STD_VERSION))
#undef g_atomic_pointer_compare_and_exchange

/* the difference here vs glib is the extra (void **) casts in atomic and
* newval, which triggered a compilation warning in clang++:
*
*
* .../lib/atomic-gssize.h:118:10: error: cannot initialize a parameter of type 'gssize *' (aka 'long *') with an rvalue of type 'void *'
* return g_atomic_pointer_compare_and_exchange(&a->value, oldval, newval);
* ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* /usr/include/glib-2.0/glib/gatomic.h:258:44: note: expanded from macro 'g_atomic_pointer_compare_and_exchange'
* __atomic_compare_exchange_n ((atomic), (void *) (&(gapcae_oldval)), (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \
* ^~~~~~~~~~~~~~~~~~~~~~~~~~~
*
*/

#define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
(G_GNUC_EXTENSION ({ \
G_STATIC_ASSERT (sizeof (oldval) == sizeof (gpointer)); \
gpointer gapcae_oldval = (gpointer)(oldval); \
G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer)); \
(void) (0 ? (gpointer) *(atomic) : NULL); \
__atomic_compare_exchange_n ((void **) (atomic), (&(gapcae_oldval)), (void **) (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \
}))
#endif

#endif

#endif

0 comments on commit a33b9c1

Please sign in to comment.