[libc-commits] [libc] [libc] Add stubs for 'strftime' function (PR #106631)

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Fri Aug 30 17:42:06 PDT 2024


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/106631

>From 1163fc6935f7447579d267f7475b2984a76a4c61 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Sun, 11 Aug 2024 09:26:00 -0500
Subject: [PATCH 1/2] [libc] Add stubs for 'strftime' function

Summary:
This patch does not actually implement the function, but provides the
interface. This partially resovles
https://github.com/llvm/llvm-project/issues/106630 as it will allow us
to build libc++ with locale support (so long as you don't use it).
---
 libc/config/gpu/entrypoints.txt |  2 ++
 libc/newhdrgen/yaml/time.yaml   | 20 ++++++++++++++++++++
 libc/spec/stdc.td               | 21 +++++++++++++++++++++
 libc/src/time/CMakeLists.txt    | 23 ++++++++++++++++++++++-
 libc/src/time/strftime.cpp      | 25 +++++++++++++++++++++++++
 libc/src/time/strftime.h        | 23 +++++++++++++++++++++++
 libc/src/time/strftime_l.cpp    | 23 +++++++++++++++++++++++
 libc/src/time/strftime_l.h      | 24 ++++++++++++++++++++++++
 8 files changed, 160 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/time/strftime.cpp
 create mode 100644 libc/src/time/strftime.h
 create mode 100644 libc/src/time/strftime_l.cpp
 create mode 100644 libc/src/time/strftime_l.h

diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index 706f603b6ff56f..eeb918ef5c1684 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -254,6 +254,8 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.time.clock
     libc.src.time.clock_gettime
     libc.src.time.nanosleep
+    libc.src.time.strftime
+    libc.src.time.strftime_l
 
     # wchar.h entrypoints
     libc.src.wchar.wctob
diff --git a/libc/newhdrgen/yaml/time.yaml b/libc/newhdrgen/yaml/time.yaml
index d2344671831c7a..a2947be9ef913a 100644
--- a/libc/newhdrgen/yaml/time.yaml
+++ b/libc/newhdrgen/yaml/time.yaml
@@ -8,6 +8,7 @@ types:
   - type_name: time_t
   - type_name: clock_t
   - type_name: size_t
+  - type_name: locale_t
 enums: []
 objects: []
 functions:
@@ -83,3 +84,22 @@ functions:
     return_type: time_t
     arguments:
       - type: time_t *
+  - name: strftime
+    standard:
+      - stdc
+    return_type: size_t
+    arguments:
+      - type: char *__restrict
+      - type: size_t
+      - type: const char *__restrict
+      - type: const struct tm *__restrict
+  - name: strftime_l
+    standard:
+      - stdc
+    return_type: size_t
+    arguments:
+      - type: char *__restrict
+      - type: size_t
+      - type: const char *__restrict
+      - type: const struct tm *__restrict
+      - type: locale_t
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 1742e1f7b0ef33..ef8e28331f33ca 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -1584,6 +1584,7 @@ def StdC : StandardSpec<"stdc"> {
          StructTimeSpec,
          TimeTType,
          SizeTType,
+         LocaleT,
       ],
       [], // Enumerations
       [
@@ -1636,6 +1637,26 @@ def StdC : StandardSpec<"stdc"> {
               RetValSpec<TimeTType>,
               [ArgSpec<TimeTTypePtr>]
           >,
+          FunctionSpec<
+              "strftime",
+              RetValSpec<SizeTType>,
+              [
+                  ArgSpec<CharRestrictedPtr>,
+                  ArgSpec<SizeTType>,
+                  ArgSpec<ConstCharRestrictedPtr>,
+                  ArgSpec<StructTmPtr>
+              ]
+          FunctionSpec<
+              "strftime_l",
+              RetValSpec<SizeTType>,
+              [
+                  ArgSpec<CharRestrictedPtr>,
+                  ArgSpec<SizeTType>,
+                  ArgSpec<ConstCharRestrictedPtr>,
+                  ArgSpec<StructTmPtr>,
+                  ArgSpec<LocaleT>
+              ]
+          >,
       ]
   >;
 
diff --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt
index 5680718715974e..a7b332c65b8583 100644
--- a/libc/src/time/CMakeLists.txt
+++ b/libc/src/time/CMakeLists.txt
@@ -75,11 +75,32 @@ add_entrypoint_object(
   HDRS
     mktime.h
   DEPENDS
-    .time_utils
     libc.include.time
     libc.src.errno.errno
 )
 
+
+add_entrypoint_object(
+  strftime
+  SRCS
+    strftime.cpp
+  HDRS
+    strftime.h
+  DEPENDS
+    libc.include.time
+)
+
+add_entrypoint_object(
+  strftime_l
+  SRCS
+    strftime_l.cpp
+  HDRS
+    strftime_l.h
+  DEPENDS
+    libc.include.time
+    libc.include.locale
+)
+
 add_entrypoint_object(
   time
   ALIAS
diff --git a/libc/src/time/strftime.cpp b/libc/src/time/strftime.cpp
new file mode 100644
index 00000000000000..4cc647035e9cf2
--- /dev/null
+++ b/libc/src/time/strftime.cpp
@@ -0,0 +1,25 @@
+//===-- Implementation of strftime function -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/time/strftime.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/time/time_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+using LIBC_NAMESPACE::time_utils::TimeConstants;
+
+LLVM_LIBC_FUNCTION(size_t, strftime,
+                   (char *__restrict, size_t, const char *__restrict,
+                    const struct tm *)) {
+  // TODO: Implement this for the default locale.
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/strftime.h b/libc/src/time/strftime.h
new file mode 100644
index 00000000000000..aa30336d8957d3
--- /dev/null
+++ b/libc/src/time/strftime.h
@@ -0,0 +1,23 @@
+//===-- Implementation header of strftime -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_TIME_STRFTIME_H
+#define LLVM_LIBC_SRC_TIME_STRFTIME_H
+
+#include "src/__support/macros/config.h"
+#include <stddef.h>
+#include <time.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+size_t strftime(char *__restrict, size_t max, const char *__restrict format,
+                const struct tm *timeptr);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_TIME_STRFTIME_H
diff --git a/libc/src/time/strftime_l.cpp b/libc/src/time/strftime_l.cpp
new file mode 100644
index 00000000000000..13ae023bb976cb
--- /dev/null
+++ b/libc/src/time/strftime_l.cpp
@@ -0,0 +1,23 @@
+//===-- Implementation of strftime_l function -----------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/time/strftime_l.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/time/time_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(size_t, strftime_l,
+                   (char *__restrict, size_t, const char *__restrict,
+                    const struct tm *, locale_t)) {
+  // TODO: Implement this for the default locale.
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/strftime_l.h b/libc/src/time/strftime_l.h
new file mode 100644
index 00000000000000..883874ec2162e4
--- /dev/null
+++ b/libc/src/time/strftime_l.h
@@ -0,0 +1,24 @@
+//===-- Implementation header of strftime_l ---------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_TIME_STRFTIME_L_H
+#define LLVM_LIBC_SRC_TIME_STRFTIME_L_H
+
+#include "include/llvm-libc-types/locale_t.h"
+#include "src/__support/macros/config.h"
+#include <stddef.h>
+#include <time.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+size_t strftime_l(char *__restrict, size_t max, const char *__restrict format,
+                  const struct tm *timeptr, locale_t locale);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_TIME_STRFTIME_L_H

>From 81f15a8f2da205cd134ec023db417741f2176615 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Fri, 30 Aug 2024 19:37:36 -0500
Subject: [PATCH 2/2] Update

---
 libc/src/time/strftime.cpp   | 3 ++-
 libc/src/time/strftime_l.cpp | 3 ++-
 libc/src/time/strftime_l.h   | 2 +-
 3 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/libc/src/time/strftime.cpp b/libc/src/time/strftime.cpp
index 4cc647035e9cf2..4fa8a7fc3ec7df 100644
--- a/libc/src/time/strftime.cpp
+++ b/libc/src/time/strftime.cpp
@@ -9,6 +9,7 @@
 #include "src/time/strftime.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/errno/libc_errno.h"
 #include "src/time/time_utils.h"
 
 namespace LIBC_NAMESPACE_DECL {
@@ -19,7 +20,7 @@ LLVM_LIBC_FUNCTION(size_t, strftime,
                    (char *__restrict, size_t, const char *__restrict,
                     const struct tm *)) {
   // TODO: Implement this for the default locale.
-  return 0;
+  return -1;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/strftime_l.cpp b/libc/src/time/strftime_l.cpp
index 13ae023bb976cb..c25103b58e8363 100644
--- a/libc/src/time/strftime_l.cpp
+++ b/libc/src/time/strftime_l.cpp
@@ -9,6 +9,7 @@
 #include "src/time/strftime_l.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/errno/libc_errno.h"
 #include "src/time/time_utils.h"
 
 namespace LIBC_NAMESPACE_DECL {
@@ -17,7 +18,7 @@ LLVM_LIBC_FUNCTION(size_t, strftime_l,
                    (char *__restrict, size_t, const char *__restrict,
                     const struct tm *, locale_t)) {
   // TODO: Implement this for the default locale.
-  return 0;
+  return -1;
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/strftime_l.h b/libc/src/time/strftime_l.h
index 883874ec2162e4..68c48007e6ad80 100644
--- a/libc/src/time/strftime_l.h
+++ b/libc/src/time/strftime_l.h
@@ -9,7 +9,7 @@
 #ifndef LLVM_LIBC_SRC_TIME_STRFTIME_L_H
 #define LLVM_LIBC_SRC_TIME_STRFTIME_L_H
 
-#include "include/llvm-libc-types/locale_t.h"
+#include "hdr/types/locale_t.h"
 #include "src/__support/macros/config.h"
 #include <stddef.h>
 #include <time.h>



More information about the libc-commits mailing list