Skip to content

Commit da61dd2

Browse files
authored
[libc] Move mbtowc, mbstowcs and inverse functions to stdlib.h (#168455)
These functions should be declared in `stdlib.h`, not `wchar.h`, as confusing as it is. Move them to the proper header file and matching directories in src/ and test/ trees. This was discovered while testing libc++ build against llvm-libc, which re-declares functions like mbtowc in std-namespace in `<cstdlib>` header, and then uses those functions in its locale implementation.
1 parent b48f293 commit da61dd2

File tree

19 files changed

+165
-164
lines changed

19 files changed

+165
-164
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,11 @@ if(LLVM_LIBC_FULL_BUILD)
12541254
libc.src.stdlib.atexit
12551255
libc.src.stdlib.exit
12561256
libc.src.stdlib.getenv
1257+
libc.src.stdlib.mbstowcs
1258+
libc.src.stdlib.mbtowc
12571259
libc.src.stdlib.quick_exit
1260+
libc.src.stdlib.wcstombs
1261+
libc.src.stdlib.wctomb
12581262

12591263
# signal.h entrypoints
12601264
libc.src.signal.kill
@@ -1372,13 +1376,9 @@ if(LLVM_LIBC_FULL_BUILD)
13721376
libc.src.wchar.mbrlen
13731377
libc.src.wchar.mbsinit
13741378
libc.src.wchar.mbrtowc
1375-
libc.src.wchar.mbtowc
1376-
libc.src.wchar.mbstowcs
13771379
libc.src.wchar.mbsrtowcs
13781380
libc.src.wchar.mbsnrtowcs
13791381
libc.src.wchar.wcrtomb
1380-
libc.src.wchar.wctomb
1381-
libc.src.wchar.wcstombs
13821382
libc.src.wchar.wcsrtombs
13831383
libc.src.wchar.wcsnrtombs
13841384

libc/include/stdlib.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ types:
1717
- type_name: lldiv_t
1818
- type_name: locale_t
1919
- type_name: size_t
20+
- type_name: wchar_t
2021
enums: []
2122
objects: []
2223
functions:
@@ -135,6 +136,22 @@ functions:
135136
arguments:
136137
- type: long long
137138
- type: long long
139+
- name: mbstowcs
140+
standards:
141+
- stdc
142+
return_type: size_t
143+
arguments:
144+
- type: wchar_t *__restrict
145+
- type: const char *__restrict
146+
- type: size_t
147+
- name: mbtowc
148+
standards:
149+
- stdc
150+
return_type: int
151+
arguments:
152+
- type: wchar_t *__restrict
153+
- type: const char *__restrict
154+
- type: size_t
138155
- name: memalignment
139156
standards:
140157
- stdc
@@ -332,3 +349,18 @@ functions:
332349
return_type: int
333350
arguments:
334351
- type: const char *
352+
- name: wctomb
353+
standards:
354+
- stdc
355+
return_type: int
356+
arguments:
357+
- type: char *
358+
- type: wchar_t
359+
- name: wcstombs
360+
standards:
361+
- stdc
362+
return_type: size_t
363+
arguments:
364+
- type: char *__restrict
365+
- type: const wchar_t *__restrict
366+
- type: size_t

libc/include/wchar.yaml

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,6 @@ functions:
5050
- type: const char *__restrict
5151
- type: size_t
5252
- type: mbstate_t *__restrict
53-
- name: mbtowc
54-
standards:
55-
- stdc
56-
return_type: int
57-
arguments:
58-
- type: wchar_t *__restrict
59-
- type: const char *__restrict
60-
- type: size_t
6153
- name: mbsnrtowcs
6254
standards:
6355
- stdc
@@ -77,14 +69,6 @@ functions:
7769
- type: const char **__restrict
7870
- type: size_t
7971
- type: mbstate_t *__restrict
80-
- name: mbstowcs
81-
standards:
82-
- stdc
83-
return_type: size_t
84-
arguments:
85-
- type: wchar_t *__restrict
86-
- type: const char *__restrict
87-
- type: size_t
8872
- name: mbsinit
8973
standards:
9074
- stdc
@@ -269,13 +253,6 @@ functions:
269253
- type: char *__restrict
270254
- type: wchar_t
271255
- type: mbstate_t *__restrict
272-
- name: wctomb
273-
standards:
274-
- stdc
275-
return_type: int
276-
arguments:
277-
- type: char *
278-
- type: wchar_t
279256
- name: wcscpy
280257
standards:
281258
- stdc
@@ -336,14 +313,6 @@ functions:
336313
- type: const wchar_t *__restrict
337314
- type: wchar_t **__restrict
338315
- type: int
339-
- name: wcstombs
340-
standards:
341-
- stdc
342-
return_type: size_t
343-
arguments:
344-
- type: char *__restrict
345-
- type: const wchar_t *__restrict
346-
- type: size_t
347316
- name: wcstoul
348317
standards:
349318
- stdc

libc/src/stdlib/CMakeLists.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,65 @@ add_entrypoint_object(
368368
libc.hdr.types.size_t
369369
)
370370

371+
add_entrypoint_object(
372+
mbtowc
373+
SRCS
374+
mbtowc.cpp
375+
HDRS
376+
mbtowc.h
377+
DEPENDS
378+
libc.hdr.types.size_t
379+
libc.hdr.types.wchar_t
380+
libc.src.__support.common
381+
libc.src.__support.macros.config
382+
libc.src.__support.libc_errno
383+
libc.src.__support.wchar.mbrtowc
384+
libc.src.__support.wchar.mbstate
385+
)
386+
387+
add_entrypoint_object(
388+
mbstowcs
389+
SRCS
390+
mbstowcs.cpp
391+
HDRS
392+
mbstowcs.h
393+
DEPENDS
394+
libc.hdr.types.size_t
395+
libc.hdr.types.wchar_t
396+
libc.src.__support.common
397+
libc.src.__support.macros.config
398+
libc.src.__support.macros.null_check
399+
libc.src.__support.libc_errno
400+
libc.src.__support.wchar.mbstate
401+
libc.src.__support.wchar.mbsnrtowcs
402+
)
403+
404+
add_entrypoint_object(
405+
wctomb
406+
SRCS
407+
wctomb.cpp
408+
HDRS
409+
wctomb.h
410+
DEPENDS
411+
libc.hdr.types.wchar_t
412+
libc.src.__support.wchar.wcrtomb
413+
libc.src.__support.wchar.mbstate
414+
libc.src.__support.libc_errno
415+
)
416+
417+
add_entrypoint_object(
418+
wcstombs
419+
SRCS
420+
wcstombs.cpp
421+
HDRS
422+
wcstombs.h
423+
DEPENDS
424+
libc.hdr.types.wchar_t
425+
libc.src.__support.wchar.mbstate
426+
libc.src.__support.wchar.wcsnrtombs
427+
libc.src.__support.libc_errno
428+
)
429+
371430
if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
372431
if(LLVM_LIBC_INCLUDE_SCUDO)
373432
set(SCUDO_DEPS "")

libc/src/wchar/mbstowcs.cpp renamed to libc/src/stdlib/mbstowcs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/wchar/mbstowcs.h"
9+
#include "src/stdlib/mbstowcs.h"
1010

1111
#include "hdr/types/size_t.h"
1212
#include "hdr/types/wchar_t.h"

libc/src/wchar/mbstowcs.h renamed to libc/src/stdlib/mbstowcs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_LIBC_SRC_WCHAR_MBSTOWCS_H
10-
#define LLVM_LIBC_SRC_WCHAR_MBSTOWCS_H
9+
#ifndef LLVM_LIBC_SRC_STDLIB_MBSTOWCS_H
10+
#define LLVM_LIBC_SRC_STDLIB_MBSTOWCS_H
1111

1212
#include "hdr/types/size_t.h"
1313
#include "hdr/types/wchar_t.h"
@@ -19,4 +19,4 @@ size_t mbstowcs(wchar_t *__restrict pwcs, const char *__restrict s, size_t n);
1919

2020
} // namespace LIBC_NAMESPACE_DECL
2121

