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]

Fix bits/math-finite.h _MSUF_ expansion namespace (bug 22028) [committed]


The current bits/math-finite.h approach to defining functions for
different types, involving math.h defining _MSUF_ and _MSUFTO_ for the
function suffixes involved, is not namespace-clean if one of those
suffixes (f, l, f128) is defined as a macro by the user before math.h
is included; too many levels of macro expansion occur.  Instead, those
suffixes should appear directly in the expansion of the macro using ##
so they don't get expanded even if defined as macros by the user (that
is, math.h should be defining __REDIRFROM_X and __REDIRTO_X directly
to use those suffixes rather than suffixes being passed as an argument
by macro callers).  This patch makes that change.

Tested for x86_64.  Committed.

2017-08-29  Joseph Myers  <joseph@codesourcery.com>

	[BZ #22028]
	* math/math.h [__FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0]
	(_MSUF_): Remove macro.
	[__FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0] (_MSUFTO_):
	Likewise.
	[__FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0]
	(__REDIRFROM_X): New macro.
	[__FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0] (__REDIRTO_X):
	Likewise.
	* math/bits/math-finite.h (__REDIRFROM_X): Remove macro.
	(__REDIRTO_X): Likewise.
	(__MATH_REDIRCALL): Do not pass _MSUF_ or _MSUFTO_ macro
	arguments.
	(__MATH_REDIRCALL_2): Likewise.
	(__MATH_REDIRCALL_INTERNAL): Likewise.
	(__REDIRFROM (lgamma, , _MSUF_)): Likewise.
	(__REDIRFROM (gamma, , _MSUF_)): Likweise.
	(__REDIRFROM (__gamma, _r_finite, _MSUF_)): Likewise.
	(__REDIRFROM (tgamma, , _MSUF_)): Likewise.
	* math/test-finite-macros.c: New file.
	* math/Makefile (tests): Add test-finite-macros.
	(CFLAGS-test-finite-macros.c): New variable.

diff --git a/math/Makefile b/math/Makefile
index 9e88cfc..c47a3a4 100644
--- a/math/Makefile
+++ b/math/Makefile
@@ -196,7 +196,7 @@ tests = test-matherr-3 test-fenv basic-test \
 	test-femode-traps test-iszero-excess-precision \
 	test-iseqsig-excess-precision test-flt-eval-method \
 	test-fp-ilogb-constants test-fp-llogb-constants \
-	test-fe-snans-always-signal $(tests-static)
+	test-fe-snans-always-signal test-finite-macros $(tests-static)
 tests-static = test-fpucw-static test-fpucw-ieee-static \
 	       test-signgam-uchar-static test-signgam-uchar-init-static \
 	       test-signgam-uint-static test-signgam-uint-init-static \
@@ -363,6 +363,8 @@ CFLAGS-test-flt-eval-method.c = -fexcess-precision=standard
 
 CFLAGS-test-fe-snans-always-signal.c = -fsignaling-nans
 
+CFLAGS-test-finite-macros.c = -ffinite-math-only
+
 include ../Rules
 
 gen-all-calls = $(gen-libm-calls) $(gen-calls)
diff --git a/math/bits/math-finite.h b/math/bits/math-finite.h
index 4c82958..de57b50 100644
--- a/math/bits/math-finite.h
+++ b/math/bits/math-finite.h
@@ -20,30 +20,26 @@
 # error "Never use <bits/math-finite.h> directly; include <math.h> instead."
 #endif
 
-#define __REDIRFROM_X(function, reentrant, suffix) \
-  function ## suffix ## reentrant
 #define __REDIRFROM(...) __REDIRFROM_X(__VA_ARGS__)
 
-#define __REDIRTO_X(function, reentrant, suffix) \
-   __ ## function ## suffix ## reentrant ## _finite
 #define __REDIRTO(...) __REDIRTO_X(__VA_ARGS__)
 
 #define __MATH_REDIRCALL_X(from, args, to) \
   extern _Mdouble_ __REDIRECT_NTH (from, args, to)
 #define __MATH_REDIRCALL(function, reentrant, args) \
   __MATH_REDIRCALL_X \
-   (__REDIRFROM (function, reentrant, _MSUF_), args, \
-    __REDIRTO (function, reentrant, _MSUFTO_))
+   (__REDIRFROM (function, reentrant), args, \
+    __REDIRTO (function, reentrant))
 #define __MATH_REDIRCALL_2(from, reentrant, args, to) \
   __MATH_REDIRCALL_X \
-   (__REDIRFROM (from, reentrant, _MSUF_), args, \
-    __REDIRTO (to, reentrant, _MSUFTO_))
+   (__REDIRFROM (from, reentrant), args, \
+    __REDIRTO (to, reentrant))
 
 #define __MATH_REDIRCALL_INTERNAL(function, reentrant, args) \
   __MATH_REDIRCALL_X \
    (__REDIRFROM (__CONCAT (__, function), \
-		 __CONCAT (reentrant, _finite), _MSUF_), \
-    args, __REDIRTO (function, _r, _MSUFTO_))
+		 __CONCAT (reentrant, _finite)), \
+    args, __REDIRTO (function, _r))
 
 
 /* acos.  */
