Skip to content

gthread: ignore deprecated declarations in static inline functions

Simon Marchi requested to merge simark/glib:gthread-deprecated into master

With a trivial file that just includes glib.h:

#include <glib.h>

Compiled with:

gcc -c test.c \
  -I /tmp/glib/include/glib-2.0/ \
  -I /tmp/glib/lib/x86_64-linux-gnu/glib-2.0/include \
  -DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_28 \
  -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_28 \
  -fmax-errors=1 \
  -Werror

We get:

In file included from /tmp/glib/include/glib-2.0/glib/gasyncqueue.h:32,
                 from /tmp/glib/include/glib-2.0/glib.h:32,
                 from test.c:1:
/tmp/glib/include/glib-2.0/glib/gthread.h: In function ‘g_rec_mutex_locker_new’:
/tmp/glib/include/glib-2.0/glib/gthread.h:396:3: error: ‘g_rec_mutex_lock’ is deprecated: Not available before 2.32 [-Werror=deprecated-declarations]
  396 |   g_rec_mutex_lock (rec_mutex);
      |   ^~~~~~~~~~~~~~~~
/tmp/glib/include/glib-2.0/glib/gthread.h:196:17: note: declared here
  196 | void            g_rec_mutex_lock                (GRecMutex      *rec_mutex);
      |                 ^~~~~~~~~~~~~~~~
compilation terminated due to -fmax-errors=1.

The problem is that the code in the static inline functions uses g_rec_mutex_lock, introduced after 2.28. This code is compiled regardless of if it's actually used or not.

Suppress the warning by using G_GNUC_BEGIN_IGNORE_DEPRECATIONS / G_GNUC_END_IGNORE_DEPRECATIONS. There are precedents for doing that, for example g_main_context_pusher_new in gmain.h.

Tested by building with all variations of GLIB_VERSION_MIN_REQUIRED / GLIB_VERSION_MAX_ALLOWED:

for i in $(seq 26 2 64); do
  gcc -c test.c \
    -I/tmp/glib/include/glib-2.0 \
    -I/tmp/glib/lib/x86_64-linux-gnu/glib-2.0/include \
    -DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_$i \
    -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_$i \
    -fmax-errors=1 \
    -Werror
done

Fixes: #2094 (closed)

Merge request reports