This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Support __STDC_WANT_LIB_EXT2__ feature test macro


This patch implements support for the __STDC_WANT_LIB_EXT2__ feature
test macro from ISO/IEC TR 24731-2:2010, thereby implementing one
possible approach for supporting ISO C feature test macros.

Recall that, as described in
<https://sourceware.org/ml/libc-alpha/2016-05/msg00486.html>, these
macros work based on the definition when affected headers are
included, so cannot be handled once when the first system header is
included because that might not be one of the headers the particular
macro in question affects.
<https://sourceware.org/ml/libc-alpha/2016-05/msg00680.html> expresses
views on possible approaches for implementation and
<https://sourceware.org/ml/libc-alpha/2016-06/msg00039.html> follows
up on that.

This patch arranges things so that the relevant condition is
__GLIBC_USE (LIB_EXT2), following one of the suggestions given.
Headers using these macros include <bits/libc-header-start.h>, which
in turn includes <features.h>.  Headers must define
__GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION before including
<bits/libc-header-start.h>, to discourage inclusion outside glibc as
requested.  __USE_GNU conditions on affected functions are changed to
__GLIBC_USE (LIB_EXT2), while it's added as an additional alternative
on the conditions for functions already enabled for some POSIX
versions.

It would be possible to convert existing __USE_* conditionals to
__GLIBC_USE (with the relevant __GLIBC_USE_* being defined in
<features.h> where __USE_* are presently defined), and so make them
typo-proof (given -Wundef -Werror in glibc builds) because __GLIBC_USE
is used with #if not #ifdef / #if defined.

No attempt is made to enforce the rule about diagnosing different
definitions of __STDC_WANT_LIB_EXT2__ when affected headers are
included; such a diagnostic is incompatible with multiple-include
guards on the affected headers, unless compiler extensions are added
to support it.

As previously noted, glibc does not implement all features from TR
24731-2:2010: the functions aswprintf vaswprintf getwdelim getwline
are not in glibc, although they would be appropriate to add if someone
wished to do so.  But I think it makes sense to support the feature
test macro if *any* of the controlled features are present in glibc.

Tested for x86_64 and x86 (testsuite, and that installed stripped
shared libraries are unchanged by the patch).

2016-06-09  Joseph Myers  <joseph@codesourcery.com>

	* bits/libc-header-start.h: New file.
	* Makefile (headers): Add bits/libc-header-start.h.
	* include/features.h (__STDC_WANT_LIB_EXT2__): Document.
	(__GLIBC_USE): New macro.
	* libio/stdio.h: Define
	__GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION and include
	<bits/libc-header-start.h> instead of including <features.h>.
	(fmemopen): Declare also if [__GLIBC_USE (LIB_EXT2)].
	(open_memstream): Likewise.
	(vasprintf): Declare if [__GLIBC_USE (LIB_EXT2)], not [__USE_GNU].
	(__asprintf): Likewise.
	(asprintf): Likewise.
	(__getdelim): Declare also if [__GLIBC_USE (LIB_EXT2)].
	(getdelim): Likewise.
	(getline): Likewise.
	* string/string.h: Define
	__GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION and include
	<bits/libc-header-start.h> instead of including <features.h>.
	(strdup): Declare also if [__GLIBC_USE (LIB_EXT2)]
	(strndup): Likewise.
	* wcsmbs/wchar.h: Define
	__GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION and include
	<bits/libc-header-start.h> instead of including <features.h>.
	(open_wmemstream): Declare also if [__GLIBC_USE (LIB_EXT2)].
	* manual/creature.texi (__STDC_WANT_LIB_EXT2__): Document macro.

diff --git a/Makefile b/Makefile
index 32748b3..4478c97 100644
--- a/Makefile
+++ b/Makefile
@@ -60,7 +60,8 @@ endif # $(AUTOCONF) = no
 		   $(addprefix install-, no-libc.a bin lib data headers others)
 
 headers := limits.h values.h features.h gnu-versions.h \
-	   bits/xopen_lim.h gnu/libc-version.h stdc-predef.h
+	   bits/xopen_lim.h gnu/libc-version.h stdc-predef.h \
+	   bits/libc-header-start.h
 
 echo-headers: subdir_echo-headers
 
diff --git a/NEWS b/NEWS
index 2341697..e1bd53c 100644
--- a/NEWS
+++ b/NEWS
@@ -33,6 +33,11 @@ Version 2.24
   group: files [SUCCESS=merge] nis
   Implemented by Stephen Gallagher (Red Hat).
 
