Skip to content

internals: Use Standard C offsetof macro if available

Simon McVittie requested to merge smcv/dbus:offsetof into master

clang 13 fails to compile our current implementation with:

.../dbus/dbus-message.c:2070:3: error: variable length array folded to constant array as an extension [-Werror,-Wgnu-folding-constant]
  _DBUS_STATIC_ASSERT (_DBUS_ALIGNOF (DBusMessageRealIter) <=
  ^
.../dbus/dbus-internals.h:460:25: note: expanded from macro '_DBUS_STATIC_ASSERT'
  typedef struct { char _assertion[(expr) ? 1 : -1]; } \

This appears to be because the "traditional" definition of offsetof(), which we're hard-coding here, does not qualify as a constant expression under C rules due to its use of pointer casts.

Modern compilers like gcc and clang have a built-in implementation of offsetof that is a constant expression.

/cc @rhabacker @pwithnall

Merge request reports