Skip to content

Commit 17bddd1

Browse files
zimirzaЗишан МирзаZishan Mirza
authored
[libc] implement template functions for localtime (#110363)
This is an implementation for template functions of localtime. Update for this pull request: Implementation as been removed from this pull request and will be added to a new one. This is because this pull request is getting big. This pull request will only contain template functions in order to implement localtime. Update: The implementation is available in https://github.com/zimirza/llvm-project/tree/localtime_implementation. --------- Co-authored-by: Зишан Мирза <[email protected]> Co-authored-by: Zishan Mirza <[email protected]>
1 parent 74d52f9 commit 17bddd1

File tree

17 files changed

+448
-6
lines changed

17 files changed

+448
-6
lines changed

libc/config/baremetal/aarch64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ set(TARGET_LIBC_ENTRYPOINTS
269269
libc.src.time.difftime
270270
libc.src.time.gmtime
271271
libc.src.time.gmtime_r
272+
libc.src.time.localtime
273+
libc.src.time.localtime_r
272274
libc.src.time.mktime
273275
libc.src.time.strftime
274276
libc.src.time.strftime_l

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,8 @@ if(LLVM_LIBC_FULL_BUILD)
13071307
libc.src.time.gettimeofday
13081308
libc.src.time.gmtime
13091309
libc.src.time.gmtime_r
1310+
libc.src.time.localtime
1311+
libc.src.time.localtime_r
13101312
libc.src.time.mktime
13111313
libc.src.time.nanosleep
13121314
libc.src.time.strftime

libc/docs/headers/time.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ Implementation Status
8787
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
8888
| gmtime_r | |check| | |check| | | |check| | | | | | | | | | |
8989
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
90-
| localtime | | | | | | | | | | | | | |
90+
| localtime | |check| | |check| | | |check| | | | | | | | | | |
9191
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
92-
| localtime_r | | | | | | | | | | | | | |
92+
| localtime_r | |check| | |check| | | |check| | | | | | | | | | |
9393
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
9494
| mktime | |check| | |check| | | |check| | | | | | | | | | |
9595
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
@@ -112,4 +112,4 @@ Implementation Status
112112
| timer_settime | | | | | | | | | | | | | |
113113
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
114114
| tzset | | | | | | | | | | | | | |
115-
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
115+
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+

libc/hdr/localtime_overlay.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===-- Including localtime.h in overlay mode -----------------------------===//
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+
9+
#ifndef LLVM_LIBC_HDR_LOCALTIME_OVERLAY_H
10+
#define LLVM_LIBC_HDR_LOCALTIME_OVERLAY_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
#error "This header should only be included in overlay mode"
14+
#endif
15+
16+
// Overlay mode
17+
18+
// glibc <unistd.h> header might provide extern inline definitions for few
19+
// functions, causing external alias errors. They are guarded by
20+
// `__USE_EXTERN_INLINES` macro.
21+
22+
#ifdef __USE_EXTERN_INLINES
23+
#define LIBC_OLD_USE_EXTERN_INLINES
24+
#undef __USE_EXTERN_INLINES
25+
#endif
26+
27+
#ifndef __NO_INLINE__
28+
#define __NO_INLINE__ 1
29+
#define LIBC_SET_NO_INLINE
30+
#endif
31+
32+
#include <localtime.h>
33+
#include <localtime_r.h>
34+
35+
#ifdef LIBC_SET_NO_INLINE
36+
#undef __NO_INLINE__
37+
#undef LIBC_SET_NO_INLINE
38+
#endif
39+
40+
#ifdef LIBC_OLD_USE_EXTERN_INLINES
41+
#define __USE_EXTERN_INLINES
42+
#undef LIBC_OLD_USE_EXTERN_INLINES
43+
#endif
44+
45+
#endif // LLVM_LIBC_HDR_LOCALTIME_OVERLAY_H

libc/include/time.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ functions:
4141
arguments:
4242
- type: const time_t *
4343
- type: char *
44+
- name: localtime
45+
standard:
46+
- stdc
47+
return_type: struct tm *
48+
arguments:
49+
- type: const time_t *
50+
- name: localtime_r
51+
standard:
52+
- stdc
53+
return_type: struct tm *
54+
arguments:
55+
- type: const time_t *
56+
- type: struct tm *
4457
- name: clock
4558
standard:
4659
- stdc

libc/src/time/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@ add_entrypoint_object(
8585
libc.include.time
8686
)
8787

88+
add_entrypoint_object(
89+
localtime
90+
SRCS
91+
localtime.cpp
92+
HDRS
93+
localtime.h
94+
DEPENDS
95+
.time_utils
96+
libc.hdr.types.time_t
97+
libc.hdr.types.struct_tm
98+
)
99+
100+
add_entrypoint_object(
101+
localtime_r
102+
SRCS
103+
localtime_r.cpp
104+
HDRS
105+
localtime_r.h
106+
DEPENDS
107+
.time_utils
108+
libc.hdr.types.time_t
109+
libc.hdr.types.struct_tm
110+
)
111+
88112
add_entrypoint_object(
89113
difftime
90114
SRCS

libc/src/time/baremetal/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,29 @@ add_entrypoint_object(
1919
libc.hdr.time_macros
2020
libc.hdr.types.struct_timespec
2121
)
22+
23+
add_entrypoint_object(
24+
localtime
25+
SRCS
26+
localtime.cpp
27+
HDRS
28+
../localtime.h
29+
time_utils.h
30+
DEPENDS
31+
.time_utils
32+
libc.hdr.types.struct_tm
33+
libc.hdr.types.time_t
34+
)
35+
36+
add_entrypoint_object(
37+
localtime_r
38+
SRCS
39+
localtime_r.cpp
40+
HDRS
41+
../localtime.h
42+
time_utils.h
43+
DEPENDS
44+
.time_utils
45+
libc.hdr.types.struct_tm
46+
libc.hdr.types.time_t
47+
)

libc/src/time/baremetal/localtime.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation of localtime for baremetal -------------------------===//
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+
9+
#include "src/time/localtime.h"
10+
#include "hdr/types/struct_tm.h"
11+
#include "hdr/types/time_t.h"
12+
#include "src/time/time_utils.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
LLVM_LIBC_FUNCTION(struct tm *, localtime, (time_t *timer)) {
17+
static struct tm tm_out;
18+
19+
return time_utils::localtime_internal(timer, &tm_out);
20+
}
21+
22+
} // namespace LIBC_NAMESPACE_DECL
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Implementation of localtime_r for baremetal -----------------------===//
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+
9+
#include "src/time/localtime_r.h"
10+
#include "hdr/types/struct_tm.h"
11+
#include "hdr/types/time_t.h"
12+
#include "src/__support/macros/null_check.h"
13+
#include "src/time/time_utils.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(struct tm *, localtime_r,
18+
(const time_t *timer, struct tm *buf)) {
19+
LIBC_CRASH_ON_NULLPTR(timer);
20+
21+
return time_utils::localtime_internal(timer, buf);
22+
}
23+
24+
} // namespace LIBC_NAMESPACE_DECL

libc/src/time/localtime.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Linux implementation of the localtime function --------------------===//
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+
9+
#include "src/time/localtime.h"
10+
#include "hdr/types/struct_tm.h"
11+
#include "hdr/types/time_t.h"
12+
#include "src/__support/macros/null_check.h"
13+
#include "src/time/time_utils.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(struct tm *, localtime, (const time_t *timer)) {
18+
LIBC_CRASH_ON_NULLPTR(timer);
19+
20+
static struct tm tm_out;
21+
return time_utils::localtime_internal(timer, &tm_out);
22+
}
23+
24+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)