22-
#endif // LLVM_LIBC_SRC_WCHAR_MBSTOWCS_H
22+
#endif // LLVM_LIBC_SRC_STDLIB_MBSTOWCS_H

libc/src/wchar/mbtowc.cpp renamed to libc/src/stdlib/mbtowc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/wchar/mbtowc.h"
9+
#include "src/stdlib/mbtowc.h"
1010

1111
#include "hdr/types/size_t.h"
1212
#include "hdr/types/wchar_t.h"

libc/src/wchar/mbtowc.h renamed to libc/src/stdlib/mbtowc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_LIBC_SRC_WCHAR_MBTOWC_H
10-
#define LLVM_LIBC_SRC_WCHAR_MBTOWC_H
9+
#ifndef LLVM_LIBC_SRC_STDLIB_MBTOWC_H
10+
#define LLVM_LIBC_SRC_STDLIB_MBTOWC_H
1111

1212
#include "hdr/types/size_t.h"
1313
#include "hdr/types/wchar_t.h"
@@ -19,4 +19,4 @@ int mbtowc(wchar_t *__restrict pwc, const char *__restrict s, size_t n);
1919

2020
} // namespace LIBC_NAMESPACE_DECL
2121

22-
#endif // LLVM_LIBC_SRC_WCHAR_MBTOWC_H
22+
#endif // LLVM_LIBC_SRC_STDLIB_MBTOWC_H

libc/src/wchar/wcstombs.cpp renamed to libc/src/stdlib/wcstombs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/wchar/wcstombs.h"
9+
#include "src/stdlib/wcstombs.h"
1010

1111
#include "hdr/types/char32_t.h"
1212
#include "hdr/types/size_t.h"

libc/src/wchar/wcstombs.h renamed to libc/src/stdlib/wcstombs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_LIBC_SRC_WCHAR_WCSTOMBS_H
10-
#define LLVM_LIBC_SRC_WCHAR_WCSTOMBS_H
9+
#ifndef LLVM_LIBC_SRC_STDLIB_WCSTOMBS_H
10+
#define LLVM_LIBC_SRC_STDLIB_WCSTOMBS_H
1111

1212
#include "hdr/types/size_t.h"
1313
#include "hdr/types/wchar_t.h"
@@ -19,4 +19,4 @@ size_t wcstombs(char *__restrict s, const wchar_t *__restrict pwcs, size_t n);
1919

2020
} // namespace LIBC_NAMESPACE_DECL
2121

22-
#endif // LLVM_LIBC_SRC_WCHAR_WCSTOMBS_H
22+
#endif // LLVM_LIBC_SRC_STDLIB_WCSTOMBS_H

0 commit comments

Comments
 (0)