From 57caf9541d98314102a55dd1cd0b8dc2be747471 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 9 Aug 2020 15:58:10 +0200 Subject: [PATCH] Use __alignof__ with clang. * m4/stddef_h.m4 (gl_STDDEF_H): Test the alignment of max_align_t also on clang. * lib/alignof.h (alignof_type): Use __alignof__ also on clang. * lib/stdalign.in.h (_Alignof): Don't activate the GCC workaround on clang. * lib/malloca.h (sa_alignof): Use __alignof__ also on clang. * lib/bitset/list.c (lbitset_elt_alloc): Use __alignof__ also on clang. * lib/bitset/table.c (tbitset_elt_alloc): Likewise. * tests/test-stddef.c: Very the behaviour of __alignof__ also on clang. --- ChangeLog | 13 +++++++++++++ lib/alignof.h | 6 ++++-- lib/bitset/list.c | 2 +- lib/bitset/table.c | 2 +- lib/malloca.h | 2 +- lib/stdalign.in.h | 14 ++++++++------ m4/stddef_h.m4 | 4 ++-- tests/test-stddef.c | 2 +- 8 files changed, 31 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index 92cdbf8574..7347ea828b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2020-08-09 Bruno Haible + + Use __alignof__ with clang. + * m4/stddef_h.m4 (gl_STDDEF_H): Test the alignment of max_align_t also + on clang. + * lib/alignof.h (alignof_type): Use __alignof__ also on clang. + * lib/stdalign.in.h (_Alignof): Don't activate the GCC workaround on + clang. + * lib/malloca.h (sa_alignof): Use __alignof__ also on clang. + * lib/bitset/list.c (lbitset_elt_alloc): Use __alignof__ also on clang. + * lib/bitset/table.c (tbitset_elt_alloc): Likewise. + * tests/test-stddef.c: Very the behaviour of __alignof__ also on clang. + 2020-08-09 Bruno Haible ignore-value: Simplify on clang. diff --git a/lib/alignof.h b/lib/alignof.h index c72e914406..3a8d8d2343 100644 --- a/lib/alignof.h +++ b/lib/alignof.h @@ -36,12 +36,14 @@ /* alignof_type (TYPE) Determine the good alignment of an object of the given type at compile time. Note that this is not necessarily the same as alignof_slot(type). - For example, with GNU C on x86 platforms: alignof_type(double) = 8, but + For example, with GNU C on x86 platforms and with clang on Linux/x86: + alignof_type(long long) = 8, but alignof_slot(long long) = 4. + And alignof_type(double) = 8, but - when -malign-double is not specified: alignof_slot(double) = 4, - when -malign-double is specified: alignof_slot(double) = 8. 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 __GNUC__ || defined __IBM__ALIGNOF__ +#if defined __GNUC__ || defined __clang__ || defined __IBM__ALIGNOF__ # define alignof_type __alignof__ #else # define alignof_type alignof_slot diff --git a/lib/bitset/list.c b/lib/bitset/list.c index ed975ef001..c1f3d9b153 100644 --- a/lib/bitset/list.c +++ b/lib/bitset/list.c @@ -122,7 +122,7 @@ lbitset_elt_alloc (void) # define OBSTACK_CHUNK_FREE free #endif -#if ! defined __GNUC__ || __GNUC__ < 2 +#if !(defined __GNUC__ || defined __clang__) # define __alignof__(type) 0 #endif diff --git a/lib/bitset/table.c b/lib/bitset/table.c index 56f1a860a4..c80eebffa5 100644 --- a/lib/bitset/table.c +++ b/lib/bitset/table.c @@ -206,7 +206,7 @@ tbitset_elt_alloc (void) # define OBSTACK_CHUNK_FREE free #endif -#if ! defined __GNUC__ || __GNUC__ < 2 +#if !(defined __GNUC__ || defined __clang__) # define __alignof__(type) 0 #endif diff --git a/lib/malloca.h b/lib/malloca.h index 2eb84805e3..b1fa21b33a 100644 --- a/lib/malloca.h +++ b/lib/malloca.h @@ -89,7 +89,7 @@ extern void freea (void *p); /* ------------------- Auxiliary, non-public definitions ------------------- */ /* Determine the alignment of a type at compile time. */ -#if defined __GNUC__ || defined __IBM__ALIGNOF__ +#if defined __GNUC__ || defined __clang__ || defined __IBM__ALIGNOF__ # define sa_alignof __alignof__ #elif defined __cplusplus template struct sa_alignof_helper { char __slot1; type __slot2; }; diff --git a/lib/stdalign.in.h b/lib/stdalign.in.h index 831307ff22..10eedc550e 100644 --- a/lib/stdalign.in.h +++ b/lib/stdalign.in.h @@ -34,11 +34,12 @@ requirement of a structure member (i.e., slot or field) that is of type TYPE, as an integer constant expression. - This differs from GCC's __alignof__ operator, which can yield a - better-performing alignment for an object of that type. For - example, on x86 with GCC, __alignof__ (double) and __alignof__ - (long long) are 8, whereas alignof (double) and alignof (long long) - are 4 unless the option '-malign-double' is used. + This differs from GCC's and clang's __alignof__ operator, which can + yield a better-performing alignment for an object of that type. For + example, on x86 with GCC and on Linux/x86 with clang, + __alignof__ (double) and __alignof__ (long long) are 8, whereas + alignof (double) and alignof (long long) are 4 unless the option + '-malign-double' is used. The result cannot be used as a value for an 'enum' constant, if you want to be portable to HP-UX 10.20 cc and AIX 3.2.5 xlc. @@ -55,7 +56,8 @@ /* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023 . */ #if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \ - || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9))) + || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \ + && !defined __clang__)) # ifdef __cplusplus # if 201103 <= __cplusplus # define _Alignof(type) alignof (type) diff --git a/m4/stddef_h.m4 b/m4/stddef_h.m4 index 6bcfadb74e..d8bc8ff64e 100644 --- a/m4/stddef_h.m4 +++ b/m4/stddef_h.m4 @@ -1,5 +1,5 @@ dnl A placeholder for , for platforms that have issues. -# stddef_h.m4 serial 6 +# stddef_h.m4 serial 7 dnl Copyright (C) 2009-2020 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -19,7 +19,7 @@ AC_DEFUN([gl_STDDEF_H], [AC_LANG_PROGRAM( [[#include unsigned int s = sizeof (max_align_t); - #if defined __GNUC__ || defined __IBM__ALIGNOF__ + #if defined __GNUC__ || defined __clang__ || defined __IBM__ALIGNOF__ int check1[2 * (__alignof__ (double) <= __alignof__ (max_align_t)) - 1]; int check2[2 * (__alignof__ (long double) <= __alignof__ (max_align_t)) - 1]; #endif diff --git a/tests/test-stddef.c b/tests/test-stddef.c index 636c075f17..4d8c732ad9 100644 --- a/tests/test-stddef.c +++ b/tests/test-stddef.c @@ -59,7 +59,7 @@ verify (alignof (ptrdiff_t) <= alignof (max_align_t)); verify (alignof (size_t) <= alignof (max_align_t)); verify (alignof (wchar_t) <= alignof (max_align_t)); verify (alignof (struct d) <= alignof (max_align_t)); -#if defined __GNUC__ || defined __IBM__ALIGNOF__ +#if defined __GNUC__ || defined __clang__ || defined __IBM__ALIGNOF__ verify (__alignof__ (double) <= __alignof__ (max_align_t)); verify (__alignof__ (int) <= __alignof__ (max_align_t)); verify (__alignof__ (long double) <= __alignof__ (max_align_t)); -- 2.39.5