Skip to content

Commit

Permalink
[libc][NFC] Switch use of errno in src/time and test/src/time to libc…
Browse files Browse the repository at this point in the history
…_errno.

Switch use of errno in src/time and test/src/time to libc_errno.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D145192
  • Loading branch information
rtenneti-google committed Mar 2, 2023
1 parent 7e770f9 commit b98c190
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 45 deletions.
4 changes: 0 additions & 4 deletions libc/src/time/CMakeLists.txt
Expand Up @@ -9,7 +9,6 @@ add_object_library(
HDRS
time_utils.h
DEPENDS
libc.include.errno
libc.include.time
libc.src.errno.errno
)
Expand Down Expand Up @@ -67,7 +66,6 @@ add_entrypoint_object(
gettimeofday.h
DEPENDS
.clock_gettime
libc.include.errno
libc.include.time
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
Expand Down Expand Up @@ -104,7 +102,6 @@ add_entrypoint_object(
mktime.h
DEPENDS
.time_utils
libc.include.errno
libc.include.time
libc.src.errno.errno
)
Expand All @@ -116,7 +113,6 @@ add_entrypoint_object(
HDRS
nanosleep.h
DEPENDS
libc.include.errno
libc.include.time
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
Expand Down
4 changes: 2 additions & 2 deletions libc/src/time/clock_gettime.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 <sys/syscall.h> // For syscall numbers.
#include <time.h>

Expand All @@ -25,7 +25,7 @@ LLVM_LIBC_FUNCTION(int, clock_gettime,
// A negative return value indicates an error with the magnitude of the
// value being the error code.
if (ret_val < 0) {
errno = -ret_val;
libc_errno = -ret_val;
return -1;
}

Expand Down
4 changes: 2 additions & 2 deletions libc/src/time/gettimeofday.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 <sys/syscall.h> // For syscall numbers.

namespace __llvm_libc {
Expand All @@ -27,7 +27,7 @@ LLVM_LIBC_FUNCTION(int, gettimeofday,
// A negative return value indicates an error with the magnitude of the
// value being the error code.
if (ret_val < 0) {
errno = -ret_val;
libc_errno = -ret_val;
return -1;
}
tv->tv_sec = tp.tv_sec;
Expand Down
4 changes: 2 additions & 2 deletions libc/src/time/linux/clock.cpp
Expand Up @@ -11,8 +11,8 @@
#include "src/__support/CPP/limits.h"
#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 <sys/syscall.h> // For syscall numbers.
#include <time.h>

Expand All @@ -23,7 +23,7 @@ LLVM_LIBC_FUNCTION(clock_t, clock, ()) {
long ret_val = __llvm_libc::syscall_impl(
SYS_clock_gettime, CLOCK_PROCESS_CPUTIME_ID, reinterpret_cast<long>(&ts));
if (ret_val < 0) {
errno = -ret_val;
libc_errno = -ret_val;
return clock_t(-1);
}

Expand Down
4 changes: 2 additions & 2 deletions libc/src/time/linux/time.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 <sys/syscall.h> // For syscall numbers.
#include <time.h>

Expand All @@ -23,7 +23,7 @@ LLVM_LIBC_FUNCTION(time_t, time, (time_t * tp)) {
long ret_val = __llvm_libc::syscall_impl(SYS_clock_gettime, CLOCK_REALTIME,
reinterpret_cast<long>(&ts));
if (ret_val < 0) {
errno = -ret_val;
libc_errno = -ret_val;
return -1;
}

Expand Down
4 changes: 2 additions & 2 deletions libc/src/time/nanosleep.cpp
Expand Up @@ -8,11 +8,11 @@
//===----------------------------------------------------------------------===//

#include "src/time/nanosleep.h"

#include "include/sys/syscall.h" // For syscall numbers.
#include "src/__support/OSUtil/syscall.h" // For syscall functions.
#include "src/__support/common.h"

#include <errno.h>
#include "src/errno/libc_errno.h"

namespace __llvm_libc {

Expand Down
8 changes: 3 additions & 5 deletions libc/src/time/time_utils.h
Expand Up @@ -11,10 +11,8 @@

#include <stddef.h> // For size_t.

#include "include/errno.h"

#include "src/__support/common.h"
#include "src/errno/llvmlibc_errno.h"
#include "src/errno/libc_errno.h"
#include "src/time/mktime.h"

#include <stdint.h>
Expand Down Expand Up @@ -86,11 +84,11 @@ extern int64_t update_from_seconds(int64_t total_seconds, struct tm *tm);

// POSIX.1-2017 requires this.
LIBC_INLINE time_t out_of_range() {
llvmlibc_errno = EOVERFLOW;
libc_errno = EOVERFLOW;
return static_cast<time_t>(-1);
}

LIBC_INLINE void invalid_value() { llvmlibc_errno = EINVAL; }
LIBC_INLINE void invalid_value() { libc_errno = EINVAL; }

LIBC_INLINE char *asctime(const struct tm *timeptr, char *buffer,
size_t bufferLength) {
Expand Down
8 changes: 4 additions & 4 deletions libc/test/src/time/CMakeLists.txt
Expand Up @@ -62,10 +62,10 @@ add_libc_unittest(
CXX_STANDARD
20
DEPENDS
libc.include.errno
libc.include.time
libc.src.time.gettimeofday
libc.src.time.nanosleep
libc.src.errno.errno
)

add_libc_unittest(
Expand Down Expand Up @@ -119,9 +119,9 @@ add_libc_unittest(
CXX_STANDARD
20
DEPENDS
libc.include.errno
libc.include.time
libc.src.time.nanosleep
libc.src.errno.errno
)

add_libc_unittest(
Expand All @@ -131,9 +131,9 @@ add_libc_unittest(
SRCS
time_test.cpp
DEPENDS
libc.include.errno
libc.include.time
libc.src.time.time
libc.src.errno.errno
)

add_libc_unittest(
Expand All @@ -143,7 +143,7 @@ add_libc_unittest(
SRCS
clock_test.cpp
DEPENDS
libc.include.errno
libc.include.time
libc.src.time.clock
libc.src.errno.errno
)
7 changes: 4 additions & 3 deletions libc/test/src/time/asctime_r_test.cpp
Expand Up @@ -7,6 +7,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/time/asctime_r.h"
#include "src/time/time_utils.h"
#include "test/UnitTest/Test.h"
Expand All @@ -27,17 +28,17 @@ static inline char *call_asctime_r(struct tm *tm_data, int year, int month,
TEST(LlvmLibcAsctimeR, Nullptr) {
char *result;
result = __llvm_libc::asctime_r(nullptr, nullptr);
ASSERT_EQ(EINVAL, llvmlibc_errno);
ASSERT_EQ(EINVAL, libc_errno);
ASSERT_STREQ(nullptr, result);

char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
result = __llvm_libc::asctime_r(nullptr, buffer);
ASSERT_EQ(EINVAL, llvmlibc_errno);
ASSERT_EQ(EINVAL, libc_errno);
ASSERT_STREQ(nullptr, result);

struct tm tm_data;
result = __llvm_libc::asctime_r(&tm_data, nullptr);
ASSERT_EQ(EINVAL, llvmlibc_errno);
ASSERT_EQ(EINVAL, libc_errno);
ASSERT_STREQ(nullptr, result);
}

Expand Down
13 changes: 7 additions & 6 deletions libc/test/src/time/asctime_test.cpp
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/time/asctime.h"
#include "test/UnitTest/Test.h"
#include "test/src/time/TmHelper.h"
Expand All @@ -21,7 +22,7 @@ static inline char *call_asctime(struct tm *tm_data, int year, int month,
TEST(LlvmLibcAsctime, Nullptr) {
char *result;
result = __llvm_libc::asctime(nullptr);
ASSERT_EQ(EINVAL, llvmlibc_errno);
ASSERT_EQ(EINVAL, libc_errno);
ASSERT_STREQ(nullptr, result);
}

Expand All @@ -39,7 +40,7 @@ TEST(LlvmLibcAsctime, InvalidWday) {
0, // sec
-1, // wday
0); // yday
ASSERT_EQ(EINVAL, llvmlibc_errno);
ASSERT_EQ(EINVAL, libc_errno);

// Test with wday = 7.
call_asctime(&tm_data,
Expand All @@ -51,7 +52,7 @@ TEST(LlvmLibcAsctime, InvalidWday) {
0, // sec
7, // wday
0); // yday
ASSERT_EQ(EINVAL, llvmlibc_errno);
ASSERT_EQ(EINVAL, libc_errno);
}

// Months are from January to December. Test passing invalid value in month.
Expand All @@ -68,7 +69,7 @@ TEST(LlvmLibcAsctime, InvalidMonth) {
0, // sec
4, // wday
0); // yday
ASSERT_EQ(EINVAL, llvmlibc_errno);
ASSERT_EQ(EINVAL, libc_errno);

// Test with month = 13.
call_asctime(&tm_data,
Expand All @@ -80,7 +81,7 @@ TEST(LlvmLibcAsctime, InvalidMonth) {
0, // sec
4, // wday
0); // yday
ASSERT_EQ(EINVAL, llvmlibc_errno);
ASSERT_EQ(EINVAL, libc_errno);
}

TEST(LlvmLibcAsctime, ValidWeekdays) {
Expand Down Expand Up @@ -208,6 +209,6 @@ TEST(LlvmLibcAsctime, Max64BitYear) {
50, // sec
2, // wday
50); // yday
ASSERT_EQ(EOVERFLOW, llvmlibc_errno);
ASSERT_EQ(EOVERFLOW, libc_errno);
ASSERT_STREQ(nullptr, result);
}
1 change: 0 additions & 1 deletion libc/test/src/time/clock_test.cpp
Expand Up @@ -9,7 +9,6 @@
#include "src/time/clock.h"
#include "test/UnitTest/Test.h"

#include <errno.h>
#include <limits.h>
#include <time.h>

Expand Down
2 changes: 0 additions & 2 deletions libc/test/src/time/difftime_test.cpp
Expand Up @@ -12,8 +12,6 @@
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include <errno.h>

using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
using __llvm_libc::time_utils::TimeConstants;

Expand Down
1 change: 0 additions & 1 deletion libc/test/src/time/gettimeofday_test.cpp
Expand Up @@ -6,7 +6,6 @@
//
//===----------------------------------------------------------------------===//

#include <errno.h>
#include <time.h>

#include "src/time/gettimeofday.h"
Expand Down
8 changes: 4 additions & 4 deletions libc/test/src/time/gmtime_test.cpp
Expand Up @@ -6,13 +6,13 @@
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/time/gmtime.h"
#include "src/time/time_utils.h"
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
#include "test/src/time/TmMatcher.h"

#include <errno.h>
#include <limits.h>

using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
Expand All @@ -25,15 +25,15 @@ TEST(LlvmLibcGmTime, OutOfRange) {
TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR);
struct tm *tm_data = __llvm_libc::gmtime(&seconds);
EXPECT_TRUE(tm_data == nullptr);
EXPECT_EQ(llvmlibc_errno, EOVERFLOW);
EXPECT_EQ(libc_errno, EOVERFLOW);

llvmlibc_errno = 0;
libc_errno = 0;
seconds = INT_MIN * static_cast<int64_t>(
TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR) -
1;
tm_data = __llvm_libc::gmtime(&seconds);
EXPECT_TRUE(tm_data == nullptr);
EXPECT_EQ(llvmlibc_errno, EOVERFLOW);
EXPECT_EQ(libc_errno, EOVERFLOW);
}

TEST(LlvmLibcGmTime, InvalidSeconds) {
Expand Down
1 change: 0 additions & 1 deletion libc/test/src/time/mktime_test.cpp
Expand Up @@ -13,7 +13,6 @@
#include "test/src/time/TmHelper.h"
#include "test/src/time/TmMatcher.h"

#include <errno.h>
#include <limits.h>

using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/time/nanosleep_test.cpp
Expand Up @@ -7,9 +7,9 @@
//
//===----------------------------------------------------------------------===//

#include <errno.h>
#include <time.h>

#include "src/errno/libc_errno.h"
#include "src/time/nanosleep.h"
#include "test/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
Expand All @@ -19,11 +19,11 @@ namespace cpp = __llvm_libc::cpp;
TEST(LlvmLibcNanosleep, SmokeTest) {
// TODO: When we have the code to read clocks, test that time has passed.
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
errno = 0;
libc_errno = 0;

struct timespec tim = {1, 500};
struct timespec tim2 = {0, 0};
int ret = __llvm_libc::nanosleep(&tim, &tim2);
ASSERT_EQ(errno, 0);
ASSERT_EQ(libc_errno, 0);
ASSERT_EQ(ret, 0);
}
1 change: 0 additions & 1 deletion libc/test/src/time/time_test.cpp
Expand Up @@ -9,7 +9,6 @@
#include "src/time/time_func.h"
#include "test/UnitTest/Test.h"

#include <errno.h>
#include <limits.h>
#include <time.h>

Expand Down

0 comments on commit b98c190

Please sign in to comment.