2

I recently came across the following warning in one of my applications that uses the GLib:

warning: ISO C prohibits argument conversion to union type [-Wpedantic]
note: in definition of macro '_G_DEFINE_BOXED_TYPE_BEGIN'
 2147 |     _g_register_boxed (g_intern_static_string (#TypeName), copy_func, free_func);

I usually compile with -wpedantic and it was the first time for me to get a warning that couldn't be traced down to my code, but seems to be caused by the internals of the _G_DEFINE_BOXED_TYPE_BEGIN-macro. The warning seems to appear whenever G_DEFINE_BOXED_TYPE is used with a dedicated free or copy function.

An example application may look as the following:

/* boxed_warning.c
 * Produces warning, when compiled with:
 * $ cc `pkg-config --cflags glib-2.0` -Wextra -Wpedantic -Wall -std=gnu11 -O0 -g -o 'boxed_warning.c.o' -c boxed_warning.c
 */
#include <glib.h>
#include <gio/gio.h>

struct _FooBoxed { gdouble x; };
typedef struct _FooBoxed FooBoxed;

static FooBoxed *
foo_boxed_copy (const FooBoxed *boxed)
{
  FooBoxed *result = g_new (FooBoxed, 1);
  *result = *boxed;
  return result;
}

G_DEFINE_BOXED_TYPE (FooBoxed, foo_boxed, (GBoxedCopyFunc) foo_boxed_copy, (GBoxedFreeFunc) g_free)

I'm using glib 2.62.4, but I can reproduce the warning even when compiling with the latest version from git.gnome.org.

Has anyone else experienced this warning when working with the GLib2.0 and found a work-around? Or is the warning indeed related to a wrong usage of the mentioned macro by my code?

  • Most GNU software is written in gnu-c, not ISO C. It's a different language, strictly speaking, and includes some features that most compilers support but are not strictly ISO - like this one. It's only a "wrong" use if you tell the compiler to consider it wrong and issue a warning with -Wpedantic. The semantics of conversion to union type are obvious, and will be correct (assuming you use the type-matching union member in your function). – BadZen Feb 07 '20 at 23:43
  • 1
    If you really get the warm fuzzies from -Wpedantic you can always just C&P from the glib source and redefine the macro in your own code to do the cast explicity! (I personally don't see any benefit in it, tho!) – BadZen Feb 07 '20 at 23:45

0 Answers0