Skip to content

[libc] Remove the #include <stdlib.h> header #114453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
5 commits merged into from Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions libc/hdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ add_proxy_header_library(
libc.include.signal
)

add_proxy_header_library(
stdlib_macros
HDRS
stdlib_macros.h
DEPENDS
.stdlib_overlay
FULL_BUILD_DEPENDS
libc.include.stdlib
libc.include.llvm-libc-macros.stdlib_macros
)

add_header_library(stdio_overlay HDRS stdio_overlay.h)

add_proxy_header_library(
Expand Down
22 changes: 22 additions & 0 deletions libc/hdr/stdlib_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Definition of macros from stdlib.h --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_STDLIB_MACROS_H
#define LLVM_LIBC_HDR_STDLIB_MACROS_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-macros/stdlib-macros.h"

#else // Overlay mode

#include "stdlib_overlay.h"

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_STDLIB_MACROS_H
36 changes: 36 additions & 0 deletions libc/hdr/stdlib_overlay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===-- Including stdlib.h in overlay mode --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_STDLIB_OVERLAY_H
#define LLVM_LIBC_HDR_STDLIB_OVERLAY_H

#ifdef LIBC_FULL_BUILD
#error "This header should only be included in overlay mode"
#endif

// Overlay mode

// glibc <stdlib.h> header might provide extern inline definitions for few
// functions, causing external alias errors. They are guarded by
// `__USE_FORTIFY_LEVEL`, which will be temporarily disabled.

#ifdef __USE_FORTIFY_LEVEL
#define LIBC_OLD_USE_FORTIFY_LEVEL __USE_FORTIFY_LEVEL
#undef __USE_FORTIFY_LEVEL
#define __USE_FORTIFY_LEVEL 0
#endif

#include <stdlib.h>

#ifdef LIBC_OLD_USE_FORTIFY_LEVEL
#undef __USE_FORTIFY_LEVEL
#define __USE_FORTIFY_LEVEL LIBC_OLD_USE_FORTIFY_LEVEL
#undef LIBC_OLD_USE_FORTIFY_LEVEL
#endif

#endif
41 changes: 41 additions & 0 deletions libc/hdr/types/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
add_proxy_header_library(
div_t
HDRS
div_t.h
DEPENDS
libc.hdr.stdlib_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.div_t
libc.include.stdlib
)

add_proxy_header_library(
ldiv_t
HDRS
ldiv_t.h
DEPENDS
libc.hdr.stdlib_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.ldiv_t
libc.include.stdlib
)

add_proxy_header_library(
lldiv_t
HDRS
lldiv_t.h
DEPENDS
libc.hdr.stdlib_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.lldiv_t
libc.include.stdlib
)

add_proxy_header_library(
sigset_t
HDRS
Expand Down Expand Up @@ -46,6 +79,14 @@ add_proxy_header_library(
libc.include.llvm-libc-types.struct_timespec
)

add_proxy_header_library(
size_t
HDRS
size_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.size_t
)

add_proxy_header_library(
mode_t
HDRS
Expand Down
22 changes: 22 additions & 0 deletions libc/hdr/types/div_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Definition of macros from div_t.h ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_TYPES_DIV_T_H
#define LLVM_LIBC_HDR_TYPES_DIV_T_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/div_t.h"

#else // Overlay mode

#include "hdr/stdlib_overlay.h"

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_DIV_T_H
22 changes: 22 additions & 0 deletions libc/hdr/types/ldiv_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Definition of macros from ldiv_t.h --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_TYPES_LDIV_T_H
#define LLVM_LIBC_HDR_TYPES_LDIV_T_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/ldiv_t.h"

#else // Overlay mode

#include "hdr/stdlib_overlay.h"

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_LDIV_T_H
22 changes: 22 additions & 0 deletions libc/hdr/types/lldiv_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Definition of macros from lldiv_t.h -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_TYPES_LLDIV_T_H
#define LLVM_LIBC_HDR_TYPES_LLDIV_T_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/lldiv_t.h"

#else // Overlay mode

#include "hdr/stdlib_overlay.h"

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_LLDIV_T_H
23 changes: 23 additions & 0 deletions libc/hdr/types/size_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Proxy for size_t --------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_HDR_TYPES_SIZE_T_H
#define LLVM_LIBC_HDR_TYPES_SIZE_T_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/size_t.h"

#else

#define __need_size_t
#include <stddef.h>
#undef __need_size_t

#endif // LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_SIZE_T_H
2 changes: 1 addition & 1 deletion libc/src/__support/CPP/new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//

#include "new.h"
#include <stdlib.h>
#include <stdlib.h> // For free, etc

void operator delete(void *mem) noexcept { ::free(mem); }

Expand Down
1 change: 0 additions & 1 deletion libc/src/__support/File/dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "src/__support/threads/mutex.h"

#include <dirent.h>
#include <stdlib.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
13 changes: 6 additions & 7 deletions libc/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ add_entrypoint_object(
HDRS
div.h
DEPENDS
libc.include.stdlib
libc.hdr.types.div_t
libc.src.__support.integer_operations
)

Expand All @@ -232,7 +232,7 @@ add_entrypoint_object(
HDRS
ldiv.h
DEPENDS
libc.include.stdlib
libc.hdr.types.ldiv_t
libc.src.__support.integer_operations
)

Expand All @@ -243,7 +243,7 @@ add_entrypoint_object(
HDRS
lldiv.h
DEPENDS
libc.include.stdlib
libc.hdr.types.lldiv_t
libc.src.__support.integer_operations
)

Expand Down Expand Up @@ -277,7 +277,7 @@ add_entrypoint_object(
qsort.h
DEPENDS
.qsort_util
libc.include.stdlib
libc.hdr.types.size_t
)

add_entrypoint_object(
Expand All @@ -288,7 +288,7 @@ add_entrypoint_object(
qsort_r.h
DEPENDS
.qsort_util
libc.include.stdlib
libc.hdr.types.size_t
)

add_object_library(
Expand All @@ -309,7 +309,7 @@ add_entrypoint_object(
rand.h
DEPENDS
.rand_util
libc.include.stdlib
libc.hdr.stdlib_macros
libc.src.__support.threads.sleep
)

Expand All @@ -321,7 +321,6 @@ add_entrypoint_object(
srand.h
DEPENDS
.rand_util
libc.include.stdlib
)

if(NOT LIBC_TARGET_OS_IS_GPU)
Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdlib/div.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_STDLIB_DIV_H
#define LLVM_LIBC_SRC_STDLIB_DIV_H

#include "hdr/types/div_t.h"
#include "src/__support/macros/config.h"
#include <stdlib.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
1 change: 0 additions & 1 deletion libc/src/stdlib/exit.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define LLVM_LIBC_SRC_STDLIB_EXIT_H

#include "src/__support/macros/config.h"
#include <stdlib.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
1 change: 0 additions & 1 deletion libc/src/stdlib/free.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//

#include "src/__support/macros/config.h"
#include <stdlib.h>

#ifndef LLVM_LIBC_SRC_STDLIB_FREE_H
#define LLVM_LIBC_SRC_STDLIB_FREE_H
Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdlib/ldiv.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_STDLIB_LDIV_H
#define LLVM_LIBC_SRC_STDLIB_LDIV_H

#include "hdr/types/ldiv_t.h"
#include "src/__support/macros/config.h"
#include <stdlib.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdlib/lldiv.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#ifndef LLVM_LIBC_SRC_STDLIB_LLDIV_H
#define LLVM_LIBC_SRC_STDLIB_LLDIV_H

#include "hdr/types/lldiv_t.h"
#include "src/__support/macros/config.h"
#include <stdlib.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdlib/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//

#include "hdr/types/size_t.h"
#include "src/__support/macros/config.h"
#include <stdlib.h>

#ifndef LLVM_LIBC_SRC_STDLIB_MALLOC_H
#define LLVM_LIBC_SRC_STDLIB_MALLOC_H
Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdlib/qsort.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_STDLIB_QSORT_H
#define LLVM_LIBC_SRC_STDLIB_QSORT_H

#include "hdr/types/size_t.h"
#include "src/__support/macros/config.h"
#include <stdlib.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdlib/qsort_r.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_STDLIB_QSORT_R_H
#define LLVM_LIBC_SRC_STDLIB_QSORT_R_H

#include "hdr/types/size_t.h"
#include "src/__support/macros/config.h"
#include <stdlib.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
2 changes: 1 addition & 1 deletion libc/src/stdlib/rand.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_STDLIB_RAND_H
#define LLVM_LIBC_SRC_STDLIB_RAND_H

#include "hdr/stdlib_macros.h"
#include "src/__support/macros/config.h"
#include <stdlib.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
1 change: 0 additions & 1 deletion libc/src/stdlib/srand.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define LLVM_LIBC_SRC_STDLIB_SRAND_H

#include "src/__support/macros/config.h"
#include <stdlib.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
Loading
Loading