@@ -129,13 +125,13 @@ __MATH_REDIRCALL_INTERNAL (lgamma, _r, (_Mdouble_, int *));
      && defined __extern_always_inline)
 /* lgamma.  */
 __extern_always_inline _Mdouble_
-__NTH (__REDIRFROM (lgamma, , _MSUF_) (_Mdouble_ __d))
+__NTH (__REDIRFROM (lgamma, ) (_Mdouble_ __d))
 {
 # if defined __USE_MISC || defined __USE_XOPEN
-  return __REDIRTO (lgamma, _r, _MSUFTO_) (__d, &signgam);
+  return __REDIRTO (lgamma, _r) (__d, &signgam);
 # else
   int __local_signgam = 0;
-  return __REDIRTO (lgamma, _r, _MSUFTO_) (__d, &__local_signgam);
+  return __REDIRTO (lgamma, _r) (__d, &__local_signgam);
 # endif
 }
 #endif
@@ -144,9 +140,9 @@ __NTH (__REDIRFROM (lgamma, , _MSUF_) (_Mdouble_ __d))
      && defined __extern_always_inline) && !__MATH_DECLARING_FLOATN
 /* gamma.  */
 __extern_always_inline _Mdouble_
-__NTH (__REDIRFROM (gamma, , _MSUF_) (_Mdouble_ __d))
+__NTH (__REDIRFROM (gamma, ) (_Mdouble_ __d))
 {
-  return __REDIRTO (lgamma, _r, _MSUFTO_) (__d, &signgam);
+  return __REDIRTO (lgamma, _r) (__d, &signgam);
 }
 #endif
 
@@ -187,21 +183,19 @@ __MATH_REDIRCALL (sqrt, , (_Mdouble_));
 #if defined __USE_ISOC99 && defined __extern_always_inline
 /* tgamma.  */
 extern _Mdouble_
-__REDIRFROM (__gamma, _r_finite, _MSUF_) (_Mdouble_, int *);
+__REDIRFROM (__gamma, _r_finite) (_Mdouble_, int *);
 
 __extern_always_inline _Mdouble_
-__NTH (__REDIRFROM (tgamma, , _MSUF_) (_Mdouble_ __d))
+__NTH (__REDIRFROM (tgamma, ) (_Mdouble_ __d))
 {
   int __local_signgam = 0;
-  _Mdouble_ __res = __REDIRTO (gamma, _r, _MSUFTO_) (__d, &__local_signgam);
+  _Mdouble_ __res = __REDIRTO (gamma, _r) (__d, &__local_signgam);
   return __local_signgam < 0 ? -__res : __res;
 }
 #endif
 
 #undef __REDIRFROM
-#undef __REDIRFROM_X
 #undef __REDIRTO
-#undef __REDIRTO_X
 #undef __MATH_REDIRCALL
 #undef __MATH_REDIRCALL_2
 #undef __MATH_REDIRCALL_INTERNAL
diff --git a/math/math.h b/math/math.h
index f3eb677..7e41b0d 100644
--- a/math/math.h
+++ b/math/math.h
@@ -644,14 +644,16 @@ iszero (__T __val)
 # define _Mdouble_ double
 # define __MATH_DECLARING_DOUBLE 1
 # define __MATH_DECLARING_FLOATN 0
