[glib/atomic-older-cplusplus: 8/10] glib: Use G_CXX_STD_VERSION to check how to behave with C++ compilers




commit 26c451519241408516cb9650fa62f7239d464c97
Author: Marco Trevisan (Treviño) <mail 3v1n0 net>
Date:   Wed Sep 14 01:31:39 2022 +0200

    glib: Use G_CXX_STD_VERSION to check how to behave with C++ compilers

 glib/gatomic.h     |  8 ++++----
 glib/glib-typeof.h |  5 ++---
 glib/gmacros.h     | 43 +++++++++++++++++++++++++------------------
 glib/gtestutils.h  |  2 +-
 glib/tests/cxx.cpp |  2 ++
 gobject/gtype.h    |  5 +++--
 6 files changed, 37 insertions(+), 28 deletions(-)
---
diff --git a/glib/gatomic.h b/glib/gatomic.h
index e1b5d23913..d5936b8de3 100644
--- a/glib/gatomic.h
+++ b/glib/gatomic.h
@@ -170,7 +170,7 @@ G_END_DECLS
     (void) (0 ? *(atomic) ^ *(atomic) : 1);                                  \
     __atomic_fetch_sub ((atomic), 1, __ATOMIC_SEQ_CST) == 1;                 \
   }))
-#if defined(glib_typeof) && defined(__cplusplus)
+#if defined (glib_typeof) && defined (G_CXX_STD_VERSION)
 /* 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) \
@@ -180,7 +180,7 @@ G_END_DECLS
     (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)) */
+#else /* if !(defined (glib_typeof) && defined (G_CXX_STD_VERSION)) */
 #define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
   (G_GNUC_EXTENSION ({                                                       \
     gint gaicae_oldval = (oldval);                                           \
@@ -230,7 +230,7 @@ G_END_DECLS
     (guint) __atomic_fetch_xor ((atomic), (val), __ATOMIC_SEQ_CST);          \
   }))
 
