[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
This commit is contained in:
Michael Jones 2021-08-25 21:11:31 +00:00
parent dc94761f3b
commit 035325275c
19 changed files with 235 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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">;

View File

@ -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,

View File

@ -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)

View File

@ -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
)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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
)

View File

@ -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);
}

View File

@ -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);
}