Skip to content

Commit

Permalink
[libc][NFC] Switch dirent, fcntl, inttypes and sched to use libc_errno.
Browse files Browse the repository at this point in the history
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D145222
  • Loading branch information
Siva Chandra Reddy committed Mar 6, 2023
1 parent deb6fb6 commit d49b993
Show file tree
Hide file tree
Showing 21 changed files with 48 additions and 50 deletions.
4 changes: 2 additions & 2 deletions libc/src/dirent/closedir.cpp
Expand Up @@ -10,17 +10,17 @@

#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(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;
Expand Down
4 changes: 2 additions & 2 deletions libc/src/dirent/opendir.cpp
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions libc/src/dirent/readdir.cpp
Expand Up @@ -10,17 +10,17 @@

#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(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;
Expand Down
3 changes: 0 additions & 3 deletions libc/src/fcntl/linux/CMakeLists.txt
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions libc/src/fcntl/linux/creat.cpp
Expand Up @@ -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.

Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions libc/src/fcntl/linux/open.cpp
Expand Up @@ -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.
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions libc/src/fcntl/linux/openat.cpp
Expand Up @@ -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.
Expand All @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions libc/src/inttypes/CMakeLists.txt
Expand Up @@ -6,6 +6,7 @@ add_entrypoint_object(
strtoimax.h
DEPENDS
libc.src.__support.str_to_integer
libc.src.errno.errno
)

add_entrypoint_object(
Expand All @@ -16,6 +17,7 @@ add_entrypoint_object(
strtoumax.h
DEPENDS
libc.src.__support.str_to_integer
libc.src.errno.errno
)

add_entrypoint_object(
Expand Down
3 changes: 2 additions & 1 deletion libc/src/inttypes/strtoimax.cpp
Expand Up @@ -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 {

Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion libc/src/inttypes/strtoumax.cpp
Expand Up @@ -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 {

Expand All @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions libc/src/sched/linux/CMakeLists.txt
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions libc/src/sched/linux/sched_getaffinity.cpp
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions libc/src/sched/linux/sched_setaffinity.cpp
Expand Up @@ -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.

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/dirent/CMakeLists.txt
Expand Up @@ -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
)

16 changes: 8 additions & 8 deletions libc/test/src/dirent/dirent_test.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions libc/test/src/fcntl/CMakeLists.txt
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions libc/test/src/fcntl/creat_test.cpp
Expand Up @@ -6,25 +6,24 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/fcntl/creat.h"
#include "src/fcntl/open.h"
#include "src/unistd/close.h"
#include "test/ErrnoSetterMatcher.h"
#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));

Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/fcntl/openat_test.cpp
Expand Up @@ -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"
Expand All @@ -14,21 +15,20 @@
#include "test/UnitTest/Test.h"
#include "utils/testutils/FDReader.h"

#include <errno.h>
#include <fcntl.h>

TEST(LlvmLibcUniStd, OpenAndReadTest) {
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
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),
Expand Down

0 comments on commit d49b993

Please sign in to comment.