Skip to content

Commit a871051

Browse files
authored
[libc] Implement locale variants for 'stdlib.h' functions (#105718)
Summary: This provides the `_l` variants for the `stdlib.h` functions. These are just copies of the same entrypoint and don't do anything with the locale information.
1 parent a0441ce commit a871051

21 files changed

+532
-0
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,19 @@ set(TARGET_LIBC_ENTRYPOINTS
173173
libc.src.stdlib.rand
174174
libc.src.stdlib.srand
175175
libc.src.stdlib.strtod
176+
libc.src.stdlib.strtod_l
176177
libc.src.stdlib.strtof
178+
libc.src.stdlib.strtof_l
177179
libc.src.stdlib.strtol
180+
libc.src.stdlib.strtol_l
178181
libc.src.stdlib.strtold
182+
libc.src.stdlib.strtold_l
179183
libc.src.stdlib.strtoll
184+
libc.src.stdlib.strtoll_l
180185
libc.src.stdlib.strtoul
186+
libc.src.stdlib.strtoul_l
181187
libc.src.stdlib.strtoull
188+
libc.src.stdlib.strtoull_l
182189
libc.src.stdlib.at_quick_exit
183190
libc.src.stdlib.quick_exit
184191
libc.src.stdlib.getenv

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,15 @@ if(LLVM_LIBC_FULL_BUILD)
800800
libc.src.ctype.tolower_l
801801
libc.src.ctype.toupper_l
802802

803+
# stdlib.h entrypoints
804+
libc.src.stdlib.strtod_l
805+
libc.src.stdlib.strtof_l
806+
libc.src.stdlib.strtol_l
807+
libc.src.stdlib.strtold_l
808+
libc.src.stdlib.strtoll_l
809+
libc.src.stdlib.strtoul_l
810+
libc.src.stdlib.strtoull_l
811+
803812
# assert.h entrypoints
804813
libc.src.assert.__assert_fail
805814

libc/include/llvm-libc-macros/stdlib-macros.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
#define EXIT_SUCCESS 0
1818
#define EXIT_FAILURE 1
1919

20+
#ifndef MB_CUR_MAX
21+
// We only support the "C" locale right now, so this is a constant byte.
22+
#define MB_CUR_MAX 1
23+
#endif // MB_CUR_MAX
24+
2025
#define RAND_MAX 2147483647
2126

2227
#endif // LLVM_LIBC_MACROS_STDLIB_MACROS_H

libc/include/stdlib.h.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_LIBC_STDLIB_H
1111

1212
#include "__llvm-libc-common.h"
13+
#include "llvm-libc-types/locale_t.h"
1314
#include "llvm-libc-macros/stdlib-macros.h"
1415

1516
%%public_api()

libc/newhdrgen/yaml/stdlib.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,63 @@ functions:
273273
- type: const char *__restrict
274274
- type: char **__restrict
275275
- type: int
276+
- name: strtod_l
277+
standards:
278+
- stdc
279+
return_type: double
280+
arguments:
281+
- type: const char *__restrict
282+
- type: char **__restrict
283+
- type: locale_t
284+
- name: strtof_l
285+
standards:
286+
- stdc
287+
return_type: float
288+
arguments:
289+
- type: const char *__restrict
290+
- type: char **__restrict
291+
- type: locale_t
292+
- name: strtol_l
293+
standards:
294+
- stdc
295+
return_type: long
296+
arguments:
297+
- type: const char *__restrict
298+
- type: char **__restrict
299+
- type: int
300+
- type: locale_t
301+
- name: strtold_l
302+
standards:
303+
- stdc
304+
return_type: long double
305+
arguments:
306+
- type: const char *__restrict
307+
- type: char **__restrict
308+
- type: locale_t
309+
- name: strtoll_l
310+
standards:
311+
- stdc
312+
return_type: long long
313+
arguments:
314+
- type: const char *__restrict
315+
- type: char **__restrict
316+
- type: int
317+
- type: locale_t
318+
- name: strtoul_l
319+
standards:
320+
- stdc
321+
return_type: unsigned long
322+
arguments:
323+
- type: const char *__restrict
324+
- type: char **__restrict
325+
- type: int
326+
- type: locale_t
327+
- name: strtoull_l
328+
standards:
329+
- stdc
330+
return_type: unsigned long long
331+
arguments:
332+
- type: const char *__restrict
333+
- type: char **__restrict
334+
- type: int
335+
- type: locale_t

libc/spec/stdc.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,14 @@ def StdC : StandardSpec<"stdc"> {
13081308
FunctionSpec<"strtoul", RetValSpec<UnsignedLongType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>, ArgSpec<IntType>]>,
13091309
FunctionSpec<"strtoull", RetValSpec<UnsignedLongLongType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>, ArgSpec<IntType>]>,
13101310

1311+
FunctionSpec<"strtof", RetValSpec<FloatType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>, ArgSpec<LocaleT>]>,
1312+
FunctionSpec<"strtod", RetValSpec<DoubleType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>, ArgSpec<LocaleT>]>,
1313+
FunctionSpec<"strtold", RetValSpec<LongDoubleType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>, ArgSpec<LocaleT>]>,
1314+
FunctionSpec<"strtol", RetValSpec<LongType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>, ArgSpec<IntType>, ArgSpec<LocaleT>]>,
1315+
FunctionSpec<"strtoll", RetValSpec<LongLongType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>, ArgSpec<IntType>, ArgSpec<LocaleT>]>,
1316+
FunctionSpec<"strtoul", RetValSpec<UnsignedLongType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>, ArgSpec<IntType>, ArgSpec<LocaleT>]>,
1317+
FunctionSpec<"strtoull", RetValSpec<UnsignedLongLongType>, [ArgSpec<ConstCharRestrictedPtr>, ArgSpec<CharRestrictedPtrPtr>, ArgSpec<IntType>, ArgSpec<LocaleT>]>,
1318+
13111319
FunctionSpec<"malloc", RetValSpec<VoidPtr>, [ArgSpec<SizeTType>]>,
13121320
FunctionSpec<"calloc", RetValSpec<VoidPtr>, [ArgSpec<SizeTType>, ArgSpec<SizeTType>]>,
13131321
FunctionSpec<"realloc", RetValSpec<VoidPtr>, [ArgSpec<VoidPtr>, ArgSpec<SizeTType>]>,