-#if defined(glib_typeof) && defined(__cplusplus)
+#if defined (glib_typeof) && defined (G_CXX_STD_VERSION)
 /* This is typesafe because we check we can assign oldval to the type of
  * (*atomic). Unfortunately it can only be done in C++ because gcc/clang warn
  * when atomic is volatile and not oldval, or when atomic is gsize* and oldval
@@ -247,7 +247,7 @@ G_END_DECLS
     (void) (0 ? (gpointer) *(atomic) : NULL);                                \
     __atomic_compare_exchange_n ((atomic), &gapcae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, 
__ATOMIC_SEQ_CST) ? TRUE : FALSE; \
   }))
-#else /* if !(defined(glib_typeof) && defined(__cplusplus) */
+#else /* if !(defined (glib_typeof) && defined (G_CXX_STD_VERSION) */
 #define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
   (G_GNUC_EXTENSION ({                                                       \
     G_STATIC_ASSERT (sizeof (oldval) == sizeof (gpointer));                  \
diff --git a/glib/glib-typeof.h b/glib/glib-typeof.h
index 106859d7bb..c3519fa477 100644
--- a/glib/glib-typeof.h
+++ b/glib/glib-typeof.h
@@ -34,11 +34,10 @@
  * This symbol is private.
  */
 #undef glib_typeof
-#if (!defined(__cplusplus) || (!defined (_MSVC_LANG) && __cplusplus < 201103L)) && \
+#if !G_CXX_STD_CHECK_VERSION (11) && \
     (G_GNUC_CHECK_VERSION(4, 8) || defined(__clang__))
 #define glib_typeof(t) __typeof__ (t)
-#elif defined(__cplusplus) && \
-      (__cplusplus >= 201103L || (defined (_MSVC_LANG) && _MSVC_LANG > 201103L)) && \
+#elif G_CXX_STD_CHECK_VERSION (11) && \
       GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68
 /* C++11 decltype() is close enough for our usage */
 #include <type_traits>
diff --git a/glib/gmacros.h b/glib/gmacros.h
index 6b9554796b..5903860cec 100644
--- a/glib/gmacros.h
+++ b/glib/gmacros.h
@@ -95,7 +95,7 @@
  * Otherwise, we say "__inline" to avoid the warning.
  */
 #define G_CAN_INLINE
-#ifndef __cplusplus
+#ifndef G_CXX_STD_VERSION
 # ifdef _MSC_VER
 #  if (_MSC_VER < 1900)
 #   define G_INLINE_DEFINE_NEEDED
@@ -841,12 +841,10 @@
 #ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */
 #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
 #define G_PASTE(identifier1,identifier2)      G_PASTE_ARGS (identifier1, identifier2)
-#if !defined(__cplusplus) && defined(__STDC_VERSION__) && \
+#if !defined(G_CXX_STD_VERSION) && defined(__STDC_VERSION__) && \
     (__STDC_VERSION__ >= 201112L || g_macro__has_feature(c_static_assert) || 
g_macro__has_extension(c_static_assert))
 #define G_STATIC_ASSERT(expr) _Static_assert (expr, "Expression evaluates to false")
-#elif (defined(__cplusplus) && __cplusplus >= 201103L) || \
-      (defined(__cplusplus) && defined (_MSC_VER) && (_MSC_VER >= 1600)) || \
-      (defined (_MSC_VER) && (_MSC_VER >= 1800))
+#elif G_CXX_STD_CHECK_VERSION (11)
 #define G_STATIC_ASSERT(expr) static_assert (expr, "Expression evaluates to false")
 #else
 #ifdef __COUNTER__
@@ -859,14 +857,14 @@
 #endif /* !__GI_SCANNER__ */
 
 /* Provide a string identifying the current code position */
-#if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)
+#if defined (__GNUC__) && (__GNUC__ < 3) && !defined (G_CXX_STD_VERSION)
 #define G_STRLOC       __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
 #else
 #define G_STRLOC       __FILE__ ":" G_STRINGIFY (__LINE__)
 #endif
 
 /* Provide a string identifying the current function, non-concatenatable */
-#if defined (__GNUC__) && defined (__cplusplus)
+#if defined (__GNUC__) && defined (G_CXX_STD_VERSION)
 #define G_STRFUNC     ((const char*) (__PRETTY_FUNCTION__))
 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
 #define G_STRFUNC     ((const char*) (__func__))
@@ -877,7 +875,7 @@
 #endif
 
 /* Guard C code in headers, while including them from C++ */
-#ifdef  __cplusplus
+#ifdef  G_CXX_STD_VERSION
 #define G_BEGIN_DECLS  extern "C" {
 #define G_END_DECLS    }
 #else
@@ -891,11 +889,11 @@
  *  defined then the current definition is correct.
  */
 #ifndef NULL
-#  ifdef __cplusplus
+#  ifdef G_CXX_STD_VERSION
 #  define NULL        (0L)
-#  else /* !__cplusplus */
+#  else /* !G_CXX_STD_VERSION */
 #  define NULL        ((void*) 0)
-#  endif /* !__cplusplus */
+#  endif /* !G_CXX_STD_VERSION */
 #endif
 
 #ifndef        FALSE
@@ -994,7 +992,8 @@
  *
  * Since: 2.60
  */
-#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__cplusplus)
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && \
+    !defined(G_CXX_STD_VERSION)
 #define G_ALIGNOF(type) _Alignof (type) \
   GLIB_AVAILABLE_MACRO_IN_2_60
 #else
@@ -1054,7 +1053,7 @@
   /* Use MSVC specific syntax.  */
 # define G_NORETURN __declspec (noreturn)
   /* Use ISO C++11 syntax when the compiler supports it.  */
-#elif defined (__cplusplus) && __cplusplus >= 201103
+#elif G_CXX_STD_CHECK_VERSION (11)
 # define G_NORETURN [[noreturn]]
   /* Use ISO C11 syntax when the compiler supports it.  */
 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112
@@ -1125,7 +1124,7 @@
  * code which includes glib.h, even if the third party code doesn’t use the new
  * macro itself. */
 #if g_macro__has_attribute(__always_inline__)
-# if defined (__cplusplus) && __cplusplus >= 201103L
+# if G_CXX_STD_CHECK_VERSION (11)
     /* Use ISO C++11 syntax when the compiler supports it. */
 #   define G_ALWAYS_INLINE [[gnu::always_inline]]
 # else
@@ -1133,7 +1132,11 @@
 # endif
 #elif defined (_MSC_VER)
   /* Use MSVC specific syntax.  */
-# define G_ALWAYS_INLINE __forceinline
+# if G_CXX_STD_CHECK_VERSION (11)
+#  define G_ALWAYS_INLINE [[msvc::forceinline]]
+# else
+#  define G_ALWAYS_INLINE __forceinline
+# endif
 #else
 # define G_ALWAYS_INLINE /* empty */
 #endif
@@ -1169,16 +1172,20 @@
  * code which includes glib.h, even if the third party code doesn’t use the new
  * macro itself. */
 #if g_macro__has_attribute(__noinline__)
-# if defined (__cplusplus) && __cplusplus >= 201103L
+# if G_CXX_STD_CHECK_VERSION (11)
     /* Use ISO C++11 syntax when the compiler supports it. */
-#   define G_NO_INLINE [[gnu::noinline]]
+#   if defined (__GNUC__)
+#      define G_NO_INLINE [[gnu::noinline]]
+#   elif defined (_MSC_VER)
+#      define G_NO_INLINE [[msvc::noinline]]
+#   endif
 # else
 #   define G_NO_INLINE __attribute__ ((__noinline__))
 # endif
 #elif defined (_MSC_VER) && (1200 <= _MSC_VER)
   /* Use MSVC specific syntax.  */
-# if defined (__cplusplus) && __cplusplus >= 201103L
     /* Use ISO C++11 syntax when the compiler supports it. */
+# if G_CXX_STD_CHECK_VERSION (11)
 #   define G_NO_INLINE [[msvc::noinline]]
 # else
 #   define G_NO_INLINE __declspec (noinline)
diff --git a/glib/gtestutils.h b/glib/gtestutils.h
index 4e38eb4146..bc404d96d9 100644
--- a/glib/gtestutils.h
+++ b/glib/gtestutils.h
@@ -195,7 +195,7 @@ typedef void (*GTestFixtureFunc) (gpointer      fixture,
                                         } G_STMT_END
 
 /* Use nullptr in C++ to catch misuse of these macros. */
-#if defined(__cplusplus) && __cplusplus >= 201100L
+#if G_CXX_STD_CHECK_VERSION (11)
 #define g_assert_null(expr)             G_STMT_START { if G_LIKELY ((expr) == nullptr) ; else \
                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, 
G_STRFUNC, \
                                                                     "'" #expr "' should be nullptr"); \
diff --git a/glib/tests/cxx.cpp b/glib/tests/cxx.cpp
index fcdd858e46..8dd1779759 100644
--- a/glib/tests/cxx.cpp
+++ b/glib/tests/cxx.cpp
@@ -100,6 +100,8 @@ test_typeof (void)
   MyObject *obj2 = g_rc_box_acquire (obj);
   g_assert_true (obj2 == obj);
 
+  G_STATIC_ASSERT (sizeof (glib_typeof (*obj)) == sizeof (glib_typeof (*obj2)));
+
   MyObject *obj3 = g_atomic_pointer_get (&obj2);
   g_assert_true (obj3 == obj);
 
diff --git a/gobject/gtype.h b/gobject/gtype.h
index 3f2afb31e0..136f7a51ee 100644
--- a/gobject/gtype.h
+++ b/gobject/gtype.h
@@ -398,7 +398,7 @@ G_BEGIN_DECLS
  * A numerical value which represents the unique identifier of a registered
  * type.
  */
-#if     GLIB_SIZEOF_SIZE_T != GLIB_SIZEOF_LONG || !defined __cplusplus
+#if     GLIB_SIZEOF_SIZE_T != GLIB_SIZEOF_LONG || !defined (G_CXX_STD_VERSION)
 typedef gsize                           GType;
 #else   /* for historic reasons, C++ links against gulong GTypes */
 typedef gulong                          GType;
@@ -2313,7 +2313,8 @@ type_name##_get_type (void) \
 /* Only use this in non-C++ on GCC >= 2.7, except for Darwin/ppc64.
  * See https://bugzilla.gnome.org/show_bug.cgi?id=647145
  */
-#if !defined (__cplusplus) && (G_GNUC_CHECK_VERSION(2, 7)) && !(defined (__APPLE__) && defined (__ppc64__))
+#if !defined (G_CXX_STD_VERSION) && (G_GNUC_CHECK_VERSION(2, 7)) && \
+    !(defined (__APPLE__) && defined (__ppc64__))
 #define _G_DEFINE_BOXED_TYPE_BEGIN(TypeName, type_name, copy_func, free_func) \
 static GType type_name##_get_type_once (void); \
 \


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]