[libc-commits] [libc] 0353252 - [libc] add inttypes header

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Aug 26 11:04:27 PDT 2021


Author: Michael Jones
Date: 2021-08-26T18:04:21Z
New Revision: 035325275c79e19414b45eca9d2fbd6ab8e25209

URL: https://github.com/llvm/llvm-project/commit/035325275c79e19414b45eca9d2fbd6ab8e25209
DIFF: https://github.com/llvm/llvm-project/commit/035325275c79e19414b45eca9d2fbd6ab8e25209.diff

LOG: [libc] add inttypes header

Add inttypes.h to llvm libc. As its first functions strtoimax and
strtoumax are included.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D108736

Added: 
    libc/include/inttypes.h.def
    libc/src/inttypes/CMakeLists.txt
    libc/src/inttypes/strtoimax.cpp
    libc/src/inttypes/strtoimax.h
    libc/src/inttypes/strtoumax.cpp
    libc/src/inttypes/strtoumax.h
    libc/test/src/inttypes/CMakeLists.txt
    libc/test/src/inttypes/strtoimax_test.cpp
    libc/test/src/inttypes/strtoumax_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/aarch64/headers.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/config/linux/x86_64/headers.txt
    libc/config/windows/entrypoints.txt
    libc/include/CMakeLists.txt
    libc/spec/spec.td
    libc/spec/stdc.td
    libc/src/CMakeLists.txt
    libc/test/src/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index cbdb9d92b9aa0..f8347678038a9 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -45,6 +45,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.string.strtok
     libc.src.string.strtok_r
 
+    # inttypes.h entrypoints
+    libc.src.inttypes.strtoimax
+    libc.src.inttypes.strtoumax
+
     # stdlib.h entrypoints
     libc.src.stdlib.atoi
     libc.src.stdlib.atol

diff  --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index eb9dfa2ab25a2..1cf5be227aa28 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -2,6 +2,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.ctype
     libc.include.errno
     libc.include.fenv
+    libc.include.inttypes
     libc.include.math
     libc.include.stdlib
     libc.include.string

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 3640271177eac..3a15068682aa7 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -45,6 +45,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.string.strtok
     libc.src.string.strtok_r
 
+    # inttypes.h entrypoints
+    libc.src.inttypes.strtoimax
+    libc.src.inttypes.strtoumax
+
     # stdlib.h entrypoints
     libc.src.stdlib.atoi
     libc.src.stdlib.atol

diff  --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index 2d4cdb11cb8ac..7baf61c91bf08 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -2,6 +2,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.assert_h
     libc.include.ctype
     libc.include.errno
+    libc.include.inttypes
     libc.include.math
     libc.include.signal
     libc.include.stdio

diff  --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 9194d3dce1c7e..995d7d9af8101 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -45,6 +45,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.string.strtok
     libc.src.string.strtok_r
 
+    # inttypes.h entrypoints
+    libc.src.inttypes.strtoimax
+    libc.src.inttypes.strtoumax
+
     # stdlib.h entrypoints
     libc.src.stdlib.atoi
     libc.src.stdlib.atol

diff  --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index d11dd464c10b1..d6101edac8468 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -33,6 +33,14 @@ add_gen_header(
     .llvm_libc_common_h
 )
 
