Feature test macros overhaul: sys/features.h

This is the complete rework of the feature tests macros for better
compatibility with GNU libc, primarily based on the Linux man pages
documentation:

http://man7.org/linux/man-pages/man7/feature_test_macros.7.html

The previous implementation was flawed in its approach that macros were
often used to hide symbols if defined (e.g. !defined __STRICT_ANSI__ or
!defined _POSIX_SOURCE), whereas the approach of glibc is that these macros
make symbols available when defined (e.g. defined _BSD_SOURCE, or as used
internally, #if __BSD_VISIBLE).  As much open-source software is written
with glibc in mind, this necessitated patching numerous packages just to
compile.

In particular, __STRICT_ANSI__ (which is defined by gcc -ansi or -std=c*)
was given too much importance.  This implementation limits the influence
of __STRICT_ANSI__ to controlling the default when no other feature test
macros are defined, and to the inclusion of <alloca.h> in <stdlib.h> as
documented.  These are the only places where __STRICT_ANSI__ should be
tested.

The following macros are now accepted: _ATFILE_SOURCE, _BSD_SOURCE,
_DEFAULT_SOURCE, _ISOC99_SOURCE, _ISOC11_SOURCE, _LARGEFILE_SOURCE,
_SVID_SOURCE, _XOPEN_SOURCE_EXTENDED.

The existing __*_VISIBLE internal macros have been kept mostly
compatible with the original BSD implementation, with some changes to
the criteria which controls them.  Several more macros in this style
have been added where needed for concision or accuracy.

Enabling C++11 or newer in the compiler also enables C99 and C11
functions.  Doing so should help move away from the need to define
_GNU_SOURCE in g++ for _GLIBCXX_USE_C99 support as on Linux:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51749

Signed-off-by: Yaakov Selkowitz <yselkowi@redhat.com>
This commit is contained in:
Yaakov Selkowitz 2016-03-14 15:31:23 -05:00
parent 446d0d355f
commit 6ba26630e0
3 changed files with 275 additions and 148 deletions

View File

@ -624,132 +624,6 @@
#define __DEQUALIFY(type, var) ((type)(__uintptr_t)(const volatile void *)(var))
#endif
/*-
* The following definitions are an extension of the behavior originally
* implemented in <sys/_posix.h>, but with a different level of granularity.
* POSIX.1 requires that the macros we test be defined before any standard
* header file is included.
*
* Here's a quick run-down of the versions:
* defined(_POSIX_SOURCE) 1003.1-1988
* _POSIX_C_SOURCE == 1 1003.1-1990
* _POSIX_C_SOURCE == 2 1003.2-1992 C Language Binding Option
* _POSIX_C_SOURCE == 199309 1003.1b-1993
* _POSIX_C_SOURCE == 199506 1003.1c-1995, 1003.1i-1995,
* and the omnibus ISO/IEC 9945-1: 1996
* _POSIX_C_SOURCE == 200112 1003.1-2001
* _POSIX_C_SOURCE == 200809 1003.1-2008
*
* In addition, the X/Open Portability Guide, which is now the Single UNIX
* Specification, defines a feature-test macro which indicates the version of
* that specification, and which subsumes _POSIX_C_SOURCE.
*
* Our macros begin with two underscores to avoid namespace screwage.
*/
/* Deal with _GNU_SOURCE, which implies everything and the kitchen sink */
#ifdef _GNU_SOURCE
#undef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#define __BSD_VISIBLE 1
#define __GNU_VISIBLE 1
#endif
/* Deal with IEEE Std. 1003.1-1990, in which _POSIX_C_SOURCE == 1. */
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 1
#undef _POSIX_C_SOURCE /* Probably illegal, but beyond caring now. */
#define _POSIX_C_SOURCE 199009
#endif
/* Deal with IEEE Std. 1003.2-1992, in which _POSIX_C_SOURCE == 2. */
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 2
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199209
#endif
/* Deal with various X/Open Portability Guides and Single UNIX Spec. */
#ifdef _XOPEN_SOURCE
#if _XOPEN_SOURCE - 0 >= 700
#define __XSI_VISIBLE 700
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809
#elif _XOPEN_SOURCE - 0 >= 600
#define __XSI_VISIBLE 600
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112
#elif _XOPEN_SOURCE - 0 >= 500
#define __XSI_VISIBLE 500
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199506
#endif
#endif
/*
* Deal with all versions of POSIX. The ordering relative to the tests above is
* important.
*/
#if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE)
#define _POSIX_C_SOURCE 198808
#endif
#ifdef _POSIX_C_SOURCE
#if _POSIX_C_SOURCE >= 200809
#define __POSIX_VISIBLE 200809
#define __ISO_C_VISIBLE 1999
#elif _POSIX_C_SOURCE >= 200112
#define __POSIX_VISIBLE 200112
#define __ISO_C_VISIBLE 1999
#elif _POSIX_C_SOURCE >= 199506
#define __POSIX_VISIBLE 199506
#define __ISO_C_VISIBLE 1990
#elif _POSIX_C_SOURCE >= 199309
#define __POSIX_VISIBLE 199309
#define __ISO_C_VISIBLE 1990
#elif _POSIX_C_SOURCE >= 199209
#define __POSIX_VISIBLE 199209
#define __ISO_C_VISIBLE 1990
#elif _POSIX_C_SOURCE >= 199009
#define __POSIX_VISIBLE 199009
#define __ISO_C_VISIBLE 1990
#else
#define __POSIX_VISIBLE 198808
#define __ISO_C_VISIBLE 0
#endif /* _POSIX_C_SOURCE */
#else
/*-
* Deal with _ANSI_SOURCE:
* If it is defined, and no other compilation environment is explicitly
* requested, then define our internal feature-test macros to zero. This
* makes no difference to the preprocessor (undefined symbols in preprocessing
* expressions are defined to have value zero), but makes it more convenient for
* a test program to print out the values.
*
* If a program mistakenly defines _ANSI_SOURCE and some other macro such as
* _POSIX_C_SOURCE, we will assume that it wants the broader compilation
* environment (and in fact we will never get here).
*/
#if defined(_ANSI_SOURCE) /* Hide almost everything. */
#define __POSIX_VISIBLE 0
#define __XSI_VISIBLE 0
#define __BSD_VISIBLE 0
#define __ISO_C_VISIBLE 1990
#elif defined(_C99_SOURCE) /* Localism to specify strict C99 env. */
#define __POSIX_VISIBLE 0
#define __XSI_VISIBLE 0
#define __BSD_VISIBLE 0
#define __ISO_C_VISIBLE 1999
#elif defined(_C11_SOURCE) /* Localism to specify strict C11 env. */
#define __POSIX_VISIBLE 0
#define __XSI_VISIBLE 0
#define __BSD_VISIBLE 0
#define __ISO_C_VISIBLE 2011
#else /* Default: everything except __GNU_VISIBLE. */
#define __POSIX_VISIBLE 200809
#define __XSI_VISIBLE 700
#define __BSD_VISIBLE 1
#define __ISO_C_VISIBLE 2011
#endif
#endif
/*
* Type Safety Checking
*

View File

@ -232,9 +232,6 @@
#if defined(__CYGWIN__)
#include <cygwin/config.h>
#if !defined (__STRICT_ANSI__) || (__STDC_VERSION__ >= 199901L)
#define __USE_XOPEN2K 1
#endif
#endif
#if defined(__rtems__)

View File

@ -39,6 +39,281 @@ extern "C" {
/* Version with trailing underscores for BSD compatibility. */
#define __GNUC_PREREQ__(ma, mi) __GNUC_PREREQ(ma, mi)
/*
* Feature test macros control which symbols are exposed by the system
* headers. Any of these must be defined before including any headers.
*
* __STRICT_ANSI__ (defined by gcc -ansi, -std=c90, -std=c99, or -std=c11)
* ISO C
*
* _POSIX_SOURCE (deprecated by _POSIX_C_SOURCE=1)
* _POSIX_C_SOURCE >= 1
* POSIX.1-1990
*
* _POSIX_C_SOURCE >= 2
* POSIX.2-1992
*
* _POSIX_C_SOURCE >= 199309L
* POSIX.1b-1993 Real-time extensions
*
* _POSIX_C_SOURCE >= 199506L
* POSIX.1c-1995 Threads extensions
*
* _POSIX_C_SOURCE >= 200112L
* POSIX.1-2001 and C99
*
* _POSIX_C_SOURCE >= 200809L
* POSIX.1-2008
*
* _XOPEN_SOURCE
* POSIX.1-1990 and XPG4
*
* _XOPEN_SOURCE_EXTENDED
* SUSv1 (POSIX.2-1992 plus XPG4v2)
*
* _XOPEN_SOURCE >= 500
* SUSv2 (POSIX.1c-1995 plus XSI)
*
* _XOPEN_SOURCE >= 600
* SUSv3 (POSIX.1-2001 plus XSI) and C99
*
* _XOPEN_SOURCE >= 700
* SUSv4 (POSIX.1-2008 plus XSI)
*
* _ISOC99_SOURCE or gcc -std=c99 or g++ -std=c++11
* ISO C99
*
* _ISOC11_SOURCE or gcc -std=c11 or g++ -std=c++11
* ISO C11
*
* _ATFILE_SOURCE (implied by _POSIX_C_SOURCE >= 200809L)
* "at" functions
*
* _LARGEFILE_SOURCE (deprecated by _XOPEN_SOURCE >= 500)
* fseeko, ftello
*
* _GNU_SOURCE
* All of the above plus GNU extensions
*
* _BSD_SOURCE (deprecated by _DEFAULT_SOURCE)
* _SVID_SOURCE (deprecated by _DEFAULT_SOURCE)
* _DEFAULT_SOURCE (or none of the above)
* POSIX-1.2008 with BSD and SVr4 extensions
*/
#ifdef _GNU_SOURCE
#undef _ATFILE_SOURCE
#define _ATFILE_SOURCE 1
#undef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE 1
#undef _ISOC99_SOURCE
#define _ISOC99_SOURCE 1
#undef _ISOC11_SOURCE
#define _ISOC11_SOURCE 1
#undef _POSIX_SOURCE
#define _POSIX_SOURCE 1
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#undef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#undef _XOPEN_SOURCE_EXTENDED
#define _XOPEN_SOURCE_EXTENDED 1
#endif /* _GNU_SOURCE */
#if defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || \
(!defined(__STRICT_ANSI__) && !defined(_ANSI_SOURCE) && \
!defined(_ISOC99_SOURCE) && !defined(_POSIX_SOURCE) && \
!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE))
#undef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE 1
#endif
#if defined(_DEFAULT_SOURCE)
#undef _POSIX_SOURCE
#define _POSIX_SOURCE 1
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
#if !defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE) && \
((!defined(__STRICT_ANSI__) && !defined(_ANSI_SOURCE)) || \
(_XOPEN_SOURCE - 0) >= 500)
#define _POSIX_SOURCE 1
#if !defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE - 0) >= 700
#define _POSIX_C_SOURCE 200809L
#elif (_XOPEN_SOURCE - 0) >= 600
#define _POSIX_C_SOURCE 200112L
#elif (_XOPEN_SOURCE - 0) >= 500
#define _POSIX_C_SOURCE 199506L
#elif (_XOPEN_SOURCE - 0) < 500
#define _POSIX_C_SOURCE 2
#endif
#endif
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809
#undef _ATFILE_SOURCE
#define _ATFILE_SOURCE 1
#endif
/*
* The following private macros are used throughout the headers to control
* which symbols should be exposed. They are for internal use only, as
* indicated by the leading double underscore, and must never be used outside
* of these headers.
*
* __POSIX_VISIBLE
* any version of POSIX.1; enabled by default, or with _POSIX_SOURCE,
* any value of _POSIX_C_SOURCE, or _XOPEN_SOURCE >= 500.
*
* __POSIX_VISIBLE >= 2
* POSIX.2-1992; enabled by default, with _POSIX_C_SOURCE >= 2,
* or _XOPEN_SOURCE >= 500.
*
* __POSIX_VISIBLE >= 199309
* POSIX.1b-1993; enabled by default, with _POSIX_C_SOURCE >= 199309L,
* or _XOPEN_SOURCE >= 500.
*
* __POSIX_VISIBLE >= 199506
* POSIX.1c-1995; enabled by default, with _POSIX_C_SOURCE >= 199506L,
* or _XOPEN_SOURCE >= 500.
*
* __POSIX_VISIBLE >= 200112
* POSIX.1-2001; enabled by default, with _POSIX_C_SOURCE >= 200112L,
* or _XOPEN_SOURCE >= 600.
*
* __POSIX_VISIBLE >= 200809
* POSIX.1-2008; enabled by default, with _POSIX_C_SOURCE >= 200809L,
* or _XOPEN_SOURCE >= 700.
*
* __XSI_VISIBLE
* XPG4 XSI extensions; enabled with any version of _XOPEN_SOURCE.
*
* __XSI_VISIBLE >= 4
* SUSv1 XSI extensions; enabled with both _XOPEN_SOURCE and
* _XOPEN_SOURCE_EXTENDED together.
*
* __XSI_VISIBLE >= 500
* SUSv2 XSI extensions; enabled with _XOPEN_SOURCE >= 500.
*
* __XSI_VISIBLE >= 600
* SUSv3 XSI extensions; enabled with _XOPEN_SOURCE >= 600.
*
* __XSI_VISIBLE >= 700
* SUSv4 XSI extensions; enabled with _XOPEN_SOURCE >= 700.
*
* __ISO_C_VISIBLE >= 1999
* ISO C99; enabled with gcc -std=c99 or newer (on by default since GCC 5),
* g++ -std=c++11 or newer (on by default since GCC 6), or with
* _ISOC99_SOURCE, _POSIX_C_SOURCE >= 200112L, or _XOPEN_SOURCE >= 600.
*
* __ISO_C_VISIBLE >= 2011
* ISO C11; enabled with gcc -std=c11 or newer (on by default since GCC 5),
* g++ -std=c++11 or newer (on by default since GCC 6), or with
* _ISOC11_SOURCE.
*
* __ATFILE_VISIBLE
* "at" functions; enabled by default, with _ATFILE_SOURCE,
* _POSIX_C_SOURCE >= 200809L, or _XOPEN_SOURCE >= 700.
*
* __LARGEFILE_VISIBLE
* fseeko, ftello; enabled with _LARGEFILE_SOURCE or _XOPEN_SOURCE >= 500.
*
* __BSD_VISIBLE
* BSD extensions; enabled by default, or with _BSD_SOURCE.
*
* __SVID_VISIBLE
* SVr4 extensions; enabled by default, or with _SVID_SOURCE.
*
* __MISC_VISIBLE
* Extensions found in both BSD and SVr4 (shorthand for
* (__BSD_VISIBLE || __SVID_VISIBLE)), or newlib-specific
* extensions; enabled by default.
*
* __GNU_VISIBLE
* GNU extensions; enabled with _GNU_SOURCE.
*
* In all cases above, "enabled by default" means either by defining
* _DEFAULT_SOURCE, or by not defining any of the public feature test macros.
* Defining _GNU_SOURCE makes all of the above avaliable.
*/
#ifdef _ATFILE_SOURCE
#define __ATFILE_VISIBLE 1
#else
#define __ATFILE_VISIBLE 0
#endif
#ifdef _DEFAULT_SOURCE
#define __BSD_VISIBLE 1
#else
#define __BSD_VISIBLE 0
#endif
#ifdef _GNU_SOURCE
#define __GNU_VISIBLE 1
#else
#define __GNU_VISIBLE 0
#endif
#if defined(_ISOC11_SOURCE) || \
(__STDC_VERSION__ - 0) >= 201112L || (__cplusplus - 0) >= 201103L
#define __ISO_C_VISIBLE 2011
#elif defined(_ISOC99_SOURCE) || \
(__STDC_VERSION__ - 0) >= 199901L || (_POSIX_C_SOURCE - 0) >= 200112L
#define __ISO_C_VISIBLE 1999
#else
#define __ISO_C_VISIBLE 1990
#endif
#if defined(_LARGEFILE_SOURCE) || (_XOPEN_SOURCE - 0) >= 500
#define __LARGEFILE_VISIBLE 1
#else
#define __LARGEFILE_VISIBLE 0
#endif
#ifdef _DEFAULT_SOURCE
#define __MISC_VISIBLE 1
#else
#define __MISC_VISIBLE 0
#endif
#if (_POSIX_C_SOURCE - 0) >= 200809L
#define __POSIX_VISIBLE 200809
#elif (_POSIX_C_SOURCE - 0) >= 200112L
#define __POSIX_VISIBLE 200112
#elif (_POSIX_C_SOURCE - 0) >= 199506L
#define __POSIX_VISIBLE 199506
#elif (_POSIX_C_SOURCE - 0) >= 199309L
#define __POSIX_VISIBLE 199309
#elif (_POSIX_C_SOURCE - 0) >= 2 || defined(_XOPEN_SOURCE)
#define __POSIX_VISIBLE 199209
#elif defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE)
#define __POSIX_VISIBLE 199009
#else
#define __POSIX_VISIBLE 0
#endif
#ifdef _DEFAULT_SOURCE
#define __SVID_VISIBLE 1
#else
#define __SVID_VISIBLE 0
#endif
#if (_XOPEN_SOURCE - 0) >= 700
#define __XSI_VISIBLE 700
#elif (_XOPEN_SOURCE - 0) >= 600
#define __XSI_VISIBLE 600
#elif (_XOPEN_SOURCE - 0) >= 500
#define __XSI_VISIBLE 500
#elif defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)
#define __XSI_VISIBLE 4
#elif defined(_XOPEN_SOURCE)
#define __XSI_VISIBLE 1
#else
#define __XSI_VISIBLE 0
#endif
/* RTEMS adheres to POSIX -- 1003.1b with some features from annexes. */
#ifdef __rtems__
@ -206,25 +481,6 @@ extern "C" {
#endif /* __CYGWIN__ */
/* Per the permission given in POSIX.1-2008 section 2.2.1, define
* _POSIX_C_SOURCE if _XOPEN_SOURCE is defined and _POSIX_C_SOURCE is not.
* (_XOPEN_SOURCE indicates that XSI extensions are desired by an application.)
* This permission is first granted in 2008, but use it for older ones, also.
* Allow for _XOPEN_SOURCE to be empty (from the earliest form of it, before it
* was required to have specific values).
*/
#if !defined(_POSIX_C_SOURCE) && defined(_XOPEN_SOURCE)
#if (_XOPEN_SOURCE - 0) == 700 /* POSIX.1-2008 */
#define _POSIX_C_SOURCE 200809L
#elif (_XOPEN_SOURCE - 0) == 600 /* POSIX.1-2001 or 2004 */
#define _POSIX_C_SOURCE 200112L
#elif (_XOPEN_SOURCE - 0) == 500 /* POSIX.1-1995 */
#define _POSIX_C_SOURCE 199506L
#elif (_XOPEN_SOURCE - 0) < 500 /* really old */
#define _POSIX_C_SOURCE 2
#endif
#endif
#ifdef __cplusplus
}
#endif