Skip to content

Commit 7a3b09b

Browse files
committed
[libc] Initial support for 'locale.h' in the LLVM libc
Summary: This patch adds the macros and entrypoints associated with the `locale.h` entrypoints. These are mostly stubs, as we (for now and the forseeable future) only expect to support the C and maybe C.UTF-8 locales in the LLVM libc.
1 parent 0889809 commit 7a3b09b

34 files changed

+779
-0
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ set(TARGET_LIBC_ENTRYPOINTS
231231
# wchar.h entrypoints
232232
libc.src.wchar.wctob
233233

234+
# locale.h entrypoints
235+
libc.src.locale.localeconv
236+
libc.src.locale.duplocale
237+
libc.src.locale.freelocale
238+
libc.src.locale.localeconv
239+
libc.src.locale.newlocale
240+
libc.src.locale.setlocale
241+
libc.src.locale.uselocale
242+
234243
# gpu/rpc.h entrypoints
235244
libc.src.gpu.rpc_host_call
236245
)

libc/config/gpu/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(TARGET_PUBLIC_HEADERS
1616
libc.include.wchar
1717
libc.include.uchar
1818
libc.include.features
19+
libc.include.locale
1920

2021
# Header for RPC extensions
2122
libc.include.gpu_rpc

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,15 @@ if(LLVM_LIBC_FULL_BUILD)
981981
libc.src.time.nanosleep
982982
libc.src.time.time
983983

984+
# locale.h entrypoints
985+
libc.src.locale.localeconv
986+
libc.src.locale.duplocale
987+
libc.src.locale.freelocale
988+
libc.src.locale.localeconv
989+
libc.src.locale.newlocale
990+
libc.src.locale.setlocale
991+
libc.src.locale.uselocale
992+
984993
# unistd.h entrypoints
985994
libc.src.unistd.__llvm_libc_syscall
986995
libc.src.unistd._exit

libc/config/linux/x86_64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ set(TARGET_PUBLIC_HEADERS
3131
libc.include.unistd
3232
libc.include.wchar
3333
libc.include.uchar
34+
libc.include.locale
3435

3536
libc.include.arpa_inet
3637

libc/hdr/types/locale_t.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from locale_t.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_HDR_LOCALE_T_H
10+
#define LLVM_LIBC_HDR_LOCALE_T_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-types/locale_t.h"
15+
16+
#else // overlay mode
17+
18+
#error "type not available in overlay mode"
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_LOCALE_T_H

libc/include/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,18 @@ add_header_macro(
700700
.llvm-libc-types.wchar_t
701701
)
702702

703+
add_header_macro(
704+
locale
705+
../libc/newhdrgen/yaml/locale.yaml
706+
locale.h.def
707+
locale.h
708+
DEPENDS
709+
.llvm_libc_common_h
710+
.llvm-libc-macros.locale_macros
711+
.llvm-libc-types.locale_t
712+
.llvm-libc-types.struct_lconv
713+
)
714+
703715
if(LIBC_TARGET_OS_IS_GPU)
704716
file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/gpu)
705717

libc/include/llvm-libc-macros/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,9 @@ add_macro_header(
289289
HDR
290290
dlfcn-macros.h
291291
)
292+
293+
add_macro_header(
294+
locale_macros
295+
HDR
296+
locale-macros.h
297+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===-- Definition of macros from locale.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_MACROS_LOCALE_MACROS_H
10+
#define LLVM_LIBC_MACROS_LOCALE_MACROS_H
11+
12+
#include "../llvm-libc-types/locale_t.h"
13+
14+
#define LC_CTYPE 0
15+
#define LC_NUMERIC 1
16+
#define LC_TIME 2
17+
#define LC_COLLATE 3
18+
#define LC_MONETARY 4
19+
#define LC_MESSAGES 5
20+
#define LC_ALL 6
21+
22+
#define LC_GLOBAL_LOCALE ((locale_t)(-1))
23+
24+
#define LC_CTYPE_MASK (1 << LC_CTYPE)
25+
#define LC_NUMERIC_MASK (1 << LC_NUMERIC)
26+
#define LC_TIME_MASK (1 << LC_TIME)
27+
#define LC_COLLATE_MASK (1 << LC_COLLATE)
28+
#define LC_MONETARY_MASK (1 << LC_MONETARY)
29+
#define LC_MESSAGES_MASK (1 << LC_MESSAGES)
30+
#define LC_ALL_MASK 0x7fffffff
31+
32+
#endif // LLVM_LIBC_MACROS_LOCALE_MACROS_H

libc/include/llvm-libc-types/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,5 @@ DEPENDS
142142
.fsblkcnt_t
143143
.fsfilcnt_t
144144
)
145+
add_header(locale_t HDR locale_t.h)
146+
add_header(struct_lconv HDR struct_lconv.h)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of type locale_t ---------------------------------------===//
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_TYPES_LOCALE_T_H
10+
#define LLVM_LIBC_TYPES_LOCALE_T_H
11+
12+
#define NUM_LOCALE_CATEGORIES 6
13+
14+
struct __locale_data;
15+
16+
struct __locale_t {
17+
struct __locale_data *data[NUM_LOCALE_CATEGORIES];
18+
};
19+
20+
typedef struct __locale_t *locale_t;
21+
22+
#endif // LLVM_LIBC_TYPES_LOCALE_T_H
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- Definition of type lconv ------------------------------------------===//
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_TYPES_LCONV_H
10+
#define LLVM_LIBC_TYPES_LCONV_H
11+
12+
struct lconv {
13+
char *decimal_point;
14+
char *thousands_sep;
15+
char *grouping;
16+
char *mon_decimal_point;
17+
char *mon_thousands_sep;
18+
char *mon_grouping;
19+
char *positive_sign;
20+
char *negative_sign;
21+
char *currency_symbol;
22+
char frac_digits;
23+
char p_cs_precedes;
24+
char n_cs_precedes;
25+
char p_sep_by_space;
26+
char n_sep_by_space;
27+
char p_sign_posn;
28+
char n_sign_posn;
29+
char *int_curr_symbol;
30+
char int_frac_digits;
31+
char int_p_cs_precedes;
32+
char int_n_cs_precedes;
33+
char int_p_sep_by_space;
34+
char int_n_sep_by_space;
35+
char int_p_sign_posn;
36+
char int_n_sign_posn;
37+
};
38+
39+
#endif // LLVM_LIBC_TYPES_LCONV_H

libc/include/locale.h.def

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- C standard library header locale.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_LOCALE_H
10+
#define LLVM_LIBC_LOCALE_H
11+
12+
#include "__llvm-libc-common.h"
13+
14+
#include "llvm-libc-macros/locale-macros.h"
15+
#include "llvm-libc-types/locale_t.h"
16+
#include "llvm-libc-types/struct_lconv.h"
17+
18+
%%public_api()
19+
20+
#endif // LLVM_LIBC_LOCALE_H

libc/newhdrgen/yaml/locale.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
header: locale.h
2+
functions:
3+
- name: localeconv
4+
standards:
5+
- stdc
6+
return_type: struct lconv *
7+
arguments:
8+
- type: void
9+
- name: duplocale
10+
standards:
11+
- stdc
12+
return_type: locale_t
13+
arguments:
14+
- type: locale_t
15+
- name: freelocale
16+
standards:
17+
- stdc
18+
return_type: void
19+
arguments:
20+
- type: locale_t
21+
- name: newlocale
22+
standards:
23+
- stdc
24+
return_type: locale_t
25+
arguments:
26+
- type: int
27+
- type: const char *
28+
- type: locale_t
29+
- name: setlocale
30+
standards:
31+
- stdc
32+
return_type: char *
33+
arguments:
34+
- type: int
35+
- type: const char *
36+
- name: uselocale
37+
standards:
38+
- stdc
39+
return_type: locale_t
40+
arguments:
41+
- type: locale_t

libc/spec/stdc.td

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ def StdC : StandardSpec<"stdc"> {
44
PtrType StructTmPtr = PtrType<StructTmType>;
55
PtrType TimeTTypePtr = PtrType<TimeTType>;
66
NamedType ClockT = NamedType<"clock_t">;
7+
NamedType LocaleT = NamedType<"locale_t">;
78

89
NamedType DivTType = NamedType<"div_t">;
910
NamedType LDivTType = NamedType<"ldiv_t">;
@@ -1587,6 +1588,61 @@ def StdC : StandardSpec<"stdc"> {
15871588
]
15881589
>;
15891590

1591+
1592+
NamedType StructLconv : NamedType<"struct lconv">;
1593+
PtrType StructLconvPtr : PtrType<StructLconv>;
1594+
1595+
HeaderSpec Locale = HeaderSpec<
1596+
"locale.h",
1597+
[], // Macros
1598+
[LocaleT, StructLconv], // Types
1599+
[], // Enumerations
1600+
[
1601+
FunctionSpec<
1602+
"duplocale",
1603+
RetValSpec<LocaleT>,
1604+
[
1605+
ArgSpec<LocaleT>
1606+
]
1607+
>,
1608+
FunctionSpec<
1609+
"freelocale",
1610+
RetValSpec<VoidType>,
1611+
[
1612+
ArgSpec<LocaleT>
1613+
]
1614+
>,
1615+
FunctionSpec<
1616+
"localeconv",
1617+
RetValSpec<StructLconvPtr>,
1618+
[]
1619+
>,
1620+
FunctionSpec<
1621+
"newlocale",
1622+
RetValSpec<LocaleT>,
1623+
[
1624+
ArgSpec<IntType>,
1625+
ArgSpec<ConstCharPtr>,
1626+
ArgSpec<LocaleT>
1627+
]
1628+
>,
1629+
FunctionSpec<
1630+
"setlocale",
1631+
RetValSpec<CharPtr>,
1632+
[
1633+
ArgSpec<IntType>,
1634+
ArgSpec<ConstCharPtr>
1635+
]
1636+
>,
1637+
FunctionSpec<
1638+
"uselocale",
1639+
RetValSpec<LocaleT>,
1640+
[
1641+
ArgSpec<LocaleT>
1642+
]
1643+
>
1644+
] // Functions
1645+
>;
15901646

15911647
let Headers = [
15921648
Assert,
@@ -1609,5 +1665,6 @@ def StdC : StandardSpec<"stdc"> {
16091665
Time,
16101666
UChar,
16111667
WChar,
1668+
Locale,
16121669
];
16131670
}

libc/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ add_subdirectory(signal)
4040
add_subdirectory(spawn)
4141
add_subdirectory(threads)
4242
add_subdirectory(time)
43+
add_subdirectory(locale)

0 commit comments

Comments
 (0)