[libcxx-commits] [libcxx] [libc++] Only include math implementation headers from `math.h`. (PR #109073)

Konstantin Varlamov via libcxx-commits libcxx-commits at lists.llvm.org
Tue Sep 17 17:41:29 PDT 2024


https://github.com/var-const created https://github.com/llvm/llvm-project/pull/109073

`math.h` has logic for undefining macros that might be defined in the
C Standard Library. We need to make sure we do that prelude before
referring to any function names that might potentially be macros;
otherwise, valid user code that includes a macro-prone `<math.h>` from
the C Standard Library before including `<cmath>` (or a header that
transitively includes `<cmath>`) ends up broken.


>From cad1eabf0965189987a3bbe081c9fcde055e8404 Mon Sep 17 00:00:00 2001
From: Konstantin Varlamov <varconst at apple.com>
Date: Tue, 17 Sep 2024 17:35:42 -0700
Subject: [PATCH] [libc++] Only include math implementation headers from
 `math.h`.

`math.h` has logic for undefining macros that might be defined in the
C Standard Library. We need to make sure we do that prelude before
referring to any function names that might potentially be macros;
otherwise, valid user code that includes a macro-prone `<math.h>` from
the C Standard Library before including `<cmath>` (or a header that
transitively includes `<cmath>`) ends up broken.
---
 libcxx/include/cmath  | 2 --
 libcxx/include/math.h | 1 +
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/libcxx/include/cmath b/libcxx/include/cmath
index 5d30b151870e0d..d1620d9374b860 100644
--- a/libcxx/include/cmath
+++ b/libcxx/include/cmath
@@ -313,7 +313,6 @@ constexpr long double lerp(long double a, long double b, long double t) noexcept
 */
 
 #include <__config>
-#include <__math/hypot.h>
 #include <__type_traits/enable_if.h>
 #include <__type_traits/is_arithmetic.h>
 #include <__type_traits/is_constant_evaluated.h>
@@ -324,7 +323,6 @@ constexpr long double lerp(long double a, long double b, long double t) noexcept
 #include <limits>
 #include <version>
 
-#include <__math/special_functions.h>
 #include <math.h>
 
 #ifndef _LIBCPP_MATH_H
diff --git a/libcxx/include/math.h b/libcxx/include/math.h
index 509ecc46184da1..1571a800973b52 100644
--- a/libcxx/include/math.h
+++ b/libcxx/include/math.h
@@ -372,6 +372,7 @@ extern "C++" {
 #    include <__math/remainder.h>
 #    include <__math/roots.h>
 #    include <__math/rounding_functions.h>
+#    include <__math/special_functions.h>
 #    include <__math/traits.h>
 #    include <__math/trigonometric_functions.h>
 #    include <__type_traits/enable_if.h>



More information about the libcxx-commits mailing list