[libc-commits] [libc] 110ee16 - [libc][NFC] Refactor internal errno.
Siva Chandra Reddy via libc-commits
libc-commits at lists.llvm.org
Tue Feb 28 12:40:51 PST 2023
Author: Siva Chandra Reddy
Date: 2023-02-28T20:40:42Z
New Revision: 110ee16467734c0e782c93cfb44c68321b012908
URL: https://github.com/llvm/llvm-project/commit/110ee16467734c0e782c93cfb44c68321b012908
DIFF: https://github.com/llvm/llvm-project/commit/110ee16467734c0e782c93cfb44c68321b012908.diff
LOG: [libc][NFC] Refactor internal errno.
This is in preparation for the transition to a solution to make libc tests
hermetic with respect to their use of errno. The implementation of strdup
has been switched over to libc_errno as an example of what the code looks
like in the new way.
See #61037 for more information.
Reviewed By: lntue
Differential Revision: https://reviews.llvm.org/D144928
Added:
libc/src/errno/libc_errno.cpp
libc/src/errno/libc_errno.h
Modified:
libc/src/errno/CMakeLists.txt
libc/src/errno/llvmlibc_errno.h
libc/src/string/CMakeLists.txt
libc/src/string/strdup.cpp
Removed:
libc/src/errno/dummy_errno.cpp
libc/src/errno/dummy_errno.h
libc/src/errno/errno.cpp
################################################################################
diff --git a/libc/src/errno/CMakeLists.txt b/libc/src/errno/CMakeLists.txt
index e9ddb279e9ec3..dd1cdc04cd6b0 100644
--- a/libc/src/errno/CMakeLists.txt
+++ b/libc/src/errno/CMakeLists.txt
@@ -1,17 +1,10 @@
-if (LLVM_LIBC_FULL_BUILD)
add_object_library(
errno
SRCS
- errno.cpp
+ libc_errno.cpp
HDRS
- llvmlibc_errno.h
+ libc_errno.h # Include this
+ llvmlibc_errno.h # DEPRECATED: Will be removed soon
+ DEPENDS
+ libc.include.errno
)
-else()
-add_object_library(
- errno
- SRCS
- dummy_errno.cpp
- HDRS
- dummy_errno.h
-)
-endif()
diff --git a/libc/src/errno/dummy_errno.cpp b/libc/src/errno/dummy_errno.cpp
deleted file mode 100644
index 6abf0ea5dbd51..0000000000000
--- a/libc/src/errno/dummy_errno.cpp
+++ /dev/null
@@ -1,7 +0,0 @@
-//===-- An empty file to be used for mixed mode builds --------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
diff --git a/libc/src/errno/dummy_errno.h b/libc/src/errno/dummy_errno.h
deleted file mode 100644
index 2d007b201b440..0000000000000
--- a/libc/src/errno/dummy_errno.h
+++ /dev/null
@@ -1,7 +0,0 @@
-//===-- An empty file to be used for mixed mode builds ----------*- C++ -*-===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
diff --git a/libc/src/errno/errno.cpp b/libc/src/errno/errno.cpp
deleted file mode 100644
index 84e111bb20930..0000000000000
--- a/libc/src/errno/errno.cpp
+++ /dev/null
@@ -1,9 +0,0 @@
-//===-- Implementation of __errno_location --------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-thread_local int __llvmlibc_errno = 0;
diff --git a/libc/src/errno/libc_errno.cpp b/libc/src/errno/libc_errno.cpp
new file mode 100644
index 0000000000000..98a76c2cbcf49
--- /dev/null
+++ b/libc/src/errno/libc_errno.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of errno -------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+namespace __llvm_libc {
+
+extern "C" {
+// TODO: Declare __llvmlibc_errno only under LIBC_COPT_PUBLIC_PACKAGING and
+// __llvmlibc_internal_errno otherwise.
+//
+// In overlay mode, this will be an unused thread local variable as libc_errno
+// will resolve to errno from the system libc's errno.h. In full build mode
+// however, libc_errno will resolve to this thread local variable via the errno
+// macro defined in LLVM libc's public errno.h header file.
+// TODO: Use a macro to distinguish full build and overlay build which can be
+// used to exclude __llvmlibc_errno under overlay build.
+thread_local int __llvmlibc_errno;
+thread_local int __llvmlibc_internal_errno;
+} // extern "C"
+
+} // namespace __llvm_libc
diff --git a/libc/src/errno/libc_errno.h b/libc/src/errno/libc_errno.h
new file mode 100644
index 0000000000000..586d88feba906
--- /dev/null
+++ b/libc/src/errno/libc_errno.h
@@ -0,0 +1,37 @@
+//===-- Implementation header for errno -------------------------*- C++ -*-===//
+//
+// 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_SRC_ERRNO_LLVMLIBC_ERRNO_H
+#define LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
+
+#include <errno.h>
+
+// All of the libc runtime and test code should use the "libc_errno" macro. They
+// should not refer to the "errno" macro directly.
+#ifdef LIBC_COPT_PUBLIC_PACKAGING
+// This macro will resolve to errno from the errno.h file included above. Under
+// full build, this will be LLVM libc's errno. In overlay build, it will be
+// system libc's errno.
+#define libc_errno errno
+#else
+namespace __llvm_libc {
+
+extern "C" {
+extern thread_local int __llvmlibc_internal_errno;
+} // extern "C"
+
+// TODO: After all of libc/src and libc/test are switched over to use
+// libc_errno, this header file will be "shipped" via an add_entrypoint_object
+// target. At which point libc_errno, should point to __llvmlibc_internal_errno
+// if LIBC_COPT_PUBLIC_PACKAGING is not defined.
+#define libc_errno errno
+
+} // namespace __llvm_libc
+#endif
+
+#endif // LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
diff --git a/libc/src/errno/llvmlibc_errno.h b/libc/src/errno/llvmlibc_errno.h
index 5e505add9b4eb..eb1f66c2e4080 100644
--- a/libc/src/errno/llvmlibc_errno.h
+++ b/libc/src/errno/llvmlibc_errno.h
@@ -9,9 +9,13 @@
#ifndef LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
#define LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
-// Internal code should use this and not use the errno macro from the
-// public header.
-extern thread_local int __llvmlibc_errno;
-#define llvmlibc_errno __llvmlibc_errno
+#include <errno.h>
+
+// DEPRECATED: Use libc_errno from libc_errno.h instead. This macro is only
+// present to facilitate gradual transition (as in, in multiple simple patches)
+// to libc_errno.
+// TODO: After all of libc/src and libc/test is switched over to use libc_errno,
+// remove this macro and header file.
+#define llvmlibc_errno errno
#endif // LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H
diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 944889a86bde6..13b9e0179e51f 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -176,7 +176,6 @@ add_entrypoint_object(
DEPENDS
.memory_utils.memcpy_implementation
.string_utils
- libc.include.errno
libc.include.stdlib
libc.src.errno.errno
)
diff --git a/libc/src/string/strdup.cpp b/libc/src/string/strdup.cpp
index 9aa0d5030c9bb..444b2918b72ad 100644
--- a/libc/src/string/strdup.cpp
+++ b/libc/src/string/strdup.cpp
@@ -7,12 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/string/strdup.h"
+#include "src/errno/libc_errno.h"
#include "src/string/allocating_string_utils.h"
#include "src/string/memory_utils/memcpy_implementations.h"
#include "src/__support/common.h"
-#include <errno.h>
#include <stdlib.h>
namespace __llvm_libc {
@@ -22,7 +22,7 @@ LLVM_LIBC_FUNCTION(char *, strdup, (const char *src)) {
if (dup)
return *dup;
if (src != nullptr)
- errno = ENOMEM;
+ libc_errno = ENOMEM;
return nullptr;
}
More information about the libc-commits
mailing list