Skip to content

Commit 0353252

Browse files
[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
1 parent dc94761 commit 0353252

19 files changed

+235
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ set(TARGET_LIBC_ENTRYPOINTS
4545
libc.src.string.strtok
4646
libc.src.string.strtok_r
4747

48+
# inttypes.h entrypoints
49+
libc.src.inttypes.strtoimax
50+
libc.src.inttypes.strtoumax
51+
4852
# stdlib.h entrypoints
4953
libc.src.stdlib.atoi
5054
libc.src.stdlib.atol

libc/config/linux/aarch64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ set(TARGET_PUBLIC_HEADERS
22
libc.include.ctype
33
libc.include.errno
44
libc.include.fenv
5+
libc.include.inttypes
56
libc.include.math
67
libc.include.stdlib
78
libc.include.string

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ set(TARGET_LIBC_ENTRYPOINTS
4545
libc.src.string.strtok
4646
libc.src.string.strtok_r
4747

48+
# inttypes.h entrypoints
49+
libc.src.inttypes.strtoimax
50+
libc.src.inttypes.strtoumax
51+
4852
# stdlib.h entrypoints
4953
libc.src.stdlib.atoi
5054
libc.src.stdlib.atol

libc/config/linux/x86_64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ set(TARGET_PUBLIC_HEADERS
22
libc.include.assert_h
33
libc.include.ctype
44
libc.include.errno
5+
libc.include.inttypes
56
libc.include.math
67
libc.include.signal
78
libc.include.stdio

libc/config/windows/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ set(TARGET_LIBC_ENTRYPOINTS
4545
libc.src.string.strtok
4646
libc.src.string.strtok_r
4747

48+
# inttypes.h entrypoints
49+
libc.src.inttypes.strtoimax
50+
libc.src.inttypes.strtoumax
51+
4852
# stdlib.h entrypoints
4953
libc.src.stdlib.atoi
5054
libc.src.stdlib.atol

libc/include/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ add_gen_header(
3333
.llvm_libc_common_h
3434
)
3535

36+
add_gen_header(
37+
inttypes
38+
DEF_FILE inttypes.h.def
39+
GEN_HDR inttypes.h
40+
DEPENDS
41+
.llvm_libc_common_h
42+
)
43+
3644
add_gen_header(
3745
math
3846
DEF_FILE math.h.def

libc/include/inttypes.h.def

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- C standard library header inttypes.h ------------------------------===//
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_INTTYPES_H
10+
#define LLVM_LIBC_INTTYPES_H
11+
12+
#include <__llvm-libc-common.h>
13+
#include <stdint.h>
14+
15+
%%public_api()
16+
17+
#endif // LLVM_LIBC_INTTYPES_H

libc/spec/spec.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def ConstVoidPtr : ConstType<VoidPtr>;
5454
def SizeTType : NamedType<"size_t">;
5555
def LongDoublePtr : PtrType<LongDoubleType>;
5656

57+
def IntMaxTType : NamedType<"intmax_t">;
58+
def UIntMaxTType : NamedType<"uintmax_t">;
59+
5760
// _Noreturn is really not a type, but it is convenient to treat it as a type.
5861
def NoReturn : NamedType<"_Noreturn void">;
5962

libc/spec/stdc.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,19 @@ def StdC : StandardSpec<"stdc"> {
489489
]
490490
>;
491491

492+
HeaderSpec IntTypes = HeaderSpec<
493+
"inttypes.h",
494+
[], // Macros
495+
[], // Types
496+
[], // Enumerations
497+
[
498+
FunctionSpec<"imaxabs", RetValSpec<IntMaxTType>, [ArgSpec<IntMaxTType>]>,
499+
FunctionSpec<"imaxdiv", RetValSpec<IntMaxTType>, [ArgSpec<IntMaxTType>, ArgSpec<IntMaxTType>]>,
500+
FunctionSpec<"strtoimax", RetValSpec<IntMaxTType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>, ArgSpec<IntType>]>,
501+
FunctionSpec<"strtoumax", RetValSpec<UIntMaxTType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>, ArgSpec<IntType>]>,
502+
]
503+
>;
504+
492505
HeaderSpec Errno = HeaderSpec<
493506
"errno.h",
494507
[
@@ -653,6 +666,7 @@ def StdC : StandardSpec<"stdc"> {
653666
String,
654667
StdIO,
655668
StdLib,
669+
IntTypes,
656670
Signal,
657671
Threads,
658672
Time,

libc/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_subdirectory(__support)
33
add_subdirectory(ctype)
44
add_subdirectory(errno)
55
add_subdirectory(fenv)
6+
add_subdirectory(inttypes)
67
add_subdirectory(math)
78
add_subdirectory(string)
89
add_subdirectory(stdlib)

libc/src/inttypes/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
add_entrypoint_object(
2+
strtoimax
3+
SRCS
4+
strtoimax.cpp
5+
HDRS
6+
strtoimax.h
7+
DEPENDS
8+
libc.src.__support.str_conv_utils
9+
)
10+
11+
add_entrypoint_object(
12+
strtoumax
13+
SRCS
14+
strtoumax.cpp
15+
HDRS
16+
strtoumax.h
17+
DEPENDS
18+
libc.src.__support.str_conv_utils
19+
)

libc/src/inttypes/strtoimax.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of strtoimax ---------------------------------------===//
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/inttypes/strtoimax.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/str_conv_utils.h"
12+
13+
namespace __llvm_libc {
14+
15+
LLVM_LIBC_FUNCTION(intmax_t, strtoimax,
16+
(const char *__restrict str, char **__restrict str_end,
17+
int base)) {
18+
return internal::strtointeger<intmax_t>(str, str_end, base);
19+
}
20+
21+
} // namespace __llvm_libc

libc/src/inttypes/strtoimax.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for strtoimax ---------------------*- C++ -*-===//
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_SRC_INTTYPES_STRTOIMAX_H
10+
#define LLVM_LIBC_SRC_INTTYPES_STRTOIMAX_H
11+
12+
#include <stdint.h>
13+
14+
namespace __llvm_libc {
15+
16+
intmax_t strtoimax(const char *__restrict str, char **__restrict str_end,
17+
int base);
18+
19+
} // namespace __llvm_libc
20+
21+
#endif // LLVM_LIBC_SRC_INTTYPES_STRTOIMAX_H

libc/src/inttypes/strtoumax.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of strtoumax ---------------------------------------===//
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/inttypes/strtoumax.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/str_conv_utils.h"
12+
13+
namespace __llvm_libc {
14+
15+
LLVM_LIBC_FUNCTION(uintmax_t, strtoumax,
16+
(const char *__restrict str, char **__restrict str_end,
17+
int base)) {
18+
return internal::strtointeger<uintmax_t>(str, str_end, base);
19+
}
20+
21+
} // namespace __llvm_libc

libc/src/inttypes/strtoumax.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for strtoumax ---------------------*- C++ -*-===//
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_SRC_INTTYPES_STRTOUMAX_H
10+
#define LLVM_LIBC_SRC_INTTYPES_STRTOUMAX_H
11+
12+
#include <stdint.h>
13+
14+
namespace __llvm_libc {
15+
16+
uintmax_t strtoumax(const char *__restrict str, char **__restrict str_end,
17+
int base);
18+
19+
} // namespace __llvm_libc
20+
21+
#endif // LLVM_LIBC_SRC_INTTYPES_STRTOUMAX_H

libc/test/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_subdirectory(__support)
2929
add_subdirectory(ctype)
3030
add_subdirectory(errno)
3131
add_subdirectory(fenv)
32+
add_subdirectory(inttypes)
3233
add_subdirectory(math)
3334
add_subdirectory(string)
3435
add_subdirectory(stdlib)

libc/test/src/inttypes/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
add_libc_testsuite(libc_inttypes_unittests)
2+
3+
add_libc_unittest(
4+
strtoimax_test
5+
SUITE
6+
libc_inttypes_unittests
7+
SRCS
8+
strtoimax_test.cpp
9+
DEPENDS
10+
libc.src.inttypes.strtoimax
11+
)
12+
13+
add_libc_unittest(
14+
strtoumax_test
15+
SUITE
16+
libc_inttypes_unittests
17+
SRCS
18+
strtoumax_test.cpp
19+
DEPENDS
20+
libc.src.inttypes.strtoumax
21+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Unittests for strtoimax -------------------------------------------===//
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/inttypes/strtoimax.h"
10+
11+
#include "utils/UnitTest/Test.h"
12+
13+
#include <errno.h>
14+
#include <limits.h>
15+
#include <stddef.h>
16+
17+
// strtoimax is equivalent to strtoll on all currently supported configurations.
18+
// Thus to avoid duplicating code there is just one test to make sure that
19+
// strtoimax works at all. For real tests see stdlib/strtoll_test.cpp.
20+
21+
TEST(LlvmLibcStrToIMaxTest, SimpleCheck) {
22+
const char *ten = "10";
23+
errno = 0;
24+
ASSERT_EQ(__llvm_libc::strtoimax(ten, nullptr, 10), intmax_t(10));
25+
ASSERT_EQ(errno, 0);
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Unittests for strtoumax -------------------------------------------===//
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/inttypes/strtoumax.h"
10+
11+
#include "utils/UnitTest/Test.h"
12+
13+
#include <errno.h>
14+
#include <limits.h>
15+
#include <stddef.h>
16+
17+
// strtoumax is equivalent to strtoull on all currently supported
18+
// configurations. Thus to avoid duplicating code there is just one test to make
19+
// sure that strtoumax works at all. For real tests see
20+
// stdlib/strtoull_test.cpp.
21+
22+
TEST(LlvmLibcStrToUMaxTest, SimpleCheck) {
23+
const char *ten = "10";
24+
errno = 0;
25+
ASSERT_EQ(__llvm_libc::strtoumax(ten, nullptr, 10), uintmax_t(10));
26+
ASSERT_EQ(errno, 0);
27+
}

0 commit comments

Comments
 (0)