Skip to content

[libc] Implement 'vfscanf' and 'vscanf' #105293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,12 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.fileno
libc.src.stdio.fprintf
libc.src.stdio.fscanf
libc.src.stdio.vfscanf
libc.src.stdio.printf
libc.src.stdio.remove
libc.src.stdio.rename
libc.src.stdio.scanf
libc.src.stdio.vscanf
libc.src.stdio.snprintf
libc.src.stdio.sprintf
libc.src.stdio.asprintf
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,12 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.fileno
libc.src.stdio.fprintf
libc.src.stdio.fscanf
libc.src.stdio.vfscanf
libc.src.stdio.printf
libc.src.stdio.remove
libc.src.stdio.rename
libc.src.stdio.scanf
libc.src.stdio.vscanf
libc.src.stdio.snprintf
libc.src.stdio.sprintf
libc.src.stdio.asprintf
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,12 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.fileno
libc.src.stdio.fprintf
libc.src.stdio.fscanf
libc.src.stdio.vfscanf
libc.src.stdio.printf
libc.src.stdio.remove
libc.src.stdio.rename
libc.src.stdio.scanf
libc.src.stdio.vscanf
libc.src.stdio.snprintf
libc.src.stdio.sprintf
libc.src.stdio.asprintf
Expand Down
15 changes: 15 additions & 0 deletions libc/newhdrgen/yaml/stdio.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ functions:
- type: FILE *__restrict
- type: const char *__restrict
- type: '...'
- name: vfscanf
standards:
- stdc
return_type: int
arguments:
- type: FILE *__restrict
- type: const char *__restrict
- type: va_list
- name: fseek
standards:
- stdc
Expand Down Expand Up @@ -284,6 +292,13 @@ functions:
arguments:
- type: const char *__restrict
- type: '...'
- name: vscanf
standards:
- stdc
return_type: int
arguments:
- type: const char *__restrict
- type: va_list
- name: setbuf
standards:
- stdc
Expand Down
13 changes: 13 additions & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -1042,13 +1042,26 @@ def StdC : StandardSpec<"stdc"> {
[ArgSpec<ConstCharRestrictedPtr>,
ArgSpec<VarArgType>]
>,
FunctionSpec<
"vscanf",
RetValSpec<IntType>,
[ArgSpec<ConstCharRestrictedPtr>,
ArgSpec<VaListType>]
>,
FunctionSpec<
"fscanf",
RetValSpec<IntType>,
[ArgSpec<FILERestrictedPtr>,
ArgSpec<ConstCharRestrictedPtr>,
ArgSpec<VarArgType>]
>,
FunctionSpec<
"vfscanf",
RetValSpec<IntType>,
[ArgSpec<FILERestrictedPtr>,
ArgSpec<ConstCharRestrictedPtr>,
ArgSpec<VaListType>]
>,
FunctionSpec<
"sprintf",
RetValSpec<IntType>,
Expand Down
20 changes: 20 additions & 0 deletions libc/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ add_entrypoint_object(
${scanf_deps}
)

add_entrypoint_object(
vfscanf
SRCS
vfscanf.cpp
HDRS
vfscanf.h
DEPENDS
${scanf_deps}
)

add_entrypoint_object(
scanf
SRCS
Expand All @@ -153,6 +163,16 @@ add_entrypoint_object(
${scanf_deps}
)

add_entrypoint_object(
vscanf
SRCS
vscanf.cpp
HDRS
vscanf.h
DEPENDS
${scanf_deps}
)

add_entrypoint_object(
sprintf
SRCS
Expand Down
34 changes: 34 additions & 0 deletions libc/src/stdio/vfscanf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===-- Implementation of vfscanf -------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdio/vfscanf.h"

#include "src/__support/File/file.h"
#include "src/__support/arg_list.h"
#include "src/__support/macros/config.h"
#include "src/stdio/scanf_core/vfscanf_internal.h"

#include "hdr/types/FILE.h"
#include <stdarg.h>

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, vfscanf,
(::FILE *__restrict stream, const char *__restrict format,
va_list vlist)) {
internal::ArgList args(vlist); // This holder class allows for easier copying
// and pointer semantics, as well as handling
// destruction automatically.
va_end(vlist);
int ret_val = scanf_core::vfscanf_internal(stream, format, args);
// This is done to avoid including stdio.h in the internals. On most systems
// EOF is -1, so this will be transformed into just "return ret_val".
return (ret_val == -1) ? EOF : ret_val;
}

} // namespace LIBC_NAMESPACE_DECL
24 changes: 24 additions & 0 deletions libc/src/stdio/vfscanf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- Implementation header of vfscanf ------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDIO_VFSCANF_H
#define LLVM_LIBC_SRC_STDIO_VFSCANF_H

#include "hdr/types/FILE.h"
#include "src/__support/macros/config.h"

#include <stdarg.h>

namespace LIBC_NAMESPACE_DECL {

int vfscanf(::FILE *__restrict stream, const char *__restrict format,
va_list vlist);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STDIO_VFSCANF_H
40 changes: 40 additions & 0 deletions libc/src/stdio/vscanf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===-- Implementation of vscanf --------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdio/vscanf.h"

#include "src/__support/File/file.h"
#include "src/__support/arg_list.h"
#include "src/__support/macros/config.h"
#include "src/stdio/scanf_core/vfscanf_internal.h"

#include "hdr/types/FILE.h"
#include <stdarg.h>

#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
#define SCANF_STDIN LIBC_NAMESPACE::stdin
#else // LIBC_COPT_STDIO_USE_SYSTEM_FILE
#define SCANF_STDIN ::stdin
#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, vscanf,
(const char *__restrict format, va_list vlist)) {
internal::ArgList args(vlist); // This holder class allows for easier copying
// and pointer semantics, as well as handling
// destruction automatically.
va_end(vlist);
int ret_val = scanf_core::vfscanf_internal(
reinterpret_cast<::FILE *>(SCANF_STDIN), format, args);
// This is done to avoid including stdio.h in the internals. On most systems
// EOF is -1, so this will be transformed into just "return ret_val".
return (ret_val == -1) ? EOF : ret_val;
}

} // namespace LIBC_NAMESPACE_DECL
23 changes: 23 additions & 0 deletions libc/src/stdio/vscanf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Implementation header of vscanf -------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDIO_VSCANF_H
#define LLVM_LIBC_SRC_STDIO_VSCANF_H

