Add utility macros for clang detection, and deprecation with messages.

There are three new macros added to features.h and sys/cdefs.h:

 * __glibc_clang_prereq: just like __GNUC_PREREQ, but for clang.
 * __glibc_clang_has_extension: wraps clang's intrinsic __has_extension.
   Writing "#if defined __clang__ && __has_extension (...)" doesn't work,
   because compilers other than clang will object to the unknown macro
   __has_extension even though they don't need to evaluate it.
   Instead, write "#if __glibc_clang_has_extension (...)".

 * __attribute_deprecated_msg__(msg): like __attribute_deprecated__, but
   if possible, prints a message.

The first two are used to define the third.  The third will be used
in subsequent patches.

	* include/features.h (__glibc_clang_prereq): New macro.
	* misc/sys/cdefs.h (__glibc_clang_has_extension)
	(__attribute_deprecated_msg__): New macros.
This commit is contained in:
Zack Weinberg 2016-04-28 11:07:58 -04:00
parent bf91be88ea
commit cab4d74b01
3 changed files with 42 additions and 5 deletions

View File

@ -1,3 +1,9 @@
2016-08-03 Zack Weinberg <zackw@panix.com>
* include/features.h (__glibc_clang_prereq): New macro.
* misc/sys/cdefs.h (__glibc_clang_has_extension)
(__attribute_deprecated_msg__): New macros.
2016-08-03 Joseph Myers <joseph@codesourcery.com> 2016-08-03 Joseph Myers <joseph@codesourcery.com>
* bits/libc-header-start.h (__GLIBC_USE_IEC_60559_BFP_EXT): New * bits/libc-header-start.h (__GLIBC_USE_IEC_60559_BFP_EXT): New

View File

@ -138,13 +138,13 @@
# define __KERNEL_STRICT_NAMES # define __KERNEL_STRICT_NAMES
#endif #endif
/* Convenience macros to test the versions of glibc and gcc. /* Convenience macro to test the version of gcc.
Use them like this: Use like this:
#if __GNUC_PREREQ (2,8) #if __GNUC_PREREQ (2,8)
... code requiring gcc 2.8 or later ... ... code requiring gcc 2.8 or later ...
#endif #endif
Note - they won't work for gcc1 or glibc1, since the _MINOR macros Note: only works for GCC 2.0 and later, because __GNUC_MINOR__ was
were not defined then. */ added in 2.0. */
#if defined __GNUC__ && defined __GNUC_MINOR__ #if defined __GNUC__ && defined __GNUC_MINOR__
# define __GNUC_PREREQ(maj, min) \ # define __GNUC_PREREQ(maj, min) \
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
@ -152,6 +152,17 @@
# define __GNUC_PREREQ(maj, min) 0 # define __GNUC_PREREQ(maj, min) 0
#endif #endif
/* Similarly for clang. Features added to GCC after version 4.2 may
or may not also be available in clang, and clang's definitions of
__GNUC(_MINOR)__ are fixed at 4 and 2 respectively. Not all such
features can be queried via __has_extension/__has_feature. */
#if defined __clang_major__ && defined __clang_minor__
# define __glibc_clang_prereq(maj, min) \
((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
#else
# define __glibc_clang_prereq(maj, min) 0
#endif
/* Whether to use feature set F. */ /* Whether to use feature set F. */
#define __GLIBC_USE(F) __GLIBC_USE_ ## F #define __GLIBC_USE(F) __GLIBC_USE_ ## F

View File

@ -77,6 +77,15 @@
#endif /* GCC. */ #endif /* GCC. */
/* Compilers that are not clang may object to
#if defined __clang__ && __has_extension(...)
even though they do not need to evaluate the right-hand side of the &&. */
#if defined __clang__ && defined __has_extension
# define __glibc_clang_has_extension(ext) __has_extension (ext)
#else
# define __glibc_clang_has_extension(ext) 0
#endif
/* These two macros are not used in glibc anymore. They are kept here /* These two macros are not used in glibc anymore. They are kept here
only because some other projects expect the macros to be defined. */ only because some other projects expect the macros to be defined. */
#define __P(args) args #define __P(args) args
@ -249,13 +258,24 @@
# define __attribute_noinline__ /* Ignore */ # define __attribute_noinline__ /* Ignore */
#endif #endif
/* gcc allows marking deprecated functions. */ /* Since version 3.2, gcc allows marking deprecated functions. */
#if __GNUC_PREREQ (3,2) #if __GNUC_PREREQ (3,2)
# define __attribute_deprecated__ __attribute__ ((__deprecated__)) # define __attribute_deprecated__ __attribute__ ((__deprecated__))
#else #else
# define __attribute_deprecated__ /* Ignore */ # define __attribute_deprecated__ /* Ignore */
#endif #endif
/* Since version 4.5, gcc also allows one to specify the message printed
when a deprecated function is used. clang claims to be gcc 4.2, but
may also support this feature. */
#if __GNUC_PREREQ (4,5) || \
__glibc_clang_has_extension (__attribute_deprecated_with_message__)
# define __attribute_deprecated_msg__(msg) \
__attribute__ ((__deprecated__ (msg)))
#else
# define __attribute_deprecated_msg__(msg) __attribute_deprecated__
#endif
/* At some point during the gcc 2.8 development the `format_arg' attribute /* At some point during the gcc 2.8 development the `format_arg' attribute
for functions was introduced. We don't want to use it unconditionally for functions was introduced. We don't want to use it unconditionally
(although this would be possible) since it generates warnings. (although this would be possible) since it generates warnings.