From 4e65bcda0cf53bcadfa39a9e05d84b75c512b5e0 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Fri, 1 Apr 2022 17:22:06 +0100 Subject: [PATCH 1/3] tests: Fix a typo in a test skip message Signed-off-by: Philip Withnall --- glib/tests/cxx.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/glib/tests/cxx.cpp b/glib/tests/cxx.cpp index be0a6bfa1..18580cfc6 100644 --- a/glib/tests/cxx.cpp +++ b/glib/tests/cxx.cpp @@ -49,7 +49,7 @@ test_typeof (void) g_clear_pointer (&obj6, g_rc_box_release); g_rc_box_release (obj); #else - g_test_skip ("This test requires C++11 compiler"); + g_test_skip ("This test requires a C++11 compiler"); #endif } From 16d1bc49ccae77c16b66224cedf4e58f4f5e01b8 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Fri, 1 Apr 2022 13:44:45 +0100 Subject: [PATCH 2/3] tests: Add C++ tests for typechecking with atomic compare and exchanges The tests have to be conditional on C++11 being enabled, as the default C++ standard on macOS is (for some reason), C++97 (`__cplusplus` is defined as `199711L`). Signed-off-by: Philip Withnall Helps: #2625 --- glib/tests/cxx.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/glib/tests/cxx.cpp b/glib/tests/cxx.cpp index 18580cfc6..6426d43a7 100644 --- a/glib/tests/cxx.cpp +++ b/glib/tests/cxx.cpp @@ -53,6 +53,40 @@ test_typeof (void) #endif } +static void +test_atomic_pointer_compare_and_exchange (void) +{ +#if __cplusplus >= 201103L + const gchar *str1 = "str1"; + const gchar *str2 = "str2"; + const gchar *atomic_string = str1; + + g_test_message ("Test that g_atomic_pointer_compare_and_exchange() with a " + "non-void* pointer doesn’t have any compiler warnings in C++ mode"); + + g_assert_true (g_atomic_pointer_compare_and_exchange (&atomic_string, str1, str2)); + g_assert_true (atomic_string == str2); +#else + g_test_skip ("This test requires a C++11 compiler"); +#endif +} + +static void +test_atomic_int_compare_and_exchange (void) +{ +#if __cplusplus >= 201103L + gint atomic_int = 5; + + g_test_message ("Test that g_atomic_int_compare_and_exchange() doesn’t have " + "any compiler warnings in C++ mode"); + + g_assert_true (g_atomic_int_compare_and_exchange (&atomic_int, 5, 50)); + g_assert_cmpint (atomic_int, ==, 50); +#else + g_test_skip ("This test requires a C++11 compiler"); +#endif +} + int main (int argc, char *argv[]) { @@ -63,6 +97,8 @@ main (int argc, char *argv[]) #endif g_test_add_func ("/C++/typeof", test_typeof); + g_test_add_func ("/C++/atomic-pointer-compare-and-exchange", test_atomic_pointer_compare_and_exchange); + g_test_add_func ("/C++/atomic-int-compare-and-exchange", test_atomic_int_compare_and_exchange); return g_test_run (); } From ad23894c1595482cdc10c17a4070a977e396ca4a Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Fri, 1 Apr 2022 13:47:19 +0100 Subject: [PATCH 3/3] gatomic: Add a C++ variant of g_atomic_int_compare_and_exchange() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The C++ variant implements type safety differently, to avoid warnings from C++ compilers about: ``` ../../../gnome-commander-1.14.2/src/intviewer/searcher.cc:303:5: error: cannot initialize a parameter of type 'gint *' (aka 'int *') with an rvalue of type 'void *' g_atomic_int_compare_and_exchange ((gint*)&src->priv->progress_value, oldval, (gint)d); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /mnt/b/yoe/master/build/tmp/work/cortexa72-yoe-linux/gnome-commander/1.14.2-r0/recipe-sysroot/usr/include/glib-2.0/glib/gatomic.h:160:44: note: expanded from macro 'g_atomic_int_compare_and_exchange' __atomic_compare_exchange_n ((atomic), (void *) (&(gaicae_oldval)), (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \ ^~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` This complements the existing C++ variant for `g_atomic_pointer_compare_and_exchange()`, and fixes a regression on C++ from https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2114. With the addition of the unit tests in the previous commit, this is effectively tested by the FreeBSD and macOS CI jobs, as they use `clang++` in C++ mode. `g++` doesn’t seem to emit a warning about this. Signed-off-by: Philip Withnall Fixes: #2625 --- glib/gatomic.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/glib/gatomic.h b/glib/gatomic.h index 5eba1dbc7..8b2b880c8 100644 --- a/glib/gatomic.h +++ b/glib/gatomic.h @@ -152,6 +152,17 @@ G_END_DECLS (void) (0 ? *(atomic) ^ *(atomic) : 1); \ __atomic_fetch_sub ((atomic), 1, __ATOMIC_SEQ_CST) == 1; \ })) +#if defined(glib_typeof) && defined(__cplusplus) && __cplusplus >= 201103L +/* See comments below about equivalent g_atomic_pointer_compare_and_exchange() + * shenanigans for type-safety when compiling in C++ mode. */ +#define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \ + (G_GNUC_EXTENSION ({ \ + glib_typeof (*(atomic)) gaicae_oldval = (oldval); \ + G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \ + (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 1); \ + __atomic_compare_exchange_n ((atomic), &gaicae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \ + })) +#else /* if !(defined(glib_typeof) && defined(__cplusplus) && __cplusplus >= 201103L) */ #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \ (G_GNUC_EXTENSION ({ \ gint gaicae_oldval = (oldval); \ @@ -159,6 +170,7 @@ G_END_DECLS (void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 1); \ __atomic_compare_exchange_n ((atomic), (void *) (&(gaicae_oldval)), (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \ })) +#endif /* defined(glib_typeof) */ #define g_atomic_int_add(atomic, val) \ (G_GNUC_EXTENSION ({ \ G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint)); \