#include "hdr/types/FILE.h"
#include "src/__support/macros/config.h"

#include <stdarg.h>

namespace LIBC_NAMESPACE_DECL {

int vscanf(const char *__restrict format, va_list vlist);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STDIO_VSCANF_H
14 changes: 14 additions & 0 deletions libc/test/src/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,20 @@ add_libc_test(
${use_system_file}
)

add_libc_test(
vfscanf_test
SUITE
libc_stdio_unittests
SRCS
vfscanf_test.cpp
DEPENDS
libc.src.stdio.vfscanf
${fscanf_test_deps}
libc.src.__support.CPP.string_view
COMPILE_OPTIONS
${use_system_file}
)

if(LIBC_CONF_SCANF_DISABLE_FLOAT)
list(APPEND sscanf_test_copts "-DLIBC_COPT_SCANF_DISABLE_FLOAT")
endif()
Expand Down
98 changes: 98 additions & 0 deletions libc/test/src/stdio/vfscanf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//===-- Unittests for vfscanf ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/string_view.h"

#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
#include "src/stdio/fclose.h"
#include "src/stdio/ferror.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fwrite.h"
#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE

#include "src/stdio/vfscanf.h"

#include "test/UnitTest/Test.h"

#include <stdio.h>

namespace scanf_test {
#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
using LIBC_NAMESPACE::fclose;
using LIBC_NAMESPACE::ferror;
using LIBC_NAMESPACE::fopen;
using LIBC_NAMESPACE::fwrite;
#else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)
using ::fclose;
using ::ferror;
using ::fopen;
using ::fwrite;
#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
} // namespace scanf_test

static int call_vfscanf(::FILE *stream, const char *__restrict format, ...) {
va_list vlist;
va_start(vlist, format);
int ret = LIBC_NAMESPACE::vfscanf(stream, format, vlist);
va_end(vlist);
return ret;
}

TEST(LlvmLibcFScanfTest, WriteToFile) {
const char *FILENAME = "fscanf_output.test";
auto FILE_PATH = libc_make_test_file_path(FILENAME);
::FILE *file = scanf_test::fopen(FILE_PATH, "w");
ASSERT_FALSE(file == nullptr);

int read;

constexpr char simple[] = "A simple string with no conversions.\n";

ASSERT_EQ(sizeof(simple) - 1,
scanf_test::fwrite(simple, 1, sizeof(simple) - 1, file));

constexpr char numbers[] = "1234567890\n";

ASSERT_EQ(sizeof(numbers) - 1,
scanf_test::fwrite(numbers, 1, sizeof(numbers) - 1, file));

constexpr char numbers_and_more[] = "1234 and more\n";

ASSERT_EQ(sizeof(numbers_and_more) - 1,
scanf_test::fwrite(numbers_and_more, 1,
sizeof(numbers_and_more) - 1, file));

read = call_vfscanf(file, "Reading from a write-only file should fail.");
EXPECT_LT(read, 0);

ASSERT_EQ(0, scanf_test::fclose(file));

file = scanf_test::fopen(FILE_PATH, "r");
ASSERT_FALSE(file == nullptr);

char data[50];
read = call_vfscanf(file, "%[A-Za-z .\n]", data);
ASSERT_EQ(read, 1);
ASSERT_STREQ(simple, data);

read = call_vfscanf(file, "%s", data);
ASSERT_EQ(read, 1);
ASSERT_EQ(LIBC_NAMESPACE::cpp::string_view(numbers, 10),
LIBC_NAMESPACE::cpp::string_view(data));

// The format string starts with a space to handle the fact that the %s leaves
// a trailing \n and %c doesn't strip leading whitespace.
read = call_vfscanf(file, " %50c", data);
ASSERT_EQ(read, 1);
ASSERT_EQ(
LIBC_NAMESPACE::cpp::string_view(numbers_and_more),
LIBC_NAMESPACE::cpp::string_view(data, sizeof(numbers_and_more) - 1));

ASSERT_EQ(scanf_test::ferror(file), 0);
ASSERT_EQ(scanf_test::fclose(file), 0);
}
Loading