Author: ed
Date: Sun Mar 18 09:19:40 2012
New Revision: 233108
URL: http://svn.freebsd.org/changeset/base/233108

Log:
  MFC r228322,228330,228477,228495,228562,228564,228859,228897,229574,230277:
  
    Bring <sys/cdefs.h> in sync with FreeBSD HEAD:
  
    - Add an __alignof() for non-GCC and GCC < 2.95.
    - Attempt to implement the following C11 keywords: _Alignas(),
      _Alignof(), _Noreturn, _Static_assert() and _Thread_local.
    - Add __generic(), which allows us to do _Generic() in a portable
      fashion.
    - Improve __offsetof(), __DECONST(), __DEVOLATILE() and __DEQUALIFY()
      to use __uintptr_t and __size_t to make them work with less
      header prerequisites.
    - Add __has_feature(), __has_include() and __has_builtin() to make it
      easier to test against Clang features.
  
  Tested by:    linimon@'s exp-run (thanks!)

Modified:
  stable/9/sys/sys/cdefs.h
Directory Properties:
  stable/9/sys/   (props changed)

Modified: stable/9/sys/sys/cdefs.h
==============================================================================
--- stable/9/sys/sys/cdefs.h    Sun Mar 18 08:08:06 2012        (r233107)
+++ stable/9/sys/sys/cdefs.h    Sun Mar 18 09:19:40 2012        (r233108)
@@ -218,6 +218,54 @@
 #endif
 #endif
 
+#if !__GNUC_PREREQ__(2, 95)
+#define        __alignof(x)    __offsetof(struct { char __a; x __b; }, __b)
+#endif
+
+/*
+ * Keywords added in C11.
+ */
+#if defined(__cplusplus) && __cplusplus >= 201103L
+#define        _Alignas(e)             alignas(e)
+#define        _Alignof(e)             alignof(e)
+#define        _Noreturn               [[noreturn]]
+#define        _Static_assert(e, s)    static_assert(e, s)
+#define        _Thread_local           thread_local
+#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
+/* Do nothing.  They are language keywords. */
+#else
+/* Not supported.  Implement them using our versions. */
+#define        _Alignas(x)             __aligned(x)
+#define        _Alignof(x)             __alignof(x)
+#define        _Noreturn               __dead2
+#define        _Thread_local           __thread
+#ifdef __COUNTER__
+#define        _Static_assert(x, y)    __Static_assert(x, __COUNTER__)
+#define        __Static_assert(x, y)   ___Static_assert(x, y)
+#define        ___Static_assert(x, y)  typedef char __assert_ ## y[(x) ? 1 : 
-1]
+#else
+#define        _Static_assert(x, y)    struct __hack
+#endif
+#endif
+
+/*
+ * Emulation of C11 _Generic().  Unlike the previously defined C11
+ * keywords, it is not possible to implement this using exactly the same
+ * syntax.  Therefore implement something similar under the name
+ * __generic().  Unlike _Generic(), this macro can only distinguish
+ * between a single type, so it requires nested invocations to
+ * distinguish multiple cases.
+ */
+
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
+#define        __generic(expr, t, yes, no)                                     
\
+       _Generic(expr, t: yes, default: no)
+#elif __GNUC_PREREQ__(3, 1) && !defined(__cplusplus)
+#define        __generic(expr, t, yes, no)                                     
\
+       __builtin_choose_expr(                                          \
+           __builtin_types_compatible_p(__typeof(expr), t), yes, no)
+#endif
+
 #if __GNUC_PREREQ__(2, 96)
 #define        __malloc_like   __attribute__((__malloc__))
 #define        __pure          __attribute__((__pure__))
@@ -319,10 +367,11 @@
 #define __offsetof(type, field)         __builtin_offsetof(type, field)
 #else
 #ifndef __cplusplus
-#define        __offsetof(type, field) ((size_t)(&((type *)0)->field))
+#define        __offsetof(type, field) \
+       ((__size_t)(__uintptr_t)((const volatile void *)&((type *)0)->field))
 #else
 #define __offsetof(type, field)                                        \
-  (__offsetof__ (reinterpret_cast <size_t>                     \
+  (__offsetof__ (reinterpret_cast <__size_t>                   \
                  (&reinterpret_cast <const volatile char &>    \
                   (static_cast<type *> (0)->field))))
 #endif
@@ -450,15 +499,15 @@
 #endif
 
 #ifndef        __DECONST
-#define        __DECONST(type, var)    ((type)(uintptr_t)(const void *)(var))
+#define        __DECONST(type, var)    ((type)(__uintptr_t)(const void *)(var))
 #endif
 
 #ifndef        __DEVOLATILE
-#define        __DEVOLATILE(type, var) ((type)(uintptr_t)(volatile void 
*)(var))
+#define        __DEVOLATILE(type, var) ((type)(__uintptr_t)(volatile void 
*)(var))
 #endif
 
 #ifndef        __DEQUALIFY
-#define        __DEQUALIFY(type, var)  ((type)(uintptr_t)(const volatile void 
*)(var))
+#define        __DEQUALIFY(type, var)  ((type)(__uintptr_t)(const volatile 
void *)(var))
 #endif
 
 /*-
@@ -574,4 +623,14 @@
 #endif
 #endif
 
+#ifndef        __has_feature
+#define        __has_feature(x) 0
+#endif
+#ifndef        __has_include
+#define        __has_include(x) 0
+#endif
+#ifndef        __has_builtin
+#define        __has_builtin(x) 0
+#endif
+
 #endif /* !_SYS_CDEFS_H_ */
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to