[BZ,#21326] Ensure C99 and C11 interfaces are available for C++

Message ID 20170922181627.GW4582@redhat.com
State New, archived
Headers

Commit Message

Jonathan Wakely Sept. 22, 2017, 6:16 p.m. UTC
  On 22/09/17 17:59 +0000, Joseph Myers wrote:
>On Fri, 22 Sep 2017, Jonathan Wakely wrote:
>
>> +#ifdef __cplusplus
>> +/* This is to enable compatibility for ISO C++17.  */
>> +#if __cplusplus >= 201703L
>> +# define __USE_ISOCXX17	1
>> +# define __USE_ISOC11	1
>> +#endif
>> /* This is to enable compatibility for ISO C++11.
>> -
>> -   So far g++ does not provide a macro.  Check the temporary macro for
>> -   now, too.  */
>> -#if ((defined __cplusplus && __cplusplus >= 201103L)
>> \
>> -     || defined __GXX_EXPERIMENTAL_CXX0X__)
>> +   Check the temporary macro for now, too.  */
>> +#if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__
>> # define __USE_ISOCXX11	1
>
>There is a comment listing all the __USE_ macros at the top of features.h.
>It should be updated to add __USE_ISOCXX17.  (Actually, it needs
>__USE_ISOCXX11 added as well, but that's a preexisting condition.  And I'm
>not sure __USE_ISOCXX17 should be added at all until there is something to
>condition on it.)

Good point. If there was reason to add it without anything using it
then there should be a __USE_ISOCXX14 as well. But since nothing needs
them, they can be left out.

>The existing __USE_* macros are also undefined in features.h before being
>possibly defined, including __USE_ISOCXX11.  If a new __USE_ISOCXX17 is
>added, it should be added to that #undef sequence as well.

Here's a new patch without __USE_ISOCXX17 and listing __USE_ISOCXX11
in the comment at the top.

Tested x86_64-linux.
This patch ensures that the C99 and C11 features required by C++ are
defined according to the value of the __cplusplus macro, and not just
because G++ always defines _GNU_SOURCE.  This will allow G++ to stop
defining _GNU_SOURCE some day, without causing the C99 and C11
interfaces to disappear for C++ programs.

	[BZ #21326]
	* include/features.h [__cplusplus >= 201103] (__USE_ISOC99): Define.
	[__cplusplus >= 201703] (__USE_ISOCXX17, __USE_ISOC11): Define.
	* math/Makefile (test-math-cxx11): New test.
	* math/test-math-cxx11.cc: New file.
---
 ChangeLog               |   8 +++
 include/features.h      |  15 +++--
 math/Makefile           |   5 +-
 math/test-math-cxx11.cc | 161 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 183 insertions(+), 6 deletions(-)
 create mode 100644 math/test-math-cxx11.cc
  

Comments

Florian Weimer Oct. 9, 2017, 1:24 p.m. UTC | #1
On 09/22/2017 08:16 PM, Jonathan Wakely wrote:
> +#ifdef __cplusplus
> +/* This is to enable compatibility for ISO C++17.  */
> +#if __cplusplus >= 201703L
> +# define __USE_ISOC11	1
> +#endif
>   /* This is to enable compatibility for ISO C++11.

The inner #if/#endif should be indented (similar to the inner #define).

> -
> -   So far g++ does not provide a macro.  Check the temporary macro for
> -   now, too.  */
> -#if ((defined __cplusplus && __cplusplus >= 201103L)			      \
> -     || defined __GXX_EXPERIMENTAL_CXX0X__)
> +   Check the temporary macro for now, too.  */
> +#if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__
>   # define __USE_ISOCXX11	1
> +# define __USE_ISOC99	1
> +#endif
>   #endif

I think there is a preexisting issue here which will trigger macro 
redefinition warnings with -Wsystem-headers.  But perhaps I'm mistaken.

Apart from that, the substance of the patch looks okay to me.

Thanks,
Florian
  

