From cccb6c7287924a400c40d4c6cd2a9bd6aaec54f0 Mon Sep 17 00:00:00 2001 From: implicitfield <114500360+implicitfield@users.noreply.github.com> Date: Sat, 29 Apr 2023 23:05:59 +0400 Subject: [PATCH] LibC: Drop complex.cpp and move its definitions to complex.h libc++ disallows including LibC's complex.h in C++ mode. This means that a C++ file cannot expect LibC's complex.h to be included, and thus cannot use c-prefixed complex number functions. As a result, complex.cpp is broken when libc++ has a higher include priority than LibC. A check for __cplusplus has been added to complex.h to warn users of toolchains that don't use libc++. --- Userland/Libraries/LibC/CMakeLists.txt | 1 - Userland/Libraries/LibC/complex.cpp | 46 ------------------------ Userland/Libraries/LibC/complex.h | 48 +++++++++++++++++++++----- 3 files changed, 40 insertions(+), 55 deletions(-) delete mode 100644 Userland/Libraries/LibC/complex.cpp diff --git a/Userland/Libraries/LibC/CMakeLists.txt b/Userland/Libraries/LibC/CMakeLists.txt index 9ef599fa1d1..a0d388de548 100644 --- a/Userland/Libraries/LibC/CMakeLists.txt +++ b/Userland/Libraries/LibC/CMakeLists.txt @@ -1,7 +1,6 @@ set(LIBC_SOURCES arpa/inet.cpp assert.cpp - complex.cpp ctype.cpp cxxabi.cpp dirent.cpp diff --git a/Userland/Libraries/LibC/complex.cpp b/Userland/Libraries/LibC/complex.cpp deleted file mode 100644 index 6820e03dab7..00000000000 --- a/Userland/Libraries/LibC/complex.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2022, Peter Elliott - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include - -extern "C" { - -// Function definitions of this form "type (name)(args)" are intentional, to -// prevent macro versions of "name" from being incorrectly expanded. These -// functions are here to provide external linkage to their macro implementations. - -// https://pubs.opengroup.org/onlinepubs/9699919799/functions/creal.html -float(crealf)(float complex z) -{ - return crealf(z); -} - -double(creal)(double complex z) -{ - return creal(z); -} - -long double(creall)(long double complex z) -{ - return creall(z); -} - -// https://pubs.opengroup.org/onlinepubs/9699919799/functions/cimag.html -double(cimag)(double complex z) -{ - return cimag(z); -} - -float(cimagf)(float complex z) -{ - return cimagf(z); -} - -long double(cimagl)(long double complex z) -{ - return cimagl(z); -} -} diff --git a/Userland/Libraries/LibC/complex.h b/Userland/Libraries/LibC/complex.h index 9731d22edf6..1501f3061c8 100644 --- a/Userland/Libraries/LibC/complex.h +++ b/Userland/Libraries/LibC/complex.h @@ -11,6 +11,10 @@ #pragma once +#ifdef __cplusplus +# error "C++ code must not include complex.h. Use AK/Complex.h instead." +#endif + #include #include @@ -25,14 +29,6 @@ __BEGIN_DECLS #define CMPLXF(x, y) ((float complex)__builtin_complex((float)x, (float)y)) #define CMPLXL(x, y) ((long double complex)__builtin_complex((long double)x, (long double)y)) -float crealf(float complex z); -double creal(double complex z); -long double creall(long double complex z); - -double cimag(double complex z); -float cimagf(float complex z); -long double cimagl(long double complex z); - // These are macro implementations of the above functions, so that they will always be inlined. #define creal(z) ((double)__real__((double complex)z)) #define crealf(z) ((float)__real__((float complex)z)) @@ -42,4 +38,40 @@ long double cimagl(long double complex z); #define cimagf(z) ((float)__imag__((float complex)z)) #define cimagl(z) ((long double)__imag__((long double complex)z)) +// Function definitions of this form "type (name)(args)" are intentional, to +// prevent macro versions of "name" from being incorrectly expanded. These +// functions are here to provide external linkage to their macro implementations. + +// https://pubs.opengroup.org/onlinepubs/9699919799/functions/creal.html +inline float(crealf)(float complex z) +{ + return crealf(z); +} + +inline double(creal)(double complex z) +{ + return creal(z); +} + +inline long double(creall)(long double complex z) +{ + return creall(z); +} + +// https://pubs.opengroup.org/onlinepubs/9699919799/functions/cimag.html +inline double(cimag)(double complex z) +{ + return cimag(z); +} + +inline float(cimagf)(float complex z) +{ + return cimagf(z); +} + +inline long double(cimagl)(long double complex z) +{ + return cimagl(z); +} + __END_DECLS