-# define _MSUF_
-# define _MSUFTO_
+# define __REDIRFROM_X(function, reentrant) \
+  function ## reentrant
+# define __REDIRTO_X(function, reentrant) \
+   __ ## function ## reentrant ## _finite
 # include <bits/math-finite.h>
 # undef _Mdouble_
 # undef __MATH_DECLARING_DOUBLE
 # undef __MATH_DECLARING_FLOATN
-# undef _MSUF_
-# undef _MSUFTO_
+# undef __REDIRFROM_X
+# undef __REDIRTO_X
 
 /* When __USE_ISOC99 is defined, include math-finite for float and
    long double, as well.  */
@@ -661,32 +663,37 @@ iszero (__T __val)
 #  define _Mdouble_ float
 #  define __MATH_DECLARING_DOUBLE 0
 #  define __MATH_DECLARING_FLOATN 0
-#  define _MSUF_ f
-#  define _MSUFTO_ f
+#  define __REDIRFROM_X(function, reentrant) \
+  function ## f ## reentrant
+#  define __REDIRTO_X(function, reentrant) \
+   __ ## function ## f ## reentrant ## _finite
 #  include <bits/math-finite.h>
 #  undef _Mdouble_
 #  undef __MATH_DECLARING_DOUBLE
 #  undef __MATH_DECLARING_FLOATN
-#  undef _MSUF_
-#  undef _MSUFTO_
+#  undef __REDIRFROM_X
+#  undef __REDIRTO_X
 
 /* Include bits/math-finite.h for long double.  */
 #  ifdef __MATH_DECLARE_LDOUBLE
 #   define _Mdouble_ long double
 #   define __MATH_DECLARING_DOUBLE 0
 #   define __MATH_DECLARING_FLOATN 0
-#   define _MSUF_ l
+#   define __REDIRFROM_X(function, reentrant) \
+  function ## l ## reentrant
 #   ifdef __NO_LONG_DOUBLE_MATH
-#    define _MSUFTO_
+#    define __REDIRTO_X(function, reentrant) \
+   __ ## function ## reentrant ## _finite
 #   else
-#    define _MSUFTO_ l
+#    define __REDIRTO_X(function, reentrant) \
+   __ ## function ## l ## reentrant ## _finite
 #   endif
 #   include <bits/math-finite.h>
 #   undef _Mdouble_
 #   undef __MATH_DECLARING_DOUBLE
 #   undef __MATH_DECLARING_FLOATN
-#   undef _MSUF_
-#   undef _MSUFTO_
+#   undef __REDIRFROM_X
+#   undef __REDIRTO_X
 #  endif
 
 # endif /* __USE_ISOC99.  */
@@ -697,18 +704,21 @@ iszero (__T __val)
 #  define _Mdouble_ _Float128
 #  define __MATH_DECLARING_DOUBLE 0
 #  define __MATH_DECLARING_FLOATN 1
-#  define _MSUF_ f128
+#  define __REDIRFROM_X(function, reentrant) \
+  function ## f128 ## reentrant
 #  if __HAVE_DISTINCT_FLOAT128
-#   define _MSUFTO_ f128
+#   define __REDIRTO_X(function, reentrant) \
+   __ ## function ## f128 ## reentrant ## _finite
 #  else
-#   define _MSUFTO_ l
+#   define __REDIRTO_X(function, reentrant) \
+   __ ## function ## l ## reentrant ## _finite
 #  endif
 #  include <bits/math-finite.h>
 #  undef _Mdouble_
 #  undef __MATH_DECLARING_DOUBLE
 #  undef __MATH_DECLARING_FLOATN
-#  undef _MSUF_
-#  undef _MSUFTO_
+#  undef __REDIRFROM_X
+#  undef __REDIRTO_X
 # endif
 #endif /* __FINITE_MATH_ONLY__ > 0.  */
 
diff --git a/math/test-finite-macros.c b/math/test-finite-macros.c
new file mode 100644
index 0000000..94a2f89
--- /dev/null
+++ b/math/test-finite-macros.c
@@ -0,0 +1,35 @@
+/* Test finite-math-only code does not conflict with user macros (bug 22028).
+   Copyright (C) 2017 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/>.  */
+
+/* The main test is that the inclusion of <math.h> compiles.  */
+#define f first test macro
+#define l second test macro
+#define f128 third test macro
+
+#include <math.h>
+
+volatile float a, b;
+
+static int
+do_test (void)
+{
+  b = acosf (a);
+  return 0;
+}
+
+#include <support/test-driver.c>

-- 
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]