use _Alignof to avoid UB in ALIGNOF

WG14 N2350 clearly says that it is an UB having type definitions
within "offsetof" [1]. Clang 16+ has started diagnosing it [2].
This patch changes the implementation of macro
"ALIGNOF" to builtin "_Alignof" to avoid undefined behavior.

_Alignof() return the ABI required minimum alignment.

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm
[2] https://reviews.llvm.org/D133574

Signed-off-by: Khem Raj <raj.khem@gmail.com>
This commit is contained in:
Khem Raj 2023-01-14 10:11:35 -08:00
parent 3333d5bde8
commit 6fec74429f

View File

@ -288,7 +288,6 @@ def c_open(self):
_c('#include "%s.h"', _ns.header)
_c('')
_c('#define ALIGNOF(type) offsetof(struct { char dummy; type member; }, member)')
if _ns.is_ext:
for (n, h) in self.direct_imports:
@ -1266,7 +1265,7 @@ def _c_serialize_helper_fields(context, self,
count += 1
code_lines.append(
'%s xcb_align_to = ALIGNOF(%s);'
'%s xcb_align_to = _Alignof(%s);'
% (space,
'char'
if field.c_field_type == 'void' or field.type.is_switch