[libc-commits] [libc] [libc] Remove the #include <stdlib.h> header (PR #114453)

Job Henandez Lara via libc-commits libc-commits at lists.llvm.org
Thu Oct 31 17:49:58 PDT 2024


https://github.com/Jobhdez updated https://github.com/llvm/llvm-project/pull/114453

>From 64fff3b1bf3283b2cf8082875b074e69353a567e Mon Sep 17 00:00:00 2001
From: Job Hernandez <jobhdezlara93 at gmail.com>
Date: Thu, 31 Oct 2024 12:04:19 -0700
Subject: [PATCH 1/3] [libc] remove the stdlib header

---
 libc/hdr/CMakeLists.txt                       | 11 +++++
 libc/hdr/stdlib_macros.h                      | 22 ++++++++++
 libc/hdr/stdlib_overlay.h                     | 36 ++++++++++++++++
 libc/hdr/types/CMakeLists.txt                 | 41 +++++++++++++++++++
 libc/hdr/types/div_t.h                        | 22 ++++++++++
 libc/hdr/types/ldiv_t.h                       | 22 ++++++++++
 libc/hdr/types/lldiv_t.h                      | 22 ++++++++++
 libc/hdr/types/size_t.h                       | 23 +++++++++++
 libc/src/__support/CPP/new.cpp                |  1 -
 libc/src/__support/CPP/new.h                  |  2 +-
 libc/src/__support/File/dir.h                 |  1 -
 libc/src/stdlib/CMakeLists.txt                | 13 +++---
 libc/src/stdlib/div.h                         |  2 +-
 libc/src/stdlib/exit.h                        |  1 -
 libc/src/stdlib/free.h                        |  1 -
 libc/src/stdlib/ldiv.h                        |  2 +-
 libc/src/stdlib/lldiv.h                       |  2 +-
 libc/src/stdlib/malloc.h                      |  2 +-
 libc/src/stdlib/qsort.h                       |  2 +-
 libc/src/stdlib/qsort_r.h                     |  2 +-
 libc/src/stdlib/rand.h                        |  2 +-
 libc/src/stdlib/srand.h                       |  1 -
 libc/src/string/CMakeLists.txt                |  2 +-
 libc/src/string/strdup.cpp                    |  3 +-
 libc/src/unistd/linux/CMakeLists.txt          |  1 -
 libc/src/unistd/linux/getcwd.cpp              |  1 -
 libc/test/src/__support/File/CMakeLists.txt   |  2 +-
 libc/test/src/__support/File/file_test.cpp    |  2 +-
 .../str_to_float_comparison_test.cpp          |  2 -
 libc/test/src/stdio/CMakeLists.txt            |  2 +-
 libc/test/src/stdio/fopencookie_test.cpp      |  2 +-
 libc/test/src/stdlib/CMakeLists.txt           | 13 +++---
 libc/test/src/stdlib/_Exit_test.cpp           |  2 -
 libc/test/src/stdlib/abort_test.cpp           |  1 -
 libc/test/src/stdlib/bsearch_test.cpp         |  2 +-
 libc/test/src/stdlib/div_test.cpp             |  2 +-
 libc/test/src/stdlib/ldiv_test.cpp            |  3 +-
 libc/test/src/stdlib/lldiv_test.cpp           |  3 +-
 libc/test/src/stdlib/qsort_r_test.cpp         |  2 +-
 libc/test/src/stdlib/rand_test.cpp            |  1 -
 libc/test/src/string/CMakeLists.txt           |  2 -
 libc/test/src/string/strdup_test.cpp          |  1 -
 libc/test/src/string/strlcat_test.cpp         |  2 +-
 libc/test/src/string/strlcpy_test.cpp         |  1 -
 libc/test/src/string/strndup_test.cpp         |  1 -
 45 files changed, 231 insertions(+), 55 deletions(-)
 create mode 100644 libc/hdr/stdlib_macros.h
 create mode 100644 libc/hdr/stdlib_overlay.h
 create mode 100644 libc/hdr/types/div_t.h
 create mode 100644 libc/hdr/types/ldiv_t.h
 create mode 100644 libc/hdr/types/lldiv_t.h
 create mode 100644 libc/hdr/types/size_t.h

diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index 80545ee4b359f4..c63eadab6f5d77 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -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(
diff --git a/libc/hdr/stdlib_macros.h b/libc/hdr/stdlib_macros.h
new file mode 100644
index 00000000000000..3faeb3a8fe36e1
--- /dev/null
+++ b/libc/hdr/stdlib_macros.h
@@ -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
diff --git a/libc/hdr/stdlib_overlay.h b/libc/hdr/stdlib_overlay.h
new file mode 100644
index 00000000000000..f095cafe5e0bce
--- /dev/null
+++ b/libc/hdr/stdlib_overlay.h
@@ -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
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index e45979857d7955..928bde71278166 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -1,3 +1,36 @@
+add_proxy_header_library(
+  div_t
+  HDRS
+    div_t.h
+  DEPENDS
+    ../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
+    ../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
+    ../stdlib_overlay
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.lldiv_t
+    libc.include.stdlib
+)
+
 add_proxy_header_library(
   sigset_t
   HDRS
@@ -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
diff --git a/libc/hdr/types/div_t.h b/libc/hdr/types/div_t.h
new file mode 100644
index 00000000000000..29c355f079e104
--- /dev/null
+++ b/libc/hdr/types/div_t.h
@@ -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
diff --git a/libc/hdr/types/ldiv_t.h b/libc/hdr/types/ldiv_t.h
new file mode 100644
index 00000000000000..c7023f5077bd0c
--- /dev/null
+++ b/libc/hdr/types/ldiv_t.h
@@ -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
diff --git a/libc/hdr/types/lldiv_t.h b/libc/hdr/types/lldiv_t.h
new file mode 100644
index 00000000000000..cd41886674a16d
--- /dev/null
+++ b/libc/hdr/types/lldiv_t.h
@@ -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
diff --git a/libc/hdr/types/size_t.h b/libc/hdr/types/size_t.h
new file mode 100644
index 00000000000000..1d9f26db3bbb3a
--- /dev/null
+++ b/libc/hdr/types/size_t.h
@@ -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
diff --git a/libc/src/__support/CPP/new.cpp b/libc/src/__support/CPP/new.cpp
index 88db8377b2fac4..5c67f856058d81 100644
--- a/libc/src/__support/CPP/new.cpp
+++ b/libc/src/__support/CPP/new.cpp
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "new.h"
-#include <stdlib.h>
 
 void operator delete(void *mem) noexcept { ::free(mem); }
 
diff --git a/libc/src/__support/CPP/new.h b/libc/src/__support/CPP/new.h
index c1b6b95033f84c..5476781c377988 100644
--- a/libc/src/__support/CPP/new.h
+++ b/libc/src/__support/CPP/new.h
@@ -14,7 +14,7 @@
 #include "src/__support/macros/properties/os.h"
 
 #include <stddef.h> // For size_t
-#include <stdlib.h> // For malloc, free etc.
+#include <stdlib.h>
 
 // Defining members in the std namespace is not preferred. But, we do it here
 // so that we can use it to define the operator new which takes std::align_val_t
diff --git a/libc/src/__support/File/dir.h b/libc/src/__support/File/dir.h
index 136c266524a011..247ac1225ceea3 100644
--- a/libc/src/__support/File/dir.h
+++ b/libc/src/__support/File/dir.h
@@ -15,7 +15,6 @@
 #include "src/__support/threads/mutex.h"
 
 #include <dirent.h>
-#include <stdlib.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index d997cd41e630fa..14d06534a6049a 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -221,7 +221,7 @@ add_entrypoint_object(
   HDRS
     div.h
   DEPENDS
-    libc.include.stdlib
+    libc.hdr.types.div_t
     libc.src.__support.integer_operations
 )
 
@@ -232,7 +232,7 @@ add_entrypoint_object(
   HDRS
     ldiv.h
   DEPENDS
-    libc.include.stdlib
+    libc.hdr.types.ldiv_t
     libc.src.__support.integer_operations
 )
 
@@ -243,7 +243,7 @@ add_entrypoint_object(
   HDRS
     lldiv.h
   DEPENDS
-    libc.include.stdlib
+    libc.hdr.types.lldiv_t
     libc.src.__support.integer_operations
 )
 
@@ -277,7 +277,7 @@ add_entrypoint_object(
     qsort.h
   DEPENDS
     .qsort_util
-    libc.include.stdlib
+    libc.hdr.types.size_t
 )
 
 add_entrypoint_object(
@@ -288,7 +288,7 @@ add_entrypoint_object(
     qsort_r.h
   DEPENDS
     .qsort_util
-    libc.include.stdlib
+    libc.hdr.types.size_t
 )
 
 add_object_library(
@@ -309,7 +309,7 @@ add_entrypoint_object(
     rand.h
   DEPENDS
     .rand_util
-    libc.include.stdlib
+    libc.hdr.stdlib_macros
     libc.src.__support.threads.sleep
 )
 
@@ -321,7 +321,6 @@ add_entrypoint_object(
     srand.h
   DEPENDS
     .rand_util
-    libc.include.stdlib
 )
 
 if(NOT LIBC_TARGET_OS_IS_GPU)
diff --git a/libc/src/stdlib/div.h b/libc/src/stdlib/div.h
index e76f79bbeee5fa..4917ac1973c5b6 100644
--- a/libc/src/stdlib/div.h
+++ b/libc/src/stdlib/div.h
@@ -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 {
 
diff --git a/libc/src/stdlib/exit.h b/libc/src/stdlib/exit.h
index 1f0153b98c1f44..3c9d37a342eb98 100644
--- a/libc/src/stdlib/exit.h
+++ b/libc/src/stdlib/exit.h
@@ -10,7 +10,6 @@
 #define LLVM_LIBC_SRC_STDLIB_EXIT_H
 
 #include "src/__support/macros/config.h"
-#include <stdlib.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/stdlib/free.h b/libc/src/stdlib/free.h
index 1b250f3d3a7aa5..77d00a5810e715 100644
--- a/libc/src/stdlib/free.h
+++ b/libc/src/stdlib/free.h
@@ -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
diff --git a/libc/src/stdlib/ldiv.h b/libc/src/stdlib/ldiv.h
index 56b71fd044b189..b6b799775c2009 100644
--- a/libc/src/stdlib/ldiv.h
+++ b/libc/src/stdlib/ldiv.h
@@ -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 {
 
diff --git a/libc/src/stdlib/lldiv.h b/libc/src/stdlib/lldiv.h
index ad688e00289ef8..9c4899172c672d 100644
--- a/libc/src/stdlib/lldiv.h
+++ b/libc/src/stdlib/lldiv.h
@@ -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 {
 
diff --git a/libc/src/stdlib/malloc.h b/libc/src/stdlib/malloc.h
index 1974f5d3a78154..074df254bf9353 100644
--- a/libc/src/stdlib/malloc.h
+++ b/libc/src/stdlib/malloc.h
@@ -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
diff --git a/libc/src/stdlib/qsort.h b/libc/src/stdlib/qsort.h
index 38988312f90147..2060584c0cdff6 100644
--- a/libc/src/stdlib/qsort.h
+++ b/libc/src/stdlib/qsort.h
@@ -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 {
 
diff --git a/libc/src/stdlib/qsort_r.h b/libc/src/stdlib/qsort_r.h
index 574968a528711f..51a459c3f76338 100644
--- a/libc/src/stdlib/qsort_r.h
+++ b/libc/src/stdlib/qsort_r.h
@@ -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 {
 
diff --git a/libc/src/stdlib/rand.h b/libc/src/stdlib/rand.h
index df217b569aa8a4..65693197164bf5 100644
--- a/libc/src/stdlib/rand.h
+++ b/libc/src/stdlib/rand.h
@@ -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 {
 
diff --git a/libc/src/stdlib/srand.h b/libc/src/stdlib/srand.h
index c9fce46ddd2bc8..3c40c744aebef9 100644
--- a/libc/src/stdlib/srand.h
+++ b/libc/src/stdlib/srand.h
@@ -10,7 +10,6 @@
 #define LLVM_LIBC_SRC_STDLIB_SRAND_H
 
 #include "src/__support/macros/config.h"
-#include <stdlib.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index b33cbc5358d60d..d6e300754d4f9c 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -239,7 +239,7 @@ add_entrypoint_object(
   DEPENDS
     .memory_utils.inline_memcpy
     .string_utils
-    libc.include.stdlib
+    libc.hdr.stdlib_macros
     libc.src.errno.errno
     libc.include.llvm-libc-types.size_t
 )
diff --git a/libc/src/string/strdup.cpp b/libc/src/string/strdup.cpp
index 4e09c87099425b..62ad6cb91bfe0c 100644
--- a/libc/src/string/strdup.cpp
+++ b/libc/src/string/strdup.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/stdlib_macros.h"
 #include "src/string/strdup.h"
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
@@ -14,8 +15,6 @@
 
 #include "src/__support/common.h"
 
-#include <stdlib.h>
-
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(char *, strdup, (const char *src)) {
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 472438ca72e49e..05b6e02ad0c882 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -179,7 +179,6 @@ add_entrypoint_object(
   HDRS
     ../getcwd.h
   DEPENDS
-    libc.include.stdlib
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
diff --git a/libc/src/unistd/linux/getcwd.cpp b/libc/src/unistd/linux/getcwd.cpp
index a10c5004d0e061..1bb11a7c8e7ba2 100644
--- a/libc/src/unistd/linux/getcwd.cpp
+++ b/libc/src/unistd/linux/getcwd.cpp
@@ -15,7 +15,6 @@
 
 #include "src/errno/libc_errno.h"
 #include <linux/limits.h> // This is safe to include without any name pollution.
-#include <stdlib.h>
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/test/src/__support/File/CMakeLists.txt b/libc/test/src/__support/File/CMakeLists.txt
index 9fa3b518421ccb..04205166bf5337 100644
--- a/libc/test/src/__support/File/CMakeLists.txt
+++ b/libc/test/src/__support/File/CMakeLists.txt
@@ -15,7 +15,7 @@ add_libc_test(
     LibcMemoryHelpers
   DEPENDS
     libc.include.stdio
-    libc.include.stdlib
+    libc.hdr.types.size_t
     libc.src.errno.errno 
     libc.src.__support.CPP.new
     libc.src.__support.File.file
diff --git a/libc/test/src/__support/File/file_test.cpp b/libc/test/src/__support/File/file_test.cpp
index 5977ea7c8e0b57..b3c9f2ba49bced 100644
--- a/libc/test/src/__support/File/file_test.cpp
+++ b/libc/test/src/__support/File/file_test.cpp
@@ -12,7 +12,7 @@
 #include "test/UnitTest/MemoryMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <stdlib.h>
+#include "hdr/types/size_t.h"
 
 using ModeFlags = LIBC_NAMESPACE::File::ModeFlags;
 using MemoryView = LIBC_NAMESPACE::testing::MemoryView;
diff --git a/libc/test/src/__support/str_to_float_comparison_test.cpp b/libc/test/src/__support/str_to_float_comparison_test.cpp
index 7641c594c3d3b8..7937167322f0cb 100644
--- a/libc/test/src/__support/str_to_float_comparison_test.cpp
+++ b/libc/test/src/__support/str_to_float_comparison_test.cpp
@@ -8,8 +8,6 @@
 
 // #include "src/__support/str_float_conv_utils.h"
 
-#include <stdlib.h>
-
 // #include "src/__support/FPUtil/FPBits.h"
 
 #include <cstdint>
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index ec94f5aaaf9b62..e17f8d8c101a96 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -98,7 +98,7 @@ add_libc_test(
     fopencookie_test.cpp
   DEPENDS
     libc.include.stdio
-    libc.include.stdlib
+    libc.hdr.types.size_t
     libc.src.errno.errno
     libc.src.stdio.clearerr
     libc.src.stdio.fclose
diff --git a/libc/test/src/stdio/fopencookie_test.cpp b/libc/test/src/stdio/fopencookie_test.cpp
index 016722aa11ab82..2c65e094a2fff2 100644
--- a/libc/test/src/stdio/fopencookie_test.cpp
+++ b/libc/test/src/stdio/fopencookie_test.cpp
@@ -18,9 +18,9 @@
 #include "test/UnitTest/MemoryMatcher.h"
 #include "test/UnitTest/Test.h"
 
+#include "hdr/types/size_t.h"
 #include "hdr/stdio_macros.h"
 #include "src/errno/libc_errno.h"
-#include <stdlib.h>
 
 using MemoryView = LIBC_NAMESPACE::testing::MemoryView;
 
diff --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt
index c68627a6687419..2683eefd032b76 100644
--- a/libc/test/src/stdlib/CMakeLists.txt
+++ b/libc/test/src/stdlib/CMakeLists.txt
@@ -259,7 +259,7 @@ add_libc_test(
   HDRS
     DivTest.h
   DEPENDS
-    libc.include.stdlib
+    libc.hdr.types.div_t
     libc.src.stdlib.div
 )
 
@@ -272,7 +272,7 @@ add_libc_test(
   HDRS
     DivTest.h
   DEPENDS
-    libc.include.stdlib
+    libc.hdr.types.ldiv_t
     libc.src.stdlib.ldiv
 )
 
@@ -285,7 +285,7 @@ add_libc_test(
   HDRS
     DivTest.h
   DEPENDS
-    libc.include.stdlib
+    libc.hdr.types.lldiv_t
     libc.src.stdlib.lldiv
 )
 
@@ -296,7 +296,7 @@ add_libc_test(
   SRCS
     bsearch_test.cpp
   DEPENDS
-    libc.include.stdlib
+    libc.hdr.types.size_t
     libc.src.stdlib.bsearch
 )
 
@@ -343,7 +343,7 @@ add_libc_test(
   SRCS
     qsort_r_test.cpp
   DEPENDS
-    libc.include.stdlib
+    libc.hdr.types.size_t
     libc.src.stdlib.qsort_r
 )
 
@@ -354,7 +354,6 @@ add_libc_test(
   SRCS
     rand_test.cpp
   DEPENDS
-    libc.include.stdlib
     libc.src.stdlib.rand
     libc.src.stdlib.srand
 )
@@ -370,7 +369,6 @@ if(LLVM_LIBC_FULL_BUILD)
     SRCS
       _Exit_test.cpp
     DEPENDS
-      libc.include.stdlib
       libc.src.stdlib._Exit
       libc.src.stdlib.exit
   )
@@ -414,7 +412,6 @@ if(LLVM_LIBC_FULL_BUILD)
     SRCS
       abort_test.cpp
     DEPENDS
-      libc.include.stdlib
       libc.include.signal
       libc.src.stdlib.abort
       libc.src.stdlib._Exit
diff --git a/libc/test/src/stdlib/_Exit_test.cpp b/libc/test/src/stdlib/_Exit_test.cpp
index 9ca0fc51aab812..333277dc01dca0 100644
--- a/libc/test/src/stdlib/_Exit_test.cpp
+++ b/libc/test/src/stdlib/_Exit_test.cpp
@@ -10,8 +10,6 @@
 #include "src/stdlib/exit.h"
 #include "test/UnitTest/Test.h"
 
-#include <stdlib.h>
-
 TEST(LlvmLibcStdlib, _Exit) {
   EXPECT_EXITS([] { LIBC_NAMESPACE::_Exit(1); }, 1);
   EXPECT_EXITS([] { LIBC_NAMESPACE::_Exit(65); }, 65);
diff --git a/libc/test/src/stdlib/abort_test.cpp b/libc/test/src/stdlib/abort_test.cpp
index 766c8d5fbb21a1..8c5fd0c97abbc0 100644
--- a/libc/test/src/stdlib/abort_test.cpp
+++ b/libc/test/src/stdlib/abort_test.cpp
@@ -10,7 +10,6 @@
 #include "test/UnitTest/Test.h"
 
 #include <signal.h>
-#include <stdlib.h>
 
 TEST(LlvmLibcStdlib, abort) {
   // -1 matches against any signal, which is necessary for now until
diff --git a/libc/test/src/stdlib/bsearch_test.cpp b/libc/test/src/stdlib/bsearch_test.cpp
index 689145806ba8d3..4240adc953a860 100644
--- a/libc/test/src/stdlib/bsearch_test.cpp
+++ b/libc/test/src/stdlib/bsearch_test.cpp
@@ -10,7 +10,7 @@
 
 #include "test/UnitTest/Test.h"
 
-#include <stdlib.h>
+#include "hdr/types/size_t.h"
 
 static int int_compare(const void *l, const void *r) {
   int li = *reinterpret_cast<const int *>(l);
diff --git a/libc/test/src/stdlib/div_test.cpp b/libc/test/src/stdlib/div_test.cpp
index d06b8348b6464e..513d7866f3cd3d 100644
--- a/libc/test/src/stdlib/div_test.cpp
+++ b/libc/test/src/stdlib/div_test.cpp
@@ -8,8 +8,8 @@
 
 #include "DivTest.h"
 
+#include "hdr/types/div_t.h"
 #include "src/stdlib/div.h"
 
-#include <stdlib.h>
 
 LIST_DIV_TESTS(int, div_t, LIBC_NAMESPACE::div)
diff --git a/libc/test/src/stdlib/ldiv_test.cpp b/libc/test/src/stdlib/ldiv_test.cpp
index 6b84163d654748..258cbfefbe87cb 100644
--- a/libc/test/src/stdlib/ldiv_test.cpp
+++ b/libc/test/src/stdlib/ldiv_test.cpp
@@ -8,8 +8,7 @@
 
 #include "DivTest.h"
 
+#include "hdr/types/ldiv_t.h"
 #include "src/stdlib/ldiv.h"
 
-#include <stdlib.h>
-
 LIST_DIV_TESTS(long, ldiv_t, LIBC_NAMESPACE::ldiv)
diff --git a/libc/test/src/stdlib/lldiv_test.cpp b/libc/test/src/stdlib/lldiv_test.cpp
index d803894fa862cc..16c2d95b613271 100644
--- a/libc/test/src/stdlib/lldiv_test.cpp
+++ b/libc/test/src/stdlib/lldiv_test.cpp
@@ -8,8 +8,9 @@
 
 #include "DivTest.h"
 
+#include "hdr/types/lldiv_t.h"
 #include "src/stdlib/lldiv.h"
 
-#include <stdlib.h>
+
 
 LIST_DIV_TESTS(long long, lldiv_t, LIBC_NAMESPACE::lldiv)
diff --git a/libc/test/src/stdlib/qsort_r_test.cpp b/libc/test/src/stdlib/qsort_r_test.cpp
index 2c810f411b03e2..6893fdc7b74c82 100644
--- a/libc/test/src/stdlib/qsort_r_test.cpp
+++ b/libc/test/src/stdlib/qsort_r_test.cpp
@@ -10,7 +10,7 @@
 
 #include "test/UnitTest/Test.h"
 
-#include <stdlib.h>
+#include "hdr/types/size_t.h"
 
 static int int_compare_count(const void *l, const void *r, void *count_arg) {
   int li = *reinterpret_cast<const int *>(l);
diff --git a/libc/test/src/stdlib/rand_test.cpp b/libc/test/src/stdlib/rand_test.cpp
index 6f25708e539053..c8672d43b0226a 100644
--- a/libc/test/src/stdlib/rand_test.cpp
+++ b/libc/test/src/stdlib/rand_test.cpp
@@ -11,7 +11,6 @@
 #include "test/UnitTest/Test.h"
 
 #include <stddef.h>
-#include <stdlib.h>
 
 TEST(LlvmLibcRandTest, UnsetSeed) {
   static int vals[1000];
diff --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index 44535957e740be..b6b59a689cc8fa 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -210,7 +210,6 @@ add_libc_test(
   SRCS
     strdup_test.cpp
   DEPENDS
-    libc.include.stdlib
     libc.src.string.strdup
     libc.src.errno.errno
 )
@@ -315,7 +314,6 @@ add_libc_test(
   SRCS
     strndup_test.cpp
   DEPENDS
-    libc.include.stdlib
     libc.src.string.strndup
 )
 
diff --git a/libc/test/src/string/strdup_test.cpp b/libc/test/src/string/strdup_test.cpp
index fd3cceaaa17cfc..0027613c69ff0c 100644
--- a/libc/test/src/string/strdup_test.cpp
+++ b/libc/test/src/string/strdup_test.cpp
@@ -10,7 +10,6 @@
 #include "src/string/strdup.h"
 #include "test/UnitTest/Test.h"
 
-#include <stdlib.h>
 
 TEST(LlvmLibcStrDupTest, EmptyString) {
   const char *empty = "";
diff --git a/libc/test/src/string/strlcat_test.cpp b/libc/test/src/string/strlcat_test.cpp
index 5757fc92b39d2a..84f54018df26b0 100644
--- a/libc/test/src/string/strlcat_test.cpp
+++ b/libc/test/src/string/strlcat_test.cpp
@@ -1,3 +1,4 @@
+
 //===-- Unittests for strlcat ---------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
@@ -8,7 +9,6 @@
 
 #include "src/string/strlcat.h"
 #include "test/UnitTest/Test.h"
-#include <stdlib.h>
 
 TEST(LlvmLibcStrlcatTest, TooBig) {
   const char *str = "cd";
diff --git a/libc/test/src/string/strlcpy_test.cpp b/libc/test/src/string/strlcpy_test.cpp
index ecf0e925a265c3..0914257ecc1f34 100644
--- a/libc/test/src/string/strlcpy_test.cpp
+++ b/libc/test/src/string/strlcpy_test.cpp
@@ -8,7 +8,6 @@
 
 #include "src/string/strlcpy.h"
 #include "test/UnitTest/Test.h"
-#include <stdlib.h>
 
 TEST(LlvmLibcStrlcpyTest, TooBig) {
   const char *str = "abc";
diff --git a/libc/test/src/string/strndup_test.cpp b/libc/test/src/string/strndup_test.cpp
index 3adcd9bacffdb2..3e7129b2d1f005 100644
--- a/libc/test/src/string/strndup_test.cpp
+++ b/libc/test/src/string/strndup_test.cpp
@@ -8,7 +8,6 @@
 
 #include "src/string/strndup.h"
 #include "test/UnitTest/Test.h"
-#include <stdlib.h>
 
 TEST(LlvmLibcstrndupTest, EmptyString) {
   const char *empty = "";

>From 5ddeb4ba81fcdf5cb3813e64c1cb47903d139574 Mon Sep 17 00:00:00 2001
From: Job Hernandez <jobhdezlara93 at gmail.com>
Date: Thu, 31 Oct 2024 12:04:42 -0700
Subject: [PATCH 2/3] format code

---
 libc/src/string/strdup.cpp               | 2 +-
 libc/test/src/stdio/fopencookie_test.cpp | 2 +-
 libc/test/src/stdlib/div_test.cpp        | 1 -
 libc/test/src/stdlib/lldiv_test.cpp      | 2 --
 libc/test/src/string/strdup_test.cpp     | 1 -
 5 files changed, 2 insertions(+), 6 deletions(-)

diff --git a/libc/src/string/strdup.cpp b/libc/src/string/strdup.cpp
index 62ad6cb91bfe0c..4cf4173a27bf3f 100644
--- a/libc/src/string/strdup.cpp
+++ b/libc/src/string/strdup.cpp
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "hdr/stdlib_macros.h"
 #include "src/string/strdup.h"
+#include "hdr/stdlib_macros.h"
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 #include "src/string/allocating_string_utils.h"
diff --git a/libc/test/src/stdio/fopencookie_test.cpp b/libc/test/src/stdio/fopencookie_test.cpp
index 2c65e094a2fff2..61ce2a207fa199 100644
--- a/libc/test/src/stdio/fopencookie_test.cpp
+++ b/libc/test/src/stdio/fopencookie_test.cpp
@@ -18,8 +18,8 @@
 #include "test/UnitTest/MemoryMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include "hdr/types/size_t.h"
 #include "hdr/stdio_macros.h"
+#include "hdr/types/size_t.h"
 #include "src/errno/libc_errno.h"
 
 using MemoryView = LIBC_NAMESPACE::testing::MemoryView;
diff --git a/libc/test/src/stdlib/div_test.cpp b/libc/test/src/stdlib/div_test.cpp
index 513d7866f3cd3d..a1dedbb9934a10 100644
--- a/libc/test/src/stdlib/div_test.cpp
+++ b/libc/test/src/stdlib/div_test.cpp
@@ -11,5 +11,4 @@
 #include "hdr/types/div_t.h"
 #include "src/stdlib/div.h"
 
-
 LIST_DIV_TESTS(int, div_t, LIBC_NAMESPACE::div)
diff --git a/libc/test/src/stdlib/lldiv_test.cpp b/libc/test/src/stdlib/lldiv_test.cpp
index 16c2d95b613271..72e24aeb8fe9be 100644
--- a/libc/test/src/stdlib/lldiv_test.cpp
+++ b/libc/test/src/stdlib/lldiv_test.cpp
@@ -11,6 +11,4 @@
 #include "hdr/types/lldiv_t.h"
 #include "src/stdlib/lldiv.h"
 
-
-
 LIST_DIV_TESTS(long long, lldiv_t, LIBC_NAMESPACE::lldiv)
diff --git a/libc/test/src/string/strdup_test.cpp b/libc/test/src/string/strdup_test.cpp
index 0027613c69ff0c..20b85c37637dd1 100644
--- a/libc/test/src/string/strdup_test.cpp
+++ b/libc/test/src/string/strdup_test.cpp
@@ -10,7 +10,6 @@
 #include "src/string/strdup.h"
 #include "test/UnitTest/Test.h"
 
-
 TEST(LlvmLibcStrDupTest, EmptyString) {
   const char *empty = "";
 

>From 1d4bcf60eafb6566f35f222a02d4d47b301cc3b1 Mon Sep 17 00:00:00 2001
From: Job Hernandez <jobhdezlara93 at gmail.com>
Date: Thu, 31 Oct 2024 17:49:22 -0700
Subject: [PATCH 3/3] address review

---
 libc/src/__support/CPP/new.cpp                           | 1 +
 libc/src/__support/CPP/new.h                             | 2 +-
 libc/test/src/__support/str_to_float_comparison_test.cpp | 2 ++
 libc/test/src/string/strlcat_test.cpp                    | 1 -
 4 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/CPP/new.cpp b/libc/src/__support/CPP/new.cpp
index 5c67f856058d81..88db8377b2fac4 100644
--- a/libc/src/__support/CPP/new.cpp
+++ b/libc/src/__support/CPP/new.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "new.h"
+#include <stdlib.h>
 
 void operator delete(void *mem) noexcept { ::free(mem); }
 
diff --git a/libc/src/__support/CPP/new.h b/libc/src/__support/CPP/new.h
index 5476781c377988..c1b6b95033f84c 100644
--- a/libc/src/__support/CPP/new.h
+++ b/libc/src/__support/CPP/new.h
@@ -14,7 +14,7 @@
 #include "src/__support/macros/properties/os.h"
 
 #include <stddef.h> // For size_t
-#include <stdlib.h>
+#include <stdlib.h> // For malloc, free etc.
 
 // Defining members in the std namespace is not preferred. But, we do it here
 // so that we can use it to define the operator new which takes std::align_val_t
diff --git a/libc/test/src/__support/str_to_float_comparison_test.cpp b/libc/test/src/__support/str_to_float_comparison_test.cpp
index 7937167322f0cb..7641c594c3d3b8 100644
--- a/libc/test/src/__support/str_to_float_comparison_test.cpp
+++ b/libc/test/src/__support/str_to_float_comparison_test.cpp
@@ -8,6 +8,8 @@
 
 // #include "src/__support/str_float_conv_utils.h"
 
+#include <stdlib.h>
+
 // #include "src/__support/FPUtil/FPBits.h"
 
 #include <cstdint>
diff --git a/libc/test/src/string/strlcat_test.cpp b/libc/test/src/string/strlcat_test.cpp
index 84f54018df26b0..08297376fc7f54 100644
--- a/libc/test/src/string/strlcat_test.cpp
+++ b/libc/test/src/string/strlcat_test.cpp
@@ -1,4 +1,3 @@
-
 //===-- Unittests for strlcat ---------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.



More information about the libc-commits mailing list