Skip to content

Commit f9c2377

Browse files
[libc][NFC] Cleanup time.h (llvm#122027)
While working on strftime I noticed some constants were being defined in unexpected places. One thing led to another, and I ended up doing a major cleanup of the time functions. What's included: All uses of <time.h> in /src and /test removed (except for LibcTest.cpp) The various time constants have been moved to time_constants.h, and the `time_constants` namespace. struct tm gets its own type indirection header now.
1 parent 75781fa commit f9c2377

33 files changed

+790
-556
lines changed

libc/hdr/types/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ add_proxy_header_library(
8585
libc.include.llvm-libc-types.struct_timespec
8686
)
8787

88+
add_proxy_header_library(
89+
struct_tm
90+
HDRS
91+
struct_tm.h
92+
FULL_BUILD_DEPENDS
93+
libc.include.llvm-libc-types.struct_tm
94+
)
95+
8896
add_proxy_header_library(
8997
size_t
9098
HDRS

libc/hdr/types/struct_tm.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Proxy for struct tm ----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_TM_H
9+
#define LLVM_LIBC_HDR_TYPES_STRUCT_TM_H
10+
11+
#ifdef LIBC_FULL_BUILD
12+
13+
#include "include/llvm-libc-types/struct_tm.h"
14+
15+
#else
16+
17+
#include <time.h>
18+
19+
#endif // LIBC_FULL_BUILD
20+
21+
#endif // LLVM_LIBC_HDR_TYPES_STRUCT_TM_H

libc/src/pthread/pthread_condattr_init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
1313

14-
#include <pthread.h> // pthread_condattr_t, PTHREAD_PROCESS_PRIVATE
15-
#include <time.h> // CLOCK_REALTIME
14+
#include "hdr/time_macros.h" // CLOCK_REALTIME
15+
#include <pthread.h> // pthread_condattr_t, PTHREAD_PROCESS_PRIVATE
1616

1717
namespace LIBC_NAMESPACE_DECL {
1818

libc/src/pthread/pthread_condattr_setclock.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
#include "src/__support/macros/config.h"
1313
#include "src/errno/libc_errno.h"
1414

15-
#include <pthread.h> // pthread_condattr_t
16-
#include <sys/types.h> // clockid_t
17-
#include <time.h> // CLOCK_MONOTONIC, CLOCK_REALTIME
15+
#include "hdr/time_macros.h" // CLOCK_MONOTONIC, CLOCK_REALTIME
16+
#include <pthread.h> // pthread_condattr_t
17+
#include <sys/types.h> // clockid_t
1818

1919
namespace LIBC_NAMESPACE_DECL {
2020

libc/src/time/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
22
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
33
endif()
44

5+
add_header_library(
6+
time_constants
7+
HDRS
8+
time_constants.h
9+
DEPENDS
10+
libc.include.time
11+
libc.src.__support.CPP.array
12+
libc.src.__support.CPP.string_view
13+
libc.hdr.types.time_t
14+
)
15+
516
add_object_library(
617
time_utils
718
SRCS
@@ -12,6 +23,10 @@ add_object_library(
1223
libc.include.time
1324
libc.src.__support.CPP.limits
1425
libc.src.errno.errno
26+
.time_constants
27+
libc.hdr.types.time_t
28+
libc.hdr.types.size_t
29+
libc.hdr.types.struct_tm
1530
)
1631

1732
add_entrypoint_object(
@@ -22,7 +37,9 @@ add_entrypoint_object(
2237
asctime.h
2338
DEPENDS
2439
.time_utils
40+
.time_constants
2541
libc.include.time
42+
libc.hdr.types.struct_tm
2643
)
2744

2845
add_entrypoint_object(
@@ -33,7 +50,9 @@ add_entrypoint_object(
3350
asctime_r.h
3451
DEPENDS
3552
.time_utils
53+
.time_constants
3654
libc.include.time
55+
libc.hdr.types.struct_tm
3756
)
3857

3958
add_entrypoint_object(
@@ -44,6 +63,7 @@ add_entrypoint_object(
4463
ctime.h
4564
DEPENDS
4665
.time_utils
66+
.time_constants
4767
libc.hdr.types.time_t
4868
libc.include.time
4969
)
@@ -56,6 +76,7 @@ add_entrypoint_object(
5676
ctime_r.h
5777
DEPENDS
5878
.time_utils
79+
.time_constants
5980
libc.hdr.types.time_t
6081
libc.include.time
6182
)
@@ -68,6 +89,7 @@ add_entrypoint_object(
6889
difftime.h
6990
DEPENDS
7091
libc.include.time
92+
libc.hdr.types.time_t
7193
)
7294

7395
add_entrypoint_object(
@@ -79,6 +101,8 @@ add_entrypoint_object(
79101
DEPENDS
80102
.time_utils
81103
libc.include.time
104+
libc.hdr.types.time_t
105+
libc.hdr.types.struct_tm
82106
)
83107

84108
add_entrypoint_object(
@@ -90,6 +114,8 @@ add_entrypoint_object(
90114
DEPENDS
91115
.time_utils
92116
libc.include.time
117+
libc.hdr.types.time_t
118+
libc.hdr.types.struct_tm
93119
)
94120

95121
add_entrypoint_object(
@@ -100,8 +126,11 @@ add_entrypoint_object(
100126
mktime.h
101127
DEPENDS
102128
.time_utils
129+
.time_constants
103130
libc.include.time
104131
libc.src.errno.errno
132+
libc.hdr.types.time_t
133+
libc.hdr.types.struct_tm
105134
)
106135

107136
add_entrypoint_object(
@@ -115,6 +144,7 @@ add_entrypoint_object(
115144
libc.hdr.types.time_t
116145
libc.src.__support.time.clock_gettime
117146
libc.src.errno.errno
147+
libc.hdr.types.struct_tm
118148
)
119149

120150
add_entrypoint_object(

libc/src/time/asctime.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
#include "src/time/asctime.h"
1010
#include "src/__support/common.h"
1111
#include "src/__support/macros/config.h"
12+
#include "src/time/time_constants.h"
1213
#include "src/time/time_utils.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {
1516

16-
using LIBC_NAMESPACE::time_utils::TimeConstants;
17-
1817
LLVM_LIBC_FUNCTION(char *, asctime, (const struct tm *timeptr)) {
19-
static char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
20-
return time_utils::asctime(timeptr, buffer, TimeConstants::ASCTIME_MAX_BYTES);
18+
static char buffer[time_constants::ASCTIME_BUFFER_SIZE];
19+
return time_utils::asctime(timeptr, buffer,
20+
time_constants::ASCTIME_MAX_BYTES);
2121
}
2222

2323
} // namespace LIBC_NAMESPACE_DECL

libc/src/time/asctime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef LLVM_LIBC_SRC_TIME_ASCTIME_H
1010
#define LLVM_LIBC_SRC_TIME_ASCTIME_H
1111

12+
#include "hdr/types/struct_tm.h"
1213
#include "src/__support/macros/config.h"
13-
#include <time.h>
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

libc/src/time/asctime_r.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
#include "src/time/asctime_r.h"
1010
#include "src/__support/common.h"
1111
#include "src/__support/macros/config.h"
12+
#include "src/time/time_constants.h"
1213
#include "src/time/time_utils.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {
1516

16-
using LIBC_NAMESPACE::time_utils::TimeConstants;
17-
1817
LLVM_LIBC_FUNCTION(char *, asctime_r,
1918
(const struct tm *timeptr, char *buffer)) {
20-
return time_utils::asctime(timeptr, buffer, TimeConstants::ASCTIME_MAX_BYTES);
19+
return time_utils::asctime(timeptr, buffer,
20+
time_constants::ASCTIME_MAX_BYTES);
2121
}
2222

2323
} // namespace LIBC_NAMESPACE_DECL

libc/src/time/asctime_r.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef LLVM_LIBC_SRC_TIME_ASCTIME_R_H
1010
#define LLVM_LIBC_SRC_TIME_ASCTIME_R_H
1111

12+
#include "hdr/types/struct_tm.h"
1213
#include "src/__support/macros/config.h"
13-
#include <time.h>
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

libc/src/time/ctime.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "ctime.h"
9+
#include "src/time/ctime.h"
1010
#include "src/__support/CPP/limits.h"
1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
13-
#include "time_utils.h"
13+
#include "src/time/time_constants.h"
14+
#include "src/time/time_utils.h"
1415

1516
namespace LIBC_NAMESPACE_DECL {
1617

17-
using LIBC_NAMESPACE::time_utils::TimeConstants;
18-
1918
LLVM_LIBC_FUNCTION(char *, ctime, (const time_t *t_ptr)) {
2019
if (t_ptr == nullptr || *t_ptr > cpp::numeric_limits<int32_t>::max()) {
2120
return nullptr;
2221
}
23-
static char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
22+
static char buffer[time_constants::ASCTIME_BUFFER_SIZE];
2423
return time_utils::asctime(time_utils::localtime(t_ptr), buffer,
25-
TimeConstants::ASCTIME_MAX_BYTES);
24+
time_constants::ASCTIME_MAX_BYTES);
2625
}
2726

2827
} // namespace LIBC_NAMESPACE_DECL

libc/src/time/ctime_r.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,23 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "ctime_r.h"
9+
#include "src/time/ctime_r.h"
1010
#include "src/__support/CPP/limits.h"
1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
13-
#include "time_utils.h"
13+
#include "src/time/time_constants.h"
14+
#include "src/time/time_utils.h"
1415

1516
namespace LIBC_NAMESPACE_DECL {
1617

17-
using LIBC_NAMESPACE::time_utils::TimeConstants;
18-
1918
LLVM_LIBC_FUNCTION(char *, ctime_r, (const time_t *t_ptr, char *buffer)) {
2019
if (t_ptr == nullptr || buffer == nullptr ||
2120
*t_ptr > cpp::numeric_limits<int32_t>::max()) {
2221
return nullptr;
2322
}
2423

2524
return time_utils::asctime(time_utils::localtime(t_ptr), buffer,
26-
TimeConstants::ASCTIME_MAX_BYTES);
25+
time_constants::ASCTIME_MAX_BYTES);
2726
}
2827

2928
} // namespace LIBC_NAMESPACE_DECL

libc/src/time/difftime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef LLVM_LIBC_SRC_TIME_DIFFTIME_H
1010
#define LLVM_LIBC_SRC_TIME_DIFFTIME_H
1111

12+
#include "hdr/types/time_t.h"
1213
#include "src/__support/macros/config.h"
13-
#include <time.h>
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

libc/src/time/gmtime.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
#ifndef LLVM_LIBC_SRC_TIME_GMTIME_H
1010
#define LLVM_LIBC_SRC_TIME_GMTIME_H
1111

12+
#include "hdr/types/struct_tm.h"
13+
#include "hdr/types/time_t.h"
1214
#include "src/__support/macros/config.h"
13-
#include <time.h>
1415

1516
namespace LIBC_NAMESPACE_DECL {
1617

libc/src/time/gmtime_r.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
#ifndef LLVM_LIBC_SRC_TIME_GMTIME_R_H
1010
#define LLVM_LIBC_SRC_TIME_GMTIME_R_H
1111

12+
#include "hdr/types/struct_tm.h"
13+
#include "hdr/types/time_t.h"
1214
#include "src/__support/macros/config.h"
13-
#include <time.h>
1415

1516
namespace LIBC_NAMESPACE_DECL {
1617

0 commit comments

Comments
 (0)