[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
improve clang support (15)
From: |
Bruno Haible |
Subject: |
improve clang support (15) |
Date: |
Sun, 09 Aug 2020 16:08:14 +0200 |
User-agent: |
KMail/5.1.3 (Linux/4.4.0-186-generic; KDE/5.18.0; x86_64; ; ) |
clang has __alignof__, like GCC. On Linux/x86 has the same difference
compared to the slot-based alignof as GCC. Whereas on Windows/x86
alignof(double) and alignof(long long) are already == 8, therefore there
alignof and __alignof__ appear to work the same.
The change in lib/stdalign.in.h is probably not needed, because the
Autoconf test reports that clang has a working <stdalign.h> already.
But anyway, it looks more future-proof with the change.
2020-08-09 Bruno Haible <bruno@clisp.org>
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.
diff --git a/lib/alignof.h b/lib/alignof.h
index c72e914..3a8d8d2 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 ed975ef..c1f3d9b 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 56f1a86..c80eebf 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 2eb8480..b1fa21b 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 <class type> struct sa_alignof_helper { char __slot1; type __slot2;
};
diff --git a/lib/stdalign.in.h b/lib/stdalign.in.h
index 831307f..10eedc5 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
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=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 6bcfadb..d8bc8ff 100644
--- a/m4/stddef_h.m4
+++ b/m4/stddef_h.m4
@@ -1,5 +1,5 @@
dnl A placeholder for <stddef.h>, 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 <stddef.h>
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 636c075..4d8c732 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));
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- improve clang support (15),
Bruno Haible <=