Patch

diff --git a/ChangeLog b/ChangeLog
index e4f304e366..12724611cc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@ 
+2017-09-22  Jonathan Wakely  <jwakely@redhat.com>
+
+	[BZ #21326]
+	* include/features.h [__cplusplus >= 201703] (__USE_ISOC11): Define.
+	[__cplusplus >= 201103] (__USE_ISOC99): Define.
+	* math/Makefile (test-math-cxx11): New test.
+	* math/test-math-cxx11.cc: New file.
+
 2017-09-22  Joseph Myers  <joseph@codesourcery.com>
 
 	* csu/Makefile (generated): Do not add version-info.h.
diff --git a/include/features.h b/include/features.h
index c063b33b49..ca5daa9b25 100644
--- a/include/features.h
+++ b/include/features.h
@@ -76,6 +76,7 @@ 
    __USE_ISOC11		Define ISO C11 things.
    __USE_ISOC99		Define ISO C99 things.
    __USE_ISOC95		Define ISO C90 AMD1 (C95) things.
+   __USE_ISOCXX11	Define ISO C++11 things.
    __USE_POSIX		Define IEEE Std 1003.1 things.
    __USE_POSIX2		Define IEEE Std 1003.2 things.
    __USE_POSIX199309	Define IEEE Std 1003.1, and .1b things.
@@ -238,13 +239,17 @@ 
 # define __USE_ISOC95	1
 #endif
 
+#ifdef __cplusplus
+/* This is to enable compatibility for ISO C++17.  */
+#if __cplusplus >= 201703L
+# define __USE_ISOC11	1
+#endif
 /* This is to enable compatibility for ISO C++11.
-
-   So far g++ does not provide a macro.  Check the temporary macro for
-   now, too.  */
-#if ((defined __cplusplus && __cplusplus >= 201103L)			      \
-     || defined __GXX_EXPERIMENTAL_CXX0X__)
+   Check the temporary macro for now, too.  */
+#if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__
 # define __USE_ISOCXX11	1
