mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-06-03 19:30:07 +02:00
glib: Use G_CXX_STD_VERSION to check how to behave with C++ compilers
This commit is contained in:
parent
f9845abe39
commit
372ab7a964
@ -170,7 +170,7 @@ G_END_DECLS
|
|||||||
(void) (0 ? *(atomic) ^ *(atomic) : 1); \
|
(void) (0 ? *(atomic) ^ *(atomic) : 1); \
|
||||||
__atomic_fetch_sub ((atomic), 1, __ATOMIC_SEQ_CST) == 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()
|
/* See comments below about equivalent g_atomic_pointer_compare_and_exchange()
|
||||||
* shenanigans for type-safety when compiling in C++ mode. */
|
* shenanigans for type-safety when compiling in C++ mode. */
|
||||||
#define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
|
#define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
|
||||||
@ -180,7 +180,7 @@ G_END_DECLS
|
|||||||
(void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 1); \
|
(void) (0 ? *(atomic) ^ (newval) ^ (oldval) : 1); \
|
||||||
__atomic_compare_exchange_n ((atomic), &gaicae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \
|
__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) \
|
#define g_atomic_int_compare_and_exchange(atomic, oldval, newval) \
|
||||||
(G_GNUC_EXTENSION ({ \
|
(G_GNUC_EXTENSION ({ \
|
||||||
gint gaicae_oldval = (oldval); \
|
gint gaicae_oldval = (oldval); \
|
||||||
@ -230,7 +230,7 @@ G_END_DECLS
|
|||||||
(guint) __atomic_fetch_xor ((atomic), (val), __ATOMIC_SEQ_CST); \
|
(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
|
/* 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
|
* (*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
|
* 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); \
|
(void) (0 ? (gpointer) *(atomic) : NULL); \
|
||||||
__atomic_compare_exchange_n ((atomic), &gapcae_oldval, (newval), FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ? TRUE : FALSE; \
|
__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) \
|
#define g_atomic_pointer_compare_and_exchange(atomic, oldval, newval) \
|
||||||
(G_GNUC_EXTENSION ({ \
|
(G_GNUC_EXTENSION ({ \
|
||||||
G_STATIC_ASSERT (sizeof (oldval) == sizeof (gpointer)); \
|
G_STATIC_ASSERT (sizeof (oldval) == sizeof (gpointer)); \
|
||||||
|
@ -34,11 +34,10 @@
|
|||||||
* This symbol is private.
|
* This symbol is private.
|
||||||
*/
|
*/
|
||||||
#undef glib_typeof
|
#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__))
|
(G_GNUC_CHECK_VERSION(4, 8) || defined(__clang__))
|
||||||
#define glib_typeof(t) __typeof__ (t)
|
#define glib_typeof(t) __typeof__ (t)
|
||||||
#elif defined(__cplusplus) && \
|
#elif G_CXX_STD_CHECK_VERSION (11) && \
|
||||||
(__cplusplus >= 201103L || (defined (_MSVC_LANG) && _MSVC_LANG > 201103L)) && \
|
|
||||||
GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68
|
GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_68
|
||||||
/* C++11 decltype() is close enough for our usage */
|
/* C++11 decltype() is close enough for our usage */
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
@ -98,7 +98,7 @@
|
|||||||
* Otherwise, we say "__inline" to avoid the warning.
|
* Otherwise, we say "__inline" to avoid the warning.
|
||||||
*/
|
*/
|
||||||
#define G_CAN_INLINE
|
#define G_CAN_INLINE
|
||||||
#ifndef __cplusplus
|
#ifndef G_CXX_STD_VERSION
|
||||||
# ifdef _MSC_VER
|
# ifdef _MSC_VER
|
||||||
# if (_MSC_VER < 1900)
|
# if (_MSC_VER < 1900)
|
||||||
# define G_INLINE_DEFINE_NEEDED
|
# define G_INLINE_DEFINE_NEEDED
|
||||||
@ -844,12 +844,10 @@
|
|||||||
#ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */
|
#ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */
|
||||||
#define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
|
#define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
|
||||||
#define G_PASTE(identifier1,identifier2) G_PASTE_ARGS (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))
|
(__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")
|
#define G_STATIC_ASSERT(expr) _Static_assert (expr, "Expression evaluates to false")
|
||||||
#elif (defined(__cplusplus) && __cplusplus >= 201103L) || \
|
#elif G_CXX_STD_CHECK_VERSION (11)
|
||||||
(defined(__cplusplus) && defined (_MSC_VER) && (_MSC_VER >= 1600)) || \
|
|
||||||
(defined (_MSC_VER) && (_MSC_VER >= 1800))
|
|
||||||
#define G_STATIC_ASSERT(expr) static_assert (expr, "Expression evaluates to false")
|
#define G_STATIC_ASSERT(expr) static_assert (expr, "Expression evaluates to false")
|
||||||
#else
|
#else
|
||||||
#ifdef __COUNTER__
|
#ifdef __COUNTER__
|
||||||
@ -862,14 +860,14 @@
|
|||||||
#endif /* !__GI_SCANNER__ */
|
#endif /* !__GI_SCANNER__ */
|
||||||
|
|
||||||
/* Provide a string identifying the current code position */
|
/* 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__ "()"
|
#define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
|
||||||
#else
|
#else
|
||||||
#define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
|
#define G_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Provide a string identifying the current function, non-concatenatable */
|
/* 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__))
|
#define G_STRFUNC ((const char*) (__PRETTY_FUNCTION__))
|
||||||
#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
|
#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
|
||||||
#define G_STRFUNC ((const char*) (__func__))
|
#define G_STRFUNC ((const char*) (__func__))
|
||||||
@ -880,7 +878,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Guard C code in headers, while including them from C++ */
|
/* 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_BEGIN_DECLS extern "C" {
|
||||||
#define G_END_DECLS }
|
#define G_END_DECLS }
|
||||||
#else
|
#else
|
||||||
@ -894,16 +892,14 @@
|
|||||||
* defined then the current definition is correct.
|
* defined then the current definition is correct.
|
||||||
*/
|
*/
|
||||||
#ifndef NULL
|
#ifndef NULL
|
||||||
# ifdef __cplusplus
|
# if G_CXX_STD_CHECK_VERSION (11)
|
||||||
# if __cplusplus >= 201103L
|
# define NULL (nullptr)
|
||||||
# define NULL (nullptr)
|
# elif defined (G_CXX_STD_VERSION)
|
||||||
# else
|
# define NULL (0L)
|
||||||
# define NULL (0L)
|
# else
|
||||||
# endif /* __cplusplus >= 201103L */
|
# define NULL ((void*) 0)
|
||||||
# else /* !__cplusplus */
|
# endif /* G_CXX_STD_CHECK_VERSION (11) */
|
||||||
# define NULL ((void*) 0)
|
#elif G_CXX_STD_CHECK_VERSION (11)
|
||||||
# endif /* !__cplusplus */
|
|
||||||
#elif defined (__cplusplus) && __cplusplus >= 201103L
|
|
||||||
# undef NULL
|
# undef NULL
|
||||||
# define NULL (nullptr)
|
# define NULL (nullptr)
|
||||||
#endif
|
#endif
|
||||||
@ -1004,7 +1000,8 @@
|
|||||||
*
|
*
|
||||||
* Since: 2.60
|
* 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) \
|
#define G_ALIGNOF(type) _Alignof (type) \
|
||||||
GLIB_AVAILABLE_MACRO_IN_2_60
|
GLIB_AVAILABLE_MACRO_IN_2_60
|
||||||
#else
|
#else
|
||||||
@ -1064,7 +1061,7 @@
|
|||||||
/* Use MSVC specific syntax. */
|
/* Use MSVC specific syntax. */
|
||||||
# define G_NORETURN __declspec (noreturn)
|
# define G_NORETURN __declspec (noreturn)
|
||||||
/* Use ISO C++11 syntax when the compiler supports it. */
|
/* 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]]
|
# define G_NORETURN [[noreturn]]
|
||||||
/* Use ISO C11 syntax when the compiler supports it. */
|
/* Use ISO C11 syntax when the compiler supports it. */
|
||||||
#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112
|
#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112
|
||||||
@ -1135,7 +1132,7 @@
|
|||||||
* code which includes glib.h, even if the third party code doesn’t use the new
|
* code which includes glib.h, even if the third party code doesn’t use the new
|
||||||
* macro itself. */
|
* macro itself. */
|
||||||
#if g_macro__has_attribute(__always_inline__)
|
#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. */
|
/* Use ISO C++11 syntax when the compiler supports it. */
|
||||||
# define G_ALWAYS_INLINE [[gnu::always_inline]]
|
# define G_ALWAYS_INLINE [[gnu::always_inline]]
|
||||||
# else
|
# else
|
||||||
@ -1179,20 +1176,18 @@
|
|||||||
* code which includes glib.h, even if the third party code doesn’t use the new
|
* code which includes glib.h, even if the third party code doesn’t use the new
|
||||||
* macro itself. */
|
* macro itself. */
|
||||||
#if g_macro__has_attribute(__noinline__)
|
#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. */
|
/* 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
|
# else
|
||||||
# define G_NO_INLINE __attribute__ ((__noinline__))
|
# define G_NO_INLINE __attribute__ ((__noinline__))
|
||||||
# endif
|
# endif
|
||||||
#elif defined (_MSC_VER) && (1200 <= _MSC_VER)
|
#elif defined (_MSC_VER) && (1200 <= _MSC_VER)
|
||||||
/* Use MSVC specific syntax. */
|
/* Use MSVC specific syntax. */
|
||||||
# if defined (__cplusplus) && __cplusplus >= 201103L
|
|
||||||
/* Use ISO C++11 syntax when the compiler supports it. */
|
/* Use ISO C++11 syntax when the compiler supports it. */
|
||||||
# define G_NO_INLINE [[msvc::noinline]]
|
# define G_NO_INLINE __declspec (noinline)
|
||||||
# else
|
|
||||||
# define G_NO_INLINE __declspec (noinline)
|
|
||||||
# endif
|
|
||||||
#else
|
#else
|
||||||
# define G_NO_INLINE /* empty */
|
# define G_NO_INLINE /* empty */
|
||||||
#endif
|
#endif
|
||||||
|
@ -195,7 +195,7 @@ typedef void (*GTestFixtureFunc) (gpointer fixture,
|
|||||||
} G_STMT_END
|
} G_STMT_END
|
||||||
|
|
||||||
/* Use nullptr in C++ to catch misuse of these macros. */
|
/* 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 \
|
#define g_assert_null(expr) G_STMT_START { if G_LIKELY ((expr) == nullptr) ; else \
|
||||||
g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
|
g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
|
||||||
"'" #expr "' should be nullptr"); \
|
"'" #expr "' should be nullptr"); \
|
||||||
|
@ -140,6 +140,8 @@ test_typeof (void)
|
|||||||
MyObject *obj2 = g_rc_box_acquire (obj);
|
MyObject *obj2 = g_rc_box_acquire (obj);
|
||||||
g_assert_true (obj2 == obj);
|
g_assert_true (obj2 == obj);
|
||||||
|
|
||||||
|
G_STATIC_ASSERT (sizeof (glib_typeof (*obj)) == sizeof (glib_typeof (*obj2)));
|
||||||
|
|
||||||
MyObject *obj3 = g_atomic_pointer_get (&obj2);
|
MyObject *obj3 = g_atomic_pointer_get (&obj2);
|
||||||
g_assert_true (obj3 == obj);
|
g_assert_true (obj3 == obj);
|
||||||
|
|
||||||
|
@ -412,7 +412,7 @@ G_BEGIN_DECLS
|
|||||||
* A numerical value which represents the unique identifier of a registered
|
* A numerical value which represents the unique identifier of a registered
|
||||||
* type.
|
* 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;
|
typedef gsize GType;
|
||||||
#else /* for historic reasons, C++ links against gulong GTypes */
|
#else /* for historic reasons, C++ links against gulong GTypes */
|
||||||
typedef gulong GType;
|
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.
|
/* Only use this in non-C++ on GCC >= 2.7, except for Darwin/ppc64.
|
||||||
* See https://bugzilla.gnome.org/show_bug.cgi?id=647145
|
* 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) \
|
#define _G_DEFINE_BOXED_TYPE_BEGIN(TypeName, type_name, copy_func, free_func) \
|
||||||
static GType type_name##_get_type_once (void); \
|
static GType type_name##_get_type_once (void); \
|
||||||
\
|
\
|
||||||
|
Loading…
x
Reference in New Issue
Block a user