From b0fd3a58354b1f5ead891907979dfd3dd36840d5 Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Sat, 14 Jan 2023 14:55:03 -0800
Subject: [PATCH] Define alignof_slot using _Alignof when using C11 or newer

WG14 N2350 made very clear that it is an UB having type definitions
within "offsetof" [1]. This patch enhances the implementation of macro
alignof_slot to use builtin "_Alignof" to avoid undefined behavior on
when using std=c11 or newer

clang 16+ has started to flag this [2]

Fixes build when using -std >= gnu11 and using clang16+ [3]

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm
[2] https://reviews.llvm.org/D133574
[3] https://public-inbox.org/bug-gnulib/20230114232744.215167-1-raj.khem@gmail.com/T/#u

Upstream-Status: Backport [https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=2d404c7dd974cc65f894526f4a1b76bc1dcd8d82]
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 lib/alignof.h | 2 ++
 1 file changed, 2 insertions(+)

--- a/lib/alignof.h
+++ b/lib/alignof.h
@@ -18,19 +18,19 @@
 #define _ALIGNOF_H
 
 #include <stddef.h>
+#include "stdalign.h"
 
 /* alignof_slot (TYPE)
    Determine the alignment of a structure slot (field) of a given type,
    at compile time.  Note that the result depends on the ABI.
-   This is the same as alignof (TYPE) and _Alignof (TYPE), defined in
-   <stdalign.h> if __alignof_is_defined is 1.
+   This is the same as alignof (TYPE).
    Note: The result cannot be used as a value for an 'enum' constant,
    due to bugs in HP-UX 10.20 cc and AIX 3.2.5 xlc.  */
 #if defined __cplusplus
   template <class type> struct alignof_helper { char __slot1; type __slot2; };
 # define alignof_slot(type) offsetof (alignof_helper<type>, __slot2)
 #else
-# define alignof_slot(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
+# define alignof_slot(type) alignof (type)
 #endif
 
 /* alignof_type (TYPE)
