glib/gobject
Thomas Haller 938a1caf89 gtype: avoid "-Wcast-align" warning with optimized G_TYPE_CHECK_INSTANCE_CAST()
We can get a "-Wcast-align", if the target type that we cast to ("ct") has a
larger alignment than GTypeInstance.

That can happen on i686 architecture, if the GObject type has larger
alignment than the parent struct (or GObject). Since on i686, embeding
a "long long" or a "long double" in a struct still does not increase
the alignment beyond 4 bytes, this usually only happens when using the
__attribute__() to increase the alignment (or to have a field that has
the alignment increased).

It can happen on x86_64 when having a "long double" field.

The compiler warning is hard to avoid but not very useful, because it purely
operates on the pointer types at compile time. G_TYPE_CHECK_INSTANCE_CAST()
instead asserts (in non-optimized mode) that the pointer really points
to the expected GTypeInstance (and if that's the case, then the alignment
should be suitable already).

This is like in commit ed553e8e30 ('gtype: Eliminate -Wcast-align warnings
with G_TYPE_CHECK_INSTANCE_CAST'). But also fix the optimized code path.

With the unpatched G_TYPE_CHECK_INSTANCE_CAST() macro, the unit test would
now show the problem (with gcc-9.3.1-2.fc30.i686 or
gcc-12.2.1-4.fc37.x86_64):

  $ export G_DISABLE_CAST_CHECKS=1
  $ export CFLAGS='-Wcast-align=strict'
  $ meson build
  $ ninja -C build
  ...
  In file included from ../gobject/gobject.h:26,
                   from ../gobject/gbinding.h:31,
                   from ../glib/glib-object.h:24,
                   from ../gobject/tests/objects-refcount1.c:2:
  ../gobject/tests/objects-refcount1.c: In function ‘my_test_dispose’:
  ../gobject/gtype.h:2523:42: warning: cast increases required alignment of target type [-Wcast-align]
   2523 | #  define _G_TYPE_CIC(ip, gt, ct)       ((ct*) ip)
        |                                          ^
  ../gobject/gtype.h:517:66: note: in expansion of macro ‘_G_TYPE_CIC’
    517 | #define G_TYPE_CHECK_INSTANCE_CAST(instance, g_type, c_type)    (_G_TYPE_CIC ((instance), (g_type), c_type))
        |                                                                  ^~~~~~~~~~~
  ../gobject/tests/objects-refcount1.c:9:37: note: in expansion of macro ‘G_TYPE_CHECK_INSTANCE_CAST’
      9 | #define MY_TEST(test)              (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
        |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
  ../gobject/tests/objects-refcount1.c:96:10: note: in expansion of macro ‘MY_TEST’
     96 |   test = MY_TEST (object);
        |          ^~~~~~~
2022-12-16 20:48:06 +01:00
..
tests gtype: avoid "-Wcast-align" warning with optimized G_TYPE_CHECK_INSTANCE_CAST() 2022-12-16 20:48:06 +01:00
gatomicarray.c Merge branch 'more-atomic-ops' into 'main' 2022-07-23 11:35:08 +00:00
gatomicarray.h Add SPDX license headers for LGPL-2.1-or-later to various files 2022-06-01 12:44:23 +01:00
gbinding.c gbinding: Explicitly ignore return values from g_param_value_validate() 2022-10-18 15:12:31 +01:00
gbinding.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gbindinggroup.c gobject: add GBindingGroup 2022-01-28 16:01:22 -08:00
gbindinggroup.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gboxed.c gboxed: Add GBookmarkFile as Boxed types 2022-10-14 16:40:56 +01:00
gboxed.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gclosure.c gclosure: Use (transfer floating) for closure construction functions 2022-12-02 11:59:16 +00:00
gclosure.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
genums.c Add SPDX license headers for LGPL-2.1-or-later to various files 2022-06-01 12:44:23 +01:00
genums.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
glib-enumtypes.c.template Revert "meson: Use gnome.mkenum_simple()" 2022-11-24 12:06:10 +00:00
glib-enumtypes.h.template Revert "meson: Use gnome.mkenum_simple()" 2022-11-24 12:06:10 +00:00
glib-genmarshal.in Replace all uses of G_VA_COPY with Standard C va_copy 2022-10-26 18:07:55 +01:00
glib-mkenums.in glib-mkenums: evaluate private symbols too 2022-11-01 15:46:13 +01:00
glib-types.h gboxed: Add GBookmarkFile as Boxed types 2022-10-14 16:40:56 +01:00
gmarshal.c Replace all uses of G_VA_COPY with Standard C va_copy 2022-10-26 18:07:55 +01:00
gmarshal.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gobject_gdb.py py: Various flake8 cleanups 2020-11-17 15:50:07 +00:00
gobject_probes.d Add missing semicolons to gobject_probes.d 2012-10-15 19:48:44 -04:00
gobject_trace.h Add SPDX license headers for LGPL-2.1-or-later to various files 2022-06-01 12:44:23 +01:00
gobject-autocleanups.h Add SPDX license headers for LGPL-2.1-or-later to various files 2022-06-01 12:44:23 +01:00
gobject-query.c Merge branch 'more-spdx' into 'main' 2022-07-05 11:06:49 +00:00
gobject.c Improve notification queue warning 2022-12-12 15:01:19 +00:00
gobject.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gobject.rc.in meson: build Windows resource files 2018-01-04 22:19:30 +01:00
gobject.stp.in glib: Namespace global tapset variables by soname 2016-11-23 10:50:39 +00:00
gobjectnotifyqueue.c Replace most GObject warnings with criticals 2022-08-09 13:18:47 -05:00
gparam.c Merge branch 'mcatanzaro/gtype-criticals' into 'main' 2022-09-19 14:35:12 +00:00
gparam.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gparamspecs.c Empty values are not valid GParamSpec 2022-09-25 14:20:24 +01:00
gparamspecs.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gsignal.c gsignal: Document that floating GClosures are sunk 2022-12-02 11:59:43 +00:00
gsignal.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gsignalgroup.c gobject/gsignalgroup: fix memory leaks on error 2022-05-17 17:34:06 +02:00
gsignalgroup.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gsourceclosure.c Add SPDX license headers for LGPL-2.1-or-later to various files 2022-06-01 12:44:23 +01:00
gsourceclosure.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gtype-private.h Merge branch 'more-spdx' into 'main' 2022-07-05 11:06:49 +00:00
gtype.c gtype: Use is_final node bit to check if a type can be derived 2022-12-12 19:40:58 +01:00
gtype.h gtype: avoid "-Wcast-align" warning with optimized G_TYPE_CHECK_INSTANCE_CAST() 2022-12-16 20:48:06 +01:00
gtypemodule.c Replace most GObject warnings with criticals 2022-08-09 13:18:47 -05:00
gtypemodule.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gtypeplugin.c Add SPDX license headers for LGPL-2.1-or-later to various files 2022-06-01 12:44:23 +01:00
gtypeplugin.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gvalue.c Replace most GObject warnings with criticals 2022-08-09 13:18:47 -05:00
gvalue.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gvaluearray.c Add SPDX license headers for LGPL-2.1-or-later to various files 2022-06-01 12:44:23 +01:00
gvaluearray.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
gvaluecollector.h Add SPDX license headers for LGPL-2.1-or-later to various files 2022-06-01 12:44:23 +01:00
gvaluetransform.c Add SPDX license headers for LGPL-2.1-or-later to various files 2022-06-01 12:44:23 +01:00
gvaluetypes.c gobject: Always ref-sink variants in g_object_set 2022-10-24 10:47:35 +01:00
gvaluetypes.h Rename all visibility macros 2022-10-13 20:53:56 -04:00
libgobject-gdb.py.in Rename gdb macros with _gdb suffix to avoid ns clashes 2016-05-23 10:52:10 -04:00
meson.build Revert "meson: Use gnome.mkenum_simple()" 2022-11-24 12:06:10 +00:00