Skip to content

Commit 33bdb53

Browse files
author
Job Henandez Lara
authored
[libc] Remove the #include <stdlib.h> header (#114453)
1 parent 3fb4b6f commit 33bdb53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+230
-56
lines changed

libc/hdr/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ add_proxy_header_library(
8181
libc.include.signal
8282
)
8383

84+
add_proxy_header_library(
85+
stdlib_macros
86+
HDRS
87+
stdlib_macros.h
88+
DEPENDS
89+
.stdlib_overlay
90+
FULL_BUILD_DEPENDS
91+
libc.include.stdlib
92+
libc.include.llvm-libc-macros.stdlib_macros
93+
)
94+
8495
add_header_library(stdio_overlay HDRS stdio_overlay.h)
8596

8697
add_proxy_header_library(

libc/hdr/stdlib_macros.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from stdlib.h --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_STDLIB_MACROS_H
10+
#define LLVM_LIBC_HDR_STDLIB_MACROS_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-macros/stdlib-macros.h"
15+
16+
#else // Overlay mode
17+
18+
#include "stdlib_overlay.h"
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_STDLIB_MACROS_H

libc/hdr/stdlib_overlay.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===-- Including stdlib.h in overlay mode --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_STDLIB_OVERLAY_H
10+
#define LLVM_LIBC_HDR_STDLIB_OVERLAY_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
#error "This header should only be included in overlay mode"
14+
#endif
15+
16+
// Overlay mode
17+
18+
// glibc <stdlib.h> header might provide extern inline definitions for few
19+
// functions, causing external alias errors. They are guarded by
20+
// `__USE_FORTIFY_LEVEL`, which will be temporarily disabled.
21+
22+
#ifdef __USE_FORTIFY_LEVEL
23+
#define LIBC_OLD_USE_FORTIFY_LEVEL __USE_FORTIFY_LEVEL
24+
#undef __USE_FORTIFY_LEVEL
25+
#define __USE_FORTIFY_LEVEL 0
26+
#endif
27+
28+
#include <stdlib.h>
29+
30+
#ifdef LIBC_OLD_USE_FORTIFY_LEVEL
31+
#undef __USE_FORTIFY_LEVEL
32+
#define __USE_FORTIFY_LEVEL LIBC_OLD_USE_FORTIFY_LEVEL
33+
#undef LIBC_OLD_USE_FORTIFY_LEVEL
34+
#endif
35+
36+
#endif

libc/hdr/types/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
add_proxy_header_library(
2+
div_t
3+
HDRS
4+
div_t.h
5+
DEPENDS
6+
libc.hdr.stdlib_overlay
7+
FULL_BUILD_DEPENDS
8+
libc.include.llvm-libc-types.div_t
9+
libc.include.stdlib
10+
)
11+
12+
add_proxy_header_library(
13+
ldiv_t
14+
HDRS
15+
ldiv_t.h
16+
DEPENDS
17+
libc.hdr.stdlib_overlay
18+
FULL_BUILD_DEPENDS
19+
libc.include.llvm-libc-types.ldiv_t
20+
libc.include.stdlib
21+
)
22+
23+
add_proxy_header_library(
24+
lldiv_t
25+
HDRS
26+
lldiv_t.h
27+
DEPENDS
28+
libc.hdr.stdlib_overlay
29+
FULL_BUILD_DEPENDS
30+
libc.include.llvm-libc-types.lldiv_t
31+
libc.include.stdlib
32+
)
33+
134
add_proxy_header_library(
235
sigset_t
336
HDRS
@@ -46,6 +79,14 @@ add_proxy_header_library(
4679
libc.include.llvm-libc-types.struct_timespec
4780
)
4881

82+
add_proxy_header_library(
83+
size_t
84+
HDRS
85+
size_t.h
86+
FULL_BUILD_DEPENDS
87+
libc.include.llvm-libc-types.size_t
88+
)
89+
4990
add_proxy_header_library(
5091
mode_t
5192
HDRS

libc/hdr/types/div_t.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from div_t.h ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_TYPES_DIV_T_H
10+
#define LLVM_LIBC_HDR_TYPES_DIV_T_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-types/div_t.h"
15+
16+
#else // Overlay mode
17+
18+
#include "hdr/stdlib_overlay.h"
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_TYPES_DIV_T_H

libc/hdr/types/ldiv_t.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from ldiv_t.h --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_TYPES_LDIV_T_H
10+
#define LLVM_LIBC_HDR_TYPES_LDIV_T_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-types/ldiv_t.h"
15+
16+
#else // Overlay mode
17+
18+
#include "hdr/stdlib_overlay.h"
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_TYPES_LDIV_T_H

libc/hdr/types/lldiv_t.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from lldiv_t.h -------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_TYPES_LLDIV_T_H
10+
#define LLVM_LIBC_HDR_TYPES_LLDIV_T_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-types/lldiv_t.h"
15+
16+
#else // Overlay mode
17+
18+
#include "hdr/stdlib_overlay.h"
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_TYPES_LLDIV_T_H

libc/hdr/types/size_t.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Proxy for size_t --------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
#ifndef LLVM_LIBC_HDR_TYPES_SIZE_T_H
9+
#define LLVM_LIBC_HDR_TYPES_SIZE_T_H
10+
11+
#ifdef LIBC_FULL_BUILD
12+
13+
#include "include/llvm-libc-types/size_t.h"
14+
15+
#else
16+
17+
#define __need_size_t
18+
#include <stddef.h>
19+
#undef __need_size_t
20+
21+
#endif // LIBC_FULL_BUILD
22+
23+
#endif // LLVM_LIBC_HDR_TYPES_SIZE_T_H

libc/src/__support/CPP/new.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "new.h"
10-
#include <stdlib.h>
10+
#include <stdlib.h> // For free, etc
1111

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

libc/src/__support/File/dir.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "src/__support/threads/mutex.h"
1616

1717
#include <dirent.h>
18-
#include <stdlib.h>
1918

2019
namespace LIBC_NAMESPACE_DECL {
2120

libc/src/stdlib/CMakeLists.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ add_entrypoint_object(
221221
HDRS
222222
div.h
223223
DEPENDS
224-
libc.include.stdlib
224+
libc.hdr.types.div_t
225225
libc.src.__support.integer_operations
226226
)
227227

@@ -232,7 +232,7 @@ add_entrypoint_object(
232232
HDRS
233233
ldiv.h
234234
DEPENDS
235-
libc.include.stdlib
235+
libc.hdr.types.ldiv_t
236236
libc.src.__support.integer_operations
237237
)
238238

@@ -243,7 +243,7 @@ add_entrypoint_object(
243243
HDRS
244244
lldiv.h
245245
DEPENDS
246-
libc.include.stdlib
246+
libc.hdr.types.lldiv_t
247247
libc.src.__support.integer_operations
248248
)
249249

@@ -277,7 +277,7 @@ add_entrypoint_object(
277277
qsort.h
278278
DEPENDS
279279
.qsort_util
280-
libc.include.stdlib
280+
libc.hdr.types.size_t
281281
)
282282

283283
add_entrypoint_object(
@@ -288,7 +288,7 @@ add_entrypoint_object(
288288
qsort_r.h
289289
DEPENDS
290290
.qsort_util
291-
libc.include.stdlib
291+
libc.hdr.types.size_t
292292
)
293293

294294
add_object_library(
@@ -309,7 +309,7 @@ add_entrypoint_object(
309309
rand.h
310310
DEPENDS
311311
.rand_util
312-
libc.include.stdlib
312+
libc.hdr.stdlib_macros
313313
libc.src.__support.threads.sleep
314314
)
315315

@@ -321,7 +321,6 @@ add_entrypoint_object(
321321
srand.h
322322
DEPENDS
323323
.rand_util
324-
libc.include.stdlib
325324
)
326325

327326
if(NOT LIBC_TARGET_OS_IS_GPU)

libc/src/stdlib/div.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef LLVM_LIBC_SRC_STDLIB_DIV_H
1010
#define LLVM_LIBC_SRC_STDLIB_DIV_H
1111

12+
#include "hdr/types/div_t.h"
1213
#include "src/__support/macros/config.h"
13-
#include <stdlib.h>
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

libc/src/stdlib/exit.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#define LLVM_LIBC_SRC_STDLIB_EXIT_H
1111

1212
#include "src/__support/macros/config.h"
13-
#include <stdlib.h>
1413

1514
namespace LIBC_NAMESPACE_DECL {
1615

libc/src/stdlib/free.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/__support/macros/config.h"
10-
#include <stdlib.h>
1110

1211
#ifndef LLVM_LIBC_SRC_STDLIB_FREE_H
1312
#define LLVM_LIBC_SRC_STDLIB_FREE_H

libc/src/stdlib/ldiv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef LLVM_LIBC_SRC_STDLIB_LDIV_H
1010
#define LLVM_LIBC_SRC_STDLIB_LDIV_H
1111

12+
#include "hdr/types/ldiv_t.h"
1213
#include "src/__support/macros/config.h"
13-
#include <stdlib.h>
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

libc/src/stdlib/lldiv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#ifndef LLVM_LIBC_SRC_STDLIB_LLDIV_H
99
#define LLVM_LIBC_SRC_STDLIB_LLDIV_H
1010

11+
#include "hdr/types/lldiv_t.h"
1112
#include "src/__support/macros/config.h"
12-
#include <stdlib.h>
1313

1414
namespace LIBC_NAMESPACE_DECL {
1515

libc/src/stdlib/malloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "hdr/types/size_t.h"
910
#include "src/__support/macros/config.h"
10-
#include <stdlib.h>
1111

1212
#ifndef LLVM_LIBC_SRC_STDLIB_MALLOC_H
1313
#define LLVM_LIBC_SRC_STDLIB_MALLOC_H

libc/src/stdlib/qsort.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef LLVM_LIBC_SRC_STDLIB_QSORT_H
1010
#define LLVM_LIBC_SRC_STDLIB_QSORT_H
1111

12+
#include "hdr/types/size_t.h"
1213
#include "src/__support/macros/config.h"
13-
#include <stdlib.h>
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

libc/src/stdlib/qsort_r.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef LLVM_LIBC_SRC_STDLIB_QSORT_R_H
1010
#define LLVM_LIBC_SRC_STDLIB_QSORT_R_H
1111

12+
#include "hdr/types/size_t.h"
1213
#include "src/__support/macros/config.h"
13-
#include <stdlib.h>
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

libc/src/stdlib/rand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef LLVM_LIBC_SRC_STDLIB_RAND_H
1010
#define LLVM_LIBC_SRC_STDLIB_RAND_H
1111

12+
#include "hdr/stdlib_macros.h"
1213
#include "src/__support/macros/config.h"
13-
#include <stdlib.h>
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

libc/src/stdlib/srand.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#define LLVM_LIBC_SRC_STDLIB_SRAND_H
1111

1212
#include "src/__support/macros/config.h"
13-
#include <stdlib.h>
1413

1514
namespace LIBC_NAMESPACE_DECL {
1615

0 commit comments

Comments
 (0)