libc/src/stdlib/CMakeLists.txt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,83 @@ if(NOT LLVM_LIBC_FULL_BUILD)
428428
return()
429429
endif()
430430

431+
add_entrypoint_object(
432+
strtof_l
433+
SRCS
434+
strtof_l.cpp
435+
HDRS
436+
strtof_l.h
437+
DEPENDS
438+
libc.src.errno.errno
439+
libc.src.__support.str_to_float
440+
)
441+
442+
add_entrypoint_object(
443+
strtod_l
444+
SRCS
445+
strtod_l.cpp
446+
HDRS
447+
strtod_l.h
448+
DEPENDS
449+
libc.src.errno.errno
450+
libc.src.__support.str_to_float
451+
)
452+
453+
add_entrypoint_object(
454+
strtold_l
455+
SRCS
456+
strtold_l.cpp
457+
HDRS
458+
strtold_l.h
459+
DEPENDS
460+
libc.src.errno.errno
461+
libc.src.__support.str_to_float
462+
)
463+
464+
add_entrypoint_object(
465+
strtol_l
466+
SRCS
467+
strtol_l.cpp
468+
HDRS
469+
strtol_l.h
470+
DEPENDS
471+
libc.src.errno.errno
472+
libc.src.__support.str_to_integer
473+
)
474+
475+
add_entrypoint_object(
476+
strtoll_l
477+
SRCS
478+
strtoll_l.cpp
479+
HDRS
480+
strtoll_l.h
481+
DEPENDS
482+
libc.src.errno.errno
483+
libc.src.__support.str_to_integer
484+
)
485+
486+
add_entrypoint_object(
487+
strtoul_l
488+
SRCS
489+
strtoul_l.cpp
490+
HDRS
491+
strtoul_l.h
492+
DEPENDS
493+
libc.src.errno.errno
494+
libc.src.__support.str_to_integer
495+
)
496+
497+
add_entrypoint_object(
498+
strtoull_l
499+
SRCS
500+
strtoull_l.cpp
501+
HDRS
502+
strtoull_l.h
503+
DEPENDS
504+
libc.src.errno.errno
505+
libc.src.__support.str_to_integer
506+
)
507+
431508
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
432509
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
433510
endif()

