Recommit [libc++] Move abs and div into stdlib.h to fix header cycle.
authorEric Fiselier <eric@efcs.ca>
Sat, 15 Feb 2020 23:55:07 +0000 (18:55 -0500)
committerRaphael Isemann <teemperor@gmail.com>
Tue, 28 Apr 2020 13:42:36 +0000 (15:42 +0200)
commitd0846b432c3040bb04484f30239858d6cff9fc0d
tree565146a83c0e8c7eca42b48b22045c67953761dd
parent8994b14e8b1eb326571411f12dab21909918abe8
Recommit [libc++] Move abs and div into stdlib.h to fix header cycle.

This relands this commit as it broke the LLDB bot the first time it landed.
See also the discussion on https://reviews.llvm.org/rG82b47b2978405f802a33b00d046e6f18ef6a47be

Since D74892 this code should now also work on macOS.

Original description:

libc++ is careful to not fracture overload sets. When one overload
is visible to a user, all of them should be. Anything less causes
subtle bugs and ODR violations.

Previously, in order to support ::abs and ::div being supplied by
both <cmath> and <cstdlib> we had to do awful things that make
<math.h> and <stdlib.h> have header cycles and be non-modular.
This really breaks with modules.

Specifically the problem was that in C++ ::abs introduces overloads
for floating point numbers, these overloads forward to ::fabs,
which are defined in math.h. Therefore ::abs needed to be in math.h
too. But this required stdlib.h to include math.h and math.h to
include stdlib.h.

To avoid these problems the definitions have been moved to stddef.h
(which math includes), and the floating point overloads of ::abs
have been changed to call __builtin_fabs, which both Clang and GCC
support.
libcxx/include/math.h
libcxx/include/stdlib.h