+add_gen_header(
+  inttypes
+  DEF_FILE inttypes.h.def
+  GEN_HDR inttypes.h
+  DEPENDS
+    .llvm_libc_common_h
+)
+
 add_gen_header(
   math
   DEF_FILE math.h.def

diff  --git a/libc/include/inttypes.h.def b/libc/include/inttypes.h.def
new file mode 100644
index 0000000000000..94cdc2f21eaf7
--- /dev/null
+++ b/libc/include/inttypes.h.def
@@ -0,0 +1,17 @@
+//===-- C standard library header inttypes.h ------------------------------===//
+//
+// 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_INTTYPES_H
+#define LLVM_LIBC_INTTYPES_H
+
+#include <__llvm-libc-common.h>
+#include <stdint.h>
+
+%%public_api()
+
+#endif // LLVM_LIBC_INTTYPES_H

diff  --git a/libc/spec/spec.td b/libc/spec/spec.td
index 242303de8e3b5..55f75f28df3ac 100644
--- a/libc/spec/spec.td
+++ b/libc/spec/spec.td
@@ -54,6 +54,9 @@ def ConstVoidPtr : ConstType<VoidPtr>;
 def SizeTType : NamedType<"size_t">;
 def LongDoublePtr : PtrType<LongDoubleType>;
 
+def IntMaxTType : NamedType<"intmax_t">;
+def UIntMaxTType : NamedType<"uintmax_t">;
+
 // _Noreturn is really not a type, but it is convenient to treat it as a type.
 def NoReturn : NamedType<"_Noreturn void">;
 

diff  --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index dadf90e34f7e3..da169a53c2773 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -489,6 +489,19 @@ def StdC : StandardSpec<"stdc"> {
       ]
   >;
 
+  HeaderSpec IntTypes = HeaderSpec<
+      "inttypes.h",
+      [], // Macros
+      [], // Types
+      [], // Enumerations
+      [
+          FunctionSpec<"imaxabs", RetValSpec<IntMaxTType>, [ArgSpec<IntMaxTType>]>,
+          FunctionSpec<"imaxdiv", RetValSpec<IntMaxTType>, [ArgSpec<IntMaxTType>, ArgSpec<IntMaxTType>]>,
+          FunctionSpec<"strtoimax", RetValSpec<IntMaxTType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>, ArgSpec<IntType>]>,
+          FunctionSpec<"strtoumax", RetValSpec<UIntMaxTType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>, ArgSpec<IntType>]>,
+      ]
+  >;
+
   HeaderSpec Errno = HeaderSpec<
       "errno.h",
       [
@@ -653,6 +666,7 @@ def StdC : StandardSpec<"stdc"> {
     String,
     StdIO,
     StdLib,
+    IntTypes,
     Signal,
     Threads,
     Time,

diff  --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index a02c310e3ae70..f13bcc669c59e 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -3,6 +3,7 @@ add_subdirectory(__support)
 add_subdirectory(ctype)
 add_subdirectory(errno)
 add_subdirectory(fenv)
+add_subdirectory(inttypes)
 add_subdirectory(math)
 add_subdirectory(string)
 add_subdirectory(stdlib)

diff  --git a/libc/src/inttypes/CMakeLists.txt b/libc/src/inttypes/CMakeLists.txt
new file mode 100644
index 0000000000000..65c1d7e2063ab
--- /dev/null
+++ b/libc/src/inttypes/CMakeLists.txt
@@ -0,0 +1,19 @@
+add_entrypoint_object(
+  strtoimax
+  SRCS
+    strtoimax.cpp
+  HDRS
+    strtoimax.h
+  DEPENDS
+    libc.src.__support.str_conv_utils
+)
+
+add_entrypoint_object(
+  strtoumax
+  SRCS
+    strtoumax.cpp
+  HDRS
+    strtoumax.h
+  DEPENDS
+    libc.src.__support.str_conv_utils
+)

diff  --git a/libc/src/inttypes/strtoimax.cpp b/libc/src/inttypes/strtoimax.cpp
new file mode 100644
index 0000000000000..ecd59ff717383
--- /dev/null
+++ b/libc/src/inttypes/strtoimax.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of strtoimax ---------------------------------------===//
+//
+// 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/inttypes/strtoimax.h"
+#include "src/__support/common.h"
+#include "src/__support/str_conv_utils.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(intmax_t, strtoimax,
+                   (const char *__restrict str, char **__restrict str_end,
+                    int base)) {
+  return internal::strtointeger<intmax_t>(str, str_end, base);
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/inttypes/strtoimax.h b/libc/src/inttypes/strtoimax.h
new file mode 100644
index 0000000000000..c2097e33616b4
--- /dev/null
+++ b/libc/src/inttypes/strtoimax.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for strtoimax ---------------------*- 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_INTTYPES_STRTOIMAX_H
+#define LLVM_LIBC_SRC_INTTYPES_STRTOIMAX_H
+
+#include <stdint.h>
+
+namespace __llvm_libc {
+
+intmax_t strtoimax(const char *__restrict str, char **__restrict str_end,
+                   int base);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_INTTYPES_STRTOIMAX_H

diff  --git a/libc/src/inttypes/strtoumax.cpp b/libc/src/inttypes/strtoumax.cpp
new file mode 100644
index 0000000000000..cc3f1451fdae3
--- /dev/null
+++ b/libc/src/inttypes/strtoumax.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of strtoumax ---------------------------------------===//
+//
+// 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/inttypes/strtoumax.h"
+#include "src/__support/common.h"
+#include "src/__support/str_conv_utils.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(uintmax_t, strtoumax,
+                   (const char *__restrict str, char **__restrict str_end,
+                    int base)) {
+  return internal::strtointeger<uintmax_t>(str, str_end, base);
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/inttypes/strtoumax.h b/libc/src/inttypes/strtoumax.h
new file mode 100644
index 0000000000000..e3ec13a53b89c
--- /dev/null
+++ b/libc/src/inttypes/strtoumax.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for strtoumax ---------------------*- 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_INTTYPES_STRTOUMAX_H
+#define LLVM_LIBC_SRC_INTTYPES_STRTOUMAX_H
+
+#include <stdint.h>
+
+namespace __llvm_libc {
+
+uintmax_t strtoumax(const char *__restrict str, char **__restrict str_end,
+                    int base);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_INTTYPES_STRTOUMAX_H

diff  --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt
index 04235dabfe075..790bb857a4e61 100644
--- a/libc/test/src/CMakeLists.txt
+++ b/libc/test/src/CMakeLists.txt
@@ -29,6 +29,7 @@ add_subdirectory(__support)
 add_subdirectory(ctype)
 add_subdirectory(errno)
 add_subdirectory(fenv)
+add_subdirectory(inttypes)
 add_subdirectory(math)
 add_subdirectory(string)
 add_subdirectory(stdlib)

diff  --git a/libc/test/src/inttypes/CMakeLists.txt b/libc/test/src/inttypes/CMakeLists.txt
new file mode 100644
index 0000000000000..d9885ba53eedb
--- /dev/null
+++ b/libc/test/src/inttypes/CMakeLists.txt
@@ -0,0 +1,21 @@
+add_libc_testsuite(libc_inttypes_unittests)
+
+add_libc_unittest(
+  strtoimax_test
+  SUITE
+    libc_inttypes_unittests
+  SRCS
+    strtoimax_test.cpp
+  DEPENDS
+    libc.src.inttypes.strtoimax
+)
+
+add_libc_unittest(
+  strtoumax_test
+  SUITE
+    libc_inttypes_unittests
+  SRCS
+    strtoumax_test.cpp
+  DEPENDS
+    libc.src.inttypes.strtoumax
+)

diff  --git a/libc/test/src/inttypes/strtoimax_test.cpp b/libc/test/src/inttypes/strtoimax_test.cpp
new file mode 100644
index 0000000000000..758d3a46348e2
--- /dev/null
+++ b/libc/test/src/inttypes/strtoimax_test.cpp
@@ -0,0 +1,26 @@
+//===-- Unittests for strtoimax -------------------------------------------===//
+//
+// 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/inttypes/strtoimax.h"
+
+#include "utils/UnitTest/Test.h"
+
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+
+// strtoimax is equivalent to strtoll on all currently supported configurations.
+// Thus to avoid duplicating code there is just one test to make sure that
+// strtoimax works at all. For real tests see stdlib/strtoll_test.cpp.
+
+TEST(LlvmLibcStrToIMaxTest, SimpleCheck) {
+  const char *ten = "10";
+  errno = 0;
+  ASSERT_EQ(__llvm_libc::strtoimax(ten, nullptr, 10), intmax_t(10));
+  ASSERT_EQ(errno, 0);
+}

diff  --git a/libc/test/src/inttypes/strtoumax_test.cpp b/libc/test/src/inttypes/strtoumax_test.cpp
new file mode 100644
index 0000000000000..2ff79783d31bf
--- /dev/null
+++ b/libc/test/src/inttypes/strtoumax_test.cpp
@@ -0,0 +1,27 @@
+//===-- Unittests for strtoumax -------------------------------------------===//
+//
+// 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/inttypes/strtoumax.h"
+
+#include "utils/UnitTest/Test.h"
+
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+
+// strtoumax is equivalent to strtoull on all currently supported
+// configurations. Thus to avoid duplicating code there is just one test to make
+// sure that strtoumax works at all. For real tests see
+// stdlib/strtoull_test.cpp.
+
+TEST(LlvmLibcStrToUMaxTest, SimpleCheck) {
+  const char *ten = "10";
+  errno = 0;
+  ASSERT_EQ(__llvm_libc::strtoumax(ten, nullptr, 10), uintmax_t(10));
+  ASSERT_EQ(errno, 0);
+}


        


More information about the libc-commits mailing list