From a0ed94a11d457037c6dc4572fc1cdea492623cdc Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 15 May 2024 12:51:48 +0100 Subject: [PATCH] Provide private G_SIGNEDNESS_OF macro in glib-private.h Signed-off-by: Simon McVittie --- girepository/girparser.c | 13 ++----------- glib/glib-init.c | 5 +++++ glib/glib-private.h | 10 ++++++++++ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/girepository/girparser.c b/girepository/girparser.c index 1ec22213d..dd0fb48b2 100644 --- a/girepository/girparser.c +++ b/girepository/girparser.c @@ -28,6 +28,7 @@ #include "girnode-private.h" #include "gitypelib-internal.h" +#include "glib-private.h" #include #include @@ -451,23 +452,13 @@ typedef struct { unsigned int is_signed : 1; } IntegerAliasInfo; -/* - * signedness: - * @T: a numeric type - * - * Returns: 1 if @T is signed, 0 if it is unsigned - */ -#define signedness(T) (((T) -1) <= 0) -G_STATIC_ASSERT (signedness (int) == 1); -G_STATIC_ASSERT (signedness (unsigned int) == 0); - static IntegerAliasInfo integer_aliases[] = { /* It is platform-dependent whether gchar is signed or unsigned, but * GObject-Introspection has traditionally treated it as signed, * so continue to hard-code that instead of using INTEGER_ALIAS */ { "gchar", sizeof (gchar), 1 }, -#define INTEGER_ALIAS(T) { #T, sizeof (T), signedness (T) } +#define INTEGER_ALIAS(T) { #T, sizeof (T), G_SIGNEDNESS_OF (T) } INTEGER_ALIAS (guchar), INTEGER_ALIAS (gshort), INTEGER_ALIAS (gushort), diff --git a/glib/glib-init.c b/glib/glib-init.c index 7d4a4d5d9..8e2957aaf 100644 --- a/glib/glib-init.c +++ b/glib/glib-init.c @@ -22,6 +22,7 @@ #include "config.h" #include "glib-init.h" +#include "glib-private.h" #include "gmacros.h" #include "gtypes.h" #include "gutils.h" /* for GDebugKey */ @@ -42,6 +43,10 @@ /* This seems as good a place as any to make static assertions about platform * assumptions we make throughout GLib. */ +/* Test that private macro G_SIGNEDNESS_OF() works as intended */ +G_STATIC_ASSERT (G_SIGNEDNESS_OF (int) == 1); +G_STATIC_ASSERT (G_SIGNEDNESS_OF (unsigned int) == 0); + /* We do not support 36-bit bytes or other historical curiosities. */ G_STATIC_ASSERT (CHAR_BIT == 8); diff --git a/glib/glib-private.h b/glib/glib-private.h index c24b59d6f..7ab2579b0 100644 --- a/glib/glib-private.h +++ b/glib/glib-private.h @@ -25,6 +25,16 @@ #include "gstdioprivate.h" #include "gdatasetprivate.h" +/* + * G_SIGNEDNESS_OF: + * @T: a numeric type such as `unsigned int` + * + * An integer constant expression indicating whether @T is a signed type. + * + * Returns: 1 if @T is signed, 0 if it is unsigned + */ +#define G_SIGNEDNESS_OF(T) (((T) -1) <= 0) + /* gcc defines __SANITIZE_ADDRESS__, clang sets the address_sanitizer * feature flag. *