+* The feature test macro __STDC_WANT_LIB_EXT2__, from ISO/IEC TR
+  24731-2:2010, is supported to enable declarations of functions from that
+  TR.  Note that not all functions from that TR are supported by the GNU C
+  Library.
+
 Security related changes:
 
 * An unnecessary stack copy in _nss_dns_getnetbyname_r was removed.  It
diff --git a/bits/libc-header-start.h b/bits/libc-header-start.h
new file mode 100644
index 0000000..b120cfe
--- /dev/null
+++ b/bits/libc-header-start.h
@@ -0,0 +1,43 @@
+/* Handle feature test macros at the start of a header.
+   Copyright (C) 2016 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* This header is internal to glibc and should not be included outside
+   of glibc headers.  Headers including it must define
+   __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION first.  This header
+   cannot have multiple include guards because ISO C feature test
+   macros depend on the definition of the macro when an affected
+   header is included, not when the first system header is
+   included.  */
+
+#ifndef __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
+# error "Never include <bits/libc-header-start.h> directly."
+#endif
+
+#undef __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
+
+#include <features.h>
+
+/* ISO/IEC TR 24731-2:2010 defines the __STDC_WANT_LIB_EXT2__
+   macro.  */
+#undef __GLIBC_USE_LIB_EXT2
+#if (defined __USE_GNU							\
+     || (defined __STDC_WANT_LIB_EXT2__ && __STDC_WANT_LIB_EXT2__ > 0))
+# define __GLIBC_USE_LIB_EXT2 1
+#else
+# define __GLIBC_USE_LIB_EXT2 0
+#endif
diff --git a/include/features.h b/include/features.h
index 9514d35..425f64d 100644
--- a/include/features.h
+++ b/include/features.h
@@ -24,6 +24,7 @@
    __STRICT_ANSI__	ISO Standard C.
    _ISOC99_SOURCE	Extensions to ISO C89 from ISO C99.
    _ISOC11_SOURCE	Extensions to ISO C99 from ISO C11.
+   __STDC_WANT_LIB_EXT2__ Extensions to ISO C99 from TR 27431-2:2010.
    _POSIX_SOURCE	IEEE Std 1003.1.
    _POSIX_C_SOURCE	If ==1, like _POSIX_SOURCE; if >=2 add IEEE Std 1003.2;
 			if >=199309L, add IEEE Std 1003.1b-1993;
@@ -58,6 +59,10 @@
    These are defined by this file and are used by the
    header files to decide what to declare or define:
 
+   __GLIBC_USE (F)	Define things from feature set F.  This is defined
+			to 1 or 0; the subsequent macros are either defined
+			or undefined, and those tests should be moved to
+			__GLIBC_USE.
    __USE_ISOC11		Define ISO C11 things.
    __USE_ISOC99		Define ISO C99 things.
    __USE_ISOC95		Define ISO C90 AMD1 (C95) things.
@@ -90,7 +95,14 @@
    explicitly undefined if they are not explicitly defined.
    Feature-test macros that are not defined by the user or compiler
    but are implied by the other feature-test macros defined (or by the
-   lack of any definitions) are defined by the file.  */
+   lack of any definitions) are defined by the file.
+
+   ISO C feature test macros depend on the definition of the macro
+   when an affected header is included, not when the first system
+   header is included, and so they are handled in
+   <bits/libc-header-start.h>, which does not have a multiple include
+   guard.  Feature test macros that can be handled from the first
+   system header included are handled here.  */
 
 
 /* Undefine everything, so we get a clean slate.  */
@@ -139,6 +151,9 @@
 # define __GNUC_PREREQ(maj, min) 0
 #endif
 
+/* Whether to use feature set F.  */
+#define __GLIBC_USE(F)	__GLIBC_USE_ ## F
+
 /* _BSD_SOURCE and _SVID_SOURCE are deprecated aliases for
    _DEFAULT_SOURCE.  If _DEFAULT_SOURCE is present we do not
    issue a warning; the expectation is that the source is being
diff --git a/libio/stdio.h b/libio/stdio.h
index 4511c3c..e37f901 100644
--- a/libio/stdio.h
+++ b/libio/stdio.h
@@ -24,7 +24,8 @@
 
 #if !defined __need_FILE && !defined __need___FILE
 # define _STDIO_H	1
-# include <features.h>
+# define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
+# include <bits/libc-header-start.h>
 
 __BEGIN_DECLS
 
@@ -316,7 +317,7 @@ extern FILE *fopencookie (void *__restrict __magic_cookie,
 			  _IO_cookie_io_functions_t __io_funcs) __THROW __wur;
 #endif
 
-#ifdef __USE_XOPEN2K8
+#if defined __USE_XOPEN2K8 || __GLIBC_USE (LIB_EXT2)
 /* Create a new stream that refers to a memory buffer.  */
 extern FILE *fmemopen (void *__s, size_t __len, const char *__modes)
   __THROW __wur;
