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

This commit is contained in:
Marco Trevisan (Treviño) 2022-09-14 01:31:39 +02:00
parent f9845abe39
commit 372ab7a964
6 changed files with 35 additions and 38 deletions

View File

@ -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
@ -248,7 +248,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)); \

View File

@ -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>

View File

@ -98,7 +98,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
@ -844,12 +844,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__
@ -862,14 +860,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__))
@ -880,7 +878,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
@ -894,16 +892,14 @@
* defined then the current definition is correct.
*/
#ifndef NULL
# ifdef __cplusplus
# if __cplusplus >= 201103L
# define NULL (nullptr)
# else
# define NULL (0L)
# endif /* __cplusplus >= 201103L */
# else /* !__cplusplus */
# define NULL ((void*) 0)
# endif /* !__cplusplus */
#elif defined (__cplusplus) && __cplusplus >= 201103L
# if G_CXX_STD_CHECK_VERSION (11)
# define NULL (nullptr)
# elif defined (G_CXX_STD_VERSION)
# define NULL (0L)
# else
# define NULL ((void*) 0)
# endif /* G_CXX_STD_CHECK_VERSION (11) */
#elif G_CXX_STD_CHECK_VERSION (11)
# undef NULL
# define NULL (nullptr)
#endif
@ -1004,7 +1000,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
@ -1064,7 +1061,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
@ -1135,7 +1132,7 @@
* code which includes glib.h, even if the third party code doesnt 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
@ -1179,20 +1176,18 @@
* code which includes glib.h, even if the third party code doesnt 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]]
# 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. */
# define G_NO_INLINE [[msvc::noinline]]
# else
# define G_NO_INLINE __declspec (noinline)
# endif
# define G_NO_INLINE __declspec (noinline)
#else
# define G_NO_INLINE /* empty */
#endif

View File

@ -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"); \

View File

@ -140,6 +140,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);

View File

@ -412,7 +412,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;
@ -2331,7 +2331,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); \
\