libc/src/stdlib/strtod_l.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- Implementation of strtod_l ----------------------------------------===//
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/stdlib/strtod_l.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/macros/config.h"
12+
#include "src/__support/str_to_float.h"
13+
#include "src/errno/libc_errno.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(double, strtod_l,
18+
(const char *__restrict str, char **__restrict str_end,
19+
locale_t)) {
20+
auto result = internal::strtofloatingpoint<double>(str);
21+
if (result.has_error())
22+
libc_errno = result.error;
23+
24+
if (str_end != nullptr)
25+
*str_end = const_cast<char *>(str + result.parsed_len);
26+
27+
return result.value;
28+
}
29+
30+
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdlib/strtod_l.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation header for strtod_l ----------------------*- 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_STDLIB_STRTOD_L_H
10+
#define LLVM_LIBC_SRC_STDLIB_STRTOD_L_H
11+
12+
#include "include/llvm-libc-types/locale_t.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
double strtod_l(const char *__restrict str, char **__restrict str_end,
18+
locale_t locale);
19+
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC_STDLIB_STRTOD_L_H

libc/src/stdlib/strtof_l.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- Implementation of strtof_l ----------------------------------------===//
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/stdlib/strtof_l.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/macros/config.h"
12+
#include "src/__support/str_to_float.h"
13+
#include "src/errno/libc_errno.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(float, strtof_l,
18+
(const char *__restrict str, char **__restrict str_end,
19+
locale_t)) {
20+
auto result = internal::strtofloatingpoint<float>(str);
21+
if (result.has_error())
22+
libc_errno = result.error;
23+
24+
if (str_end != nullptr)
25+
*str_end = const_cast<char *>(str + result.parsed_len);
26+
27+
return result.value;
28+
}
29+
30+
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdlib/strtof_l.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation header for strtof_l ----------------------*- 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_STDLIB_STRTOF_L_H
10+
#define LLVM_LIBC_SRC_STDLIB_STRTOF_L_H
11+
12+
#include "include/llvm-libc-types/locale_t.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
float strtof_l(const char *__restrict str, char **__restrict str_end,
18+
locale_t locale);
19+
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC_STDLIB_STRTOF_L_H

libc/src/stdlib/strtol_l.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- Implementation of strtol_l ----------------------------------------===//
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/stdlib/strtol_l.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/macros/config.h"
12+
#include "src/__support/str_to_integer.h"
13+
#include "src/errno/libc_errno.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(long, strtol_l,
18+
(const char *__restrict str, char **__restrict str_end,
19+
int base, locale_t)) {
20+
auto result = internal::strtointeger<long>(str, base);
21+
if (result.has_error())
22+
libc_errno = result.error;
23+
24+
if (str_end != nullptr)
25+
*str_end = const_cast<char *>(str + result.parsed_len);
26+
27+
return result;
28+
}
29+
30+
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdlib/strtol_l.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation header for strtol_l ----------------------*- 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_STDLIB_STRTOL_L_H
10+
#define LLVM_LIBC_SRC_STDLIB_STRTOL_L_H
11+
12+
#include "include/llvm-libc-types/locale_t.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
long strtol_l(const char *__restrict str, char **__restrict str_end, int base,
18+
locale_t locale);
19+
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC_STDLIB_STRTOL_L_H

0 commit comments

Comments
 (0)