@@ -395,7 +396,7 @@ extern int vsnprintf (char *__restrict __s, size_t __maxlen,
 __END_NAMESPACE_C99
 #endif
 
-#ifdef __USE_GNU
+#if __GLIBC_USE (LIB_EXT2)
 /* Write formatted output to a string dynamically allocated with `malloc'.
    Store the address of the string in *PTR.  */
 extern int vasprintf (char **__restrict __ptr, const char *__restrict __f,
@@ -653,7 +654,7 @@ extern char *fgets_unlocked (char *__restrict __s, int __n,
 #endif
 
 
-#ifdef	__USE_XOPEN2K8
+#if defined __USE_XOPEN2K8 || __GLIBC_USE (LIB_EXT2)
 /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
    (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
    NULL), pointing to *N characters of space.  It is realloc'd as
diff --git a/manual/creature.texi b/manual/creature.texi
index 3c68616..65c5928 100644
--- a/manual/creature.texi
+++ b/manual/creature.texi
@@ -166,6 +166,14 @@ macro @code{_ISOC99_SOURCE} should be defined.
 @end defvr
 
 @comment (none)
+@comment ISO
+@defvr Macro __STDC_WANT_LIB_EXT2__
+If you define this macro to the value @code{1}, features from ISO/IEC
+TR 24731-2:2010 (Dynamic Allocation Functions) are enabled.  Only some
+of the features from this TR are supported by @theglibc{}.
+@end defvr
+
+@comment (none)
 @comment GNU
 @defvr Macro _GNU_SOURCE
 If you define this macro, everything is included: @w{ISO C89}, @w{ISO
diff --git a/string/string.h b/string/string.h
index c7f8fde..57deaa4 100644
--- a/string/string.h
+++ b/string/string.h
@@ -22,7 +22,8 @@
 #ifndef	_STRING_H
 #define	_STRING_H	1
 
-#include <features.h>
+#define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
+#include <bits/libc-header-start.h>
 
 __BEGIN_DECLS
 
@@ -166,7 +167,8 @@ extern size_t strxfrm_l (char *__dest, const char *__src, size_t __n,
 			 __locale_t __l) __THROW __nonnull ((2, 4));
 #endif
 
-#if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8
+#if (defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8	\
+     || __GLIBC_USE (LIB_EXT2))
 /* Duplicate S, returning an identical malloc'd string.  */
 extern char *strdup (const char *__s)
      __THROW __attribute_malloc__ __nonnull ((1));
@@ -175,7 +177,7 @@ extern char *strdup (const char *__s)
 /* Return a malloc'd copy of at most N bytes of STRING.  The
    resultant string is terminated even if no null terminator
    appears before STRING[N].  */
-#if defined __USE_XOPEN2K8
+#if defined __USE_XOPEN2K8 || __GLIBC_USE (LIB_EXT2)
 extern char *strndup (const char *__string, size_t __n)
      __THROW __attribute_malloc__ __nonnull ((1));
 #endif
diff --git a/wcsmbs/wchar.h b/wcsmbs/wchar.h
index e413416..9686fcd 100644
--- a/wcsmbs/wchar.h
+++ b/wcsmbs/wchar.h
@@ -24,7 +24,8 @@
 
 #if !defined __need_mbstate_t && !defined __need_wint_t
 # define _WCHAR_H 1
-# include <features.h>
+# define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
+# include <bits/libc-header-start.h>
 #endif
 
 #ifdef _WCHAR_H
@@ -574,10 +575,12 @@ extern wchar_t *wcpcpy (wchar_t *__restrict __dest,
 extern wchar_t *wcpncpy (wchar_t *__restrict __dest,
 			 const wchar_t *__restrict __src, size_t __n)
      __THROW;
+#endif
 
 
 /* Wide character I/O functions.  */
 
+#if defined __USE_XOPEN2K8 || __GLIBC_USE (LIB_EXT2)
 /* Like OPEN_MEMSTREAM, but the stream is wide oriented and produces
    a wide character string.  */
 extern __FILE *open_wmemstream (wchar_t **__bufloc, size_t *__sizeloc) __THROW;

-- 
Joseph S. Myers
joseph@codesourcery.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]