Skip to content

Commit a5e67fb

Browse files
[libc] Implement vasprintf and asprintf (llvm#98824)
[libc] Implement vasprintf and asprintf --------- Co-authored-by: Izaak Schroeder <[email protected]>
1 parent 2a5f7e5 commit a5e67fb

File tree

20 files changed

+515
-39
lines changed

20 files changed

+515
-39
lines changed

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ set(TARGET_LIBC_ENTRYPOINTS
9090
libc.src.stdio.remove
9191
libc.src.stdio.snprintf
9292
libc.src.stdio.sprintf
93+
libc.src.stdio.asprintf
9394
libc.src.stdio.vprintf
9495
libc.src.stdio.vsnprintf
9596
libc.src.stdio.vsprintf
97+
libc.src.stdio.vasprintf
9698

9799
# stdbit.h entrypoints
98100
libc.src.stdbit.stdc_bit_ceil_uc

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ set(TARGET_LIBC_ENTRYPOINTS
8686
libc.src.stdio.remove
8787
libc.src.stdio.snprintf
8888
libc.src.stdio.sprintf
89+
libc.src.stdio.asprintf
8990
libc.src.stdio.vprintf
9091
libc.src.stdio.vsnprintf
9192
libc.src.stdio.vsprintf
93+
libc.src.stdio.vasprintf
9294

9395
# stdbit.h entrypoints
9496
libc.src.stdbit.stdc_bit_ceil_uc

libc/config/darwin/arm/entrypoints.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ set(TARGET_LIBC_ENTRYPOINTS
9494
libc.src.stdlib.calloc
9595
libc.src.stdlib.realloc
9696
libc.src.stdlib.free
97+
98+
# stdio.h external entrypoints
99+
libc.src.stdio.snprintf
100+
libc.src.stdio.sprintf
101+
libc.src.stdio.asprintf
102+
libc.src.stdio.asprintf
103+
libc.src.stdio.vprintf
104+
libc.src.stdio.vsnprintf
105+
libc.src.stdio.vsprintf
106+
libc.src.stdio.vasprintf
97107
)
98108

99109
set(TARGET_LIBM_ENTRYPOINTS

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,12 @@ set(TARGET_LIBC_ENTRYPOINTS
207207
libc.src.stdio.rename
208208
libc.src.stdio.snprintf
209209
libc.src.stdio.sprintf
210+
libc.src.stdio.asprintf
210211
#libc.src.stdio.scanf
211212
#libc.src.stdio.sscanf
212213
libc.src.stdio.vsnprintf
213214
libc.src.stdio.vsprintf
215+
libc.src.stdio.vasprintf
214216

215217
# sys/mman.h entrypoints
216218
libc.src.sys.mman.madvise

libc/config/linux/riscv/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,14 @@ set(TARGET_LIBC_ENTRYPOINTS
216216
libc.src.stdio.scanf
217217
libc.src.stdio.snprintf
218218
libc.src.stdio.sprintf
219+
libc.src.stdio.asprintf
219220
libc.src.stdio.sscanf
220221
libc.src.stdio.vsscanf
221222
libc.src.stdio.vfprintf
222223
libc.src.stdio.vprintf
223224
libc.src.stdio.vsnprintf
224225
libc.src.stdio.vsprintf
226+
libc.src.stdio.vasprintf
225227

226228
# sys/epoll.h entrypoints
227229
libc.src.sys.epoll.epoll_create

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,14 @@ set(TARGET_LIBC_ENTRYPOINTS
216216
libc.src.stdio.scanf
217217
libc.src.stdio.snprintf
218218
libc.src.stdio.sprintf
219+
libc.src.stdio.asprintf
219220
libc.src.stdio.sscanf
220221
libc.src.stdio.vsscanf
221222
libc.src.stdio.vfprintf
222223
libc.src.stdio.vprintf
223224
libc.src.stdio.vsnprintf
224225
libc.src.stdio.vsprintf
226+
libc.src.stdio.vasprintf
225227

226228
# sys/epoll.h entrypoints
227229
libc.src.sys.epoll.epoll_create

libc/newhdrgen/yaml/stdio.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ functions:
9797
arguments:
9898
- type: const char *__restrict
9999
- type: va_list
100+
- name: asprintf
101+
standards:
102+
- GNUExtensions
103+
return_type: int
104+
arguments:
105+
- type: char **__restrict
106+
- type: const char *__restrict
107+
- type: ...
108+
- name: vasprintf
109+
standards:
110+
- GNUExtensions
111+
return_type: int
112+
arguments:
113+
- type: char **__restrict
114+
- type: const char *__restrict
115+
- type: va_list
100116
- name: sscanf
101117
standards:
102118
- stdc

libc/spec/stdc.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,13 @@ def StdC : StandardSpec<"stdc"> {
971971
ArgSpec<ConstCharRestrictedPtr>,
972972
ArgSpec<VarArgType>]
973973
>,
974+
FunctionSpec<
975+
"asprintf",
976+
RetValSpec<IntType>,
977+
[ArgSpec<CharRestrictedPtrPtr>,
978+
ArgSpec<ConstCharRestrictedPtr>,
979+
ArgSpec<VarArgType>]
980+
>,
974981
FunctionSpec<
975982
"vsprintf",
976983
RetValSpec<IntType>,
@@ -1004,6 +1011,13 @@ def StdC : StandardSpec<"stdc"> {
10041011
RetValSpec<IntType>,
10051012
[ArgSpec<IntType>, ArgSpec<FILEPtr>]
10061013
>,
1014+
FunctionSpec<
1015+
"vasprintf",
1016+
RetValSpec<IntType>,
1017+
[ArgSpec<CharRestrictedPtrPtr>,
1018+
ArgSpec<ConstCharRestrictedPtr>,
1019+
ArgSpec<VaListType>]
1020+
>,
10071021
],
10081022
[
10091023
ObjectSpec<

libc/src/stdio/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ add_entrypoint_object(
175175
libc.src.stdio.printf_core.writer
176176
)
177177

178+
add_entrypoint_object(
179+
asprintf
180+
SRCS
181+
asprintf.cpp
182+
HDRS
183+
asprintf.h
184+
DEPENDS
185+
libc.src.stdio.printf_core.vasprintf_internal
186+
)
187+
178188
add_entrypoint_object(
179189
vsprintf
180190
SRCS
@@ -197,6 +207,16 @@ add_entrypoint_object(
197207
libc.src.stdio.printf_core.writer
198208
)
199209

210+
add_entrypoint_object(
211+
vasprintf
212+
SRCS
213+
vasprintf.cpp
214+
HDRS
215+
vasprintf.h
216+
DEPENDS
217+
libc.src.stdio.printf_core.vasprintf_internal
218+
)
219+
200220
add_subdirectory(printf_core)
201221
add_subdirectory(scanf_core)
202222

libc/src/stdio/asprintf.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Implementation of asprintf -----------------------------*- 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+
#include "src/stdio/asprintf.h"
10+
#include "src/__support/arg_list.h"
11+
#include "src/__support/macros/config.h"
12+
#include "src/stdio/printf_core/vasprintf_internal.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(int, asprintf,
17+
(char **__restrict buffer, const char *format, ...)) {
18+
va_list vlist;
19+
va_start(vlist, format);
20+
internal::ArgList args(vlist); // This holder class allows for easier copying
21+
// and pointer semantics, as well as handling
22+
// destruction automatically.
23+
va_end(vlist);
24+
int ret = printf_core::vasprintf_internal(buffer, format, args);
25+
return ret;
26+
}
27+
28+
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)