[libc-commits] [libc] d49b993 - [libc][NFC] Switch dirent, fcntl, inttypes and sched to use libc_errno.
Siva Chandra Reddy via libc-commits
libc-commits at lists.llvm.org
Mon Mar 6 14:25:13 PST 2023
Author: Siva Chandra Reddy
Date: 2023-03-06T22:24:51Z
New Revision: d49b993f0d563932f72f435a95c315120e8b1aeb
URL: https://github.com/llvm/llvm-project/commit/d49b993f0d563932f72f435a95c315120e8b1aeb
DIFF: https://github.com/llvm/llvm-project/commit/d49b993f0d563932f72f435a95c315120e8b1aeb.diff
LOG: [libc][NFC] Switch dirent, fcntl, inttypes and sched to use libc_errno.
Reviewed By: michaelrj
Differential Revision: https://reviews.llvm.org/D145222
Added:
Modified:
libc/src/dirent/closedir.cpp
libc/src/dirent/opendir.cpp
libc/src/dirent/readdir.cpp
libc/src/fcntl/linux/CMakeLists.txt
libc/src/fcntl/linux/creat.cpp
libc/src/fcntl/linux/open.cpp
libc/src/fcntl/linux/openat.cpp
libc/src/inttypes/CMakeLists.txt
libc/src/inttypes/strtoimax.cpp
libc/src/inttypes/strtoumax.cpp
libc/src/sched/linux/CMakeLists.txt
libc/src/sched/linux/sched_getaffinity.cpp
libc/src/sched/linux/sched_setaffinity.cpp
libc/test/src/dirent/CMakeLists.txt
libc/test/src/dirent/dirent_test.cpp
libc/test/src/fcntl/CMakeLists.txt
libc/test/src/fcntl/creat_test.cpp
libc/test/src/fcntl/openat_test.cpp
libc/test/src/sched/CMakeLists.txt
libc/test/src/sched/affinity_test.cpp
libc/test/src/sched/cpu_count_test.cpp
Removed:
################################################################################
diff --git a/libc/src/dirent/closedir.cpp b/libc/src/dirent/closedir.cpp
index 39dfc0ccc5f0c..2863106b4ce89 100644
--- a/libc/src/dirent/closedir.cpp
+++ b/libc/src/dirent/closedir.cpp
@@ -10,9 +10,9 @@
#include "src/__support/File/dir.h"
#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
#include <dirent.h>
-#include <errno.h>
namespace __llvm_libc {
@@ -20,7 +20,7 @@ LLVM_LIBC_FUNCTION(int, closedir, (::DIR * dir)) {
auto *d = reinterpret_cast<__llvm_libc::Dir *>(dir);
int retval = d->close();
if (retval != 0) {
- errno = retval;
+ libc_errno = retval;
return -1;
}
return 0;
diff --git a/libc/src/dirent/opendir.cpp b/libc/src/dirent/opendir.cpp
index cf5ee18d3f050..06749e82c75f1 100644
--- a/libc/src/dirent/opendir.cpp
+++ b/libc/src/dirent/opendir.cpp
@@ -10,16 +10,16 @@
#include "src/__support/File/dir.h"
#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
#include <dirent.h>
-#include <errno.h>
namespace __llvm_libc {
LLVM_LIBC_FUNCTION(::DIR *, opendir, (const char *name)) {
auto dir = Dir::open(name);
if (!dir) {
- errno = dir.error();
+ libc_errno = dir.error();
return nullptr;
}
return reinterpret_cast<DIR *>(dir.value());
diff --git a/libc/src/dirent/readdir.cpp b/libc/src/dirent/readdir.cpp
index b9ce30140ee0e..10040aa15b63a 100644
--- a/libc/src/dirent/readdir.cpp
+++ b/libc/src/dirent/readdir.cpp
@@ -10,9 +10,9 @@
#include "src/__support/File/dir.h"
#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
#include <dirent.h>
-#include <errno.h>
namespace __llvm_libc {
@@ -20,7 +20,7 @@ LLVM_LIBC_FUNCTION(struct ::dirent *, readdir, (::DIR * dir)) {
auto *d = reinterpret_cast<__llvm_libc::Dir *>(dir);
auto dirent_val = d->read();
if (!dirent_val) {
- errno = dirent_val.error();
+ libc_errno = dirent_val.error();
return nullptr;
}
return dirent_val;
diff --git a/libc/src/fcntl/linux/CMakeLists.txt b/libc/src/fcntl/linux/CMakeLists.txt
index 926292ca93cd2..87b8d4695c4fc 100644
--- a/libc/src/fcntl/linux/CMakeLists.txt
+++ b/libc/src/fcntl/linux/CMakeLists.txt
@@ -5,7 +5,6 @@ add_entrypoint_object(
HDRS
../creat.h
DEPENDS
- libc.include.errno
libc.include.fcntl
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
@@ -18,7 +17,6 @@ add_entrypoint_object(
HDRS
../open.h
DEPENDS
- libc.include.errno
libc.include.fcntl
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
@@ -31,7 +29,6 @@ add_entrypoint_object(
HDRS
../openat.h
DEPENDS
- libc.include.errno
libc.include.fcntl
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
diff --git a/libc/src/fcntl/linux/creat.cpp b/libc/src/fcntl/linux/creat.cpp
index 4cf7b792b4d58..e2b0df7ef5f8b 100644
--- a/libc/src/fcntl/linux/creat.cpp
+++ b/libc/src/fcntl/linux/creat.cpp
@@ -10,8 +10,8 @@
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
-#include <errno.h>
#include <fcntl.h>
#include <sys/syscall.h> // For syscall numbers.
@@ -29,7 +29,7 @@ LLVM_LIBC_FUNCTION(int, creat, (const char *path, int mode_flags)) {
if (fd > 0)
return fd;
- errno = -fd;
+ libc_errno = -fd;
return -1;
}
diff --git a/libc/src/fcntl/linux/open.cpp b/libc/src/fcntl/linux/open.cpp
index 8fe68debcdcfe..e6ce370d890ed 100644
--- a/libc/src/fcntl/linux/open.cpp
+++ b/libc/src/fcntl/linux/open.cpp
@@ -10,8 +10,8 @@
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
-#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sys/syscall.h> // For syscall numbers.
@@ -38,7 +38,7 @@ LLVM_LIBC_FUNCTION(int, open, (const char *path, int flags, ...)) {
if (fd > 0)
return fd;
- errno = -fd;
+ libc_errno = -fd;
return -1;
}
diff --git a/libc/src/fcntl/linux/openat.cpp b/libc/src/fcntl/linux/openat.cpp
index 23e2c6b89cdcb..d8a1faecd8b73 100644
--- a/libc/src/fcntl/linux/openat.cpp
+++ b/libc/src/fcntl/linux/openat.cpp
@@ -10,8 +10,8 @@
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
-#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sys/syscall.h> // For syscall numbers.
@@ -33,7 +33,7 @@ LLVM_LIBC_FUNCTION(int, openat, (int dfd, const char *path, int flags, ...)) {
if (fd > 0)
return fd;
- errno = -fd;
+ libc_errno = -fd;
return -1;
}
diff --git a/libc/src/inttypes/CMakeLists.txt b/libc/src/inttypes/CMakeLists.txt
index f26273c3a5738..c3111ed80ff81 100644
--- a/libc/src/inttypes/CMakeLists.txt
+++ b/libc/src/inttypes/CMakeLists.txt
@@ -6,6 +6,7 @@ add_entrypoint_object(
strtoimax.h
DEPENDS
libc.src.__support.str_to_integer
+ libc.src.errno.errno
)
add_entrypoint_object(
@@ -16,6 +17,7 @@ add_entrypoint_object(
strtoumax.h
DEPENDS
libc.src.__support.str_to_integer
+ libc.src.errno.errno
)
add_entrypoint_object(
diff --git a/libc/src/inttypes/strtoimax.cpp b/libc/src/inttypes/strtoimax.cpp
index 3fa22662b653e..5a2562380aa6a 100644
--- a/libc/src/inttypes/strtoimax.cpp
+++ b/libc/src/inttypes/strtoimax.cpp
@@ -9,6 +9,7 @@
#include "src/inttypes/strtoimax.h"
#include "src/__support/common.h"
#include "src/__support/str_to_integer.h"
+#include "src/errno/libc_errno.h"
namespace __llvm_libc {
@@ -17,7 +18,7 @@ LLVM_LIBC_FUNCTION(intmax_t, strtoimax,
int base)) {
auto result = internal::strtointeger<intmax_t>(str, base);
if (result.has_error())
- errno = result.error;
+ libc_errno = result.error;
if (str_end != nullptr)
*str_end = const_cast<char *>(str + result.parsed_len);
diff --git a/libc/src/inttypes/strtoumax.cpp b/libc/src/inttypes/strtoumax.cpp
index 6c1ecb6d75814..0e33c55ec3e2d 100644
--- a/libc/src/inttypes/strtoumax.cpp
+++ b/libc/src/inttypes/strtoumax.cpp
@@ -9,6 +9,7 @@
#include "src/inttypes/strtoumax.h"
#include "src/__support/common.h"
#include "src/__support/str_to_integer.h"
+#include "src/errno/libc_errno.h"
namespace __llvm_libc {
@@ -17,7 +18,7 @@ LLVM_LIBC_FUNCTION(uintmax_t, strtoumax,
int base)) {
auto result = internal::strtointeger<uintmax_t>(str, base);
if (result.has_error())
- errno = result.error;
+ libc_errno = result.error;
if (str_end != nullptr)
*str_end = const_cast<char *>(str + result.parsed_len);
diff --git a/libc/src/sched/linux/CMakeLists.txt b/libc/src/sched/linux/CMakeLists.txt
index 2246066d7c524..82d3ab5c3a5ca 100644
--- a/libc/src/sched/linux/CMakeLists.txt
+++ b/libc/src/sched/linux/CMakeLists.txt
@@ -5,7 +5,6 @@ add_entrypoint_object(
HDRS
../sched_getaffinity.h
DEPENDS
- libc.include.errno
libc.include.sched
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
@@ -18,7 +17,6 @@ add_entrypoint_object(
HDRS
../sched_setaffinity.h
DEPENDS
- libc.include.errno
libc.include.sched
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
diff --git a/libc/src/sched/linux/sched_getaffinity.cpp b/libc/src/sched/linux/sched_getaffinity.cpp
index a4c3de9808f0b..f53bf4d99675a 100644
--- a/libc/src/sched/linux/sched_getaffinity.cpp
+++ b/libc/src/sched/linux/sched_getaffinity.cpp
@@ -10,8 +10,8 @@
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
-#include <errno.h>
#include <sched.h>
#include <stdint.h>
#include <sys/syscall.h> // For syscall numbers.
@@ -23,7 +23,7 @@ LLVM_LIBC_FUNCTION(int, sched_getaffinity,
long ret =
__llvm_libc::syscall_impl(SYS_sched_getaffinity, tid, cpuset_size, mask);
if (ret < 0) {
- errno = -ret;
+ libc_errno = -ret;
return -1;
}
if (size_t(ret) < cpuset_size) {
diff --git a/libc/src/sched/linux/sched_setaffinity.cpp b/libc/src/sched/linux/sched_setaffinity.cpp
index 6feb9977912bb..07e51ee362af0 100644
--- a/libc/src/sched/linux/sched_setaffinity.cpp
+++ b/libc/src/sched/linux/sched_setaffinity.cpp
@@ -10,8 +10,8 @@
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
-#include <errno.h>
#include <sched.h>
#include <sys/syscall.h> // For syscall numbers.
@@ -22,7 +22,7 @@ LLVM_LIBC_FUNCTION(int, sched_setaffinity,
long ret =
__llvm_libc::syscall_impl(SYS_sched_setaffinity, tid, cpuset_size, mask);
if (ret < 0) {
- errno = -ret;
+ libc_errno = -ret;
return -1;
}
return 0;
diff --git a/libc/test/src/dirent/CMakeLists.txt b/libc/test/src/dirent/CMakeLists.txt
index cb96dd510265c..014fe37cc09f6 100644
--- a/libc/test/src/dirent/CMakeLists.txt
+++ b/libc/test/src/dirent/CMakeLists.txt
@@ -8,11 +8,11 @@ add_libc_unittest(
SRCS
dirent_test.cpp
DEPENDS
- libc.include.dirent
libc.src.__support.CPP.string_view
libc.src.dirent.closedir
libc.src.dirent.dirfd
libc.src.dirent.opendir
libc.src.dirent.readdir
+ libc.src.errno.errno
)
diff --git a/libc/test/src/dirent/dirent_test.cpp b/libc/test/src/dirent/dirent_test.cpp
index 7c168e78a63a1..72f9f4632c8dc 100644
--- a/libc/test/src/dirent/dirent_test.cpp
+++ b/libc/test/src/dirent/dirent_test.cpp
@@ -11,11 +11,11 @@
#include "src/dirent/dirfd.h"
#include "src/dirent/opendir.h"
#include "src/dirent/readdir.h"
+#include "src/errno/libc_errno.h"
#include "test/UnitTest/Test.h"
#include <dirent.h>
-#include <errno.h>
using string_view = __llvm_libc::cpp::string_view;
@@ -44,7 +44,7 @@ TEST(LlvmLibcDirentTest, SimpleOpenAndRead) {
}
// Verify that we don't break out of the above loop in error.
- ASSERT_EQ(errno, 0);
+ ASSERT_EQ(libc_errno, 0);
ASSERT_TRUE(file1 != nullptr);
ASSERT_TRUE(file2 != nullptr);
@@ -55,17 +55,17 @@ TEST(LlvmLibcDirentTest, SimpleOpenAndRead) {
}
TEST(LlvmLibcDirentTest, OpenNonExistentDir) {
- errno = 0;
+ libc_errno = 0;
::DIR *dir = __llvm_libc::opendir("___xyz123__.non_existent__");
ASSERT_TRUE(dir == nullptr);
- ASSERT_EQ(errno, ENOENT);
- errno = 0;
+ ASSERT_EQ(libc_errno, ENOENT);
+ libc_errno = 0;
}
TEST(LlvmLibcDirentTest, OpenFile) {
- errno = 0;
+ libc_errno = 0;
::DIR *dir = __llvm_libc::opendir("testdata/file1.txt");
ASSERT_TRUE(dir == nullptr);
- ASSERT_EQ(errno, ENOTDIR);
- errno = 0;
+ ASSERT_EQ(libc_errno, ENOTDIR);
+ libc_errno = 0;
}
diff --git a/libc/test/src/fcntl/CMakeLists.txt b/libc/test/src/fcntl/CMakeLists.txt
index 877e00b9cc420..d90b2fab10347 100644
--- a/libc/test/src/fcntl/CMakeLists.txt
+++ b/libc/test/src/fcntl/CMakeLists.txt
@@ -9,8 +9,8 @@ add_libc_unittest(
SRCS
creat_test.cpp
DEPENDS
- libc.include.errno
libc.include.fcntl
+ libc.src.errno.errno
libc.src.fcntl.creat
libc.src.fcntl.open
libc.src.unistd.close
@@ -24,8 +24,8 @@ add_libc_unittest(
SRCS
openat_test.cpp
DEPENDS
- libc.include.errno
libc.include.fcntl
+ libc.src.errno.errno
libc.src.fcntl.open
libc.src.fcntl.openat
libc.src.unistd.close
diff --git a/libc/test/src/fcntl/creat_test.cpp b/libc/test/src/fcntl/creat_test.cpp
index dfe5600f0f811..b7d577ad461ce 100644
--- a/libc/test/src/fcntl/creat_test.cpp
+++ b/libc/test/src/fcntl/creat_test.cpp
@@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
+#include "src/errno/libc_errno.h"
#include "src/fcntl/creat.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
@@ -13,18 +14,16 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"
-#include <errno.h>
-
TEST(LlvmLibcCreatTest, CreatAndOpen) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
constexpr const char *TEST_FILE = "testdata/creat.test";
int fd = __llvm_libc::creat(TEST_FILE, S_IRWXU);
- ASSERT_EQ(errno, 0);
+ ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
fd = __llvm_libc::open(TEST_FILE, O_RDONLY);
- ASSERT_EQ(errno, 0);
+ ASSERT_EQ(libc_errno, 0);
ASSERT_GT(fd, 0);
ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
diff --git a/libc/test/src/fcntl/openat_test.cpp b/libc/test/src/fcntl/openat_test.cpp
index 9716b1f71182d..5310d4647f851 100644
--- a/libc/test/src/fcntl/openat_test.cpp
+++ b/libc/test/src/fcntl/openat_test.cpp
@@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
+#include "src/errno/libc_errno.h"
#include "src/fcntl/open.h"
#include "src/fcntl/openat.h"
#include "src/unistd/close.h"
@@ -14,7 +15,6 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"
-#include <errno.h>
#include <fcntl.h>
TEST(LlvmLibcUniStd, OpenAndReadTest) {
@@ -22,13 +22,13 @@ TEST(LlvmLibcUniStd, OpenAndReadTest) {
constexpr const char *TEST_DIR = "testdata";
constexpr const char *TEST_FILE = "openat.test";
int dir_fd = __llvm_libc::open(TEST_DIR, O_DIRECTORY);
- ASSERT_EQ(errno, 0);
+ ASSERT_EQ(libc_errno, 0);
ASSERT_GT(dir_fd, 0);
constexpr const char TEST_MSG[] = "openat test";
constexpr int TEST_MSG_SIZE = sizeof(TEST_MSG) - 1;
int read_fd = __llvm_libc::openat(dir_fd, TEST_FILE, O_RDONLY);
- ASSERT_EQ(errno, 0);
+ ASSERT_EQ(libc_errno, 0);
ASSERT_GT(read_fd, 0);
char read_buf[TEST_MSG_SIZE];
ASSERT_THAT(__llvm_libc::read(read_fd, read_buf, TEST_MSG_SIZE),
diff --git a/libc/test/src/sched/CMakeLists.txt b/libc/test/src/sched/CMakeLists.txt
index cddb574fcbc13..ad4dfdb553e1b 100644
--- a/libc/test/src/sched/CMakeLists.txt
+++ b/libc/test/src/sched/CMakeLists.txt
@@ -7,10 +7,10 @@ add_libc_unittest(
SRCS
affinity_test.cpp
DEPENDS
- libc.include.errno
libc.include.sched
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
+ libc.src.errno.errno
libc.src.sched.sched_getaffinity
libc.src.sched.sched_setaffinity
libc.test.errno_setter_matcher
@@ -23,10 +23,10 @@ add_libc_unittest(
SRCS
cpu_count_test.cpp
DEPENDS
- libc.include.errno
libc.include.sched
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
+ libc.src.errno.errno
libc.src.sched.sched_getaffinity
libc.src.sched.__sched_getcpucount
libc.test.errno_setter_matcher
diff --git a/libc/test/src/sched/affinity_test.cpp b/libc/test/src/sched/affinity_test.cpp
index 446e85721e91f..b8da2ebf71480 100644
--- a/libc/test/src/sched/affinity_test.cpp
+++ b/libc/test/src/sched/affinity_test.cpp
@@ -7,17 +7,17 @@
//===----------------------------------------------------------------------===//
#include "src/__support/OSUtil/syscall.h"
+#include "src/errno/libc_errno.h"
#include "src/sched/sched_getaffinity.h"
#include "src/sched/sched_setaffinity.h"
#include "test/ErrnoSetterMatcher.h"
-#include <errno.h>
#include <sched.h>
#include <sys/syscall.h>
TEST(LlvmLibcSchedAffinityTest, SmokeTest) {
cpu_set_t mask;
- errno = 0;
+ libc_errno = 0;
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
pid_t tid = __llvm_libc::syscall_impl(SYS_gettid);
ASSERT_GT(tid, pid_t(0));
@@ -32,13 +32,13 @@ TEST(LlvmLibcSchedAffinityTest, BadMask) {
using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
pid_t tid = __llvm_libc::syscall_impl(SYS_gettid);
- errno = 0;
+ libc_errno = 0;
ASSERT_THAT(__llvm_libc::sched_getaffinity(tid, sizeof(cpu_set_t), nullptr),
Fails(EFAULT));
- errno = 0;
+ libc_errno = 0;
ASSERT_THAT(__llvm_libc::sched_setaffinity(tid, sizeof(cpu_set_t), nullptr),
Fails(EFAULT));
- errno = 0;
+ libc_errno = 0;
}
diff --git a/libc/test/src/sched/cpu_count_test.cpp b/libc/test/src/sched/cpu_count_test.cpp
index 02b3ff2b381e7..1ca643ce74869 100644
--- a/libc/test/src/sched/cpu_count_test.cpp
+++ b/libc/test/src/sched/cpu_count_test.cpp
@@ -7,17 +7,17 @@
//===----------------------------------------------------------------------===//
#include "src/__support/OSUtil/syscall.h"
+#include "src/errno/libc_errno.h"
#include "src/sched/sched_getaffinity.h"
#include "src/sched/sched_getcpucount.h"
#include "test/ErrnoSetterMatcher.h"
-#include <errno.h>
#include <sched.h>
#include <sys/syscall.h>
TEST(LlvmLibcSchedAffinityTest, SmokeTest) {
cpu_set_t mask;
- errno = 0;
+ libc_errno = 0;
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
pid_t tid = __llvm_libc::syscall_impl(SYS_gettid);
ASSERT_GT(tid, pid_t(0));
More information about the libc-commits
mailing list