Provide private G_SIGNEDNESS_OF macro in glib-private.h

Signed-off-by: Simon McVittie <smcv@debian.org>
This commit is contained in:
Simon McVittie 2024-05-15 12:51:48 +01:00 committed by Philip Withnall
parent 7c41a6529b
commit a0ed94a11d
3 changed files with 17 additions and 11 deletions

View File

@ -28,6 +28,7 @@
#include "girnode-private.h"
#include "gitypelib-internal.h"
#include "glib-private.h"
#include <stdlib.h>
#include <string.h>
@ -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),

View File

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

View File

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