+# define __USE_ISOC99	1
+#endif
 #endif
 
 /* If none of the ANSI/POSIX macros are defined, or if _DEFAULT_SOURCE
diff --git a/math/Makefile b/math/Makefile
index 0601f3ac43..12e73366fb 100644
--- a/math/Makefile
+++ b/math/Makefile
@@ -207,7 +207,8 @@  tests-internal = test-matherr test-matherr-2
 tests-static += atest-exp atest-sincos atest-exp2
 
 ifneq (,$(CXX))
-tests += test-math-isinff test-math-iszero test-math-issignaling
+tests += test-math-isinff test-math-iszero test-math-issignaling \
+	 test-math-cxx11
 endif
 
 ifneq (no,$(PERL))
@@ -352,6 +353,8 @@  CFLAGS-test-signgam-ullong-init.c = -std=c99
 CFLAGS-test-signgam-ullong-static.c = -std=c99
 CFLAGS-test-signgam-ullong-init-static.c = -std=c99
 
+CFLAGS-test-math-cxx11.cc = -std=c++11
+
 CFLAGS-test-math-isinff.cc = -std=gnu++11
 CFLAGS-test-math-iszero.cc = -std=gnu++11
 CFLAGS-test-math-issignaling.cc = -std=gnu++11
diff --git a/math/test-math-cxx11.cc b/math/test-math-cxx11.cc
new file mode 100644
index 0000000000..5d93e668e6
--- /dev/null
+++ b/math/test-math-cxx11.cc
@@ -0,0 +1,161 @@ 
+/* Test C99 math functions are available in C++11 without _GNU_SOURCE.
+   Copyright (C) 2017 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Jonathan Wakely <jwakely@redhat.com>, 2017.
+
+   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/>.  */
+
+#undef _GNU_SOURCE
+#undef _DEFAULT_SOURCE
+#undef _XOPEN_SOURCE
+#undef _POSIX_SOURCE
+#undef _POSIX_C_SOURCE
+// __STRICT_ANSI__ gets defined by -std=c++11 in CFLAGS
+#include <math.h>
+#include <stdio.h>
+
+static int
+do_test (void)
+{
+#ifdef _GNU_SOURCE
+  printf ("FAIL: _GNU_SOURCE is defined.\n");
+  return 1;
+#endif
+
+#if __cplusplus >= 201103L
+  /* Verify that C11 math functions and types are defined for C++11,
+     without _GNU_SOURCE being defined. [BZ #21326] */
+  (void) FP_INFINITE;
+  (void) FP_NAN;
+  (void) FP_NORMAL;
+  (void) FP_SUBNORMAL;
+  (void) FP_ZERO;
+  double_t d = 1.0;
+  (void) d;
+  float_t f = 1.0f;
+  (void) f;
+  (void) acosh;
+  (void) acoshf;
+  (void) acoshl;
+  (void) asinh;
+  (void) asinhf;
+  (void) asinhl;
+  (void) atanh;
+  (void) atanhf;
+  (void) atanhl;
+  (void) cbrt;
+  (void) cbrtf;
+  (void) cbrtl;
+  (void) copysign;
+  (void) copysignf;
+  (void) copysignl;
+  (void) erf;
+  (void) erff;
+  (void) erfl;
+  (void) erfc;
+  (void) erfcf;
+  (void) erfcl;
+  (void) exp2;
+  (void) exp2f;
+  (void) exp2l;
+  (void) expm1;
+  (void) expm1f;
+  (void) expm1l;
+  (void) fdim;
+  (void) fdimf;
+  (void) fdiml;
+  (void) fma;
+  (void) fmaf;
+  (void) fmal;
+  (void) fmax;
+  (void) fmaxf;
+  (void) fmaxl;
+  (void) fmin;
+  (void) fminf;
+  (void) fminl;
+  (void) hypot;
+  (void) hypotf;
+  (void) hypotl;
+  (void) ilogb;
+  (void) ilogbf;
+  (void) ilogbl;
+  (void) lgamma;
+  (void) lgammaf;
+  (void) lgammal;
+  (void) llrint;
+  (void) llrintf;
+  (void) llrintl;
+  (void) llround;
+  (void) llroundf;
+  (void) llroundl;
+  (void) log1p;
+  (void) log1pf;
+  (void) log1pl;
+  (void) log2;
+  (void) log2f;
+  (void) log2l;
+  (void) logb;
+  (void) logbf;
+  (void) logbl;
+  (void) lrint;
+  (void) lrintf;
+  (void) lrintl;
+  (void) lround;
+  (void) lroundf;
+  (void) lroundl;
+  (void) nan;
+  (void) nanf;
+  (void) nanl;
+  (void) nearbyint;
+  (void) nearbyintf;
+  (void) nearbyintl;
+  (void) nextafter;
+  (void) nextafterf;
+  (void) nextafterl;
+  (void) nexttoward;
+  (void) nexttowardf;
+  (void) nexttowardl;
+  (void) remainder;
+  (void) remainderf;
+  (void) remainderl;
+  (void) remquo;
+  (void) remquof;
+  (void) remquol;
+  (void) rint;
+  (void) rintf;
+  (void) rintl;
+  (void) round;
+  (void) roundf;
+  (void) roundl;
+  (void) scalbln;
+  (void) scalblnf;
+  (void) scalblnl;
+  (void) scalbn;
+  (void) scalbnf;
+  (void) scalbnl;
+  (void) tgamma;
+  (void) tgammaf;
+  (void) tgammal;
+  (void) trunc;
+  (void) truncf;
+  (void) truncl;
+  printf ("PASS: C11 math functions present in C++11 without _GNU_SOURCE.\n");
+#else
+  printf ("UNSUPPORTED: C++11 not enabled.\n");
+#endif
+  return 0;
+}
+
+#include <support/test-driver.c>