blob: 8757abd7a0e9f4e700f8634f3d67131c562939e7 [file] [log] [blame]
Andrew Geisslerc5535c92023-01-27 16:10:19 -06001From b0fd3a58354b1f5ead891907979dfd3dd36840d5 Mon Sep 17 00:00:00 2001
2From: Khem Raj <raj.khem@gmail.com>
3Date: Sat, 14 Jan 2023 14:55:03 -0800
4Subject: [PATCH] Define alignof_slot using _Alignof when using C11 or newer
5
6WG14 N2350 made very clear that it is an UB having type definitions
7within "offsetof" [1]. This patch enhances the implementation of macro
8alignof_slot to use builtin "_Alignof" to avoid undefined behavior on
9when using std=c11 or newer
10
11clang 16+ has started to flag this [2]
12
13Fixes build when using -std >= gnu11 and using clang16+ [3]
14
15[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm
16[2] https://reviews.llvm.org/D133574
17[3] https://public-inbox.org/bug-gnulib/20230114232744.215167-1-raj.khem@gmail.com/T/#u
18
19Upstream-Status: Backport [https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=2d404c7dd974cc65f894526f4a1b76bc1dcd8d82]
20Signed-off-by: Khem Raj <raj.khem@gmail.com>
21---
22 lib/alignof.h | 2 ++
23 1 file changed, 2 insertions(+)
24
25--- a/lib/alignof.h
26+++ b/lib/alignof.h
27@@ -18,19 +18,19 @@
28 #define _ALIGNOF_H
29
30 #include <stddef.h>
31+#include "stdalign.h"
32
33 /* alignof_slot (TYPE)
34 Determine the alignment of a structure slot (field) of a given type,
35 at compile time. Note that the result depends on the ABI.
36- This is the same as alignof (TYPE) and _Alignof (TYPE), defined in
37- <stdalign.h> if __alignof_is_defined is 1.
38+ This is the same as alignof (TYPE).
39 Note: The result cannot be used as a value for an 'enum' constant,
40 due to bugs in HP-UX 10.20 cc and AIX 3.2.5 xlc. */
41 #if defined __cplusplus
42 template <class type> struct alignof_helper { char __slot1; type __slot2; };
43 # define alignof_slot(type) offsetof (alignof_helper<type>, __slot2)
44 #else
45-# define alignof_slot(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
46+# define alignof_slot(type) alignof (type)
47 #endif
48
49 /* alignof_type (TYPE)